[S390] cio: move device unregistration to dedicated work queue
[linux/fpc-iii.git] / drivers / s390 / cio / device.c
blob38a79ecfc743edff2eaee6b746c4a16c2ec18796
1 /*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
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>
19 #include <linux/timer.h>
21 #include <asm/ccwdev.h>
22 #include <asm/cio.h>
23 #include <asm/param.h> /* HZ */
24 #include <asm/cmb.h>
25 #include <asm/isc.h>
27 #include "chp.h"
28 #include "cio.h"
29 #include "cio_debug.h"
30 #include "css.h"
31 #include "device.h"
32 #include "ioasm.h"
33 #include "io_sch.h"
35 static struct timer_list recovery_timer;
36 static DEFINE_SPINLOCK(recovery_lock);
37 static int recovery_phase;
38 static const unsigned long recovery_delay[] = { 3, 30, 300 };
40 /******************* bus type handling ***********************/
42 /* The Linux driver model distinguishes between a bus type and
43 * the bus itself. Of course we only have one channel
44 * subsystem driver and one channel system per machine, but
45 * we still use the abstraction. T.R. says it's a good idea. */
46 static int
47 ccw_bus_match (struct device * dev, struct device_driver * drv)
49 struct ccw_device *cdev = to_ccwdev(dev);
50 struct ccw_driver *cdrv = to_ccwdrv(drv);
51 const struct ccw_device_id *ids = cdrv->ids, *found;
53 if (!ids)
54 return 0;
56 found = ccw_device_id_match(ids, &cdev->id);
57 if (!found)
58 return 0;
60 cdev->id.driver_info = found->driver_info;
62 return 1;
65 /* Store modalias string delimited by prefix/suffix string into buffer with
66 * specified size. Return length of resulting string (excluding trailing '\0')
67 * even if string doesn't fit buffer (snprintf semantics). */
68 static int snprint_alias(char *buf, size_t size,
69 struct ccw_device_id *id, const char *suffix)
71 int len;
73 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
74 if (len > size)
75 return len;
76 buf += len;
77 size -= len;
79 if (id->dev_type != 0)
80 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
81 id->dev_model, suffix);
82 else
83 len += snprintf(buf, size, "dtdm%s", suffix);
85 return len;
88 /* Set up environment variables for ccw device uevent. Return 0 on success,
89 * non-zero otherwise. */
90 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
92 struct ccw_device *cdev = to_ccwdev(dev);
93 struct ccw_device_id *id = &(cdev->id);
94 int ret;
95 char modalias_buf[30];
97 /* CU_TYPE= */
98 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
99 if (ret)
100 return ret;
102 /* CU_MODEL= */
103 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
104 if (ret)
105 return ret;
107 /* The next two can be zero, that's ok for us */
108 /* DEV_TYPE= */
109 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
110 if (ret)
111 return ret;
113 /* DEV_MODEL= */
114 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
115 if (ret)
116 return ret;
118 /* MODALIAS= */
119 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
120 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
121 return ret;
124 struct bus_type ccw_bus_type;
126 static void io_subchannel_irq(struct subchannel *);
127 static int io_subchannel_probe(struct subchannel *);
128 static int io_subchannel_remove(struct subchannel *);
129 static void io_subchannel_shutdown(struct subchannel *);
130 static int io_subchannel_sch_event(struct subchannel *, int);
131 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
132 int);
134 static struct css_device_id io_subchannel_ids[] = {
135 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
136 { /* end of list */ },
138 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
140 static struct css_driver io_subchannel_driver = {
141 .owner = THIS_MODULE,
142 .subchannel_type = io_subchannel_ids,
143 .name = "io_subchannel",
144 .irq = io_subchannel_irq,
145 .sch_event = io_subchannel_sch_event,
146 .chp_event = io_subchannel_chp_event,
147 .probe = io_subchannel_probe,
148 .remove = io_subchannel_remove,
149 .shutdown = io_subchannel_shutdown,
152 struct workqueue_struct *ccw_device_work;
153 wait_queue_head_t ccw_device_init_wq;
154 atomic_t ccw_device_init_count;
156 static void recovery_func(unsigned long data);
158 static int __init
159 init_ccw_bus_type (void)
161 int ret;
163 init_waitqueue_head(&ccw_device_init_wq);
164 atomic_set(&ccw_device_init_count, 0);
165 setup_timer(&recovery_timer, recovery_func, 0);
167 ccw_device_work = create_singlethread_workqueue("cio");
168 if (!ccw_device_work)
169 return -ENOMEM; /* FIXME: better errno ? */
170 slow_path_wq = create_singlethread_workqueue("kslowcrw");
171 if (!slow_path_wq) {
172 ret = -ENOMEM; /* FIXME: better errno ? */
173 goto out_err;
175 if ((ret = bus_register (&ccw_bus_type)))
176 goto out_err;
178 ret = css_driver_register(&io_subchannel_driver);
179 if (ret)
180 goto out_err;
182 wait_event(ccw_device_init_wq,
183 atomic_read(&ccw_device_init_count) == 0);
184 flush_workqueue(ccw_device_work);
185 return 0;
186 out_err:
187 if (ccw_device_work)
188 destroy_workqueue(ccw_device_work);
189 if (slow_path_wq)
190 destroy_workqueue(slow_path_wq);
191 return ret;
194 static void __exit
195 cleanup_ccw_bus_type (void)
197 css_driver_unregister(&io_subchannel_driver);
198 bus_unregister(&ccw_bus_type);
199 destroy_workqueue(ccw_device_work);
202 subsys_initcall(init_ccw_bus_type);
203 module_exit(cleanup_ccw_bus_type);
205 /************************ device handling **************************/
208 * A ccw_device has some interfaces in sysfs in addition to the
209 * standard ones.
210 * The following entries are designed to export the information which
211 * resided in 2.4 in /proc/subchannels. Subchannel and device number
212 * are obvious, so they don't have an entry :)
213 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
215 static ssize_t
216 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
218 struct subchannel *sch = to_subchannel(dev);
219 struct chsc_ssd_info *ssd = &sch->ssd_info;
220 ssize_t ret = 0;
221 int chp;
222 int mask;
224 for (chp = 0; chp < 8; chp++) {
225 mask = 0x80 >> chp;
226 if (ssd->path_mask & mask)
227 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
228 else
229 ret += sprintf(buf + ret, "00 ");
231 ret += sprintf (buf+ret, "\n");
232 return min((ssize_t)PAGE_SIZE, ret);
235 static ssize_t
236 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
238 struct subchannel *sch = to_subchannel(dev);
239 struct pmcw *pmcw = &sch->schib.pmcw;
241 return sprintf (buf, "%02x %02x %02x\n",
242 pmcw->pim, pmcw->pam, pmcw->pom);
245 static ssize_t
246 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
248 struct ccw_device *cdev = to_ccwdev(dev);
249 struct ccw_device_id *id = &(cdev->id);
251 if (id->dev_type != 0)
252 return sprintf(buf, "%04x/%02x\n",
253 id->dev_type, id->dev_model);
254 else
255 return sprintf(buf, "n/a\n");
258 static ssize_t
259 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
261 struct ccw_device *cdev = to_ccwdev(dev);
262 struct ccw_device_id *id = &(cdev->id);
264 return sprintf(buf, "%04x/%02x\n",
265 id->cu_type, id->cu_model);
268 static ssize_t
269 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
271 struct ccw_device *cdev = to_ccwdev(dev);
272 struct ccw_device_id *id = &(cdev->id);
273 int len;
275 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
277 return len > PAGE_SIZE ? PAGE_SIZE : len;
280 static ssize_t
281 online_show (struct device *dev, struct device_attribute *attr, char *buf)
283 struct ccw_device *cdev = to_ccwdev(dev);
285 return sprintf(buf, cdev->online ? "1\n" : "0\n");
288 int ccw_device_is_orphan(struct ccw_device *cdev)
290 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
293 static void ccw_device_unregister(struct ccw_device *cdev)
295 if (test_and_clear_bit(1, &cdev->private->registered))
296 device_del(&cdev->dev);
299 static void ccw_device_remove_orphan_cb(struct work_struct *work)
301 struct ccw_device_private *priv;
302 struct ccw_device *cdev;
304 priv = container_of(work, struct ccw_device_private, kick_work);
305 cdev = priv->cdev;
306 ccw_device_unregister(cdev);
307 put_device(&cdev->dev);
308 /* Release cdev reference for workqueue processing. */
309 put_device(&cdev->dev);
312 static void ccw_device_call_sch_unregister(struct work_struct *work);
314 static void
315 ccw_device_remove_disconnected(struct ccw_device *cdev)
317 unsigned long flags;
320 * Forced offline in disconnected state means
321 * 'throw away device'.
323 /* Get cdev reference for workqueue processing. */
324 if (!get_device(&cdev->dev))
325 return;
326 if (ccw_device_is_orphan(cdev)) {
328 * Deregister ccw device.
329 * Unfortunately, we cannot do this directly from the
330 * attribute method.
332 spin_lock_irqsave(cdev->ccwlock, flags);
333 cdev->private->state = DEV_STATE_NOT_OPER;
334 spin_unlock_irqrestore(cdev->ccwlock, flags);
335 PREPARE_WORK(&cdev->private->kick_work,
336 ccw_device_remove_orphan_cb);
337 } else
338 /* Deregister subchannel, which will kill the ccw device. */
339 PREPARE_WORK(&cdev->private->kick_work,
340 ccw_device_call_sch_unregister);
341 queue_work(slow_path_wq, &cdev->private->kick_work);
345 * ccw_device_set_offline() - disable a ccw device for I/O
346 * @cdev: target ccw device
348 * This function calls the driver's set_offline() function for @cdev, if
349 * given, and then disables @cdev.
350 * Returns:
351 * %0 on success and a negative error value on failure.
352 * Context:
353 * enabled, ccw device lock not held
355 int ccw_device_set_offline(struct ccw_device *cdev)
357 int ret;
359 if (!cdev)
360 return -ENODEV;
361 if (!cdev->online || !cdev->drv)
362 return -EINVAL;
364 if (cdev->drv->set_offline) {
365 ret = cdev->drv->set_offline(cdev);
366 if (ret != 0)
367 return ret;
369 cdev->online = 0;
370 spin_lock_irq(cdev->ccwlock);
371 ret = ccw_device_offline(cdev);
372 if (ret == -ENODEV) {
373 if (cdev->private->state != DEV_STATE_NOT_OPER) {
374 cdev->private->state = DEV_STATE_OFFLINE;
375 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
377 spin_unlock_irq(cdev->ccwlock);
378 return ret;
380 spin_unlock_irq(cdev->ccwlock);
381 if (ret == 0)
382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
383 else {
384 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
385 "device 0.%x.%04x\n",
386 ret, cdev->private->dev_id.ssid,
387 cdev->private->dev_id.devno);
388 cdev->online = 1;
390 return ret;
394 * ccw_device_set_online() - enable a ccw device for I/O
395 * @cdev: target ccw device
397 * This function first enables @cdev and then calls the driver's set_online()
398 * function for @cdev, if given. If set_online() returns an error, @cdev is
399 * disabled again.
400 * Returns:
401 * %0 on success and a negative error value on failure.
402 * Context:
403 * enabled, ccw device lock not held
405 int ccw_device_set_online(struct ccw_device *cdev)
407 int ret;
409 if (!cdev)
410 return -ENODEV;
411 if (cdev->online || !cdev->drv)
412 return -EINVAL;
414 spin_lock_irq(cdev->ccwlock);
415 ret = ccw_device_online(cdev);
416 spin_unlock_irq(cdev->ccwlock);
417 if (ret == 0)
418 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
419 else {
420 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
421 "device 0.%x.%04x\n",
422 ret, cdev->private->dev_id.ssid,
423 cdev->private->dev_id.devno);
424 return ret;
426 if (cdev->private->state != DEV_STATE_ONLINE)
427 return -ENODEV;
428 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
429 cdev->online = 1;
430 return 0;
432 spin_lock_irq(cdev->ccwlock);
433 ret = ccw_device_offline(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
438 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
439 "device 0.%x.%04x\n",
440 ret, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
442 return (ret == 0) ? -ENODEV : ret;
445 static void online_store_handle_offline(struct ccw_device *cdev)
447 if (cdev->private->state == DEV_STATE_DISCONNECTED)
448 ccw_device_remove_disconnected(cdev);
449 else if (cdev->drv && cdev->drv->set_offline)
450 ccw_device_set_offline(cdev);
453 static int online_store_recog_and_online(struct ccw_device *cdev)
455 int ret;
457 /* Do device recognition, if needed. */
458 if (cdev->id.cu_type == 0) {
459 ret = ccw_device_recognition(cdev);
460 if (ret) {
461 CIO_MSG_EVENT(0, "Couldn't start recognition "
462 "for device 0.%x.%04x (ret=%d)\n",
463 cdev->private->dev_id.ssid,
464 cdev->private->dev_id.devno, ret);
465 return ret;
467 wait_event(cdev->private->wait_q,
468 cdev->private->flags.recog_done);
470 if (cdev->drv && cdev->drv->set_online)
471 ccw_device_set_online(cdev);
472 return 0;
474 static int online_store_handle_online(struct ccw_device *cdev, int force)
476 int ret;
478 ret = online_store_recog_and_online(cdev);
479 if (ret)
480 return ret;
481 if (force && cdev->private->state == DEV_STATE_BOXED) {
482 ret = ccw_device_stlck(cdev);
483 if (ret)
484 return ret;
485 if (cdev->id.cu_type == 0)
486 cdev->private->state = DEV_STATE_NOT_OPER;
487 online_store_recog_and_online(cdev);
489 return 0;
492 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
493 const char *buf, size_t count)
495 struct ccw_device *cdev = to_ccwdev(dev);
496 int force, ret;
497 unsigned long i;
499 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
500 return -EAGAIN;
502 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
503 atomic_set(&cdev->private->onoff, 0);
504 return -EINVAL;
506 if (!strncmp(buf, "force\n", count)) {
507 force = 1;
508 i = 1;
509 ret = 0;
510 } else {
511 force = 0;
512 ret = strict_strtoul(buf, 16, &i);
514 if (ret)
515 goto out;
516 switch (i) {
517 case 0:
518 online_store_handle_offline(cdev);
519 ret = count;
520 break;
521 case 1:
522 ret = online_store_handle_online(cdev, force);
523 if (!ret)
524 ret = count;
525 break;
526 default:
527 ret = -EINVAL;
529 out:
530 if (cdev->drv)
531 module_put(cdev->drv->owner);
532 atomic_set(&cdev->private->onoff, 0);
533 return ret;
536 static ssize_t
537 available_show (struct device *dev, struct device_attribute *attr, char *buf)
539 struct ccw_device *cdev = to_ccwdev(dev);
540 struct subchannel *sch;
542 if (ccw_device_is_orphan(cdev))
543 return sprintf(buf, "no device\n");
544 switch (cdev->private->state) {
545 case DEV_STATE_BOXED:
546 return sprintf(buf, "boxed\n");
547 case DEV_STATE_DISCONNECTED:
548 case DEV_STATE_DISCONNECTED_SENSE_ID:
549 case DEV_STATE_NOT_OPER:
550 sch = to_subchannel(dev->parent);
551 if (!sch->lpm)
552 return sprintf(buf, "no path\n");
553 else
554 return sprintf(buf, "no device\n");
555 default:
556 /* All other states considered fine. */
557 return sprintf(buf, "good\n");
561 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
562 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
563 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
564 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
565 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
566 static DEVICE_ATTR(online, 0644, online_show, online_store);
567 static DEVICE_ATTR(availability, 0444, available_show, NULL);
569 static struct attribute *io_subchannel_attrs[] = {
570 &dev_attr_chpids.attr,
571 &dev_attr_pimpampom.attr,
572 NULL,
575 static struct attribute_group io_subchannel_attr_group = {
576 .attrs = io_subchannel_attrs,
579 static struct attribute * ccwdev_attrs[] = {
580 &dev_attr_devtype.attr,
581 &dev_attr_cutype.attr,
582 &dev_attr_modalias.attr,
583 &dev_attr_online.attr,
584 &dev_attr_cmb_enable.attr,
585 &dev_attr_availability.attr,
586 NULL,
589 static struct attribute_group ccwdev_attr_group = {
590 .attrs = ccwdev_attrs,
593 static struct attribute_group *ccwdev_attr_groups[] = {
594 &ccwdev_attr_group,
595 NULL,
598 /* this is a simple abstraction for device_register that sets the
599 * correct bus type and adds the bus specific files */
600 static int ccw_device_register(struct ccw_device *cdev)
602 struct device *dev = &cdev->dev;
603 int ret;
605 dev->bus = &ccw_bus_type;
607 if ((ret = device_add(dev)))
608 return ret;
610 set_bit(1, &cdev->private->registered);
611 return ret;
614 struct match_data {
615 struct ccw_dev_id dev_id;
616 struct ccw_device * sibling;
619 static int
620 match_devno(struct device * dev, void * data)
622 struct match_data * d = data;
623 struct ccw_device * cdev;
625 cdev = to_ccwdev(dev);
626 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
627 !ccw_device_is_orphan(cdev) &&
628 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
629 (cdev != d->sibling))
630 return 1;
631 return 0;
634 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
635 struct ccw_device *sibling)
637 struct device *dev;
638 struct match_data data;
640 data.dev_id = *dev_id;
641 data.sibling = sibling;
642 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
644 return dev ? to_ccwdev(dev) : NULL;
647 static int match_orphan(struct device *dev, void *data)
649 struct ccw_dev_id *dev_id;
650 struct ccw_device *cdev;
652 dev_id = data;
653 cdev = to_ccwdev(dev);
654 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
657 static struct ccw_device *
658 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
659 struct ccw_dev_id *dev_id)
661 struct device *dev;
663 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
664 match_orphan);
666 return dev ? to_ccwdev(dev) : NULL;
669 static void
670 ccw_device_add_changed(struct work_struct *work)
672 struct ccw_device_private *priv;
673 struct ccw_device *cdev;
675 priv = container_of(work, struct ccw_device_private, kick_work);
676 cdev = priv->cdev;
677 if (device_add(&cdev->dev)) {
678 put_device(&cdev->dev);
679 return;
681 set_bit(1, &cdev->private->registered);
684 void ccw_device_do_unreg_rereg(struct work_struct *work)
686 struct ccw_device_private *priv;
687 struct ccw_device *cdev;
688 struct subchannel *sch;
690 priv = container_of(work, struct ccw_device_private, kick_work);
691 cdev = priv->cdev;
692 sch = to_subchannel(cdev->dev.parent);
694 ccw_device_unregister(cdev);
695 PREPARE_WORK(&cdev->private->kick_work,
696 ccw_device_add_changed);
697 queue_work(ccw_device_work, &cdev->private->kick_work);
700 static void
701 ccw_device_release(struct device *dev)
703 struct ccw_device *cdev;
705 cdev = to_ccwdev(dev);
706 kfree(cdev->private);
707 kfree(cdev);
710 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
712 struct ccw_device *cdev;
714 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
715 if (cdev) {
716 cdev->private = kzalloc(sizeof(struct ccw_device_private),
717 GFP_KERNEL | GFP_DMA);
718 if (cdev->private)
719 return cdev;
721 kfree(cdev);
722 return ERR_PTR(-ENOMEM);
725 static int io_subchannel_initialize_dev(struct subchannel *sch,
726 struct ccw_device *cdev)
728 cdev->private->cdev = cdev;
729 atomic_set(&cdev->private->onoff, 0);
730 cdev->dev.parent = &sch->dev;
731 cdev->dev.release = ccw_device_release;
732 INIT_WORK(&cdev->private->kick_work, NULL);
733 cdev->dev.groups = ccwdev_attr_groups;
734 /* Do first half of device_register. */
735 device_initialize(&cdev->dev);
736 if (!get_device(&sch->dev)) {
737 if (cdev->dev.release)
738 cdev->dev.release(&cdev->dev);
739 return -ENODEV;
741 return 0;
744 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
746 struct ccw_device *cdev;
747 int ret;
749 cdev = io_subchannel_allocate_dev(sch);
750 if (!IS_ERR(cdev)) {
751 ret = io_subchannel_initialize_dev(sch, cdev);
752 if (ret) {
753 kfree(cdev);
754 cdev = ERR_PTR(ret);
757 return cdev;
760 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
762 static void sch_attach_device(struct subchannel *sch,
763 struct ccw_device *cdev)
765 css_update_ssd_info(sch);
766 spin_lock_irq(sch->lock);
767 sch_set_cdev(sch, cdev);
768 cdev->private->schid = sch->schid;
769 cdev->ccwlock = sch->lock;
770 ccw_device_trigger_reprobe(cdev);
771 spin_unlock_irq(sch->lock);
774 static void sch_attach_disconnected_device(struct subchannel *sch,
775 struct ccw_device *cdev)
777 struct subchannel *other_sch;
778 int ret;
780 other_sch = to_subchannel(get_device(cdev->dev.parent));
781 ret = device_move(&cdev->dev, &sch->dev);
782 if (ret) {
783 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
784 "(ret=%d)!\n", cdev->private->dev_id.ssid,
785 cdev->private->dev_id.devno, ret);
786 put_device(&other_sch->dev);
787 return;
789 sch_set_cdev(other_sch, NULL);
790 /* No need to keep a subchannel without ccw device around. */
791 css_sch_device_unregister(other_sch);
792 put_device(&other_sch->dev);
793 sch_attach_device(sch, cdev);
796 static void sch_attach_orphaned_device(struct subchannel *sch,
797 struct ccw_device *cdev)
799 int ret;
801 /* Try to move the ccw device to its new subchannel. */
802 ret = device_move(&cdev->dev, &sch->dev);
803 if (ret) {
804 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
805 "failed (ret=%d)!\n",
806 cdev->private->dev_id.ssid,
807 cdev->private->dev_id.devno, ret);
808 return;
810 sch_attach_device(sch, cdev);
813 static void sch_create_and_recog_new_device(struct subchannel *sch)
815 struct ccw_device *cdev;
817 /* Need to allocate a new ccw device. */
818 cdev = io_subchannel_create_ccwdev(sch);
819 if (IS_ERR(cdev)) {
820 /* OK, we did everything we could... */
821 css_sch_device_unregister(sch);
822 return;
824 spin_lock_irq(sch->lock);
825 sch_set_cdev(sch, cdev);
826 spin_unlock_irq(sch->lock);
827 /* Start recognition for the new ccw device. */
828 if (io_subchannel_recog(cdev, sch)) {
829 spin_lock_irq(sch->lock);
830 sch_set_cdev(sch, NULL);
831 spin_unlock_irq(sch->lock);
832 if (cdev->dev.release)
833 cdev->dev.release(&cdev->dev);
834 css_sch_device_unregister(sch);
839 void ccw_device_move_to_orphanage(struct work_struct *work)
841 struct ccw_device_private *priv;
842 struct ccw_device *cdev;
843 struct ccw_device *replacing_cdev;
844 struct subchannel *sch;
845 int ret;
846 struct channel_subsystem *css;
847 struct ccw_dev_id dev_id;
849 priv = container_of(work, struct ccw_device_private, kick_work);
850 cdev = priv->cdev;
851 sch = to_subchannel(cdev->dev.parent);
852 css = to_css(sch->dev.parent);
853 dev_id.devno = sch->schib.pmcw.dev;
854 dev_id.ssid = sch->schid.ssid;
857 * Move the orphaned ccw device to the orphanage so the replacing
858 * ccw device can take its place on the subchannel.
860 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
861 if (ret) {
862 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
863 "(ret=%d)!\n", cdev->private->dev_id.ssid,
864 cdev->private->dev_id.devno, ret);
865 return;
867 cdev->ccwlock = css->pseudo_subchannel->lock;
869 * Search for the replacing ccw device
870 * - among the disconnected devices
871 * - in the orphanage
873 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
874 if (replacing_cdev) {
875 sch_attach_disconnected_device(sch, replacing_cdev);
876 return;
878 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
879 if (replacing_cdev) {
880 sch_attach_orphaned_device(sch, replacing_cdev);
881 return;
883 sch_create_and_recog_new_device(sch);
887 * Register recognized device.
889 static void
890 io_subchannel_register(struct work_struct *work)
892 struct ccw_device_private *priv;
893 struct ccw_device *cdev;
894 struct subchannel *sch;
895 int ret;
896 unsigned long flags;
898 priv = container_of(work, struct ccw_device_private, kick_work);
899 cdev = priv->cdev;
900 sch = to_subchannel(cdev->dev.parent);
901 css_update_ssd_info(sch);
903 * io_subchannel_register() will also be called after device
904 * recognition has been done for a boxed device (which will already
905 * be registered). We need to reprobe since we may now have sense id
906 * information.
908 if (klist_node_attached(&cdev->dev.knode_parent)) {
909 if (!cdev->drv) {
910 ret = device_reprobe(&cdev->dev);
911 if (ret)
912 /* We can't do much here. */
913 CIO_MSG_EVENT(0, "device_reprobe() returned"
914 " %d for 0.%x.%04x\n", ret,
915 cdev->private->dev_id.ssid,
916 cdev->private->dev_id.devno);
918 goto out;
921 * Now we know this subchannel will stay, we can throw
922 * our delayed uevent.
924 sch->dev.uevent_suppress = 0;
925 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
926 /* make it known to the system */
927 ret = ccw_device_register(cdev);
928 if (ret) {
929 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
930 cdev->private->dev_id.ssid,
931 cdev->private->dev_id.devno, ret);
932 put_device(&cdev->dev);
933 spin_lock_irqsave(sch->lock, flags);
934 sch_set_cdev(sch, NULL);
935 spin_unlock_irqrestore(sch->lock, flags);
936 kfree (cdev->private);
937 kfree (cdev);
938 put_device(&sch->dev);
939 if (atomic_dec_and_test(&ccw_device_init_count))
940 wake_up(&ccw_device_init_wq);
941 return;
943 put_device(&cdev->dev);
944 out:
945 cdev->private->flags.recog_done = 1;
946 put_device(&sch->dev);
947 wake_up(&cdev->private->wait_q);
948 if (atomic_dec_and_test(&ccw_device_init_count))
949 wake_up(&ccw_device_init_wq);
952 static void ccw_device_call_sch_unregister(struct work_struct *work)
954 struct ccw_device_private *priv;
955 struct ccw_device *cdev;
956 struct subchannel *sch;
958 priv = container_of(work, struct ccw_device_private, kick_work);
959 cdev = priv->cdev;
960 /* Get subchannel reference for local processing. */
961 if (!get_device(cdev->dev.parent))
962 return;
963 sch = to_subchannel(cdev->dev.parent);
964 css_sch_device_unregister(sch);
965 /* Reset intparm to zeroes. */
966 sch->schib.pmcw.intparm = 0;
967 cio_modify(sch);
968 /* Release cdev reference for workqueue processing.*/
969 put_device(&cdev->dev);
970 /* Release subchannel reference for local processing. */
971 put_device(&sch->dev);
975 * subchannel recognition done. Called from the state machine.
977 void
978 io_subchannel_recog_done(struct ccw_device *cdev)
980 struct subchannel *sch;
982 if (css_init_done == 0) {
983 cdev->private->flags.recog_done = 1;
984 return;
986 switch (cdev->private->state) {
987 case DEV_STATE_NOT_OPER:
988 cdev->private->flags.recog_done = 1;
989 /* Remove device found not operational. */
990 if (!get_device(&cdev->dev))
991 break;
992 sch = to_subchannel(cdev->dev.parent);
993 PREPARE_WORK(&cdev->private->kick_work,
994 ccw_device_call_sch_unregister);
995 queue_work(slow_path_wq, &cdev->private->kick_work);
996 /* Release subchannel reference for asynchronous recognition. */
997 put_device(&sch->dev);
998 if (atomic_dec_and_test(&ccw_device_init_count))
999 wake_up(&ccw_device_init_wq);
1000 break;
1001 case DEV_STATE_BOXED:
1002 /* Device did not respond in time. */
1003 case DEV_STATE_OFFLINE:
1005 * We can't register the device in interrupt context so
1006 * we schedule a work item.
1008 if (!get_device(&cdev->dev))
1009 break;
1010 PREPARE_WORK(&cdev->private->kick_work,
1011 io_subchannel_register);
1012 queue_work(slow_path_wq, &cdev->private->kick_work);
1013 break;
1017 static int
1018 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1020 int rc;
1021 struct ccw_device_private *priv;
1023 sch_set_cdev(sch, cdev);
1024 cdev->ccwlock = sch->lock;
1026 /* Init private data. */
1027 priv = cdev->private;
1028 priv->dev_id.devno = sch->schib.pmcw.dev;
1029 priv->dev_id.ssid = sch->schid.ssid;
1030 priv->schid = sch->schid;
1031 priv->state = DEV_STATE_NOT_OPER;
1032 INIT_LIST_HEAD(&priv->cmb_list);
1033 init_waitqueue_head(&priv->wait_q);
1034 init_timer(&priv->timer);
1036 /* Set an initial name for the device. */
1037 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1038 sch->schid.ssid, sch->schib.pmcw.dev);
1040 /* Increase counter of devices currently in recognition. */
1041 atomic_inc(&ccw_device_init_count);
1043 /* Start async. device sensing. */
1044 spin_lock_irq(sch->lock);
1045 rc = ccw_device_recognition(cdev);
1046 spin_unlock_irq(sch->lock);
1047 if (rc) {
1048 if (atomic_dec_and_test(&ccw_device_init_count))
1049 wake_up(&ccw_device_init_wq);
1051 return rc;
1054 static void ccw_device_move_to_sch(struct work_struct *work)
1056 struct ccw_device_private *priv;
1057 int rc;
1058 struct subchannel *sch;
1059 struct ccw_device *cdev;
1060 struct subchannel *former_parent;
1062 priv = container_of(work, struct ccw_device_private, kick_work);
1063 sch = priv->sch;
1064 cdev = priv->cdev;
1065 former_parent = ccw_device_is_orphan(cdev) ?
1066 NULL : to_subchannel(get_device(cdev->dev.parent));
1067 mutex_lock(&sch->reg_mutex);
1068 /* Try to move the ccw device to its new subchannel. */
1069 rc = device_move(&cdev->dev, &sch->dev);
1070 mutex_unlock(&sch->reg_mutex);
1071 if (rc) {
1072 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1073 "0.%x.%04x failed (ret=%d)!\n",
1074 cdev->private->dev_id.ssid,
1075 cdev->private->dev_id.devno, sch->schid.ssid,
1076 sch->schid.sch_no, rc);
1077 css_sch_device_unregister(sch);
1078 goto out;
1080 if (former_parent) {
1081 spin_lock_irq(former_parent->lock);
1082 sch_set_cdev(former_parent, NULL);
1083 spin_unlock_irq(former_parent->lock);
1084 css_sch_device_unregister(former_parent);
1085 /* Reset intparm to zeroes. */
1086 former_parent->schib.pmcw.intparm = 0;
1087 cio_modify(former_parent);
1089 sch_attach_device(sch, cdev);
1090 out:
1091 if (former_parent)
1092 put_device(&former_parent->dev);
1093 put_device(&cdev->dev);
1096 static void io_subchannel_irq(struct subchannel *sch)
1098 struct ccw_device *cdev;
1100 cdev = sch_get_cdev(sch);
1102 CIO_TRACE_EVENT(3, "IRQ");
1103 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1104 if (cdev)
1105 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1108 static void io_subchannel_init_fields(struct subchannel *sch)
1110 if (cio_is_console(sch->schid))
1111 sch->opm = 0xff;
1112 else
1113 sch->opm = chp_get_sch_opm(sch);
1114 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1115 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1117 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1118 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1119 sch->schib.pmcw.dev, sch->schid.ssid,
1120 sch->schid.sch_no, sch->schib.pmcw.pim,
1121 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1122 /* Initially set up some fields in the pmcw. */
1123 sch->schib.pmcw.ena = 0;
1124 sch->schib.pmcw.csense = 1; /* concurrent sense */
1125 if ((sch->lpm & (sch->lpm - 1)) != 0)
1126 sch->schib.pmcw.mp = 1; /* multipath mode */
1127 /* clean up possible residual cmf stuff */
1128 sch->schib.pmcw.mme = 0;
1129 sch->schib.pmcw.mbfc = 0;
1130 sch->schib.pmcw.mbi = 0;
1131 sch->schib.mba = 0;
1134 static int io_subchannel_probe(struct subchannel *sch)
1136 struct ccw_device *cdev;
1137 int rc;
1138 unsigned long flags;
1139 struct ccw_dev_id dev_id;
1141 cdev = sch_get_cdev(sch);
1142 if (cdev) {
1143 rc = sysfs_create_group(&sch->dev.kobj,
1144 &io_subchannel_attr_group);
1145 if (rc)
1146 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1147 "attributes for subchannel "
1148 "0.%x.%04x (rc=%d)\n",
1149 sch->schid.ssid, sch->schid.sch_no, rc);
1151 * This subchannel already has an associated ccw_device.
1152 * Throw the delayed uevent for the subchannel, register
1153 * the ccw_device and exit. This happens for all early
1154 * devices, e.g. the console.
1156 sch->dev.uevent_suppress = 0;
1157 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1158 cdev->dev.groups = ccwdev_attr_groups;
1159 device_initialize(&cdev->dev);
1160 ccw_device_register(cdev);
1162 * Check if the device is already online. If it is
1163 * the reference count needs to be corrected
1164 * (see ccw_device_online and css_init_done for the
1165 * ugly details).
1167 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1168 cdev->private->state != DEV_STATE_OFFLINE &&
1169 cdev->private->state != DEV_STATE_BOXED)
1170 get_device(&cdev->dev);
1171 return 0;
1173 io_subchannel_init_fields(sch);
1175 * First check if a fitting device may be found amongst the
1176 * disconnected devices or in the orphanage.
1178 dev_id.devno = sch->schib.pmcw.dev;
1179 dev_id.ssid = sch->schid.ssid;
1180 rc = sysfs_create_group(&sch->dev.kobj,
1181 &io_subchannel_attr_group);
1182 if (rc)
1183 return rc;
1184 /* Allocate I/O subchannel private data. */
1185 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1186 GFP_KERNEL | GFP_DMA);
1187 if (!sch->private) {
1188 rc = -ENOMEM;
1189 goto out_err;
1191 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1192 if (!cdev)
1193 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1194 &dev_id);
1195 if (cdev) {
1197 * Schedule moving the device until when we have a registered
1198 * subchannel to move to and succeed the probe. We can
1199 * unregister later again, when the probe is through.
1201 cdev->private->sch = sch;
1202 PREPARE_WORK(&cdev->private->kick_work,
1203 ccw_device_move_to_sch);
1204 queue_work(slow_path_wq, &cdev->private->kick_work);
1205 return 0;
1207 cdev = io_subchannel_create_ccwdev(sch);
1208 if (IS_ERR(cdev)) {
1209 rc = PTR_ERR(cdev);
1210 goto out_err;
1212 rc = io_subchannel_recog(cdev, sch);
1213 if (rc) {
1214 spin_lock_irqsave(sch->lock, flags);
1215 sch_set_cdev(sch, NULL);
1216 spin_unlock_irqrestore(sch->lock, flags);
1217 if (cdev->dev.release)
1218 cdev->dev.release(&cdev->dev);
1219 goto out_err;
1221 return 0;
1222 out_err:
1223 kfree(sch->private);
1224 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1225 return rc;
1228 static int
1229 io_subchannel_remove (struct subchannel *sch)
1231 struct ccw_device *cdev;
1232 unsigned long flags;
1234 cdev = sch_get_cdev(sch);
1235 if (!cdev)
1236 return 0;
1237 /* Set ccw device to not operational and drop reference. */
1238 spin_lock_irqsave(cdev->ccwlock, flags);
1239 sch_set_cdev(sch, NULL);
1240 cdev->private->state = DEV_STATE_NOT_OPER;
1241 spin_unlock_irqrestore(cdev->ccwlock, flags);
1242 ccw_device_unregister(cdev);
1243 put_device(&cdev->dev);
1244 kfree(sch->private);
1245 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1246 return 0;
1249 static int io_subchannel_notify(struct subchannel *sch, int event)
1251 struct ccw_device *cdev;
1253 cdev = sch_get_cdev(sch);
1254 if (!cdev)
1255 return 0;
1256 return ccw_device_notify(cdev, event);
1259 static void io_subchannel_verify(struct subchannel *sch)
1261 struct ccw_device *cdev;
1263 cdev = sch_get_cdev(sch);
1264 if (cdev)
1265 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1268 static int check_for_io_on_path(struct subchannel *sch, int mask)
1270 int cc;
1272 cc = stsch(sch->schid, &sch->schib);
1273 if (cc)
1274 return 0;
1275 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1276 return 1;
1277 return 0;
1280 static void terminate_internal_io(struct subchannel *sch,
1281 struct ccw_device *cdev)
1283 if (cio_clear(sch)) {
1284 /* Recheck device in case clear failed. */
1285 sch->lpm = 0;
1286 if (cdev->online)
1287 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1288 else
1289 css_schedule_eval(sch->schid);
1290 return;
1292 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1293 /* Request retry of internal operation. */
1294 cdev->private->flags.intretry = 1;
1295 /* Call handler. */
1296 if (cdev->handler)
1297 cdev->handler(cdev, cdev->private->intparm,
1298 ERR_PTR(-EIO));
1301 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1303 struct ccw_device *cdev;
1305 cdev = sch_get_cdev(sch);
1306 if (!cdev)
1307 return;
1308 if (check_for_io_on_path(sch, mask)) {
1309 if (cdev->private->state == DEV_STATE_ONLINE)
1310 ccw_device_kill_io(cdev);
1311 else {
1312 terminate_internal_io(sch, cdev);
1313 /* Re-start path verification. */
1314 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1316 } else
1317 /* trigger path verification. */
1318 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1322 static int io_subchannel_chp_event(struct subchannel *sch,
1323 struct chp_link *link, int event)
1325 int mask;
1327 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1328 if (!mask)
1329 return 0;
1330 switch (event) {
1331 case CHP_VARY_OFF:
1332 sch->opm &= ~mask;
1333 sch->lpm &= ~mask;
1334 io_subchannel_terminate_path(sch, mask);
1335 break;
1336 case CHP_VARY_ON:
1337 sch->opm |= mask;
1338 sch->lpm |= mask;
1339 io_subchannel_verify(sch);
1340 break;
1341 case CHP_OFFLINE:
1342 if (stsch(sch->schid, &sch->schib))
1343 return -ENXIO;
1344 if (!css_sch_is_valid(&sch->schib))
1345 return -ENODEV;
1346 io_subchannel_terminate_path(sch, mask);
1347 break;
1348 case CHP_ONLINE:
1349 if (stsch(sch->schid, &sch->schib))
1350 return -ENXIO;
1351 sch->lpm |= mask & sch->opm;
1352 io_subchannel_verify(sch);
1353 break;
1355 return 0;
1358 static void
1359 io_subchannel_shutdown(struct subchannel *sch)
1361 struct ccw_device *cdev;
1362 int ret;
1364 cdev = sch_get_cdev(sch);
1366 if (cio_is_console(sch->schid))
1367 return;
1368 if (!sch->schib.pmcw.ena)
1369 /* Nothing to do. */
1370 return;
1371 ret = cio_disable_subchannel(sch);
1372 if (ret != -EBUSY)
1373 /* Subchannel is disabled, we're done. */
1374 return;
1375 cdev->private->state = DEV_STATE_QUIESCE;
1376 if (cdev->handler)
1377 cdev->handler(cdev, cdev->private->intparm,
1378 ERR_PTR(-EIO));
1379 ret = ccw_device_cancel_halt_clear(cdev);
1380 if (ret == -EBUSY) {
1381 ccw_device_set_timeout(cdev, HZ/10);
1382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1384 cio_disable_subchannel(sch);
1387 static int io_subchannel_get_status(struct subchannel *sch)
1389 struct schib schib;
1391 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1392 return CIO_GONE;
1393 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1394 return CIO_REVALIDATE;
1395 if (!sch->lpm)
1396 return CIO_NO_PATH;
1397 return CIO_OPER;
1400 static int device_is_disconnected(struct ccw_device *cdev)
1402 if (!cdev)
1403 return 0;
1404 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1405 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1408 static int recovery_check(struct device *dev, void *data)
1410 struct ccw_device *cdev = to_ccwdev(dev);
1411 int *redo = data;
1413 spin_lock_irq(cdev->ccwlock);
1414 switch (cdev->private->state) {
1415 case DEV_STATE_DISCONNECTED:
1416 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1417 cdev->private->dev_id.ssid,
1418 cdev->private->dev_id.devno);
1419 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1420 *redo = 1;
1421 break;
1422 case DEV_STATE_DISCONNECTED_SENSE_ID:
1423 *redo = 1;
1424 break;
1426 spin_unlock_irq(cdev->ccwlock);
1428 return 0;
1431 static void recovery_work_func(struct work_struct *unused)
1433 int redo = 0;
1435 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1436 if (redo) {
1437 spin_lock_irq(&recovery_lock);
1438 if (!timer_pending(&recovery_timer)) {
1439 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1440 recovery_phase++;
1441 mod_timer(&recovery_timer, jiffies +
1442 recovery_delay[recovery_phase] * HZ);
1444 spin_unlock_irq(&recovery_lock);
1445 } else
1446 CIO_MSG_EVENT(4, "recovery: end\n");
1449 static DECLARE_WORK(recovery_work, recovery_work_func);
1451 static void recovery_func(unsigned long data)
1454 * We can't do our recovery in softirq context and it's not
1455 * performance critical, so we schedule it.
1457 schedule_work(&recovery_work);
1460 static void ccw_device_schedule_recovery(void)
1462 unsigned long flags;
1464 CIO_MSG_EVENT(4, "recovery: schedule\n");
1465 spin_lock_irqsave(&recovery_lock, flags);
1466 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1467 recovery_phase = 0;
1468 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1470 spin_unlock_irqrestore(&recovery_lock, flags);
1473 static void device_set_disconnected(struct ccw_device *cdev)
1475 if (!cdev)
1476 return;
1477 ccw_device_set_timeout(cdev, 0);
1478 cdev->private->flags.fake_irb = 0;
1479 cdev->private->state = DEV_STATE_DISCONNECTED;
1480 if (cdev->online)
1481 ccw_device_schedule_recovery();
1484 void ccw_device_set_notoper(struct ccw_device *cdev)
1486 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1488 CIO_TRACE_EVENT(2, "notoper");
1489 CIO_TRACE_EVENT(2, sch->dev.bus_id);
1490 ccw_device_set_timeout(cdev, 0);
1491 cio_disable_subchannel(sch);
1492 cdev->private->state = DEV_STATE_NOT_OPER;
1495 static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1497 int event, ret, disc;
1498 unsigned long flags;
1499 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1500 struct ccw_device *cdev;
1502 spin_lock_irqsave(sch->lock, flags);
1503 cdev = sch_get_cdev(sch);
1504 disc = device_is_disconnected(cdev);
1505 if (disc && slow) {
1506 /* Disconnected devices are evaluated directly only.*/
1507 spin_unlock_irqrestore(sch->lock, flags);
1508 return 0;
1510 /* No interrupt after machine check - kill pending timers. */
1511 if (cdev)
1512 ccw_device_set_timeout(cdev, 0);
1513 if (!disc && !slow) {
1514 /* Non-disconnected devices are evaluated on the slow path. */
1515 spin_unlock_irqrestore(sch->lock, flags);
1516 return -EAGAIN;
1518 event = io_subchannel_get_status(sch);
1519 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1520 sch->schid.ssid, sch->schid.sch_no, event,
1521 disc ? "disconnected" : "normal",
1522 slow ? "slow" : "fast");
1523 /* Analyze subchannel status. */
1524 action = NONE;
1525 switch (event) {
1526 case CIO_NO_PATH:
1527 if (disc) {
1528 /* Check if paths have become available. */
1529 action = REPROBE;
1530 break;
1532 /* fall through */
1533 case CIO_GONE:
1534 /* Ask driver what to do with device. */
1535 if (io_subchannel_notify(sch, event))
1536 action = DISC;
1537 else
1538 action = UNREGISTER;
1539 break;
1540 case CIO_REVALIDATE:
1541 /* Device will be removed, so no notify necessary. */
1542 if (disc)
1543 /* Reprobe because immediate unregister might block. */
1544 action = REPROBE;
1545 else
1546 action = UNREGISTER_PROBE;
1547 break;
1548 case CIO_OPER:
1549 if (disc)
1550 /* Get device operational again. */
1551 action = REPROBE;
1552 break;
1554 /* Perform action. */
1555 ret = 0;
1556 switch (action) {
1557 case UNREGISTER:
1558 case UNREGISTER_PROBE:
1559 ccw_device_set_notoper(cdev);
1560 /* Unregister device (will use subchannel lock). */
1561 spin_unlock_irqrestore(sch->lock, flags);
1562 css_sch_device_unregister(sch);
1563 spin_lock_irqsave(sch->lock, flags);
1565 /* Reset intparm to zeroes. */
1566 sch->schib.pmcw.intparm = 0;
1567 cio_modify(sch);
1568 break;
1569 case REPROBE:
1570 ccw_device_trigger_reprobe(cdev);
1571 break;
1572 case DISC:
1573 device_set_disconnected(cdev);
1574 break;
1575 default:
1576 break;
1578 spin_unlock_irqrestore(sch->lock, flags);
1579 /* Probe if necessary. */
1580 if (action == UNREGISTER_PROBE)
1581 ret = css_probe_device(sch->schid);
1583 return ret;
1586 #ifdef CONFIG_CCW_CONSOLE
1587 static struct ccw_device console_cdev;
1588 static struct ccw_device_private console_private;
1589 static int console_cdev_in_use;
1591 static DEFINE_SPINLOCK(ccw_console_lock);
1593 spinlock_t * cio_get_console_lock(void)
1595 return &ccw_console_lock;
1598 static int ccw_device_console_enable(struct ccw_device *cdev,
1599 struct subchannel *sch)
1601 int rc;
1603 /* Attach subchannel private data. */
1604 sch->private = cio_get_console_priv();
1605 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1606 io_subchannel_init_fields(sch);
1607 sch->driver = &io_subchannel_driver;
1608 /* Initialize the ccw_device structure. */
1609 cdev->dev.parent= &sch->dev;
1610 rc = io_subchannel_recog(cdev, sch);
1611 if (rc)
1612 return rc;
1614 /* Now wait for the async. recognition to come to an end. */
1615 spin_lock_irq(cdev->ccwlock);
1616 while (!dev_fsm_final_state(cdev))
1617 wait_cons_dev();
1618 rc = -EIO;
1619 if (cdev->private->state != DEV_STATE_OFFLINE)
1620 goto out_unlock;
1621 ccw_device_online(cdev);
1622 while (!dev_fsm_final_state(cdev))
1623 wait_cons_dev();
1624 if (cdev->private->state != DEV_STATE_ONLINE)
1625 goto out_unlock;
1626 rc = 0;
1627 out_unlock:
1628 spin_unlock_irq(cdev->ccwlock);
1629 return 0;
1632 struct ccw_device *
1633 ccw_device_probe_console(void)
1635 struct subchannel *sch;
1636 int ret;
1638 if (xchg(&console_cdev_in_use, 1) != 0)
1639 return ERR_PTR(-EBUSY);
1640 sch = cio_probe_console();
1641 if (IS_ERR(sch)) {
1642 console_cdev_in_use = 0;
1643 return (void *) sch;
1645 memset(&console_cdev, 0, sizeof(struct ccw_device));
1646 memset(&console_private, 0, sizeof(struct ccw_device_private));
1647 console_cdev.private = &console_private;
1648 console_private.cdev = &console_cdev;
1649 ret = ccw_device_console_enable(&console_cdev, sch);
1650 if (ret) {
1651 cio_release_console();
1652 console_cdev_in_use = 0;
1653 return ERR_PTR(ret);
1655 console_cdev.online = 1;
1656 return &console_cdev;
1658 #endif
1661 * get ccw_device matching the busid, but only if owned by cdrv
1663 static int
1664 __ccwdev_check_busid(struct device *dev, void *id)
1666 char *bus_id;
1668 bus_id = id;
1670 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1675 * get_ccwdev_by_busid() - obtain device from a bus id
1676 * @cdrv: driver the device is owned by
1677 * @bus_id: bus id of the device to be searched
1679 * This function searches all devices owned by @cdrv for a device with a bus
1680 * id matching @bus_id.
1681 * Returns:
1682 * If a match is found, its reference count of the found device is increased
1683 * and it is returned; else %NULL is returned.
1685 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1686 const char *bus_id)
1688 struct device *dev;
1689 struct device_driver *drv;
1691 drv = get_driver(&cdrv->driver);
1692 if (!drv)
1693 return NULL;
1695 dev = driver_find_device(drv, NULL, (void *)bus_id,
1696 __ccwdev_check_busid);
1697 put_driver(drv);
1699 return dev ? to_ccwdev(dev) : NULL;
1702 /************************** device driver handling ************************/
1704 /* This is the implementation of the ccw_driver class. The probe, remove
1705 * and release methods are initially very similar to the device_driver
1706 * implementations, with the difference that they have ccw_device
1707 * arguments.
1709 * A ccw driver also contains the information that is needed for
1710 * device matching.
1712 static int
1713 ccw_device_probe (struct device *dev)
1715 struct ccw_device *cdev = to_ccwdev(dev);
1716 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1717 int ret;
1719 cdev->drv = cdrv; /* to let the driver call _set_online */
1721 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1723 if (ret) {
1724 cdev->drv = NULL;
1725 return ret;
1728 return 0;
1731 static int
1732 ccw_device_remove (struct device *dev)
1734 struct ccw_device *cdev = to_ccwdev(dev);
1735 struct ccw_driver *cdrv = cdev->drv;
1736 int ret;
1738 if (cdrv->remove)
1739 cdrv->remove(cdev);
1740 if (cdev->online) {
1741 cdev->online = 0;
1742 spin_lock_irq(cdev->ccwlock);
1743 ret = ccw_device_offline(cdev);
1744 spin_unlock_irq(cdev->ccwlock);
1745 if (ret == 0)
1746 wait_event(cdev->private->wait_q,
1747 dev_fsm_final_state(cdev));
1748 else
1749 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1750 "device 0.%x.%04x\n",
1751 ret, cdev->private->dev_id.ssid,
1752 cdev->private->dev_id.devno);
1754 ccw_device_set_timeout(cdev, 0);
1755 cdev->drv = NULL;
1756 return 0;
1759 static void ccw_device_shutdown(struct device *dev)
1761 struct ccw_device *cdev;
1763 cdev = to_ccwdev(dev);
1764 if (cdev->drv && cdev->drv->shutdown)
1765 cdev->drv->shutdown(cdev);
1766 disable_cmf(cdev);
1769 struct bus_type ccw_bus_type = {
1770 .name = "ccw",
1771 .match = ccw_bus_match,
1772 .uevent = ccw_uevent,
1773 .probe = ccw_device_probe,
1774 .remove = ccw_device_remove,
1775 .shutdown = ccw_device_shutdown,
1779 * ccw_driver_register() - register a ccw driver
1780 * @cdriver: driver to be registered
1782 * This function is mainly a wrapper around driver_register().
1783 * Returns:
1784 * %0 on success and a negative error value on failure.
1786 int ccw_driver_register(struct ccw_driver *cdriver)
1788 struct device_driver *drv = &cdriver->driver;
1790 drv->bus = &ccw_bus_type;
1791 drv->name = cdriver->name;
1792 drv->owner = cdriver->owner;
1794 return driver_register(drv);
1798 * ccw_driver_unregister() - deregister a ccw driver
1799 * @cdriver: driver to be deregistered
1801 * This function is mainly a wrapper around driver_unregister().
1803 void ccw_driver_unregister(struct ccw_driver *cdriver)
1805 driver_unregister(&cdriver->driver);
1808 /* Helper func for qdio. */
1809 struct subchannel_id
1810 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1812 struct subchannel *sch;
1814 sch = to_subchannel(cdev->dev.parent);
1815 return sch->schid;
1818 MODULE_LICENSE("GPL");
1819 EXPORT_SYMBOL(ccw_device_set_online);
1820 EXPORT_SYMBOL(ccw_device_set_offline);
1821 EXPORT_SYMBOL(ccw_driver_register);
1822 EXPORT_SYMBOL(ccw_driver_unregister);
1823 EXPORT_SYMBOL(get_ccwdev_by_busid);
1824 EXPORT_SYMBOL(ccw_bus_type);
1825 EXPORT_SYMBOL(ccw_device_work);
1826 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);