]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/nvdimm/pmem.c
devm_memremap: Fix error value when memremap failed
[mirror_ubuntu-jammy-kernel.git] / drivers / nvdimm / pmem.c
CommitLineData
9e853f23
RZ
1/*
2 * Persistent Memory Driver
3 *
9f53f9fa 4 * Copyright (c) 2014-2015, Intel Corporation.
9e853f23
RZ
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>
b95f5f43 25#include <linux/badblocks.h>
9476df7d 26#include <linux/memremap.h>
32ab0a3f 27#include <linux/vmalloc.h>
34c0fd54 28#include <linux/pfn_t.h>
9e853f23 29#include <linux/slab.h>
61031952 30#include <linux/pmem.h>
9f53f9fa 31#include <linux/nd.h>
32ab0a3f 32#include "pfn.h"
9f53f9fa 33#include "nd.h"
9e853f23
RZ
34
35struct pmem_device {
36 struct request_queue *pmem_queue;
37 struct gendisk *pmem_disk;
32ab0a3f 38 struct nd_namespace_common *ndns;
9e853f23
RZ
39
40 /* One contiguous memory region per device */
41 phys_addr_t phys_addr;
32ab0a3f
DW
42 /* when non-zero this device is hosting a 'pfn' instance */
43 phys_addr_t data_offset;
34c0fd54 44 unsigned long pfn_flags;
61031952 45 void __pmem *virt_addr;
9e853f23 46 size_t size;
b95f5f43 47 struct badblocks bb;
9e853f23
RZ
48};
49
50static int pmem_major;
9e853f23 51
e10624f8
DW
52static bool is_bad_pmem(struct badblocks *bb, sector_t sector, unsigned int len)
53{
54 if (bb->count) {
55 sector_t first_bad;
56 int num_bad;
57
58 return !!badblocks_check(bb, sector, len / 512, &first_bad,
59 &num_bad);
60 }
61
62 return false;
63}
64
65static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
9e853f23
RZ
66 unsigned int len, unsigned int off, int rw,
67 sector_t sector)
68{
69 void *mem = kmap_atomic(page);
32ab0a3f 70 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
61031952 71 void __pmem *pmem_addr = pmem->virt_addr + pmem_off;
9e853f23
RZ
72
73 if (rw == READ) {
e10624f8
DW
74 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
75 return -EIO;
61031952 76 memcpy_from_pmem(mem + off, pmem_addr, len);
9e853f23
RZ
77 flush_dcache_page(page);
78 } else {
79 flush_dcache_page(page);
61031952 80 memcpy_to_pmem(pmem_addr, mem + off, len);
9e853f23
RZ
81 }
82
83 kunmap_atomic(mem);
e10624f8 84 return 0;
9e853f23
RZ
85}
86
dece1635 87static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
9e853f23 88{
e10624f8 89 int rc = 0;
f0dc089c
DW
90 bool do_acct;
91 unsigned long start;
9e853f23 92 struct bio_vec bvec;
9e853f23 93 struct bvec_iter iter;
edc870e5
DW
94 struct block_device *bdev = bio->bi_bdev;
95 struct pmem_device *pmem = bdev->bd_disk->private_data;
9e853f23 96
f0dc089c 97 do_acct = nd_iostat_start(bio, &start);
e10624f8
DW
98 bio_for_each_segment(bvec, bio, iter) {
99 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
100 bvec.bv_offset, bio_data_dir(bio),
101 iter.bi_sector);
102 if (rc) {
103 bio->bi_error = rc;
104 break;
105 }
106 }
f0dc089c
DW
107 if (do_acct)
108 nd_iostat_end(bio, start);
61031952
RZ
109
110 if (bio_data_dir(bio))
111 wmb_pmem();
112
4246a0b6 113 bio_endio(bio);
dece1635 114 return BLK_QC_T_NONE;
9e853f23
RZ
115}
116
117static int pmem_rw_page(struct block_device *bdev, sector_t sector,
118 struct page *page, int rw)
119{
120 struct pmem_device *pmem = bdev->bd_disk->private_data;
e10624f8 121 int rc;
9e853f23 122
e10624f8 123 rc = pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
ba8fe0f8
RZ
124 if (rw & WRITE)
125 wmb_pmem();
9e853f23 126
e10624f8
DW
127 /*
128 * The ->rw_page interface is subtle and tricky. The core
129 * retries on any error, so we can only invoke page_endio() in
130 * the successful completion case. Otherwise, we'll see crashes
131 * caused by double completion.
132 */
133 if (rc == 0)
134 page_endio(page, rw & WRITE, 0);
135
136 return rc;
9e853f23
RZ
137}
138
139static long pmem_direct_access(struct block_device *bdev, sector_t sector,
34c0fd54 140 void __pmem **kaddr, pfn_t *pfn)
9e853f23
RZ
141{
142 struct pmem_device *pmem = bdev->bd_disk->private_data;
32ab0a3f 143 resource_size_t offset = sector * 512 + pmem->data_offset;
589e75d1 144
e2e05394 145 *kaddr = pmem->virt_addr + offset;
34c0fd54 146 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
9e853f23 147
589e75d1 148 return pmem->size - offset;
9e853f23
RZ
149}
150
151static const struct block_device_operations pmem_fops = {
152 .owner = THIS_MODULE,
153 .rw_page = pmem_rw_page,
154 .direct_access = pmem_direct_access,
58138820 155 .revalidate_disk = nvdimm_revalidate_disk,
9e853f23
RZ
156};
157
9f53f9fa
DW
158static struct pmem_device *pmem_alloc(struct device *dev,
159 struct resource *res, int id)
9e853f23
RZ
160{
161 struct pmem_device *pmem;
468ded03 162 struct request_queue *q;
9e853f23 163
708ab62b 164 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
9e853f23 165 if (!pmem)
8c2f7e86 166 return ERR_PTR(-ENOMEM);
9e853f23
RZ
167
168 pmem->phys_addr = res->start;
169 pmem->size = resource_size(res);
96601adb 170 if (!arch_has_wmb_pmem())
61031952 171 dev_warn(dev, "unable to guarantee persistence of writes\n");
9e853f23 172
708ab62b
CH
173 if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
174 dev_name(dev))) {
9f53f9fa
DW
175 dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
176 &pmem->phys_addr, pmem->size);
8c2f7e86 177 return ERR_PTR(-EBUSY);
9e853f23
RZ
178 }
179
468ded03
DW
180 q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
181 if (!q)
182 return ERR_PTR(-ENOMEM);
183
34c0fd54
DW
184 pmem->pfn_flags = PFN_DEV;
185 if (pmem_should_map_pages(dev)) {
4b94ffdc 186 pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, res,
5c2c2587 187 &q->q_usage_counter, NULL);
34c0fd54
DW
188 pmem->pfn_flags |= PFN_MAP;
189 } else
a639315d
DW
190 pmem->virt_addr = (void __pmem *) devm_memremap(dev,
191 pmem->phys_addr, pmem->size,
192 ARCH_MEMREMAP_PMEM);
b36f4761 193
468ded03
DW
194 if (IS_ERR(pmem->virt_addr)) {
195 blk_cleanup_queue(q);
b36f4761 196 return (void __force *) pmem->virt_addr;
468ded03 197 }
8c2f7e86 198
468ded03 199 pmem->pmem_queue = q;
8c2f7e86
DW
200 return pmem;
201}
202
203static void pmem_detach_disk(struct pmem_device *pmem)
204{
32ab0a3f
DW
205 if (!pmem->pmem_disk)
206 return;
207
8c2f7e86
DW
208 del_gendisk(pmem->pmem_disk);
209 put_disk(pmem->pmem_disk);
210 blk_cleanup_queue(pmem->pmem_queue);
211}
212
32ab0a3f
DW
213static int pmem_attach_disk(struct device *dev,
214 struct nd_namespace_common *ndns, struct pmem_device *pmem)
8c2f7e86 215{
538ea4aa 216 int nid = dev_to_node(dev);
8c2f7e86 217 struct gendisk *disk;
9e853f23 218
9e853f23 219 blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
6b47496a 220 blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE);
43d3fa3a 221 blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
9e853f23 222 blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
0f51c4fa 223 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
9e853f23 224
538ea4aa 225 disk = alloc_disk_node(0, nid);
8c2f7e86
DW
226 if (!disk) {
227 blk_cleanup_queue(pmem->pmem_queue);
228 return -ENOMEM;
229 }
9e853f23 230
9e853f23 231 disk->major = pmem_major;
9f53f9fa 232 disk->first_minor = 0;
9e853f23
RZ
233 disk->fops = &pmem_fops;
234 disk->private_data = pmem;
235 disk->queue = pmem->pmem_queue;
236 disk->flags = GENHD_FL_EXT_DEVT;
5212e11f 237 nvdimm_namespace_disk_name(ndns, disk->disk_name);
32ab0a3f
DW
238 disk->driverfs_dev = dev;
239 set_capacity(disk, (pmem->size - pmem->data_offset) / 512);
9e853f23 240 pmem->pmem_disk = disk;
710d69cc 241 devm_exit_badblocks(dev, &pmem->bb);
b95f5f43
DW
242 if (devm_init_badblocks(dev, &pmem->bb))
243 return -ENOMEM;
244 nvdimm_namespace_add_poison(ndns, &pmem->bb, pmem->data_offset);
9e853f23 245
57f7f317 246 disk->bb = &pmem->bb;
9e853f23 247 add_disk(disk);
58138820 248 revalidate_disk(disk);
9e853f23 249
8c2f7e86
DW
250 return 0;
251}
9e853f23 252
8c2f7e86
DW
253static int pmem_rw_bytes(struct nd_namespace_common *ndns,
254 resource_size_t offset, void *buf, size_t size, int rw)
255{
256 struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
257
258 if (unlikely(offset + size > pmem->size)) {
259 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
260 return -EFAULT;
261 }
262
710d69cc
DW
263 if (rw == READ) {
264 unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
265
266 if (unlikely(is_bad_pmem(&pmem->bb, offset / 512, sz_align)))
267 return -EIO;
61031952 268 memcpy_from_pmem(buf, pmem->virt_addr + offset, size);
710d69cc 269 } else {
61031952
RZ
270 memcpy_to_pmem(pmem->virt_addr + offset, buf, size);
271 wmb_pmem();
272 }
8c2f7e86
DW
273
274 return 0;
275}
276
32ab0a3f
DW
277static int nd_pfn_init(struct nd_pfn *nd_pfn)
278{
279 struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
280 struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev);
281 struct nd_namespace_common *ndns = nd_pfn->ndns;
282 struct nd_region *nd_region;
283 unsigned long npfns;
284 phys_addr_t offset;
285 u64 checksum;
286 int rc;
287
288 if (!pfn_sb)
289 return -ENOMEM;
290
291 nd_pfn->pfn_sb = pfn_sb;
292 rc = nd_pfn_validate(nd_pfn);
3fa96268
DW
293 if (rc == -ENODEV)
294 /* no info block, do init */;
295 else
32ab0a3f
DW
296 return rc;
297
32ab0a3f
DW
298 nd_region = to_nd_region(nd_pfn->dev.parent);
299 if (nd_region->ro) {
300 dev_info(&nd_pfn->dev,
301 "%s is read-only, unable to init metadata\n",
302 dev_name(&nd_region->dev));
303 goto err;
304 }
305
306 memset(pfn_sb, 0, sizeof(*pfn_sb));
307 npfns = (pmem->size - SZ_8K) / SZ_4K;
308 /*
309 * Note, we use 64 here for the standard size of struct page,
310 * debugging options may cause it to be larger in which case the
311 * implementation will limit the pfns advertised through
312 * ->direct_access() to those that are included in the memmap.
313 */
314 if (nd_pfn->mode == PFN_MODE_PMEM)
315c5625 315 offset = ALIGN(SZ_8K + 64 * npfns, nd_pfn->align);
32ab0a3f 316 else if (nd_pfn->mode == PFN_MODE_RAM)
315c5625 317 offset = ALIGN(SZ_8K, nd_pfn->align);
32ab0a3f
DW
318 else
319 goto err;
320
321 npfns = (pmem->size - offset) / SZ_4K;
322 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
323 pfn_sb->dataoff = cpu_to_le64(offset);
324 pfn_sb->npfns = cpu_to_le64(npfns);
325 memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
326 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
a34d5e8a 327 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
32ab0a3f
DW
328 pfn_sb->version_major = cpu_to_le16(1);
329 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
330 pfn_sb->checksum = cpu_to_le64(checksum);
331
332 rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
333 if (rc)
334 goto err;
335
336 return 0;
337 err:
338 nd_pfn->pfn_sb = NULL;
339 kfree(pfn_sb);
340 return -ENXIO;
341}
342
343static int nvdimm_namespace_detach_pfn(struct nd_namespace_common *ndns)
344{
345 struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
346 struct pmem_device *pmem;
347
348 /* free pmem disk */
349 pmem = dev_get_drvdata(&nd_pfn->dev);
350 pmem_detach_disk(pmem);
351
352 /* release nd_pfn resources */
353 kfree(nd_pfn->pfn_sb);
354 nd_pfn->pfn_sb = NULL;
355
356 return 0;
357}
358
359static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns)
9e853f23 360{
32ab0a3f
DW
361 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
362 struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
363 struct device *dev = &nd_pfn->dev;
32ab0a3f 364 struct nd_region *nd_region;
d2c0f041 365 struct vmem_altmap *altmap;
32ab0a3f
DW
366 struct nd_pfn_sb *pfn_sb;
367 struct pmem_device *pmem;
5c2c2587 368 struct request_queue *q;
32ab0a3f
DW
369 phys_addr_t offset;
370 int rc;
d2c0f041
DW
371 struct vmem_altmap __altmap = {
372 .base_pfn = __phys_to_pfn(nsio->res.start),
373 .reserve = __phys_to_pfn(SZ_8K),
374 };
32ab0a3f
DW
375
376 if (!nd_pfn->uuid || !nd_pfn->ndns)
377 return -ENODEV;
378
379 nd_region = to_nd_region(dev->parent);
380 rc = nd_pfn_init(nd_pfn);
381 if (rc)
382 return rc;
383
32ab0a3f
DW
384 pfn_sb = nd_pfn->pfn_sb;
385 offset = le64_to_cpu(pfn_sb->dataoff);
386 nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
387 if (nd_pfn->mode == PFN_MODE_RAM) {
315c5625 388 if (offset < SZ_8K)
32ab0a3f
DW
389 return -EINVAL;
390 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
391 altmap = NULL;
d2c0f041
DW
392 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
393 nd_pfn->npfns = (resource_size(&nsio->res) - offset)
394 / PAGE_SIZE;
395 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
396 dev_info(&nd_pfn->dev,
397 "number of pfns truncated from %lld to %ld\n",
398 le64_to_cpu(nd_pfn->pfn_sb->npfns),
399 nd_pfn->npfns);
400 altmap = & __altmap;
401 altmap->free = __phys_to_pfn(offset - SZ_8K);
402 altmap->alloc = 0;
32ab0a3f
DW
403 } else {
404 rc = -ENXIO;
405 goto err;
406 }
407
408 /* establish pfn range for lookup, and switch to direct map */
409 pmem = dev_get_drvdata(dev);
5c2c2587 410 q = pmem->pmem_queue;
a639315d 411 devm_memunmap(dev, (void __force *) pmem->virt_addr);
4b94ffdc 412 pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &nsio->res,
5c2c2587 413 &q->q_usage_counter, altmap);
34c0fd54 414 pmem->pfn_flags |= PFN_MAP;
32ab0a3f
DW
415 if (IS_ERR(pmem->virt_addr)) {
416 rc = PTR_ERR(pmem->virt_addr);
417 goto err;
418 }
419
420 /* attach pmem disk in "pfn-mode" */
421 pmem->data_offset = offset;
422 rc = pmem_attach_disk(dev, ndns, pmem);
423 if (rc)
424 goto err;
425
426 return rc;
427 err:
428 nvdimm_namespace_detach_pfn(ndns);
429 return rc;
9e853f23
RZ
430}
431
9f53f9fa 432static int nd_pmem_probe(struct device *dev)
9e853f23 433{
9f53f9fa 434 struct nd_region *nd_region = to_nd_region(dev->parent);
8c2f7e86
DW
435 struct nd_namespace_common *ndns;
436 struct nd_namespace_io *nsio;
9e853f23 437 struct pmem_device *pmem;
9e853f23 438
8c2f7e86
DW
439 ndns = nvdimm_namespace_common_probe(dev);
440 if (IS_ERR(ndns))
441 return PTR_ERR(ndns);
bf9bccc1 442
8c2f7e86 443 nsio = to_nd_namespace_io(&ndns->dev);
9f53f9fa 444 pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
9e853f23
RZ
445 if (IS_ERR(pmem))
446 return PTR_ERR(pmem);
447
32ab0a3f 448 pmem->ndns = ndns;
9f53f9fa 449 dev_set_drvdata(dev, pmem);
8c2f7e86 450 ndns->rw_bytes = pmem_rw_bytes;
710d69cc
DW
451 if (devm_init_badblocks(dev, &pmem->bb))
452 return -ENOMEM;
453 nvdimm_namespace_add_poison(ndns, &pmem->bb, 0);
708ab62b 454
468ded03
DW
455 if (is_nd_btt(dev)) {
456 /* btt allocates its own request_queue */
457 blk_cleanup_queue(pmem->pmem_queue);
458 pmem->pmem_queue = NULL;
708ab62b 459 return nvdimm_namespace_attach_btt(ndns);
468ded03 460 }
708ab62b 461
32ab0a3f
DW
462 if (is_nd_pfn(dev))
463 return nvdimm_namespace_attach_pfn(ndns);
464
468ded03
DW
465 if (nd_btt_probe(ndns, pmem) == 0 || nd_pfn_probe(ndns, pmem) == 0) {
466 /*
467 * We'll come back as either btt-pmem, or pfn-pmem, so
468 * drop the queue allocation for now.
469 */
470 blk_cleanup_queue(pmem->pmem_queue);
32ab0a3f
DW
471 return -ENXIO;
472 }
473
474 return pmem_attach_disk(dev, ndns, pmem);
9e853f23
RZ
475}
476
9f53f9fa 477static int nd_pmem_remove(struct device *dev)
9e853f23 478{
9f53f9fa 479 struct pmem_device *pmem = dev_get_drvdata(dev);
9e853f23 480
8c2f7e86 481 if (is_nd_btt(dev))
32ab0a3f
DW
482 nvdimm_namespace_detach_btt(pmem->ndns);
483 else if (is_nd_pfn(dev))
484 nvdimm_namespace_detach_pfn(pmem->ndns);
8c2f7e86
DW
485 else
486 pmem_detach_disk(pmem);
8c2f7e86 487
9e853f23
RZ
488 return 0;
489}
490
9f53f9fa
DW
491MODULE_ALIAS("pmem");
492MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
bf9bccc1 493MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
9f53f9fa
DW
494static struct nd_device_driver nd_pmem_driver = {
495 .probe = nd_pmem_probe,
496 .remove = nd_pmem_remove,
497 .drv = {
498 .name = "nd_pmem",
9e853f23 499 },
bf9bccc1 500 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
9e853f23
RZ
501};
502
503static int __init pmem_init(void)
504{
505 int error;
506
507 pmem_major = register_blkdev(0, "pmem");
508 if (pmem_major < 0)
509 return pmem_major;
510
9f53f9fa
DW
511 error = nd_driver_register(&nd_pmem_driver);
512 if (error) {
9e853f23 513 unregister_blkdev(pmem_major, "pmem");
9f53f9fa
DW
514 return error;
515 }
516
517 return 0;
9e853f23
RZ
518}
519module_init(pmem_init);
520
521static void pmem_exit(void)
522{
9f53f9fa 523 driver_unregister(&nd_pmem_driver.drv);
9e853f23
RZ
524 unregister_blkdev(pmem_major, "pmem");
525}
526module_exit(pmem_exit);
527
528MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
529MODULE_LICENSE("GPL v2");