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/blk-mq.h>
29 #include <linux/pfn_t.h>
30 #include <linux/slab.h>
31 #include <linux/pmem.h>
32 #include <linux/uio.h>
33 #include <linux/dax.h>
39 static struct device *to_dev(struct pmem_device *pmem)
42 * nvdimm bus services need a 'dev' parameter, and we record the device
48 static struct nd_region *to_region(struct pmem_device *pmem)
50 return to_nd_region(to_dev(pmem)->parent);
53 static int pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
56 struct device *dev = to_dev(pmem);
61 sector = (offset - pmem->data_offset) / 512;
63 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
66 if (cleared > 0 && cleared / 512) {
68 dev_dbg(dev, "%s: %#llx clear %ld sector%s\n", __func__,
69 (unsigned long long) sector, cleared,
70 cleared > 1 ? "s" : "");
71 badblocks_clear(&pmem->bb, sector, cleared);
74 arch_invalidate_pmem(pmem->virt_addr + offset, len);
79 static void write_pmem(void *pmem_addr, struct page *page,
80 unsigned int off, unsigned int len)
82 void *mem = kmap_atomic(page);
84 memcpy_flushcache(pmem_addr, mem + off, len);
88 static int read_pmem(struct page *page, unsigned int off,
89 void *pmem_addr, unsigned int len)
92 void *mem = kmap_atomic(page);
94 rc = memcpy_mcsafe(mem + off, pmem_addr, len);
101 static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
102 unsigned int len, unsigned int off, bool is_write,
106 bool bad_pmem = false;
107 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
108 void *pmem_addr = pmem->virt_addr + pmem_off;
110 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
114 if (unlikely(bad_pmem))
117 rc = read_pmem(page, off, pmem_addr, len);
118 flush_dcache_page(page);
122 * Note that we write the data both before and after
123 * clearing poison. The write before clear poison
124 * handles situations where the latest written data is
125 * preserved and the clear poison operation simply marks
126 * the address range as valid without changing the data.
127 * In this case application software can assume that an
128 * interrupted write will either return the new good
131 * However, if pmem_clear_poison() leaves the data in an
132 * indeterminate state we need to perform the write
133 * after clear poison.
135 flush_dcache_page(page);
136 write_pmem(pmem_addr, page, off, len);
137 if (unlikely(bad_pmem)) {
138 rc = pmem_clear_poison(pmem, pmem_off, len);
139 write_pmem(pmem_addr, page, off, len);
146 /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
148 #define REQ_FLUSH REQ_PREFLUSH
151 static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
157 struct bvec_iter iter;
158 struct pmem_device *pmem = q->queuedata;
159 struct nd_region *nd_region = to_region(pmem);
161 if (bio->bi_opf & REQ_FLUSH)
162 nvdimm_flush(nd_region);
164 do_acct = nd_iostat_start(bio, &start);
165 bio_for_each_segment(bvec, bio, iter) {
166 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
167 bvec.bv_offset, op_is_write(bio_op(bio)),
175 nd_iostat_end(bio, start);
177 if (bio->bi_opf & REQ_FUA)
178 nvdimm_flush(nd_region);
181 return BLK_QC_T_NONE;
184 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
185 struct page *page, bool is_write)
187 struct pmem_device *pmem = bdev->bd_queue->queuedata;
190 rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
193 * The ->rw_page interface is subtle and tricky. The core
194 * retries on any error, so we can only invoke page_endio() in
195 * the successful completion case. Otherwise, we'll see crashes
196 * caused by double completion.
199 page_endio(page, is_write, 0);
204 /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
205 __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
206 long nr_pages, void **kaddr, pfn_t *pfn)
208 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
210 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
211 PFN_PHYS(nr_pages))))
213 *kaddr = pmem->virt_addr + offset;
214 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
217 * If badblocks are present, limit known good range to the
220 if (unlikely(pmem->bb.count))
222 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
225 static const struct block_device_operations pmem_fops = {
226 .owner = THIS_MODULE,
227 .rw_page = pmem_rw_page,
228 .revalidate_disk = nvdimm_revalidate_disk,
231 static long pmem_dax_direct_access(struct dax_device *dax_dev,
232 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
234 struct pmem_device *pmem = dax_get_private(dax_dev);
236 return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
239 static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
240 void *addr, size_t bytes, struct iov_iter *i)
242 return copy_from_iter_flushcache(addr, bytes, i);
245 static void pmem_dax_flush(struct dax_device *dax_dev, pgoff_t pgoff,
246 void *addr, size_t size)
248 arch_wb_cache_pmem(addr, size);
251 static const struct dax_operations pmem_dax_ops = {
252 .direct_access = pmem_dax_direct_access,
253 .copy_from_iter = pmem_copy_from_iter,
254 .flush = pmem_dax_flush,
257 static void pmem_release_queue(void *q)
259 blk_cleanup_queue(q);
262 static void pmem_freeze_queue(void *q)
264 blk_freeze_queue_start(q);
267 static void pmem_release_disk(void *__pmem)
269 struct pmem_device *pmem = __pmem;
271 kill_dax(pmem->dax_dev);
272 put_dax(pmem->dax_dev);
273 del_gendisk(pmem->disk);
274 put_disk(pmem->disk);
277 static int pmem_attach_disk(struct device *dev,
278 struct nd_namespace_common *ndns)
280 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
281 struct nd_region *nd_region = to_nd_region(dev->parent);
282 struct vmem_altmap __altmap, *altmap = NULL;
283 struct resource *res = &nsio->res;
284 struct nd_pfn *nd_pfn = NULL;
285 struct dax_device *dax_dev;
286 int nid = dev_to_node(dev);
287 struct nd_pfn_sb *pfn_sb;
288 struct pmem_device *pmem;
289 struct resource pfn_res;
290 struct request_queue *q;
291 struct gendisk *disk;
294 /* while nsio_rw_bytes is active, parse a pfn info block if present */
295 if (is_nd_pfn(dev)) {
296 nd_pfn = to_nd_pfn(dev);
297 altmap = nvdimm_setup_pfn(nd_pfn, &pfn_res, &__altmap);
299 return PTR_ERR(altmap);
302 /* we're attaching a block device, disable raw namespace access */
303 devm_nsio_disable(dev, nsio);
305 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
309 dev_set_drvdata(dev, pmem);
310 pmem->phys_addr = res->start;
311 pmem->size = resource_size(res);
312 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE)
313 || nvdimm_has_flush(nd_region) < 0)
314 dev_warn(dev, "unable to guarantee persistence of writes\n");
316 if (!devm_request_mem_region(dev, res->start, resource_size(res),
317 dev_name(&ndns->dev))) {
318 dev_warn(dev, "could not reserve region %pR\n", res);
322 q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
326 if (devm_add_action_or_reset(dev, pmem_release_queue, q))
329 pmem->pfn_flags = PFN_DEV;
330 if (is_nd_pfn(dev)) {
331 addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
333 pfn_sb = nd_pfn->pfn_sb;
334 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
335 pmem->pfn_pad = resource_size(res) - resource_size(&pfn_res);
336 pmem->pfn_flags |= PFN_MAP;
337 res = &pfn_res; /* for badblocks populate */
338 res->start += pmem->data_offset;
339 } else if (pmem_should_map_pages(dev)) {
340 addr = devm_memremap_pages(dev, &nsio->res,
341 &q->q_usage_counter, NULL);
342 pmem->pfn_flags |= PFN_MAP;
344 addr = devm_memremap(dev, pmem->phys_addr,
345 pmem->size, ARCH_MEMREMAP_PMEM);
348 * At release time the queue must be frozen before
349 * devm_memremap_pages is unwound
351 if (devm_add_action_or_reset(dev, pmem_freeze_queue, q))
355 return PTR_ERR(addr);
356 pmem->virt_addr = addr;
358 blk_queue_write_cache(q, true, true);
359 blk_queue_make_request(q, pmem_make_request);
360 blk_queue_physical_block_size(q, PAGE_SIZE);
361 blk_queue_max_hw_sectors(q, UINT_MAX);
362 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
363 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
364 queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
367 disk = alloc_disk_node(0, nid);
372 disk->fops = &pmem_fops;
374 disk->flags = GENHD_FL_EXT_DEVT;
375 nvdimm_namespace_disk_name(ndns, disk->disk_name);
376 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
378 if (devm_init_badblocks(dev, &pmem->bb))
380 nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
381 disk->bb = &pmem->bb;
383 dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops);
388 pmem->dax_dev = dax_dev;
390 device_add_disk(dev, disk);
391 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
394 revalidate_disk(disk);
399 static int nd_pmem_probe(struct device *dev)
401 struct nd_namespace_common *ndns;
403 ndns = nvdimm_namespace_common_probe(dev);
405 return PTR_ERR(ndns);
407 if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
411 return nvdimm_namespace_attach_btt(ndns);
414 return pmem_attach_disk(dev, ndns);
416 /* if we find a valid info-block we'll come back as that personality */
417 if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
418 || nd_dax_probe(dev, ndns) == 0)
421 /* ...otherwise we're just a raw pmem device */
422 return pmem_attach_disk(dev, ndns);
425 static int nd_pmem_remove(struct device *dev)
428 nvdimm_namespace_detach_btt(to_nd_btt(dev));
429 nvdimm_flush(to_nd_region(dev->parent));
434 static void nd_pmem_shutdown(struct device *dev)
436 nvdimm_flush(to_nd_region(dev->parent));
439 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
441 struct nd_region *nd_region;
442 resource_size_t offset = 0, end_trunc = 0;
443 struct nd_namespace_common *ndns;
444 struct nd_namespace_io *nsio;
446 struct badblocks *bb;
448 if (event != NVDIMM_REVALIDATE_POISON)
451 if (is_nd_btt(dev)) {
452 struct nd_btt *nd_btt = to_nd_btt(dev);
455 nd_region = to_nd_region(ndns->dev.parent);
456 nsio = to_nd_namespace_io(&ndns->dev);
459 struct pmem_device *pmem = dev_get_drvdata(dev);
461 nd_region = to_region(pmem);
464 if (is_nd_pfn(dev)) {
465 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
466 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
469 offset = pmem->data_offset +
470 __le32_to_cpu(pfn_sb->start_pad);
471 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
476 nsio = to_nd_namespace_io(&ndns->dev);
479 res.start = nsio->res.start + offset;
480 res.end = nsio->res.end - end_trunc;
481 nvdimm_badblocks_populate(nd_region, bb, &res);
484 MODULE_ALIAS("pmem");
485 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
486 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
487 static struct nd_device_driver nd_pmem_driver = {
488 .probe = nd_pmem_probe,
489 .remove = nd_pmem_remove,
490 .notify = nd_pmem_notify,
491 .shutdown = nd_pmem_shutdown,
495 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
498 static int __init pmem_init(void)
500 return nd_driver_register(&nd_pmem_driver);
502 module_init(pmem_init);
504 static void pmem_exit(void)
506 driver_unregister(&nd_pmem_driver.drv);
508 module_exit(pmem_exit);
510 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
511 MODULE_LICENSE("GPL v2");