blob: 59c4b94cdb4b4314d0636a636283764fb5465ee0 [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;
Peter Oberparleiter40154b82006-06-29 14:57:03 +020034static int need_reprobe = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080035static int max_ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020037struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Heiko Carstens4d284ca2007-02-05 21:18:53 +010039int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080040for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
41{
42 struct subchannel_id schid;
43 int ret;
44
45 init_subchannel_id(&schid);
46 ret = -ENODEV;
47 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080048 do {
49 ret = fn(schid, data);
50 if (ret)
51 break;
52 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
53 schid.sch_no = 0;
54 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080055 return ret;
56}
57
Peter Oberparleitere82a1562008-01-26 14:10:48 +010058struct cb_data {
59 void *data;
60 struct idset *set;
61 int (*fn_known_sch)(struct subchannel *, void *);
62 int (*fn_unknown_sch)(struct subchannel_id, void *);
63};
64
65static int call_fn_known_sch(struct device *dev, void *data)
66{
67 struct subchannel *sch = to_subchannel(dev);
68 struct cb_data *cb = data;
69 int rc = 0;
70
71 idset_sch_del(cb->set, sch->schid);
72 if (cb->fn_known_sch)
73 rc = cb->fn_known_sch(sch, cb->data);
74 return rc;
75}
76
77static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
78{
79 struct cb_data *cb = data;
80 int rc = 0;
81
82 if (idset_sch_contains(cb->set, schid))
83 rc = cb->fn_unknown_sch(schid, cb->data);
84 return rc;
85}
86
Sebastian Ott90ac24a2009-03-26 15:24:11 +010087static int call_fn_all_sch(struct subchannel_id schid, void *data)
88{
89 struct cb_data *cb = data;
90 struct subchannel *sch;
91 int rc = 0;
92
93 sch = get_subchannel_by_schid(schid);
94 if (sch) {
95 if (cb->fn_known_sch)
96 rc = cb->fn_known_sch(sch, cb->data);
97 put_device(&sch->dev);
98 } else {
99 if (cb->fn_unknown_sch)
100 rc = cb->fn_unknown_sch(schid, cb->data);
101 }
102
103 return rc;
104}
105
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100106int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
107 int (*fn_unknown)(struct subchannel_id,
108 void *), void *data)
109{
110 struct cb_data cb;
111 int rc;
112
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100113 cb.data = data;
114 cb.fn_known_sch = fn_known;
115 cb.fn_unknown_sch = fn_unknown;
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100116
117 cb.set = idset_sch_new();
118 if (!cb.set)
119 /* fall back to brute force scanning in case of oom */
120 return for_each_subchannel(call_fn_all_sch, &cb);
121
122 idset_fill(cb.set);
123
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100124 /* Process registered subchannels. */
125 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
126 if (rc)
127 goto out;
128 /* Process unregistered subchannels. */
129 if (fn_unknown)
130 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
131out:
132 idset_free(cb.set);
133
134 return rc;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800138css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct subchannel *sch;
141 int ret;
142
143 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
144 if (sch == NULL)
145 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800146 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (ret < 0) {
148 kfree(sch);
149 return ERR_PTR(ret);
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return sch;
152}
153
154static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155css_subchannel_release(struct device *dev)
156{
157 struct subchannel *sch;
158
159 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100160 if (!cio_is_console(sch->schid)) {
Peter Oberparleiter1da73bc2009-09-11 10:28:16 +0200161 /* Reset intparm to zeroes. */
162 sch->config.intparm = 0;
163 cio_commit_config(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100164 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Cornelia Huck07c6a332007-07-27 12:29:10 +0200169static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200170{
171 int ret;
172
173 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200174 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
175 sch->schid.sch_no);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200176 ret = device_register(&sch->dev);
177 mutex_unlock(&sch->reg_mutex);
178 return ret;
179}
180
Cornelia Huck44a1c192008-07-14 09:58:47 +0200181/**
182 * css_sch_device_unregister - unregister a subchannel
183 * @sch: subchannel to be unregistered
184 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200185void css_sch_device_unregister(struct subchannel *sch)
186{
187 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200188 if (device_is_registered(&sch->dev))
189 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200190 mutex_unlock(&sch->reg_mutex);
191}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200192EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200193
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200194static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
195{
196 int i;
197 int mask;
198
199 memset(ssd, 0, sizeof(struct chsc_ssd_info));
200 ssd->path_mask = pmcw->pim;
201 for (i = 0; i < 8; i++) {
202 mask = 0x80 >> i;
203 if (pmcw->pim & mask) {
204 chp_id_init(&ssd->chpid[i]);
205 ssd->chpid[i].id = pmcw->chpid[i];
206 }
207 }
208}
209
210static void ssd_register_chpids(struct chsc_ssd_info *ssd)
211{
212 int i;
213 int mask;
214
215 for (i = 0; i < 8; i++) {
216 mask = 0x80 >> i;
217 if (ssd->path_mask & mask)
218 if (!chp_is_registered(ssd->chpid[i]))
219 chp_new(ssd->chpid[i]);
220 }
221}
222
223void css_update_ssd_info(struct subchannel *sch)
224{
225 int ret;
226
227 if (cio_is_console(sch->schid)) {
228 /* Console is initialized too early for functions requiring
229 * memory allocation. */
230 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
231 } else {
232 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
233 if (ret)
234 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
235 ssd_register_chpids(&sch->ssd_info);
236 }
237}
238
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200239static ssize_t type_show(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 struct subchannel *sch = to_subchannel(dev);
243
244 return sprintf(buf, "%01x\n", sch->st);
245}
246
247static DEVICE_ATTR(type, 0444, type_show, NULL);
248
249static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
250 char *buf)
251{
252 struct subchannel *sch = to_subchannel(dev);
253
254 return sprintf(buf, "css:t%01X\n", sch->st);
255}
256
257static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
258
259static struct attribute *subch_attrs[] = {
260 &dev_attr_type.attr,
261 &dev_attr_modalias.attr,
262 NULL,
263};
264
265static struct attribute_group subch_attr_group = {
266 .attrs = subch_attrs,
267};
268
David Brownella4dbd672009-06-24 10:06:31 -0700269static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200270 &subch_attr_group,
271 NULL,
272};
273
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200274static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int ret;
277
278 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200279 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sch->dev.bus = &css_bus_type;
281 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200282 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200283 /*
284 * We don't want to generate uevents for I/O subchannels that don't
285 * have a working ccw device behind them since they will be
286 * unregistered before they can be used anyway, so we delay the add
287 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200288 * Note that we suppress the uevent for all subchannel types;
289 * the subchannel driver can decide itself when it wants to inform
290 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200291 */
Ming Leif67f1292009-03-01 21:10:49 +0800292 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200293 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200295 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100296 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200297 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
298 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100299 return ret;
300 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200301 if (!sch->driver) {
302 /*
303 * No driver matched. Generate the uevent now so that
304 * a fitting driver module may be loaded based on the
305 * modalias.
306 */
Ming Leif67f1292009-03-01 21:10:49 +0800307 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200308 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return ret;
311}
312
Cornelia Huckc820de32008-07-14 09:58:45 +0200313int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 int ret;
316 struct subchannel *sch;
317
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800318 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (IS_ERR(sch))
320 return PTR_ERR(sch);
321 ret = css_register_subchannel(sch);
322 if (ret)
Sebastian Ottc6304932009-09-11 10:28:38 +0200323 put_device(&sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return ret;
325}
326
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700327static int
328check_subchannel(struct device * dev, void * data)
329{
330 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800331 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700332
333 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800334 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800338get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct device *dev;
341
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700342 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200343 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700345 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100348/**
349 * css_sch_is_valid() - check if a subchannel is valid
350 * @schib: subchannel information block for the subchannel
351 */
352int css_sch_is_valid(struct schib *schib)
353{
354 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
355 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200356 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
357 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100358 return 1;
359}
360EXPORT_SYMBOL_GPL(css_sch_is_valid);
361
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200362static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
363{
364 struct schib schib;
365
366 if (!slow) {
367 /* Will be done on the slow path. */
368 return -EAGAIN;
369 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100370 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200371 /* Unusable - ignore. */
372 return 0;
373 }
374 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
375 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
376
377 return css_probe_device(schid);
378}
379
Cornelia Huckc820de32008-07-14 09:58:45 +0200380static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
381{
382 int ret = 0;
383
384 if (sch->driver) {
385 if (sch->driver->sch_event)
386 ret = sch->driver->sch_event(sch, slow);
387 else
388 dev_dbg(&sch->dev,
389 "Got subchannel machine check but "
390 "no sch_event handler provided.\n");
391 }
392 return ret;
393}
394
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200395static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200396{
397 struct subchannel *sch;
398 int ret;
399
400 sch = get_subchannel_by_schid(schid);
401 if (sch) {
402 ret = css_evaluate_known_subchannel(sch, slow);
403 put_device(&sch->dev);
404 } else
405 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200406 if (ret == -EAGAIN)
407 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200410static struct idset *slow_subchannel_set;
411static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200412static wait_queue_head_t css_eval_wq;
413static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200414
415static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200417 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200418 atomic_set(&css_eval_scheduled, 0);
419 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200420 slow_subchannel_set = idset_sch_new();
421 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200422 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200423 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200425 return 0;
426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100428static int slow_eval_known_fn(struct subchannel *sch, void *data)
429{
430 int eval;
431 int rc;
432
433 spin_lock_irq(&slow_subchannel_lock);
434 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
435 idset_sch_del(slow_subchannel_set, sch->schid);
436 spin_unlock_irq(&slow_subchannel_lock);
437 if (eval) {
438 rc = css_evaluate_known_subchannel(sch, 1);
439 if (rc == -EAGAIN)
440 css_schedule_eval(sch->schid);
441 }
442 return 0;
443}
444
445static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
446{
447 int eval;
448 int rc = 0;
449
450 spin_lock_irq(&slow_subchannel_lock);
451 eval = idset_sch_contains(slow_subchannel_set, schid);
452 idset_sch_del(slow_subchannel_set, schid);
453 spin_unlock_irq(&slow_subchannel_lock);
454 if (eval) {
455 rc = css_evaluate_new_subchannel(schid, 1);
456 switch (rc) {
457 case -EAGAIN:
458 css_schedule_eval(schid);
459 rc = 0;
460 break;
461 case -ENXIO:
462 case -ENOMEM:
463 case -EIO:
464 /* These should abort looping */
465 break;
466 default:
467 rc = 0;
468 }
469 }
470 return rc;
471}
472
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200473static void css_slow_path_func(struct work_struct *unused)
474{
Sebastian Ott25530552009-09-22 22:58:34 +0200475 unsigned long flags;
476
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200477 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100478 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
479 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200480 spin_lock_irqsave(&slow_subchannel_lock, flags);
481 if (idset_is_empty(slow_subchannel_set)) {
482 atomic_set(&css_eval_scheduled, 0);
483 wake_up(&css_eval_wq);
484 }
485 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200488static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489struct workqueue_struct *slow_path_wq;
490
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200491void css_schedule_eval(struct subchannel_id schid)
492{
493 unsigned long flags;
494
495 spin_lock_irqsave(&slow_subchannel_lock, flags);
496 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200497 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200498 queue_work(slow_path_wq, &slow_path_work);
499 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
500}
501
502void css_schedule_eval_all(void)
503{
504 unsigned long flags;
505
506 spin_lock_irqsave(&slow_subchannel_lock, flags);
507 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200508 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200509 queue_work(slow_path_wq, &slow_path_work);
510 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
511}
512
Cornelia Huck22806dc2008-04-17 07:45:59 +0200513void css_wait_for_slow_path(void)
514{
Cornelia Huck22806dc2008-04-17 07:45:59 +0200515 flush_workqueue(slow_path_wq);
516}
517
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200518/* Reprobe subchannel if unregistered. */
519static int reprobe_subchannel(struct subchannel_id schid, void *data)
520{
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200521 int ret;
522
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200523 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
524 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200525 if (need_reprobe)
526 return -EAGAIN;
527
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200528 ret = css_probe_device(schid);
529 switch (ret) {
530 case 0:
531 break;
532 case -ENXIO:
533 case -ENOMEM:
Peter Oberparleiter671756162007-12-04 16:09:02 +0100534 case -EIO:
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200535 /* These should abort looping */
536 break;
537 default:
538 ret = 0;
539 }
540
541 return ret;
542}
543
Peter Oberparleiter56e25e92009-03-26 15:24:20 +0100544static void reprobe_after_idle(struct work_struct *unused)
545{
546 /* Make sure initial subchannel scan is done. */
547 wait_event(ccw_device_init_wq,
548 atomic_read(&ccw_device_init_count) == 0);
549 if (need_reprobe)
550 css_schedule_reprobe();
551}
552
553static DECLARE_WORK(reprobe_idle_work, reprobe_after_idle);
554
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200555/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000556static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200557{
558 int ret;
559
Michael Ernst139b83d2008-05-07 09:22:54 +0200560 CIO_MSG_EVENT(4, "reprobe start\n");
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200561
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200562 /* Make sure initial subchannel scan is done. */
Peter Oberparleiter56e25e92009-03-26 15:24:20 +0100563 if (atomic_read(&ccw_device_init_count) != 0) {
564 queue_work(ccw_device_work, &reprobe_idle_work);
565 return;
566 }
567 need_reprobe = 0;
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100568 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200569
Michael Ernst139b83d2008-05-07 09:22:54 +0200570 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200571 need_reprobe);
572}
573
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100574static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200575
576/* Schedule reprobing of all unregistered subchannels. */
577void css_schedule_reprobe(void)
578{
579 need_reprobe = 1;
Cornelia Huckc5d4a992007-11-20 11:13:41 +0100580 queue_work(slow_path_wq, &css_reprobe_work);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200581}
582
583EXPORT_SYMBOL_GPL(css_schedule_reprobe);
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * Called from the machine check handler for subchannel report words.
587 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200588static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800590 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Cornelia Huckc1156182008-07-14 09:58:46 +0200592 if (overflow) {
593 css_schedule_eval_all();
594 return;
595 }
596 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
597 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
598 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
599 crw0->erc, crw0->rsid);
600 if (crw1)
601 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
602 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
603 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
604 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800605 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200606 mchk_schid.sch_no = crw0->rsid;
607 if (crw1)
608 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800609
Cornelia Huckc1156182008-07-14 09:58:46 +0200610 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * Since we are always presented with IPI in the CRW, we have to
612 * use stsch() to find out if the subchannel in question has come
613 * or gone.
614 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200615 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Sebastian Ott2f176442009-09-22 22:58:33 +0200618static int __init setup_subchannel(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800619{
620 struct subchannel *sch;
621 int ret;
622
623 if (cio_is_console(schid))
624 sch = cio_get_console_subchannel();
625 else {
626 sch = css_alloc_subchannel(schid);
627 if (IS_ERR(sch))
628 ret = PTR_ERR(sch);
629 else
630 ret = 0;
631 switch (ret) {
632 case 0:
633 break;
634 case -ENOMEM:
635 panic("Out of memory in init_channel_subsystem\n");
636 /* -ENXIO: no more subchannels. */
637 case -ENXIO:
638 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800639 /* -EIO: this subchannel set not supported. */
640 case -EIO:
641 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800642 default:
643 return 0;
644 }
645 }
646 /*
647 * We register ALL valid subchannels in ioinfo, even those
648 * that have been present before init_channel_subsystem.
649 * These subchannels can't have been registered yet (kmalloc
650 * not working) so we do it now. This is true e.g. for the
651 * console subchannel.
652 */
Sebastian Ottc6304932009-09-11 10:28:38 +0200653 if (css_register_subchannel(sch)) {
654 if (!cio_is_console(schid))
655 put_device(&sch->dev);
656 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800657 return 0;
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800661css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Cornelia Huck75784c02008-07-14 09:58:57 +0200663 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800664 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
665 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
666 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100668 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800670 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#endif
672 }
Heiko Carstense86a6ed2009-09-11 10:29:04 +0200673 css->global_pgid.cpu_id = S390_lowcore.cpu_id.ident;
674 css->global_pgid.cpu_model = S390_lowcore.cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800675 css->global_pgid.tod_high = tod_high;
676
677}
678
Cornelia Huck3b793062006-01-06 00:19:26 -0800679static void
680channel_subsystem_release(struct device *dev)
681{
682 struct channel_subsystem *css;
683
684 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800685 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200686 if (css->pseudo_subchannel) {
687 /* Implies that it has been generated but never registered. */
688 css_subchannel_release(&css->pseudo_subchannel->dev);
689 css->pseudo_subchannel = NULL;
690 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800691 kfree(css);
692}
693
Cornelia Huck495a5b42006-03-24 03:15:14 -0800694static ssize_t
695css_cm_enable_show(struct device *dev, struct device_attribute *attr,
696 char *buf)
697{
698 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200699 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800700
701 if (!css)
702 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200703 mutex_lock(&css->mutex);
704 ret = sprintf(buf, "%x\n", css->cm_enabled);
705 mutex_unlock(&css->mutex);
706 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800707}
708
709static ssize_t
710css_cm_enable_store(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
712{
713 struct channel_subsystem *css = to_css(dev);
714 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200715 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800716
Cornelia Huck2f972202008-04-30 13:38:33 +0200717 ret = strict_strtoul(buf, 16, &val);
718 if (ret)
719 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200720 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200721 switch (val) {
722 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800723 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
724 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200725 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800726 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
727 break;
728 default:
729 ret = -EINVAL;
730 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200731 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800732 return ret < 0 ? ret : count;
733}
734
735static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
736
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100737static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800738{
739 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100740 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200741 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800742
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200743 css = channel_subsystems[nr];
744 memset(css, 0, sizeof(struct channel_subsystem));
745 css->pseudo_subchannel =
746 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
747 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100748 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200749 css->pseudo_subchannel->dev.parent = &css->device;
750 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200751 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200752 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100753 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200754 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100755 return ret;
756 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200757 mutex_init(&css->mutex);
758 css->valid = 1;
759 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200760 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200761 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800762 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200763 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Cornelia Hucka55360d2007-10-12 16:11:20 +0200767static int css_reboot_event(struct notifier_block *this,
768 unsigned long event,
769 void *ptr)
770{
771 int ret, i;
772
773 ret = NOTIFY_DONE;
774 for (i = 0; i <= __MAX_CSSID; i++) {
775 struct channel_subsystem *css;
776
777 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200778 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200779 if (css->cm_enabled)
780 if (chsc_secm(css, 0))
781 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200782 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200783 }
784
785 return ret;
786}
787
788static struct notifier_block css_reboot_notifier = {
789 .notifier_call = css_reboot_event,
790};
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200793 * Since the css devices are neither on a bus nor have a class
794 * nor have a special device type, we cannot stop/restart channel
795 * path measurements via the normal suspend/resume callbacks, but have
796 * to use notifiers.
797 */
798static int css_power_event(struct notifier_block *this, unsigned long event,
799 void *ptr)
800{
801 void *secm_area;
802 int ret, i;
803
804 switch (event) {
805 case PM_HIBERNATION_PREPARE:
806 case PM_SUSPEND_PREPARE:
807 ret = NOTIFY_DONE;
808 for (i = 0; i <= __MAX_CSSID; i++) {
809 struct channel_subsystem *css;
810
811 css = channel_subsystems[i];
812 mutex_lock(&css->mutex);
813 if (!css->cm_enabled) {
814 mutex_unlock(&css->mutex);
815 continue;
816 }
817 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
818 GFP_DMA);
819 if (secm_area) {
820 if (__chsc_do_secm(css, 0, secm_area))
821 ret = NOTIFY_BAD;
822 free_page((unsigned long)secm_area);
823 } else
824 ret = NOTIFY_BAD;
825
826 mutex_unlock(&css->mutex);
827 }
828 break;
829 case PM_POST_HIBERNATION:
830 case PM_POST_SUSPEND:
831 ret = NOTIFY_DONE;
832 for (i = 0; i <= __MAX_CSSID; i++) {
833 struct channel_subsystem *css;
834
835 css = channel_subsystems[i];
836 mutex_lock(&css->mutex);
837 if (!css->cm_enabled) {
838 mutex_unlock(&css->mutex);
839 continue;
840 }
841 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
842 GFP_DMA);
843 if (secm_area) {
844 if (__chsc_do_secm(css, 1, secm_area))
845 ret = NOTIFY_BAD;
846 free_page((unsigned long)secm_area);
847 } else
848 ret = NOTIFY_BAD;
849
850 mutex_unlock(&css->mutex);
851 }
852 /* search for subchannels, which appeared during hibernation */
853 css_schedule_reprobe();
854 break;
855 default:
856 ret = NOTIFY_DONE;
857 }
858 return ret;
859
860}
861static struct notifier_block css_power_notifier = {
862 .notifier_call = css_power_event,
863};
864
865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * Now that the driver core is running, we can setup our channel subsystem.
867 * The struct subchannel's are created during probing (except for the
868 * static console subchannel).
869 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200870static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800872 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Cornelia Huck4434a382007-07-27 12:29:21 +0200874 ret = chsc_determine_css_characteristics();
875 if (ret == -ENOMEM)
Sebastian Ott2f176442009-09-22 22:58:33 +0200876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Cornelia Huck4434a382007-07-27 12:29:21 +0200878 ret = chsc_alloc_sei_area();
879 if (ret)
880 goto out;
881
882 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 Huckfb6958a2006-01-06 00:19:25 -0800893 /* Try to enable MSS. */
894 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
895 switch (ret) {
896 case 0: /* Success. */
897 max_ssid = __MAX_SSID;
898 break;
899 case -ENOMEM:
900 goto out_bus;
901 default:
902 max_ssid = 0;
903 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800904 /* Setup css structure. */
905 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200906 struct channel_subsystem *css;
907
908 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
909 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800910 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800911 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800912 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200913 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100914 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200915 if (ret) {
916 kfree(channel_subsystems[i]);
917 goto out_unregister;
918 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200919 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200920 if (ret) {
921 put_device(&css->device);
922 goto out_unregister;
923 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200924 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200925 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200926 &dev_attr_cm_enable);
927 if (ret)
928 goto out_device;
929 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200930 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200931 if (ret) {
932 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100933 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200934 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800935 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200936 ret = register_reboot_notifier(&css_reboot_notifier);
937 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200938 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200939 ret = register_pm_notifier(&css_power_notifier);
940 if (ret) {
941 unregister_reboot_notifier(&css_reboot_notifier);
942 goto out_unregister;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 css_init_done = 1;
945
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200946 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200947 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100950out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200951 if (css_chsc_characteristics.secm)
952 device_remove_file(&channel_subsystems[i]->device,
953 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200954out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200955 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800956out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800957 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200958 struct channel_subsystem *css;
959
Cornelia Hucka28c6942006-01-06 00:19:23 -0800960 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200961 css = channel_subsystems[i];
962 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200963 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200964 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200965 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800966 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200967 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800968 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800969out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 bus_unregister(&css_bus_type);
971out:
Heiko Carstensf5daba12009-03-26 15:24:01 +0100972 crw_unregister_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200973 chsc_free_sei_area();
974 kfree(slow_subchannel_set);
Michael Ernste6d5a422008-12-25 13:39:36 +0100975 pr_alert("The CSS device driver initialization failed with "
976 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return ret;
978}
979
Sebastian Ott2f176442009-09-22 22:58:33 +0200980static void __init css_bus_cleanup(void)
981{
982 struct channel_subsystem *css;
983 int i;
984
985 for (i = 0; i <= __MAX_CSSID; i++) {
986 css = channel_subsystems[i];
987 device_unregister(&css->pseudo_subchannel->dev);
988 css->pseudo_subchannel = NULL;
989 if (css_chsc_characteristics.secm)
990 device_remove_file(&css->device, &dev_attr_cm_enable);
991 device_unregister(&css->device);
992 }
993 bus_unregister(&css_bus_type);
994 crw_unregister_handler(CRW_RSC_CSS);
995 chsc_free_sei_area();
996 kfree(slow_subchannel_set);
997 isc_unregister(IO_SCH_ISC);
998}
999
1000static int __init channel_subsystem_init(void)
1001{
1002 int ret;
1003
1004 ret = css_bus_init();
1005 if (ret)
1006 return ret;
1007
1008 ret = io_subchannel_init();
1009 if (ret)
1010 css_bus_cleanup();
1011
1012 return ret;
1013}
1014subsys_initcall(channel_subsystem_init);
1015
1016/*
1017 * Wait for the initialization of devices to finish, to make sure we are
1018 * done with our setup if the search for the root device starts.
1019 */
1020static int __init channel_subsystem_init_sync(void)
1021{
1022 /* Allocate and register subchannels. */
1023 for_each_subchannel(setup_subchannel, NULL);
Sebastian Ott25530552009-09-22 22:58:34 +02001024 /* Wait for the evaluation of subchannels to finish. */
1025 wait_event(css_eval_wq, atomic_read(&css_eval_scheduled) == 0);
Sebastian Ott2f176442009-09-22 22:58:33 +02001026
1027 /* Wait for the initialization of ccw devices to finish. */
1028 wait_event(ccw_device_init_wq,
1029 atomic_read(&ccw_device_init_count) == 0);
1030 flush_workqueue(ccw_device_work);
1031
1032 return 0;
1033}
1034subsys_initcall_sync(channel_subsystem_init_sync);
1035
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001036int sch_is_pseudo_sch(struct subchannel *sch)
1037{
1038 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1039}
1040
Cornelia Huckf08adc02008-07-14 09:59:03 +02001041static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Cornelia Huck084325d2008-01-26 14:10:38 +01001043 struct subchannel *sch = to_subchannel(dev);
1044 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001045 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Cornelia Huckf08adc02008-07-14 09:59:03 +02001047 for (id = driver->subchannel_type; id->match_flags; id++) {
1048 if (sch->st == id->type)
1049 return 1;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 return 0;
1053}
1054
Cornelia Huck98c13c22008-01-26 14:10:40 +01001055static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001056{
1057 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001058 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001059
1060 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001061 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001062 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1063 if (ret)
1064 sch->driver = NULL;
1065 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001066}
1067
Cornelia Huck98c13c22008-01-26 14:10:40 +01001068static int css_remove(struct device *dev)
1069{
1070 struct subchannel *sch;
1071 int ret;
1072
1073 sch = to_subchannel(dev);
1074 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1075 sch->driver = NULL;
1076 return ret;
1077}
1078
1079static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001080{
1081 struct subchannel *sch;
1082
1083 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001084 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001085 sch->driver->shutdown(sch);
1086}
1087
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001088static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1089{
1090 struct subchannel *sch = to_subchannel(dev);
1091 int ret;
1092
1093 ret = add_uevent_var(env, "ST=%01X", sch->st);
1094 if (ret)
1095 return ret;
1096 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1097 return ret;
1098}
1099
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001100static int css_pm_prepare(struct device *dev)
1101{
1102 struct subchannel *sch = to_subchannel(dev);
1103 struct css_driver *drv;
1104
1105 if (mutex_is_locked(&sch->reg_mutex))
1106 return -EAGAIN;
1107 if (!sch->dev.driver)
1108 return 0;
1109 drv = to_cssdriver(sch->dev.driver);
1110 /* Notify drivers that they may not register children. */
1111 return drv->prepare ? drv->prepare(sch) : 0;
1112}
1113
1114static void css_pm_complete(struct device *dev)
1115{
1116 struct subchannel *sch = to_subchannel(dev);
1117 struct css_driver *drv;
1118
1119 if (!sch->dev.driver)
1120 return;
1121 drv = to_cssdriver(sch->dev.driver);
1122 if (drv->complete)
1123 drv->complete(sch);
1124}
1125
1126static int css_pm_freeze(struct device *dev)
1127{
1128 struct subchannel *sch = to_subchannel(dev);
1129 struct css_driver *drv;
1130
1131 if (!sch->dev.driver)
1132 return 0;
1133 drv = to_cssdriver(sch->dev.driver);
1134 return drv->freeze ? drv->freeze(sch) : 0;
1135}
1136
1137static int css_pm_thaw(struct device *dev)
1138{
1139 struct subchannel *sch = to_subchannel(dev);
1140 struct css_driver *drv;
1141
1142 if (!sch->dev.driver)
1143 return 0;
1144 drv = to_cssdriver(sch->dev.driver);
1145 return drv->thaw ? drv->thaw(sch) : 0;
1146}
1147
1148static int css_pm_restore(struct device *dev)
1149{
1150 struct subchannel *sch = to_subchannel(dev);
1151 struct css_driver *drv;
1152
1153 if (!sch->dev.driver)
1154 return 0;
1155 drv = to_cssdriver(sch->dev.driver);
1156 return drv->restore ? drv->restore(sch) : 0;
1157}
1158
1159static struct dev_pm_ops css_pm_ops = {
1160 .prepare = css_pm_prepare,
1161 .complete = css_pm_complete,
1162 .freeze = css_pm_freeze,
1163 .thaw = css_pm_thaw,
1164 .restore = css_pm_restore,
1165};
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001168 .name = "css",
1169 .match = css_bus_match,
1170 .probe = css_probe,
1171 .remove = css_remove,
1172 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001173 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001174 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175};
1176
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001177/**
1178 * css_driver_register - register a css driver
1179 * @cdrv: css driver to register
1180 *
1181 * This is mainly a wrapper around driver_register that sets name
1182 * and bus_type in the embedded struct device_driver correctly.
1183 */
1184int css_driver_register(struct css_driver *cdrv)
1185{
1186 cdrv->drv.name = cdrv->name;
1187 cdrv->drv.bus = &css_bus_type;
Cornelia Huck4beee642008-01-26 14:10:47 +01001188 cdrv->drv.owner = cdrv->owner;
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001189 return driver_register(&cdrv->drv);
1190}
1191EXPORT_SYMBOL_GPL(css_driver_register);
1192
1193/**
1194 * css_driver_unregister - unregister a css driver
1195 * @cdrv: css driver to unregister
1196 *
1197 * This is a wrapper around driver_unregister.
1198 */
1199void css_driver_unregister(struct css_driver *cdrv)
1200{
1201 driver_unregister(&cdrv->drv);
1202}
1203EXPORT_SYMBOL_GPL(css_driver_unregister);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205MODULE_LICENSE("GPL");
1206EXPORT_SYMBOL(css_bus_type);