2 * Persistent Memory Driver
4 * Copyright (c) 2014-2015, Intel Corporation.
5 * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
6 * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 #include <asm/cacheflush.h>
19 #include <linux/blkdev.h>
20 #include <linux/hdreg.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/badblocks.h>
26 #include <linux/memremap.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pfn_t.h>
29 #include <linux/slab.h>
30 #include <linux/pmem.h>
36 static struct device *to_dev(struct pmem_device *pmem)
39 * nvdimm bus services need a 'dev' parameter, and we record the device
45 static struct nd_region *to_region(struct pmem_device *pmem)
47 return to_nd_region(to_dev(pmem)->parent);
50 static void pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
53 struct device *dev = to_dev(pmem);
54 sector_t sector, cleared;
56 sector = (offset - pmem->data_offset) / 512;
57 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len) / 512;
60 dev_dbg(dev, "%s: %#llx clear %ld sector%s\n",
61 __func__, (unsigned long long) sector,
62 cleared, cleared > 1 ? "s" : "");
63 badblocks_clear(&pmem->bb, sector, cleared);
65 invalidate_pmem(pmem->virt_addr + offset, len);
68 static void write_pmem(void *pmem_addr, struct page *page,
69 unsigned int off, unsigned int len)
71 void *mem = kmap_atomic(page);
73 memcpy_to_pmem(pmem_addr, mem + off, len);
77 static int read_pmem(struct page *page, unsigned int off,
78 void *pmem_addr, unsigned int len)
81 void *mem = kmap_atomic(page);
83 rc = memcpy_from_pmem(mem + off, pmem_addr, len);
88 static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
89 unsigned int len, unsigned int off, bool is_write,
93 bool bad_pmem = false;
94 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
95 void *pmem_addr = pmem->virt_addr + pmem_off;
97 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
101 if (unlikely(bad_pmem))
104 rc = read_pmem(page, off, pmem_addr, len);
105 flush_dcache_page(page);
109 * Note that we write the data both before and after
110 * clearing poison. The write before clear poison
111 * handles situations where the latest written data is
112 * preserved and the clear poison operation simply marks
113 * the address range as valid without changing the data.
114 * In this case application software can assume that an
115 * interrupted write will either return the new good
118 * However, if pmem_clear_poison() leaves the data in an
119 * indeterminate state we need to perform the write
120 * after clear poison.
122 flush_dcache_page(page);
123 write_pmem(pmem_addr, page, off, len);
124 if (unlikely(bad_pmem)) {
125 pmem_clear_poison(pmem, pmem_off, len);
126 write_pmem(pmem_addr, page, off, len);
133 /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
135 #define REQ_FLUSH REQ_PREFLUSH
138 static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
144 struct bvec_iter iter;
145 struct pmem_device *pmem = q->queuedata;
146 struct nd_region *nd_region = to_region(pmem);
148 if (bio->bi_opf & REQ_FLUSH)
149 nvdimm_flush(nd_region);
151 do_acct = nd_iostat_start(bio, &start);
152 bio_for_each_segment(bvec, bio, iter) {
153 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
154 bvec.bv_offset, op_is_write(bio_op(bio)),
162 nd_iostat_end(bio, start);
164 if (bio->bi_opf & REQ_FUA)
165 nvdimm_flush(nd_region);
168 return BLK_QC_T_NONE;
171 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
172 struct page *page, bool is_write)
174 struct pmem_device *pmem = bdev->bd_queue->queuedata;
177 rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
180 * The ->rw_page interface is subtle and tricky. The core
181 * retries on any error, so we can only invoke page_endio() in
182 * the successful completion case. Otherwise, we'll see crashes
183 * caused by double completion.
186 page_endio(page, is_write, 0);
191 /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
192 __weak long pmem_direct_access(struct block_device *bdev, sector_t sector,
193 void **kaddr, pfn_t *pfn, long size)
195 struct pmem_device *pmem = bdev->bd_queue->queuedata;
196 resource_size_t offset = sector * 512 + pmem->data_offset;
198 if (unlikely(is_bad_pmem(&pmem->bb, sector, size)))
200 *kaddr = pmem->virt_addr + offset;
201 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
204 * If badblocks are present, limit known good range to the
207 if (unlikely(pmem->bb.count))
209 return pmem->size - pmem->pfn_pad - offset;
212 static const struct block_device_operations pmem_fops = {
213 .owner = THIS_MODULE,
214 .rw_page = pmem_rw_page,
215 .direct_access = pmem_direct_access,
216 .revalidate_disk = nvdimm_revalidate_disk,
219 static void pmem_release_queue(void *q)
221 blk_cleanup_queue(q);
224 static void pmem_release_disk(void *disk)
230 static int pmem_attach_disk(struct device *dev,
231 struct nd_namespace_common *ndns)
233 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
234 struct nd_region *nd_region = to_nd_region(dev->parent);
235 struct vmem_altmap __altmap, *altmap = NULL;
236 struct resource *res = &nsio->res;
237 struct nd_pfn *nd_pfn = NULL;
238 int nid = dev_to_node(dev);
239 struct nd_pfn_sb *pfn_sb;
240 struct pmem_device *pmem;
241 struct resource pfn_res;
242 struct request_queue *q;
243 struct gendisk *disk;
246 /* while nsio_rw_bytes is active, parse a pfn info block if present */
247 if (is_nd_pfn(dev)) {
248 nd_pfn = to_nd_pfn(dev);
249 altmap = nvdimm_setup_pfn(nd_pfn, &pfn_res, &__altmap);
251 return PTR_ERR(altmap);
254 /* we're attaching a block device, disable raw namespace access */
255 devm_nsio_disable(dev, nsio);
257 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
261 dev_set_drvdata(dev, pmem);
262 pmem->phys_addr = res->start;
263 pmem->size = resource_size(res);
264 if (nvdimm_has_flush(nd_region) < 0)
265 dev_warn(dev, "unable to guarantee persistence of writes\n");
267 if (!devm_request_mem_region(dev, res->start, resource_size(res),
268 dev_name(&ndns->dev))) {
269 dev_warn(dev, "could not reserve region %pR\n", res);
273 q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
277 pmem->pfn_flags = PFN_DEV;
278 if (is_nd_pfn(dev)) {
279 addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
281 pfn_sb = nd_pfn->pfn_sb;
282 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
283 pmem->pfn_pad = resource_size(res) - resource_size(&pfn_res);
284 pmem->pfn_flags |= PFN_MAP;
285 res = &pfn_res; /* for badblocks populate */
286 res->start += pmem->data_offset;
287 } else if (pmem_should_map_pages(dev)) {
288 addr = devm_memremap_pages(dev, &nsio->res,
289 &q->q_usage_counter, NULL);
290 pmem->pfn_flags |= PFN_MAP;
292 addr = devm_memremap(dev, pmem->phys_addr,
293 pmem->size, ARCH_MEMREMAP_PMEM);
296 * At release time the queue must be dead before
297 * devm_memremap_pages is unwound
299 if (devm_add_action_or_reset(dev, pmem_release_queue, q))
303 return PTR_ERR(addr);
304 pmem->virt_addr = addr;
306 blk_queue_write_cache(q, true, true);
307 blk_queue_make_request(q, pmem_make_request);
308 blk_queue_physical_block_size(q, PAGE_SIZE);
309 blk_queue_max_hw_sectors(q, UINT_MAX);
310 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
311 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
312 queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
315 disk = alloc_disk_node(0, nid);
319 disk->fops = &pmem_fops;
321 disk->flags = GENHD_FL_EXT_DEVT;
322 nvdimm_namespace_disk_name(ndns, disk->disk_name);
323 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
325 if (devm_init_badblocks(dev, &pmem->bb))
327 nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
328 disk->bb = &pmem->bb;
329 device_add_disk(dev, disk);
331 if (devm_add_action_or_reset(dev, pmem_release_disk, disk))
334 revalidate_disk(disk);
339 static int nd_pmem_probe(struct device *dev)
341 struct nd_namespace_common *ndns;
343 ndns = nvdimm_namespace_common_probe(dev);
345 return PTR_ERR(ndns);
347 if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
351 return nvdimm_namespace_attach_btt(ndns);
354 return pmem_attach_disk(dev, ndns);
356 /* if we find a valid info-block we'll come back as that personality */
357 if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
358 || nd_dax_probe(dev, ndns) == 0)
361 /* ...otherwise we're just a raw pmem device */
362 return pmem_attach_disk(dev, ndns);
365 static int nd_pmem_remove(struct device *dev)
368 nvdimm_namespace_detach_btt(to_nd_btt(dev));
369 nvdimm_flush(to_nd_region(dev->parent));
374 static void nd_pmem_shutdown(struct device *dev)
376 nvdimm_flush(to_nd_region(dev->parent));
379 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
381 struct pmem_device *pmem = dev_get_drvdata(dev);
382 struct nd_region *nd_region = to_region(pmem);
383 resource_size_t offset = 0, end_trunc = 0;
384 struct nd_namespace_common *ndns;
385 struct nd_namespace_io *nsio;
388 if (event != NVDIMM_REVALIDATE_POISON)
391 if (is_nd_btt(dev)) {
392 struct nd_btt *nd_btt = to_nd_btt(dev);
395 } else if (is_nd_pfn(dev)) {
396 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
397 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
400 offset = pmem->data_offset + __le32_to_cpu(pfn_sb->start_pad);
401 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
405 nsio = to_nd_namespace_io(&ndns->dev);
406 res.start = nsio->res.start + offset;
407 res.end = nsio->res.end - end_trunc;
408 nvdimm_badblocks_populate(nd_region, &pmem->bb, &res);
411 MODULE_ALIAS("pmem");
412 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
413 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
414 static struct nd_device_driver nd_pmem_driver = {
415 .probe = nd_pmem_probe,
416 .remove = nd_pmem_remove,
417 .notify = nd_pmem_notify,
418 .shutdown = nd_pmem_shutdown,
422 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
425 static int __init pmem_init(void)
427 return nd_driver_register(&nd_pmem_driver);
429 module_init(pmem_init);
431 static void pmem_exit(void)
433 driver_unregister(&nd_pmem_driver.drv);
435 module_exit(pmem_exit);
437 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
438 MODULE_LICENSE("GPL v2");