blob: 0dc6f6d26287d0f12ca97537b53ad7ca4d34bea6 [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
155css_free_subchannel(struct subchannel *sch)
156{
157 if (sch) {
158 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +0100159 sch->config.intparm = 0;
160 cio_commit_config(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100161 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 kfree(sch);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166static void
167css_subchannel_release(struct device *dev)
168{
169 struct subchannel *sch;
170
171 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100172 if (!cio_is_console(sch->schid)) {
Peter Oberparleiter1da73bc2009-09-11 10:28:16 +0200173 /* Reset intparm to zeroes. */
174 sch->config.intparm = 0;
175 cio_commit_config(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100176 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Cornelia Huck07c6a332007-07-27 12:29:10 +0200181static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200182{
183 int ret;
184
185 mutex_lock(&sch->reg_mutex);
Sebastian Ottab6aae02009-09-11 10:28:24 +0200186 if (cio_is_console(sch->schid))
187 sch->dev.init_name = cio_get_console_sch_name(sch->schid);
188 else
189 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
190 sch->schid.sch_no);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200191 ret = device_register(&sch->dev);
192 mutex_unlock(&sch->reg_mutex);
193 return ret;
194}
195
Cornelia Huck44a1c192008-07-14 09:58:47 +0200196/**
197 * css_sch_device_unregister - unregister a subchannel
198 * @sch: subchannel to be unregistered
199 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200200void css_sch_device_unregister(struct subchannel *sch)
201{
202 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200203 if (device_is_registered(&sch->dev))
204 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200205 mutex_unlock(&sch->reg_mutex);
206}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200207EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200208
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200209static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
210{
211 int i;
212 int mask;
213
214 memset(ssd, 0, sizeof(struct chsc_ssd_info));
215 ssd->path_mask = pmcw->pim;
216 for (i = 0; i < 8; i++) {
217 mask = 0x80 >> i;
218 if (pmcw->pim & mask) {
219 chp_id_init(&ssd->chpid[i]);
220 ssd->chpid[i].id = pmcw->chpid[i];
221 }
222 }
223}
224
225static void ssd_register_chpids(struct chsc_ssd_info *ssd)
226{
227 int i;
228 int mask;
229
230 for (i = 0; i < 8; i++) {
231 mask = 0x80 >> i;
232 if (ssd->path_mask & mask)
233 if (!chp_is_registered(ssd->chpid[i]))
234 chp_new(ssd->chpid[i]);
235 }
236}
237
238void css_update_ssd_info(struct subchannel *sch)
239{
240 int ret;
241
242 if (cio_is_console(sch->schid)) {
243 /* Console is initialized too early for functions requiring
244 * memory allocation. */
245 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
246 } else {
247 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
248 if (ret)
249 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
250 ssd_register_chpids(&sch->ssd_info);
251 }
252}
253
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200254static ssize_t type_show(struct device *dev, struct device_attribute *attr,
255 char *buf)
256{
257 struct subchannel *sch = to_subchannel(dev);
258
259 return sprintf(buf, "%01x\n", sch->st);
260}
261
262static DEVICE_ATTR(type, 0444, type_show, NULL);
263
264static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
265 char *buf)
266{
267 struct subchannel *sch = to_subchannel(dev);
268
269 return sprintf(buf, "css:t%01X\n", sch->st);
270}
271
272static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
273
274static struct attribute *subch_attrs[] = {
275 &dev_attr_type.attr,
276 &dev_attr_modalias.attr,
277 NULL,
278};
279
280static struct attribute_group subch_attr_group = {
281 .attrs = subch_attrs,
282};
283
284static struct attribute_group *default_subch_attr_groups[] = {
285 &subch_attr_group,
286 NULL,
287};
288
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200289static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int ret;
292
293 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200294 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sch->dev.bus = &css_bus_type;
296 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200297 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200298 /*
299 * We don't want to generate uevents for I/O subchannels that don't
300 * have a working ccw device behind them since they will be
301 * unregistered before they can be used anyway, so we delay the add
302 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200303 * Note that we suppress the uevent for all subchannel types;
304 * the subchannel driver can decide itself when it wants to inform
305 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200306 */
Ming Leif67f1292009-03-01 21:10:49 +0800307 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200308 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200310 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100311 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200312 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
313 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100314 return ret;
315 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200316 if (!sch->driver) {
317 /*
318 * No driver matched. Generate the uevent now so that
319 * a fitting driver module may be loaded based on the
320 * modalias.
321 */
Ming Leif67f1292009-03-01 21:10:49 +0800322 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200323 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return ret;
326}
327
Cornelia Huckc820de32008-07-14 09:58:45 +0200328int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int ret;
331 struct subchannel *sch;
332
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800333 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (IS_ERR(sch))
335 return PTR_ERR(sch);
336 ret = css_register_subchannel(sch);
337 if (ret)
338 css_free_subchannel(sch);
339 return ret;
340}
341
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700342static int
343check_subchannel(struct device * dev, void * data)
344{
345 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800346 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700347
348 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800349 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800353get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct device *dev;
356
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700357 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200358 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700360 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100363/**
364 * css_sch_is_valid() - check if a subchannel is valid
365 * @schib: subchannel information block for the subchannel
366 */
367int css_sch_is_valid(struct schib *schib)
368{
369 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
370 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200371 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
372 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100373 return 1;
374}
375EXPORT_SYMBOL_GPL(css_sch_is_valid);
376
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200377static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
378{
379 struct schib schib;
380
381 if (!slow) {
382 /* Will be done on the slow path. */
383 return -EAGAIN;
384 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100385 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200386 /* Unusable - ignore. */
387 return 0;
388 }
389 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
390 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
391
392 return css_probe_device(schid);
393}
394
Cornelia Huckc820de32008-07-14 09:58:45 +0200395static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
396{
397 int ret = 0;
398
399 if (sch->driver) {
400 if (sch->driver->sch_event)
401 ret = sch->driver->sch_event(sch, slow);
402 else
403 dev_dbg(&sch->dev,
404 "Got subchannel machine check but "
405 "no sch_event handler provided.\n");
406 }
407 return ret;
408}
409
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200410static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200411{
412 struct subchannel *sch;
413 int ret;
414
415 sch = get_subchannel_by_schid(schid);
416 if (sch) {
417 ret = css_evaluate_known_subchannel(sch, slow);
418 put_device(&sch->dev);
419 } else
420 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200421 if (ret == -EAGAIN)
422 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200425static struct idset *slow_subchannel_set;
426static spinlock_t slow_subchannel_lock;
427
428static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200430 spin_lock_init(&slow_subchannel_lock);
431 slow_subchannel_set = idset_sch_new();
432 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200433 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200434 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200436 return 0;
437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100439static int slow_eval_known_fn(struct subchannel *sch, void *data)
440{
441 int eval;
442 int rc;
443
444 spin_lock_irq(&slow_subchannel_lock);
445 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
446 idset_sch_del(slow_subchannel_set, sch->schid);
447 spin_unlock_irq(&slow_subchannel_lock);
448 if (eval) {
449 rc = css_evaluate_known_subchannel(sch, 1);
450 if (rc == -EAGAIN)
451 css_schedule_eval(sch->schid);
452 }
453 return 0;
454}
455
456static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
457{
458 int eval;
459 int rc = 0;
460
461 spin_lock_irq(&slow_subchannel_lock);
462 eval = idset_sch_contains(slow_subchannel_set, schid);
463 idset_sch_del(slow_subchannel_set, schid);
464 spin_unlock_irq(&slow_subchannel_lock);
465 if (eval) {
466 rc = css_evaluate_new_subchannel(schid, 1);
467 switch (rc) {
468 case -EAGAIN:
469 css_schedule_eval(schid);
470 rc = 0;
471 break;
472 case -ENXIO:
473 case -ENOMEM:
474 case -EIO:
475 /* These should abort looping */
476 break;
477 default:
478 rc = 0;
479 }
480 }
481 return rc;
482}
483
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200484static void css_slow_path_func(struct work_struct *unused)
485{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200486 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100487 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
488 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200491static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492struct workqueue_struct *slow_path_wq;
493
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200494void css_schedule_eval(struct subchannel_id schid)
495{
496 unsigned long flags;
497
498 spin_lock_irqsave(&slow_subchannel_lock, flags);
499 idset_sch_add(slow_subchannel_set, schid);
500 queue_work(slow_path_wq, &slow_path_work);
501 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
502}
503
504void css_schedule_eval_all(void)
505{
506 unsigned long flags;
507
508 spin_lock_irqsave(&slow_subchannel_lock, flags);
509 idset_fill(slow_subchannel_set);
510 queue_work(slow_path_wq, &slow_path_work);
511 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
512}
513
Cornelia Huck22806dc2008-04-17 07:45:59 +0200514void css_wait_for_slow_path(void)
515{
Cornelia Huck22806dc2008-04-17 07:45:59 +0200516 flush_workqueue(slow_path_wq);
517}
518
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200519/* Reprobe subchannel if unregistered. */
520static int reprobe_subchannel(struct subchannel_id schid, void *data)
521{
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200522 int ret;
523
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200524 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
525 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200526 if (need_reprobe)
527 return -EAGAIN;
528
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200529 ret = css_probe_device(schid);
530 switch (ret) {
531 case 0:
532 break;
533 case -ENXIO:
534 case -ENOMEM:
Peter Oberparleiter671756162007-12-04 16:09:02 +0100535 case -EIO:
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200536 /* These should abort looping */
537 break;
538 default:
539 ret = 0;
540 }
541
542 return ret;
543}
544
Peter Oberparleiter56e25e92009-03-26 15:24:20 +0100545static void reprobe_after_idle(struct work_struct *unused)
546{
547 /* Make sure initial subchannel scan is done. */
548 wait_event(ccw_device_init_wq,
549 atomic_read(&ccw_device_init_count) == 0);
550 if (need_reprobe)
551 css_schedule_reprobe();
552}
553
554static DECLARE_WORK(reprobe_idle_work, reprobe_after_idle);
555
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200556/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000557static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200558{
559 int ret;
560
Michael Ernst139b83d2008-05-07 09:22:54 +0200561 CIO_MSG_EVENT(4, "reprobe start\n");
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200562
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200563 /* Make sure initial subchannel scan is done. */
Peter Oberparleiter56e25e92009-03-26 15:24:20 +0100564 if (atomic_read(&ccw_device_init_count) != 0) {
565 queue_work(ccw_device_work, &reprobe_idle_work);
566 return;
567 }
568 need_reprobe = 0;
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100569 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200570
Michael Ernst139b83d2008-05-07 09:22:54 +0200571 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200572 need_reprobe);
573}
574
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100575static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200576
577/* Schedule reprobing of all unregistered subchannels. */
578void css_schedule_reprobe(void)
579{
580 need_reprobe = 1;
Cornelia Huckc5d4a992007-11-20 11:13:41 +0100581 queue_work(slow_path_wq, &css_reprobe_work);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200582}
583
584EXPORT_SYMBOL_GPL(css_schedule_reprobe);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * Called from the machine check handler for subchannel report words.
588 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200589static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800591 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Cornelia Huckc1156182008-07-14 09:58:46 +0200593 if (overflow) {
594 css_schedule_eval_all();
595 return;
596 }
597 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
598 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
599 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
600 crw0->erc, crw0->rsid);
601 if (crw1)
602 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
603 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
604 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
605 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800606 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200607 mchk_schid.sch_no = crw0->rsid;
608 if (crw1)
609 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800610
Cornelia Huckc1156182008-07-14 09:58:46 +0200611 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * Since we are always presented with IPI in the CRW, we have to
613 * use stsch() to find out if the subchannel in question has come
614 * or gone.
615 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200616 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800619static int __init
620__init_channel_subsystem(struct subchannel_id schid, void *data)
621{
622 struct subchannel *sch;
623 int ret;
624
625 if (cio_is_console(schid))
626 sch = cio_get_console_subchannel();
627 else {
628 sch = css_alloc_subchannel(schid);
629 if (IS_ERR(sch))
630 ret = PTR_ERR(sch);
631 else
632 ret = 0;
633 switch (ret) {
634 case 0:
635 break;
636 case -ENOMEM:
637 panic("Out of memory in init_channel_subsystem\n");
638 /* -ENXIO: no more subchannels. */
639 case -ENXIO:
640 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800641 /* -EIO: this subchannel set not supported. */
642 case -EIO:
643 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800644 default:
645 return 0;
646 }
647 }
648 /*
649 * We register ALL valid subchannels in ioinfo, even those
650 * that have been present before init_channel_subsystem.
651 * These subchannels can't have been registered yet (kmalloc
652 * not working) so we do it now. This is true e.g. for the
653 * console subchannel.
654 */
655 css_register_subchannel(sch);
656 return 0;
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800660css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Cornelia Huck75784c02008-07-14 09:58:57 +0200662 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800663 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
664 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
665 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100667 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800669 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
671 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800672 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
673 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
674 css->global_pgid.tod_high = tod_high;
675
676}
677
Cornelia Huck3b793062006-01-06 00:19:26 -0800678static void
679channel_subsystem_release(struct device *dev)
680{
681 struct channel_subsystem *css;
682
683 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800684 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200685 if (css->pseudo_subchannel) {
686 /* Implies that it has been generated but never registered. */
687 css_subchannel_release(&css->pseudo_subchannel->dev);
688 css->pseudo_subchannel = NULL;
689 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800690 kfree(css);
691}
692
Cornelia Huck495a5b42006-03-24 03:15:14 -0800693static ssize_t
694css_cm_enable_show(struct device *dev, struct device_attribute *attr,
695 char *buf)
696{
697 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200698 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800699
700 if (!css)
701 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200702 mutex_lock(&css->mutex);
703 ret = sprintf(buf, "%x\n", css->cm_enabled);
704 mutex_unlock(&css->mutex);
705 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800706}
707
708static ssize_t
709css_cm_enable_store(struct device *dev, struct device_attribute *attr,
710 const char *buf, size_t count)
711{
712 struct channel_subsystem *css = to_css(dev);
713 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200714 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800715
Cornelia Huck2f972202008-04-30 13:38:33 +0200716 ret = strict_strtoul(buf, 16, &val);
717 if (ret)
718 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200719 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200720 switch (val) {
721 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800722 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
723 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200724 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800725 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
726 break;
727 default:
728 ret = -EINVAL;
729 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200730 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800731 return ret < 0 ? ret : count;
732}
733
734static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
735
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100736static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800737{
738 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100739 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200740 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800741
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200742 css = channel_subsystems[nr];
743 memset(css, 0, sizeof(struct channel_subsystem));
744 css->pseudo_subchannel =
745 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
746 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100747 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200748 css->pseudo_subchannel->dev.parent = &css->device;
749 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200750 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200751 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100752 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200753 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100754 return ret;
755 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200756 mutex_init(&css->mutex);
757 css->valid = 1;
758 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200759 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200760 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800761 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200762 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Cornelia Hucka55360d2007-10-12 16:11:20 +0200766static int css_reboot_event(struct notifier_block *this,
767 unsigned long event,
768 void *ptr)
769{
770 int ret, i;
771
772 ret = NOTIFY_DONE;
773 for (i = 0; i <= __MAX_CSSID; i++) {
774 struct channel_subsystem *css;
775
776 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200777 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200778 if (css->cm_enabled)
779 if (chsc_secm(css, 0))
780 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200781 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200782 }
783
784 return ret;
785}
786
787static struct notifier_block css_reboot_notifier = {
788 .notifier_call = css_reboot_event,
789};
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200792 * Since the css devices are neither on a bus nor have a class
793 * nor have a special device type, we cannot stop/restart channel
794 * path measurements via the normal suspend/resume callbacks, but have
795 * to use notifiers.
796 */
797static int css_power_event(struct notifier_block *this, unsigned long event,
798 void *ptr)
799{
800 void *secm_area;
801 int ret, i;
802
803 switch (event) {
804 case PM_HIBERNATION_PREPARE:
805 case PM_SUSPEND_PREPARE:
806 ret = NOTIFY_DONE;
807 for (i = 0; i <= __MAX_CSSID; i++) {
808 struct channel_subsystem *css;
809
810 css = channel_subsystems[i];
811 mutex_lock(&css->mutex);
812 if (!css->cm_enabled) {
813 mutex_unlock(&css->mutex);
814 continue;
815 }
816 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
817 GFP_DMA);
818 if (secm_area) {
819 if (__chsc_do_secm(css, 0, secm_area))
820 ret = NOTIFY_BAD;
821 free_page((unsigned long)secm_area);
822 } else
823 ret = NOTIFY_BAD;
824
825 mutex_unlock(&css->mutex);
826 }
827 break;
828 case PM_POST_HIBERNATION:
829 case PM_POST_SUSPEND:
830 ret = NOTIFY_DONE;
831 for (i = 0; i <= __MAX_CSSID; i++) {
832 struct channel_subsystem *css;
833
834 css = channel_subsystems[i];
835 mutex_lock(&css->mutex);
836 if (!css->cm_enabled) {
837 mutex_unlock(&css->mutex);
838 continue;
839 }
840 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
841 GFP_DMA);
842 if (secm_area) {
843 if (__chsc_do_secm(css, 1, secm_area))
844 ret = NOTIFY_BAD;
845 free_page((unsigned long)secm_area);
846 } else
847 ret = NOTIFY_BAD;
848
849 mutex_unlock(&css->mutex);
850 }
851 /* search for subchannels, which appeared during hibernation */
852 css_schedule_reprobe();
853 break;
854 default:
855 ret = NOTIFY_DONE;
856 }
857 return ret;
858
859}
860static struct notifier_block css_power_notifier = {
861 .notifier_call = css_power_event,
862};
863
864/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * Now that the driver core is running, we can setup our channel subsystem.
866 * The struct subchannel's are created during probing (except for the
867 * static console subchannel).
868 */
869static int __init
870init_channel_subsystem (void)
871{
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)
876 goto out; /* No need to continue. */
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);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100931 if (ret)
932 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800933 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200934 ret = register_reboot_notifier(&css_reboot_notifier);
935 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200936 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200937 ret = register_pm_notifier(&css_power_notifier);
938 if (ret) {
939 unregister_reboot_notifier(&css_reboot_notifier);
940 goto out_unregister;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 css_init_done = 1;
943
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200944 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200945 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800947 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100949out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200950 if (css_chsc_characteristics.secm)
951 device_remove_file(&channel_subsystems[i]->device,
952 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200953out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200954 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800955out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800956 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200957 struct channel_subsystem *css;
958
Cornelia Hucka28c6942006-01-06 00:19:23 -0800959 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200960 css = channel_subsystems[i];
961 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200962 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200963 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200964 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800965 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200966 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800967 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800968out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 bus_unregister(&css_bus_type);
970out:
Heiko Carstensf5daba12009-03-26 15:24:01 +0100971 crw_unregister_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200972 chsc_free_sei_area();
973 kfree(slow_subchannel_set);
Michael Ernste6d5a422008-12-25 13:39:36 +0100974 pr_alert("The CSS device driver initialization failed with "
975 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return ret;
977}
978
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100979int sch_is_pseudo_sch(struct subchannel *sch)
980{
981 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
982}
983
Cornelia Huckf08adc02008-07-14 09:59:03 +0200984static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Cornelia Huck084325d2008-01-26 14:10:38 +0100986 struct subchannel *sch = to_subchannel(dev);
987 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +0200988 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Cornelia Huckf08adc02008-07-14 09:59:03 +0200990 for (id = driver->subchannel_type; id->match_flags; id++) {
991 if (sch->st == id->type)
992 return 1;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 return 0;
996}
997
Cornelia Huck98c13c22008-01-26 14:10:40 +0100998static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100999{
1000 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001001 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001002
1003 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001004 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001005 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1006 if (ret)
1007 sch->driver = NULL;
1008 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001009}
1010
Cornelia Huck98c13c22008-01-26 14:10:40 +01001011static int css_remove(struct device *dev)
1012{
1013 struct subchannel *sch;
1014 int ret;
1015
1016 sch = to_subchannel(dev);
1017 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1018 sch->driver = NULL;
1019 return ret;
1020}
1021
1022static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001023{
1024 struct subchannel *sch;
1025
1026 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001027 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001028 sch->driver->shutdown(sch);
1029}
1030
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001031static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1032{
1033 struct subchannel *sch = to_subchannel(dev);
1034 int ret;
1035
1036 ret = add_uevent_var(env, "ST=%01X", sch->st);
1037 if (ret)
1038 return ret;
1039 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1040 return ret;
1041}
1042
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001043static int css_pm_prepare(struct device *dev)
1044{
1045 struct subchannel *sch = to_subchannel(dev);
1046 struct css_driver *drv;
1047
1048 if (mutex_is_locked(&sch->reg_mutex))
1049 return -EAGAIN;
1050 if (!sch->dev.driver)
1051 return 0;
1052 drv = to_cssdriver(sch->dev.driver);
1053 /* Notify drivers that they may not register children. */
1054 return drv->prepare ? drv->prepare(sch) : 0;
1055}
1056
1057static void css_pm_complete(struct device *dev)
1058{
1059 struct subchannel *sch = to_subchannel(dev);
1060 struct css_driver *drv;
1061
1062 if (!sch->dev.driver)
1063 return;
1064 drv = to_cssdriver(sch->dev.driver);
1065 if (drv->complete)
1066 drv->complete(sch);
1067}
1068
1069static int css_pm_freeze(struct device *dev)
1070{
1071 struct subchannel *sch = to_subchannel(dev);
1072 struct css_driver *drv;
1073
1074 if (!sch->dev.driver)
1075 return 0;
1076 drv = to_cssdriver(sch->dev.driver);
1077 return drv->freeze ? drv->freeze(sch) : 0;
1078}
1079
1080static int css_pm_thaw(struct device *dev)
1081{
1082 struct subchannel *sch = to_subchannel(dev);
1083 struct css_driver *drv;
1084
1085 if (!sch->dev.driver)
1086 return 0;
1087 drv = to_cssdriver(sch->dev.driver);
1088 return drv->thaw ? drv->thaw(sch) : 0;
1089}
1090
1091static int css_pm_restore(struct device *dev)
1092{
1093 struct subchannel *sch = to_subchannel(dev);
1094 struct css_driver *drv;
1095
1096 if (!sch->dev.driver)
1097 return 0;
1098 drv = to_cssdriver(sch->dev.driver);
1099 return drv->restore ? drv->restore(sch) : 0;
1100}
1101
1102static struct dev_pm_ops css_pm_ops = {
1103 .prepare = css_pm_prepare,
1104 .complete = css_pm_complete,
1105 .freeze = css_pm_freeze,
1106 .thaw = css_pm_thaw,
1107 .restore = css_pm_restore,
1108};
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001111 .name = "css",
1112 .match = css_bus_match,
1113 .probe = css_probe,
1114 .remove = css_remove,
1115 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001116 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001117 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118};
1119
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001120/**
1121 * css_driver_register - register a css driver
1122 * @cdrv: css driver to register
1123 *
1124 * This is mainly a wrapper around driver_register that sets name
1125 * and bus_type in the embedded struct device_driver correctly.
1126 */
1127int css_driver_register(struct css_driver *cdrv)
1128{
1129 cdrv->drv.name = cdrv->name;
1130 cdrv->drv.bus = &css_bus_type;
Cornelia Huck4beee642008-01-26 14:10:47 +01001131 cdrv->drv.owner = cdrv->owner;
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001132 return driver_register(&cdrv->drv);
1133}
1134EXPORT_SYMBOL_GPL(css_driver_register);
1135
1136/**
1137 * css_driver_unregister - unregister a css driver
1138 * @cdrv: css driver to unregister
1139 *
1140 * This is a wrapper around driver_unregister.
1141 */
1142void css_driver_unregister(struct css_driver *cdrv)
1143{
1144 driver_unregister(&cdrv->drv);
1145}
1146EXPORT_SYMBOL_GPL(css_driver_unregister);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148subsys_initcall(init_channel_subsystem);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150MODULE_LICENSE("GPL");
1151EXPORT_SYMBOL(css_bus_type);