Dan Williams | b94d523 | 2015-05-19 22:54:31 -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 | */ |
| 13 | #include <linux/libnvdimm.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/device.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include "nd-core.h" |
| 20 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame^] | 21 | LIST_HEAD(nvdimm_bus_list); |
| 22 | DEFINE_MUTEX(nvdimm_bus_list_mutex); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 23 | static DEFINE_IDA(nd_ida); |
| 24 | |
| 25 | static void nvdimm_bus_release(struct device *dev) |
| 26 | { |
| 27 | struct nvdimm_bus *nvdimm_bus; |
| 28 | |
| 29 | nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); |
| 30 | ida_simple_remove(&nd_ida, nvdimm_bus->id); |
| 31 | kfree(nvdimm_bus); |
| 32 | } |
| 33 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 34 | struct nvdimm_bus *to_nvdimm_bus(struct device *dev) |
| 35 | { |
| 36 | struct nvdimm_bus *nvdimm_bus; |
| 37 | |
| 38 | nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); |
| 39 | WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release); |
| 40 | return nvdimm_bus; |
| 41 | } |
| 42 | EXPORT_SYMBOL_GPL(to_nvdimm_bus); |
| 43 | |
| 44 | struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus) |
| 45 | { |
| 46 | /* struct nvdimm_bus definition is private to libnvdimm */ |
| 47 | return nvdimm_bus->nd_desc; |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(to_nd_desc); |
| 50 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame^] | 51 | struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev) |
| 52 | { |
| 53 | struct device *dev; |
| 54 | |
| 55 | for (dev = nd_dev; dev; dev = dev->parent) |
| 56 | if (dev->release == nvdimm_bus_release) |
| 57 | break; |
| 58 | dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n"); |
| 59 | if (dev) |
| 60 | return to_nvdimm_bus(dev); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 64 | static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus) |
| 65 | { |
| 66 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 67 | struct device *parent = nvdimm_bus->dev.parent; |
| 68 | |
| 69 | if (nd_desc->provider_name) |
| 70 | return nd_desc->provider_name; |
| 71 | else if (parent) |
| 72 | return dev_name(parent); |
| 73 | else |
| 74 | return "unknown"; |
| 75 | } |
| 76 | |
| 77 | static ssize_t provider_show(struct device *dev, |
| 78 | struct device_attribute *attr, char *buf) |
| 79 | { |
| 80 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 81 | |
| 82 | return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus)); |
| 83 | } |
| 84 | static DEVICE_ATTR_RO(provider); |
| 85 | |
| 86 | static struct attribute *nvdimm_bus_attributes[] = { |
| 87 | &dev_attr_provider.attr, |
| 88 | NULL, |
| 89 | }; |
| 90 | |
| 91 | struct attribute_group nvdimm_bus_attribute_group = { |
| 92 | .attrs = nvdimm_bus_attributes, |
| 93 | }; |
| 94 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); |
| 95 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 96 | struct nvdimm_bus *nvdimm_bus_register(struct device *parent, |
| 97 | struct nvdimm_bus_descriptor *nd_desc) |
| 98 | { |
| 99 | struct nvdimm_bus *nvdimm_bus; |
| 100 | int rc; |
| 101 | |
| 102 | nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL); |
| 103 | if (!nvdimm_bus) |
| 104 | return NULL; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 105 | INIT_LIST_HEAD(&nvdimm_bus->list); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 106 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); |
| 107 | if (nvdimm_bus->id < 0) { |
| 108 | kfree(nvdimm_bus); |
| 109 | return NULL; |
| 110 | } |
| 111 | nvdimm_bus->nd_desc = nd_desc; |
| 112 | nvdimm_bus->dev.parent = parent; |
| 113 | nvdimm_bus->dev.release = nvdimm_bus_release; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 114 | nvdimm_bus->dev.groups = nd_desc->attr_groups; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 115 | dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id); |
| 116 | rc = device_register(&nvdimm_bus->dev); |
| 117 | if (rc) { |
| 118 | dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 119 | goto err; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 122 | rc = nvdimm_bus_create_ndctl(nvdimm_bus); |
| 123 | if (rc) |
| 124 | goto err; |
| 125 | |
| 126 | mutex_lock(&nvdimm_bus_list_mutex); |
| 127 | list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list); |
| 128 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 129 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 130 | return nvdimm_bus; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 131 | err: |
| 132 | put_device(&nvdimm_bus->dev); |
| 133 | return NULL; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 134 | } |
| 135 | EXPORT_SYMBOL_GPL(nvdimm_bus_register); |
| 136 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame^] | 137 | static int child_unregister(struct device *dev, void *data) |
| 138 | { |
| 139 | /* |
| 140 | * the singular ndctl class device per bus needs to be |
| 141 | * "device_destroy"ed, so skip it here |
| 142 | * |
| 143 | * i.e. remove classless children |
| 144 | */ |
| 145 | if (dev->class) |
| 146 | /* pass */; |
| 147 | else |
| 148 | device_unregister(dev); |
| 149 | return 0; |
| 150 | } |
| 151 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 152 | void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) |
| 153 | { |
| 154 | if (!nvdimm_bus) |
| 155 | return; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 156 | |
| 157 | mutex_lock(&nvdimm_bus_list_mutex); |
| 158 | list_del_init(&nvdimm_bus->list); |
| 159 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 160 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame^] | 161 | device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 162 | nvdimm_bus_destroy_ndctl(nvdimm_bus); |
| 163 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 164 | device_unregister(&nvdimm_bus->dev); |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); |
| 167 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 168 | static __init int libnvdimm_init(void) |
| 169 | { |
| 170 | return nvdimm_bus_init(); |
| 171 | } |
| 172 | |
| 173 | static __exit void libnvdimm_exit(void) |
| 174 | { |
| 175 | WARN_ON(!list_empty(&nvdimm_bus_list)); |
| 176 | nvdimm_bus_exit(); |
| 177 | } |
| 178 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 179 | MODULE_LICENSE("GPL v2"); |
| 180 | MODULE_AUTHOR("Intel Corporation"); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 181 | subsys_initcall(libnvdimm_init); |
| 182 | module_exit(libnvdimm_exit); |