xref: /linux/drivers/s390/cio/scm.c (revision 15f83bb0191261adece5a26bfdf93c6eccdbc0bb)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 
9 #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 
18 static struct device *scm_root;
19 
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 
23 static 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 
31 static 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 	if (scmdrv->remove)
37 		scmdrv->remove(scmdev);
38 
39 	return 0;
40 }
41 
42 static int scmdev_uevent(struct device *dev, struct kobj_uevent_env *env)
43 {
44 	return add_uevent_var(env, "MODALIAS=scm:scmdev");
45 }
46 
47 static struct bus_type scm_bus_type = {
48 	.name  = "scm",
49 	.probe = scmdev_probe,
50 	.remove = scmdev_remove,
51 	.uevent = scmdev_uevent,
52 };
53 
54 /**
55  * scm_driver_register() - register a scm driver
56  * @scmdrv: driver to be registered
57  */
58 int scm_driver_register(struct scm_driver *scmdrv)
59 {
60 	struct device_driver *drv = &scmdrv->drv;
61 
62 	drv->bus = &scm_bus_type;
63 
64 	return driver_register(drv);
65 }
66 EXPORT_SYMBOL_GPL(scm_driver_register);
67 
68 /**
69  * scm_driver_unregister() - deregister a scm driver
70  * @scmdrv: driver to be deregistered
71  */
72 void scm_driver_unregister(struct scm_driver *scmdrv)
73 {
74 	driver_unregister(&scmdrv->drv);
75 }
76 EXPORT_SYMBOL_GPL(scm_driver_unregister);
77 
78 void scm_irq_handler(struct aob *aob, blk_status_t error)
79 {
80 	struct aob_rq_header *aobrq = (void *) aob->request.data;
81 	struct scm_device *scmdev = aobrq->scmdev;
82 	struct scm_driver *scmdrv = to_scm_drv(scmdev->dev.driver);
83 
84 	scmdrv->handler(scmdev, aobrq->data, error);
85 }
86 EXPORT_SYMBOL_GPL(scm_irq_handler);
87 
88 #define scm_attr(name)							\
89 static ssize_t show_##name(struct device *dev,				\
90 	       struct device_attribute *attr, char *buf)		\
91 {									\
92 	struct scm_device *scmdev = to_scm_dev(dev);			\
93 	int ret;							\
94 									\
95 	device_lock(dev);						\
96 	ret = sprintf(buf, "%u\n", scmdev->attrs.name);			\
97 	device_unlock(dev);						\
98 									\
99 	return ret;							\
100 }									\
101 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
102 
103 scm_attr(persistence);
104 scm_attr(oper_state);
105 scm_attr(data_state);
106 scm_attr(rank);
107 scm_attr(release);
108 scm_attr(res_id);
109 
110 static struct attribute *scmdev_attrs[] = {
111 	&dev_attr_persistence.attr,
112 	&dev_attr_oper_state.attr,
113 	&dev_attr_data_state.attr,
114 	&dev_attr_rank.attr,
115 	&dev_attr_release.attr,
116 	&dev_attr_res_id.attr,
117 	NULL,
118 };
119 
120 static struct attribute_group scmdev_attr_group = {
121 	.attrs = scmdev_attrs,
122 };
123 
124 static const struct attribute_group *scmdev_attr_groups[] = {
125 	&scmdev_attr_group,
126 	NULL,
127 };
128 
129 static void scmdev_release(struct device *dev)
130 {
131 	struct scm_device *scmdev = to_scm_dev(dev);
132 
133 	kfree(scmdev);
134 }
135 
136 static void scmdev_setup(struct scm_device *scmdev, struct sale *sale,
137 			 unsigned int size, unsigned int max_blk_count)
138 {
139 	dev_set_name(&scmdev->dev, "%016llx", (unsigned long long) sale->sa);
140 	scmdev->nr_max_block = max_blk_count;
141 	scmdev->address = sale->sa;
142 	scmdev->size = 1UL << size;
143 	scmdev->attrs.rank = sale->rank;
144 	scmdev->attrs.persistence = sale->p;
145 	scmdev->attrs.oper_state = sale->op_state;
146 	scmdev->attrs.data_state = sale->data_state;
147 	scmdev->attrs.rank = sale->rank;
148 	scmdev->attrs.release = sale->r;
149 	scmdev->attrs.res_id = sale->rid;
150 	scmdev->dev.parent = scm_root;
151 	scmdev->dev.bus = &scm_bus_type;
152 	scmdev->dev.release = scmdev_release;
153 	scmdev->dev.groups = scmdev_attr_groups;
154 }
155 
156 /*
157  * Check for state-changes, notify the driver and userspace.
158  */
159 static void scmdev_update(struct scm_device *scmdev, struct sale *sale)
160 {
161 	struct scm_driver *scmdrv;
162 	bool changed;
163 
164 	device_lock(&scmdev->dev);
165 	changed = scmdev->attrs.rank != sale->rank ||
166 		  scmdev->attrs.oper_state != sale->op_state;
167 	scmdev->attrs.rank = sale->rank;
168 	scmdev->attrs.oper_state = sale->op_state;
169 	if (!scmdev->dev.driver)
170 		goto out;
171 	scmdrv = to_scm_drv(scmdev->dev.driver);
172 	if (changed && scmdrv->notify)
173 		scmdrv->notify(scmdev, SCM_CHANGE);
174 out:
175 	device_unlock(&scmdev->dev);
176 	if (changed)
177 		kobject_uevent(&scmdev->dev.kobj, KOBJ_CHANGE);
178 }
179 
180 static int check_address(struct device *dev, const void *data)
181 {
182 	struct scm_device *scmdev = to_scm_dev(dev);
183 	const struct sale *sale = data;
184 
185 	return scmdev->address == sale->sa;
186 }
187 
188 static struct scm_device *scmdev_find(struct sale *sale)
189 {
190 	struct device *dev;
191 
192 	dev = bus_find_device(&scm_bus_type, NULL, sale, check_address);
193 
194 	return dev ? to_scm_dev(dev) : NULL;
195 }
196 
197 static int scm_add(struct chsc_scm_info *scm_info, size_t num)
198 {
199 	struct sale *sale, *scmal = scm_info->scmal;
200 	struct scm_device *scmdev;
201 	int ret;
202 
203 	for (sale = scmal; sale < scmal + num; sale++) {
204 		scmdev = scmdev_find(sale);
205 		if (scmdev) {
206 			scmdev_update(scmdev, sale);
207 			/* Release reference from scm_find(). */
208 			put_device(&scmdev->dev);
209 			continue;
210 		}
211 		scmdev = kzalloc(sizeof(*scmdev), GFP_KERNEL);
212 		if (!scmdev)
213 			return -ENODEV;
214 		scmdev_setup(scmdev, sale, scm_info->is, scm_info->mbc);
215 		ret = device_register(&scmdev->dev);
216 		if (ret) {
217 			/* Release reference from device_initialize(). */
218 			put_device(&scmdev->dev);
219 			return ret;
220 		}
221 	}
222 
223 	return 0;
224 }
225 
226 int scm_update_information(void)
227 {
228 	struct chsc_scm_info *scm_info;
229 	u64 token = 0;
230 	size_t num;
231 	int ret;
232 
233 	scm_info = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
234 	if (!scm_info)
235 		return -ENOMEM;
236 
237 	do {
238 		ret = chsc_scm_info(scm_info, token);
239 		if (ret)
240 			break;
241 
242 		num = (scm_info->response.length -
243 		       (offsetof(struct chsc_scm_info, scmal) -
244 			offsetof(struct chsc_scm_info, response))
245 		      ) / sizeof(struct sale);
246 
247 		ret = scm_add(scm_info, num);
248 		if (ret)
249 			break;
250 
251 		token = scm_info->restok;
252 	} while (token);
253 
254 	free_page((unsigned long)scm_info);
255 
256 	return ret;
257 }
258 
259 static int scm_dev_avail(struct device *dev, void *unused)
260 {
261 	struct scm_driver *scmdrv = to_scm_drv(dev->driver);
262 	struct scm_device *scmdev = to_scm_dev(dev);
263 
264 	if (dev->driver && scmdrv->notify)
265 		scmdrv->notify(scmdev, SCM_AVAIL);
266 
267 	return 0;
268 }
269 
270 int scm_process_availability_information(void)
271 {
272 	return bus_for_each_dev(&scm_bus_type, NULL, NULL, scm_dev_avail);
273 }
274 
275 static int __init scm_init(void)
276 {
277 	int ret;
278 
279 	ret = bus_register(&scm_bus_type);
280 	if (ret)
281 		return ret;
282 
283 	scm_root = root_device_register("scm");
284 	if (IS_ERR(scm_root)) {
285 		bus_unregister(&scm_bus_type);
286 		return PTR_ERR(scm_root);
287 	}
288 
289 	scm_update_information();
290 	return 0;
291 }
292 subsys_initcall_sync(scm_init);
293