]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/s390/cio/device.c
[S390] cio: change misleading console logic
[net-next-2.6.git] / drivers / s390 / cio / device.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
1da177e4 4 *
c820de39 5 * Copyright IBM Corp. 2002,2008
1da177e4 6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
4ce3b30c 7 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
1da177e4
LT
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/device.h>
18#include <linux/workqueue.h>
90ab1336 19#include <linux/timer.h>
1da177e4
LT
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
4e57b681 23#include <asm/param.h> /* HZ */
1842f2b1 24#include <asm/cmb.h>
3a3fc29a 25#include <asm/isc.h>
1da177e4 26
0ae7a7b2 27#include "chp.h"
1da177e4 28#include "cio.h"
d7b5a4c9 29#include "cio_debug.h"
1da177e4
LT
30#include "css.h"
31#include "device.h"
32#include "ioasm.h"
cd6b4f27 33#include "io_sch.h"
ecf5d9ef 34#include "blacklist.h"
1da177e4 35
90ab1336 36static struct timer_list recovery_timer;
486d0a00 37static DEFINE_SPINLOCK(recovery_lock);
90ab1336
PO
38static int recovery_phase;
39static const unsigned long recovery_delay[] = { 3, 30, 300 };
40
1da177e4
LT
41/******************* bus type handling ***********************/
42
43/* The Linux driver model distinguishes between a bus type and
44 * the bus itself. Of course we only have one channel
45 * subsystem driver and one channel system per machine, but
46 * we still use the abstraction. T.R. says it's a good idea. */
47static int
48ccw_bus_match (struct device * dev, struct device_driver * drv)
49{
50 struct ccw_device *cdev = to_ccwdev(dev);
51 struct ccw_driver *cdrv = to_ccwdrv(drv);
52 const struct ccw_device_id *ids = cdrv->ids, *found;
53
54 if (!ids)
55 return 0;
56
57 found = ccw_device_id_match(ids, &cdev->id);
58 if (!found)
59 return 0;
60
61 cdev->id.driver_info = found->driver_info;
62
63 return 1;
64}
65
db0c2d59
PO
66/* Store modalias string delimited by prefix/suffix string into buffer with
67 * specified size. Return length of resulting string (excluding trailing '\0')
68 * even if string doesn't fit buffer (snprintf semantics). */
cfbe9bb2 69static int snprint_alias(char *buf, size_t size,
db0c2d59 70 struct ccw_device_id *id, const char *suffix)
1da177e4 71{
db0c2d59 72 int len;
1da177e4 73
cfbe9bb2 74 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
db0c2d59
PO
75 if (len > size)
76 return len;
77 buf += len;
78 size -= len;
1da177e4 79
db0c2d59
PO
80 if (id->dev_type != 0)
81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82 id->dev_model, suffix);
83 else
84 len += snprintf(buf, size, "dtdm%s", suffix);
1da177e4 85
db0c2d59
PO
86 return len;
87}
88
89/* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
7eff2e7a 91static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
db0c2d59
PO
92{
93 struct ccw_device *cdev = to_ccwdev(dev);
94 struct ccw_device_id *id = &(cdev->id);
cfbe9bb2
CH
95 int ret;
96 char modalias_buf[30];
1da177e4 97
db0c2d59 98 /* CU_TYPE= */
7eff2e7a 99 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
cfbe9bb2
CH
100 if (ret)
101 return ret;
db0c2d59
PO
102
103 /* CU_MODEL= */
7eff2e7a 104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
cfbe9bb2
CH
105 if (ret)
106 return ret;
1da177e4
LT
107
108 /* The next two can be zero, that's ok for us */
db0c2d59 109 /* DEV_TYPE= */
7eff2e7a 110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
cfbe9bb2
CH
111 if (ret)
112 return ret;
1da177e4 113
db0c2d59 114 /* DEV_MODEL= */
7eff2e7a 115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
cfbe9bb2
CH
116 if (ret)
117 return ret;
db0c2d59
PO
118
119 /* MODALIAS= */
cfbe9bb2 120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
7eff2e7a
KS
121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
122 return ret;
1da177e4
LT
123}
124
8bbace7e 125struct bus_type ccw_bus_type;
1da177e4 126
602b20f2
CH
127static void io_subchannel_irq(struct subchannel *);
128static int io_subchannel_probe(struct subchannel *);
129static int io_subchannel_remove(struct subchannel *);
8bbace7e 130static void io_subchannel_shutdown(struct subchannel *);
c820de39 131static int io_subchannel_sch_event(struct subchannel *, int);
99611f87
CH
132static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
133 int);
8ea7f559
SO
134static void recovery_func(unsigned long data);
135struct workqueue_struct *ccw_device_work;
136wait_queue_head_t ccw_device_init_wq;
137atomic_t ccw_device_init_count;
1da177e4 138
f08adc00
CH
139static struct css_device_id io_subchannel_ids[] = {
140 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
141 { /* end of list */ },
142};
143MODULE_DEVICE_TABLE(css, io_subchannel_ids);
144
93a27592
CH
145static int io_subchannel_prepare(struct subchannel *sch)
146{
147 struct ccw_device *cdev;
148 /*
149 * Don't allow suspend while a ccw device registration
150 * is still outstanding.
151 */
152 cdev = sch_get_cdev(sch);
153 if (cdev && !device_is_registered(&cdev->dev))
154 return -EAGAIN;
155 return 0;
156}
157
8ea7f559
SO
158static void io_subchannel_settle(void)
159{
160 wait_event(ccw_device_init_wq,
161 atomic_read(&ccw_device_init_count) == 0);
162 flush_workqueue(ccw_device_work);
163}
164
f7e5d67c 165static struct css_driver io_subchannel_driver = {
4beee646 166 .owner = THIS_MODULE,
f08adc00 167 .subchannel_type = io_subchannel_ids,
25b7bb58 168 .name = "io_subchannel",
1da177e4 169 .irq = io_subchannel_irq,
c820de39
CH
170 .sch_event = io_subchannel_sch_event,
171 .chp_event = io_subchannel_chp_event,
8bbace7e
CH
172 .probe = io_subchannel_probe,
173 .remove = io_subchannel_remove,
174 .shutdown = io_subchannel_shutdown,
93a27592 175 .prepare = io_subchannel_prepare,
8ea7f559 176 .settle = io_subchannel_settle,
1da177e4
LT
177};
178
2f17644d 179int __init io_subchannel_init(void)
1da177e4
LT
180{
181 int ret;
182
183 init_waitqueue_head(&ccw_device_init_wq);
184 atomic_set(&ccw_device_init_count, 0);
90ab1336 185 setup_timer(&recovery_timer, recovery_func, 0);
1da177e4
LT
186
187 ccw_device_work = create_singlethread_workqueue("cio");
188 if (!ccw_device_work)
2f17644d 189 return -ENOMEM;
1da177e4
LT
190 slow_path_wq = create_singlethread_workqueue("kslowcrw");
191 if (!slow_path_wq) {
2f17644d 192 ret = -ENOMEM;
1da177e4
LT
193 goto out_err;
194 }
195 if ((ret = bus_register (&ccw_bus_type)))
196 goto out_err;
197
25b7bb58
CH
198 ret = css_driver_register(&io_subchannel_driver);
199 if (ret)
1da177e4
LT
200 goto out_err;
201
1da177e4
LT
202 return 0;
203out_err:
204 if (ccw_device_work)
205 destroy_workqueue(ccw_device_work);
1da177e4
LT
206 if (slow_path_wq)
207 destroy_workqueue(slow_path_wq);
208 return ret;
209}
210
1da177e4
LT
211
212/************************ device handling **************************/
213
214/*
215 * A ccw_device has some interfaces in sysfs in addition to the
216 * standard ones.
217 * The following entries are designed to export the information which
218 * resided in 2.4 in /proc/subchannels. Subchannel and device number
219 * are obvious, so they don't have an entry :)
220 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
221 */
222static ssize_t
3fd3c0a5 223chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
224{
225 struct subchannel *sch = to_subchannel(dev);
7ad6a249 226 struct chsc_ssd_info *ssd = &sch->ssd_info;
1da177e4
LT
227 ssize_t ret = 0;
228 int chp;
7ad6a249
PO
229 int mask;
230
231 for (chp = 0; chp < 8; chp++) {
232 mask = 0x80 >> chp;
233 if (ssd->path_mask & mask)
234 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
235 else
236 ret += sprintf(buf + ret, "00 ");
237 }
1da177e4
LT
238 ret += sprintf (buf+ret, "\n");
239 return min((ssize_t)PAGE_SIZE, ret);
240}
241
242static ssize_t
3fd3c0a5 243pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
244{
245 struct subchannel *sch = to_subchannel(dev);
246 struct pmcw *pmcw = &sch->schib.pmcw;
247
248 return sprintf (buf, "%02x %02x %02x\n",
249 pmcw->pim, pmcw->pam, pmcw->pom);
250}
251
252static ssize_t
3fd3c0a5 253devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
254{
255 struct ccw_device *cdev = to_ccwdev(dev);
256 struct ccw_device_id *id = &(cdev->id);
257
258 if (id->dev_type != 0)
259 return sprintf(buf, "%04x/%02x\n",
260 id->dev_type, id->dev_model);
261 else
262 return sprintf(buf, "n/a\n");
263}
264
265static ssize_t
3fd3c0a5 266cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
267{
268 struct ccw_device *cdev = to_ccwdev(dev);
269 struct ccw_device_id *id = &(cdev->id);
270
271 return sprintf(buf, "%04x/%02x\n",
272 id->cu_type, id->cu_model);
273}
274
f1fc78a8
BB
275static ssize_t
276modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
277{
278 struct ccw_device *cdev = to_ccwdev(dev);
279 struct ccw_device_id *id = &(cdev->id);
db0c2d59 280 int len;
f1fc78a8 281
086a6c62 282 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
db0c2d59
PO
283
284 return len > PAGE_SIZE ? PAGE_SIZE : len;
f1fc78a8
BB
285}
286
1da177e4 287static ssize_t
3fd3c0a5 288online_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
289{
290 struct ccw_device *cdev = to_ccwdev(dev);
291
292 return sprintf(buf, cdev->online ? "1\n" : "0\n");
293}
294
d7b5a4c9
CH
295int ccw_device_is_orphan(struct ccw_device *cdev)
296{
297 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
298}
299
ef99516c 300static void ccw_device_unregister(struct ccw_device *cdev)
7674da77 301{
3b554a14 302 if (test_and_clear_bit(1, &cdev->private->registered)) {
ef99516c 303 device_del(&cdev->dev);
3b554a14
SO
304 /* Release reference from device_initialize(). */
305 put_device(&cdev->dev);
306 }
7674da77
CH
307}
308
46fbe4e4 309static void ccw_device_remove_orphan_cb(struct work_struct *work)
59a8a6e2 310{
46fbe4e4
PO
311 struct ccw_device_private *priv;
312 struct ccw_device *cdev;
59a8a6e2 313
46fbe4e4
PO
314 priv = container_of(work, struct ccw_device_private, kick_work);
315 cdev = priv->cdev;
59a8a6e2 316 ccw_device_unregister(cdev);
46fbe4e4
PO
317 /* Release cdev reference for workqueue processing. */
318 put_device(&cdev->dev);
59a8a6e2
CH
319}
320
1da177e4
LT
321static void
322ccw_device_remove_disconnected(struct ccw_device *cdev)
323{
d7b5a4c9 324 unsigned long flags;
59a8a6e2 325
1da177e4
LT
326 /*
327 * Forced offline in disconnected state means
328 * 'throw away device'.
329 */
d7b5a4c9 330 if (ccw_device_is_orphan(cdev)) {
59a8a6e2
CH
331 /*
332 * Deregister ccw device.
333 * Unfortunately, we cannot do this directly from the
334 * attribute method.
335 */
be7a2ddc
SO
336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev->dev))
338 return;
d7b5a4c9
CH
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
46fbe4e4
PO
342 PREPARE_WORK(&cdev->private->kick_work,
343 ccw_device_remove_orphan_cb);
c4621a62 344 queue_work(slow_path_wq, &cdev->private->kick_work);
46fbe4e4
PO
345 } else
346 /* Deregister subchannel, which will kill the ccw device. */
c4621a62 347 ccw_device_schedule_sch_unregister(cdev);
1da177e4
LT
348}
349
b2ffd8e9
CH
350/**
351 * ccw_device_set_offline() - disable a ccw device for I/O
352 * @cdev: target ccw device
353 *
354 * This function calls the driver's set_offline() function for @cdev, if
355 * given, and then disables @cdev.
356 * Returns:
357 * %0 on success and a negative error value on failure.
358 * Context:
359 * enabled, ccw device lock not held
360 */
361int ccw_device_set_offline(struct ccw_device *cdev)
1da177e4
LT
362{
363 int ret;
364
365 if (!cdev)
366 return -ENODEV;
367 if (!cdev->online || !cdev->drv)
368 return -EINVAL;
369
370 if (cdev->drv->set_offline) {
371 ret = cdev->drv->set_offline(cdev);
372 if (ret != 0)
373 return ret;
374 }
375 cdev->online = 0;
376 spin_lock_irq(cdev->ccwlock);
217ee6c6
ME
377 /* Wait until a final state or DISCONNECTED is reached */
378 while (!dev_fsm_final_state(cdev) &&
379 cdev->private->state != DEV_STATE_DISCONNECTED) {
1da177e4 380 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
381 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
382 cdev->private->state == DEV_STATE_DISCONNECTED));
383 spin_lock_irq(cdev->ccwlock);
1da177e4 384 }
217ee6c6
ME
385 ret = ccw_device_offline(cdev);
386 if (ret)
387 goto error;
1da177e4 388 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
389 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
390 cdev->private->state == DEV_STATE_DISCONNECTED));
391 /* Give up reference from ccw_device_set_online(). */
392 put_device(&cdev->dev);
393 return 0;
394
395error:
396 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
397 ret, cdev->private->dev_id.ssid,
398 cdev->private->dev_id.devno);
399 cdev->private->state = DEV_STATE_OFFLINE;
400 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
401 spin_unlock_irq(cdev->ccwlock);
402 /* Give up reference from ccw_device_set_online(). */
403 put_device(&cdev->dev);
404 return -ENODEV;
1da177e4
LT
405}
406
b2ffd8e9
CH
407/**
408 * ccw_device_set_online() - enable a ccw device for I/O
409 * @cdev: target ccw device
410 *
411 * This function first enables @cdev and then calls the driver's set_online()
412 * function for @cdev, if given. If set_online() returns an error, @cdev is
413 * disabled again.
414 * Returns:
415 * %0 on success and a negative error value on failure.
416 * Context:
417 * enabled, ccw device lock not held
418 */
419int ccw_device_set_online(struct ccw_device *cdev)
1da177e4
LT
420{
421 int ret;
217ee6c6 422 int ret2;
1da177e4
LT
423
424 if (!cdev)
425 return -ENODEV;
426 if (cdev->online || !cdev->drv)
427 return -EINVAL;
9cd67421
CH
428 /* Hold on to an extra reference while device is online. */
429 if (!get_device(&cdev->dev))
430 return -ENODEV;
1da177e4
LT
431
432 spin_lock_irq(cdev->ccwlock);
433 ret = ccw_device_online(cdev);
434 spin_unlock_irq(cdev->ccwlock);
435 if (ret == 0)
436 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
437 else {
139b83dd 438 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
e556bbbd
CH
439 "device 0.%x.%04x\n",
440 ret, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
9cd67421
CH
442 /* Give up online reference since onlining failed. */
443 put_device(&cdev->dev);
1da177e4
LT
444 return ret;
445 }
217ee6c6
ME
446 spin_lock_irq(cdev->ccwlock);
447 /* Check if online processing was successful */
448 if ((cdev->private->state != DEV_STATE_ONLINE) &&
449 (cdev->private->state != DEV_STATE_W4SENSE)) {
450 spin_unlock_irq(cdev->ccwlock);
9cd67421
CH
451 /* Give up online reference since onlining failed. */
452 put_device(&cdev->dev);
1da177e4 453 return -ENODEV;
9cd67421 454 }
217ee6c6
ME
455 spin_unlock_irq(cdev->ccwlock);
456 if (cdev->drv->set_online)
457 ret = cdev->drv->set_online(cdev);
458 if (ret)
459 goto rollback;
460 cdev->online = 1;
461 return 0;
462
463rollback:
1da177e4 464 spin_lock_irq(cdev->ccwlock);
217ee6c6
ME
465 /* Wait until a final state or DISCONNECTED is reached */
466 while (!dev_fsm_final_state(cdev) &&
467 cdev->private->state != DEV_STATE_DISCONNECTED) {
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
470 cdev->private->state == DEV_STATE_DISCONNECTED));
471 spin_lock_irq(cdev->ccwlock);
472 }
473 ret2 = ccw_device_offline(cdev);
474 if (ret2)
475 goto error;
476 spin_unlock_irq(cdev->ccwlock);
477 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
478 cdev->private->state == DEV_STATE_DISCONNECTED));
479 /* Give up online reference since onlining failed. */
480 put_device(&cdev->dev);
481 return ret;
482
483error:
484 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
485 "device 0.%x.%04x\n",
486 ret2, cdev->private->dev_id.ssid,
487 cdev->private->dev_id.devno);
488 cdev->private->state = DEV_STATE_OFFLINE;
1da177e4 489 spin_unlock_irq(cdev->ccwlock);
9cd67421
CH
490 /* Give up online reference since onlining failed. */
491 put_device(&cdev->dev);
217ee6c6 492 return ret;
1da177e4
LT
493}
494
e74fe0ce 495static int online_store_handle_offline(struct ccw_device *cdev)
f5ba6c86
CH
496{
497 if (cdev->private->state == DEV_STATE_DISCONNECTED)
498 ccw_device_remove_disconnected(cdev);
e74fe0ce
SO
499 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
500 return ccw_device_set_offline(cdev);
501 return 0;
f5ba6c86
CH
502}
503
504static int online_store_recog_and_online(struct ccw_device *cdev)
505{
506 int ret;
507
508 /* Do device recognition, if needed. */
99f6a570 509 if (cdev->private->state == DEV_STATE_BOXED) {
f5ba6c86
CH
510 ret = ccw_device_recognition(cdev);
511 if (ret) {
e556bbbd
CH
512 CIO_MSG_EVENT(0, "Couldn't start recognition "
513 "for device 0.%x.%04x (ret=%d)\n",
514 cdev->private->dev_id.ssid,
515 cdev->private->dev_id.devno, ret);
f5ba6c86
CH
516 return ret;
517 }
518 wait_event(cdev->private->wait_q,
519 cdev->private->flags.recog_done);
156013ff
SO
520 if (cdev->private->state != DEV_STATE_OFFLINE)
521 /* recognition failed */
522 return -EAGAIN;
f5ba6c86
CH
523 }
524 if (cdev->drv && cdev->drv->set_online)
525 ccw_device_set_online(cdev);
526 return 0;
527}
156013ff 528
c78aa6cb 529static int online_store_handle_online(struct ccw_device *cdev, int force)
f5ba6c86
CH
530{
531 int ret;
532
533 ret = online_store_recog_and_online(cdev);
156013ff 534 if (ret && !force)
c78aa6cb 535 return ret;
f5ba6c86
CH
536 if (force && cdev->private->state == DEV_STATE_BOXED) {
537 ret = ccw_device_stlck(cdev);
c78aa6cb
ME
538 if (ret)
539 return ret;
f5ba6c86
CH
540 if (cdev->id.cu_type == 0)
541 cdev->private->state = DEV_STATE_NOT_OPER;
156013ff
SO
542 ret = online_store_recog_and_online(cdev);
543 if (ret)
544 return ret;
f5ba6c86 545 }
c78aa6cb 546 return 0;
f5ba6c86
CH
547}
548
549static ssize_t online_store (struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
1da177e4
LT
551{
552 struct ccw_device *cdev = to_ccwdev(dev);
2f972202
CH
553 int force, ret;
554 unsigned long i;
1da177e4 555
b5cd99e6
SO
556 if ((cdev->private->state != DEV_STATE_OFFLINE &&
557 cdev->private->state != DEV_STATE_ONLINE &&
558 cdev->private->state != DEV_STATE_BOXED &&
559 cdev->private->state != DEV_STATE_DISCONNECTED) ||
560 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
1da177e4
LT
561 return -EAGAIN;
562
563 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
564 atomic_set(&cdev->private->onoff, 0);
565 return -EINVAL;
566 }
567 if (!strncmp(buf, "force\n", count)) {
568 force = 1;
569 i = 1;
2f972202 570 ret = 0;
1da177e4
LT
571 } else {
572 force = 0;
2f972202 573 ret = strict_strtoul(buf, 16, &i);
1da177e4 574 }
2f972202
CH
575 if (ret)
576 goto out;
f5ba6c86
CH
577 switch (i) {
578 case 0:
e74fe0ce 579 ret = online_store_handle_offline(cdev);
f5ba6c86
CH
580 break;
581 case 1:
c78aa6cb 582 ret = online_store_handle_online(cdev, force);
f5ba6c86
CH
583 break;
584 default:
2f972202 585 ret = -EINVAL;
1da177e4 586 }
2f972202 587out:
1da177e4
LT
588 if (cdev->drv)
589 module_put(cdev->drv->owner);
590 atomic_set(&cdev->private->onoff, 0);
e74fe0ce 591 return (ret < 0) ? ret : count;
1da177e4
LT
592}
593
594static ssize_t
3fd3c0a5 595available_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
596{
597 struct ccw_device *cdev = to_ccwdev(dev);
598 struct subchannel *sch;
599
d7b5a4c9
CH
600 if (ccw_device_is_orphan(cdev))
601 return sprintf(buf, "no device\n");
1da177e4
LT
602 switch (cdev->private->state) {
603 case DEV_STATE_BOXED:
604 return sprintf(buf, "boxed\n");
605 case DEV_STATE_DISCONNECTED:
606 case DEV_STATE_DISCONNECTED_SENSE_ID:
607 case DEV_STATE_NOT_OPER:
608 sch = to_subchannel(dev->parent);
609 if (!sch->lpm)
610 return sprintf(buf, "no path\n");
611 else
612 return sprintf(buf, "no device\n");
613 default:
614 /* All other states considered fine. */
615 return sprintf(buf, "good\n");
616 }
617}
618
619static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
620static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
621static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
622static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
f1fc78a8 623static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
1da177e4 624static DEVICE_ATTR(online, 0644, online_show, online_store);
1da177e4
LT
625static DEVICE_ATTR(availability, 0444, available_show, NULL);
626
7e9db9ea 627static struct attribute *io_subchannel_attrs[] = {
1da177e4
LT
628 &dev_attr_chpids.attr,
629 &dev_attr_pimpampom.attr,
630 NULL,
631};
632
7e9db9ea
CH
633static struct attribute_group io_subchannel_attr_group = {
634 .attrs = io_subchannel_attrs,
529192f3 635};
1da177e4
LT
636
637static struct attribute * ccwdev_attrs[] = {
638 &dev_attr_devtype.attr,
639 &dev_attr_cutype.attr,
f1fc78a8 640 &dev_attr_modalias.attr,
1da177e4
LT
641 &dev_attr_online.attr,
642 &dev_attr_cmb_enable.attr,
643 &dev_attr_availability.attr,
644 NULL,
645};
646
647static struct attribute_group ccwdev_attr_group = {
648 .attrs = ccwdev_attrs,
649};
650
a4dbd674 651static const struct attribute_group *ccwdev_attr_groups[] = {
ef99516c
CH
652 &ccwdev_attr_group,
653 NULL,
654};
1da177e4
LT
655
656/* this is a simple abstraction for device_register that sets the
657 * correct bus type and adds the bus specific files */
3c9da7ba 658static int ccw_device_register(struct ccw_device *cdev)
1da177e4
LT
659{
660 struct device *dev = &cdev->dev;
661 int ret;
662
663 dev->bus = &ccw_bus_type;
3ac276f8
SO
664 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
665 cdev->private->dev_id.devno);
666 if (ret)
667 return ret;
668 ret = device_add(dev);
669 if (ret)
1da177e4
LT
670 return ret;
671
672 set_bit(1, &cdev->private->registered);
1da177e4
LT
673 return ret;
674}
675
b0744bd2 676struct match_data {
78964268 677 struct ccw_dev_id dev_id;
b0744bd2
CH
678 struct ccw_device * sibling;
679};
680
681static int
682match_devno(struct device * dev, void * data)
683{
78964268 684 struct match_data * d = data;
b0744bd2
CH
685 struct ccw_device * cdev;
686
687 cdev = to_ccwdev(dev);
688 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
d7b5a4c9 689 !ccw_device_is_orphan(cdev) &&
78964268 690 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
d7b5a4c9 691 (cdev != d->sibling))
b0744bd2 692 return 1;
b0744bd2
CH
693 return 0;
694}
695
78964268
CH
696static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
697 struct ccw_device *sibling)
1da177e4 698{
1da177e4 699 struct device *dev;
292888c8 700 struct match_data data;
1da177e4 701
78964268 702 data.dev_id = *dev_id;
292888c8 703 data.sibling = sibling;
e5945b4f 704 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
1da177e4 705
b0744bd2 706 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
707}
708
d7b5a4c9
CH
709static int match_orphan(struct device *dev, void *data)
710{
711 struct ccw_dev_id *dev_id;
712 struct ccw_device *cdev;
713
714 dev_id = data;
715 cdev = to_ccwdev(dev);
716 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
717}
718
719static struct ccw_device *
720get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
721 struct ccw_dev_id *dev_id)
722{
723 struct device *dev;
724
725 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
726 match_orphan);
727
728 return dev ? to_ccwdev(dev) : NULL;
729}
730
eb32ae8d 731void ccw_device_do_unbind_bind(struct work_struct *work)
1da177e4 732{
c1637532 733 struct ccw_device_private *priv;
1da177e4
LT
734 struct ccw_device *cdev;
735 struct subchannel *sch;
eb32ae8d 736 int ret;
1da177e4 737
c1637532
MS
738 priv = container_of(work, struct ccw_device_private, kick_work);
739 cdev = priv->cdev;
1da177e4 740 sch = to_subchannel(cdev->dev.parent);
d7b5a4c9 741
eb32ae8d
CH
742 if (test_bit(1, &cdev->private->registered)) {
743 device_release_driver(&cdev->dev);
744 ret = device_attach(&cdev->dev);
745 WARN_ON(ret == -ENODEV);
746 }
1da177e4
LT
747}
748
749static void
750ccw_device_release(struct device *dev)
751{
752 struct ccw_device *cdev;
753
754 cdev = to_ccwdev(dev);
6eff208f
CH
755 /* Release reference of parent subchannel. */
756 put_device(cdev->dev.parent);
1da177e4
LT
757 kfree(cdev->private);
758 kfree(cdev);
759}
760
7674da77
CH
761static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
762{
763 struct ccw_device *cdev;
764
765 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
766 if (cdev) {
767 cdev->private = kzalloc(sizeof(struct ccw_device_private),
768 GFP_KERNEL | GFP_DMA);
769 if (cdev->private)
770 return cdev;
771 }
772 kfree(cdev);
773 return ERR_PTR(-ENOMEM);
774}
775
776static int io_subchannel_initialize_dev(struct subchannel *sch,
777 struct ccw_device *cdev)
778{
779 cdev->private->cdev = cdev;
780 atomic_set(&cdev->private->onoff, 0);
781 cdev->dev.parent = &sch->dev;
782 cdev->dev.release = ccw_device_release;
33583c36 783 INIT_WORK(&cdev->private->kick_work, NULL);
ef99516c 784 cdev->dev.groups = ccwdev_attr_groups;
7674da77
CH
785 /* Do first half of device_register. */
786 device_initialize(&cdev->dev);
787 if (!get_device(&sch->dev)) {
283fdd0b
CH
788 /* Release reference from device_initialize(). */
789 put_device(&cdev->dev);
7674da77
CH
790 return -ENODEV;
791 }
792 return 0;
793}
794
795static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
796{
797 struct ccw_device *cdev;
798 int ret;
799
800 cdev = io_subchannel_allocate_dev(sch);
801 if (!IS_ERR(cdev)) {
802 ret = io_subchannel_initialize_dev(sch, cdev);
06739a8a 803 if (ret)
7674da77 804 cdev = ERR_PTR(ret);
7674da77
CH
805 }
806 return cdev;
807}
808
d7b5a4c9
CH
809static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
810
811static void sch_attach_device(struct subchannel *sch,
812 struct ccw_device *cdev)
813{
82b7ac05 814 css_update_ssd_info(sch);
d7b5a4c9 815 spin_lock_irq(sch->lock);
db6a6423 816 sch_set_cdev(sch, cdev);
d7b5a4c9
CH
817 cdev->private->schid = sch->schid;
818 cdev->ccwlock = sch->lock;
c820de39 819 ccw_device_trigger_reprobe(cdev);
d7b5a4c9
CH
820 spin_unlock_irq(sch->lock);
821}
822
823static void sch_attach_disconnected_device(struct subchannel *sch,
824 struct ccw_device *cdev)
825{
826 struct subchannel *other_sch;
827 int ret;
828
6eff208f
CH
829 /* Get reference for new parent. */
830 if (!get_device(&sch->dev))
831 return;
832 other_sch = to_subchannel(cdev->dev.parent);
833 /* Note: device_move() changes cdev->dev.parent */
ffa6a705 834 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
d7b5a4c9 835 if (ret) {
139b83dd 836 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
d7b5a4c9
CH
837 "(ret=%d)!\n", cdev->private->dev_id.ssid,
838 cdev->private->dev_id.devno, ret);
6eff208f
CH
839 /* Put reference for new parent. */
840 put_device(&sch->dev);
d7b5a4c9
CH
841 return;
842 }
db6a6423 843 sch_set_cdev(other_sch, NULL);
d7b5a4c9
CH
844 /* No need to keep a subchannel without ccw device around. */
845 css_sch_device_unregister(other_sch);
d7b5a4c9 846 sch_attach_device(sch, cdev);
6eff208f
CH
847 /* Put reference for old parent. */
848 put_device(&other_sch->dev);
d7b5a4c9
CH
849}
850
851static void sch_attach_orphaned_device(struct subchannel *sch,
852 struct ccw_device *cdev)
853{
854 int ret;
6eff208f 855 struct subchannel *pseudo_sch;
d7b5a4c9 856
6eff208f
CH
857 /* Get reference for new parent. */
858 if (!get_device(&sch->dev))
859 return;
860 pseudo_sch = to_subchannel(cdev->dev.parent);
861 /*
862 * Try to move the ccw device to its new subchannel.
863 * Note: device_move() changes cdev->dev.parent
864 */
ffa6a705 865 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
d7b5a4c9
CH
866 if (ret) {
867 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
868 "failed (ret=%d)!\n",
869 cdev->private->dev_id.ssid,
870 cdev->private->dev_id.devno, ret);
6eff208f
CH
871 /* Put reference for new parent. */
872 put_device(&sch->dev);
d7b5a4c9
CH
873 return;
874 }
875 sch_attach_device(sch, cdev);
6eff208f
CH
876 /* Put reference on pseudo subchannel. */
877 put_device(&pseudo_sch->dev);
d7b5a4c9
CH
878}
879
880static void sch_create_and_recog_new_device(struct subchannel *sch)
881{
882 struct ccw_device *cdev;
883
884 /* Need to allocate a new ccw device. */
885 cdev = io_subchannel_create_ccwdev(sch);
886 if (IS_ERR(cdev)) {
887 /* OK, we did everything we could... */
888 css_sch_device_unregister(sch);
889 return;
890 }
891 spin_lock_irq(sch->lock);
db6a6423 892 sch_set_cdev(sch, cdev);
d7b5a4c9
CH
893 spin_unlock_irq(sch->lock);
894 /* Start recognition for the new ccw device. */
895 if (io_subchannel_recog(cdev, sch)) {
896 spin_lock_irq(sch->lock);
db6a6423 897 sch_set_cdev(sch, NULL);
d7b5a4c9 898 spin_unlock_irq(sch->lock);
d7b5a4c9 899 css_sch_device_unregister(sch);
6eff208f
CH
900 /* Put reference from io_subchannel_create_ccwdev(). */
901 put_device(&sch->dev);
902 /* Give up initial reference. */
903 put_device(&cdev->dev);
d7b5a4c9
CH
904 }
905}
906
907
908void ccw_device_move_to_orphanage(struct work_struct *work)
909{
910 struct ccw_device_private *priv;
911 struct ccw_device *cdev;
912 struct ccw_device *replacing_cdev;
913 struct subchannel *sch;
914 int ret;
915 struct channel_subsystem *css;
916 struct ccw_dev_id dev_id;
917
918 priv = container_of(work, struct ccw_device_private, kick_work);
919 cdev = priv->cdev;
920 sch = to_subchannel(cdev->dev.parent);
921 css = to_css(sch->dev.parent);
922 dev_id.devno = sch->schib.pmcw.dev;
923 dev_id.ssid = sch->schid.ssid;
924
6eff208f
CH
925 /* Increase refcount for pseudo subchannel. */
926 get_device(&css->pseudo_subchannel->dev);
d7b5a4c9
CH
927 /*
928 * Move the orphaned ccw device to the orphanage so the replacing
929 * ccw device can take its place on the subchannel.
6eff208f 930 * Note: device_move() changes cdev->dev.parent
d7b5a4c9 931 */
ffa6a705
CH
932 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
933 DPM_ORDER_NONE);
d7b5a4c9
CH
934 if (ret) {
935 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
936 "(ret=%d)!\n", cdev->private->dev_id.ssid,
937 cdev->private->dev_id.devno, ret);
6eff208f
CH
938 /* Decrease refcount for pseudo subchannel again. */
939 put_device(&css->pseudo_subchannel->dev);
d7b5a4c9
CH
940 return;
941 }
942 cdev->ccwlock = css->pseudo_subchannel->lock;
943 /*
944 * Search for the replacing ccw device
945 * - among the disconnected devices
946 * - in the orphanage
947 */
948 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
949 if (replacing_cdev) {
950 sch_attach_disconnected_device(sch, replacing_cdev);
85acc407 951 /* Release reference from get_disc_ccwdev_by_dev_id() */
97166f52 952 put_device(&replacing_cdev->dev);
6eff208f
CH
953 /* Release reference of subchannel from old cdev. */
954 put_device(&sch->dev);
d7b5a4c9
CH
955 return;
956 }
957 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
958 if (replacing_cdev) {
959 sch_attach_orphaned_device(sch, replacing_cdev);
85acc407 960 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
97166f52 961 put_device(&replacing_cdev->dev);
6eff208f
CH
962 /* Release reference of subchannel from old cdev. */
963 put_device(&sch->dev);
d7b5a4c9
CH
964 return;
965 }
966 sch_create_and_recog_new_device(sch);
6eff208f
CH
967 /* Release reference of subchannel from old cdev. */
968 put_device(&sch->dev);
d7b5a4c9
CH
969}
970
1da177e4
LT
971/*
972 * Register recognized device.
973 */
974static void
c1637532 975io_subchannel_register(struct work_struct *work)
1da177e4 976{
c1637532 977 struct ccw_device_private *priv;
1da177e4
LT
978 struct ccw_device *cdev;
979 struct subchannel *sch;
980 int ret;
981 unsigned long flags;
982
c1637532
MS
983 priv = container_of(work, struct ccw_device_private, kick_work);
984 cdev = priv->cdev;
1da177e4 985 sch = to_subchannel(cdev->dev.parent);
5fb6b854
CH
986 /*
987 * Check if subchannel is still registered. It may have become
988 * unregistered if a machine check hit us after finishing
989 * device recognition but before the register work could be
990 * queued.
991 */
992 if (!device_is_registered(&sch->dev))
993 goto out_err;
82b7ac05 994 css_update_ssd_info(sch);
47af5518
CH
995 /*
996 * io_subchannel_register() will also be called after device
997 * recognition has been done for a boxed device (which will already
998 * be registered). We need to reprobe since we may now have sense id
999 * information.
1000 */
d6a30761 1001 if (device_is_registered(&cdev->dev)) {
47af5518
CH
1002 if (!cdev->drv) {
1003 ret = device_reprobe(&cdev->dev);
1004 if (ret)
1005 /* We can't do much here. */
139b83dd 1006 CIO_MSG_EVENT(0, "device_reprobe() returned"
e556bbbd
CH
1007 " %d for 0.%x.%04x\n", ret,
1008 cdev->private->dev_id.ssid,
1009 cdev->private->dev_id.devno);
47af5518 1010 }
1da177e4
LT
1011 goto out;
1012 }
fa1a8c23
CH
1013 /*
1014 * Now we know this subchannel will stay, we can throw
1015 * our delayed uevent.
1016 */
f67f129e 1017 dev_set_uevent_suppress(&sch->dev, 0);
fa1a8c23 1018 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1da177e4
LT
1019 /* make it known to the system */
1020 ret = ccw_device_register(cdev);
1021 if (ret) {
e556bbbd
CH
1022 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
1023 cdev->private->dev_id.ssid,
1024 cdev->private->dev_id.devno, ret);
2ec22984 1025 spin_lock_irqsave(sch->lock, flags);
db6a6423 1026 sch_set_cdev(sch, NULL);
2ec22984 1027 spin_unlock_irqrestore(sch->lock, flags);
6eff208f
CH
1028 /* Release initial device reference. */
1029 put_device(&cdev->dev);
5fb6b854 1030 goto out_err;
1da177e4 1031 }
1da177e4
LT
1032out:
1033 cdev->private->flags.recog_done = 1;
1da177e4 1034 wake_up(&cdev->private->wait_q);
5fb6b854
CH
1035out_err:
1036 /* Release reference for workqueue processing. */
1037 put_device(&cdev->dev);
1da177e4
LT
1038 if (atomic_dec_and_test(&ccw_device_init_count))
1039 wake_up(&ccw_device_init_wq);
1040}
1041
3f4cf6e7 1042static void ccw_device_call_sch_unregister(struct work_struct *work)
1da177e4 1043{
c1637532
MS
1044 struct ccw_device_private *priv;
1045 struct ccw_device *cdev;
1da177e4
LT
1046 struct subchannel *sch;
1047
c1637532
MS
1048 priv = container_of(work, struct ccw_device_private, kick_work);
1049 cdev = priv->cdev;
46fbe4e4
PO
1050 /* Get subchannel reference for local processing. */
1051 if (!get_device(cdev->dev.parent))
1052 return;
1da177e4 1053 sch = to_subchannel(cdev->dev.parent);
6ab4879a 1054 css_sch_device_unregister(sch);
46fbe4e4 1055 /* Release cdev reference for workqueue processing.*/
1da177e4 1056 put_device(&cdev->dev);
46fbe4e4 1057 /* Release subchannel reference for local processing. */
1da177e4
LT
1058 put_device(&sch->dev);
1059}
1060
c4621a62
SO
1061void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1062{
be7a2ddc
SO
1063 /* Get cdev reference for workqueue processing. */
1064 if (!get_device(&cdev->dev))
1065 return;
c4621a62
SO
1066 PREPARE_WORK(&cdev->private->kick_work,
1067 ccw_device_call_sch_unregister);
1068 queue_work(slow_path_wq, &cdev->private->kick_work);
1069}
1070
1da177e4
LT
1071/*
1072 * subchannel recognition done. Called from the state machine.
1073 */
1074void
1075io_subchannel_recog_done(struct ccw_device *cdev)
1076{
1da177e4
LT
1077 if (css_init_done == 0) {
1078 cdev->private->flags.recog_done = 1;
1079 return;
1080 }
1081 switch (cdev->private->state) {
47593bfa
SO
1082 case DEV_STATE_BOXED:
1083 /* Device did not respond in time. */
1da177e4
LT
1084 case DEV_STATE_NOT_OPER:
1085 cdev->private->flags.recog_done = 1;
c4621a62 1086 ccw_device_schedule_sch_unregister(cdev);
1da177e4
LT
1087 if (atomic_dec_and_test(&ccw_device_init_count))
1088 wake_up(&ccw_device_init_wq);
1089 break;
1da177e4
LT
1090 case DEV_STATE_OFFLINE:
1091 /*
1092 * We can't register the device in interrupt context so
1093 * we schedule a work item.
1094 */
1095 if (!get_device(&cdev->dev))
1096 break;
1097 PREPARE_WORK(&cdev->private->kick_work,
c1637532 1098 io_subchannel_register);
1da177e4
LT
1099 queue_work(slow_path_wq, &cdev->private->kick_work);
1100 break;
1101 }
1102}
1103
1104static int
1105io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1106{
1107 int rc;
1108 struct ccw_device_private *priv;
1109
db6a6423 1110 sch_set_cdev(sch, cdev);
2ec22984 1111 cdev->ccwlock = sch->lock;
fb6958a5 1112
1da177e4
LT
1113 /* Init private data. */
1114 priv = cdev->private;
78964268
CH
1115 priv->dev_id.devno = sch->schib.pmcw.dev;
1116 priv->dev_id.ssid = sch->schid.ssid;
1117 priv->schid = sch->schid;
1da177e4
LT
1118 priv->state = DEV_STATE_NOT_OPER;
1119 INIT_LIST_HEAD(&priv->cmb_list);
1120 init_waitqueue_head(&priv->wait_q);
1121 init_timer(&priv->timer);
1122
1da177e4
LT
1123 /* Increase counter of devices currently in recognition. */
1124 atomic_inc(&ccw_device_init_count);
1125
1126 /* Start async. device sensing. */
2ec22984 1127 spin_lock_irq(sch->lock);
1da177e4 1128 rc = ccw_device_recognition(cdev);
2ec22984 1129 spin_unlock_irq(sch->lock);
1da177e4
LT
1130 if (rc) {
1131 if (atomic_dec_and_test(&ccw_device_init_count))
1132 wake_up(&ccw_device_init_wq);
1133 }
1134 return rc;
1135}
1136
d7b5a4c9
CH
1137static void ccw_device_move_to_sch(struct work_struct *work)
1138{
1139 struct ccw_device_private *priv;
1140 int rc;
1141 struct subchannel *sch;
1142 struct ccw_device *cdev;
1143 struct subchannel *former_parent;
1144
1145 priv = container_of(work, struct ccw_device_private, kick_work);
1146 sch = priv->sch;
1147 cdev = priv->cdev;
6eff208f
CH
1148 former_parent = to_subchannel(cdev->dev.parent);
1149 /* Get reference for new parent. */
1150 if (!get_device(&sch->dev))
1151 return;
d7b5a4c9 1152 mutex_lock(&sch->reg_mutex);
6eff208f
CH
1153 /*
1154 * Try to move the ccw device to its new subchannel.
1155 * Note: device_move() changes cdev->dev.parent
1156 */
ffa6a705 1157 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
d7b5a4c9
CH
1158 mutex_unlock(&sch->reg_mutex);
1159 if (rc) {
139b83dd 1160 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
d7b5a4c9
CH
1161 "0.%x.%04x failed (ret=%d)!\n",
1162 cdev->private->dev_id.ssid,
1163 cdev->private->dev_id.devno, sch->schid.ssid,
1164 sch->schid.sch_no, rc);
1165 css_sch_device_unregister(sch);
6eff208f
CH
1166 /* Put reference for new parent again. */
1167 put_device(&sch->dev);
d7b5a4c9
CH
1168 goto out;
1169 }
6eff208f 1170 if (!sch_is_pseudo_sch(former_parent)) {
d7b5a4c9 1171 spin_lock_irq(former_parent->lock);
db6a6423 1172 sch_set_cdev(former_parent, NULL);
d7b5a4c9
CH
1173 spin_unlock_irq(former_parent->lock);
1174 css_sch_device_unregister(former_parent);
1175 /* Reset intparm to zeroes. */
13952ec1
SO
1176 former_parent->config.intparm = 0;
1177 cio_commit_config(former_parent);
d7b5a4c9
CH
1178 }
1179 sch_attach_device(sch, cdev);
1180out:
6eff208f
CH
1181 /* Put reference for old parent. */
1182 put_device(&former_parent->dev);
d7b5a4c9
CH
1183 put_device(&cdev->dev);
1184}
1185
602b20f2
CH
1186static void io_subchannel_irq(struct subchannel *sch)
1187{
1188 struct ccw_device *cdev;
1189
db6a6423 1190 cdev = sch_get_cdev(sch);
602b20f2 1191
efd986db
SO
1192 CIO_TRACE_EVENT(6, "IRQ");
1193 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
602b20f2
CH
1194 if (cdev)
1195 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1196}
1197
13952ec1
SO
1198void io_subchannel_init_config(struct subchannel *sch)
1199{
1200 memset(&sch->config, 0, sizeof(sch->config));
1201 sch->config.csense = 1;
d36f0c66
SO
1202 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1203 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
13952ec1
SO
1204 sch->config.mp = 1;
1205}
1206
0ae7a7b2
CH
1207static void io_subchannel_init_fields(struct subchannel *sch)
1208{
1209 if (cio_is_console(sch->schid))
1210 sch->opm = 0xff;
1211 else
1212 sch->opm = chp_get_sch_opm(sch);
1213 sch->lpm = sch->schib.pmcw.pam & sch->opm;
3a3fc29a 1214 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
0ae7a7b2
CH
1215
1216 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1217 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1218 sch->schib.pmcw.dev, sch->schid.ssid,
1219 sch->schid.sch_no, sch->schib.pmcw.pim,
1220 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
13952ec1
SO
1221
1222 io_subchannel_init_config(sch);
0ae7a7b2
CH
1223}
1224
90ed2b69
CH
1225static void io_subchannel_do_unreg(struct work_struct *work)
1226{
1227 struct subchannel *sch;
1228
1229 sch = container_of(work, struct subchannel, work);
1230 css_sch_device_unregister(sch);
90ed2b69
CH
1231 put_device(&sch->dev);
1232}
1233
1234/* Schedule unregister if we have no cdev. */
1235static void io_subchannel_schedule_removal(struct subchannel *sch)
1236{
1237 get_device(&sch->dev);
1238 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1239 queue_work(slow_path_wq, &sch->work);
1240}
1241
1242/*
1243 * Note: We always return 0 so that we bind to the device even on error.
1244 * This is needed so that our remove function is called on unregister.
1245 */
0ae7a7b2 1246static int io_subchannel_probe(struct subchannel *sch)
1da177e4 1247{
1da177e4
LT
1248 struct ccw_device *cdev;
1249 int rc;
1250 unsigned long flags;
d7b5a4c9 1251 struct ccw_dev_id dev_id;
1da177e4 1252
6d7c5afc 1253 if (cio_is_console(sch->schid)) {
7e9db9ea
CH
1254 rc = sysfs_create_group(&sch->dev.kobj,
1255 &io_subchannel_attr_group);
1256 if (rc)
1257 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1258 "attributes for subchannel "
1259 "0.%x.%04x (rc=%d)\n",
1260 sch->schid.ssid, sch->schid.sch_no, rc);
1da177e4 1261 /*
6d7c5afc 1262 * The console subchannel already has an associated ccw_device.
7e9db9ea 1263 * Throw the delayed uevent for the subchannel, register
6d7c5afc 1264 * the ccw_device and exit.
1da177e4 1265 */
f67f129e 1266 dev_set_uevent_suppress(&sch->dev, 0);
7e9db9ea 1267 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
6d7c5afc 1268 cdev = sch_get_cdev(sch);
e1031786 1269 cdev->dev.groups = ccwdev_attr_groups;
1da177e4
LT
1270 device_initialize(&cdev->dev);
1271 ccw_device_register(cdev);
1da177e4
LT
1272 /*
1273 * Check if the device is already online. If it is
9cd67421
CH
1274 * the reference count needs to be corrected since we
1275 * didn't obtain a reference in ccw_device_set_online.
1da177e4
LT
1276 */
1277 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1278 cdev->private->state != DEV_STATE_OFFLINE &&
1279 cdev->private->state != DEV_STATE_BOXED)
1280 get_device(&cdev->dev);
1281 return 0;
1282 }
0ae7a7b2 1283 io_subchannel_init_fields(sch);
f444cc0e
SO
1284 rc = cio_commit_config(sch);
1285 if (rc)
1286 goto out_schedule;
7e9db9ea
CH
1287 rc = sysfs_create_group(&sch->dev.kobj,
1288 &io_subchannel_attr_group);
1289 if (rc)
90ed2b69 1290 goto out_schedule;
cd6b4f27
CH
1291 /* Allocate I/O subchannel private data. */
1292 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1293 GFP_KERNEL | GFP_DMA);
90ed2b69 1294 if (!sch->private)
7e9db9ea 1295 goto out_err;
111e95a4
SO
1296 /*
1297 * First check if a fitting device may be found amongst the
1298 * disconnected devices or in the orphanage.
1299 */
1300 dev_id.devno = sch->schib.pmcw.dev;
1301 dev_id.ssid = sch->schid.ssid;
d7b5a4c9
CH
1302 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1303 if (!cdev)
1304 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1305 &dev_id);
1306 if (cdev) {
1307 /*
1308 * Schedule moving the device until when we have a registered
1309 * subchannel to move to and succeed the probe. We can
1310 * unregister later again, when the probe is through.
1311 */
1312 cdev->private->sch = sch;
1313 PREPARE_WORK(&cdev->private->kick_work,
1314 ccw_device_move_to_sch);
1315 queue_work(slow_path_wq, &cdev->private->kick_work);
1316 return 0;
1317 }
7674da77 1318 cdev = io_subchannel_create_ccwdev(sch);
90ed2b69 1319 if (IS_ERR(cdev))
7e9db9ea 1320 goto out_err;
8bbace7e 1321 rc = io_subchannel_recog(cdev, sch);
1da177e4 1322 if (rc) {
2ec22984 1323 spin_lock_irqsave(sch->lock, flags);
90ed2b69 1324 io_subchannel_recog_done(cdev);
2ec22984 1325 spin_unlock_irqrestore(sch->lock, flags);
1da177e4 1326 }
7e9db9ea
CH
1327 return 0;
1328out_err:
1329 kfree(sch->private);
1330 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
90ed2b69
CH
1331out_schedule:
1332 io_subchannel_schedule_removal(sch);
1333 return 0;
1da177e4
LT
1334}
1335
1da177e4 1336static int
8bbace7e 1337io_subchannel_remove (struct subchannel *sch)
1da177e4
LT
1338{
1339 struct ccw_device *cdev;
1340 unsigned long flags;
1341
db6a6423
CH
1342 cdev = sch_get_cdev(sch);
1343 if (!cdev)
1da177e4 1344 return 0;
1da177e4
LT
1345 /* Set ccw device to not operational and drop reference. */
1346 spin_lock_irqsave(cdev->ccwlock, flags);
db6a6423 1347 sch_set_cdev(sch, NULL);
1da177e4
LT
1348 cdev->private->state = DEV_STATE_NOT_OPER;
1349 spin_unlock_irqrestore(cdev->ccwlock, flags);
ef99516c 1350 ccw_device_unregister(cdev);
cd6b4f27 1351 kfree(sch->private);
7e9db9ea 1352 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1da177e4
LT
1353 return 0;
1354}
1355
602b20f2 1356static int io_subchannel_notify(struct subchannel *sch, int event)
1da177e4
LT
1357{
1358 struct ccw_device *cdev;
1359
db6a6423 1360 cdev = sch_get_cdev(sch);
1da177e4
LT
1361 if (!cdev)
1362 return 0;
c820de39 1363 return ccw_device_notify(cdev, event);
1da177e4
LT
1364}
1365
602b20f2 1366static void io_subchannel_verify(struct subchannel *sch)
1da177e4
LT
1367{
1368 struct ccw_device *cdev;
1369
db6a6423 1370 cdev = sch_get_cdev(sch);
1da177e4
LT
1371 if (cdev)
1372 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1373}
1374
c820de39 1375static int check_for_io_on_path(struct subchannel *sch, int mask)
1da177e4 1376{
cdb912a4 1377 if (cio_update_schib(sch))
c820de39 1378 return 0;
23d805b6 1379 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
c820de39
CH
1380 return 1;
1381 return 0;
1382}
1383
1384static void terminate_internal_io(struct subchannel *sch,
1385 struct ccw_device *cdev)
1386{
1387 if (cio_clear(sch)) {
1388 /* Recheck device in case clear failed. */
1389 sch->lpm = 0;
1390 if (cdev->online)
1391 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1392 else
1393 css_schedule_eval(sch->schid);
d23861ff 1394 return;
c820de39 1395 }
1da177e4 1396 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
c820de39
CH
1397 /* Request retry of internal operation. */
1398 cdev->private->flags.intretry = 1;
1399 /* Call handler. */
1da177e4
LT
1400 if (cdev->handler)
1401 cdev->handler(cdev, cdev->private->intparm,
1402 ERR_PTR(-EIO));
1403}
1404
c820de39
CH
1405static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1406{
1407 struct ccw_device *cdev;
1408
1409 cdev = sch_get_cdev(sch);
1410 if (!cdev)
1411 return;
1412 if (check_for_io_on_path(sch, mask)) {
1413 if (cdev->private->state == DEV_STATE_ONLINE)
1414 ccw_device_kill_io(cdev);
1415 else {
1416 terminate_internal_io(sch, cdev);
1417 /* Re-start path verification. */
1418 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1419 }
1420 } else
1421 /* trigger path verification. */
1422 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1423
1424}
1425
99611f87
CH
1426static int io_subchannel_chp_event(struct subchannel *sch,
1427 struct chp_link *link, int event)
c820de39
CH
1428{
1429 int mask;
c820de39 1430
99611f87 1431 mask = chp_ssd_get_mask(&sch->ssd_info, link);
c820de39
CH
1432 if (!mask)
1433 return 0;
1434 switch (event) {
1435 case CHP_VARY_OFF:
1436 sch->opm &= ~mask;
1437 sch->lpm &= ~mask;
1438 io_subchannel_terminate_path(sch, mask);
1439 break;
1440 case CHP_VARY_ON:
1441 sch->opm |= mask;
1442 sch->lpm |= mask;
1443 io_subchannel_verify(sch);
1444 break;
1445 case CHP_OFFLINE:
cdb912a4 1446 if (cio_update_schib(sch))
c820de39
CH
1447 return -ENODEV;
1448 io_subchannel_terminate_path(sch, mask);
1449 break;
1450 case CHP_ONLINE:
cdb912a4
SO
1451 if (cio_update_schib(sch))
1452 return -ENODEV;
c820de39
CH
1453 sch->lpm |= mask & sch->opm;
1454 io_subchannel_verify(sch);
1455 break;
1456 }
1457 return 0;
1458}
1459
1da177e4 1460static void
8bbace7e 1461io_subchannel_shutdown(struct subchannel *sch)
1da177e4 1462{
1da177e4
LT
1463 struct ccw_device *cdev;
1464 int ret;
1465
db6a6423 1466 cdev = sch_get_cdev(sch);
1da177e4 1467
a8237fc4 1468 if (cio_is_console(sch->schid))
1da177e4
LT
1469 return;
1470 if (!sch->schib.pmcw.ena)
1471 /* Nothing to do. */
1472 return;
1473 ret = cio_disable_subchannel(sch);
1474 if (ret != -EBUSY)
1475 /* Subchannel is disabled, we're done. */
1476 return;
1477 cdev->private->state = DEV_STATE_QUIESCE;
1478 if (cdev->handler)
1479 cdev->handler(cdev, cdev->private->intparm,
1480 ERR_PTR(-EIO));
1481 ret = ccw_device_cancel_halt_clear(cdev);
1482 if (ret == -EBUSY) {
1483 ccw_device_set_timeout(cdev, HZ/10);
1484 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1485 }
1486 cio_disable_subchannel(sch);
1487}
1488
c820de39
CH
1489static int io_subchannel_get_status(struct subchannel *sch)
1490{
1491 struct schib schib;
1492
1493 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1494 return CIO_GONE;
1495 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1496 return CIO_REVALIDATE;
1497 if (!sch->lpm)
1498 return CIO_NO_PATH;
1499 return CIO_OPER;
1500}
1501
1502static int device_is_disconnected(struct ccw_device *cdev)
1503{
1504 if (!cdev)
1505 return 0;
1506 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1507 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1508}
1509
1510static int recovery_check(struct device *dev, void *data)
1511{
1512 struct ccw_device *cdev = to_ccwdev(dev);
1513 int *redo = data;
1514
1515 spin_lock_irq(cdev->ccwlock);
1516 switch (cdev->private->state) {
1517 case DEV_STATE_DISCONNECTED:
1518 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1519 cdev->private->dev_id.ssid,
1520 cdev->private->dev_id.devno);
1521 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1522 *redo = 1;
1523 break;
1524 case DEV_STATE_DISCONNECTED_SENSE_ID:
1525 *redo = 1;
1526 break;
1527 }
1528 spin_unlock_irq(cdev->ccwlock);
1529
1530 return 0;
1531}
1532
1533static void recovery_work_func(struct work_struct *unused)
1534{
1535 int redo = 0;
1536
1537 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1538 if (redo) {
1539 spin_lock_irq(&recovery_lock);
1540 if (!timer_pending(&recovery_timer)) {
1541 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1542 recovery_phase++;
1543 mod_timer(&recovery_timer, jiffies +
1544 recovery_delay[recovery_phase] * HZ);
1545 }
1546 spin_unlock_irq(&recovery_lock);
1547 } else
1548 CIO_MSG_EVENT(4, "recovery: end\n");
1549}
1550
1551static DECLARE_WORK(recovery_work, recovery_work_func);
1552
1553static void recovery_func(unsigned long data)
1554{
1555 /*
1556 * We can't do our recovery in softirq context and it's not
1557 * performance critical, so we schedule it.
1558 */
1559 schedule_work(&recovery_work);
1560}
1561
1562static void ccw_device_schedule_recovery(void)
1563{
1564 unsigned long flags;
1565
1566 CIO_MSG_EVENT(4, "recovery: schedule\n");
1567 spin_lock_irqsave(&recovery_lock, flags);
1568 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1569 recovery_phase = 0;
1570 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1571 }
1572 spin_unlock_irqrestore(&recovery_lock, flags);
1573}
1574
ecf5d9ef
PO
1575static int purge_fn(struct device *dev, void *data)
1576{
1577 struct ccw_device *cdev = to_ccwdev(dev);
1578 struct ccw_device_private *priv = cdev->private;
1579 int unreg;
1580
1581 spin_lock_irq(cdev->ccwlock);
1582 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1583 (priv->state == DEV_STATE_OFFLINE);
1584 spin_unlock_irq(cdev->ccwlock);
1585 if (!unreg)
1586 goto out;
ecf5d9ef
PO
1587 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1588 priv->dev_id.devno);
c4621a62 1589 ccw_device_schedule_sch_unregister(cdev);
ecf5d9ef
PO
1590
1591out:
1592 /* Abort loop in case of pending signal. */
1593 if (signal_pending(current))
1594 return -EINTR;
1595
1596 return 0;
1597}
1598
1599/**
1600 * ccw_purge_blacklisted - purge unused, blacklisted devices
1601 *
1602 * Unregister all ccw devices that are offline and on the blacklist.
1603 */
1604int ccw_purge_blacklisted(void)
1605{
1606 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1607 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1608 return 0;
1609}
1610
6afcc775 1611void ccw_device_set_disconnected(struct ccw_device *cdev)
c820de39
CH
1612{
1613 if (!cdev)
1614 return;
1615 ccw_device_set_timeout(cdev, 0);
1616 cdev->private->flags.fake_irb = 0;
1617 cdev->private->state = DEV_STATE_DISCONNECTED;
1618 if (cdev->online)
1619 ccw_device_schedule_recovery();
1620}
1621
91c36919
PO
1622void ccw_device_set_notoper(struct ccw_device *cdev)
1623{
1624 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1625
1626 CIO_TRACE_EVENT(2, "notoper");
b9d3aed7 1627 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
91c36919
PO
1628 ccw_device_set_timeout(cdev, 0);
1629 cio_disable_subchannel(sch);
1630 cdev->private->state = DEV_STATE_NOT_OPER;
1631}
1632
c820de39
CH
1633static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1634{
1635 int event, ret, disc;
1636 unsigned long flags;
91c36919 1637 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
c820de39
CH
1638 struct ccw_device *cdev;
1639
1640 spin_lock_irqsave(sch->lock, flags);
1641 cdev = sch_get_cdev(sch);
1642 disc = device_is_disconnected(cdev);
1643 if (disc && slow) {
1644 /* Disconnected devices are evaluated directly only.*/
1645 spin_unlock_irqrestore(sch->lock, flags);
1646 return 0;
1647 }
1648 /* No interrupt after machine check - kill pending timers. */
1649 if (cdev)
1650 ccw_device_set_timeout(cdev, 0);
1651 if (!disc && !slow) {
1652 /* Non-disconnected devices are evaluated on the slow path. */
1653 spin_unlock_irqrestore(sch->lock, flags);
1654 return -EAGAIN;
1655 }
1656 event = io_subchannel_get_status(sch);
1657 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1658 sch->schid.ssid, sch->schid.sch_no, event,
1659 disc ? "disconnected" : "normal",
1660 slow ? "slow" : "fast");
1661 /* Analyze subchannel status. */
1662 action = NONE;
1663 switch (event) {
1664 case CIO_NO_PATH:
1665 if (disc) {
1666 /* Check if paths have become available. */
1667 action = REPROBE;
1668 break;
1669 }
1670 /* fall through */
1671 case CIO_GONE:
c820de39 1672 /* Ask driver what to do with device. */
91c36919
PO
1673 if (io_subchannel_notify(sch, event))
1674 action = DISC;
1675 else
1676 action = UNREGISTER;
c820de39
CH
1677 break;
1678 case CIO_REVALIDATE:
1679 /* Device will be removed, so no notify necessary. */
1680 if (disc)
1681 /* Reprobe because immediate unregister might block. */
1682 action = REPROBE;
1683 else
1684 action = UNREGISTER_PROBE;
1685 break;
1686 case CIO_OPER:
1687 if (disc)
1688 /* Get device operational again. */
1689 action = REPROBE;
1690 break;
1691 }
1692 /* Perform action. */
1693 ret = 0;
1694 switch (action) {
1695 case UNREGISTER:
1696 case UNREGISTER_PROBE:
91c36919 1697 ccw_device_set_notoper(cdev);
c820de39
CH
1698 /* Unregister device (will use subchannel lock). */
1699 spin_unlock_irqrestore(sch->lock, flags);
1700 css_sch_device_unregister(sch);
1701 spin_lock_irqsave(sch->lock, flags);
c820de39
CH
1702 break;
1703 case REPROBE:
1704 ccw_device_trigger_reprobe(cdev);
1705 break;
91c36919 1706 case DISC:
6afcc775 1707 ccw_device_set_disconnected(cdev);
91c36919 1708 break;
c820de39
CH
1709 default:
1710 break;
1711 }
1712 spin_unlock_irqrestore(sch->lock, flags);
1713 /* Probe if necessary. */
1714 if (action == UNREGISTER_PROBE)
1715 ret = css_probe_device(sch->schid);
1716
1717 return ret;
1718}
1719
1da177e4
LT
1720#ifdef CONFIG_CCW_CONSOLE
1721static struct ccw_device console_cdev;
1722static struct ccw_device_private console_private;
1723static int console_cdev_in_use;
1724
2ec22984
CH
1725static DEFINE_SPINLOCK(ccw_console_lock);
1726
1727spinlock_t * cio_get_console_lock(void)
1728{
1729 return &ccw_console_lock;
1730}
1731
0ae7a7b2
CH
1732static int ccw_device_console_enable(struct ccw_device *cdev,
1733 struct subchannel *sch)
1da177e4
LT
1734{
1735 int rc;
1736
cd6b4f27
CH
1737 /* Attach subchannel private data. */
1738 sch->private = cio_get_console_priv();
1739 memset(sch->private, 0, sizeof(struct io_subchannel_private));
0ae7a7b2 1740 io_subchannel_init_fields(sch);
f444cc0e
SO
1741 rc = cio_commit_config(sch);
1742 if (rc)
1743 return rc;
0ae7a7b2 1744 sch->driver = &io_subchannel_driver;
1da177e4 1745 /* Initialize the ccw_device structure. */
292888c8 1746 cdev->dev.parent= &sch->dev;
1da177e4
LT
1747 rc = io_subchannel_recog(cdev, sch);
1748 if (rc)
1749 return rc;
1750
1751 /* Now wait for the async. recognition to come to an end. */
1752 spin_lock_irq(cdev->ccwlock);
1753 while (!dev_fsm_final_state(cdev))
1754 wait_cons_dev();
1755 rc = -EIO;
1756 if (cdev->private->state != DEV_STATE_OFFLINE)
1757 goto out_unlock;
1758 ccw_device_online(cdev);
1759 while (!dev_fsm_final_state(cdev))
1760 wait_cons_dev();
1761 if (cdev->private->state != DEV_STATE_ONLINE)
1762 goto out_unlock;
1763 rc = 0;
1764out_unlock:
1765 spin_unlock_irq(cdev->ccwlock);
1766 return 0;
1767}
1768
1769struct ccw_device *
1770ccw_device_probe_console(void)
1771{
1772 struct subchannel *sch;
1773 int ret;
1774
1775 if (xchg(&console_cdev_in_use, 1) != 0)
600b5d16 1776 return ERR_PTR(-EBUSY);
1da177e4
LT
1777 sch = cio_probe_console();
1778 if (IS_ERR(sch)) {
1779 console_cdev_in_use = 0;
1780 return (void *) sch;
1781 }
1782 memset(&console_cdev, 0, sizeof(struct ccw_device));
1783 memset(&console_private, 0, sizeof(struct ccw_device_private));
1784 console_cdev.private = &console_private;
c1637532 1785 console_private.cdev = &console_cdev;
1da177e4
LT
1786 ret = ccw_device_console_enable(&console_cdev, sch);
1787 if (ret) {
1788 cio_release_console();
1789 console_cdev_in_use = 0;
1790 return ERR_PTR(ret);
1791 }
1792 console_cdev.online = 1;
1793 return &console_cdev;
1794}
1f4e7eda 1795
6664845c
MS
1796static int ccw_device_pm_restore(struct device *dev);
1797
1798int ccw_device_force_console(void)
1799{
1800 if (!console_cdev_in_use)
1801 return -ENODEV;
1802 return ccw_device_pm_restore(&console_cdev.dev);
1803}
1804EXPORT_SYMBOL_GPL(ccw_device_force_console);
1da177e4
LT
1805#endif
1806
1807/*
1808 * get ccw_device matching the busid, but only if owned by cdrv
1809 */
b0744bd2
CH
1810static int
1811__ccwdev_check_busid(struct device *dev, void *id)
1812{
1813 char *bus_id;
1814
12975aef 1815 bus_id = id;
b0744bd2 1816
98df67b3 1817 return (strcmp(bus_id, dev_name(dev)) == 0);
b0744bd2
CH
1818}
1819
1820
b2ffd8e9
CH
1821/**
1822 * get_ccwdev_by_busid() - obtain device from a bus id
1823 * @cdrv: driver the device is owned by
1824 * @bus_id: bus id of the device to be searched
1825 *
1826 * This function searches all devices owned by @cdrv for a device with a bus
1827 * id matching @bus_id.
1828 * Returns:
1829 * If a match is found, its reference count of the found device is increased
1830 * and it is returned; else %NULL is returned.
1831 */
1832struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1833 const char *bus_id)
1da177e4 1834{
b0744bd2 1835 struct device *dev;
1da177e4
LT
1836 struct device_driver *drv;
1837
1838 drv = get_driver(&cdrv->driver);
1839 if (!drv)
b0744bd2 1840 return NULL;
1da177e4 1841
b0744bd2
CH
1842 dev = driver_find_device(drv, NULL, (void *)bus_id,
1843 __ccwdev_check_busid);
1da177e4
LT
1844 put_driver(drv);
1845
d2c993d8 1846 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
1847}
1848
1849/************************** device driver handling ************************/
1850
1851/* This is the implementation of the ccw_driver class. The probe, remove
1852 * and release methods are initially very similar to the device_driver
1853 * implementations, with the difference that they have ccw_device
1854 * arguments.
1855 *
1856 * A ccw driver also contains the information that is needed for
1857 * device matching.
1858 */
1859static int
1860ccw_device_probe (struct device *dev)
1861{
1862 struct ccw_device *cdev = to_ccwdev(dev);
1863 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1864 int ret;
1865
1866 cdev->drv = cdrv; /* to let the driver call _set_online */
1867
1868 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1869
1870 if (ret) {
d2c993d8 1871 cdev->drv = NULL;
1da177e4
LT
1872 return ret;
1873 }
1874
1875 return 0;
1876}
1877
1878static int
1879ccw_device_remove (struct device *dev)
1880{
1881 struct ccw_device *cdev = to_ccwdev(dev);
1882 struct ccw_driver *cdrv = cdev->drv;
1883 int ret;
1884
1da177e4
LT
1885 if (cdrv->remove)
1886 cdrv->remove(cdev);
1887 if (cdev->online) {
1888 cdev->online = 0;
1889 spin_lock_irq(cdev->ccwlock);
1890 ret = ccw_device_offline(cdev);
1891 spin_unlock_irq(cdev->ccwlock);
1892 if (ret == 0)
1893 wait_event(cdev->private->wait_q,
1894 dev_fsm_final_state(cdev));
1895 else
139b83dd 1896 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
1897 "device 0.%x.%04x\n",
1898 ret, cdev->private->dev_id.ssid,
1899 cdev->private->dev_id.devno);
9cd67421
CH
1900 /* Give up reference obtained in ccw_device_set_online(). */
1901 put_device(&cdev->dev);
1da177e4
LT
1902 }
1903 ccw_device_set_timeout(cdev, 0);
d2c993d8 1904 cdev->drv = NULL;
1da177e4
LT
1905 return 0;
1906}
1907
958974fb
CH
1908static void ccw_device_shutdown(struct device *dev)
1909{
1910 struct ccw_device *cdev;
1911
1912 cdev = to_ccwdev(dev);
1913 if (cdev->drv && cdev->drv->shutdown)
1914 cdev->drv->shutdown(cdev);
1842f2b1 1915 disable_cmf(cdev);
958974fb
CH
1916}
1917
823d494a
SO
1918static int ccw_device_pm_prepare(struct device *dev)
1919{
1920 struct ccw_device *cdev = to_ccwdev(dev);
1921
1922 if (work_pending(&cdev->private->kick_work))
1923 return -EAGAIN;
1924 /* Fail while device is being set online/offline. */
1925 if (atomic_read(&cdev->private->onoff))
1926 return -EAGAIN;
1927
1928 if (cdev->online && cdev->drv && cdev->drv->prepare)
1929 return cdev->drv->prepare(cdev);
1930
1931 return 0;
1932}
1933
1934static void ccw_device_pm_complete(struct device *dev)
1935{
1936 struct ccw_device *cdev = to_ccwdev(dev);
1937
1938 if (cdev->online && cdev->drv && cdev->drv->complete)
1939 cdev->drv->complete(cdev);
1940}
1941
1942static int ccw_device_pm_freeze(struct device *dev)
1943{
1944 struct ccw_device *cdev = to_ccwdev(dev);
1945 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1946 int ret, cm_enabled;
1947
1948 /* Fail suspend while device is in transistional state. */
1949 if (!dev_fsm_final_state(cdev))
1950 return -EAGAIN;
1951 if (!cdev->online)
1952 return 0;
1953 if (cdev->drv && cdev->drv->freeze) {
1954 ret = cdev->drv->freeze(cdev);
1955 if (ret)
1956 return ret;
1957 }
1958
1959 spin_lock_irq(sch->lock);
1960 cm_enabled = cdev->private->cmb != NULL;
1961 spin_unlock_irq(sch->lock);
1962 if (cm_enabled) {
1963 /* Don't have the css write on memory. */
1964 ret = ccw_set_cmf(cdev, 0);
1965 if (ret)
1966 return ret;
1967 }
1968 /* From here on, disallow device driver I/O. */
1969 spin_lock_irq(sch->lock);
1970 ret = cio_disable_subchannel(sch);
1971 spin_unlock_irq(sch->lock);
1972
1973 return ret;
1974}
1975
1976static int ccw_device_pm_thaw(struct device *dev)
1977{
1978 struct ccw_device *cdev = to_ccwdev(dev);
1979 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1980 int ret, cm_enabled;
1981
1982 if (!cdev->online)
1983 return 0;
1984
1985 spin_lock_irq(sch->lock);
1986 /* Allow device driver I/O again. */
1987 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1988 cm_enabled = cdev->private->cmb != NULL;
1989 spin_unlock_irq(sch->lock);
1990 if (ret)
1991 return ret;
1992
1993 if (cm_enabled) {
1994 ret = ccw_set_cmf(cdev, 1);
1995 if (ret)
1996 return ret;
1997 }
1998
1999 if (cdev->drv && cdev->drv->thaw)
2000 ret = cdev->drv->thaw(cdev);
2001
2002 return ret;
2003}
2004
2005static void __ccw_device_pm_restore(struct ccw_device *cdev)
2006{
2007 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2008 int ret;
2009
2010 if (cio_is_console(sch->schid))
2011 goto out;
2012 /*
2013 * While we were sleeping, devices may have gone or become
2014 * available again. Kick re-detection.
2015 */
2016 spin_lock_irq(sch->lock);
2017 cdev->private->flags.resuming = 1;
2018 ret = ccw_device_recognition(cdev);
2019 spin_unlock_irq(sch->lock);
2020 if (ret) {
2021 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
f014824e
SO
2022 "0.%x.%04x (ret=%d)\n",
2023 cdev->private->dev_id.ssid,
2024 cdev->private->dev_id.devno, ret);
823d494a
SO
2025 spin_lock_irq(sch->lock);
2026 cdev->private->state = DEV_STATE_DISCONNECTED;
2027 spin_unlock_irq(sch->lock);
2028 /* notify driver after the resume cb */
2029 goto out;
2030 }
2031 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2032 cdev->private->state == DEV_STATE_DISCONNECTED);
2033
2034out:
2035 cdev->private->flags.resuming = 0;
2036}
2037
2038static int resume_handle_boxed(struct ccw_device *cdev)
2039{
2040 cdev->private->state = DEV_STATE_BOXED;
2041 if (ccw_device_notify(cdev, CIO_BOXED))
2042 return 0;
2043 ccw_device_schedule_sch_unregister(cdev);
2044 return -ENODEV;
2045}
2046
2047static int resume_handle_disc(struct ccw_device *cdev)
2048{
2049 cdev->private->state = DEV_STATE_DISCONNECTED;
2050 if (ccw_device_notify(cdev, CIO_GONE))
2051 return 0;
2052 ccw_device_schedule_sch_unregister(cdev);
2053 return -ENODEV;
2054}
2055
2056static int ccw_device_pm_restore(struct device *dev)
2057{
2058 struct ccw_device *cdev = to_ccwdev(dev);
2059 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2060 int ret = 0, cm_enabled;
2061
2062 __ccw_device_pm_restore(cdev);
2063 spin_lock_irq(sch->lock);
2064 if (cio_is_console(sch->schid)) {
2065 cio_enable_subchannel(sch, (u32)(addr_t)sch);
2066 spin_unlock_irq(sch->lock);
2067 goto out_restore;
2068 }
2069 cdev->private->flags.donotify = 0;
2070 /* check recognition results */
2071 switch (cdev->private->state) {
2072 case DEV_STATE_OFFLINE:
2073 break;
2074 case DEV_STATE_BOXED:
2075 ret = resume_handle_boxed(cdev);
2076 spin_unlock_irq(sch->lock);
2077 if (ret)
2078 goto out;
2079 goto out_restore;
2080 case DEV_STATE_DISCONNECTED:
2081 goto out_disc_unlock;
2082 default:
2083 goto out_unreg_unlock;
2084 }
2085 /* check if the device id has changed */
2086 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
f014824e
SO
2087 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
2088 "changed from %04x to %04x)\n",
2089 sch->schid.ssid, sch->schid.sch_no,
823d494a
SO
2090 cdev->private->dev_id.devno,
2091 sch->schib.pmcw.dev);
2092 goto out_unreg_unlock;
2093 }
2094 /* check if the device type has changed */
2095 if (!ccw_device_test_sense_data(cdev)) {
2096 ccw_device_update_sense_data(cdev);
2097 PREPARE_WORK(&cdev->private->kick_work,
2098 ccw_device_do_unbind_bind);
2099 queue_work(ccw_device_work, &cdev->private->kick_work);
2100 ret = -ENODEV;
2101 goto out_unlock;
2102 }
2103 if (!cdev->online) {
2104 ret = 0;
2105 goto out_unlock;
2106 }
2107 ret = ccw_device_online(cdev);
2108 if (ret)
2109 goto out_disc_unlock;
2110
2111 cm_enabled = cdev->private->cmb != NULL;
2112 spin_unlock_irq(sch->lock);
2113
2114 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2115 if (cdev->private->state != DEV_STATE_ONLINE) {
2116 spin_lock_irq(sch->lock);
2117 goto out_disc_unlock;
2118 }
2119 if (cm_enabled) {
2120 ret = ccw_set_cmf(cdev, 1);
2121 if (ret) {
f014824e
SO
2122 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2123 "(rc=%d)\n", cdev->private->dev_id.ssid,
2124 cdev->private->dev_id.devno, ret);
823d494a
SO
2125 ret = 0;
2126 }
2127 }
2128
2129out_restore:
2130 if (cdev->online && cdev->drv && cdev->drv->restore)
2131 ret = cdev->drv->restore(cdev);
2132out:
2133 return ret;
2134
2135out_disc_unlock:
2136 ret = resume_handle_disc(cdev);
2137 spin_unlock_irq(sch->lock);
2138 if (ret)
2139 return ret;
2140 goto out_restore;
2141
2142out_unreg_unlock:
2143 ccw_device_schedule_sch_unregister(cdev);
2144 ret = -ENODEV;
2145out_unlock:
2146 spin_unlock_irq(sch->lock);
2147 return ret;
2148}
2149
2150static struct dev_pm_ops ccw_pm_ops = {
2151 .prepare = ccw_device_pm_prepare,
2152 .complete = ccw_device_pm_complete,
2153 .freeze = ccw_device_pm_freeze,
2154 .thaw = ccw_device_pm_thaw,
2155 .restore = ccw_device_pm_restore,
2156};
2157
8bbace7e
CH
2158struct bus_type ccw_bus_type = {
2159 .name = "ccw",
2160 .match = ccw_bus_match,
2161 .uevent = ccw_uevent,
2162 .probe = ccw_device_probe,
2163 .remove = ccw_device_remove,
958974fb 2164 .shutdown = ccw_device_shutdown,
823d494a 2165 .pm = &ccw_pm_ops,
8bbace7e
CH
2166};
2167
b2ffd8e9
CH
2168/**
2169 * ccw_driver_register() - register a ccw driver
2170 * @cdriver: driver to be registered
2171 *
2172 * This function is mainly a wrapper around driver_register().
2173 * Returns:
2174 * %0 on success and a negative error value on failure.
2175 */
2176int ccw_driver_register(struct ccw_driver *cdriver)
1da177e4
LT
2177{
2178 struct device_driver *drv = &cdriver->driver;
2179
2180 drv->bus = &ccw_bus_type;
2181 drv->name = cdriver->name;
4beee646 2182 drv->owner = cdriver->owner;
1da177e4
LT
2183
2184 return driver_register(drv);
2185}
2186
b2ffd8e9
CH
2187/**
2188 * ccw_driver_unregister() - deregister a ccw driver
2189 * @cdriver: driver to be deregistered
2190 *
2191 * This function is mainly a wrapper around driver_unregister().
2192 */
2193void ccw_driver_unregister(struct ccw_driver *cdriver)
1da177e4
LT
2194{
2195 driver_unregister(&cdriver->driver);
2196}
2197
a8237fc4
CH
2198/* Helper func for qdio. */
2199struct subchannel_id
2200ccw_device_get_subchannel_id(struct ccw_device *cdev)
2201{
2202 struct subchannel *sch;
2203
2204 sch = to_subchannel(cdev->dev.parent);
2205 return sch->schid;
2206}
2207
1da177e4
LT
2208MODULE_LICENSE("GPL");
2209EXPORT_SYMBOL(ccw_device_set_online);
2210EXPORT_SYMBOL(ccw_device_set_offline);
2211EXPORT_SYMBOL(ccw_driver_register);
2212EXPORT_SYMBOL(ccw_driver_unregister);
2213EXPORT_SYMBOL(get_ccwdev_by_busid);
2214EXPORT_SYMBOL(ccw_bus_type);
2215EXPORT_SYMBOL(ccw_device_work);
a8237fc4 2216EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);