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 | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 21 | static LIST_HEAD(nvdimm_bus_list); |
| 22 | static 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 | |
| 51 | static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus) |
| 52 | { |
| 53 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 54 | struct device *parent = nvdimm_bus->dev.parent; |
| 55 | |
| 56 | if (nd_desc->provider_name) |
| 57 | return nd_desc->provider_name; |
| 58 | else if (parent) |
| 59 | return dev_name(parent); |
| 60 | else |
| 61 | return "unknown"; |
| 62 | } |
| 63 | |
| 64 | static ssize_t provider_show(struct device *dev, |
| 65 | struct device_attribute *attr, char *buf) |
| 66 | { |
| 67 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 68 | |
| 69 | return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus)); |
| 70 | } |
| 71 | static DEVICE_ATTR_RO(provider); |
| 72 | |
| 73 | static struct attribute *nvdimm_bus_attributes[] = { |
| 74 | &dev_attr_provider.attr, |
| 75 | NULL, |
| 76 | }; |
| 77 | |
| 78 | struct attribute_group nvdimm_bus_attribute_group = { |
| 79 | .attrs = nvdimm_bus_attributes, |
| 80 | }; |
| 81 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); |
| 82 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 83 | struct nvdimm_bus *nvdimm_bus_register(struct device *parent, |
| 84 | struct nvdimm_bus_descriptor *nd_desc) |
| 85 | { |
| 86 | struct nvdimm_bus *nvdimm_bus; |
| 87 | int rc; |
| 88 | |
| 89 | nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL); |
| 90 | if (!nvdimm_bus) |
| 91 | return NULL; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 92 | INIT_LIST_HEAD(&nvdimm_bus->list); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 93 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); |
| 94 | if (nvdimm_bus->id < 0) { |
| 95 | kfree(nvdimm_bus); |
| 96 | return NULL; |
| 97 | } |
| 98 | nvdimm_bus->nd_desc = nd_desc; |
| 99 | nvdimm_bus->dev.parent = parent; |
| 100 | nvdimm_bus->dev.release = nvdimm_bus_release; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 101 | nvdimm_bus->dev.groups = nd_desc->attr_groups; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 102 | dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id); |
| 103 | rc = device_register(&nvdimm_bus->dev); |
| 104 | if (rc) { |
| 105 | dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 106 | goto err; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 109 | rc = nvdimm_bus_create_ndctl(nvdimm_bus); |
| 110 | if (rc) |
| 111 | goto err; |
| 112 | |
| 113 | mutex_lock(&nvdimm_bus_list_mutex); |
| 114 | list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list); |
| 115 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 116 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 117 | return nvdimm_bus; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 118 | err: |
| 119 | put_device(&nvdimm_bus->dev); |
| 120 | return NULL; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 121 | } |
| 122 | EXPORT_SYMBOL_GPL(nvdimm_bus_register); |
| 123 | |
| 124 | void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) |
| 125 | { |
| 126 | if (!nvdimm_bus) |
| 127 | return; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 128 | |
| 129 | mutex_lock(&nvdimm_bus_list_mutex); |
| 130 | list_del_init(&nvdimm_bus->list); |
| 131 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 132 | |
| 133 | nvdimm_bus_destroy_ndctl(nvdimm_bus); |
| 134 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 135 | device_unregister(&nvdimm_bus->dev); |
| 136 | } |
| 137 | EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); |
| 138 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 139 | static __init int libnvdimm_init(void) |
| 140 | { |
| 141 | return nvdimm_bus_init(); |
| 142 | } |
| 143 | |
| 144 | static __exit void libnvdimm_exit(void) |
| 145 | { |
| 146 | WARN_ON(!list_empty(&nvdimm_bus_list)); |
| 147 | nvdimm_bus_exit(); |
| 148 | } |
| 149 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 150 | MODULE_LICENSE("GPL v2"); |
| 151 | MODULE_AUTHOR("Intel Corporation"); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame^] | 152 | subsys_initcall(libnvdimm_init); |
| 153 | module_exit(libnvdimm_exit); |