2 * Recognize and maintain s390 storage class memory.
4 * Copyright IBM Corp. 2012
5 * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
17 static struct device *scm_root;
18 static struct eadm_ops *eadm_ops;
19 static DEFINE_MUTEX(eadm_ops_mutex);
21 #define to_scm_dev(n) container_of(n, struct scm_device, dev)
22 #define to_scm_drv(d) container_of(d, struct scm_driver, drv)
24 static int scmdev_probe(struct device *dev)
26 struct scm_device *scmdev = to_scm_dev(dev);
27 struct scm_driver *scmdrv = to_scm_drv(dev->driver);
29 return scmdrv->probe ? scmdrv->probe(scmdev) : -ENODEV;
32 static int scmdev_remove(struct device *dev)
34 struct scm_device *scmdev = to_scm_dev(dev);
35 struct scm_driver *scmdrv = to_scm_drv(dev->driver);
37 return scmdrv->remove ? scmdrv->remove(scmdev) : -ENODEV;
40 static int scmdev_uevent(struct device *dev, struct kobj_uevent_env *env)
42 return add_uevent_var(env, "MODALIAS=scm:scmdev");
45 static struct bus_type scm_bus_type = {
47 .probe = scmdev_probe,
48 .remove = scmdev_remove,
49 .uevent = scmdev_uevent,
53 * scm_driver_register() - register a scm driver
54 * @scmdrv: driver to be registered
56 int scm_driver_register(struct scm_driver *scmdrv)
58 struct device_driver *drv = &scmdrv->drv;
60 drv->bus = &scm_bus_type;
62 return driver_register(drv);
64 EXPORT_SYMBOL_GPL(scm_driver_register);
67 * scm_driver_unregister() - deregister a scm driver
68 * @scmdrv: driver to be deregistered
70 void scm_driver_unregister(struct scm_driver *scmdrv)
72 driver_unregister(&scmdrv->drv);
74 EXPORT_SYMBOL_GPL(scm_driver_unregister);
80 mutex_lock(&eadm_ops_mutex);
81 if (!eadm_ops || !try_module_get(eadm_ops->owner))
83 mutex_unlock(&eadm_ops_mutex);
87 EXPORT_SYMBOL_GPL(scm_get_ref);
89 void scm_put_ref(void)
91 mutex_lock(&eadm_ops_mutex);
92 module_put(eadm_ops->owner);
93 mutex_unlock(&eadm_ops_mutex);
95 EXPORT_SYMBOL_GPL(scm_put_ref);
97 void register_eadm_ops(struct eadm_ops *ops)
99 mutex_lock(&eadm_ops_mutex);
101 mutex_unlock(&eadm_ops_mutex);
103 EXPORT_SYMBOL_GPL(register_eadm_ops);
105 void unregister_eadm_ops(struct eadm_ops *ops)
107 mutex_lock(&eadm_ops_mutex);
109 mutex_unlock(&eadm_ops_mutex);
111 EXPORT_SYMBOL_GPL(unregister_eadm_ops);
113 int scm_start_aob(struct aob *aob)
115 return eadm_ops->eadm_start(aob);
117 EXPORT_SYMBOL_GPL(scm_start_aob);
119 void scm_irq_handler(struct aob *aob, int error)
121 struct aob_rq_header *aobrq = (void *) aob->request.data;
122 struct scm_device *scmdev = aobrq->scmdev;
123 struct scm_driver *scmdrv = to_scm_drv(scmdev->dev.driver);
125 scmdrv->handler(scmdev, aobrq->data, error);
127 EXPORT_SYMBOL_GPL(scm_irq_handler);
129 #define scm_attr(name) \
130 static ssize_t show_##name(struct device *dev, \
131 struct device_attribute *attr, char *buf) \
133 struct scm_device *scmdev = to_scm_dev(dev); \
137 ret = sprintf(buf, "%u\n", scmdev->attrs.name); \
138 device_unlock(dev); \
142 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
144 scm_attr(persistence);
145 scm_attr(oper_state);
146 scm_attr(data_state);
151 static struct attribute *scmdev_attrs[] = {
152 &dev_attr_persistence.attr,
153 &dev_attr_oper_state.attr,
154 &dev_attr_data_state.attr,
156 &dev_attr_release.attr,
157 &dev_attr_res_id.attr,
161 static struct attribute_group scmdev_attr_group = {
162 .attrs = scmdev_attrs,
165 static const struct attribute_group *scmdev_attr_groups[] = {
170 static void scmdev_release(struct device *dev)
172 struct scm_device *scmdev = to_scm_dev(dev);
177 static void scmdev_setup(struct scm_device *scmdev, struct sale *sale,
178 unsigned int size, unsigned int max_blk_count)
180 dev_set_name(&scmdev->dev, "%016llx", (unsigned long long) sale->sa);
181 scmdev->nr_max_block = max_blk_count;
182 scmdev->address = sale->sa;
183 scmdev->size = 1UL << size;
184 scmdev->attrs.rank = sale->rank;
185 scmdev->attrs.persistence = sale->p;
186 scmdev->attrs.oper_state = sale->op_state;
187 scmdev->attrs.data_state = sale->data_state;
188 scmdev->attrs.rank = sale->rank;
189 scmdev->attrs.release = sale->r;
190 scmdev->attrs.res_id = sale->rid;
191 scmdev->dev.parent = scm_root;
192 scmdev->dev.bus = &scm_bus_type;
193 scmdev->dev.release = scmdev_release;
194 scmdev->dev.groups = scmdev_attr_groups;
198 * Check for state-changes, notify the driver and userspace.
200 static void scmdev_update(struct scm_device *scmdev, struct sale *sale)
202 struct scm_driver *scmdrv;
205 device_lock(&scmdev->dev);
206 changed = scmdev->attrs.rank != sale->rank ||
207 scmdev->attrs.oper_state != sale->op_state;
208 scmdev->attrs.rank = sale->rank;
209 scmdev->attrs.oper_state = sale->op_state;
210 if (!scmdev->dev.driver)
212 scmdrv = to_scm_drv(scmdev->dev.driver);
213 if (changed && scmdrv->notify)
214 scmdrv->notify(scmdev, SCM_CHANGE);
216 device_unlock(&scmdev->dev);
218 kobject_uevent(&scmdev->dev.kobj, KOBJ_CHANGE);
221 static int check_address(struct device *dev, void *data)
223 struct scm_device *scmdev = to_scm_dev(dev);
224 struct sale *sale = data;
226 return scmdev->address == sale->sa;
229 static struct scm_device *scmdev_find(struct sale *sale)
233 dev = bus_find_device(&scm_bus_type, NULL, sale, check_address);
235 return dev ? to_scm_dev(dev) : NULL;
238 static int scm_add(struct chsc_scm_info *scm_info, size_t num)
240 struct sale *sale, *scmal = scm_info->scmal;
241 struct scm_device *scmdev;
244 for (sale = scmal; sale < scmal + num; sale++) {
245 scmdev = scmdev_find(sale);
247 scmdev_update(scmdev, sale);
248 /* Release reference from scm_find(). */
249 put_device(&scmdev->dev);
252 scmdev = kzalloc(sizeof(*scmdev), GFP_KERNEL);
255 scmdev_setup(scmdev, sale, scm_info->is, scm_info->mbc);
256 ret = device_register(&scmdev->dev);
258 /* Release reference from device_initialize(). */
259 put_device(&scmdev->dev);
267 int scm_update_information(void)
269 struct chsc_scm_info *scm_info;
274 scm_info = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
279 ret = chsc_scm_info(scm_info, token);
283 num = (scm_info->response.length -
284 (offsetof(struct chsc_scm_info, scmal) -
285 offsetof(struct chsc_scm_info, response))
286 ) / sizeof(struct sale);
288 ret = scm_add(scm_info, num);
292 token = scm_info->restok;
295 free_page((unsigned long)scm_info);
300 static int scm_dev_avail(struct device *dev, void *unused)
302 struct scm_driver *scmdrv = to_scm_drv(dev->driver);
303 struct scm_device *scmdev = to_scm_dev(dev);
305 if (dev->driver && scmdrv->notify)
306 scmdrv->notify(scmdev, SCM_AVAIL);
311 int scm_process_availability_information(void)
313 return bus_for_each_dev(&scm_bus_type, NULL, NULL, scm_dev_avail);
316 static int __init scm_init(void)
320 ret = bus_register(&scm_bus_type);
324 scm_root = root_device_register("scm");
325 if (IS_ERR(scm_root)) {
326 bus_unregister(&scm_bus_type);
327 return PTR_ERR(scm_root);
330 scm_update_information();
333 subsys_initcall_sync(scm_init);