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 */ 13 #include <linux/module.h> 14 #include <linux/device.h> 15 #include <linux/nd.h> 16 #include "nd.h" 17 18 static int nd_region_probe(struct device *dev) 19 { 20 int err; 21 struct nd_region_namespaces *num_ns; 22 struct nd_region *nd_region = to_nd_region(dev); 23 int rc = nd_region_register_namespaces(nd_region, &err); 24 25 num_ns = devm_kzalloc(dev, sizeof(*num_ns), GFP_KERNEL); 26 if (!num_ns) 27 return -ENOMEM; 28 29 if (rc < 0) 30 return rc; 31 32 num_ns->active = rc; 33 num_ns->count = rc + err; 34 dev_set_drvdata(dev, num_ns); 35 36 if (err == 0) 37 return 0; 38 39 if (rc == err) 40 return -ENODEV; 41 42 /* 43 * Given multiple namespaces per region, we do not want to 44 * disable all the successfully registered peer namespaces upon 45 * a single registration failure. If userspace is missing a 46 * namespace that it expects it can disable/re-enable the region 47 * to retry discovery after correcting the failure. 48 * <regionX>/namespaces returns the current 49 * "<async-registered>/<total>" namespace count. 50 */ 51 dev_err(dev, "failed to register %d namespace%s, continuing...\n", 52 err, err == 1 ? "" : "s"); 53 return 0; 54 } 55 56 static int child_unregister(struct device *dev, void *data) 57 { 58 nd_device_unregister(dev, ND_SYNC); 59 return 0; 60 } 61 62 static int nd_region_remove(struct device *dev) 63 { 64 /* flush attribute readers and disable */ 65 nvdimm_bus_lock(dev); 66 dev_set_drvdata(dev, NULL); 67 nvdimm_bus_unlock(dev); 68 69 device_for_each_child(dev, NULL, child_unregister); 70 return 0; 71 } 72 73 static struct nd_device_driver nd_region_driver = { 74 .probe = nd_region_probe, 75 .remove = nd_region_remove, 76 .drv = { 77 .name = "nd_region", 78 }, 79 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM, 80 }; 81 82 int __init nd_region_init(void) 83 { 84 return nd_driver_register(&nd_region_driver); 85 } 86 87 void nd_region_exit(void) 88 { 89 driver_unregister(&nd_region_driver.drv); 90 } 91 92 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM); 93 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK); 94