Linux Audio

Check our new training course

In-person Linux kernel drivers training

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