68737bc68a0731c79d44ec7fef1338bd30600f3b
[platform/kernel/linux-rpi.git] / drivers / nvdimm / pmem.c
1 /*
2  * Persistent Memory Driver
3  *
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>.
7  *
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.
11  *
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
15  * more details.
16  */
17
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>
34 #include <linux/nd.h>
35 #include "pmem.h"
36 #include "pfn.h"
37 #include "nd.h"
38
39 static struct device *to_dev(struct pmem_device *pmem)
40 {
41         /*
42          * nvdimm bus services need a 'dev' parameter, and we record the device
43          * at init in bb.dev.
44          */
45         return pmem->bb.dev;
46 }
47
48 static struct nd_region *to_region(struct pmem_device *pmem)
49 {
50         return to_nd_region(to_dev(pmem)->parent);
51 }
52
53 static int pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
54                 unsigned int len)
55 {
56         struct device *dev = to_dev(pmem);
57         sector_t sector;
58         long cleared;
59         int rc = 0;
60
61         sector = (offset - pmem->data_offset) / 512;
62
63         cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
64         if (cleared < len)
65                 rc = -EIO;
66         if (cleared > 0 && cleared / 512) {
67                 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);
72         }
73
74         arch_invalidate_pmem(pmem->virt_addr + offset, len);
75
76         return rc;
77 }
78
79 static void write_pmem(void *pmem_addr, struct page *page,
80                 unsigned int off, unsigned int len)
81 {
82         void *mem = kmap_atomic(page);
83
84         memcpy_flushcache(pmem_addr, mem + off, len);
85         kunmap_atomic(mem);
86 }
87
88 static int read_pmem(struct page *page, unsigned int off,
89                 void *pmem_addr, unsigned int len)
90 {
91         int rc;
92         void *mem = kmap_atomic(page);
93
94         rc = memcpy_mcsafe(mem + off, pmem_addr, len);
95         kunmap_atomic(mem);
96         if (rc)
97                 return -EIO;
98         return 0;
99 }
100
101 static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
102                         unsigned int len, unsigned int off, bool is_write,
103                         sector_t sector)
104 {
105         int rc = 0;
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;
109
110         if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
111                 bad_pmem = true;
112
113         if (!is_write) {
114                 if (unlikely(bad_pmem))
115                         rc = -EIO;
116                 else {
117                         rc = read_pmem(page, off, pmem_addr, len);
118                         flush_dcache_page(page);
119                 }
120         } else {
121                 /*
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
129                  * data or an error.
130                  *
131                  * However, if pmem_clear_poison() leaves the data in an
132                  * indeterminate state we need to perform the write
133                  * after clear poison.
134                  */
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);
140                 }
141         }
142
143         return rc;
144 }
145
146 /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
147 #ifndef REQ_FLUSH
148 #define REQ_FLUSH REQ_PREFLUSH
149 #endif
150
151 static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
152 {
153         int rc = 0;
154         bool do_acct;
155         unsigned long start;
156         struct bio_vec bvec;
157         struct bvec_iter iter;
158         struct pmem_device *pmem = q->queuedata;
159         struct nd_region *nd_region = to_region(pmem);
160
161         if (bio->bi_opf & REQ_FLUSH)
162                 nvdimm_flush(nd_region);
163
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)),
168                                 iter.bi_sector);
169                 if (rc) {
170                         bio->bi_error = rc;
171                         break;
172                 }
173         }
174         if (do_acct)
175                 nd_iostat_end(bio, start);
176
177         if (bio->bi_opf & REQ_FUA)
178                 nvdimm_flush(nd_region);
179
180         bio_endio(bio);
181         return BLK_QC_T_NONE;
182 }
183
184 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
185                        struct page *page, bool is_write)
186 {
187         struct pmem_device *pmem = bdev->bd_queue->queuedata;
188         int rc;
189
190         rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
191
192         /*
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.
197          */
198         if (rc == 0)
199                 page_endio(page, is_write, 0);
200
201         return rc;
202 }
203
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)
207 {
208         resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
209
210         if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
211                                         PFN_PHYS(nr_pages))))
212                 return -EIO;
213         *kaddr = pmem->virt_addr + offset;
214         *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
215
216         /*
217          * If badblocks are present, limit known good range to the
218          * requested range.
219          */
220         if (unlikely(pmem->bb.count))
221                 return nr_pages;
222         return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
223 }
224
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,
229 };
230
231 static long pmem_dax_direct_access(struct dax_device *dax_dev,
232                 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
233 {
234         struct pmem_device *pmem = dax_get_private(dax_dev);
235
236         return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
237 }
238
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)
241 {
242         return copy_from_iter_flushcache(addr, bytes, i);
243 }
244
245 static void pmem_dax_flush(struct dax_device *dax_dev, pgoff_t pgoff,
246                 void *addr, size_t size)
247 {
248         arch_wb_cache_pmem(addr, size);
249 }
250
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,
255 };
256
257 static void pmem_release_queue(void *q)
258 {
259         blk_cleanup_queue(q);
260 }
261
262 static void pmem_freeze_queue(void *q)
263 {
264         blk_freeze_queue_start(q);
265 }
266
267 static void pmem_release_disk(void *__pmem)
268 {
269         struct pmem_device *pmem = __pmem;
270
271         kill_dax(pmem->dax_dev);
272         put_dax(pmem->dax_dev);
273         del_gendisk(pmem->disk);
274         put_disk(pmem->disk);
275 }
276
277 static int pmem_attach_disk(struct device *dev,
278                 struct nd_namespace_common *ndns)
279 {
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;
292         void *addr;
293
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);
298                 if (IS_ERR(altmap))
299                         return PTR_ERR(altmap);
300         }
301
302         /* we're attaching a block device, disable raw namespace access */
303         devm_nsio_disable(dev, nsio);
304
305         pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
306         if (!pmem)
307                 return -ENOMEM;
308
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");
315
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);
319                 return -EBUSY;
320         }
321
322         q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
323         if (!q)
324                 return -ENOMEM;
325
326         if (devm_add_action_or_reset(dev, pmem_release_queue, q))
327                 return -ENOMEM;
328
329         pmem->pfn_flags = PFN_DEV;
330         if (is_nd_pfn(dev)) {
331                 addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
332                                 altmap);
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;
343         } else
344                 addr = devm_memremap(dev, pmem->phys_addr,
345                                 pmem->size, ARCH_MEMREMAP_PMEM);
346
347         /*
348          * At release time the queue must be frozen before
349          * devm_memremap_pages is unwound
350          */
351         if (devm_add_action_or_reset(dev, pmem_freeze_queue, q))
352                 return -ENOMEM;
353
354         if (IS_ERR(addr))
355                 return PTR_ERR(addr);
356         pmem->virt_addr = addr;
357
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);
365         q->queuedata = pmem;
366
367         disk = alloc_disk_node(0, nid);
368         if (!disk)
369                 return -ENOMEM;
370         pmem->disk = disk;
371
372         disk->fops              = &pmem_fops;
373         disk->queue             = q;
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)
377                         / 512);
378         if (devm_init_badblocks(dev, &pmem->bb))
379                 return -ENOMEM;
380         nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
381         disk->bb = &pmem->bb;
382
383         dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops);
384         if (!dax_dev) {
385                 put_disk(disk);
386                 return -ENOMEM;
387         }
388         pmem->dax_dev = dax_dev;
389
390         device_add_disk(dev, disk);
391         if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
392                 return -ENOMEM;
393
394         revalidate_disk(disk);
395
396         return 0;
397 }
398
399 static int nd_pmem_probe(struct device *dev)
400 {
401         struct nd_namespace_common *ndns;
402
403         ndns = nvdimm_namespace_common_probe(dev);
404         if (IS_ERR(ndns))
405                 return PTR_ERR(ndns);
406
407         if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
408                 return -ENXIO;
409
410         if (is_nd_btt(dev))
411                 return nvdimm_namespace_attach_btt(ndns);
412
413         if (is_nd_pfn(dev))
414                 return pmem_attach_disk(dev, ndns);
415
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)
419                 return -ENXIO;
420
421         /* ...otherwise we're just a raw pmem device */
422         return pmem_attach_disk(dev, ndns);
423 }
424
425 static int nd_pmem_remove(struct device *dev)
426 {
427         if (is_nd_btt(dev))
428                 nvdimm_namespace_detach_btt(to_nd_btt(dev));
429         nvdimm_flush(to_nd_region(dev->parent));
430
431         return 0;
432 }
433
434 static void nd_pmem_shutdown(struct device *dev)
435 {
436         nvdimm_flush(to_nd_region(dev->parent));
437 }
438
439 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
440 {
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;
445         struct resource res;
446         struct badblocks *bb;
447
448         if (event != NVDIMM_REVALIDATE_POISON)
449                 return;
450
451         if (is_nd_btt(dev)) {
452                 struct nd_btt *nd_btt = to_nd_btt(dev);
453
454                 ndns = nd_btt->ndns;
455                 nd_region = to_nd_region(ndns->dev.parent);
456                 nsio = to_nd_namespace_io(&ndns->dev);
457                 bb = &nsio->bb;
458         } else {
459                 struct pmem_device *pmem = dev_get_drvdata(dev);
460
461                 nd_region = to_region(pmem);
462                 bb = &pmem->bb;
463
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;
467
468                         ndns = nd_pfn->ndns;
469                         offset = pmem->data_offset +
470                                         __le32_to_cpu(pfn_sb->start_pad);
471                         end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
472                 } else {
473                         ndns = to_ndns(dev);
474                 }
475
476                 nsio = to_nd_namespace_io(&ndns->dev);
477         }
478
479         res.start = nsio->res.start + offset;
480         res.end = nsio->res.end - end_trunc;
481         nvdimm_badblocks_populate(nd_region, bb, &res);
482 }
483
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,
492         .drv = {
493                 .name = "nd_pmem",
494         },
495         .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
496 };
497
498 static int __init pmem_init(void)
499 {
500         return nd_driver_register(&nd_pmem_driver);
501 }
502 module_init(pmem_init);
503
504 static void pmem_exit(void)
505 {
506         driver_unregister(&nd_pmem_driver.drv);
507 }
508 module_exit(pmem_exit);
509
510 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
511 MODULE_LICENSE("GPL v2");