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 struct workqueue_struct
*ccw_device_notify_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 ccw_device_notify_work
= create_singlethread_workqueue("cio_notify");
172 if (!ccw_device_notify_work
) {
173 ret
= -ENOMEM
; /* FIXME: better errno ? */
176 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
178 ret
= -ENOMEM
; /* FIXME: better errno ? */
181 if ((ret
= bus_register (&ccw_bus_type
)))
184 ret
= css_driver_register(&io_subchannel_driver
);
188 wait_event(ccw_device_init_wq
,
189 atomic_read(&ccw_device_init_count
) == 0);
190 flush_workqueue(ccw_device_work
);
194 destroy_workqueue(ccw_device_work
);
195 if (ccw_device_notify_work
)
196 destroy_workqueue(ccw_device_notify_work
);
198 destroy_workqueue(slow_path_wq
);
203 cleanup_ccw_bus_type (void)
205 css_driver_unregister(&io_subchannel_driver
);
206 bus_unregister(&ccw_bus_type
);
207 destroy_workqueue(ccw_device_notify_work
);
208 destroy_workqueue(ccw_device_work
);
211 subsys_initcall(init_ccw_bus_type
);
212 module_exit(cleanup_ccw_bus_type
);
214 /************************ device handling **************************/
217 * A ccw_device has some interfaces in sysfs in addition to the
219 * The following entries are designed to export the information which
220 * resided in 2.4 in /proc/subchannels. Subchannel and device number
221 * are obvious, so they don't have an entry :)
222 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
225 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
227 struct subchannel
*sch
= to_subchannel(dev
);
228 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
233 for (chp
= 0; chp
< 8; chp
++) {
235 if (ssd
->path_mask
& mask
)
236 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
238 ret
+= sprintf(buf
+ ret
, "00 ");
240 ret
+= sprintf (buf
+ret
, "\n");
241 return min((ssize_t
)PAGE_SIZE
, ret
);
245 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
247 struct subchannel
*sch
= to_subchannel(dev
);
248 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
250 return sprintf (buf
, "%02x %02x %02x\n",
251 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
255 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
257 struct ccw_device
*cdev
= to_ccwdev(dev
);
258 struct ccw_device_id
*id
= &(cdev
->id
);
260 if (id
->dev_type
!= 0)
261 return sprintf(buf
, "%04x/%02x\n",
262 id
->dev_type
, id
->dev_model
);
264 return sprintf(buf
, "n/a\n");
268 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
270 struct ccw_device
*cdev
= to_ccwdev(dev
);
271 struct ccw_device_id
*id
= &(cdev
->id
);
273 return sprintf(buf
, "%04x/%02x\n",
274 id
->cu_type
, id
->cu_model
);
278 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
280 struct ccw_device
*cdev
= to_ccwdev(dev
);
281 struct ccw_device_id
*id
= &(cdev
->id
);
284 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
286 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
290 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
292 struct ccw_device
*cdev
= to_ccwdev(dev
);
294 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
297 int ccw_device_is_orphan(struct ccw_device
*cdev
)
299 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
302 static void ccw_device_unregister(struct ccw_device
*cdev
)
304 if (test_and_clear_bit(1, &cdev
->private->registered
))
305 device_del(&cdev
->dev
);
308 static void ccw_device_remove_orphan_cb(struct device
*dev
)
310 struct ccw_device
*cdev
= to_ccwdev(dev
);
312 ccw_device_unregister(cdev
);
313 put_device(&cdev
->dev
);
316 static void ccw_device_remove_sch_cb(struct device
*dev
)
318 struct subchannel
*sch
;
320 sch
= to_subchannel(dev
);
321 css_sch_device_unregister(sch
);
322 /* Reset intparm to zeroes. */
323 sch
->schib
.pmcw
.intparm
= 0;
325 put_device(&sch
->dev
);
329 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
335 * Forced offline in disconnected state means
336 * 'throw away device'.
338 if (ccw_device_is_orphan(cdev
)) {
340 * Deregister ccw device.
341 * Unfortunately, we cannot do this directly from the
344 spin_lock_irqsave(cdev
->ccwlock
, flags
);
345 cdev
->private->state
= DEV_STATE_NOT_OPER
;
346 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
347 rc
= device_schedule_callback(&cdev
->dev
,
348 ccw_device_remove_orphan_cb
);
350 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
352 cdev
->private->dev_id
.ssid
,
353 cdev
->private->dev_id
.devno
);
356 /* Deregister subchannel, which will kill the ccw device. */
357 rc
= device_schedule_callback(cdev
->dev
.parent
,
358 ccw_device_remove_sch_cb
);
360 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
362 cdev
->private->dev_id
.ssid
,
363 cdev
->private->dev_id
.devno
);
367 * ccw_device_set_offline() - disable a ccw device for I/O
368 * @cdev: target ccw device
370 * This function calls the driver's set_offline() function for @cdev, if
371 * given, and then disables @cdev.
373 * %0 on success and a negative error value on failure.
375 * enabled, ccw device lock not held
377 int ccw_device_set_offline(struct ccw_device
*cdev
)
383 if (!cdev
->online
|| !cdev
->drv
)
386 if (cdev
->drv
->set_offline
) {
387 ret
= cdev
->drv
->set_offline(cdev
);
392 spin_lock_irq(cdev
->ccwlock
);
393 ret
= ccw_device_offline(cdev
);
394 if (ret
== -ENODEV
) {
395 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
396 cdev
->private->state
= DEV_STATE_OFFLINE
;
397 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
399 spin_unlock_irq(cdev
->ccwlock
);
402 spin_unlock_irq(cdev
->ccwlock
);
404 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
406 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
407 "device 0.%x.%04x\n",
408 ret
, cdev
->private->dev_id
.ssid
,
409 cdev
->private->dev_id
.devno
);
416 * ccw_device_set_online() - enable a ccw device for I/O
417 * @cdev: target ccw device
419 * This function first enables @cdev and then calls the driver's set_online()
420 * function for @cdev, if given. If set_online() returns an error, @cdev is
423 * %0 on success and a negative error value on failure.
425 * enabled, ccw device lock not held
427 int ccw_device_set_online(struct ccw_device
*cdev
)
433 if (cdev
->online
|| !cdev
->drv
)
436 spin_lock_irq(cdev
->ccwlock
);
437 ret
= ccw_device_online(cdev
);
438 spin_unlock_irq(cdev
->ccwlock
);
440 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
442 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
443 "device 0.%x.%04x\n",
444 ret
, cdev
->private->dev_id
.ssid
,
445 cdev
->private->dev_id
.devno
);
448 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
450 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
454 spin_lock_irq(cdev
->ccwlock
);
455 ret
= ccw_device_offline(cdev
);
456 spin_unlock_irq(cdev
->ccwlock
);
458 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
460 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
461 "device 0.%x.%04x\n",
462 ret
, cdev
->private->dev_id
.ssid
,
463 cdev
->private->dev_id
.devno
);
464 return (ret
== 0) ? -ENODEV
: ret
;
467 static void online_store_handle_offline(struct ccw_device
*cdev
)
469 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
470 ccw_device_remove_disconnected(cdev
);
471 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
472 ccw_device_set_offline(cdev
);
475 static int online_store_recog_and_online(struct ccw_device
*cdev
)
479 /* Do device recognition, if needed. */
480 if (cdev
->id
.cu_type
== 0) {
481 ret
= ccw_device_recognition(cdev
);
483 CIO_MSG_EVENT(0, "Couldn't start recognition "
484 "for device 0.%x.%04x (ret=%d)\n",
485 cdev
->private->dev_id
.ssid
,
486 cdev
->private->dev_id
.devno
, ret
);
489 wait_event(cdev
->private->wait_q
,
490 cdev
->private->flags
.recog_done
);
492 if (cdev
->drv
&& cdev
->drv
->set_online
)
493 ccw_device_set_online(cdev
);
496 static void online_store_handle_online(struct ccw_device
*cdev
, int force
)
500 ret
= online_store_recog_and_online(cdev
);
503 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
504 ret
= ccw_device_stlck(cdev
);
507 "ccw_device_stlck returned %d!\n", ret
);
510 if (cdev
->id
.cu_type
== 0)
511 cdev
->private->state
= DEV_STATE_NOT_OPER
;
512 online_store_recog_and_online(cdev
);
517 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
518 const char *buf
, size_t count
)
520 struct ccw_device
*cdev
= to_ccwdev(dev
);
524 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
527 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
528 atomic_set(&cdev
->private->onoff
, 0);
531 if (!strncmp(buf
, "force\n", count
)) {
537 ret
= strict_strtoul(buf
, 16, &i
);
543 online_store_handle_offline(cdev
);
547 online_store_handle_online(cdev
, force
);
555 module_put(cdev
->drv
->owner
);
556 atomic_set(&cdev
->private->onoff
, 0);
561 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
563 struct ccw_device
*cdev
= to_ccwdev(dev
);
564 struct subchannel
*sch
;
566 if (ccw_device_is_orphan(cdev
))
567 return sprintf(buf
, "no device\n");
568 switch (cdev
->private->state
) {
569 case DEV_STATE_BOXED
:
570 return sprintf(buf
, "boxed\n");
571 case DEV_STATE_DISCONNECTED
:
572 case DEV_STATE_DISCONNECTED_SENSE_ID
:
573 case DEV_STATE_NOT_OPER
:
574 sch
= to_subchannel(dev
->parent
);
576 return sprintf(buf
, "no path\n");
578 return sprintf(buf
, "no device\n");
580 /* All other states considered fine. */
581 return sprintf(buf
, "good\n");
585 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
586 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
587 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
588 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
589 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
590 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
591 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
593 static struct attribute
*io_subchannel_attrs
[] = {
594 &dev_attr_chpids
.attr
,
595 &dev_attr_pimpampom
.attr
,
599 static struct attribute_group io_subchannel_attr_group
= {
600 .attrs
= io_subchannel_attrs
,
603 static struct attribute
* ccwdev_attrs
[] = {
604 &dev_attr_devtype
.attr
,
605 &dev_attr_cutype
.attr
,
606 &dev_attr_modalias
.attr
,
607 &dev_attr_online
.attr
,
608 &dev_attr_cmb_enable
.attr
,
609 &dev_attr_availability
.attr
,
613 static struct attribute_group ccwdev_attr_group
= {
614 .attrs
= ccwdev_attrs
,
617 static struct attribute_group
*ccwdev_attr_groups
[] = {
622 /* this is a simple abstraction for device_register that sets the
623 * correct bus type and adds the bus specific files */
624 static int ccw_device_register(struct ccw_device
*cdev
)
626 struct device
*dev
= &cdev
->dev
;
629 dev
->bus
= &ccw_bus_type
;
631 if ((ret
= device_add(dev
)))
634 set_bit(1, &cdev
->private->registered
);
639 struct ccw_dev_id dev_id
;
640 struct ccw_device
* sibling
;
644 match_devno(struct device
* dev
, void * data
)
646 struct match_data
* d
= data
;
647 struct ccw_device
* cdev
;
649 cdev
= to_ccwdev(dev
);
650 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
651 !ccw_device_is_orphan(cdev
) &&
652 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
653 (cdev
!= d
->sibling
))
658 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
659 struct ccw_device
*sibling
)
662 struct match_data data
;
664 data
.dev_id
= *dev_id
;
665 data
.sibling
= sibling
;
666 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
668 return dev
? to_ccwdev(dev
) : NULL
;
671 static int match_orphan(struct device
*dev
, void *data
)
673 struct ccw_dev_id
*dev_id
;
674 struct ccw_device
*cdev
;
677 cdev
= to_ccwdev(dev
);
678 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
681 static struct ccw_device
*
682 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
683 struct ccw_dev_id
*dev_id
)
687 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
690 return dev
? to_ccwdev(dev
) : NULL
;
694 ccw_device_add_changed(struct work_struct
*work
)
696 struct ccw_device_private
*priv
;
697 struct ccw_device
*cdev
;
699 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
701 if (device_add(&cdev
->dev
)) {
702 put_device(&cdev
->dev
);
705 set_bit(1, &cdev
->private->registered
);
708 void ccw_device_do_unreg_rereg(struct work_struct
*work
)
710 struct ccw_device_private
*priv
;
711 struct ccw_device
*cdev
;
712 struct subchannel
*sch
;
714 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
716 sch
= to_subchannel(cdev
->dev
.parent
);
718 ccw_device_unregister(cdev
);
719 PREPARE_WORK(&cdev
->private->kick_work
,
720 ccw_device_add_changed
);
721 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
725 ccw_device_release(struct device
*dev
)
727 struct ccw_device
*cdev
;
729 cdev
= to_ccwdev(dev
);
730 kfree(cdev
->private);
734 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
736 struct ccw_device
*cdev
;
738 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
740 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
741 GFP_KERNEL
| GFP_DMA
);
746 return ERR_PTR(-ENOMEM
);
749 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
750 struct ccw_device
*cdev
)
752 cdev
->private->cdev
= cdev
;
753 atomic_set(&cdev
->private->onoff
, 0);
754 cdev
->dev
.parent
= &sch
->dev
;
755 cdev
->dev
.release
= ccw_device_release
;
756 INIT_WORK(&cdev
->private->kick_work
, NULL
);
757 cdev
->dev
.groups
= ccwdev_attr_groups
;
758 /* Do first half of device_register. */
759 device_initialize(&cdev
->dev
);
760 if (!get_device(&sch
->dev
)) {
761 if (cdev
->dev
.release
)
762 cdev
->dev
.release(&cdev
->dev
);
768 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
770 struct ccw_device
*cdev
;
773 cdev
= io_subchannel_allocate_dev(sch
);
775 ret
= io_subchannel_initialize_dev(sch
, cdev
);
784 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
786 static void sch_attach_device(struct subchannel
*sch
,
787 struct ccw_device
*cdev
)
789 css_update_ssd_info(sch
);
790 spin_lock_irq(sch
->lock
);
791 sch_set_cdev(sch
, cdev
);
792 cdev
->private->schid
= sch
->schid
;
793 cdev
->ccwlock
= sch
->lock
;
794 ccw_device_trigger_reprobe(cdev
);
795 spin_unlock_irq(sch
->lock
);
798 static void sch_attach_disconnected_device(struct subchannel
*sch
,
799 struct ccw_device
*cdev
)
801 struct subchannel
*other_sch
;
804 other_sch
= to_subchannel(get_device(cdev
->dev
.parent
));
805 ret
= device_move(&cdev
->dev
, &sch
->dev
);
807 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
808 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
809 cdev
->private->dev_id
.devno
, ret
);
810 put_device(&other_sch
->dev
);
813 sch_set_cdev(other_sch
, NULL
);
814 /* No need to keep a subchannel without ccw device around. */
815 css_sch_device_unregister(other_sch
);
816 put_device(&other_sch
->dev
);
817 sch_attach_device(sch
, cdev
);
820 static void sch_attach_orphaned_device(struct subchannel
*sch
,
821 struct ccw_device
*cdev
)
825 /* Try to move the ccw device to its new subchannel. */
826 ret
= device_move(&cdev
->dev
, &sch
->dev
);
828 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
829 "failed (ret=%d)!\n",
830 cdev
->private->dev_id
.ssid
,
831 cdev
->private->dev_id
.devno
, ret
);
834 sch_attach_device(sch
, cdev
);
837 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
839 struct ccw_device
*cdev
;
841 /* Need to allocate a new ccw device. */
842 cdev
= io_subchannel_create_ccwdev(sch
);
844 /* OK, we did everything we could... */
845 css_sch_device_unregister(sch
);
848 spin_lock_irq(sch
->lock
);
849 sch_set_cdev(sch
, cdev
);
850 spin_unlock_irq(sch
->lock
);
851 /* Start recognition for the new ccw device. */
852 if (io_subchannel_recog(cdev
, sch
)) {
853 spin_lock_irq(sch
->lock
);
854 sch_set_cdev(sch
, NULL
);
855 spin_unlock_irq(sch
->lock
);
856 if (cdev
->dev
.release
)
857 cdev
->dev
.release(&cdev
->dev
);
858 css_sch_device_unregister(sch
);
863 void ccw_device_move_to_orphanage(struct work_struct
*work
)
865 struct ccw_device_private
*priv
;
866 struct ccw_device
*cdev
;
867 struct ccw_device
*replacing_cdev
;
868 struct subchannel
*sch
;
870 struct channel_subsystem
*css
;
871 struct ccw_dev_id dev_id
;
873 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
875 sch
= to_subchannel(cdev
->dev
.parent
);
876 css
= to_css(sch
->dev
.parent
);
877 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
878 dev_id
.ssid
= sch
->schid
.ssid
;
881 * Move the orphaned ccw device to the orphanage so the replacing
882 * ccw device can take its place on the subchannel.
884 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
);
886 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
887 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
888 cdev
->private->dev_id
.devno
, ret
);
891 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
893 * Search for the replacing ccw device
894 * - among the disconnected devices
897 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
898 if (replacing_cdev
) {
899 sch_attach_disconnected_device(sch
, replacing_cdev
);
902 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
903 if (replacing_cdev
) {
904 sch_attach_orphaned_device(sch
, replacing_cdev
);
907 sch_create_and_recog_new_device(sch
);
911 * Register recognized device.
914 io_subchannel_register(struct work_struct
*work
)
916 struct ccw_device_private
*priv
;
917 struct ccw_device
*cdev
;
918 struct subchannel
*sch
;
922 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
924 sch
= to_subchannel(cdev
->dev
.parent
);
925 css_update_ssd_info(sch
);
927 * io_subchannel_register() will also be called after device
928 * recognition has been done for a boxed device (which will already
929 * be registered). We need to reprobe since we may now have sense id
932 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
934 ret
= device_reprobe(&cdev
->dev
);
936 /* We can't do much here. */
937 CIO_MSG_EVENT(0, "device_reprobe() returned"
938 " %d for 0.%x.%04x\n", ret
,
939 cdev
->private->dev_id
.ssid
,
940 cdev
->private->dev_id
.devno
);
945 * Now we know this subchannel will stay, we can throw
946 * our delayed uevent.
948 sch
->dev
.uevent_suppress
= 0;
949 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
950 /* make it known to the system */
951 ret
= ccw_device_register(cdev
);
953 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
954 cdev
->private->dev_id
.ssid
,
955 cdev
->private->dev_id
.devno
, ret
);
956 put_device(&cdev
->dev
);
957 spin_lock_irqsave(sch
->lock
, flags
);
958 sch_set_cdev(sch
, NULL
);
959 spin_unlock_irqrestore(sch
->lock
, flags
);
960 kfree (cdev
->private);
962 put_device(&sch
->dev
);
963 if (atomic_dec_and_test(&ccw_device_init_count
))
964 wake_up(&ccw_device_init_wq
);
967 put_device(&cdev
->dev
);
969 cdev
->private->flags
.recog_done
= 1;
970 put_device(&sch
->dev
);
971 wake_up(&cdev
->private->wait_q
);
972 if (atomic_dec_and_test(&ccw_device_init_count
))
973 wake_up(&ccw_device_init_wq
);
976 static void ccw_device_call_sch_unregister(struct work_struct
*work
)
978 struct ccw_device_private
*priv
;
979 struct ccw_device
*cdev
;
980 struct subchannel
*sch
;
982 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
984 sch
= to_subchannel(cdev
->dev
.parent
);
985 css_sch_device_unregister(sch
);
986 /* Reset intparm to zeroes. */
987 sch
->schib
.pmcw
.intparm
= 0;
989 put_device(&cdev
->dev
);
990 put_device(&sch
->dev
);
994 * subchannel recognition done. Called from the state machine.
997 io_subchannel_recog_done(struct ccw_device
*cdev
)
999 struct subchannel
*sch
;
1001 if (css_init_done
== 0) {
1002 cdev
->private->flags
.recog_done
= 1;
1005 switch (cdev
->private->state
) {
1006 case DEV_STATE_NOT_OPER
:
1007 cdev
->private->flags
.recog_done
= 1;
1008 /* Remove device found not operational. */
1009 if (!get_device(&cdev
->dev
))
1011 sch
= to_subchannel(cdev
->dev
.parent
);
1012 PREPARE_WORK(&cdev
->private->kick_work
,
1013 ccw_device_call_sch_unregister
);
1014 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1015 if (atomic_dec_and_test(&ccw_device_init_count
))
1016 wake_up(&ccw_device_init_wq
);
1018 case DEV_STATE_BOXED
:
1019 /* Device did not respond in time. */
1020 case DEV_STATE_OFFLINE
:
1022 * We can't register the device in interrupt context so
1023 * we schedule a work item.
1025 if (!get_device(&cdev
->dev
))
1027 PREPARE_WORK(&cdev
->private->kick_work
,
1028 io_subchannel_register
);
1029 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1035 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
1038 struct ccw_device_private
*priv
;
1040 sch_set_cdev(sch
, cdev
);
1041 cdev
->ccwlock
= sch
->lock
;
1043 /* Init private data. */
1044 priv
= cdev
->private;
1045 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1046 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
1047 priv
->schid
= sch
->schid
;
1048 priv
->state
= DEV_STATE_NOT_OPER
;
1049 INIT_LIST_HEAD(&priv
->cmb_list
);
1050 init_waitqueue_head(&priv
->wait_q
);
1051 init_timer(&priv
->timer
);
1053 /* Set an initial name for the device. */
1054 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
1055 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1057 /* Increase counter of devices currently in recognition. */
1058 atomic_inc(&ccw_device_init_count
);
1060 /* Start async. device sensing. */
1061 spin_lock_irq(sch
->lock
);
1062 rc
= ccw_device_recognition(cdev
);
1063 spin_unlock_irq(sch
->lock
);
1065 if (atomic_dec_and_test(&ccw_device_init_count
))
1066 wake_up(&ccw_device_init_wq
);
1071 static void ccw_device_move_to_sch(struct work_struct
*work
)
1073 struct ccw_device_private
*priv
;
1075 struct subchannel
*sch
;
1076 struct ccw_device
*cdev
;
1077 struct subchannel
*former_parent
;
1079 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1082 former_parent
= ccw_device_is_orphan(cdev
) ?
1083 NULL
: to_subchannel(get_device(cdev
->dev
.parent
));
1084 mutex_lock(&sch
->reg_mutex
);
1085 /* Try to move the ccw device to its new subchannel. */
1086 rc
= device_move(&cdev
->dev
, &sch
->dev
);
1087 mutex_unlock(&sch
->reg_mutex
);
1089 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1090 "0.%x.%04x failed (ret=%d)!\n",
1091 cdev
->private->dev_id
.ssid
,
1092 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1093 sch
->schid
.sch_no
, rc
);
1094 css_sch_device_unregister(sch
);
1097 if (former_parent
) {
1098 spin_lock_irq(former_parent
->lock
);
1099 sch_set_cdev(former_parent
, NULL
);
1100 spin_unlock_irq(former_parent
->lock
);
1101 css_sch_device_unregister(former_parent
);
1102 /* Reset intparm to zeroes. */
1103 former_parent
->schib
.pmcw
.intparm
= 0;
1104 cio_modify(former_parent
);
1106 sch_attach_device(sch
, cdev
);
1109 put_device(&former_parent
->dev
);
1110 put_device(&cdev
->dev
);
1113 static void io_subchannel_irq(struct subchannel
*sch
)
1115 struct ccw_device
*cdev
;
1117 cdev
= sch_get_cdev(sch
);
1119 CIO_TRACE_EVENT(3, "IRQ");
1120 CIO_TRACE_EVENT(3, sch
->dev
.bus_id
);
1122 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1125 static void io_subchannel_init_fields(struct subchannel
*sch
)
1127 if (cio_is_console(sch
->schid
))
1130 sch
->opm
= chp_get_sch_opm(sch
);
1131 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1132 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1134 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1135 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1136 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1137 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1138 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1139 /* Initially set up some fields in the pmcw. */
1140 sch
->schib
.pmcw
.ena
= 0;
1141 sch
->schib
.pmcw
.csense
= 1; /* concurrent sense */
1142 if ((sch
->lpm
& (sch
->lpm
- 1)) != 0)
1143 sch
->schib
.pmcw
.mp
= 1; /* multipath mode */
1144 /* clean up possible residual cmf stuff */
1145 sch
->schib
.pmcw
.mme
= 0;
1146 sch
->schib
.pmcw
.mbfc
= 0;
1147 sch
->schib
.pmcw
.mbi
= 0;
1151 static int io_subchannel_probe(struct subchannel
*sch
)
1153 struct ccw_device
*cdev
;
1155 unsigned long flags
;
1156 struct ccw_dev_id dev_id
;
1158 cdev
= sch_get_cdev(sch
);
1160 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1161 &io_subchannel_attr_group
);
1163 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1164 "attributes for subchannel "
1165 "0.%x.%04x (rc=%d)\n",
1166 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1168 * This subchannel already has an associated ccw_device.
1169 * Throw the delayed uevent for the subchannel, register
1170 * the ccw_device and exit. This happens for all early
1171 * devices, e.g. the console.
1173 sch
->dev
.uevent_suppress
= 0;
1174 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1175 cdev
->dev
.groups
= ccwdev_attr_groups
;
1176 device_initialize(&cdev
->dev
);
1177 ccw_device_register(cdev
);
1179 * Check if the device is already online. If it is
1180 * the reference count needs to be corrected
1181 * (see ccw_device_online and css_init_done for the
1184 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1185 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1186 cdev
->private->state
!= DEV_STATE_BOXED
)
1187 get_device(&cdev
->dev
);
1190 io_subchannel_init_fields(sch
);
1192 * First check if a fitting device may be found amongst the
1193 * disconnected devices or in the orphanage.
1195 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1196 dev_id
.ssid
= sch
->schid
.ssid
;
1197 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1198 &io_subchannel_attr_group
);
1201 /* Allocate I/O subchannel private data. */
1202 sch
->private = kzalloc(sizeof(struct io_subchannel_private
),
1203 GFP_KERNEL
| GFP_DMA
);
1204 if (!sch
->private) {
1208 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1210 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1214 * Schedule moving the device until when we have a registered
1215 * subchannel to move to and succeed the probe. We can
1216 * unregister later again, when the probe is through.
1218 cdev
->private->sch
= sch
;
1219 PREPARE_WORK(&cdev
->private->kick_work
,
1220 ccw_device_move_to_sch
);
1221 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1224 cdev
= io_subchannel_create_ccwdev(sch
);
1229 rc
= io_subchannel_recog(cdev
, sch
);
1231 spin_lock_irqsave(sch
->lock
, flags
);
1232 sch_set_cdev(sch
, NULL
);
1233 spin_unlock_irqrestore(sch
->lock
, flags
);
1234 if (cdev
->dev
.release
)
1235 cdev
->dev
.release(&cdev
->dev
);
1240 kfree(sch
->private);
1241 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1246 io_subchannel_remove (struct subchannel
*sch
)
1248 struct ccw_device
*cdev
;
1249 unsigned long flags
;
1251 cdev
= sch_get_cdev(sch
);
1254 /* Set ccw device to not operational and drop reference. */
1255 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1256 sch_set_cdev(sch
, NULL
);
1257 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1258 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1259 ccw_device_unregister(cdev
);
1260 put_device(&cdev
->dev
);
1261 kfree(sch
->private);
1262 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1266 static int io_subchannel_notify(struct subchannel
*sch
, int event
)
1268 struct ccw_device
*cdev
;
1270 cdev
= sch_get_cdev(sch
);
1273 return ccw_device_notify(cdev
, event
);
1276 static void io_subchannel_verify(struct subchannel
*sch
)
1278 struct ccw_device
*cdev
;
1280 cdev
= sch_get_cdev(sch
);
1282 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1285 static int check_for_io_on_path(struct subchannel
*sch
, int mask
)
1289 cc
= stsch(sch
->schid
, &sch
->schib
);
1292 if (scsw_actl(&sch
->schib
.scsw
) && sch
->schib
.pmcw
.lpum
== mask
)
1297 static void terminate_internal_io(struct subchannel
*sch
,
1298 struct ccw_device
*cdev
)
1300 if (cio_clear(sch
)) {
1301 /* Recheck device in case clear failed. */
1304 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1306 css_schedule_eval(sch
->schid
);
1309 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1310 /* Request retry of internal operation. */
1311 cdev
->private->flags
.intretry
= 1;
1314 cdev
->handler(cdev
, cdev
->private->intparm
,
1318 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1320 struct ccw_device
*cdev
;
1322 cdev
= sch_get_cdev(sch
);
1325 if (check_for_io_on_path(sch
, mask
)) {
1326 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1327 ccw_device_kill_io(cdev
);
1329 terminate_internal_io(sch
, cdev
);
1330 /* Re-start path verification. */
1331 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1334 /* trigger path verification. */
1335 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1339 static int io_subchannel_chp_event(struct subchannel
*sch
,
1340 struct chp_link
*link
, int event
)
1344 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1351 io_subchannel_terminate_path(sch
, mask
);
1356 io_subchannel_verify(sch
);
1359 if (stsch(sch
->schid
, &sch
->schib
))
1361 if (!css_sch_is_valid(&sch
->schib
))
1363 io_subchannel_terminate_path(sch
, mask
);
1366 if (stsch(sch
->schid
, &sch
->schib
))
1368 sch
->lpm
|= mask
& sch
->opm
;
1369 io_subchannel_verify(sch
);
1376 io_subchannel_shutdown(struct subchannel
*sch
)
1378 struct ccw_device
*cdev
;
1381 cdev
= sch_get_cdev(sch
);
1383 if (cio_is_console(sch
->schid
))
1385 if (!sch
->schib
.pmcw
.ena
)
1386 /* Nothing to do. */
1388 ret
= cio_disable_subchannel(sch
);
1390 /* Subchannel is disabled, we're done. */
1392 cdev
->private->state
= DEV_STATE_QUIESCE
;
1394 cdev
->handler(cdev
, cdev
->private->intparm
,
1396 ret
= ccw_device_cancel_halt_clear(cdev
);
1397 if (ret
== -EBUSY
) {
1398 ccw_device_set_timeout(cdev
, HZ
/10);
1399 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1401 cio_disable_subchannel(sch
);
1404 static int io_subchannel_get_status(struct subchannel
*sch
)
1408 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
1410 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
1411 return CIO_REVALIDATE
;
1417 static int device_is_disconnected(struct ccw_device
*cdev
)
1421 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1422 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1425 static int recovery_check(struct device
*dev
, void *data
)
1427 struct ccw_device
*cdev
= to_ccwdev(dev
);
1430 spin_lock_irq(cdev
->ccwlock
);
1431 switch (cdev
->private->state
) {
1432 case DEV_STATE_DISCONNECTED
:
1433 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1434 cdev
->private->dev_id
.ssid
,
1435 cdev
->private->dev_id
.devno
);
1436 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1439 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1443 spin_unlock_irq(cdev
->ccwlock
);
1448 static void recovery_work_func(struct work_struct
*unused
)
1452 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1454 spin_lock_irq(&recovery_lock
);
1455 if (!timer_pending(&recovery_timer
)) {
1456 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1458 mod_timer(&recovery_timer
, jiffies
+
1459 recovery_delay
[recovery_phase
] * HZ
);
1461 spin_unlock_irq(&recovery_lock
);
1463 CIO_MSG_EVENT(4, "recovery: end\n");
1466 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1468 static void recovery_func(unsigned long data
)
1471 * We can't do our recovery in softirq context and it's not
1472 * performance critical, so we schedule it.
1474 schedule_work(&recovery_work
);
1477 static void ccw_device_schedule_recovery(void)
1479 unsigned long flags
;
1481 CIO_MSG_EVENT(4, "recovery: schedule\n");
1482 spin_lock_irqsave(&recovery_lock
, flags
);
1483 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1485 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1487 spin_unlock_irqrestore(&recovery_lock
, flags
);
1490 static void device_set_disconnected(struct ccw_device
*cdev
)
1494 ccw_device_set_timeout(cdev
, 0);
1495 cdev
->private->flags
.fake_irb
= 0;
1496 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1498 ccw_device_schedule_recovery();
1501 static int io_subchannel_sch_event(struct subchannel
*sch
, int slow
)
1503 int event
, ret
, disc
;
1504 unsigned long flags
;
1505 enum { NONE
, UNREGISTER
, UNREGISTER_PROBE
, REPROBE
} action
;
1506 struct ccw_device
*cdev
;
1508 spin_lock_irqsave(sch
->lock
, flags
);
1509 cdev
= sch_get_cdev(sch
);
1510 disc
= device_is_disconnected(cdev
);
1512 /* Disconnected devices are evaluated directly only.*/
1513 spin_unlock_irqrestore(sch
->lock
, flags
);
1516 /* No interrupt after machine check - kill pending timers. */
1518 ccw_device_set_timeout(cdev
, 0);
1519 if (!disc
&& !slow
) {
1520 /* Non-disconnected devices are evaluated on the slow path. */
1521 spin_unlock_irqrestore(sch
->lock
, flags
);
1524 event
= io_subchannel_get_status(sch
);
1525 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1526 sch
->schid
.ssid
, sch
->schid
.sch_no
, event
,
1527 disc
? "disconnected" : "normal",
1528 slow
? "slow" : "fast");
1529 /* Analyze subchannel status. */
1534 /* Check if paths have become available. */
1540 /* Prevent unwanted effects when opening lock. */
1541 cio_disable_subchannel(sch
);
1542 device_set_disconnected(cdev
);
1543 /* Ask driver what to do with device. */
1544 action
= UNREGISTER
;
1545 spin_unlock_irqrestore(sch
->lock
, flags
);
1546 ret
= io_subchannel_notify(sch
, event
);
1547 spin_lock_irqsave(sch
->lock
, flags
);
1551 case CIO_REVALIDATE
:
1552 /* Device will be removed, so no notify necessary. */
1554 /* Reprobe because immediate unregister might block. */
1557 action
= UNREGISTER_PROBE
;
1561 /* Get device operational again. */
1565 /* Perform action. */
1569 case UNREGISTER_PROBE
:
1570 /* Unregister device (will use subchannel lock). */
1571 spin_unlock_irqrestore(sch
->lock
, flags
);
1572 css_sch_device_unregister(sch
);
1573 spin_lock_irqsave(sch
->lock
, flags
);
1575 /* Reset intparm to zeroes. */
1576 sch
->schib
.pmcw
.intparm
= 0;
1580 ccw_device_trigger_reprobe(cdev
);
1585 spin_unlock_irqrestore(sch
->lock
, flags
);
1586 /* Probe if necessary. */
1587 if (action
== UNREGISTER_PROBE
)
1588 ret
= css_probe_device(sch
->schid
);
1593 #ifdef CONFIG_CCW_CONSOLE
1594 static struct ccw_device console_cdev
;
1595 static struct ccw_device_private console_private
;
1596 static int console_cdev_in_use
;
1598 static DEFINE_SPINLOCK(ccw_console_lock
);
1600 spinlock_t
* cio_get_console_lock(void)
1602 return &ccw_console_lock
;
1605 static int ccw_device_console_enable(struct ccw_device
*cdev
,
1606 struct subchannel
*sch
)
1610 /* Attach subchannel private data. */
1611 sch
->private = cio_get_console_priv();
1612 memset(sch
->private, 0, sizeof(struct io_subchannel_private
));
1613 io_subchannel_init_fields(sch
);
1614 sch
->driver
= &io_subchannel_driver
;
1615 /* Initialize the ccw_device structure. */
1616 cdev
->dev
.parent
= &sch
->dev
;
1617 rc
= io_subchannel_recog(cdev
, sch
);
1621 /* Now wait for the async. recognition to come to an end. */
1622 spin_lock_irq(cdev
->ccwlock
);
1623 while (!dev_fsm_final_state(cdev
))
1626 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1628 ccw_device_online(cdev
);
1629 while (!dev_fsm_final_state(cdev
))
1631 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1635 spin_unlock_irq(cdev
->ccwlock
);
1640 ccw_device_probe_console(void)
1642 struct subchannel
*sch
;
1645 if (xchg(&console_cdev_in_use
, 1) != 0)
1646 return ERR_PTR(-EBUSY
);
1647 sch
= cio_probe_console();
1649 console_cdev_in_use
= 0;
1650 return (void *) sch
;
1652 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1653 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1654 console_cdev
.private = &console_private
;
1655 console_private
.cdev
= &console_cdev
;
1656 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1658 cio_release_console();
1659 console_cdev_in_use
= 0;
1660 return ERR_PTR(ret
);
1662 console_cdev
.online
= 1;
1663 return &console_cdev
;
1668 * get ccw_device matching the busid, but only if owned by cdrv
1671 __ccwdev_check_busid(struct device
*dev
, void *id
)
1677 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1682 * get_ccwdev_by_busid() - obtain device from a bus id
1683 * @cdrv: driver the device is owned by
1684 * @bus_id: bus id of the device to be searched
1686 * This function searches all devices owned by @cdrv for a device with a bus
1687 * id matching @bus_id.
1689 * If a match is found, its reference count of the found device is increased
1690 * and it is returned; else %NULL is returned.
1692 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1696 struct device_driver
*drv
;
1698 drv
= get_driver(&cdrv
->driver
);
1702 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1703 __ccwdev_check_busid
);
1706 return dev
? to_ccwdev(dev
) : NULL
;
1709 /************************** device driver handling ************************/
1711 /* This is the implementation of the ccw_driver class. The probe, remove
1712 * and release methods are initially very similar to the device_driver
1713 * implementations, with the difference that they have ccw_device
1716 * A ccw driver also contains the information that is needed for
1720 ccw_device_probe (struct device
*dev
)
1722 struct ccw_device
*cdev
= to_ccwdev(dev
);
1723 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1726 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1728 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1739 ccw_device_remove (struct device
*dev
)
1741 struct ccw_device
*cdev
= to_ccwdev(dev
);
1742 struct ccw_driver
*cdrv
= cdev
->drv
;
1749 spin_lock_irq(cdev
->ccwlock
);
1750 ret
= ccw_device_offline(cdev
);
1751 spin_unlock_irq(cdev
->ccwlock
);
1753 wait_event(cdev
->private->wait_q
,
1754 dev_fsm_final_state(cdev
));
1756 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1757 "device 0.%x.%04x\n",
1758 ret
, cdev
->private->dev_id
.ssid
,
1759 cdev
->private->dev_id
.devno
);
1761 ccw_device_set_timeout(cdev
, 0);
1766 static void ccw_device_shutdown(struct device
*dev
)
1768 struct ccw_device
*cdev
;
1770 cdev
= to_ccwdev(dev
);
1771 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1772 cdev
->drv
->shutdown(cdev
);
1776 struct bus_type ccw_bus_type
= {
1778 .match
= ccw_bus_match
,
1779 .uevent
= ccw_uevent
,
1780 .probe
= ccw_device_probe
,
1781 .remove
= ccw_device_remove
,
1782 .shutdown
= ccw_device_shutdown
,
1786 * ccw_driver_register() - register a ccw driver
1787 * @cdriver: driver to be registered
1789 * This function is mainly a wrapper around driver_register().
1791 * %0 on success and a negative error value on failure.
1793 int ccw_driver_register(struct ccw_driver
*cdriver
)
1795 struct device_driver
*drv
= &cdriver
->driver
;
1797 drv
->bus
= &ccw_bus_type
;
1798 drv
->name
= cdriver
->name
;
1799 drv
->owner
= cdriver
->owner
;
1801 return driver_register(drv
);
1805 * ccw_driver_unregister() - deregister a ccw driver
1806 * @cdriver: driver to be deregistered
1808 * This function is mainly a wrapper around driver_unregister().
1810 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
1812 driver_unregister(&cdriver
->driver
);
1815 /* Helper func for qdio. */
1816 struct subchannel_id
1817 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1819 struct subchannel
*sch
;
1821 sch
= to_subchannel(cdev
->dev
.parent
);
1825 MODULE_LICENSE("GPL");
1826 EXPORT_SYMBOL(ccw_device_set_online
);
1827 EXPORT_SYMBOL(ccw_device_set_offline
);
1828 EXPORT_SYMBOL(ccw_driver_register
);
1829 EXPORT_SYMBOL(ccw_driver_unregister
);
1830 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1831 EXPORT_SYMBOL(ccw_bus_type
);
1832 EXPORT_SYMBOL(ccw_device_work
);
1833 EXPORT_SYMBOL(ccw_device_notify_work
);
1834 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);