libnvdimm, namespace: avoid multiple sector calculations
[linux-2.6-block.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/pfn_t.h>
29 #include <linux/slab.h>
30 #include <linux/pmem.h>
31 #include <linux/nd.h>
32 #include "pmem.h"
33 #include "pfn.h"
34 #include "nd.h"
35
36 static struct device *to_dev(struct pmem_device *pmem)
37 {
38         /*
39          * nvdimm bus services need a 'dev' parameter, and we record the device
40          * at init in bb.dev.
41          */
42         return pmem->bb.dev;
43 }
44
45 static struct nd_region *to_region(struct pmem_device *pmem)
46 {
47         return to_nd_region(to_dev(pmem)->parent);
48 }
49
50 static void pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
51                 unsigned int len)
52 {
53         struct device *dev = to_dev(pmem);
54         sector_t sector, cleared;
55
56         sector = (offset - pmem->data_offset) / 512;
57         cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len) / 512;
58
59         if (cleared) {
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);
64         }
65         invalidate_pmem(pmem->virt_addr + offset, len);
66 }
67
68 static void write_pmem(void *pmem_addr, struct page *page,
69                 unsigned int off, unsigned int len)
70 {
71         void *mem = kmap_atomic(page);
72
73         memcpy_to_pmem(pmem_addr, mem + off, len);
74         kunmap_atomic(mem);
75 }
76
77 static int read_pmem(struct page *page, unsigned int off,
78                 void *pmem_addr, unsigned int len)
79 {
80         int rc;
81         void *mem = kmap_atomic(page);
82
83         rc = memcpy_from_pmem(mem + off, pmem_addr, len);
84         kunmap_atomic(mem);
85         return rc;
86 }
87
88 static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
89                         unsigned int len, unsigned int off, bool is_write,
90                         sector_t sector)
91 {
92         int rc = 0;
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;
96
97         if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
98                 bad_pmem = true;
99
100         if (!is_write) {
101                 if (unlikely(bad_pmem))
102                         rc = -EIO;
103                 else {
104                         rc = read_pmem(page, off, pmem_addr, len);
105                         flush_dcache_page(page);
106                 }
107         } else {
108                 /*
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
116                  * data or an error.
117                  *
118                  * However, if pmem_clear_poison() leaves the data in an
119                  * indeterminate state we need to perform the write
120                  * after clear poison.
121                  */
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);
127                 }
128         }
129
130         return rc;
131 }
132
133 /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
134 #ifndef REQ_FLUSH
135 #define REQ_FLUSH REQ_PREFLUSH
136 #endif
137
138 static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
139 {
140         int rc = 0;
141         bool do_acct;
142         unsigned long start;
143         struct bio_vec bvec;
144         struct bvec_iter iter;
145         struct pmem_device *pmem = q->queuedata;
146         struct nd_region *nd_region = to_region(pmem);
147
148         if (bio->bi_opf & REQ_FLUSH)
149                 nvdimm_flush(nd_region);
150
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)),
155                                 iter.bi_sector);
156                 if (rc) {
157                         bio->bi_error = rc;
158                         break;
159                 }
160         }
161         if (do_acct)
162                 nd_iostat_end(bio, start);
163
164         if (bio->bi_opf & REQ_FUA)
165                 nvdimm_flush(nd_region);
166
167         bio_endio(bio);
168         return BLK_QC_T_NONE;
169 }
170
171 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
172                        struct page *page, bool is_write)
173 {
174         struct pmem_device *pmem = bdev->bd_queue->queuedata;
175         int rc;
176
177         rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
178
179         /*
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.
184          */
185         if (rc == 0)
186                 page_endio(page, is_write, 0);
187
188         return rc;
189 }
190
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)
194 {
195         struct pmem_device *pmem = bdev->bd_queue->queuedata;
196         resource_size_t offset = sector * 512 + pmem->data_offset;
197
198         if (unlikely(is_bad_pmem(&pmem->bb, sector, size)))
199                 return -EIO;
200         *kaddr = pmem->virt_addr + offset;
201         *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
202
203         /*
204          * If badblocks are present, limit known good range to the
205          * requested range.
206          */
207         if (unlikely(pmem->bb.count))
208                 return size;
209         return pmem->size - pmem->pfn_pad - offset;
210 }
211
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,
217 };
218
219 static void pmem_release_queue(void *q)
220 {
221         blk_cleanup_queue(q);
222 }
223
224 static void pmem_release_disk(void *disk)
225 {
226         del_gendisk(disk);
227         put_disk(disk);
228 }
229
230 static int pmem_attach_disk(struct device *dev,
231                 struct nd_namespace_common *ndns)
232 {
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;
244         void *addr;
245
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);
250                 if (IS_ERR(altmap))
251                         return PTR_ERR(altmap);
252         }
253
254         /* we're attaching a block device, disable raw namespace access */
255         devm_nsio_disable(dev, nsio);
256
257         pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
258         if (!pmem)
259                 return -ENOMEM;
260
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");
266
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);
270                 return -EBUSY;
271         }
272
273         q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
274         if (!q)
275                 return -ENOMEM;
276
277         pmem->pfn_flags = PFN_DEV;
278         if (is_nd_pfn(dev)) {
279                 addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
280                                 altmap);
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;
291         } else
292                 addr = devm_memremap(dev, pmem->phys_addr,
293                                 pmem->size, ARCH_MEMREMAP_PMEM);
294
295         /*
296          * At release time the queue must be dead before
297          * devm_memremap_pages is unwound
298          */
299         if (devm_add_action_or_reset(dev, pmem_release_queue, q))
300                 return -ENOMEM;
301
302         if (IS_ERR(addr))
303                 return PTR_ERR(addr);
304         pmem->virt_addr = addr;
305
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);
313         q->queuedata = pmem;
314
315         disk = alloc_disk_node(0, nid);
316         if (!disk)
317                 return -ENOMEM;
318
319         disk->fops              = &pmem_fops;
320         disk->queue             = q;
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)
324                         / 512);
325         if (devm_init_badblocks(dev, &pmem->bb))
326                 return -ENOMEM;
327         nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
328         disk->bb = &pmem->bb;
329         device_add_disk(dev, disk);
330
331         if (devm_add_action_or_reset(dev, pmem_release_disk, disk))
332                 return -ENOMEM;
333
334         revalidate_disk(disk);
335
336         return 0;
337 }
338
339 static int nd_pmem_probe(struct device *dev)
340 {
341         struct nd_namespace_common *ndns;
342
343         ndns = nvdimm_namespace_common_probe(dev);
344         if (IS_ERR(ndns))
345                 return PTR_ERR(ndns);
346
347         if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
348                 return -ENXIO;
349
350         if (is_nd_btt(dev))
351                 return nvdimm_namespace_attach_btt(ndns);
352
353         if (is_nd_pfn(dev))
354                 return pmem_attach_disk(dev, ndns);
355
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)
359                 return -ENXIO;
360
361         /* ...otherwise we're just a raw pmem device */
362         return pmem_attach_disk(dev, ndns);
363 }
364
365 static int nd_pmem_remove(struct device *dev)
366 {
367         if (is_nd_btt(dev))
368                 nvdimm_namespace_detach_btt(to_nd_btt(dev));
369         nvdimm_flush(to_nd_region(dev->parent));
370
371         return 0;
372 }
373
374 static void nd_pmem_shutdown(struct device *dev)
375 {
376         nvdimm_flush(to_nd_region(dev->parent));
377 }
378
379 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
380 {
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;
386         struct resource res;
387
388         if (event != NVDIMM_REVALIDATE_POISON)
389                 return;
390
391         if (is_nd_btt(dev)) {
392                 struct nd_btt *nd_btt = to_nd_btt(dev);
393
394                 ndns = nd_btt->ndns;
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;
398
399                 ndns = nd_pfn->ndns;
400                 offset = pmem->data_offset + __le32_to_cpu(pfn_sb->start_pad);
401                 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
402         } else
403                 ndns = to_ndns(dev);
404
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);
409 }
410
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,
419         .drv = {
420                 .name = "nd_pmem",
421         },
422         .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
423 };
424
425 static int __init pmem_init(void)
426 {
427         return nd_driver_register(&nd_pmem_driver);
428 }
429 module_init(pmem_init);
430
431 static void pmem_exit(void)
432 {
433         driver_unregister(&nd_pmem_driver.drv);
434 }
435 module_exit(pmem_exit);
436
437 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
438 MODULE_LICENSE("GPL v2");