blob: 7679aee6fa147af0459855a899888850752437a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02002 * driver for channel subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02004 * Copyright IBM Corp. 2002, 2009
5 *
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Michael Ernste6d5a422008-12-25 13:39:36 +01009
10#define KMSG_COMPONENT "cio"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/slab.h>
17#include <linux/errno.h>
18#include <linux/list.h>
Cornelia Hucka55360d2007-10-12 16:11:20 +020019#include <linux/reboot.h>
Sebastian Ottdcbd16d2009-06-16 10:30:22 +020020#include <linux/suspend.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020021#include <asm/isc.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010022#include <asm/crw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "css.h"
25#include "cio.h"
26#include "cio_debug.h"
27#include "ioasm.h"
28#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020029#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020030#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020031#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033int css_init_done = 0;
Sebastian Ottb0a285d2009-09-22 22:58:37 +020034int max_ssid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020036struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Heiko Carstens4d284ca2007-02-05 21:18:53 +010038int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080039for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
40{
41 struct subchannel_id schid;
42 int ret;
43
44 init_subchannel_id(&schid);
45 ret = -ENODEV;
46 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080047 do {
48 ret = fn(schid, data);
49 if (ret)
50 break;
51 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
52 schid.sch_no = 0;
53 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080054 return ret;
55}
56
Peter Oberparleitere82a1562008-01-26 14:10:48 +010057struct cb_data {
58 void *data;
59 struct idset *set;
60 int (*fn_known_sch)(struct subchannel *, void *);
61 int (*fn_unknown_sch)(struct subchannel_id, void *);
62};
63
64static int call_fn_known_sch(struct device *dev, void *data)
65{
66 struct subchannel *sch = to_subchannel(dev);
67 struct cb_data *cb = data;
68 int rc = 0;
69
70 idset_sch_del(cb->set, sch->schid);
71 if (cb->fn_known_sch)
72 rc = cb->fn_known_sch(sch, cb->data);
73 return rc;
74}
75
76static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
77{
78 struct cb_data *cb = data;
79 int rc = 0;
80
81 if (idset_sch_contains(cb->set, schid))
82 rc = cb->fn_unknown_sch(schid, cb->data);
83 return rc;
84}
85
Sebastian Ott90ac24a2009-03-26 15:24:11 +010086static int call_fn_all_sch(struct subchannel_id schid, void *data)
87{
88 struct cb_data *cb = data;
89 struct subchannel *sch;
90 int rc = 0;
91
92 sch = get_subchannel_by_schid(schid);
93 if (sch) {
94 if (cb->fn_known_sch)
95 rc = cb->fn_known_sch(sch, cb->data);
96 put_device(&sch->dev);
97 } else {
98 if (cb->fn_unknown_sch)
99 rc = cb->fn_unknown_sch(schid, cb->data);
100 }
101
102 return rc;
103}
104
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100105int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
106 int (*fn_unknown)(struct subchannel_id,
107 void *), void *data)
108{
109 struct cb_data cb;
110 int rc;
111
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100112 cb.data = data;
113 cb.fn_known_sch = fn_known;
114 cb.fn_unknown_sch = fn_unknown;
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100115
116 cb.set = idset_sch_new();
117 if (!cb.set)
118 /* fall back to brute force scanning in case of oom */
119 return for_each_subchannel(call_fn_all_sch, &cb);
120
121 idset_fill(cb.set);
122
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100123 /* Process registered subchannels. */
124 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
125 if (rc)
126 goto out;
127 /* Process unregistered subchannels. */
128 if (fn_unknown)
129 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
130out:
131 idset_free(cb.set);
132
133 return rc;
134}
135
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100136static void css_sch_todo(struct work_struct *work);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800139css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct subchannel *sch;
142 int ret;
143
144 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
145 if (sch == NULL)
146 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800147 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (ret < 0) {
149 kfree(sch);
150 return ERR_PTR(ret);
151 }
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100152 INIT_WORK(&sch->todo_work, css_sch_todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return sch;
154}
155
156static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157css_subchannel_release(struct device *dev)
158{
159 struct subchannel *sch;
160
161 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100162 if (!cio_is_console(sch->schid)) {
Peter Oberparleiter1da73bc2009-09-11 10:28:16 +0200163 /* Reset intparm to zeroes. */
164 sch->config.intparm = 0;
165 cio_commit_config(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100166 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Cornelia Huck07c6a332007-07-27 12:29:10 +0200171static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200172{
173 int ret;
174
175 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200176 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
177 sch->schid.sch_no);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200178 ret = device_register(&sch->dev);
179 mutex_unlock(&sch->reg_mutex);
180 return ret;
181}
182
Cornelia Huck44a1c192008-07-14 09:58:47 +0200183/**
184 * css_sch_device_unregister - unregister a subchannel
185 * @sch: subchannel to be unregistered
186 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200187void css_sch_device_unregister(struct subchannel *sch)
188{
189 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200190 if (device_is_registered(&sch->dev))
191 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200192 mutex_unlock(&sch->reg_mutex);
193}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200194EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200195
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100196static void css_sch_todo(struct work_struct *work)
197{
198 struct subchannel *sch;
199 enum sch_todo todo;
200
201 sch = container_of(work, struct subchannel, todo_work);
202 /* Find out todo. */
203 spin_lock_irq(sch->lock);
204 todo = sch->todo;
205 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
206 sch->schid.sch_no, todo);
207 sch->todo = SCH_TODO_NOTHING;
208 spin_unlock_irq(sch->lock);
209 /* Perform todo. */
210 if (todo == SCH_TODO_UNREG)
211 css_sch_device_unregister(sch);
212 /* Release workqueue ref. */
213 put_device(&sch->dev);
214}
215
216/**
217 * css_sched_sch_todo - schedule a subchannel operation
218 * @sch: subchannel
219 * @todo: todo
220 *
221 * Schedule the operation identified by @todo to be performed on the slow path
222 * workqueue. Do nothing if another operation with higher priority is already
223 * scheduled. Needs to be called with subchannel lock held.
224 */
225void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
226{
227 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
228 sch->schid.ssid, sch->schid.sch_no, todo);
229 if (sch->todo >= todo)
230 return;
231 /* Get workqueue ref. */
232 if (!get_device(&sch->dev))
233 return;
234 sch->todo = todo;
235 if (!queue_work(slow_path_wq, &sch->todo_work)) {
236 /* Already queued, release workqueue ref. */
237 put_device(&sch->dev);
238 }
239}
240
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200241static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
242{
243 int i;
244 int mask;
245
246 memset(ssd, 0, sizeof(struct chsc_ssd_info));
247 ssd->path_mask = pmcw->pim;
248 for (i = 0; i < 8; i++) {
249 mask = 0x80 >> i;
250 if (pmcw->pim & mask) {
251 chp_id_init(&ssd->chpid[i]);
252 ssd->chpid[i].id = pmcw->chpid[i];
253 }
254 }
255}
256
257static void ssd_register_chpids(struct chsc_ssd_info *ssd)
258{
259 int i;
260 int mask;
261
262 for (i = 0; i < 8; i++) {
263 mask = 0x80 >> i;
264 if (ssd->path_mask & mask)
265 if (!chp_is_registered(ssd->chpid[i]))
266 chp_new(ssd->chpid[i]);
267 }
268}
269
270void css_update_ssd_info(struct subchannel *sch)
271{
272 int ret;
273
274 if (cio_is_console(sch->schid)) {
275 /* Console is initialized too early for functions requiring
276 * memory allocation. */
277 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
278 } else {
279 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
280 if (ret)
281 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
282 ssd_register_chpids(&sch->ssd_info);
283 }
284}
285
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200286static ssize_t type_show(struct device *dev, struct device_attribute *attr,
287 char *buf)
288{
289 struct subchannel *sch = to_subchannel(dev);
290
291 return sprintf(buf, "%01x\n", sch->st);
292}
293
294static DEVICE_ATTR(type, 0444, type_show, NULL);
295
296static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
297 char *buf)
298{
299 struct subchannel *sch = to_subchannel(dev);
300
301 return sprintf(buf, "css:t%01X\n", sch->st);
302}
303
304static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
305
306static struct attribute *subch_attrs[] = {
307 &dev_attr_type.attr,
308 &dev_attr_modalias.attr,
309 NULL,
310};
311
312static struct attribute_group subch_attr_group = {
313 .attrs = subch_attrs,
314};
315
David Brownella4dbd672009-06-24 10:06:31 -0700316static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200317 &subch_attr_group,
318 NULL,
319};
320
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200321static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int ret;
324
325 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200326 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sch->dev.bus = &css_bus_type;
328 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200329 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200330 /*
331 * We don't want to generate uevents for I/O subchannels that don't
332 * have a working ccw device behind them since they will be
333 * unregistered before they can be used anyway, so we delay the add
334 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200335 * Note that we suppress the uevent for all subchannel types;
336 * the subchannel driver can decide itself when it wants to inform
337 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200338 */
Ming Leif67f1292009-03-01 21:10:49 +0800339 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200340 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200342 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100343 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200344 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
345 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100346 return ret;
347 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200348 if (!sch->driver) {
349 /*
350 * No driver matched. Generate the uevent now so that
351 * a fitting driver module may be loaded based on the
352 * modalias.
353 */
Ming Leif67f1292009-03-01 21:10:49 +0800354 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200355 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return ret;
358}
359
Cornelia Huckc820de32008-07-14 09:58:45 +0200360int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 int ret;
363 struct subchannel *sch;
364
Sebastian Ott703e5c92009-09-22 22:58:38 +0200365 if (cio_is_console(schid))
366 sch = cio_get_console_subchannel();
367 else {
368 sch = css_alloc_subchannel(schid);
369 if (IS_ERR(sch))
370 return PTR_ERR(sch);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ret = css_register_subchannel(sch);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200373 if (ret) {
374 if (!cio_is_console(schid))
375 put_device(&sch->dev);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return ret;
378}
379
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700380static int
381check_subchannel(struct device * dev, void * data)
382{
383 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800384 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700385
386 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800387 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800391get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct device *dev;
394
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700395 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200396 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700398 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100401/**
402 * css_sch_is_valid() - check if a subchannel is valid
403 * @schib: subchannel information block for the subchannel
404 */
405int css_sch_is_valid(struct schib *schib)
406{
407 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
408 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200409 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
410 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100411 return 1;
412}
413EXPORT_SYMBOL_GPL(css_sch_is_valid);
414
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200415static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
416{
417 struct schib schib;
418
419 if (!slow) {
420 /* Will be done on the slow path. */
421 return -EAGAIN;
422 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100423 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200424 /* Unusable - ignore. */
425 return 0;
426 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100427 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
428 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200429
430 return css_probe_device(schid);
431}
432
Cornelia Huckc820de32008-07-14 09:58:45 +0200433static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
434{
435 int ret = 0;
436
437 if (sch->driver) {
438 if (sch->driver->sch_event)
439 ret = sch->driver->sch_event(sch, slow);
440 else
441 dev_dbg(&sch->dev,
442 "Got subchannel machine check but "
443 "no sch_event handler provided.\n");
444 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100445 if (ret != 0 && ret != -EAGAIN) {
446 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
447 sch->schid.ssid, sch->schid.sch_no, ret);
448 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200449 return ret;
450}
451
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200452static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200453{
454 struct subchannel *sch;
455 int ret;
456
457 sch = get_subchannel_by_schid(schid);
458 if (sch) {
459 ret = css_evaluate_known_subchannel(sch, slow);
460 put_device(&sch->dev);
461 } else
462 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200463 if (ret == -EAGAIN)
464 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200467static struct idset *slow_subchannel_set;
468static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200469static wait_queue_head_t css_eval_wq;
470static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200471
472static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200474 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200475 atomic_set(&css_eval_scheduled, 0);
476 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200477 slow_subchannel_set = idset_sch_new();
478 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200479 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200480 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200482 return 0;
483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100485static int slow_eval_known_fn(struct subchannel *sch, void *data)
486{
487 int eval;
488 int rc;
489
490 spin_lock_irq(&slow_subchannel_lock);
491 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
492 idset_sch_del(slow_subchannel_set, sch->schid);
493 spin_unlock_irq(&slow_subchannel_lock);
494 if (eval) {
495 rc = css_evaluate_known_subchannel(sch, 1);
496 if (rc == -EAGAIN)
497 css_schedule_eval(sch->schid);
498 }
499 return 0;
500}
501
502static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
503{
504 int eval;
505 int rc = 0;
506
507 spin_lock_irq(&slow_subchannel_lock);
508 eval = idset_sch_contains(slow_subchannel_set, schid);
509 idset_sch_del(slow_subchannel_set, schid);
510 spin_unlock_irq(&slow_subchannel_lock);
511 if (eval) {
512 rc = css_evaluate_new_subchannel(schid, 1);
513 switch (rc) {
514 case -EAGAIN:
515 css_schedule_eval(schid);
516 rc = 0;
517 break;
518 case -ENXIO:
519 case -ENOMEM:
520 case -EIO:
521 /* These should abort looping */
522 break;
523 default:
524 rc = 0;
525 }
526 }
527 return rc;
528}
529
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200530static void css_slow_path_func(struct work_struct *unused)
531{
Sebastian Ott25530552009-09-22 22:58:34 +0200532 unsigned long flags;
533
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200534 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100535 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
536 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200537 spin_lock_irqsave(&slow_subchannel_lock, flags);
538 if (idset_is_empty(slow_subchannel_set)) {
539 atomic_set(&css_eval_scheduled, 0);
540 wake_up(&css_eval_wq);
541 }
542 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200545static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546struct workqueue_struct *slow_path_wq;
547
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200548void css_schedule_eval(struct subchannel_id schid)
549{
550 unsigned long flags;
551
552 spin_lock_irqsave(&slow_subchannel_lock, flags);
553 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200554 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200555 queue_work(slow_path_wq, &slow_path_work);
556 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
557}
558
559void css_schedule_eval_all(void)
560{
561 unsigned long flags;
562
563 spin_lock_irqsave(&slow_subchannel_lock, flags);
564 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200565 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200566 queue_work(slow_path_wq, &slow_path_work);
567 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
568}
569
Sebastian Ott703e5c92009-09-22 22:58:38 +0200570static int __unset_registered(struct device *dev, void *data)
571{
572 struct idset *set = data;
573 struct subchannel *sch = to_subchannel(dev);
574
575 idset_sch_del(set, sch->schid);
576 return 0;
577}
578
579void css_schedule_eval_all_unreg(void)
580{
581 unsigned long flags;
582 struct idset *unreg_set;
583
584 /* Find unregistered subchannels. */
585 unreg_set = idset_sch_new();
586 if (!unreg_set) {
587 /* Fallback. */
588 css_schedule_eval_all();
589 return;
590 }
591 idset_fill(unreg_set);
592 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
593 /* Apply to slow_subchannel_set. */
594 spin_lock_irqsave(&slow_subchannel_lock, flags);
595 idset_add_set(slow_subchannel_set, unreg_set);
596 atomic_set(&css_eval_scheduled, 1);
597 queue_work(slow_path_wq, &slow_path_work);
598 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
599 idset_free(unreg_set);
600}
601
Cornelia Huck22806dc2008-04-17 07:45:59 +0200602void css_wait_for_slow_path(void)
603{
Cornelia Huck22806dc2008-04-17 07:45:59 +0200604 flush_workqueue(slow_path_wq);
605}
606
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200607/* Schedule reprobing of all unregistered subchannels. */
608void css_schedule_reprobe(void)
609{
Sebastian Ott703e5c92009-09-22 22:58:38 +0200610 css_schedule_eval_all_unreg();
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200611}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200612EXPORT_SYMBOL_GPL(css_schedule_reprobe);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * Called from the machine check handler for subchannel report words.
616 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200617static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800619 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Cornelia Huckc1156182008-07-14 09:58:46 +0200621 if (overflow) {
622 css_schedule_eval_all();
623 return;
624 }
625 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
626 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
627 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
628 crw0->erc, crw0->rsid);
629 if (crw1)
630 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
631 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
632 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
633 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800634 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200635 mchk_schid.sch_no = crw0->rsid;
636 if (crw1)
637 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800638
Cornelia Huckc1156182008-07-14 09:58:46 +0200639 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Since we are always presented with IPI in the CRW, we have to
641 * use stsch() to find out if the subchannel in question has come
642 * or gone.
643 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200644 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800648css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Cornelia Huck75784c02008-07-14 09:58:57 +0200650 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800651 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
652 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
653 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100655 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800657 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#endif
659 }
Heiko Carstense86a6ed2009-09-11 10:29:04 +0200660 css->global_pgid.cpu_id = S390_lowcore.cpu_id.ident;
661 css->global_pgid.cpu_model = S390_lowcore.cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800662 css->global_pgid.tod_high = tod_high;
663
664}
665
Cornelia Huck3b793062006-01-06 00:19:26 -0800666static void
667channel_subsystem_release(struct device *dev)
668{
669 struct channel_subsystem *css;
670
671 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800672 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200673 if (css->pseudo_subchannel) {
674 /* Implies that it has been generated but never registered. */
675 css_subchannel_release(&css->pseudo_subchannel->dev);
676 css->pseudo_subchannel = NULL;
677 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800678 kfree(css);
679}
680
Cornelia Huck495a5b42006-03-24 03:15:14 -0800681static ssize_t
682css_cm_enable_show(struct device *dev, struct device_attribute *attr,
683 char *buf)
684{
685 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200686 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800687
688 if (!css)
689 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200690 mutex_lock(&css->mutex);
691 ret = sprintf(buf, "%x\n", css->cm_enabled);
692 mutex_unlock(&css->mutex);
693 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800694}
695
696static ssize_t
697css_cm_enable_store(struct device *dev, struct device_attribute *attr,
698 const char *buf, size_t count)
699{
700 struct channel_subsystem *css = to_css(dev);
701 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200702 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800703
Cornelia Huck2f972202008-04-30 13:38:33 +0200704 ret = strict_strtoul(buf, 16, &val);
705 if (ret)
706 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200707 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200708 switch (val) {
709 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800710 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
711 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200712 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800713 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
714 break;
715 default:
716 ret = -EINVAL;
717 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200718 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800719 return ret < 0 ? ret : count;
720}
721
722static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
723
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100724static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800725{
726 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100727 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200728 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800729
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200730 css = channel_subsystems[nr];
731 memset(css, 0, sizeof(struct channel_subsystem));
732 css->pseudo_subchannel =
733 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
734 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100735 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200736 css->pseudo_subchannel->dev.parent = &css->device;
737 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200738 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100739 mutex_init(&css->pseudo_subchannel->reg_mutex);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200740 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100741 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200742 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100743 return ret;
744 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200745 mutex_init(&css->mutex);
746 css->valid = 1;
747 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200748 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200749 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800750 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200751 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Cornelia Hucka55360d2007-10-12 16:11:20 +0200755static int css_reboot_event(struct notifier_block *this,
756 unsigned long event,
757 void *ptr)
758{
759 int ret, i;
760
761 ret = NOTIFY_DONE;
762 for (i = 0; i <= __MAX_CSSID; i++) {
763 struct channel_subsystem *css;
764
765 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200766 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200767 if (css->cm_enabled)
768 if (chsc_secm(css, 0))
769 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200770 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200771 }
772
773 return ret;
774}
775
776static struct notifier_block css_reboot_notifier = {
777 .notifier_call = css_reboot_event,
778};
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200781 * Since the css devices are neither on a bus nor have a class
782 * nor have a special device type, we cannot stop/restart channel
783 * path measurements via the normal suspend/resume callbacks, but have
784 * to use notifiers.
785 */
786static int css_power_event(struct notifier_block *this, unsigned long event,
787 void *ptr)
788{
789 void *secm_area;
790 int ret, i;
791
792 switch (event) {
793 case PM_HIBERNATION_PREPARE:
794 case PM_SUSPEND_PREPARE:
795 ret = NOTIFY_DONE;
796 for (i = 0; i <= __MAX_CSSID; i++) {
797 struct channel_subsystem *css;
798
799 css = channel_subsystems[i];
800 mutex_lock(&css->mutex);
801 if (!css->cm_enabled) {
802 mutex_unlock(&css->mutex);
803 continue;
804 }
805 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
806 GFP_DMA);
807 if (secm_area) {
808 if (__chsc_do_secm(css, 0, secm_area))
809 ret = NOTIFY_BAD;
810 free_page((unsigned long)secm_area);
811 } else
812 ret = NOTIFY_BAD;
813
814 mutex_unlock(&css->mutex);
815 }
816 break;
817 case PM_POST_HIBERNATION:
818 case PM_POST_SUSPEND:
819 ret = NOTIFY_DONE;
820 for (i = 0; i <= __MAX_CSSID; i++) {
821 struct channel_subsystem *css;
822
823 css = channel_subsystems[i];
824 mutex_lock(&css->mutex);
825 if (!css->cm_enabled) {
826 mutex_unlock(&css->mutex);
827 continue;
828 }
829 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
830 GFP_DMA);
831 if (secm_area) {
832 if (__chsc_do_secm(css, 1, secm_area))
833 ret = NOTIFY_BAD;
834 free_page((unsigned long)secm_area);
835 } else
836 ret = NOTIFY_BAD;
837
838 mutex_unlock(&css->mutex);
839 }
840 /* search for subchannels, which appeared during hibernation */
841 css_schedule_reprobe();
842 break;
843 default:
844 ret = NOTIFY_DONE;
845 }
846 return ret;
847
848}
849static struct notifier_block css_power_notifier = {
850 .notifier_call = css_power_event,
851};
852
853/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * Now that the driver core is running, we can setup our channel subsystem.
855 * The struct subchannel's are created during probing (except for the
856 * static console subchannel).
857 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200858static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800860 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Cornelia Huck4434a382007-07-27 12:29:21 +0200862 ret = chsc_determine_css_characteristics();
863 if (ret == -ENOMEM)
Sebastian Ott2f176442009-09-22 22:58:33 +0200864 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Cornelia Huck4434a382007-07-27 12:29:21 +0200866 ret = chsc_alloc_sei_area();
867 if (ret)
868 goto out;
869
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200870 /* Try to enable MSS. */
871 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
872 switch (ret) {
873 case 0: /* Success. */
874 max_ssid = __MAX_SSID;
875 break;
876 case -ENOMEM:
877 goto out;
878 default:
879 max_ssid = 0;
880 }
881
Cornelia Huck4434a382007-07-27 12:29:21 +0200882 ret = slow_subchannel_init();
883 if (ret)
884 goto out;
885
Heiko Carstensf5daba12009-03-26 15:24:01 +0100886 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200887 if (ret)
888 goto out;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if ((ret = bus_register(&css_bus_type)))
891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Cornelia Hucka28c6942006-01-06 00:19:23 -0800893 /* Setup css structure. */
894 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200895 struct channel_subsystem *css;
896
897 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
898 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800899 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800900 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800901 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200902 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100903 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200904 if (ret) {
905 kfree(channel_subsystems[i]);
906 goto out_unregister;
907 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200908 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200909 if (ret) {
910 put_device(&css->device);
911 goto out_unregister;
912 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200913 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200914 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200915 &dev_attr_cm_enable);
916 if (ret)
917 goto out_device;
918 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200919 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200920 if (ret) {
921 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100922 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200923 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800924 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200925 ret = register_reboot_notifier(&css_reboot_notifier);
926 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200927 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200928 ret = register_pm_notifier(&css_power_notifier);
929 if (ret) {
930 unregister_reboot_notifier(&css_reboot_notifier);
931 goto out_unregister;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 css_init_done = 1;
934
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200935 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200936 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100939out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200940 if (css_chsc_characteristics.secm)
941 device_remove_file(&channel_subsystems[i]->device,
942 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200943out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200944 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800945out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800946 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200947 struct channel_subsystem *css;
948
Cornelia Hucka28c6942006-01-06 00:19:23 -0800949 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200950 css = channel_subsystems[i];
951 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200952 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200953 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200954 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800955 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200956 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 bus_unregister(&css_bus_type);
959out:
Heiko Carstensf5daba12009-03-26 15:24:01 +0100960 crw_unregister_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200961 chsc_free_sei_area();
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200962 idset_free(slow_subchannel_set);
Michael Ernste6d5a422008-12-25 13:39:36 +0100963 pr_alert("The CSS device driver initialization failed with "
964 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return ret;
966}
967
Sebastian Ott2f176442009-09-22 22:58:33 +0200968static void __init css_bus_cleanup(void)
969{
970 struct channel_subsystem *css;
971 int i;
972
973 for (i = 0; i <= __MAX_CSSID; i++) {
974 css = channel_subsystems[i];
975 device_unregister(&css->pseudo_subchannel->dev);
976 css->pseudo_subchannel = NULL;
977 if (css_chsc_characteristics.secm)
978 device_remove_file(&css->device, &dev_attr_cm_enable);
979 device_unregister(&css->device);
980 }
981 bus_unregister(&css_bus_type);
982 crw_unregister_handler(CRW_RSC_CSS);
983 chsc_free_sei_area();
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200984 idset_free(slow_subchannel_set);
Sebastian Ott2f176442009-09-22 22:58:33 +0200985 isc_unregister(IO_SCH_ISC);
986}
987
988static int __init channel_subsystem_init(void)
989{
990 int ret;
991
992 ret = css_bus_init();
993 if (ret)
994 return ret;
995
996 ret = io_subchannel_init();
997 if (ret)
998 css_bus_cleanup();
999
1000 return ret;
1001}
1002subsys_initcall(channel_subsystem_init);
1003
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001004static int css_settle(struct device_driver *drv, void *unused)
1005{
1006 struct css_driver *cssdrv = to_cssdriver(drv);
1007
1008 if (cssdrv->settle)
1009 cssdrv->settle();
1010 return 0;
1011}
1012
Sebastian Ott2f176442009-09-22 22:58:33 +02001013/*
1014 * Wait for the initialization of devices to finish, to make sure we are
1015 * done with our setup if the search for the root device starts.
1016 */
1017static int __init channel_subsystem_init_sync(void)
1018{
Sebastian Ott703e5c92009-09-22 22:58:38 +02001019 /* Start initial subchannel evaluation. */
1020 css_schedule_eval_all();
Sebastian Ott25530552009-09-22 22:58:34 +02001021 /* Wait for the evaluation of subchannels to finish. */
1022 wait_event(css_eval_wq, atomic_read(&css_eval_scheduled) == 0);
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001023 /* Wait for the subchannel type specific initialization to finish */
1024 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott2f176442009-09-22 22:58:33 +02001025}
1026subsys_initcall_sync(channel_subsystem_init_sync);
1027
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001028int sch_is_pseudo_sch(struct subchannel *sch)
1029{
1030 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1031}
1032
Cornelia Huckf08adc02008-07-14 09:59:03 +02001033static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Cornelia Huck084325d2008-01-26 14:10:38 +01001035 struct subchannel *sch = to_subchannel(dev);
1036 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001037 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Cornelia Huckf08adc02008-07-14 09:59:03 +02001039 for (id = driver->subchannel_type; id->match_flags; id++) {
1040 if (sch->st == id->type)
1041 return 1;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 return 0;
1045}
1046
Cornelia Huck98c13c22008-01-26 14:10:40 +01001047static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001048{
1049 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001050 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001051
1052 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001053 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001054 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1055 if (ret)
1056 sch->driver = NULL;
1057 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001058}
1059
Cornelia Huck98c13c22008-01-26 14:10:40 +01001060static int css_remove(struct device *dev)
1061{
1062 struct subchannel *sch;
1063 int ret;
1064
1065 sch = to_subchannel(dev);
1066 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1067 sch->driver = NULL;
1068 return ret;
1069}
1070
1071static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001072{
1073 struct subchannel *sch;
1074
1075 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001076 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001077 sch->driver->shutdown(sch);
1078}
1079
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001080static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1081{
1082 struct subchannel *sch = to_subchannel(dev);
1083 int ret;
1084
1085 ret = add_uevent_var(env, "ST=%01X", sch->st);
1086 if (ret)
1087 return ret;
1088 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1089 return ret;
1090}
1091
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001092static int css_pm_prepare(struct device *dev)
1093{
1094 struct subchannel *sch = to_subchannel(dev);
1095 struct css_driver *drv;
1096
1097 if (mutex_is_locked(&sch->reg_mutex))
1098 return -EAGAIN;
1099 if (!sch->dev.driver)
1100 return 0;
1101 drv = to_cssdriver(sch->dev.driver);
1102 /* Notify drivers that they may not register children. */
1103 return drv->prepare ? drv->prepare(sch) : 0;
1104}
1105
1106static void css_pm_complete(struct device *dev)
1107{
1108 struct subchannel *sch = to_subchannel(dev);
1109 struct css_driver *drv;
1110
1111 if (!sch->dev.driver)
1112 return;
1113 drv = to_cssdriver(sch->dev.driver);
1114 if (drv->complete)
1115 drv->complete(sch);
1116}
1117
1118static int css_pm_freeze(struct device *dev)
1119{
1120 struct subchannel *sch = to_subchannel(dev);
1121 struct css_driver *drv;
1122
1123 if (!sch->dev.driver)
1124 return 0;
1125 drv = to_cssdriver(sch->dev.driver);
1126 return drv->freeze ? drv->freeze(sch) : 0;
1127}
1128
1129static int css_pm_thaw(struct device *dev)
1130{
1131 struct subchannel *sch = to_subchannel(dev);
1132 struct css_driver *drv;
1133
1134 if (!sch->dev.driver)
1135 return 0;
1136 drv = to_cssdriver(sch->dev.driver);
1137 return drv->thaw ? drv->thaw(sch) : 0;
1138}
1139
1140static int css_pm_restore(struct device *dev)
1141{
1142 struct subchannel *sch = to_subchannel(dev);
1143 struct css_driver *drv;
1144
1145 if (!sch->dev.driver)
1146 return 0;
1147 drv = to_cssdriver(sch->dev.driver);
1148 return drv->restore ? drv->restore(sch) : 0;
1149}
1150
Alexey Dobriyan47145212009-12-14 18:00:08 -08001151static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001152 .prepare = css_pm_prepare,
1153 .complete = css_pm_complete,
1154 .freeze = css_pm_freeze,
1155 .thaw = css_pm_thaw,
1156 .restore = css_pm_restore,
1157};
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001160 .name = "css",
1161 .match = css_bus_match,
1162 .probe = css_probe,
1163 .remove = css_remove,
1164 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001165 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001166 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167};
1168
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001169/**
1170 * css_driver_register - register a css driver
1171 * @cdrv: css driver to register
1172 *
1173 * This is mainly a wrapper around driver_register that sets name
1174 * and bus_type in the embedded struct device_driver correctly.
1175 */
1176int css_driver_register(struct css_driver *cdrv)
1177{
1178 cdrv->drv.name = cdrv->name;
1179 cdrv->drv.bus = &css_bus_type;
Cornelia Huck4beee642008-01-26 14:10:47 +01001180 cdrv->drv.owner = cdrv->owner;
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001181 return driver_register(&cdrv->drv);
1182}
1183EXPORT_SYMBOL_GPL(css_driver_register);
1184
1185/**
1186 * css_driver_unregister - unregister a css driver
1187 * @cdrv: css driver to unregister
1188 *
1189 * This is a wrapper around driver_unregister.
1190 */
1191void css_driver_unregister(struct css_driver *cdrv)
1192{
1193 driver_unregister(&cdrv->drv);
1194}
1195EXPORT_SYMBOL_GPL(css_driver_unregister);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197MODULE_LICENSE("GPL");
1198EXPORT_SYMBOL(css_bus_type);