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)
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>
23 #include <asm/param.h> /* HZ */
29 #include "cio_debug.h"
34 #include "blacklist.h"
36 static struct timer_list recovery_timer
;
37 static DEFINE_SPINLOCK(recovery_lock
);
38 static int recovery_phase
;
39 static const unsigned long recovery_delay
[] = { 3, 30, 300 };
41 /******************* bus type handling ***********************/
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. */
48 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
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
;
57 found
= ccw_device_id_match(ids
, &cdev
->id
);
61 cdev
->id
.driver_info
= found
->driver_info
;
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). */
69 static int snprint_alias(char *buf
, size_t size
,
70 struct ccw_device_id
*id
, const char *suffix
)
74 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
80 if (id
->dev_type
!= 0)
81 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
82 id
->dev_model
, suffix
);
84 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
89 /* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
91 static int ccw_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
93 struct ccw_device
*cdev
= to_ccwdev(dev
);
94 struct ccw_device_id
*id
= &(cdev
->id
);
96 char modalias_buf
[30];
99 ret
= add_uevent_var(env
, "CU_TYPE=%04X", id
->cu_type
);
104 ret
= add_uevent_var(env
, "CU_MODEL=%02X", id
->cu_model
);
108 /* The next two can be zero, that's ok for us */
110 ret
= add_uevent_var(env
, "DEV_TYPE=%04X", id
->dev_type
);
115 ret
= add_uevent_var(env
, "DEV_MODEL=%02X", id
->dev_model
);
120 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
121 ret
= add_uevent_var(env
, "MODALIAS=%s", modalias_buf
);
125 struct bus_type ccw_bus_type
;
127 static void io_subchannel_irq(struct subchannel
*);
128 static int io_subchannel_probe(struct subchannel
*);
129 static int io_subchannel_remove(struct subchannel
*);
130 static void io_subchannel_shutdown(struct subchannel
*);
131 static int io_subchannel_sch_event(struct subchannel
*, int);
132 static int io_subchannel_chp_event(struct subchannel
*, struct chp_link
*,
135 static struct css_device_id io_subchannel_ids
[] = {
136 { .match_flags
= 0x1, .type
= SUBCHANNEL_TYPE_IO
, },
137 { /* end of list */ },
139 MODULE_DEVICE_TABLE(css
, io_subchannel_ids
);
141 static struct css_driver io_subchannel_driver
= {
142 .owner
= THIS_MODULE
,
143 .subchannel_type
= io_subchannel_ids
,
144 .name
= "io_subchannel",
145 .irq
= io_subchannel_irq
,
146 .sch_event
= io_subchannel_sch_event
,
147 .chp_event
= io_subchannel_chp_event
,
148 .probe
= io_subchannel_probe
,
149 .remove
= io_subchannel_remove
,
150 .shutdown
= io_subchannel_shutdown
,
153 struct workqueue_struct
*ccw_device_work
;
154 wait_queue_head_t ccw_device_init_wq
;
155 atomic_t ccw_device_init_count
;
157 static void recovery_func(unsigned long data
);
160 init_ccw_bus_type (void)
164 init_waitqueue_head(&ccw_device_init_wq
);
165 atomic_set(&ccw_device_init_count
, 0);
166 setup_timer(&recovery_timer
, recovery_func
, 0);
168 ccw_device_work
= create_singlethread_workqueue("cio");
169 if (!ccw_device_work
)
170 return -ENOMEM
; /* FIXME: better errno ? */
171 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
173 ret
= -ENOMEM
; /* FIXME: better errno ? */
176 if ((ret
= bus_register (&ccw_bus_type
)))
179 ret
= css_driver_register(&io_subchannel_driver
);
183 wait_event(ccw_device_init_wq
,
184 atomic_read(&ccw_device_init_count
) == 0);
185 flush_workqueue(ccw_device_work
);
189 destroy_workqueue(ccw_device_work
);
191 destroy_workqueue(slow_path_wq
);
196 cleanup_ccw_bus_type (void)
198 css_driver_unregister(&io_subchannel_driver
);
199 bus_unregister(&ccw_bus_type
);
200 destroy_workqueue(ccw_device_work
);
203 subsys_initcall(init_ccw_bus_type
);
204 module_exit(cleanup_ccw_bus_type
);
206 /************************ device handling **************************/
209 * A ccw_device has some interfaces in sysfs in addition to the
211 * The following entries are designed to export the information which
212 * resided in 2.4 in /proc/subchannels. Subchannel and device number
213 * are obvious, so they don't have an entry :)
214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
217 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
219 struct subchannel
*sch
= to_subchannel(dev
);
220 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
225 for (chp
= 0; chp
< 8; chp
++) {
227 if (ssd
->path_mask
& mask
)
228 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
230 ret
+= sprintf(buf
+ ret
, "00 ");
232 ret
+= sprintf (buf
+ret
, "\n");
233 return min((ssize_t
)PAGE_SIZE
, ret
);
237 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
239 struct subchannel
*sch
= to_subchannel(dev
);
240 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
242 return sprintf (buf
, "%02x %02x %02x\n",
243 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
247 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
249 struct ccw_device
*cdev
= to_ccwdev(dev
);
250 struct ccw_device_id
*id
= &(cdev
->id
);
252 if (id
->dev_type
!= 0)
253 return sprintf(buf
, "%04x/%02x\n",
254 id
->dev_type
, id
->dev_model
);
256 return sprintf(buf
, "n/a\n");
260 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
262 struct ccw_device
*cdev
= to_ccwdev(dev
);
263 struct ccw_device_id
*id
= &(cdev
->id
);
265 return sprintf(buf
, "%04x/%02x\n",
266 id
->cu_type
, id
->cu_model
);
270 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
272 struct ccw_device
*cdev
= to_ccwdev(dev
);
273 struct ccw_device_id
*id
= &(cdev
->id
);
276 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
278 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
282 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
284 struct ccw_device
*cdev
= to_ccwdev(dev
);
286 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
289 int ccw_device_is_orphan(struct ccw_device
*cdev
)
291 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
294 static void ccw_device_unregister(struct ccw_device
*cdev
)
296 if (test_and_clear_bit(1, &cdev
->private->registered
))
297 device_del(&cdev
->dev
);
300 static void ccw_device_remove_orphan_cb(struct work_struct
*work
)
302 struct ccw_device_private
*priv
;
303 struct ccw_device
*cdev
;
305 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
307 ccw_device_unregister(cdev
);
308 put_device(&cdev
->dev
);
309 /* Release cdev reference for workqueue processing. */
310 put_device(&cdev
->dev
);
313 static void ccw_device_call_sch_unregister(struct work_struct
*work
);
316 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
321 * Forced offline in disconnected state means
322 * 'throw away device'.
324 /* Get cdev reference for workqueue processing. */
325 if (!get_device(&cdev
->dev
))
327 if (ccw_device_is_orphan(cdev
)) {
329 * Deregister ccw device.
330 * Unfortunately, we cannot do this directly from the
333 spin_lock_irqsave(cdev
->ccwlock
, flags
);
334 cdev
->private->state
= DEV_STATE_NOT_OPER
;
335 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
336 PREPARE_WORK(&cdev
->private->kick_work
,
337 ccw_device_remove_orphan_cb
);
339 /* Deregister subchannel, which will kill the ccw device. */
340 PREPARE_WORK(&cdev
->private->kick_work
,
341 ccw_device_call_sch_unregister
);
342 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
346 * ccw_device_set_offline() - disable a ccw device for I/O
347 * @cdev: target ccw device
349 * This function calls the driver's set_offline() function for @cdev, if
350 * given, and then disables @cdev.
352 * %0 on success and a negative error value on failure.
354 * enabled, ccw device lock not held
356 int ccw_device_set_offline(struct ccw_device
*cdev
)
362 if (!cdev
->online
|| !cdev
->drv
)
365 if (cdev
->drv
->set_offline
) {
366 ret
= cdev
->drv
->set_offline(cdev
);
371 spin_lock_irq(cdev
->ccwlock
);
372 ret
= ccw_device_offline(cdev
);
373 if (ret
== -ENODEV
) {
374 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
375 cdev
->private->state
= DEV_STATE_OFFLINE
;
376 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
378 spin_unlock_irq(cdev
->ccwlock
);
379 /* Give up reference from ccw_device_set_online(). */
380 put_device(&cdev
->dev
);
383 spin_unlock_irq(cdev
->ccwlock
);
385 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
386 /* Give up reference from ccw_device_set_online(). */
387 put_device(&cdev
->dev
);
389 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
390 "device 0.%x.%04x\n",
391 ret
, cdev
->private->dev_id
.ssid
,
392 cdev
->private->dev_id
.devno
);
399 * ccw_device_set_online() - enable a ccw device for I/O
400 * @cdev: target ccw device
402 * This function first enables @cdev and then calls the driver's set_online()
403 * function for @cdev, if given. If set_online() returns an error, @cdev is
406 * %0 on success and a negative error value on failure.
408 * enabled, ccw device lock not held
410 int ccw_device_set_online(struct ccw_device
*cdev
)
416 if (cdev
->online
|| !cdev
->drv
)
418 /* Hold on to an extra reference while device is online. */
419 if (!get_device(&cdev
->dev
))
422 spin_lock_irq(cdev
->ccwlock
);
423 ret
= ccw_device_online(cdev
);
424 spin_unlock_irq(cdev
->ccwlock
);
426 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
428 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
429 "device 0.%x.%04x\n",
430 ret
, cdev
->private->dev_id
.ssid
,
431 cdev
->private->dev_id
.devno
);
432 /* Give up online reference since onlining failed. */
433 put_device(&cdev
->dev
);
436 if (cdev
->private->state
!= DEV_STATE_ONLINE
) {
437 /* Give up online reference since onlining failed. */
438 put_device(&cdev
->dev
);
441 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
445 spin_lock_irq(cdev
->ccwlock
);
446 ret
= ccw_device_offline(cdev
);
447 spin_unlock_irq(cdev
->ccwlock
);
449 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
451 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
452 "device 0.%x.%04x\n",
453 ret
, cdev
->private->dev_id
.ssid
,
454 cdev
->private->dev_id
.devno
);
455 /* Give up online reference since onlining failed. */
456 put_device(&cdev
->dev
);
457 return (ret
== 0) ? -ENODEV
: ret
;
460 static void online_store_handle_offline(struct ccw_device
*cdev
)
462 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
463 ccw_device_remove_disconnected(cdev
);
464 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
465 ccw_device_set_offline(cdev
);
468 static int online_store_recog_and_online(struct ccw_device
*cdev
)
472 /* Do device recognition, if needed. */
473 if (cdev
->id
.cu_type
== 0) {
474 ret
= ccw_device_recognition(cdev
);
476 CIO_MSG_EVENT(0, "Couldn't start recognition "
477 "for device 0.%x.%04x (ret=%d)\n",
478 cdev
->private->dev_id
.ssid
,
479 cdev
->private->dev_id
.devno
, ret
);
482 wait_event(cdev
->private->wait_q
,
483 cdev
->private->flags
.recog_done
);
485 if (cdev
->drv
&& cdev
->drv
->set_online
)
486 ccw_device_set_online(cdev
);
489 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
493 ret
= online_store_recog_and_online(cdev
);
496 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
497 ret
= ccw_device_stlck(cdev
);
500 if (cdev
->id
.cu_type
== 0)
501 cdev
->private->state
= DEV_STATE_NOT_OPER
;
502 online_store_recog_and_online(cdev
);
507 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
508 const char *buf
, size_t count
)
510 struct ccw_device
*cdev
= to_ccwdev(dev
);
514 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
517 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
518 atomic_set(&cdev
->private->onoff
, 0);
521 if (!strncmp(buf
, "force\n", count
)) {
527 ret
= strict_strtoul(buf
, 16, &i
);
533 online_store_handle_offline(cdev
);
537 ret
= online_store_handle_online(cdev
, force
);
546 module_put(cdev
->drv
->owner
);
547 atomic_set(&cdev
->private->onoff
, 0);
552 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
554 struct ccw_device
*cdev
= to_ccwdev(dev
);
555 struct subchannel
*sch
;
557 if (ccw_device_is_orphan(cdev
))
558 return sprintf(buf
, "no device\n");
559 switch (cdev
->private->state
) {
560 case DEV_STATE_BOXED
:
561 return sprintf(buf
, "boxed\n");
562 case DEV_STATE_DISCONNECTED
:
563 case DEV_STATE_DISCONNECTED_SENSE_ID
:
564 case DEV_STATE_NOT_OPER
:
565 sch
= to_subchannel(dev
->parent
);
567 return sprintf(buf
, "no path\n");
569 return sprintf(buf
, "no device\n");
571 /* All other states considered fine. */
572 return sprintf(buf
, "good\n");
576 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
577 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
578 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
579 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
580 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
581 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
582 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
584 static struct attribute
*io_subchannel_attrs
[] = {
585 &dev_attr_chpids
.attr
,
586 &dev_attr_pimpampom
.attr
,
590 static struct attribute_group io_subchannel_attr_group
= {
591 .attrs
= io_subchannel_attrs
,
594 static struct attribute
* ccwdev_attrs
[] = {
595 &dev_attr_devtype
.attr
,
596 &dev_attr_cutype
.attr
,
597 &dev_attr_modalias
.attr
,
598 &dev_attr_online
.attr
,
599 &dev_attr_cmb_enable
.attr
,
600 &dev_attr_availability
.attr
,
604 static struct attribute_group ccwdev_attr_group
= {
605 .attrs
= ccwdev_attrs
,
608 static struct attribute_group
*ccwdev_attr_groups
[] = {
613 /* this is a simple abstraction for device_register that sets the
614 * correct bus type and adds the bus specific files */
615 static int ccw_device_register(struct ccw_device
*cdev
)
617 struct device
*dev
= &cdev
->dev
;
620 dev
->bus
= &ccw_bus_type
;
622 if ((ret
= device_add(dev
)))
625 set_bit(1, &cdev
->private->registered
);
630 struct ccw_dev_id dev_id
;
631 struct ccw_device
* sibling
;
635 match_devno(struct device
* dev
, void * data
)
637 struct match_data
* d
= data
;
638 struct ccw_device
* cdev
;
640 cdev
= to_ccwdev(dev
);
641 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
642 !ccw_device_is_orphan(cdev
) &&
643 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
644 (cdev
!= d
->sibling
))
649 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
650 struct ccw_device
*sibling
)
653 struct match_data data
;
655 data
.dev_id
= *dev_id
;
656 data
.sibling
= sibling
;
657 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
659 return dev
? to_ccwdev(dev
) : NULL
;
662 static int match_orphan(struct device
*dev
, void *data
)
664 struct ccw_dev_id
*dev_id
;
665 struct ccw_device
*cdev
;
668 cdev
= to_ccwdev(dev
);
669 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
672 static struct ccw_device
*
673 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
674 struct ccw_dev_id
*dev_id
)
678 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
681 return dev
? to_ccwdev(dev
) : NULL
;
685 ccw_device_add_changed(struct work_struct
*work
)
687 struct ccw_device_private
*priv
;
688 struct ccw_device
*cdev
;
690 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
692 if (device_add(&cdev
->dev
)) {
693 put_device(&cdev
->dev
);
696 set_bit(1, &cdev
->private->registered
);
699 void ccw_device_do_unreg_rereg(struct work_struct
*work
)
701 struct ccw_device_private
*priv
;
702 struct ccw_device
*cdev
;
703 struct subchannel
*sch
;
705 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
707 sch
= to_subchannel(cdev
->dev
.parent
);
709 ccw_device_unregister(cdev
);
710 PREPARE_WORK(&cdev
->private->kick_work
,
711 ccw_device_add_changed
);
712 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
716 ccw_device_release(struct device
*dev
)
718 struct ccw_device
*cdev
;
720 cdev
= to_ccwdev(dev
);
721 /* Release reference of parent subchannel. */
722 put_device(cdev
->dev
.parent
);
723 kfree(cdev
->private);
727 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
729 struct ccw_device
*cdev
;
731 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
733 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
734 GFP_KERNEL
| GFP_DMA
);
739 return ERR_PTR(-ENOMEM
);
742 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
743 struct ccw_device
*cdev
)
745 cdev
->private->cdev
= cdev
;
746 atomic_set(&cdev
->private->onoff
, 0);
747 cdev
->dev
.parent
= &sch
->dev
;
748 cdev
->dev
.release
= ccw_device_release
;
749 INIT_WORK(&cdev
->private->kick_work
, NULL
);
750 cdev
->dev
.groups
= ccwdev_attr_groups
;
751 /* Do first half of device_register. */
752 device_initialize(&cdev
->dev
);
753 if (!get_device(&sch
->dev
)) {
754 /* Release reference from device_initialize(). */
755 put_device(&cdev
->dev
);
761 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
763 struct ccw_device
*cdev
;
766 cdev
= io_subchannel_allocate_dev(sch
);
768 ret
= io_subchannel_initialize_dev(sch
, cdev
);
777 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
779 static void sch_attach_device(struct subchannel
*sch
,
780 struct ccw_device
*cdev
)
782 css_update_ssd_info(sch
);
783 spin_lock_irq(sch
->lock
);
784 sch_set_cdev(sch
, cdev
);
785 cdev
->private->schid
= sch
->schid
;
786 cdev
->ccwlock
= sch
->lock
;
787 ccw_device_trigger_reprobe(cdev
);
788 spin_unlock_irq(sch
->lock
);
791 static void sch_attach_disconnected_device(struct subchannel
*sch
,
792 struct ccw_device
*cdev
)
794 struct subchannel
*other_sch
;
797 /* Get reference for new parent. */
798 if (!get_device(&sch
->dev
))
800 other_sch
= to_subchannel(cdev
->dev
.parent
);
801 /* Note: device_move() changes cdev->dev.parent */
802 ret
= device_move(&cdev
->dev
, &sch
->dev
);
804 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
805 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
806 cdev
->private->dev_id
.devno
, ret
);
807 /* Put reference for new parent. */
808 put_device(&sch
->dev
);
811 sch_set_cdev(other_sch
, NULL
);
812 /* No need to keep a subchannel without ccw device around. */
813 css_sch_device_unregister(other_sch
);
814 sch_attach_device(sch
, cdev
);
815 /* Put reference for old parent. */
816 put_device(&other_sch
->dev
);
819 static void sch_attach_orphaned_device(struct subchannel
*sch
,
820 struct ccw_device
*cdev
)
823 struct subchannel
*pseudo_sch
;
825 /* Get reference for new parent. */
826 if (!get_device(&sch
->dev
))
828 pseudo_sch
= to_subchannel(cdev
->dev
.parent
);
830 * Try to move the ccw device to its new subchannel.
831 * Note: device_move() changes cdev->dev.parent
833 ret
= device_move(&cdev
->dev
, &sch
->dev
);
835 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
836 "failed (ret=%d)!\n",
837 cdev
->private->dev_id
.ssid
,
838 cdev
->private->dev_id
.devno
, ret
);
839 /* Put reference for new parent. */
840 put_device(&sch
->dev
);
843 sch_attach_device(sch
, cdev
);
844 /* Put reference on pseudo subchannel. */
845 put_device(&pseudo_sch
->dev
);
848 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
850 struct ccw_device
*cdev
;
852 /* Need to allocate a new ccw device. */
853 cdev
= io_subchannel_create_ccwdev(sch
);
855 /* OK, we did everything we could... */
856 css_sch_device_unregister(sch
);
859 spin_lock_irq(sch
->lock
);
860 sch_set_cdev(sch
, cdev
);
861 spin_unlock_irq(sch
->lock
);
862 /* Start recognition for the new ccw device. */
863 if (io_subchannel_recog(cdev
, sch
)) {
864 spin_lock_irq(sch
->lock
);
865 sch_set_cdev(sch
, NULL
);
866 spin_unlock_irq(sch
->lock
);
867 css_sch_device_unregister(sch
);
868 /* Put reference from io_subchannel_create_ccwdev(). */
869 put_device(&sch
->dev
);
870 /* Give up initial reference. */
871 put_device(&cdev
->dev
);
876 void ccw_device_move_to_orphanage(struct work_struct
*work
)
878 struct ccw_device_private
*priv
;
879 struct ccw_device
*cdev
;
880 struct ccw_device
*replacing_cdev
;
881 struct subchannel
*sch
;
883 struct channel_subsystem
*css
;
884 struct ccw_dev_id dev_id
;
886 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
888 sch
= to_subchannel(cdev
->dev
.parent
);
889 css
= to_css(sch
->dev
.parent
);
890 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
891 dev_id
.ssid
= sch
->schid
.ssid
;
893 /* Increase refcount for pseudo subchannel. */
894 get_device(&css
->pseudo_subchannel
->dev
);
896 * Move the orphaned ccw device to the orphanage so the replacing
897 * ccw device can take its place on the subchannel.
898 * Note: device_move() changes cdev->dev.parent
900 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
);
902 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
903 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
904 cdev
->private->dev_id
.devno
, ret
);
905 /* Decrease refcount for pseudo subchannel again. */
906 put_device(&css
->pseudo_subchannel
->dev
);
909 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
911 * Search for the replacing ccw device
912 * - among the disconnected devices
915 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
916 if (replacing_cdev
) {
917 sch_attach_disconnected_device(sch
, replacing_cdev
);
918 /* Release reference from get_disc_ccwdev_by_dev_id() */
919 put_device(&replacing_cdev
->dev
);
920 /* Release reference of subchannel from old cdev. */
921 put_device(&sch
->dev
);
924 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
925 if (replacing_cdev
) {
926 sch_attach_orphaned_device(sch
, replacing_cdev
);
927 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
928 put_device(&replacing_cdev
->dev
);
929 /* Release reference of subchannel from old cdev. */
930 put_device(&sch
->dev
);
933 sch_create_and_recog_new_device(sch
);
934 /* Release reference of subchannel from old cdev. */
935 put_device(&sch
->dev
);
939 * Register recognized device.
942 io_subchannel_register(struct work_struct
*work
)
944 struct ccw_device_private
*priv
;
945 struct ccw_device
*cdev
;
946 struct subchannel
*sch
;
950 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
952 sch
= to_subchannel(cdev
->dev
.parent
);
954 * Check if subchannel is still registered. It may have become
955 * unregistered if a machine check hit us after finishing
956 * device recognition but before the register work could be
959 if (!device_is_registered(&sch
->dev
))
961 css_update_ssd_info(sch
);
963 * io_subchannel_register() will also be called after device
964 * recognition has been done for a boxed device (which will already
965 * be registered). We need to reprobe since we may now have sense id
968 if (device_is_registered(&cdev
->dev
)) {
970 ret
= device_reprobe(&cdev
->dev
);
972 /* We can't do much here. */
973 CIO_MSG_EVENT(0, "device_reprobe() returned"
974 " %d for 0.%x.%04x\n", ret
,
975 cdev
->private->dev_id
.ssid
,
976 cdev
->private->dev_id
.devno
);
981 * Now we know this subchannel will stay, we can throw
982 * our delayed uevent.
984 sch
->dev
.uevent_suppress
= 0;
985 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
986 /* make it known to the system */
987 ret
= ccw_device_register(cdev
);
989 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
990 cdev
->private->dev_id
.ssid
,
991 cdev
->private->dev_id
.devno
, ret
);
992 spin_lock_irqsave(sch
->lock
, flags
);
993 sch_set_cdev(sch
, NULL
);
994 spin_unlock_irqrestore(sch
->lock
, flags
);
995 /* Release initial device reference. */
996 put_device(&cdev
->dev
);
1000 cdev
->private->flags
.recog_done
= 1;
1001 wake_up(&cdev
->private->wait_q
);
1003 /* Release reference for workqueue processing. */
1004 put_device(&cdev
->dev
);
1005 if (atomic_dec_and_test(&ccw_device_init_count
))
1006 wake_up(&ccw_device_init_wq
);
1009 static void ccw_device_call_sch_unregister(struct work_struct
*work
)
1011 struct ccw_device_private
*priv
;
1012 struct ccw_device
*cdev
;
1013 struct subchannel
*sch
;
1015 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1017 /* Get subchannel reference for local processing. */
1018 if (!get_device(cdev
->dev
.parent
))
1020 sch
= to_subchannel(cdev
->dev
.parent
);
1021 css_sch_device_unregister(sch
);
1022 /* Reset intparm to zeroes. */
1023 sch
->schib
.pmcw
.intparm
= 0;
1025 /* Release cdev reference for workqueue processing.*/
1026 put_device(&cdev
->dev
);
1027 /* Release subchannel reference for local processing. */
1028 put_device(&sch
->dev
);
1032 * subchannel recognition done. Called from the state machine.
1035 io_subchannel_recog_done(struct ccw_device
*cdev
)
1037 struct subchannel
*sch
;
1039 if (css_init_done
== 0) {
1040 cdev
->private->flags
.recog_done
= 1;
1043 switch (cdev
->private->state
) {
1044 case DEV_STATE_NOT_OPER
:
1045 cdev
->private->flags
.recog_done
= 1;
1046 /* Remove device found not operational. */
1047 if (!get_device(&cdev
->dev
))
1049 sch
= to_subchannel(cdev
->dev
.parent
);
1050 PREPARE_WORK(&cdev
->private->kick_work
,
1051 ccw_device_call_sch_unregister
);
1052 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1053 if (atomic_dec_and_test(&ccw_device_init_count
))
1054 wake_up(&ccw_device_init_wq
);
1056 case DEV_STATE_BOXED
:
1057 /* Device did not respond in time. */
1058 case DEV_STATE_OFFLINE
:
1060 * We can't register the device in interrupt context so
1061 * we schedule a work item.
1063 if (!get_device(&cdev
->dev
))
1065 PREPARE_WORK(&cdev
->private->kick_work
,
1066 io_subchannel_register
);
1067 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1073 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
1076 struct ccw_device_private
*priv
;
1078 sch_set_cdev(sch
, cdev
);
1079 cdev
->ccwlock
= sch
->lock
;
1081 /* Init private data. */
1082 priv
= cdev
->private;
1083 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1084 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
1085 priv
->schid
= sch
->schid
;
1086 priv
->state
= DEV_STATE_NOT_OPER
;
1087 INIT_LIST_HEAD(&priv
->cmb_list
);
1088 init_waitqueue_head(&priv
->wait_q
);
1089 init_timer(&priv
->timer
);
1091 /* Set an initial name for the device. */
1092 if (cio_is_console(sch
->schid
))
1093 cdev
->dev
.init_name
= cio_get_console_cdev_name(sch
);
1095 dev_set_name(&cdev
->dev
, "0.%x.%04x",
1096 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1098 /* Increase counter of devices currently in recognition. */
1099 atomic_inc(&ccw_device_init_count
);
1101 /* Start async. device sensing. */
1102 spin_lock_irq(sch
->lock
);
1103 rc
= ccw_device_recognition(cdev
);
1104 spin_unlock_irq(sch
->lock
);
1106 if (atomic_dec_and_test(&ccw_device_init_count
))
1107 wake_up(&ccw_device_init_wq
);
1112 static void ccw_device_move_to_sch(struct work_struct
*work
)
1114 struct ccw_device_private
*priv
;
1116 struct subchannel
*sch
;
1117 struct ccw_device
*cdev
;
1118 struct subchannel
*former_parent
;
1120 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1123 former_parent
= to_subchannel(cdev
->dev
.parent
);
1124 /* Get reference for new parent. */
1125 if (!get_device(&sch
->dev
))
1127 mutex_lock(&sch
->reg_mutex
);
1129 * Try to move the ccw device to its new subchannel.
1130 * Note: device_move() changes cdev->dev.parent
1132 rc
= device_move(&cdev
->dev
, &sch
->dev
);
1133 mutex_unlock(&sch
->reg_mutex
);
1135 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1136 "0.%x.%04x failed (ret=%d)!\n",
1137 cdev
->private->dev_id
.ssid
,
1138 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1139 sch
->schid
.sch_no
, rc
);
1140 css_sch_device_unregister(sch
);
1141 /* Put reference for new parent again. */
1142 put_device(&sch
->dev
);
1145 if (!sch_is_pseudo_sch(former_parent
)) {
1146 spin_lock_irq(former_parent
->lock
);
1147 sch_set_cdev(former_parent
, NULL
);
1148 spin_unlock_irq(former_parent
->lock
);
1149 css_sch_device_unregister(former_parent
);
1150 /* Reset intparm to zeroes. */
1151 former_parent
->schib
.pmcw
.intparm
= 0;
1152 cio_modify(former_parent
);
1154 sch_attach_device(sch
, cdev
);
1156 /* Put reference for old parent. */
1157 put_device(&former_parent
->dev
);
1158 put_device(&cdev
->dev
);
1161 static void io_subchannel_irq(struct subchannel
*sch
)
1163 struct ccw_device
*cdev
;
1165 cdev
= sch_get_cdev(sch
);
1167 CIO_TRACE_EVENT(3, "IRQ");
1168 CIO_TRACE_EVENT(3, dev_name(&sch
->dev
));
1170 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1173 static void io_subchannel_init_fields(struct subchannel
*sch
)
1175 if (cio_is_console(sch
->schid
))
1178 sch
->opm
= chp_get_sch_opm(sch
);
1179 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1180 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1182 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1183 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1184 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1185 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1186 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1187 /* Initially set up some fields in the pmcw. */
1188 sch
->schib
.pmcw
.ena
= 0;
1189 sch
->schib
.pmcw
.csense
= 1; /* concurrent sense */
1190 if ((sch
->lpm
& (sch
->lpm
- 1)) != 0)
1191 sch
->schib
.pmcw
.mp
= 1; /* multipath mode */
1192 /* clean up possible residual cmf stuff */
1193 sch
->schib
.pmcw
.mme
= 0;
1194 sch
->schib
.pmcw
.mbfc
= 0;
1195 sch
->schib
.pmcw
.mbi
= 0;
1199 static void io_subchannel_do_unreg(struct work_struct
*work
)
1201 struct subchannel
*sch
;
1203 sch
= container_of(work
, struct subchannel
, work
);
1204 css_sch_device_unregister(sch
);
1205 /* Reset intparm to zeroes. */
1206 sch
->schib
.pmcw
.intparm
= 0;
1208 put_device(&sch
->dev
);
1211 /* Schedule unregister if we have no cdev. */
1212 static void io_subchannel_schedule_removal(struct subchannel
*sch
)
1214 get_device(&sch
->dev
);
1215 INIT_WORK(&sch
->work
, io_subchannel_do_unreg
);
1216 queue_work(slow_path_wq
, &sch
->work
);
1220 * Note: We always return 0 so that we bind to the device even on error.
1221 * This is needed so that our remove function is called on unregister.
1223 static int io_subchannel_probe(struct subchannel
*sch
)
1225 struct ccw_device
*cdev
;
1227 unsigned long flags
;
1228 struct ccw_dev_id dev_id
;
1230 cdev
= sch_get_cdev(sch
);
1232 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1233 &io_subchannel_attr_group
);
1235 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1236 "attributes for subchannel "
1237 "0.%x.%04x (rc=%d)\n",
1238 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1240 * This subchannel already has an associated ccw_device.
1241 * Throw the delayed uevent for the subchannel, register
1242 * the ccw_device and exit. This happens for all early
1243 * devices, e.g. the console.
1245 sch
->dev
.uevent_suppress
= 0;
1246 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1247 cdev
->dev
.groups
= ccwdev_attr_groups
;
1248 device_initialize(&cdev
->dev
);
1249 ccw_device_register(cdev
);
1251 * Check if the device is already online. If it is
1252 * the reference count needs to be corrected since we
1253 * didn't obtain a reference in ccw_device_set_online.
1255 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1256 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1257 cdev
->private->state
!= DEV_STATE_BOXED
)
1258 get_device(&cdev
->dev
);
1261 io_subchannel_init_fields(sch
);
1262 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1263 &io_subchannel_attr_group
);
1266 /* Allocate I/O subchannel private data. */
1267 sch
->private = kzalloc(sizeof(struct io_subchannel_private
),
1268 GFP_KERNEL
| GFP_DMA
);
1272 * First check if a fitting device may be found amongst the
1273 * disconnected devices or in the orphanage.
1275 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1276 dev_id
.ssid
= sch
->schid
.ssid
;
1277 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1279 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1283 * Schedule moving the device until when we have a registered
1284 * subchannel to move to and succeed the probe. We can
1285 * unregister later again, when the probe is through.
1287 cdev
->private->sch
= sch
;
1288 PREPARE_WORK(&cdev
->private->kick_work
,
1289 ccw_device_move_to_sch
);
1290 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1293 cdev
= io_subchannel_create_ccwdev(sch
);
1296 rc
= io_subchannel_recog(cdev
, sch
);
1298 spin_lock_irqsave(sch
->lock
, flags
);
1299 io_subchannel_recog_done(cdev
);
1300 spin_unlock_irqrestore(sch
->lock
, flags
);
1304 kfree(sch
->private);
1305 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1307 io_subchannel_schedule_removal(sch
);
1312 io_subchannel_remove (struct subchannel
*sch
)
1314 struct ccw_device
*cdev
;
1315 unsigned long flags
;
1317 cdev
= sch_get_cdev(sch
);
1320 /* Set ccw device to not operational and drop reference. */
1321 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1322 sch_set_cdev(sch
, NULL
);
1323 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1324 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1325 ccw_device_unregister(cdev
);
1326 put_device(&cdev
->dev
);
1327 kfree(sch
->private);
1328 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1332 static int io_subchannel_notify(struct subchannel
*sch
, int event
)
1334 struct ccw_device
*cdev
;
1336 cdev
= sch_get_cdev(sch
);
1339 return ccw_device_notify(cdev
, event
);
1342 static void io_subchannel_verify(struct subchannel
*sch
)
1344 struct ccw_device
*cdev
;
1346 cdev
= sch_get_cdev(sch
);
1348 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1351 static int check_for_io_on_path(struct subchannel
*sch
, int mask
)
1355 cc
= stsch(sch
->schid
, &sch
->schib
);
1358 if (scsw_actl(&sch
->schib
.scsw
) && sch
->schib
.pmcw
.lpum
== mask
)
1363 static void terminate_internal_io(struct subchannel
*sch
,
1364 struct ccw_device
*cdev
)
1366 if (cio_clear(sch
)) {
1367 /* Recheck device in case clear failed. */
1370 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1372 css_schedule_eval(sch
->schid
);
1375 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1376 /* Request retry of internal operation. */
1377 cdev
->private->flags
.intretry
= 1;
1380 cdev
->handler(cdev
, cdev
->private->intparm
,
1384 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1386 struct ccw_device
*cdev
;
1388 cdev
= sch_get_cdev(sch
);
1391 if (check_for_io_on_path(sch
, mask
)) {
1392 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1393 ccw_device_kill_io(cdev
);
1395 terminate_internal_io(sch
, cdev
);
1396 /* Re-start path verification. */
1397 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1400 /* trigger path verification. */
1401 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1405 static int io_subchannel_chp_event(struct subchannel
*sch
,
1406 struct chp_link
*link
, int event
)
1410 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1417 io_subchannel_terminate_path(sch
, mask
);
1422 io_subchannel_verify(sch
);
1425 if (stsch(sch
->schid
, &sch
->schib
))
1427 if (!css_sch_is_valid(&sch
->schib
))
1429 io_subchannel_terminate_path(sch
, mask
);
1432 if (stsch(sch
->schid
, &sch
->schib
))
1434 sch
->lpm
|= mask
& sch
->opm
;
1435 io_subchannel_verify(sch
);
1442 io_subchannel_shutdown(struct subchannel
*sch
)
1444 struct ccw_device
*cdev
;
1447 cdev
= sch_get_cdev(sch
);
1449 if (cio_is_console(sch
->schid
))
1451 if (!sch
->schib
.pmcw
.ena
)
1452 /* Nothing to do. */
1454 ret
= cio_disable_subchannel(sch
);
1456 /* Subchannel is disabled, we're done. */
1458 cdev
->private->state
= DEV_STATE_QUIESCE
;
1460 cdev
->handler(cdev
, cdev
->private->intparm
,
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
));
1467 cio_disable_subchannel(sch
);
1470 static int io_subchannel_get_status(struct subchannel
*sch
)
1474 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
1476 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
1477 return CIO_REVALIDATE
;
1483 static int device_is_disconnected(struct ccw_device
*cdev
)
1487 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1488 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1491 static int recovery_check(struct device
*dev
, void *data
)
1493 struct ccw_device
*cdev
= to_ccwdev(dev
);
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
);
1505 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1509 spin_unlock_irq(cdev
->ccwlock
);
1514 static void recovery_work_func(struct work_struct
*unused
)
1518 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1520 spin_lock_irq(&recovery_lock
);
1521 if (!timer_pending(&recovery_timer
)) {
1522 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1524 mod_timer(&recovery_timer
, jiffies
+
1525 recovery_delay
[recovery_phase
] * HZ
);
1527 spin_unlock_irq(&recovery_lock
);
1529 CIO_MSG_EVENT(4, "recovery: end\n");
1532 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1534 static void recovery_func(unsigned long data
)
1537 * We can't do our recovery in softirq context and it's not
1538 * performance critical, so we schedule it.
1540 schedule_work(&recovery_work
);
1543 static void ccw_device_schedule_recovery(void)
1545 unsigned long flags
;
1547 CIO_MSG_EVENT(4, "recovery: schedule\n");
1548 spin_lock_irqsave(&recovery_lock
, flags
);
1549 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1551 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1553 spin_unlock_irqrestore(&recovery_lock
, flags
);
1556 static int purge_fn(struct device
*dev
, void *data
)
1558 struct ccw_device
*cdev
= to_ccwdev(dev
);
1559 struct ccw_device_private
*priv
= cdev
->private;
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
);
1568 if (!get_device(&cdev
->dev
))
1570 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv
->dev_id
.ssid
,
1571 priv
->dev_id
.devno
);
1572 PREPARE_WORK(&cdev
->private->kick_work
, ccw_device_call_sch_unregister
);
1573 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1576 /* Abort loop in case of pending signal. */
1577 if (signal_pending(current
))
1584 * ccw_purge_blacklisted - purge unused, blacklisted devices
1586 * Unregister all ccw devices that are offline and on the blacklist.
1588 int ccw_purge_blacklisted(void)
1590 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1591 bus_for_each_dev(&ccw_bus_type
, NULL
, NULL
, purge_fn
);
1595 static void device_set_disconnected(struct ccw_device
*cdev
)
1599 ccw_device_set_timeout(cdev
, 0);
1600 cdev
->private->flags
.fake_irb
= 0;
1601 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1603 ccw_device_schedule_recovery();
1606 void ccw_device_set_notoper(struct ccw_device
*cdev
)
1608 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1610 CIO_TRACE_EVENT(2, "notoper");
1611 CIO_TRACE_EVENT(2, dev_name(&sch
->dev
));
1612 ccw_device_set_timeout(cdev
, 0);
1613 cio_disable_subchannel(sch
);
1614 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1617 static int io_subchannel_sch_event(struct subchannel
*sch
, int slow
)
1619 int event
, ret
, disc
;
1620 unsigned long flags
;
1621 enum { NONE
, UNREGISTER
, UNREGISTER_PROBE
, REPROBE
, DISC
} action
;
1622 struct ccw_device
*cdev
;
1624 spin_lock_irqsave(sch
->lock
, flags
);
1625 cdev
= sch_get_cdev(sch
);
1626 disc
= device_is_disconnected(cdev
);
1628 /* Disconnected devices are evaluated directly only.*/
1629 spin_unlock_irqrestore(sch
->lock
, flags
);
1632 /* No interrupt after machine check - kill pending timers. */
1634 ccw_device_set_timeout(cdev
, 0);
1635 if (!disc
&& !slow
) {
1636 /* Non-disconnected devices are evaluated on the slow path. */
1637 spin_unlock_irqrestore(sch
->lock
, flags
);
1640 event
= io_subchannel_get_status(sch
);
1641 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1642 sch
->schid
.ssid
, sch
->schid
.sch_no
, event
,
1643 disc
? "disconnected" : "normal",
1644 slow
? "slow" : "fast");
1645 /* Analyze subchannel status. */
1650 /* Check if paths have become available. */
1656 /* Ask driver what to do with device. */
1657 if (io_subchannel_notify(sch
, event
))
1660 action
= UNREGISTER
;
1662 case CIO_REVALIDATE
:
1663 /* Device will be removed, so no notify necessary. */
1665 /* Reprobe because immediate unregister might block. */
1668 action
= UNREGISTER_PROBE
;
1672 /* Get device operational again. */
1676 /* Perform action. */
1680 case UNREGISTER_PROBE
:
1681 ccw_device_set_notoper(cdev
);
1682 /* Unregister device (will use subchannel lock). */
1683 spin_unlock_irqrestore(sch
->lock
, flags
);
1684 css_sch_device_unregister(sch
);
1685 spin_lock_irqsave(sch
->lock
, flags
);
1687 /* Reset intparm to zeroes. */
1688 sch
->schib
.pmcw
.intparm
= 0;
1692 ccw_device_trigger_reprobe(cdev
);
1695 device_set_disconnected(cdev
);
1700 spin_unlock_irqrestore(sch
->lock
, flags
);
1701 /* Probe if necessary. */
1702 if (action
== UNREGISTER_PROBE
)
1703 ret
= css_probe_device(sch
->schid
);
1708 #ifdef CONFIG_CCW_CONSOLE
1709 static struct ccw_device console_cdev
;
1710 static char console_cdev_name
[10] = "0.x.xxxx";
1711 static struct ccw_device_private console_private
;
1712 static int console_cdev_in_use
;
1714 static DEFINE_SPINLOCK(ccw_console_lock
);
1716 spinlock_t
* cio_get_console_lock(void)
1718 return &ccw_console_lock
;
1721 static int ccw_device_console_enable(struct ccw_device
*cdev
,
1722 struct subchannel
*sch
)
1726 /* Attach subchannel private data. */
1727 sch
->private = cio_get_console_priv();
1728 memset(sch
->private, 0, sizeof(struct io_subchannel_private
));
1729 io_subchannel_init_fields(sch
);
1730 sch
->driver
= &io_subchannel_driver
;
1731 /* Initialize the ccw_device structure. */
1732 cdev
->dev
.parent
= &sch
->dev
;
1733 rc
= io_subchannel_recog(cdev
, sch
);
1737 /* Now wait for the async. recognition to come to an end. */
1738 spin_lock_irq(cdev
->ccwlock
);
1739 while (!dev_fsm_final_state(cdev
))
1742 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1744 ccw_device_online(cdev
);
1745 while (!dev_fsm_final_state(cdev
))
1747 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1751 spin_unlock_irq(cdev
->ccwlock
);
1756 ccw_device_probe_console(void)
1758 struct subchannel
*sch
;
1761 if (xchg(&console_cdev_in_use
, 1) != 0)
1762 return ERR_PTR(-EBUSY
);
1763 sch
= cio_probe_console();
1765 console_cdev_in_use
= 0;
1766 return (void *) sch
;
1768 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1769 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1770 console_cdev
.private = &console_private
;
1771 console_private
.cdev
= &console_cdev
;
1772 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1774 cio_release_console();
1775 console_cdev_in_use
= 0;
1776 return ERR_PTR(ret
);
1778 console_cdev
.online
= 1;
1779 return &console_cdev
;
1783 const char *cio_get_console_cdev_name(struct subchannel
*sch
)
1785 snprintf(console_cdev_name
, 10, "0.%x.%04x",
1786 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1787 return (const char *)console_cdev_name
;
1792 * get ccw_device matching the busid, but only if owned by cdrv
1795 __ccwdev_check_busid(struct device
*dev
, void *id
)
1801 return (strcmp(bus_id
, dev_name(dev
)) == 0);
1806 * get_ccwdev_by_busid() - obtain device from a bus id
1807 * @cdrv: driver the device is owned by
1808 * @bus_id: bus id of the device to be searched
1810 * This function searches all devices owned by @cdrv for a device with a bus
1811 * id matching @bus_id.
1813 * If a match is found, its reference count of the found device is increased
1814 * and it is returned; else %NULL is returned.
1816 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1820 struct device_driver
*drv
;
1822 drv
= get_driver(&cdrv
->driver
);
1826 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1827 __ccwdev_check_busid
);
1830 return dev
? to_ccwdev(dev
) : NULL
;
1833 /************************** device driver handling ************************/
1835 /* This is the implementation of the ccw_driver class. The probe, remove
1836 * and release methods are initially very similar to the device_driver
1837 * implementations, with the difference that they have ccw_device
1840 * A ccw driver also contains the information that is needed for
1844 ccw_device_probe (struct device
*dev
)
1846 struct ccw_device
*cdev
= to_ccwdev(dev
);
1847 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1850 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1852 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1863 ccw_device_remove (struct device
*dev
)
1865 struct ccw_device
*cdev
= to_ccwdev(dev
);
1866 struct ccw_driver
*cdrv
= cdev
->drv
;
1873 spin_lock_irq(cdev
->ccwlock
);
1874 ret
= ccw_device_offline(cdev
);
1875 spin_unlock_irq(cdev
->ccwlock
);
1877 wait_event(cdev
->private->wait_q
,
1878 dev_fsm_final_state(cdev
));
1880 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1881 "device 0.%x.%04x\n",
1882 ret
, cdev
->private->dev_id
.ssid
,
1883 cdev
->private->dev_id
.devno
);
1884 /* Give up reference obtained in ccw_device_set_online(). */
1885 put_device(&cdev
->dev
);
1887 ccw_device_set_timeout(cdev
, 0);
1892 static void ccw_device_shutdown(struct device
*dev
)
1894 struct ccw_device
*cdev
;
1896 cdev
= to_ccwdev(dev
);
1897 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1898 cdev
->drv
->shutdown(cdev
);
1902 struct bus_type ccw_bus_type
= {
1904 .match
= ccw_bus_match
,
1905 .uevent
= ccw_uevent
,
1906 .probe
= ccw_device_probe
,
1907 .remove
= ccw_device_remove
,
1908 .shutdown
= ccw_device_shutdown
,
1912 * ccw_driver_register() - register a ccw driver
1913 * @cdriver: driver to be registered
1915 * This function is mainly a wrapper around driver_register().
1917 * %0 on success and a negative error value on failure.
1919 int ccw_driver_register(struct ccw_driver
*cdriver
)
1921 struct device_driver
*drv
= &cdriver
->driver
;
1923 drv
->bus
= &ccw_bus_type
;
1924 drv
->name
= cdriver
->name
;
1925 drv
->owner
= cdriver
->owner
;
1927 return driver_register(drv
);
1931 * ccw_driver_unregister() - deregister a ccw driver
1932 * @cdriver: driver to be deregistered
1934 * This function is mainly a wrapper around driver_unregister().
1936 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
1938 driver_unregister(&cdriver
->driver
);
1941 /* Helper func for qdio. */
1942 struct subchannel_id
1943 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1945 struct subchannel
*sch
;
1947 sch
= to_subchannel(cdev
->dev
.parent
);
1951 MODULE_LICENSE("GPL");
1952 EXPORT_SYMBOL(ccw_device_set_online
);
1953 EXPORT_SYMBOL(ccw_device_set_offline
);
1954 EXPORT_SYMBOL(ccw_driver_register
);
1955 EXPORT_SYMBOL(ccw_driver_unregister
);
1956 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1957 EXPORT_SYMBOL(ccw_bus_type
);
1958 EXPORT_SYMBOL(ccw_device_work
);
1959 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);