blob: 6bca1d5455d4f6ce1997d39d09792a90e65511a0 [file] [log] [blame]
Greg Kroah-Hartman724117b2017-11-14 18:38:02 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Ott1d1c8f72012-08-28 16:46:26 +02002/*
3 * Recognize and maintain s390 storage class memory.
4 *
5 * Copyright IBM Corp. 2012
6 * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
7 */
8
Sebastian Ott1d1c8f72012-08-28 16:46:26 +02009#include <linux/device.h>
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/slab.h>
13#include <linux/init.h>
14#include <linux/err.h>
15#include <asm/eadm.h>
16#include "chsc.h"
17
18static struct device *scm_root;
Sebastian Ott1d1c8f72012-08-28 16:46:26 +020019
20#define to_scm_dev(n) container_of(n, struct scm_device, dev)
21#define to_scm_drv(d) container_of(d, struct scm_driver, drv)
22
23static int scmdev_probe(struct device *dev)
24{
25 struct scm_device *scmdev = to_scm_dev(dev);
26 struct scm_driver *scmdrv = to_scm_drv(dev->driver);
27
28 return scmdrv->probe ? scmdrv->probe(scmdev) : -ENODEV;
29}
30
31static int scmdev_remove(struct device *dev)
32{
33 struct scm_device *scmdev = to_scm_dev(dev);
34 struct scm_driver *scmdrv = to_scm_drv(dev->driver);
35
36 return scmdrv->remove ? scmdrv->remove(scmdev) : -ENODEV;
37}
38
39static int scmdev_uevent(struct device *dev, struct kobj_uevent_env *env)
40{
41 return add_uevent_var(env, "MODALIAS=scm:scmdev");
42}
43
44static struct bus_type scm_bus_type = {
45 .name = "scm",
46 .probe = scmdev_probe,
47 .remove = scmdev_remove,
48 .uevent = scmdev_uevent,
49};
50
51/**
52 * scm_driver_register() - register a scm driver
53 * @scmdrv: driver to be registered
54 */
55int scm_driver_register(struct scm_driver *scmdrv)
56{
57 struct device_driver *drv = &scmdrv->drv;
58
59 drv->bus = &scm_bus_type;
60
61 return driver_register(drv);
62}
63EXPORT_SYMBOL_GPL(scm_driver_register);
64
65/**
66 * scm_driver_unregister() - deregister a scm driver
67 * @scmdrv: driver to be deregistered
68 */
69void scm_driver_unregister(struct scm_driver *scmdrv)
70{
71 driver_unregister(&scmdrv->drv);
72}
73EXPORT_SYMBOL_GPL(scm_driver_unregister);
74
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020075void scm_irq_handler(struct aob *aob, blk_status_t error)
Sebastian Ott1d1c8f72012-08-28 16:46:26 +020076{
77 struct aob_rq_header *aobrq = (void *) aob->request.data;
78 struct scm_device *scmdev = aobrq->scmdev;
79 struct scm_driver *scmdrv = to_scm_drv(scmdev->dev.driver);
80
81 scmdrv->handler(scmdev, aobrq->data, error);
82}
83EXPORT_SYMBOL_GPL(scm_irq_handler);
84
85#define scm_attr(name) \
86static ssize_t show_##name(struct device *dev, \
87 struct device_attribute *attr, char *buf) \
88{ \
89 struct scm_device *scmdev = to_scm_dev(dev); \
90 int ret; \
91 \
Sebastian Ottc3e6d402012-09-04 19:36:41 +020092 device_lock(dev); \
Sebastian Ott1d1c8f72012-08-28 16:46:26 +020093 ret = sprintf(buf, "%u\n", scmdev->attrs.name); \
Sebastian Ottc3e6d402012-09-04 19:36:41 +020094 device_unlock(dev); \
Sebastian Ott1d1c8f72012-08-28 16:46:26 +020095 \
96 return ret; \
97} \
98static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
99
100scm_attr(persistence);
101scm_attr(oper_state);
102scm_attr(data_state);
103scm_attr(rank);
104scm_attr(release);
105scm_attr(res_id);
106
107static struct attribute *scmdev_attrs[] = {
108 &dev_attr_persistence.attr,
109 &dev_attr_oper_state.attr,
110 &dev_attr_data_state.attr,
111 &dev_attr_rank.attr,
112 &dev_attr_release.attr,
113 &dev_attr_res_id.attr,
114 NULL,
115};
116
117static struct attribute_group scmdev_attr_group = {
118 .attrs = scmdev_attrs,
119};
120
121static const struct attribute_group *scmdev_attr_groups[] = {
122 &scmdev_attr_group,
123 NULL,
124};
125
126static void scmdev_release(struct device *dev)
127{
128 struct scm_device *scmdev = to_scm_dev(dev);
129
130 kfree(scmdev);
131}
132
133static void scmdev_setup(struct scm_device *scmdev, struct sale *sale,
134 unsigned int size, unsigned int max_blk_count)
135{
136 dev_set_name(&scmdev->dev, "%016llx", (unsigned long long) sale->sa);
137 scmdev->nr_max_block = max_blk_count;
138 scmdev->address = sale->sa;
139 scmdev->size = 1UL << size;
140 scmdev->attrs.rank = sale->rank;
141 scmdev->attrs.persistence = sale->p;
142 scmdev->attrs.oper_state = sale->op_state;
143 scmdev->attrs.data_state = sale->data_state;
144 scmdev->attrs.rank = sale->rank;
145 scmdev->attrs.release = sale->r;
146 scmdev->attrs.res_id = sale->rid;
147 scmdev->dev.parent = scm_root;
148 scmdev->dev.bus = &scm_bus_type;
149 scmdev->dev.release = scmdev_release;
150 scmdev->dev.groups = scmdev_attr_groups;
Sebastian Ott1d1c8f72012-08-28 16:46:26 +0200151}
152
Sebastian Ott40ff4cc2012-08-28 16:47:02 +0200153/*
154 * Check for state-changes, notify the driver and userspace.
155 */
156static void scmdev_update(struct scm_device *scmdev, struct sale *sale)
157{
158 struct scm_driver *scmdrv;
159 bool changed;
160
161 device_lock(&scmdev->dev);
162 changed = scmdev->attrs.rank != sale->rank ||
163 scmdev->attrs.oper_state != sale->op_state;
164 scmdev->attrs.rank = sale->rank;
165 scmdev->attrs.oper_state = sale->op_state;
166 if (!scmdev->dev.driver)
167 goto out;
168 scmdrv = to_scm_drv(scmdev->dev.driver);
169 if (changed && scmdrv->notify)
Sebastian Ott93481c92013-02-28 12:07:38 +0100170 scmdrv->notify(scmdev, SCM_CHANGE);
Sebastian Ott40ff4cc2012-08-28 16:47:02 +0200171out:
172 device_unlock(&scmdev->dev);
173 if (changed)
174 kobject_uevent(&scmdev->dev.kobj, KOBJ_CHANGE);
175}
176
177static int check_address(struct device *dev, void *data)
178{
179 struct scm_device *scmdev = to_scm_dev(dev);
180 struct sale *sale = data;
181
182 return scmdev->address == sale->sa;
183}
184
185static struct scm_device *scmdev_find(struct sale *sale)
186{
187 struct device *dev;
188
189 dev = bus_find_device(&scm_bus_type, NULL, sale, check_address);
190
191 return dev ? to_scm_dev(dev) : NULL;
192}
193
Sebastian Ott1d1c8f72012-08-28 16:46:26 +0200194static int scm_add(struct chsc_scm_info *scm_info, size_t num)
195{
196 struct sale *sale, *scmal = scm_info->scmal;
197 struct scm_device *scmdev;
198 int ret;
199
200 for (sale = scmal; sale < scmal + num; sale++) {
Sebastian Ott40ff4cc2012-08-28 16:47:02 +0200201 scmdev = scmdev_find(sale);
202 if (scmdev) {
203 scmdev_update(scmdev, sale);
204 /* Release reference from scm_find(). */
205 put_device(&scmdev->dev);
206 continue;
207 }
Sebastian Ott1d1c8f72012-08-28 16:46:26 +0200208 scmdev = kzalloc(sizeof(*scmdev), GFP_KERNEL);
209 if (!scmdev)
210 return -ENODEV;
211 scmdev_setup(scmdev, sale, scm_info->is, scm_info->mbc);
212 ret = device_register(&scmdev->dev);
213 if (ret) {
214 /* Release reference from device_initialize(). */
215 put_device(&scmdev->dev);
216 return ret;
217 }
218 }
219
220 return 0;
221}
222
Sebastian Ott40ff4cc2012-08-28 16:47:02 +0200223int scm_update_information(void)
Sebastian Ott1d1c8f72012-08-28 16:46:26 +0200224{
225 struct chsc_scm_info *scm_info;
226 u64 token = 0;
227 size_t num;
228 int ret;
229
230 scm_info = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
231 if (!scm_info)
232 return -ENOMEM;
233
234 do {
235 ret = chsc_scm_info(scm_info, token);
236 if (ret)
237 break;
238
239 num = (scm_info->response.length -
240 (offsetof(struct chsc_scm_info, scmal) -
241 offsetof(struct chsc_scm_info, response))
242 ) / sizeof(struct sale);
243
244 ret = scm_add(scm_info, num);
245 if (ret)
246 break;
247
248 token = scm_info->restok;
249 } while (token);
250
251 free_page((unsigned long)scm_info);
252
253 return ret;
254}
255
Sebastian Ottaebfa662013-02-28 12:07:55 +0100256static int scm_dev_avail(struct device *dev, void *unused)
257{
258 struct scm_driver *scmdrv = to_scm_drv(dev->driver);
259 struct scm_device *scmdev = to_scm_dev(dev);
260
261 if (dev->driver && scmdrv->notify)
262 scmdrv->notify(scmdev, SCM_AVAIL);
263
264 return 0;
265}
266
267int scm_process_availability_information(void)
268{
269 return bus_for_each_dev(&scm_bus_type, NULL, NULL, scm_dev_avail);
270}
271
Sebastian Ott1d1c8f72012-08-28 16:46:26 +0200272static int __init scm_init(void)
273{
274 int ret;
275
276 ret = bus_register(&scm_bus_type);
277 if (ret)
278 return ret;
279
280 scm_root = root_device_register("scm");
281 if (IS_ERR(scm_root)) {
282 bus_unregister(&scm_bus_type);
283 return PTR_ERR(scm_root);
284 }
285
286 scm_update_information();
287 return 0;
288}
289subsys_initcall_sync(scm_init);