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
);
381 spin_unlock_irq(cdev
->ccwlock
);
383 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
385 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
386 "device 0.%x.%04x\n",
387 ret
, cdev
->private->dev_id
.ssid
,
388 cdev
->private->dev_id
.devno
);
395 * ccw_device_set_online() - enable a ccw device for I/O
396 * @cdev: target ccw device
398 * This function first enables @cdev and then calls the driver's set_online()
399 * function for @cdev, if given. If set_online() returns an error, @cdev is
402 * %0 on success and a negative error value on failure.
404 * enabled, ccw device lock not held
406 int ccw_device_set_online(struct ccw_device
*cdev
)
412 if (cdev
->online
|| !cdev
->drv
)
415 spin_lock_irq(cdev
->ccwlock
);
416 ret
= ccw_device_online(cdev
);
417 spin_unlock_irq(cdev
->ccwlock
);
419 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
421 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
422 "device 0.%x.%04x\n",
423 ret
, cdev
->private->dev_id
.ssid
,
424 cdev
->private->dev_id
.devno
);
427 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
429 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
433 spin_lock_irq(cdev
->ccwlock
);
434 ret
= ccw_device_offline(cdev
);
435 spin_unlock_irq(cdev
->ccwlock
);
437 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
439 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
440 "device 0.%x.%04x\n",
441 ret
, cdev
->private->dev_id
.ssid
,
442 cdev
->private->dev_id
.devno
);
443 return (ret
== 0) ? -ENODEV
: ret
;
446 static void online_store_handle_offline(struct ccw_device
*cdev
)
448 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
449 ccw_device_remove_disconnected(cdev
);
450 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
451 ccw_device_set_offline(cdev
);
454 static int online_store_recog_and_online(struct ccw_device
*cdev
)
458 /* Do device recognition, if needed. */
459 if (cdev
->id
.cu_type
== 0) {
460 ret
= ccw_device_recognition(cdev
);
462 CIO_MSG_EVENT(0, "Couldn't start recognition "
463 "for device 0.%x.%04x (ret=%d)\n",
464 cdev
->private->dev_id
.ssid
,
465 cdev
->private->dev_id
.devno
, ret
);
468 wait_event(cdev
->private->wait_q
,
469 cdev
->private->flags
.recog_done
);
471 if (cdev
->drv
&& cdev
->drv
->set_online
)
472 ccw_device_set_online(cdev
);
475 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
479 ret
= online_store_recog_and_online(cdev
);
482 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
483 ret
= ccw_device_stlck(cdev
);
486 if (cdev
->id
.cu_type
== 0)
487 cdev
->private->state
= DEV_STATE_NOT_OPER
;
488 online_store_recog_and_online(cdev
);
493 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
494 const char *buf
, size_t count
)
496 struct ccw_device
*cdev
= to_ccwdev(dev
);
500 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
503 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
504 atomic_set(&cdev
->private->onoff
, 0);
507 if (!strncmp(buf
, "force\n", count
)) {
513 ret
= strict_strtoul(buf
, 16, &i
);
519 online_store_handle_offline(cdev
);
523 ret
= online_store_handle_online(cdev
, force
);
532 module_put(cdev
->drv
->owner
);
533 atomic_set(&cdev
->private->onoff
, 0);
538 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
540 struct ccw_device
*cdev
= to_ccwdev(dev
);
541 struct subchannel
*sch
;
543 if (ccw_device_is_orphan(cdev
))
544 return sprintf(buf
, "no device\n");
545 switch (cdev
->private->state
) {
546 case DEV_STATE_BOXED
:
547 return sprintf(buf
, "boxed\n");
548 case DEV_STATE_DISCONNECTED
:
549 case DEV_STATE_DISCONNECTED_SENSE_ID
:
550 case DEV_STATE_NOT_OPER
:
551 sch
= to_subchannel(dev
->parent
);
553 return sprintf(buf
, "no path\n");
555 return sprintf(buf
, "no device\n");
557 /* All other states considered fine. */
558 return sprintf(buf
, "good\n");
562 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
563 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
564 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
565 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
566 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
567 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
568 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
570 static struct attribute
*io_subchannel_attrs
[] = {
571 &dev_attr_chpids
.attr
,
572 &dev_attr_pimpampom
.attr
,
576 static struct attribute_group io_subchannel_attr_group
= {
577 .attrs
= io_subchannel_attrs
,
580 static struct attribute
* ccwdev_attrs
[] = {
581 &dev_attr_devtype
.attr
,
582 &dev_attr_cutype
.attr
,
583 &dev_attr_modalias
.attr
,
584 &dev_attr_online
.attr
,
585 &dev_attr_cmb_enable
.attr
,
586 &dev_attr_availability
.attr
,
590 static struct attribute_group ccwdev_attr_group
= {
591 .attrs
= ccwdev_attrs
,
594 static struct attribute_group
*ccwdev_attr_groups
[] = {
599 /* this is a simple abstraction for device_register that sets the
600 * correct bus type and adds the bus specific files */
601 static int ccw_device_register(struct ccw_device
*cdev
)
603 struct device
*dev
= &cdev
->dev
;
606 dev
->bus
= &ccw_bus_type
;
608 if ((ret
= device_add(dev
)))
611 set_bit(1, &cdev
->private->registered
);
616 struct ccw_dev_id dev_id
;
617 struct ccw_device
* sibling
;
621 match_devno(struct device
* dev
, void * data
)
623 struct match_data
* d
= data
;
624 struct ccw_device
* cdev
;
626 cdev
= to_ccwdev(dev
);
627 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
628 !ccw_device_is_orphan(cdev
) &&
629 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
630 (cdev
!= d
->sibling
))
635 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
636 struct ccw_device
*sibling
)
639 struct match_data data
;
641 data
.dev_id
= *dev_id
;
642 data
.sibling
= sibling
;
643 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
645 return dev
? to_ccwdev(dev
) : NULL
;
648 static int match_orphan(struct device
*dev
, void *data
)
650 struct ccw_dev_id
*dev_id
;
651 struct ccw_device
*cdev
;
654 cdev
= to_ccwdev(dev
);
655 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
658 static struct ccw_device
*
659 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
660 struct ccw_dev_id
*dev_id
)
664 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
667 return dev
? to_ccwdev(dev
) : NULL
;
671 ccw_device_add_changed(struct work_struct
*work
)
673 struct ccw_device_private
*priv
;
674 struct ccw_device
*cdev
;
676 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
678 if (device_add(&cdev
->dev
)) {
679 put_device(&cdev
->dev
);
682 set_bit(1, &cdev
->private->registered
);
685 void ccw_device_do_unreg_rereg(struct work_struct
*work
)
687 struct ccw_device_private
*priv
;
688 struct ccw_device
*cdev
;
689 struct subchannel
*sch
;
691 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
693 sch
= to_subchannel(cdev
->dev
.parent
);
695 ccw_device_unregister(cdev
);
696 PREPARE_WORK(&cdev
->private->kick_work
,
697 ccw_device_add_changed
);
698 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
702 ccw_device_release(struct device
*dev
)
704 struct ccw_device
*cdev
;
706 cdev
= to_ccwdev(dev
);
707 kfree(cdev
->private);
711 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
713 struct ccw_device
*cdev
;
715 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
717 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
718 GFP_KERNEL
| GFP_DMA
);
723 return ERR_PTR(-ENOMEM
);
726 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
727 struct ccw_device
*cdev
)
729 cdev
->private->cdev
= cdev
;
730 atomic_set(&cdev
->private->onoff
, 0);
731 cdev
->dev
.parent
= &sch
->dev
;
732 cdev
->dev
.release
= ccw_device_release
;
733 INIT_WORK(&cdev
->private->kick_work
, NULL
);
734 cdev
->dev
.groups
= ccwdev_attr_groups
;
735 /* Do first half of device_register. */
736 device_initialize(&cdev
->dev
);
737 if (!get_device(&sch
->dev
)) {
738 if (cdev
->dev
.release
)
739 cdev
->dev
.release(&cdev
->dev
);
745 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
747 struct ccw_device
*cdev
;
750 cdev
= io_subchannel_allocate_dev(sch
);
752 ret
= io_subchannel_initialize_dev(sch
, cdev
);
761 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
763 static void sch_attach_device(struct subchannel
*sch
,
764 struct ccw_device
*cdev
)
766 css_update_ssd_info(sch
);
767 spin_lock_irq(sch
->lock
);
768 sch_set_cdev(sch
, cdev
);
769 cdev
->private->schid
= sch
->schid
;
770 cdev
->ccwlock
= sch
->lock
;
771 ccw_device_trigger_reprobe(cdev
);
772 spin_unlock_irq(sch
->lock
);
775 static void sch_attach_disconnected_device(struct subchannel
*sch
,
776 struct ccw_device
*cdev
)
778 struct subchannel
*other_sch
;
781 other_sch
= to_subchannel(get_device(cdev
->dev
.parent
));
782 ret
= device_move(&cdev
->dev
, &sch
->dev
);
784 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
785 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
786 cdev
->private->dev_id
.devno
, ret
);
787 put_device(&other_sch
->dev
);
790 sch_set_cdev(other_sch
, NULL
);
791 /* No need to keep a subchannel without ccw device around. */
792 css_sch_device_unregister(other_sch
);
793 put_device(&other_sch
->dev
);
794 sch_attach_device(sch
, cdev
);
797 static void sch_attach_orphaned_device(struct subchannel
*sch
,
798 struct ccw_device
*cdev
)
802 /* Try to move the ccw device to its new subchannel. */
803 ret
= device_move(&cdev
->dev
, &sch
->dev
);
805 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
806 "failed (ret=%d)!\n",
807 cdev
->private->dev_id
.ssid
,
808 cdev
->private->dev_id
.devno
, ret
);
811 sch_attach_device(sch
, cdev
);
814 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
816 struct ccw_device
*cdev
;
818 /* Need to allocate a new ccw device. */
819 cdev
= io_subchannel_create_ccwdev(sch
);
821 /* OK, we did everything we could... */
822 css_sch_device_unregister(sch
);
825 spin_lock_irq(sch
->lock
);
826 sch_set_cdev(sch
, cdev
);
827 spin_unlock_irq(sch
->lock
);
828 /* Start recognition for the new ccw device. */
829 if (io_subchannel_recog(cdev
, sch
)) {
830 spin_lock_irq(sch
->lock
);
831 sch_set_cdev(sch
, NULL
);
832 spin_unlock_irq(sch
->lock
);
833 if (cdev
->dev
.release
)
834 cdev
->dev
.release(&cdev
->dev
);
835 css_sch_device_unregister(sch
);
840 void ccw_device_move_to_orphanage(struct work_struct
*work
)
842 struct ccw_device_private
*priv
;
843 struct ccw_device
*cdev
;
844 struct ccw_device
*replacing_cdev
;
845 struct subchannel
*sch
;
847 struct channel_subsystem
*css
;
848 struct ccw_dev_id dev_id
;
850 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
852 sch
= to_subchannel(cdev
->dev
.parent
);
853 css
= to_css(sch
->dev
.parent
);
854 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
855 dev_id
.ssid
= sch
->schid
.ssid
;
858 * Move the orphaned ccw device to the orphanage so the replacing
859 * ccw device can take its place on the subchannel.
861 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
);
863 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
864 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
865 cdev
->private->dev_id
.devno
, ret
);
868 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
870 * Search for the replacing ccw device
871 * - among the disconnected devices
874 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
875 if (replacing_cdev
) {
876 sch_attach_disconnected_device(sch
, replacing_cdev
);
877 /* Release reference from get_disc_ccwdev_by_dev_id() */
878 put_device(&cdev
->dev
);
881 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
882 if (replacing_cdev
) {
883 sch_attach_orphaned_device(sch
, replacing_cdev
);
884 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
885 put_device(&cdev
->dev
);
888 sch_create_and_recog_new_device(sch
);
892 * Register recognized device.
895 io_subchannel_register(struct work_struct
*work
)
897 struct ccw_device_private
*priv
;
898 struct ccw_device
*cdev
;
899 struct subchannel
*sch
;
903 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
905 sch
= to_subchannel(cdev
->dev
.parent
);
906 css_update_ssd_info(sch
);
908 * io_subchannel_register() will also be called after device
909 * recognition has been done for a boxed device (which will already
910 * be registered). We need to reprobe since we may now have sense id
913 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
915 ret
= device_reprobe(&cdev
->dev
);
917 /* We can't do much here. */
918 CIO_MSG_EVENT(0, "device_reprobe() returned"
919 " %d for 0.%x.%04x\n", ret
,
920 cdev
->private->dev_id
.ssid
,
921 cdev
->private->dev_id
.devno
);
926 * Now we know this subchannel will stay, we can throw
927 * our delayed uevent.
929 sch
->dev
.uevent_suppress
= 0;
930 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
931 /* make it known to the system */
932 ret
= ccw_device_register(cdev
);
934 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
935 cdev
->private->dev_id
.ssid
,
936 cdev
->private->dev_id
.devno
, ret
);
937 put_device(&cdev
->dev
);
938 spin_lock_irqsave(sch
->lock
, flags
);
939 sch_set_cdev(sch
, NULL
);
940 spin_unlock_irqrestore(sch
->lock
, flags
);
941 kfree (cdev
->private);
943 put_device(&sch
->dev
);
944 if (atomic_dec_and_test(&ccw_device_init_count
))
945 wake_up(&ccw_device_init_wq
);
948 put_device(&cdev
->dev
);
950 cdev
->private->flags
.recog_done
= 1;
951 put_device(&sch
->dev
);
952 wake_up(&cdev
->private->wait_q
);
953 if (atomic_dec_and_test(&ccw_device_init_count
))
954 wake_up(&ccw_device_init_wq
);
957 static void ccw_device_call_sch_unregister(struct work_struct
*work
)
959 struct ccw_device_private
*priv
;
960 struct ccw_device
*cdev
;
961 struct subchannel
*sch
;
963 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
965 /* Get subchannel reference for local processing. */
966 if (!get_device(cdev
->dev
.parent
))
968 sch
= to_subchannel(cdev
->dev
.parent
);
969 css_sch_device_unregister(sch
);
970 /* Reset intparm to zeroes. */
971 sch
->schib
.pmcw
.intparm
= 0;
973 /* Release cdev reference for workqueue processing.*/
974 put_device(&cdev
->dev
);
975 /* Release subchannel reference for local processing. */
976 put_device(&sch
->dev
);
980 * subchannel recognition done. Called from the state machine.
983 io_subchannel_recog_done(struct ccw_device
*cdev
)
985 struct subchannel
*sch
;
987 if (css_init_done
== 0) {
988 cdev
->private->flags
.recog_done
= 1;
991 switch (cdev
->private->state
) {
992 case DEV_STATE_NOT_OPER
:
993 cdev
->private->flags
.recog_done
= 1;
994 /* Remove device found not operational. */
995 if (!get_device(&cdev
->dev
))
997 sch
= to_subchannel(cdev
->dev
.parent
);
998 PREPARE_WORK(&cdev
->private->kick_work
,
999 ccw_device_call_sch_unregister
);
1000 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1001 /* Release subchannel reference for asynchronous recognition. */
1002 put_device(&sch
->dev
);
1003 if (atomic_dec_and_test(&ccw_device_init_count
))
1004 wake_up(&ccw_device_init_wq
);
1006 case DEV_STATE_BOXED
:
1007 /* Device did not respond in time. */
1008 case DEV_STATE_OFFLINE
:
1010 * We can't register the device in interrupt context so
1011 * we schedule a work item.
1013 if (!get_device(&cdev
->dev
))
1015 PREPARE_WORK(&cdev
->private->kick_work
,
1016 io_subchannel_register
);
1017 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1023 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
1026 struct ccw_device_private
*priv
;
1028 sch_set_cdev(sch
, cdev
);
1029 cdev
->ccwlock
= sch
->lock
;
1031 /* Init private data. */
1032 priv
= cdev
->private;
1033 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1034 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
1035 priv
->schid
= sch
->schid
;
1036 priv
->state
= DEV_STATE_NOT_OPER
;
1037 INIT_LIST_HEAD(&priv
->cmb_list
);
1038 init_waitqueue_head(&priv
->wait_q
);
1039 init_timer(&priv
->timer
);
1041 /* Set an initial name for the device. */
1042 if (cio_is_console(sch
->schid
))
1043 cdev
->dev
.init_name
= cio_get_console_cdev_name(sch
);
1045 dev_set_name(&cdev
->dev
, "0.%x.%04x",
1046 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1048 /* Increase counter of devices currently in recognition. */
1049 atomic_inc(&ccw_device_init_count
);
1051 /* Start async. device sensing. */
1052 spin_lock_irq(sch
->lock
);
1053 rc
= ccw_device_recognition(cdev
);
1054 spin_unlock_irq(sch
->lock
);
1056 if (atomic_dec_and_test(&ccw_device_init_count
))
1057 wake_up(&ccw_device_init_wq
);
1062 static void ccw_device_move_to_sch(struct work_struct
*work
)
1064 struct ccw_device_private
*priv
;
1066 struct subchannel
*sch
;
1067 struct ccw_device
*cdev
;
1068 struct subchannel
*former_parent
;
1070 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1073 former_parent
= ccw_device_is_orphan(cdev
) ?
1074 NULL
: to_subchannel(get_device(cdev
->dev
.parent
));
1075 mutex_lock(&sch
->reg_mutex
);
1076 /* Try to move the ccw device to its new subchannel. */
1077 rc
= device_move(&cdev
->dev
, &sch
->dev
);
1078 mutex_unlock(&sch
->reg_mutex
);
1080 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1081 "0.%x.%04x failed (ret=%d)!\n",
1082 cdev
->private->dev_id
.ssid
,
1083 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1084 sch
->schid
.sch_no
, rc
);
1085 css_sch_device_unregister(sch
);
1088 if (former_parent
) {
1089 spin_lock_irq(former_parent
->lock
);
1090 sch_set_cdev(former_parent
, NULL
);
1091 spin_unlock_irq(former_parent
->lock
);
1092 css_sch_device_unregister(former_parent
);
1093 /* Reset intparm to zeroes. */
1094 former_parent
->schib
.pmcw
.intparm
= 0;
1095 cio_modify(former_parent
);
1097 sch_attach_device(sch
, cdev
);
1100 put_device(&former_parent
->dev
);
1101 put_device(&cdev
->dev
);
1104 static void io_subchannel_irq(struct subchannel
*sch
)
1106 struct ccw_device
*cdev
;
1108 cdev
= sch_get_cdev(sch
);
1110 CIO_TRACE_EVENT(3, "IRQ");
1111 CIO_TRACE_EVENT(3, dev_name(&sch
->dev
));
1113 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1116 static void io_subchannel_init_fields(struct subchannel
*sch
)
1118 if (cio_is_console(sch
->schid
))
1121 sch
->opm
= chp_get_sch_opm(sch
);
1122 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1123 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1125 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1126 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1127 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1128 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1129 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1130 /* Initially set up some fields in the pmcw. */
1131 sch
->schib
.pmcw
.ena
= 0;
1132 sch
->schib
.pmcw
.csense
= 1; /* concurrent sense */
1133 if ((sch
->lpm
& (sch
->lpm
- 1)) != 0)
1134 sch
->schib
.pmcw
.mp
= 1; /* multipath mode */
1135 /* clean up possible residual cmf stuff */
1136 sch
->schib
.pmcw
.mme
= 0;
1137 sch
->schib
.pmcw
.mbfc
= 0;
1138 sch
->schib
.pmcw
.mbi
= 0;
1142 static int io_subchannel_probe(struct subchannel
*sch
)
1144 struct ccw_device
*cdev
;
1146 unsigned long flags
;
1147 struct ccw_dev_id dev_id
;
1149 cdev
= sch_get_cdev(sch
);
1151 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1152 &io_subchannel_attr_group
);
1154 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1155 "attributes for subchannel "
1156 "0.%x.%04x (rc=%d)\n",
1157 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1159 * This subchannel already has an associated ccw_device.
1160 * Throw the delayed uevent for the subchannel, register
1161 * the ccw_device and exit. This happens for all early
1162 * devices, e.g. the console.
1164 sch
->dev
.uevent_suppress
= 0;
1165 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1166 cdev
->dev
.groups
= ccwdev_attr_groups
;
1167 device_initialize(&cdev
->dev
);
1168 ccw_device_register(cdev
);
1170 * Check if the device is already online. If it is
1171 * the reference count needs to be corrected
1172 * (see ccw_device_online and css_init_done for the
1175 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1176 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1177 cdev
->private->state
!= DEV_STATE_BOXED
)
1178 get_device(&cdev
->dev
);
1181 io_subchannel_init_fields(sch
);
1183 * First check if a fitting device may be found amongst the
1184 * disconnected devices or in the orphanage.
1186 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1187 dev_id
.ssid
= sch
->schid
.ssid
;
1188 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1189 &io_subchannel_attr_group
);
1192 /* Allocate I/O subchannel private data. */
1193 sch
->private = kzalloc(sizeof(struct io_subchannel_private
),
1194 GFP_KERNEL
| GFP_DMA
);
1195 if (!sch
->private) {
1199 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1201 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1205 * Schedule moving the device until when we have a registered
1206 * subchannel to move to and succeed the probe. We can
1207 * unregister later again, when the probe is through.
1209 cdev
->private->sch
= sch
;
1210 PREPARE_WORK(&cdev
->private->kick_work
,
1211 ccw_device_move_to_sch
);
1212 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1215 cdev
= io_subchannel_create_ccwdev(sch
);
1220 rc
= io_subchannel_recog(cdev
, sch
);
1222 spin_lock_irqsave(sch
->lock
, flags
);
1223 sch_set_cdev(sch
, NULL
);
1224 spin_unlock_irqrestore(sch
->lock
, flags
);
1225 if (cdev
->dev
.release
)
1226 cdev
->dev
.release(&cdev
->dev
);
1231 kfree(sch
->private);
1232 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1237 io_subchannel_remove (struct subchannel
*sch
)
1239 struct ccw_device
*cdev
;
1240 unsigned long flags
;
1242 cdev
= sch_get_cdev(sch
);
1245 /* Set ccw device to not operational and drop reference. */
1246 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1247 sch_set_cdev(sch
, NULL
);
1248 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1249 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1250 ccw_device_unregister(cdev
);
1251 put_device(&cdev
->dev
);
1252 kfree(sch
->private);
1253 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1257 static int io_subchannel_notify(struct subchannel
*sch
, int event
)
1259 struct ccw_device
*cdev
;
1261 cdev
= sch_get_cdev(sch
);
1264 return ccw_device_notify(cdev
, event
);
1267 static void io_subchannel_verify(struct subchannel
*sch
)
1269 struct ccw_device
*cdev
;
1271 cdev
= sch_get_cdev(sch
);
1273 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1276 static int check_for_io_on_path(struct subchannel
*sch
, int mask
)
1280 cc
= stsch(sch
->schid
, &sch
->schib
);
1283 if (scsw_actl(&sch
->schib
.scsw
) && sch
->schib
.pmcw
.lpum
== mask
)
1288 static void terminate_internal_io(struct subchannel
*sch
,
1289 struct ccw_device
*cdev
)
1291 if (cio_clear(sch
)) {
1292 /* Recheck device in case clear failed. */
1295 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1297 css_schedule_eval(sch
->schid
);
1300 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1301 /* Request retry of internal operation. */
1302 cdev
->private->flags
.intretry
= 1;
1305 cdev
->handler(cdev
, cdev
->private->intparm
,
1309 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1311 struct ccw_device
*cdev
;
1313 cdev
= sch_get_cdev(sch
);
1316 if (check_for_io_on_path(sch
, mask
)) {
1317 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1318 ccw_device_kill_io(cdev
);
1320 terminate_internal_io(sch
, cdev
);
1321 /* Re-start path verification. */
1322 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1325 /* trigger path verification. */
1326 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1330 static int io_subchannel_chp_event(struct subchannel
*sch
,
1331 struct chp_link
*link
, int event
)
1335 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1342 io_subchannel_terminate_path(sch
, mask
);
1347 io_subchannel_verify(sch
);
1350 if (stsch(sch
->schid
, &sch
->schib
))
1352 if (!css_sch_is_valid(&sch
->schib
))
1354 io_subchannel_terminate_path(sch
, mask
);
1357 if (stsch(sch
->schid
, &sch
->schib
))
1359 sch
->lpm
|= mask
& sch
->opm
;
1360 io_subchannel_verify(sch
);
1367 io_subchannel_shutdown(struct subchannel
*sch
)
1369 struct ccw_device
*cdev
;
1372 cdev
= sch_get_cdev(sch
);
1374 if (cio_is_console(sch
->schid
))
1376 if (!sch
->schib
.pmcw
.ena
)
1377 /* Nothing to do. */
1379 ret
= cio_disable_subchannel(sch
);
1381 /* Subchannel is disabled, we're done. */
1383 cdev
->private->state
= DEV_STATE_QUIESCE
;
1385 cdev
->handler(cdev
, cdev
->private->intparm
,
1387 ret
= ccw_device_cancel_halt_clear(cdev
);
1388 if (ret
== -EBUSY
) {
1389 ccw_device_set_timeout(cdev
, HZ
/10);
1390 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1392 cio_disable_subchannel(sch
);
1395 static int io_subchannel_get_status(struct subchannel
*sch
)
1399 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
1401 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
1402 return CIO_REVALIDATE
;
1408 static int device_is_disconnected(struct ccw_device
*cdev
)
1412 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1413 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1416 static int recovery_check(struct device
*dev
, void *data
)
1418 struct ccw_device
*cdev
= to_ccwdev(dev
);
1421 spin_lock_irq(cdev
->ccwlock
);
1422 switch (cdev
->private->state
) {
1423 case DEV_STATE_DISCONNECTED
:
1424 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1425 cdev
->private->dev_id
.ssid
,
1426 cdev
->private->dev_id
.devno
);
1427 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1430 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1434 spin_unlock_irq(cdev
->ccwlock
);
1439 static void recovery_work_func(struct work_struct
*unused
)
1443 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1445 spin_lock_irq(&recovery_lock
);
1446 if (!timer_pending(&recovery_timer
)) {
1447 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1449 mod_timer(&recovery_timer
, jiffies
+
1450 recovery_delay
[recovery_phase
] * HZ
);
1452 spin_unlock_irq(&recovery_lock
);
1454 CIO_MSG_EVENT(4, "recovery: end\n");
1457 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1459 static void recovery_func(unsigned long data
)
1462 * We can't do our recovery in softirq context and it's not
1463 * performance critical, so we schedule it.
1465 schedule_work(&recovery_work
);
1468 static void ccw_device_schedule_recovery(void)
1470 unsigned long flags
;
1472 CIO_MSG_EVENT(4, "recovery: schedule\n");
1473 spin_lock_irqsave(&recovery_lock
, flags
);
1474 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1476 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1478 spin_unlock_irqrestore(&recovery_lock
, flags
);
1481 static int purge_fn(struct device
*dev
, void *data
)
1483 struct ccw_device
*cdev
= to_ccwdev(dev
);
1484 struct ccw_device_private
*priv
= cdev
->private;
1487 spin_lock_irq(cdev
->ccwlock
);
1488 unreg
= is_blacklisted(priv
->dev_id
.ssid
, priv
->dev_id
.devno
) &&
1489 (priv
->state
== DEV_STATE_OFFLINE
);
1490 spin_unlock_irq(cdev
->ccwlock
);
1493 if (!get_device(&cdev
->dev
))
1495 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv
->dev_id
.ssid
,
1496 priv
->dev_id
.devno
);
1497 PREPARE_WORK(&cdev
->private->kick_work
, ccw_device_call_sch_unregister
);
1498 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1501 /* Abort loop in case of pending signal. */
1502 if (signal_pending(current
))
1509 * ccw_purge_blacklisted - purge unused, blacklisted devices
1511 * Unregister all ccw devices that are offline and on the blacklist.
1513 int ccw_purge_blacklisted(void)
1515 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1516 bus_for_each_dev(&ccw_bus_type
, NULL
, NULL
, purge_fn
);
1520 static void device_set_disconnected(struct ccw_device
*cdev
)
1524 ccw_device_set_timeout(cdev
, 0);
1525 cdev
->private->flags
.fake_irb
= 0;
1526 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1528 ccw_device_schedule_recovery();
1531 void ccw_device_set_notoper(struct ccw_device
*cdev
)
1533 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1535 CIO_TRACE_EVENT(2, "notoper");
1536 CIO_TRACE_EVENT(2, dev_name(&sch
->dev
));
1537 ccw_device_set_timeout(cdev
, 0);
1538 cio_disable_subchannel(sch
);
1539 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1542 static int io_subchannel_sch_event(struct subchannel
*sch
, int slow
)
1544 int event
, ret
, disc
;
1545 unsigned long flags
;
1546 enum { NONE
, UNREGISTER
, UNREGISTER_PROBE
, REPROBE
, DISC
} action
;
1547 struct ccw_device
*cdev
;
1549 spin_lock_irqsave(sch
->lock
, flags
);
1550 cdev
= sch_get_cdev(sch
);
1551 disc
= device_is_disconnected(cdev
);
1553 /* Disconnected devices are evaluated directly only.*/
1554 spin_unlock_irqrestore(sch
->lock
, flags
);
1557 /* No interrupt after machine check - kill pending timers. */
1559 ccw_device_set_timeout(cdev
, 0);
1560 if (!disc
&& !slow
) {
1561 /* Non-disconnected devices are evaluated on the slow path. */
1562 spin_unlock_irqrestore(sch
->lock
, flags
);
1565 event
= io_subchannel_get_status(sch
);
1566 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1567 sch
->schid
.ssid
, sch
->schid
.sch_no
, event
,
1568 disc
? "disconnected" : "normal",
1569 slow
? "slow" : "fast");
1570 /* Analyze subchannel status. */
1575 /* Check if paths have become available. */
1581 /* Ask driver what to do with device. */
1582 if (io_subchannel_notify(sch
, event
))
1585 action
= UNREGISTER
;
1587 case CIO_REVALIDATE
:
1588 /* Device will be removed, so no notify necessary. */
1590 /* Reprobe because immediate unregister might block. */
1593 action
= UNREGISTER_PROBE
;
1597 /* Get device operational again. */
1601 /* Perform action. */
1605 case UNREGISTER_PROBE
:
1606 ccw_device_set_notoper(cdev
);
1607 /* Unregister device (will use subchannel lock). */
1608 spin_unlock_irqrestore(sch
->lock
, flags
);
1609 css_sch_device_unregister(sch
);
1610 spin_lock_irqsave(sch
->lock
, flags
);
1612 /* Reset intparm to zeroes. */
1613 sch
->schib
.pmcw
.intparm
= 0;
1617 ccw_device_trigger_reprobe(cdev
);
1620 device_set_disconnected(cdev
);
1625 spin_unlock_irqrestore(sch
->lock
, flags
);
1626 /* Probe if necessary. */
1627 if (action
== UNREGISTER_PROBE
)
1628 ret
= css_probe_device(sch
->schid
);
1633 #ifdef CONFIG_CCW_CONSOLE
1634 static struct ccw_device console_cdev
;
1635 static char console_cdev_name
[10] = "0.x.xxxx";
1636 static struct ccw_device_private console_private
;
1637 static int console_cdev_in_use
;
1639 static DEFINE_SPINLOCK(ccw_console_lock
);
1641 spinlock_t
* cio_get_console_lock(void)
1643 return &ccw_console_lock
;
1646 static int ccw_device_console_enable(struct ccw_device
*cdev
,
1647 struct subchannel
*sch
)
1651 /* Attach subchannel private data. */
1652 sch
->private = cio_get_console_priv();
1653 memset(sch
->private, 0, sizeof(struct io_subchannel_private
));
1654 io_subchannel_init_fields(sch
);
1655 sch
->driver
= &io_subchannel_driver
;
1656 /* Initialize the ccw_device structure. */
1657 cdev
->dev
.parent
= &sch
->dev
;
1658 rc
= io_subchannel_recog(cdev
, sch
);
1662 /* Now wait for the async. recognition to come to an end. */
1663 spin_lock_irq(cdev
->ccwlock
);
1664 while (!dev_fsm_final_state(cdev
))
1667 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1669 ccw_device_online(cdev
);
1670 while (!dev_fsm_final_state(cdev
))
1672 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1676 spin_unlock_irq(cdev
->ccwlock
);
1681 ccw_device_probe_console(void)
1683 struct subchannel
*sch
;
1686 if (xchg(&console_cdev_in_use
, 1) != 0)
1687 return ERR_PTR(-EBUSY
);
1688 sch
= cio_probe_console();
1690 console_cdev_in_use
= 0;
1691 return (void *) sch
;
1693 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1694 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1695 console_cdev
.private = &console_private
;
1696 console_private
.cdev
= &console_cdev
;
1697 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1699 cio_release_console();
1700 console_cdev_in_use
= 0;
1701 return ERR_PTR(ret
);
1703 console_cdev
.online
= 1;
1704 return &console_cdev
;
1708 const char *cio_get_console_cdev_name(struct subchannel
*sch
)
1710 snprintf(console_cdev_name
, 10, "0.%x.%04x",
1711 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1712 return (const char *)console_cdev_name
;
1717 * get ccw_device matching the busid, but only if owned by cdrv
1720 __ccwdev_check_busid(struct device
*dev
, void *id
)
1726 return (strncmp(bus_id
, dev_name(dev
), BUS_ID_SIZE
) == 0);
1731 * get_ccwdev_by_busid() - obtain device from a bus id
1732 * @cdrv: driver the device is owned by
1733 * @bus_id: bus id of the device to be searched
1735 * This function searches all devices owned by @cdrv for a device with a bus
1736 * id matching @bus_id.
1738 * If a match is found, its reference count of the found device is increased
1739 * and it is returned; else %NULL is returned.
1741 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1745 struct device_driver
*drv
;
1747 drv
= get_driver(&cdrv
->driver
);
1751 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1752 __ccwdev_check_busid
);
1755 return dev
? to_ccwdev(dev
) : NULL
;
1758 /************************** device driver handling ************************/
1760 /* This is the implementation of the ccw_driver class. The probe, remove
1761 * and release methods are initially very similar to the device_driver
1762 * implementations, with the difference that they have ccw_device
1765 * A ccw driver also contains the information that is needed for
1769 ccw_device_probe (struct device
*dev
)
1771 struct ccw_device
*cdev
= to_ccwdev(dev
);
1772 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1775 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1777 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1788 ccw_device_remove (struct device
*dev
)
1790 struct ccw_device
*cdev
= to_ccwdev(dev
);
1791 struct ccw_driver
*cdrv
= cdev
->drv
;
1798 spin_lock_irq(cdev
->ccwlock
);
1799 ret
= ccw_device_offline(cdev
);
1800 spin_unlock_irq(cdev
->ccwlock
);
1802 wait_event(cdev
->private->wait_q
,
1803 dev_fsm_final_state(cdev
));
1805 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1806 "device 0.%x.%04x\n",
1807 ret
, cdev
->private->dev_id
.ssid
,
1808 cdev
->private->dev_id
.devno
);
1810 ccw_device_set_timeout(cdev
, 0);
1815 static void ccw_device_shutdown(struct device
*dev
)
1817 struct ccw_device
*cdev
;
1819 cdev
= to_ccwdev(dev
);
1820 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1821 cdev
->drv
->shutdown(cdev
);
1825 struct bus_type ccw_bus_type
= {
1827 .match
= ccw_bus_match
,
1828 .uevent
= ccw_uevent
,
1829 .probe
= ccw_device_probe
,
1830 .remove
= ccw_device_remove
,
1831 .shutdown
= ccw_device_shutdown
,
1835 * ccw_driver_register() - register a ccw driver
1836 * @cdriver: driver to be registered
1838 * This function is mainly a wrapper around driver_register().
1840 * %0 on success and a negative error value on failure.
1842 int ccw_driver_register(struct ccw_driver
*cdriver
)
1844 struct device_driver
*drv
= &cdriver
->driver
;
1846 drv
->bus
= &ccw_bus_type
;
1847 drv
->name
= cdriver
->name
;
1848 drv
->owner
= cdriver
->owner
;
1850 return driver_register(drv
);
1854 * ccw_driver_unregister() - deregister a ccw driver
1855 * @cdriver: driver to be deregistered
1857 * This function is mainly a wrapper around driver_unregister().
1859 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
1861 driver_unregister(&cdriver
->driver
);
1864 /* Helper func for qdio. */
1865 struct subchannel_id
1866 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1868 struct subchannel
*sch
;
1870 sch
= to_subchannel(cdev
->dev
.parent
);
1874 MODULE_LICENSE("GPL");
1875 EXPORT_SYMBOL(ccw_device_set_online
);
1876 EXPORT_SYMBOL(ccw_device_set_offline
);
1877 EXPORT_SYMBOL(ccw_driver_register
);
1878 EXPORT_SYMBOL(ccw_driver_unregister
);
1879 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1880 EXPORT_SYMBOL(ccw_bus_type
);
1881 EXPORT_SYMBOL(ccw_device_work
);
1882 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);