blob: 85d43c6bcb66a3521de03370d74c399c87440de8 [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)) {
173 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Cornelia Huck07c6a332007-07-27 12:29:10 +0200178static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200179{
180 int ret;
181
182 mutex_lock(&sch->reg_mutex);
183 ret = device_register(&sch->dev);
184 mutex_unlock(&sch->reg_mutex);
185 return ret;
186}
187
Cornelia Huck44a1c192008-07-14 09:58:47 +0200188/**
189 * css_sch_device_unregister - unregister a subchannel
190 * @sch: subchannel to be unregistered
191 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200192void css_sch_device_unregister(struct subchannel *sch)
193{
194 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200195 if (device_is_registered(&sch->dev))
196 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200197 mutex_unlock(&sch->reg_mutex);
198}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200199EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200200
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200201static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
202{
203 int i;
204 int mask;
205
206 memset(ssd, 0, sizeof(struct chsc_ssd_info));
207 ssd->path_mask = pmcw->pim;
208 for (i = 0; i < 8; i++) {
209 mask = 0x80 >> i;
210 if (pmcw->pim & mask) {
211 chp_id_init(&ssd->chpid[i]);
212 ssd->chpid[i].id = pmcw->chpid[i];
213 }
214 }
215}
216
217static void ssd_register_chpids(struct chsc_ssd_info *ssd)
218{
219 int i;
220 int mask;
221
222 for (i = 0; i < 8; i++) {
223 mask = 0x80 >> i;
224 if (ssd->path_mask & mask)
225 if (!chp_is_registered(ssd->chpid[i]))
226 chp_new(ssd->chpid[i]);
227 }
228}
229
230void css_update_ssd_info(struct subchannel *sch)
231{
232 int ret;
233
234 if (cio_is_console(sch->schid)) {
235 /* Console is initialized too early for functions requiring
236 * memory allocation. */
237 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
238 } else {
239 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
240 if (ret)
241 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
242 ssd_register_chpids(&sch->ssd_info);
243 }
244}
245
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200246static ssize_t type_show(struct device *dev, struct device_attribute *attr,
247 char *buf)
248{
249 struct subchannel *sch = to_subchannel(dev);
250
251 return sprintf(buf, "%01x\n", sch->st);
252}
253
254static DEVICE_ATTR(type, 0444, type_show, NULL);
255
256static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
257 char *buf)
258{
259 struct subchannel *sch = to_subchannel(dev);
260
261 return sprintf(buf, "css:t%01X\n", sch->st);
262}
263
264static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
265
266static struct attribute *subch_attrs[] = {
267 &dev_attr_type.attr,
268 &dev_attr_modalias.attr,
269 NULL,
270};
271
272static struct attribute_group subch_attr_group = {
273 .attrs = subch_attrs,
274};
275
276static struct attribute_group *default_subch_attr_groups[] = {
277 &subch_attr_group,
278 NULL,
279};
280
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200281static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 int ret;
284
285 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200286 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 sch->dev.bus = &css_bus_type;
288 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200289 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200290 /*
291 * We don't want to generate uevents for I/O subchannels that don't
292 * have a working ccw device behind them since they will be
293 * unregistered before they can be used anyway, so we delay the add
294 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200295 * Note that we suppress the uevent for all subchannel types;
296 * the subchannel driver can decide itself when it wants to inform
297 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200298 */
Ming Leif67f1292009-03-01 21:10:49 +0800299 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200300 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200302 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100303 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200304 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
305 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100306 return ret;
307 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200308 if (!sch->driver) {
309 /*
310 * No driver matched. Generate the uevent now so that
311 * a fitting driver module may be loaded based on the
312 * modalias.
313 */
Ming Leif67f1292009-03-01 21:10:49 +0800314 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200315 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return ret;
318}
319
Cornelia Huckc820de32008-07-14 09:58:45 +0200320int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int ret;
323 struct subchannel *sch;
324
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800325 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (IS_ERR(sch))
327 return PTR_ERR(sch);
328 ret = css_register_subchannel(sch);
329 if (ret)
330 css_free_subchannel(sch);
331 return ret;
332}
333
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700334static int
335check_subchannel(struct device * dev, void * data)
336{
337 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800338 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700339
340 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800341 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800345get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct device *dev;
348
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700349 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200350 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700352 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100355/**
356 * css_sch_is_valid() - check if a subchannel is valid
357 * @schib: subchannel information block for the subchannel
358 */
359int css_sch_is_valid(struct schib *schib)
360{
361 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
362 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200363 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
364 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100365 return 1;
366}
367EXPORT_SYMBOL_GPL(css_sch_is_valid);
368
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200369static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
370{
371 struct schib schib;
372
373 if (!slow) {
374 /* Will be done on the slow path. */
375 return -EAGAIN;
376 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100377 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200378 /* Unusable - ignore. */
379 return 0;
380 }
381 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
382 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
383
384 return css_probe_device(schid);
385}
386
Cornelia Huckc820de32008-07-14 09:58:45 +0200387static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
388{
389 int ret = 0;
390
391 if (sch->driver) {
392 if (sch->driver->sch_event)
393 ret = sch->driver->sch_event(sch, slow);
394 else
395 dev_dbg(&sch->dev,
396 "Got subchannel machine check but "
397 "no sch_event handler provided.\n");
398 }
399 return ret;
400}
401
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200402static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200403{
404 struct subchannel *sch;
405 int ret;
406
407 sch = get_subchannel_by_schid(schid);
408 if (sch) {
409 ret = css_evaluate_known_subchannel(sch, slow);
410 put_device(&sch->dev);
411 } else
412 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200413 if (ret == -EAGAIN)
414 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200417static struct idset *slow_subchannel_set;
418static spinlock_t slow_subchannel_lock;
419
420static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200422 spin_lock_init(&slow_subchannel_lock);
423 slow_subchannel_set = idset_sch_new();
424 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200425 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200426 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200428 return 0;
429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100431static int slow_eval_known_fn(struct subchannel *sch, void *data)
432{
433 int eval;
434 int rc;
435
436 spin_lock_irq(&slow_subchannel_lock);
437 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
438 idset_sch_del(slow_subchannel_set, sch->schid);
439 spin_unlock_irq(&slow_subchannel_lock);
440 if (eval) {
441 rc = css_evaluate_known_subchannel(sch, 1);
442 if (rc == -EAGAIN)
443 css_schedule_eval(sch->schid);
444 }
445 return 0;
446}
447
448static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
449{
450 int eval;
451 int rc = 0;
452
453 spin_lock_irq(&slow_subchannel_lock);
454 eval = idset_sch_contains(slow_subchannel_set, schid);
455 idset_sch_del(slow_subchannel_set, schid);
456 spin_unlock_irq(&slow_subchannel_lock);
457 if (eval) {
458 rc = css_evaluate_new_subchannel(schid, 1);
459 switch (rc) {
460 case -EAGAIN:
461 css_schedule_eval(schid);
462 rc = 0;
463 break;
464 case -ENXIO:
465 case -ENOMEM:
466 case -EIO:
467 /* These should abort looping */
468 break;
469 default:
470 rc = 0;
471 }
472 }
473 return rc;
474}
475
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200476static void css_slow_path_func(struct work_struct *unused)
477{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200478 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100479 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
480 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200483static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484struct workqueue_struct *slow_path_wq;
485
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200486void css_schedule_eval(struct subchannel_id schid)
487{
488 unsigned long flags;
489
490 spin_lock_irqsave(&slow_subchannel_lock, flags);
491 idset_sch_add(slow_subchannel_set, schid);
492 queue_work(slow_path_wq, &slow_path_work);
493 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
494}
495
496void css_schedule_eval_all(void)
497{
498 unsigned long flags;
499
500 spin_lock_irqsave(&slow_subchannel_lock, flags);
501 idset_fill(slow_subchannel_set);
502 queue_work(slow_path_wq, &slow_path_work);
503 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
504}
505
Cornelia Huck22806dc2008-04-17 07:45:59 +0200506void css_wait_for_slow_path(void)
507{
Cornelia Huck22806dc2008-04-17 07:45:59 +0200508 flush_workqueue(slow_path_wq);
509}
510
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200511/* Reprobe subchannel if unregistered. */
512static int reprobe_subchannel(struct subchannel_id schid, void *data)
513{
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200514 int ret;
515
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200516 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
517 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200518 if (need_reprobe)
519 return -EAGAIN;
520
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200521 ret = css_probe_device(schid);
522 switch (ret) {
523 case 0:
524 break;
525 case -ENXIO:
526 case -ENOMEM:
Peter Oberparleiter67175612007-12-04 16:09:02 +0100527 case -EIO:
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200528 /* These should abort looping */
529 break;
530 default:
531 ret = 0;
532 }
533
534 return ret;
535}
536
Peter Oberparleiter56e25e92009-03-26 15:24:20 +0100537static void reprobe_after_idle(struct work_struct *unused)
538{
539 /* Make sure initial subchannel scan is done. */
540 wait_event(ccw_device_init_wq,
541 atomic_read(&ccw_device_init_count) == 0);
542 if (need_reprobe)
543 css_schedule_reprobe();
544}
545
546static DECLARE_WORK(reprobe_idle_work, reprobe_after_idle);
547
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200548/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000549static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200550{
551 int ret;
552
Michael Ernst139b83d2008-05-07 09:22:54 +0200553 CIO_MSG_EVENT(4, "reprobe start\n");
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200554
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200555 /* Make sure initial subchannel scan is done. */
Peter Oberparleiter56e25e92009-03-26 15:24:20 +0100556 if (atomic_read(&ccw_device_init_count) != 0) {
557 queue_work(ccw_device_work, &reprobe_idle_work);
558 return;
559 }
560 need_reprobe = 0;
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100561 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200562
Michael Ernst139b83d2008-05-07 09:22:54 +0200563 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200564 need_reprobe);
565}
566
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100567static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200568
569/* Schedule reprobing of all unregistered subchannels. */
570void css_schedule_reprobe(void)
571{
572 need_reprobe = 1;
Cornelia Huckc5d4a992007-11-20 11:13:41 +0100573 queue_work(slow_path_wq, &css_reprobe_work);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200574}
575
576EXPORT_SYMBOL_GPL(css_schedule_reprobe);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * Called from the machine check handler for subchannel report words.
580 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200581static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800583 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Cornelia Huckc1156182008-07-14 09:58:46 +0200585 if (overflow) {
586 css_schedule_eval_all();
587 return;
588 }
589 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
590 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
591 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
592 crw0->erc, crw0->rsid);
593 if (crw1)
594 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
595 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
596 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
597 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800598 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200599 mchk_schid.sch_no = crw0->rsid;
600 if (crw1)
601 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800602
Cornelia Huckc1156182008-07-14 09:58:46 +0200603 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * Since we are always presented with IPI in the CRW, we have to
605 * use stsch() to find out if the subchannel in question has come
606 * or gone.
607 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200608 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800611static int __init
612__init_channel_subsystem(struct subchannel_id schid, void *data)
613{
614 struct subchannel *sch;
615 int ret;
616
617 if (cio_is_console(schid))
618 sch = cio_get_console_subchannel();
619 else {
620 sch = css_alloc_subchannel(schid);
621 if (IS_ERR(sch))
622 ret = PTR_ERR(sch);
623 else
624 ret = 0;
625 switch (ret) {
626 case 0:
627 break;
628 case -ENOMEM:
629 panic("Out of memory in init_channel_subsystem\n");
630 /* -ENXIO: no more subchannels. */
631 case -ENXIO:
632 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800633 /* -EIO: this subchannel set not supported. */
634 case -EIO:
635 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800636 default:
637 return 0;
638 }
639 }
640 /*
641 * We register ALL valid subchannels in ioinfo, even those
642 * that have been present before init_channel_subsystem.
643 * These subchannels can't have been registered yet (kmalloc
644 * not working) so we do it now. This is true e.g. for the
645 * console subchannel.
646 */
647 css_register_subchannel(sch);
648 return 0;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800652css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Cornelia Huck75784c02008-07-14 09:58:57 +0200654 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800655 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
656 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
657 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100659 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800661 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662#endif
663 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800664 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
665 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
666 css->global_pgid.tod_high = tod_high;
667
668}
669
Cornelia Huck3b793062006-01-06 00:19:26 -0800670static void
671channel_subsystem_release(struct device *dev)
672{
673 struct channel_subsystem *css;
674
675 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800676 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200677 if (css->pseudo_subchannel) {
678 /* Implies that it has been generated but never registered. */
679 css_subchannel_release(&css->pseudo_subchannel->dev);
680 css->pseudo_subchannel = NULL;
681 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800682 kfree(css);
683}
684
Cornelia Huck495a5b42006-03-24 03:15:14 -0800685static ssize_t
686css_cm_enable_show(struct device *dev, struct device_attribute *attr,
687 char *buf)
688{
689 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200690 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800691
692 if (!css)
693 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200694 mutex_lock(&css->mutex);
695 ret = sprintf(buf, "%x\n", css->cm_enabled);
696 mutex_unlock(&css->mutex);
697 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800698}
699
700static ssize_t
701css_cm_enable_store(struct device *dev, struct device_attribute *attr,
702 const char *buf, size_t count)
703{
704 struct channel_subsystem *css = to_css(dev);
705 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200706 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800707
Cornelia Huck2f972202008-04-30 13:38:33 +0200708 ret = strict_strtoul(buf, 16, &val);
709 if (ret)
710 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200711 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200712 switch (val) {
713 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800714 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
715 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200716 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800717 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
718 break;
719 default:
720 ret = -EINVAL;
721 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200722 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800723 return ret < 0 ? ret : count;
724}
725
726static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
727
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100728static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800729{
730 u32 tod_high;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100731 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200732 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800733
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200734 css = channel_subsystems[nr];
735 memset(css, 0, sizeof(struct channel_subsystem));
736 css->pseudo_subchannel =
737 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
738 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100739 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200740 css->pseudo_subchannel->dev.parent = &css->device;
741 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200742 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200743 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100744 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200745 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100746 return ret;
747 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200748 mutex_init(&css->mutex);
749 css->valid = 1;
750 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200751 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200752 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800753 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200754 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Cornelia Hucka55360d2007-10-12 16:11:20 +0200758static int css_reboot_event(struct notifier_block *this,
759 unsigned long event,
760 void *ptr)
761{
762 int ret, i;
763
764 ret = NOTIFY_DONE;
765 for (i = 0; i <= __MAX_CSSID; i++) {
766 struct channel_subsystem *css;
767
768 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200769 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200770 if (css->cm_enabled)
771 if (chsc_secm(css, 0))
772 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200773 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200774 }
775
776 return ret;
777}
778
779static struct notifier_block css_reboot_notifier = {
780 .notifier_call = css_reboot_event,
781};
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200784 * Since the css devices are neither on a bus nor have a class
785 * nor have a special device type, we cannot stop/restart channel
786 * path measurements via the normal suspend/resume callbacks, but have
787 * to use notifiers.
788 */
789static int css_power_event(struct notifier_block *this, unsigned long event,
790 void *ptr)
791{
792 void *secm_area;
793 int ret, i;
794
795 switch (event) {
796 case PM_HIBERNATION_PREPARE:
797 case PM_SUSPEND_PREPARE:
798 ret = NOTIFY_DONE;
799 for (i = 0; i <= __MAX_CSSID; i++) {
800 struct channel_subsystem *css;
801
802 css = channel_subsystems[i];
803 mutex_lock(&css->mutex);
804 if (!css->cm_enabled) {
805 mutex_unlock(&css->mutex);
806 continue;
807 }
808 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
809 GFP_DMA);
810 if (secm_area) {
811 if (__chsc_do_secm(css, 0, secm_area))
812 ret = NOTIFY_BAD;
813 free_page((unsigned long)secm_area);
814 } else
815 ret = NOTIFY_BAD;
816
817 mutex_unlock(&css->mutex);
818 }
819 break;
820 case PM_POST_HIBERNATION:
821 case PM_POST_SUSPEND:
822 ret = NOTIFY_DONE;
823 for (i = 0; i <= __MAX_CSSID; i++) {
824 struct channel_subsystem *css;
825
826 css = channel_subsystems[i];
827 mutex_lock(&css->mutex);
828 if (!css->cm_enabled) {
829 mutex_unlock(&css->mutex);
830 continue;
831 }
832 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
833 GFP_DMA);
834 if (secm_area) {
835 if (__chsc_do_secm(css, 1, secm_area))
836 ret = NOTIFY_BAD;
837 free_page((unsigned long)secm_area);
838 } else
839 ret = NOTIFY_BAD;
840
841 mutex_unlock(&css->mutex);
842 }
843 /* search for subchannels, which appeared during hibernation */
844 css_schedule_reprobe();
845 break;
846 default:
847 ret = NOTIFY_DONE;
848 }
849 return ret;
850
851}
852static struct notifier_block css_power_notifier = {
853 .notifier_call = css_power_event,
854};
855
856/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * Now that the driver core is running, we can setup our channel subsystem.
858 * The struct subchannel's are created during probing (except for the
859 * static console subchannel).
860 */
861static int __init
862init_channel_subsystem (void)
863{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800864 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Cornelia Huck4434a382007-07-27 12:29:21 +0200866 ret = chsc_determine_css_characteristics();
867 if (ret == -ENOMEM)
868 goto out; /* No need to continue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Cornelia Huck4434a382007-07-27 12:29:21 +0200870 ret = chsc_alloc_sei_area();
871 if (ret)
872 goto out;
873
874 ret = slow_subchannel_init();
875 if (ret)
876 goto out;
877
Heiko Carstensf5daba12009-03-26 15:24:01 +0100878 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200879 if (ret)
880 goto out;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if ((ret = bus_register(&css_bus_type)))
883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800885 /* Try to enable MSS. */
886 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
887 switch (ret) {
888 case 0: /* Success. */
889 max_ssid = __MAX_SSID;
890 break;
891 case -ENOMEM:
892 goto out_bus;
893 default:
894 max_ssid = 0;
895 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800896 /* Setup css structure. */
897 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200898 struct channel_subsystem *css;
899
900 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
901 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800902 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800903 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800904 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200905 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100906 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200907 if (ret) {
908 kfree(channel_subsystems[i]);
909 goto out_unregister;
910 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200911 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200912 if (ret) {
913 put_device(&css->device);
914 goto out_unregister;
915 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200916 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200917 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200918 &dev_attr_cm_enable);
919 if (ret)
920 goto out_device;
921 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200922 ret = device_register(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100923 if (ret)
924 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800925 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200926 ret = register_reboot_notifier(&css_reboot_notifier);
927 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200928 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200929 ret = register_pm_notifier(&css_power_notifier);
930 if (ret) {
931 unregister_reboot_notifier(&css_reboot_notifier);
932 goto out_unregister;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 css_init_done = 1;
935
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200936 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200937 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800939 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100941out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200942 if (css_chsc_characteristics.secm)
943 device_remove_file(&channel_subsystems[i]->device,
944 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200945out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200946 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800947out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800948 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200949 struct channel_subsystem *css;
950
Cornelia Hucka28c6942006-01-06 00:19:23 -0800951 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200952 css = channel_subsystems[i];
953 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200954 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200955 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200956 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800957 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200958 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800959 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800960out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 bus_unregister(&css_bus_type);
962out:
Heiko Carstensf5daba12009-03-26 15:24:01 +0100963 crw_unregister_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200964 chsc_free_sei_area();
965 kfree(slow_subchannel_set);
Michael Ernste6d5a422008-12-25 13:39:36 +0100966 pr_alert("The CSS device driver initialization failed with "
967 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return ret;
969}
970
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100971int sch_is_pseudo_sch(struct subchannel *sch)
972{
973 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
974}
975
Cornelia Huckf08adc02008-07-14 09:59:03 +0200976static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Cornelia Huck084325d2008-01-26 14:10:38 +0100978 struct subchannel *sch = to_subchannel(dev);
979 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +0200980 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Cornelia Huckf08adc02008-07-14 09:59:03 +0200982 for (id = driver->subchannel_type; id->match_flags; id++) {
983 if (sch->st == id->type)
984 return 1;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 return 0;
988}
989
Cornelia Huck98c13c22008-01-26 14:10:40 +0100990static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100991{
992 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +0100993 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100994
995 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +0100996 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +0100997 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
998 if (ret)
999 sch->driver = NULL;
1000 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001001}
1002
Cornelia Huck98c13c22008-01-26 14:10:40 +01001003static int css_remove(struct device *dev)
1004{
1005 struct subchannel *sch;
1006 int ret;
1007
1008 sch = to_subchannel(dev);
1009 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1010 sch->driver = NULL;
1011 return ret;
1012}
1013
1014static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001015{
1016 struct subchannel *sch;
1017
1018 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001019 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001020 sch->driver->shutdown(sch);
1021}
1022
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001023static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1024{
1025 struct subchannel *sch = to_subchannel(dev);
1026 int ret;
1027
1028 ret = add_uevent_var(env, "ST=%01X", sch->st);
1029 if (ret)
1030 return ret;
1031 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1032 return ret;
1033}
1034
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001035static int css_pm_prepare(struct device *dev)
1036{
1037 struct subchannel *sch = to_subchannel(dev);
1038 struct css_driver *drv;
1039
1040 if (mutex_is_locked(&sch->reg_mutex))
1041 return -EAGAIN;
1042 if (!sch->dev.driver)
1043 return 0;
1044 drv = to_cssdriver(sch->dev.driver);
1045 /* Notify drivers that they may not register children. */
1046 return drv->prepare ? drv->prepare(sch) : 0;
1047}
1048
1049static void css_pm_complete(struct device *dev)
1050{
1051 struct subchannel *sch = to_subchannel(dev);
1052 struct css_driver *drv;
1053
1054 if (!sch->dev.driver)
1055 return;
1056 drv = to_cssdriver(sch->dev.driver);
1057 if (drv->complete)
1058 drv->complete(sch);
1059}
1060
1061static int css_pm_freeze(struct device *dev)
1062{
1063 struct subchannel *sch = to_subchannel(dev);
1064 struct css_driver *drv;
1065
1066 if (!sch->dev.driver)
1067 return 0;
1068 drv = to_cssdriver(sch->dev.driver);
1069 return drv->freeze ? drv->freeze(sch) : 0;
1070}
1071
1072static int css_pm_thaw(struct device *dev)
1073{
1074 struct subchannel *sch = to_subchannel(dev);
1075 struct css_driver *drv;
1076
1077 if (!sch->dev.driver)
1078 return 0;
1079 drv = to_cssdriver(sch->dev.driver);
1080 return drv->thaw ? drv->thaw(sch) : 0;
1081}
1082
1083static int css_pm_restore(struct device *dev)
1084{
1085 struct subchannel *sch = to_subchannel(dev);
1086 struct css_driver *drv;
1087
1088 if (!sch->dev.driver)
1089 return 0;
1090 drv = to_cssdriver(sch->dev.driver);
1091 return drv->restore ? drv->restore(sch) : 0;
1092}
1093
1094static struct dev_pm_ops css_pm_ops = {
1095 .prepare = css_pm_prepare,
1096 .complete = css_pm_complete,
1097 .freeze = css_pm_freeze,
1098 .thaw = css_pm_thaw,
1099 .restore = css_pm_restore,
1100};
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001103 .name = "css",
1104 .match = css_bus_match,
1105 .probe = css_probe,
1106 .remove = css_remove,
1107 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001108 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001109 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110};
1111
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001112/**
1113 * css_driver_register - register a css driver
1114 * @cdrv: css driver to register
1115 *
1116 * This is mainly a wrapper around driver_register that sets name
1117 * and bus_type in the embedded struct device_driver correctly.
1118 */
1119int css_driver_register(struct css_driver *cdrv)
1120{
1121 cdrv->drv.name = cdrv->name;
1122 cdrv->drv.bus = &css_bus_type;
Cornelia Huck4beee642008-01-26 14:10:47 +01001123 cdrv->drv.owner = cdrv->owner;
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001124 return driver_register(&cdrv->drv);
1125}
1126EXPORT_SYMBOL_GPL(css_driver_register);
1127
1128/**
1129 * css_driver_unregister - unregister a css driver
1130 * @cdrv: css driver to unregister
1131 *
1132 * This is a wrapper around driver_unregister.
1133 */
1134void css_driver_unregister(struct css_driver *cdrv)
1135{
1136 driver_unregister(&cdrv->drv);
1137}
1138EXPORT_SYMBOL_GPL(css_driver_unregister);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140subsys_initcall(init_channel_subsystem);
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142MODULE_LICENSE("GPL");
1143EXPORT_SYMBOL(css_bus_type);