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