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"
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. */
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
;
56 found
= ccw_device_id_match(ids
, &cdev
->id
);
60 cdev
->id
.driver_info
= found
->driver_info
;
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
)
73 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
79 if (id
->dev_type
!= 0)
80 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
81 id
->dev_model
, suffix
);
83 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
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
);
95 char modalias_buf
[30];
98 ret
= add_uevent_var(env
, "CU_TYPE=%04X", id
->cu_type
);
103 ret
= add_uevent_var(env
, "CU_MODEL=%02X", id
->cu_model
);
107 /* The next two can be zero, that's ok for us */
109 ret
= add_uevent_var(env
, "DEV_TYPE=%04X", id
->dev_type
);
114 ret
= add_uevent_var(env
, "DEV_MODEL=%02X", id
->dev_model
);
119 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
120 ret
= add_uevent_var(env
, "MODALIAS=%s", modalias_buf
);
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
*,
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
);
159 init_ccw_bus_type (void)
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");
172 ret
= -ENOMEM
; /* FIXME: better errno ? */
175 if ((ret
= bus_register (&ccw_bus_type
)))
178 ret
= css_driver_register(&io_subchannel_driver
);
182 wait_event(ccw_device_init_wq
,
183 atomic_read(&ccw_device_init_count
) == 0);
184 flush_workqueue(ccw_device_work
);
188 destroy_workqueue(ccw_device_work
);
190 destroy_workqueue(slow_path_wq
);
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
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?
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
;
224 for (chp
= 0; chp
< 8; chp
++) {
226 if (ssd
->path_mask
& mask
)
227 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
229 ret
+= sprintf(buf
+ ret
, "00 ");
231 ret
+= sprintf (buf
+ret
, "\n");
232 return min((ssize_t
)PAGE_SIZE
, ret
);
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
);
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
);
255 return sprintf(buf
, "n/a\n");
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
);
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
);
275 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
277 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
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
);
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
);
315 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
320 * Forced offline in disconnected state means
321 * 'throw away device'.
323 /* Get cdev reference for workqueue processing. */
324 if (!get_device(&cdev
->dev
))
326 if (ccw_device_is_orphan(cdev
)) {
328 * Deregister ccw device.
329 * Unfortunately, we cannot do this directly from the
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
);
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.
351 * %0 on success and a negative error value on failure.
353 * enabled, ccw device lock not held
355 int ccw_device_set_offline(struct ccw_device
*cdev
)
361 if (!cdev
->online
|| !cdev
->drv
)
364 if (cdev
->drv
->set_offline
) {
365 ret
= cdev
->drv
->set_offline(cdev
);
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
);
380 spin_unlock_irq(cdev
->ccwlock
);
382 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
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
);
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
401 * %0 on success and a negative error value on failure.
403 * enabled, ccw device lock not held
405 int ccw_device_set_online(struct ccw_device
*cdev
)
411 if (cdev
->online
|| !cdev
->drv
)
414 spin_lock_irq(cdev
->ccwlock
);
415 ret
= ccw_device_online(cdev
);
416 spin_unlock_irq(cdev
->ccwlock
);
418 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
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
);
426 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
428 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
432 spin_lock_irq(cdev
->ccwlock
);
433 ret
= ccw_device_offline(cdev
);
434 spin_unlock_irq(cdev
->ccwlock
);
436 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
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
)
457 /* Do device recognition, if needed. */
458 if (cdev
->id
.cu_type
== 0) {
459 ret
= ccw_device_recognition(cdev
);
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
);
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
);
474 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
478 ret
= online_store_recog_and_online(cdev
);
481 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
482 ret
= ccw_device_stlck(cdev
);
485 if (cdev
->id
.cu_type
== 0)
486 cdev
->private->state
= DEV_STATE_NOT_OPER
;
487 online_store_recog_and_online(cdev
);
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
);
499 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
502 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
503 atomic_set(&cdev
->private->onoff
, 0);
506 if (!strncmp(buf
, "force\n", count
)) {
512 ret
= strict_strtoul(buf
, 16, &i
);
518 online_store_handle_offline(cdev
);
522 ret
= online_store_handle_online(cdev
, force
);
531 module_put(cdev
->drv
->owner
);
532 atomic_set(&cdev
->private->onoff
, 0);
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
);
552 return sprintf(buf
, "no path\n");
554 return sprintf(buf
, "no device\n");
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
,
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
,
589 static struct attribute_group ccwdev_attr_group
= {
590 .attrs
= ccwdev_attrs
,
593 static struct attribute_group
*ccwdev_attr_groups
[] = {
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
;
605 dev
->bus
= &ccw_bus_type
;
607 if ((ret
= device_add(dev
)))
610 set_bit(1, &cdev
->private->registered
);
615 struct ccw_dev_id dev_id
;
616 struct ccw_device
* sibling
;
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
))
634 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
635 struct ccw_device
*sibling
)
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
;
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
)
663 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
666 return dev
? to_ccwdev(dev
) : NULL
;
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
);
677 if (device_add(&cdev
->dev
)) {
678 put_device(&cdev
->dev
);
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
);
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
);
701 ccw_device_release(struct device
*dev
)
703 struct ccw_device
*cdev
;
705 cdev
= to_ccwdev(dev
);
706 kfree(cdev
->private);
710 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
712 struct ccw_device
*cdev
;
714 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
716 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
717 GFP_KERNEL
| GFP_DMA
);
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
);
744 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
746 struct ccw_device
*cdev
;
749 cdev
= io_subchannel_allocate_dev(sch
);
751 ret
= io_subchannel_initialize_dev(sch
, 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
;
780 other_sch
= to_subchannel(get_device(cdev
->dev
.parent
));
781 ret
= device_move(&cdev
->dev
, &sch
->dev
);
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
);
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
)
801 /* Try to move the ccw device to its new subchannel. */
802 ret
= device_move(&cdev
->dev
, &sch
->dev
);
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
);
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
);
820 /* OK, we did everything we could... */
821 css_sch_device_unregister(sch
);
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
;
846 struct channel_subsystem
*css
;
847 struct ccw_dev_id dev_id
;
849 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
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
);
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
);
867 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
869 * Search for the replacing ccw device
870 * - among the disconnected devices
873 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
874 if (replacing_cdev
) {
875 sch_attach_disconnected_device(sch
, replacing_cdev
);
878 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
879 if (replacing_cdev
) {
880 sch_attach_orphaned_device(sch
, replacing_cdev
);
883 sch_create_and_recog_new_device(sch
);
887 * Register recognized device.
890 io_subchannel_register(struct work_struct
*work
)
892 struct ccw_device_private
*priv
;
893 struct ccw_device
*cdev
;
894 struct subchannel
*sch
;
898 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
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
908 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
910 ret
= device_reprobe(&cdev
->dev
);
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
);
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
);
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);
938 put_device(&sch
->dev
);
939 if (atomic_dec_and_test(&ccw_device_init_count
))
940 wake_up(&ccw_device_init_wq
);
943 put_device(&cdev
->dev
);
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
);
960 /* Get subchannel reference for local processing. */
961 if (!get_device(cdev
->dev
.parent
))
963 sch
= to_subchannel(cdev
->dev
.parent
);
964 css_sch_device_unregister(sch
);
965 /* Reset intparm to zeroes. */
966 sch
->schib
.pmcw
.intparm
= 0;
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.
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;
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
))
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
);
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
))
1010 PREPARE_WORK(&cdev
->private->kick_work
,
1011 io_subchannel_register
);
1012 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1018 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
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
);
1048 if (atomic_dec_and_test(&ccw_device_init_count
))
1049 wake_up(&ccw_device_init_wq
);
1054 static void ccw_device_move_to_sch(struct work_struct
*work
)
1056 struct ccw_device_private
*priv
;
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
);
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
);
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
);
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
);
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
);
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
))
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;
1134 static int io_subchannel_probe(struct subchannel
*sch
)
1136 struct ccw_device
*cdev
;
1138 unsigned long flags
;
1139 struct ccw_dev_id dev_id
;
1141 cdev
= sch_get_cdev(sch
);
1143 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1144 &io_subchannel_attr_group
);
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
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
);
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
);
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) {
1191 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1193 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
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
);
1207 cdev
= io_subchannel_create_ccwdev(sch
);
1212 rc
= io_subchannel_recog(cdev
, sch
);
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
);
1223 kfree(sch
->private);
1224 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1229 io_subchannel_remove (struct subchannel
*sch
)
1231 struct ccw_device
*cdev
;
1232 unsigned long flags
;
1234 cdev
= sch_get_cdev(sch
);
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
);
1249 static int io_subchannel_notify(struct subchannel
*sch
, int event
)
1251 struct ccw_device
*cdev
;
1253 cdev
= sch_get_cdev(sch
);
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
);
1265 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1268 static int check_for_io_on_path(struct subchannel
*sch
, int mask
)
1272 cc
= stsch(sch
->schid
, &sch
->schib
);
1275 if (scsw_actl(&sch
->schib
.scsw
) && sch
->schib
.pmcw
.lpum
== mask
)
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. */
1287 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1289 css_schedule_eval(sch
->schid
);
1292 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1293 /* Request retry of internal operation. */
1294 cdev
->private->flags
.intretry
= 1;
1297 cdev
->handler(cdev
, cdev
->private->intparm
,
1301 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1303 struct ccw_device
*cdev
;
1305 cdev
= sch_get_cdev(sch
);
1308 if (check_for_io_on_path(sch
, mask
)) {
1309 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1310 ccw_device_kill_io(cdev
);
1312 terminate_internal_io(sch
, cdev
);
1313 /* Re-start path verification. */
1314 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
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
)
1327 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1334 io_subchannel_terminate_path(sch
, mask
);
1339 io_subchannel_verify(sch
);
1342 if (stsch(sch
->schid
, &sch
->schib
))
1344 if (!css_sch_is_valid(&sch
->schib
))
1346 io_subchannel_terminate_path(sch
, mask
);
1349 if (stsch(sch
->schid
, &sch
->schib
))
1351 sch
->lpm
|= mask
& sch
->opm
;
1352 io_subchannel_verify(sch
);
1359 io_subchannel_shutdown(struct subchannel
*sch
)
1361 struct ccw_device
*cdev
;
1364 cdev
= sch_get_cdev(sch
);
1366 if (cio_is_console(sch
->schid
))
1368 if (!sch
->schib
.pmcw
.ena
)
1369 /* Nothing to do. */
1371 ret
= cio_disable_subchannel(sch
);
1373 /* Subchannel is disabled, we're done. */
1375 cdev
->private->state
= DEV_STATE_QUIESCE
;
1377 cdev
->handler(cdev
, cdev
->private->intparm
,
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
)
1391 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
1393 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
1394 return CIO_REVALIDATE
;
1400 static int device_is_disconnected(struct ccw_device
*cdev
)
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
);
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
);
1422 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1426 spin_unlock_irq(cdev
->ccwlock
);
1431 static void recovery_work_func(struct work_struct
*unused
)
1435 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1437 spin_lock_irq(&recovery_lock
);
1438 if (!timer_pending(&recovery_timer
)) {
1439 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1441 mod_timer(&recovery_timer
, jiffies
+
1442 recovery_delay
[recovery_phase
] * HZ
);
1444 spin_unlock_irq(&recovery_lock
);
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)) {
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
)
1477 ccw_device_set_timeout(cdev
, 0);
1478 cdev
->private->flags
.fake_irb
= 0;
1479 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
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
);
1506 /* Disconnected devices are evaluated directly only.*/
1507 spin_unlock_irqrestore(sch
->lock
, flags
);
1510 /* No interrupt after machine check - kill pending timers. */
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
);
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. */
1528 /* Check if paths have become available. */
1534 /* Ask driver what to do with device. */
1535 if (io_subchannel_notify(sch
, event
))
1538 action
= UNREGISTER
;
1540 case CIO_REVALIDATE
:
1541 /* Device will be removed, so no notify necessary. */
1543 /* Reprobe because immediate unregister might block. */
1546 action
= UNREGISTER_PROBE
;
1550 /* Get device operational again. */
1554 /* Perform action. */
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;
1570 ccw_device_trigger_reprobe(cdev
);
1573 device_set_disconnected(cdev
);
1578 spin_unlock_irqrestore(sch
->lock
, flags
);
1579 /* Probe if necessary. */
1580 if (action
== UNREGISTER_PROBE
)
1581 ret
= css_probe_device(sch
->schid
);
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
)
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
);
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
))
1619 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1621 ccw_device_online(cdev
);
1622 while (!dev_fsm_final_state(cdev
))
1624 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1628 spin_unlock_irq(cdev
->ccwlock
);
1633 ccw_device_probe_console(void)
1635 struct subchannel
*sch
;
1638 if (xchg(&console_cdev_in_use
, 1) != 0)
1639 return ERR_PTR(-EBUSY
);
1640 sch
= cio_probe_console();
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
);
1651 cio_release_console();
1652 console_cdev_in_use
= 0;
1653 return ERR_PTR(ret
);
1655 console_cdev
.online
= 1;
1656 return &console_cdev
;
1661 * get ccw_device matching the busid, but only if owned by cdrv
1664 __ccwdev_check_busid(struct device
*dev
, void *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.
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
,
1689 struct device_driver
*drv
;
1691 drv
= get_driver(&cdrv
->driver
);
1695 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1696 __ccwdev_check_busid
);
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
1709 * A ccw driver also contains the information that is needed for
1713 ccw_device_probe (struct device
*dev
)
1715 struct ccw_device
*cdev
= to_ccwdev(dev
);
1716 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1719 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1721 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1732 ccw_device_remove (struct device
*dev
)
1734 struct ccw_device
*cdev
= to_ccwdev(dev
);
1735 struct ccw_driver
*cdrv
= cdev
->drv
;
1742 spin_lock_irq(cdev
->ccwlock
);
1743 ret
= ccw_device_offline(cdev
);
1744 spin_unlock_irq(cdev
->ccwlock
);
1746 wait_event(cdev
->private->wait_q
,
1747 dev_fsm_final_state(cdev
));
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);
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
);
1769 struct bus_type ccw_bus_type
= {
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().
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
);
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
);