Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 13 | #include <linux/cpumask.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/nd.h> |
| 17 | #include "nd.h" |
| 18 | |
| 19 | static int nd_region_probe(struct device *dev) |
| 20 | { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 21 | int err, rc; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 22 | static unsigned long once; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 23 | struct nd_region_data *ndrd; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 24 | struct nd_region *nd_region = to_nd_region(dev); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 25 | |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 26 | if (nd_region->num_lanes > num_online_cpus() |
| 27 | && nd_region->num_lanes < num_possible_cpus() |
| 28 | && !test_and_set_bit(0, &once)) { |
| 29 | dev_info(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n", |
| 30 | num_online_cpus(), nd_region->num_lanes, |
| 31 | num_possible_cpus()); |
| 32 | dev_info(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n", |
| 33 | nd_region->num_lanes); |
| 34 | } |
| 35 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 36 | rc = nd_region_activate(nd_region); |
| 37 | if (rc) |
| 38 | return rc; |
| 39 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 40 | rc = nd_blk_region_init(nd_region); |
| 41 | if (rc) |
| 42 | return rc; |
| 43 | |
| 44 | rc = nd_region_register_namespaces(nd_region, &err); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 45 | if (rc < 0) |
| 46 | return rc; |
| 47 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 48 | ndrd = dev_get_drvdata(dev); |
| 49 | ndrd->ns_active = rc; |
| 50 | ndrd->ns_count = rc + err; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 51 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 52 | if (rc && err && rc == err) |
| 53 | return -ENODEV; |
| 54 | |
| 55 | nd_region->btt_seed = nd_btt_create(nd_region); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 56 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 57 | nd_region->dax_seed = nd_dax_create(nd_region); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 58 | if (err == 0) |
| 59 | return 0; |
| 60 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 61 | /* |
| 62 | * Given multiple namespaces per region, we do not want to |
| 63 | * disable all the successfully registered peer namespaces upon |
| 64 | * a single registration failure. If userspace is missing a |
| 65 | * namespace that it expects it can disable/re-enable the region |
| 66 | * to retry discovery after correcting the failure. |
| 67 | * <regionX>/namespaces returns the current |
| 68 | * "<async-registered>/<total>" namespace count. |
| 69 | */ |
| 70 | dev_err(dev, "failed to register %d namespace%s, continuing...\n", |
| 71 | err, err == 1 ? "" : "s"); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int child_unregister(struct device *dev, void *data) |
| 76 | { |
| 77 | nd_device_unregister(dev, ND_SYNC); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int nd_region_remove(struct device *dev) |
| 82 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 83 | struct nd_region *nd_region = to_nd_region(dev); |
| 84 | |
Dan Williams | a8f7202 | 2016-07-09 16:46:29 -0700 | [diff] [blame] | 85 | device_for_each_child(dev, NULL, child_unregister); |
| 86 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 87 | /* flush attribute readers and disable */ |
| 88 | nvdimm_bus_lock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 89 | nd_region->ns_seed = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 90 | nd_region->btt_seed = NULL; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 91 | nd_region->pfn_seed = NULL; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 92 | nd_region->dax_seed = NULL; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 93 | dev_set_drvdata(dev, NULL); |
| 94 | nvdimm_bus_unlock(dev); |
| 95 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 99 | static int child_notify(struct device *dev, void *data) |
| 100 | { |
| 101 | nd_device_notify(dev, *(enum nvdimm_event *) data); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static void nd_region_notify(struct device *dev, enum nvdimm_event event) |
| 106 | { |
| 107 | device_for_each_child(dev, &event, child_notify); |
| 108 | } |
| 109 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 110 | static struct nd_device_driver nd_region_driver = { |
| 111 | .probe = nd_region_probe, |
| 112 | .remove = nd_region_remove, |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 113 | .notify = nd_region_notify, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 114 | .drv = { |
| 115 | .name = "nd_region", |
| 116 | }, |
| 117 | .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM, |
| 118 | }; |
| 119 | |
| 120 | int __init nd_region_init(void) |
| 121 | { |
| 122 | return nd_driver_register(&nd_region_driver); |
| 123 | } |
| 124 | |
| 125 | void nd_region_exit(void) |
| 126 | { |
| 127 | driver_unregister(&nd_region_driver.drv); |
| 128 | } |
| 129 | |
| 130 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM); |
| 131 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK); |