2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
21 #include <asm/ccwdev.h>
23 #include <asm/param.h> /* HZ */
26 #include "cio_debug.h"
31 /******************* bus type handling ***********************/
33 /* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
38 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
40 struct ccw_device
*cdev
= to_ccwdev(dev
);
41 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
42 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
47 found
= ccw_device_id_match(ids
, &cdev
->id
);
51 cdev
->id
.driver_info
= found
->driver_info
;
56 /* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
59 static int snprint_alias(char *buf
, size_t size
, const char *prefix
,
60 struct ccw_device_id
*id
, const char *suffix
)
64 len
= snprintf(buf
, size
, "%sccw:t%04Xm%02X", prefix
, id
->cu_type
,
71 if (id
->dev_type
!= 0)
72 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
73 id
->dev_model
, suffix
);
75 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
80 /* Set up environment variables for ccw device uevent. Return 0 on success,
81 * non-zero otherwise. */
82 static int ccw_uevent(struct device
*dev
, char **envp
, int num_envp
,
83 char *buffer
, int buffer_size
)
85 struct ccw_device
*cdev
= to_ccwdev(dev
);
86 struct ccw_device_id
*id
= &(cdev
->id
);
91 len
= snprintf(buffer
, buffer_size
, "CU_TYPE=%04X", id
->cu_type
) + 1;
92 if (len
> buffer_size
|| i
>= num_envp
)
99 len
= snprintf(buffer
, buffer_size
, "CU_MODEL=%02X", id
->cu_model
) + 1;
100 if (len
> buffer_size
|| i
>= num_envp
)
106 /* The next two can be zero, that's ok for us */
108 len
= snprintf(buffer
, buffer_size
, "DEV_TYPE=%04X", id
->dev_type
) + 1;
109 if (len
> buffer_size
|| i
>= num_envp
)
116 len
= snprintf(buffer
, buffer_size
, "DEV_MODEL=%02X",
117 (unsigned char) id
->dev_model
) + 1;
118 if (len
> buffer_size
|| i
>= num_envp
)
125 len
= snprint_alias(buffer
, buffer_size
, "MODALIAS=", id
, "") + 1;
126 if (len
> buffer_size
|| i
>= num_envp
)
137 struct bus_type ccw_bus_type
;
139 static int io_subchannel_probe (struct subchannel
*);
140 static int io_subchannel_remove (struct subchannel
*);
141 static int io_subchannel_notify(struct device
*, int);
142 static void io_subchannel_verify(struct device
*);
143 static void io_subchannel_ioterm(struct device
*);
144 static void io_subchannel_shutdown(struct subchannel
*);
146 struct css_driver io_subchannel_driver
= {
147 .subchannel_type
= SUBCHANNEL_TYPE_IO
,
149 .name
= "io_subchannel",
150 .bus
= &css_bus_type
,
152 .irq
= io_subchannel_irq
,
153 .notify
= io_subchannel_notify
,
154 .verify
= io_subchannel_verify
,
155 .termination
= io_subchannel_ioterm
,
156 .probe
= io_subchannel_probe
,
157 .remove
= io_subchannel_remove
,
158 .shutdown
= io_subchannel_shutdown
,
161 struct workqueue_struct
*ccw_device_work
;
162 struct workqueue_struct
*ccw_device_notify_work
;
163 wait_queue_head_t ccw_device_init_wq
;
164 atomic_t ccw_device_init_count
;
167 init_ccw_bus_type (void)
171 init_waitqueue_head(&ccw_device_init_wq
);
172 atomic_set(&ccw_device_init_count
, 0);
174 ccw_device_work
= create_singlethread_workqueue("cio");
175 if (!ccw_device_work
)
176 return -ENOMEM
; /* FIXME: better errno ? */
177 ccw_device_notify_work
= create_singlethread_workqueue("cio_notify");
178 if (!ccw_device_notify_work
) {
179 ret
= -ENOMEM
; /* FIXME: better errno ? */
182 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
184 ret
= -ENOMEM
; /* FIXME: better errno ? */
187 if ((ret
= bus_register (&ccw_bus_type
)))
190 if ((ret
= driver_register(&io_subchannel_driver
.drv
)))
193 wait_event(ccw_device_init_wq
,
194 atomic_read(&ccw_device_init_count
) == 0);
195 flush_workqueue(ccw_device_work
);
199 destroy_workqueue(ccw_device_work
);
200 if (ccw_device_notify_work
)
201 destroy_workqueue(ccw_device_notify_work
);
203 destroy_workqueue(slow_path_wq
);
208 cleanup_ccw_bus_type (void)
210 driver_unregister(&io_subchannel_driver
.drv
);
211 bus_unregister(&ccw_bus_type
);
212 destroy_workqueue(ccw_device_notify_work
);
213 destroy_workqueue(ccw_device_work
);
216 subsys_initcall(init_ccw_bus_type
);
217 module_exit(cleanup_ccw_bus_type
);
219 /************************ device handling **************************/
222 * A ccw_device has some interfaces in sysfs in addition to the
224 * The following entries are designed to export the information which
225 * resided in 2.4 in /proc/subchannels. Subchannel and device number
226 * are obvious, so they don't have an entry :)
227 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
230 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
232 struct subchannel
*sch
= to_subchannel(dev
);
233 struct ssd_info
*ssd
= &sch
->ssd_info
;
238 for (chp
= 0; chp
< 8; chp
++)
239 ret
+= sprintf (buf
+ret
, "%02x ", ssd
->chpid
[chp
]);
241 ret
+= sprintf (buf
, "n/a");
242 ret
+= sprintf (buf
+ret
, "\n");
243 return min((ssize_t
)PAGE_SIZE
, ret
);
247 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
249 struct subchannel
*sch
= to_subchannel(dev
);
250 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
252 return sprintf (buf
, "%02x %02x %02x\n",
253 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
257 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
259 struct ccw_device
*cdev
= to_ccwdev(dev
);
260 struct ccw_device_id
*id
= &(cdev
->id
);
262 if (id
->dev_type
!= 0)
263 return sprintf(buf
, "%04x/%02x\n",
264 id
->dev_type
, id
->dev_model
);
266 return sprintf(buf
, "n/a\n");
270 cutype_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
);
275 return sprintf(buf
, "%04x/%02x\n",
276 id
->cu_type
, id
->cu_model
);
280 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
282 struct ccw_device
*cdev
= to_ccwdev(dev
);
283 struct ccw_device_id
*id
= &(cdev
->id
);
286 len
= snprint_alias(buf
, PAGE_SIZE
, "", id
, "\n") + 1;
288 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
292 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
294 struct ccw_device
*cdev
= to_ccwdev(dev
);
296 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
299 int ccw_device_is_orphan(struct ccw_device
*cdev
)
301 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
304 static void ccw_device_unregister(struct work_struct
*work
)
306 struct ccw_device_private
*priv
;
307 struct ccw_device
*cdev
;
309 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
311 if (test_and_clear_bit(1, &cdev
->private->registered
))
312 device_unregister(&cdev
->dev
);
313 put_device(&cdev
->dev
);
317 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
319 struct subchannel
*sch
;
322 * Forced offline in disconnected state means
323 * 'throw away device'.
325 if (ccw_device_is_orphan(cdev
)) {
326 /* Deregister ccw device. */
327 spin_lock_irqsave(cdev
->ccwlock
, flags
);
328 cdev
->private->state
= DEV_STATE_NOT_OPER
;
329 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
330 if (get_device(&cdev
->dev
)) {
331 PREPARE_WORK(&cdev
->private->kick_work
,
332 ccw_device_unregister
);
333 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
337 sch
= to_subchannel(cdev
->dev
.parent
);
338 css_sch_device_unregister(sch
);
339 /* Reset intparm to zeroes. */
340 sch
->schib
.pmcw
.intparm
= 0;
342 put_device(&sch
->dev
);
346 ccw_device_set_offline(struct ccw_device
*cdev
)
352 if (!cdev
->online
|| !cdev
->drv
)
355 if (cdev
->drv
->set_offline
) {
356 ret
= cdev
->drv
->set_offline(cdev
);
361 spin_lock_irq(cdev
->ccwlock
);
362 ret
= ccw_device_offline(cdev
);
363 if (ret
== -ENODEV
) {
364 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
365 cdev
->private->state
= DEV_STATE_OFFLINE
;
366 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
368 spin_unlock_irq(cdev
->ccwlock
);
371 spin_unlock_irq(cdev
->ccwlock
);
373 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
375 pr_debug("ccw_device_offline returned %d, device %s\n",
376 ret
, cdev
->dev
.bus_id
);
383 ccw_device_set_online(struct ccw_device
*cdev
)
389 if (cdev
->online
|| !cdev
->drv
)
392 spin_lock_irq(cdev
->ccwlock
);
393 ret
= ccw_device_online(cdev
);
394 spin_unlock_irq(cdev
->ccwlock
);
396 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
398 pr_debug("ccw_device_online returned %d, device %s\n",
399 ret
, cdev
->dev
.bus_id
);
402 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
404 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
408 spin_lock_irq(cdev
->ccwlock
);
409 ret
= ccw_device_offline(cdev
);
410 spin_unlock_irq(cdev
->ccwlock
);
412 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
414 pr_debug("ccw_device_offline returned %d, device %s\n",
415 ret
, cdev
->dev
.bus_id
);
416 return (ret
== 0) ? -ENODEV
: ret
;
420 online_store (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
422 struct ccw_device
*cdev
= to_ccwdev(dev
);
426 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
429 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
430 atomic_set(&cdev
->private->onoff
, 0);
433 if (!strncmp(buf
, "force\n", count
)) {
438 i
= simple_strtoul(buf
, &tmp
, 16);
441 /* Do device recognition, if needed. */
442 if (cdev
->id
.cu_type
== 0) {
443 ret
= ccw_device_recognition(cdev
);
445 printk(KERN_WARNING
"Couldn't start recognition "
446 "for device %s (ret=%d)\n",
447 cdev
->dev
.bus_id
, ret
);
450 wait_event(cdev
->private->wait_q
,
451 cdev
->private->flags
.recog_done
);
453 if (cdev
->drv
&& cdev
->drv
->set_online
)
454 ccw_device_set_online(cdev
);
456 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
457 ccw_device_remove_disconnected(cdev
);
458 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
459 ccw_device_set_offline(cdev
);
461 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
462 ret
= ccw_device_stlck(cdev
);
464 printk(KERN_WARNING
"ccw_device_stlck for device %s "
465 "returned %d!\n", cdev
->dev
.bus_id
, ret
);
468 /* Do device recognition, if needed. */
469 if (cdev
->id
.cu_type
== 0) {
470 cdev
->private->state
= DEV_STATE_NOT_OPER
;
471 ret
= ccw_device_recognition(cdev
);
473 printk(KERN_WARNING
"Couldn't start recognition "
474 "for device %s (ret=%d)\n",
475 cdev
->dev
.bus_id
, ret
);
478 wait_event(cdev
->private->wait_q
,
479 cdev
->private->flags
.recog_done
);
481 if (cdev
->drv
&& cdev
->drv
->set_online
)
482 ccw_device_set_online(cdev
);
486 module_put(cdev
->drv
->owner
);
487 atomic_set(&cdev
->private->onoff
, 0);
492 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
494 struct ccw_device
*cdev
= to_ccwdev(dev
);
495 struct subchannel
*sch
;
497 if (ccw_device_is_orphan(cdev
))
498 return sprintf(buf
, "no device\n");
499 switch (cdev
->private->state
) {
500 case DEV_STATE_BOXED
:
501 return sprintf(buf
, "boxed\n");
502 case DEV_STATE_DISCONNECTED
:
503 case DEV_STATE_DISCONNECTED_SENSE_ID
:
504 case DEV_STATE_NOT_OPER
:
505 sch
= to_subchannel(dev
->parent
);
507 return sprintf(buf
, "no path\n");
509 return sprintf(buf
, "no device\n");
511 /* All other states considered fine. */
512 return sprintf(buf
, "good\n");
516 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
517 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
518 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
519 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
520 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
521 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
522 extern struct device_attribute dev_attr_cmb_enable
;
523 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
525 static struct attribute
* subch_attrs
[] = {
526 &dev_attr_chpids
.attr
,
527 &dev_attr_pimpampom
.attr
,
531 static struct attribute_group subch_attr_group
= {
532 .attrs
= subch_attrs
,
535 struct attribute_group
*subch_attr_groups
[] = {
540 static struct attribute
* ccwdev_attrs
[] = {
541 &dev_attr_devtype
.attr
,
542 &dev_attr_cutype
.attr
,
543 &dev_attr_modalias
.attr
,
544 &dev_attr_online
.attr
,
545 &dev_attr_cmb_enable
.attr
,
546 &dev_attr_availability
.attr
,
550 static struct attribute_group ccwdev_attr_group
= {
551 .attrs
= ccwdev_attrs
,
555 device_add_files (struct device
*dev
)
557 return sysfs_create_group(&dev
->kobj
, &ccwdev_attr_group
);
561 device_remove_files(struct device
*dev
)
563 sysfs_remove_group(&dev
->kobj
, &ccwdev_attr_group
);
566 /* this is a simple abstraction for device_register that sets the
567 * correct bus type and adds the bus specific files */
568 static int ccw_device_register(struct ccw_device
*cdev
)
570 struct device
*dev
= &cdev
->dev
;
573 dev
->bus
= &ccw_bus_type
;
575 if ((ret
= device_add(dev
)))
578 set_bit(1, &cdev
->private->registered
);
579 if ((ret
= device_add_files(dev
))) {
580 if (test_and_clear_bit(1, &cdev
->private->registered
))
587 struct ccw_dev_id dev_id
;
588 struct ccw_device
* sibling
;
592 match_devno(struct device
* dev
, void * data
)
594 struct match_data
* d
= data
;
595 struct ccw_device
* cdev
;
597 cdev
= to_ccwdev(dev
);
598 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
599 !ccw_device_is_orphan(cdev
) &&
600 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
601 (cdev
!= d
->sibling
))
606 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
607 struct ccw_device
*sibling
)
610 struct match_data data
;
612 data
.dev_id
= *dev_id
;
613 data
.sibling
= sibling
;
614 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
616 return dev
? to_ccwdev(dev
) : NULL
;
619 static int match_orphan(struct device
*dev
, void *data
)
621 struct ccw_dev_id
*dev_id
;
622 struct ccw_device
*cdev
;
625 cdev
= to_ccwdev(dev
);
626 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
629 static struct ccw_device
*
630 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
631 struct ccw_dev_id
*dev_id
)
635 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
638 return dev
? to_ccwdev(dev
) : NULL
;
642 ccw_device_add_changed(struct work_struct
*work
)
644 struct ccw_device_private
*priv
;
645 struct ccw_device
*cdev
;
647 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
649 if (device_add(&cdev
->dev
)) {
650 put_device(&cdev
->dev
);
653 set_bit(1, &cdev
->private->registered
);
654 if (device_add_files(&cdev
->dev
)) {
655 if (test_and_clear_bit(1, &cdev
->private->registered
))
656 device_unregister(&cdev
->dev
);
660 void ccw_device_do_unreg_rereg(struct work_struct
*work
)
662 struct ccw_device_private
*priv
;
663 struct ccw_device
*cdev
;
664 struct subchannel
*sch
;
666 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
668 sch
= to_subchannel(cdev
->dev
.parent
);
670 device_remove_files(&cdev
->dev
);
671 if (test_and_clear_bit(1, &cdev
->private->registered
))
672 device_del(&cdev
->dev
);
673 PREPARE_WORK(&cdev
->private->kick_work
,
674 ccw_device_add_changed
);
675 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
679 ccw_device_release(struct device
*dev
)
681 struct ccw_device
*cdev
;
683 cdev
= to_ccwdev(dev
);
684 kfree(cdev
->private);
688 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
690 struct ccw_device
*cdev
;
692 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
694 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
695 GFP_KERNEL
| GFP_DMA
);
700 return ERR_PTR(-ENOMEM
);
703 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
704 struct ccw_device
*cdev
)
706 cdev
->private->cdev
= cdev
;
707 atomic_set(&cdev
->private->onoff
, 0);
708 cdev
->dev
.parent
= &sch
->dev
;
709 cdev
->dev
.release
= ccw_device_release
;
710 INIT_LIST_HEAD(&cdev
->private->kick_work
.entry
);
711 /* Do first half of device_register. */
712 device_initialize(&cdev
->dev
);
713 if (!get_device(&sch
->dev
)) {
714 if (cdev
->dev
.release
)
715 cdev
->dev
.release(&cdev
->dev
);
721 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
723 struct ccw_device
*cdev
;
726 cdev
= io_subchannel_allocate_dev(sch
);
728 ret
= io_subchannel_initialize_dev(sch
, cdev
);
737 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
739 static void sch_attach_device(struct subchannel
*sch
,
740 struct ccw_device
*cdev
)
742 spin_lock_irq(sch
->lock
);
743 sch
->dev
.driver_data
= cdev
;
744 cdev
->private->schid
= sch
->schid
;
745 cdev
->ccwlock
= sch
->lock
;
746 device_trigger_reprobe(sch
);
747 spin_unlock_irq(sch
->lock
);
750 static void sch_attach_disconnected_device(struct subchannel
*sch
,
751 struct ccw_device
*cdev
)
753 struct subchannel
*other_sch
;
756 other_sch
= to_subchannel(get_device(cdev
->dev
.parent
));
757 ret
= device_move(&cdev
->dev
, &sch
->dev
);
759 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
760 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
761 cdev
->private->dev_id
.devno
, ret
);
762 put_device(&other_sch
->dev
);
765 other_sch
->dev
.driver_data
= NULL
;
766 /* No need to keep a subchannel without ccw device around. */
767 css_sch_device_unregister(other_sch
);
768 put_device(&other_sch
->dev
);
769 sch_attach_device(sch
, cdev
);
772 static void sch_attach_orphaned_device(struct subchannel
*sch
,
773 struct ccw_device
*cdev
)
777 /* Try to move the ccw device to its new subchannel. */
778 ret
= device_move(&cdev
->dev
, &sch
->dev
);
780 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
781 "failed (ret=%d)!\n",
782 cdev
->private->dev_id
.ssid
,
783 cdev
->private->dev_id
.devno
, ret
);
786 sch_attach_device(sch
, cdev
);
789 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
791 struct ccw_device
*cdev
;
793 /* Need to allocate a new ccw device. */
794 cdev
= io_subchannel_create_ccwdev(sch
);
796 /* OK, we did everything we could... */
797 css_sch_device_unregister(sch
);
800 spin_lock_irq(sch
->lock
);
801 sch
->dev
.driver_data
= cdev
;
802 spin_unlock_irq(sch
->lock
);
803 /* Start recognition for the new ccw device. */
804 if (io_subchannel_recog(cdev
, sch
)) {
805 spin_lock_irq(sch
->lock
);
806 sch
->dev
.driver_data
= NULL
;
807 spin_unlock_irq(sch
->lock
);
808 if (cdev
->dev
.release
)
809 cdev
->dev
.release(&cdev
->dev
);
810 css_sch_device_unregister(sch
);
815 void ccw_device_move_to_orphanage(struct work_struct
*work
)
817 struct ccw_device_private
*priv
;
818 struct ccw_device
*cdev
;
819 struct ccw_device
*replacing_cdev
;
820 struct subchannel
*sch
;
822 struct channel_subsystem
*css
;
823 struct ccw_dev_id dev_id
;
825 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
827 sch
= to_subchannel(cdev
->dev
.parent
);
828 css
= to_css(sch
->dev
.parent
);
829 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
830 dev_id
.ssid
= sch
->schid
.ssid
;
833 * Move the orphaned ccw device to the orphanage so the replacing
834 * ccw device can take its place on the subchannel.
836 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
);
838 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
839 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
840 cdev
->private->dev_id
.devno
, ret
);
843 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
845 * Search for the replacing ccw device
846 * - among the disconnected devices
849 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
850 if (replacing_cdev
) {
851 sch_attach_disconnected_device(sch
, replacing_cdev
);
854 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
855 if (replacing_cdev
) {
856 sch_attach_orphaned_device(sch
, replacing_cdev
);
859 sch_create_and_recog_new_device(sch
);
863 * Register recognized device.
866 io_subchannel_register(struct work_struct
*work
)
868 struct ccw_device_private
*priv
;
869 struct ccw_device
*cdev
;
870 struct subchannel
*sch
;
874 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
876 sch
= to_subchannel(cdev
->dev
.parent
);
879 * io_subchannel_register() will also be called after device
880 * recognition has been done for a boxed device (which will already
881 * be registered). We need to reprobe since we may now have sense id
884 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
886 ret
= device_reprobe(&cdev
->dev
);
888 /* We can't do much here. */
889 dev_info(&cdev
->dev
, "device_reprobe() returned"
894 /* make it known to the system */
895 ret
= ccw_device_register(cdev
);
897 printk (KERN_WARNING
"%s: could not register %s\n",
898 __func__
, cdev
->dev
.bus_id
);
899 put_device(&cdev
->dev
);
900 spin_lock_irqsave(sch
->lock
, flags
);
901 sch
->dev
.driver_data
= NULL
;
902 spin_unlock_irqrestore(sch
->lock
, flags
);
903 kfree (cdev
->private);
905 put_device(&sch
->dev
);
906 if (atomic_dec_and_test(&ccw_device_init_count
))
907 wake_up(&ccw_device_init_wq
);
910 put_device(&cdev
->dev
);
912 cdev
->private->flags
.recog_done
= 1;
913 put_device(&sch
->dev
);
914 wake_up(&cdev
->private->wait_q
);
915 if (atomic_dec_and_test(&ccw_device_init_count
))
916 wake_up(&ccw_device_init_wq
);
920 ccw_device_call_sch_unregister(struct work_struct
*work
)
922 struct ccw_device_private
*priv
;
923 struct ccw_device
*cdev
;
924 struct subchannel
*sch
;
926 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
928 sch
= to_subchannel(cdev
->dev
.parent
);
929 css_sch_device_unregister(sch
);
930 /* Reset intparm to zeroes. */
931 sch
->schib
.pmcw
.intparm
= 0;
933 put_device(&cdev
->dev
);
934 put_device(&sch
->dev
);
938 * subchannel recognition done. Called from the state machine.
941 io_subchannel_recog_done(struct ccw_device
*cdev
)
943 struct subchannel
*sch
;
945 if (css_init_done
== 0) {
946 cdev
->private->flags
.recog_done
= 1;
949 switch (cdev
->private->state
) {
950 case DEV_STATE_NOT_OPER
:
951 cdev
->private->flags
.recog_done
= 1;
952 /* Remove device found not operational. */
953 if (!get_device(&cdev
->dev
))
955 sch
= to_subchannel(cdev
->dev
.parent
);
956 PREPARE_WORK(&cdev
->private->kick_work
,
957 ccw_device_call_sch_unregister
);
958 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
959 if (atomic_dec_and_test(&ccw_device_init_count
))
960 wake_up(&ccw_device_init_wq
);
962 case DEV_STATE_BOXED
:
963 /* Device did not respond in time. */
964 case DEV_STATE_OFFLINE
:
966 * We can't register the device in interrupt context so
967 * we schedule a work item.
969 if (!get_device(&cdev
->dev
))
971 PREPARE_WORK(&cdev
->private->kick_work
,
972 io_subchannel_register
);
973 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
979 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
982 struct ccw_device_private
*priv
;
984 sch
->dev
.driver_data
= cdev
;
985 sch
->driver
= &io_subchannel_driver
;
986 cdev
->ccwlock
= sch
->lock
;
988 /* Init private data. */
989 priv
= cdev
->private;
990 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
991 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
992 priv
->schid
= sch
->schid
;
993 priv
->state
= DEV_STATE_NOT_OPER
;
994 INIT_LIST_HEAD(&priv
->cmb_list
);
995 init_waitqueue_head(&priv
->wait_q
);
996 init_timer(&priv
->timer
);
998 /* Set an initial name for the device. */
999 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
1000 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1002 /* Increase counter of devices currently in recognition. */
1003 atomic_inc(&ccw_device_init_count
);
1005 /* Start async. device sensing. */
1006 spin_lock_irq(sch
->lock
);
1007 rc
= ccw_device_recognition(cdev
);
1008 spin_unlock_irq(sch
->lock
);
1010 if (atomic_dec_and_test(&ccw_device_init_count
))
1011 wake_up(&ccw_device_init_wq
);
1016 static void ccw_device_move_to_sch(struct work_struct
*work
)
1018 struct ccw_device_private
*priv
;
1020 struct subchannel
*sch
;
1021 struct ccw_device
*cdev
;
1022 struct subchannel
*former_parent
;
1024 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1027 former_parent
= ccw_device_is_orphan(cdev
) ?
1028 NULL
: to_subchannel(get_device(cdev
->dev
.parent
));
1029 mutex_lock(&sch
->reg_mutex
);
1030 /* Try to move the ccw device to its new subchannel. */
1031 rc
= device_move(&cdev
->dev
, &sch
->dev
);
1032 mutex_unlock(&sch
->reg_mutex
);
1034 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1035 "0.%x.%04x failed (ret=%d)!\n",
1036 cdev
->private->dev_id
.ssid
,
1037 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1038 sch
->schid
.sch_no
, rc
);
1039 css_sch_device_unregister(sch
);
1042 if (former_parent
) {
1043 spin_lock_irq(former_parent
->lock
);
1044 former_parent
->dev
.driver_data
= NULL
;
1045 spin_unlock_irq(former_parent
->lock
);
1046 css_sch_device_unregister(former_parent
);
1047 /* Reset intparm to zeroes. */
1048 former_parent
->schib
.pmcw
.intparm
= 0;
1049 cio_modify(former_parent
);
1051 sch_attach_device(sch
, cdev
);
1054 put_device(&former_parent
->dev
);
1055 put_device(&cdev
->dev
);
1059 io_subchannel_probe (struct subchannel
*sch
)
1061 struct ccw_device
*cdev
;
1063 unsigned long flags
;
1064 struct ccw_dev_id dev_id
;
1066 if (sch
->dev
.driver_data
) {
1068 * This subchannel already has an associated ccw_device.
1069 * Register it and exit. This happens for all early
1070 * device, e.g. the console.
1072 cdev
= sch
->dev
.driver_data
;
1073 device_initialize(&cdev
->dev
);
1074 ccw_device_register(cdev
);
1076 * Check if the device is already online. If it is
1077 * the reference count needs to be corrected
1078 * (see ccw_device_online and css_init_done for the
1081 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1082 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1083 cdev
->private->state
!= DEV_STATE_BOXED
)
1084 get_device(&cdev
->dev
);
1088 * First check if a fitting device may be found amongst the
1089 * disconnected devices or in the orphanage.
1091 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1092 dev_id
.ssid
= sch
->schid
.ssid
;
1093 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1095 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1099 * Schedule moving the device until when we have a registered
1100 * subchannel to move to and succeed the probe. We can
1101 * unregister later again, when the probe is through.
1103 cdev
->private->sch
= sch
;
1104 PREPARE_WORK(&cdev
->private->kick_work
,
1105 ccw_device_move_to_sch
);
1106 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1109 cdev
= io_subchannel_create_ccwdev(sch
);
1111 return PTR_ERR(cdev
);
1113 rc
= io_subchannel_recog(cdev
, sch
);
1115 spin_lock_irqsave(sch
->lock
, flags
);
1116 sch
->dev
.driver_data
= NULL
;
1117 spin_unlock_irqrestore(sch
->lock
, flags
);
1118 if (cdev
->dev
.release
)
1119 cdev
->dev
.release(&cdev
->dev
);
1126 io_subchannel_remove (struct subchannel
*sch
)
1128 struct ccw_device
*cdev
;
1129 unsigned long flags
;
1131 if (!sch
->dev
.driver_data
)
1133 cdev
= sch
->dev
.driver_data
;
1134 /* Set ccw device to not operational and drop reference. */
1135 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1136 sch
->dev
.driver_data
= NULL
;
1137 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1138 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1140 * Put unregistration on workqueue to avoid livelocks on the css bus
1143 if (get_device(&cdev
->dev
)) {
1144 PREPARE_WORK(&cdev
->private->kick_work
,
1145 ccw_device_unregister
);
1146 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
1152 io_subchannel_notify(struct device
*dev
, int event
)
1154 struct ccw_device
*cdev
;
1156 cdev
= dev
->driver_data
;
1163 return cdev
->drv
->notify
? cdev
->drv
->notify(cdev
, event
) : 0;
1167 io_subchannel_verify(struct device
*dev
)
1169 struct ccw_device
*cdev
;
1171 cdev
= dev
->driver_data
;
1173 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1177 io_subchannel_ioterm(struct device
*dev
)
1179 struct ccw_device
*cdev
;
1181 cdev
= dev
->driver_data
;
1184 /* Internal I/O will be retried by the interrupt handler. */
1185 if (cdev
->private->flags
.intretry
)
1187 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1189 cdev
->handler(cdev
, cdev
->private->intparm
,
1194 io_subchannel_shutdown(struct subchannel
*sch
)
1196 struct ccw_device
*cdev
;
1199 cdev
= sch
->dev
.driver_data
;
1201 if (cio_is_console(sch
->schid
))
1203 if (!sch
->schib
.pmcw
.ena
)
1204 /* Nothing to do. */
1206 ret
= cio_disable_subchannel(sch
);
1208 /* Subchannel is disabled, we're done. */
1210 cdev
->private->state
= DEV_STATE_QUIESCE
;
1212 cdev
->handler(cdev
, cdev
->private->intparm
,
1214 ret
= ccw_device_cancel_halt_clear(cdev
);
1215 if (ret
== -EBUSY
) {
1216 ccw_device_set_timeout(cdev
, HZ
/10);
1217 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1219 cio_disable_subchannel(sch
);
1222 #ifdef CONFIG_CCW_CONSOLE
1223 static struct ccw_device console_cdev
;
1224 static struct ccw_device_private console_private
;
1225 static int console_cdev_in_use
;
1227 static DEFINE_SPINLOCK(ccw_console_lock
);
1229 spinlock_t
* cio_get_console_lock(void)
1231 return &ccw_console_lock
;
1235 ccw_device_console_enable (struct ccw_device
*cdev
, struct subchannel
*sch
)
1239 /* Initialize the ccw_device structure. */
1240 cdev
->dev
.parent
= &sch
->dev
;
1241 rc
= io_subchannel_recog(cdev
, sch
);
1245 /* Now wait for the async. recognition to come to an end. */
1246 spin_lock_irq(cdev
->ccwlock
);
1247 while (!dev_fsm_final_state(cdev
))
1250 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1252 ccw_device_online(cdev
);
1253 while (!dev_fsm_final_state(cdev
))
1255 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1259 spin_unlock_irq(cdev
->ccwlock
);
1264 ccw_device_probe_console(void)
1266 struct subchannel
*sch
;
1269 if (xchg(&console_cdev_in_use
, 1) != 0)
1270 return ERR_PTR(-EBUSY
);
1271 sch
= cio_probe_console();
1273 console_cdev_in_use
= 0;
1274 return (void *) sch
;
1276 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1277 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1278 console_cdev
.private = &console_private
;
1279 console_private
.cdev
= &console_cdev
;
1280 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1282 cio_release_console();
1283 console_cdev_in_use
= 0;
1284 return ERR_PTR(ret
);
1286 console_cdev
.online
= 1;
1287 return &console_cdev
;
1292 * get ccw_device matching the busid, but only if owned by cdrv
1295 __ccwdev_check_busid(struct device
*dev
, void *id
)
1301 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1306 get_ccwdev_by_busid(struct ccw_driver
*cdrv
, const char *bus_id
)
1309 struct device_driver
*drv
;
1311 drv
= get_driver(&cdrv
->driver
);
1315 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1316 __ccwdev_check_busid
);
1319 return dev
? to_ccwdev(dev
) : NULL
;
1322 /************************** device driver handling ************************/
1324 /* This is the implementation of the ccw_driver class. The probe, remove
1325 * and release methods are initially very similar to the device_driver
1326 * implementations, with the difference that they have ccw_device
1329 * A ccw driver also contains the information that is needed for
1333 ccw_device_probe (struct device
*dev
)
1335 struct ccw_device
*cdev
= to_ccwdev(dev
);
1336 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1339 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1341 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1352 ccw_device_remove (struct device
*dev
)
1354 struct ccw_device
*cdev
= to_ccwdev(dev
);
1355 struct ccw_driver
*cdrv
= cdev
->drv
;
1358 pr_debug("removing device %s\n", cdev
->dev
.bus_id
);
1363 spin_lock_irq(cdev
->ccwlock
);
1364 ret
= ccw_device_offline(cdev
);
1365 spin_unlock_irq(cdev
->ccwlock
);
1367 wait_event(cdev
->private->wait_q
,
1368 dev_fsm_final_state(cdev
));
1370 //FIXME: we can't fail!
1371 pr_debug("ccw_device_offline returned %d, device %s\n",
1372 ret
, cdev
->dev
.bus_id
);
1374 ccw_device_set_timeout(cdev
, 0);
1379 struct bus_type ccw_bus_type
= {
1381 .match
= ccw_bus_match
,
1382 .uevent
= ccw_uevent
,
1383 .probe
= ccw_device_probe
,
1384 .remove
= ccw_device_remove
,
1388 ccw_driver_register (struct ccw_driver
*cdriver
)
1390 struct device_driver
*drv
= &cdriver
->driver
;
1392 drv
->bus
= &ccw_bus_type
;
1393 drv
->name
= cdriver
->name
;
1395 return driver_register(drv
);
1399 ccw_driver_unregister (struct ccw_driver
*cdriver
)
1401 driver_unregister(&cdriver
->driver
);
1404 /* Helper func for qdio. */
1405 struct subchannel_id
1406 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1408 struct subchannel
*sch
;
1410 sch
= to_subchannel(cdev
->dev
.parent
);
1414 MODULE_LICENSE("GPL");
1415 EXPORT_SYMBOL(ccw_device_set_online
);
1416 EXPORT_SYMBOL(ccw_device_set_offline
);
1417 EXPORT_SYMBOL(ccw_driver_register
);
1418 EXPORT_SYMBOL(ccw_driver_unregister
);
1419 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1420 EXPORT_SYMBOL(ccw_bus_type
);
1421 EXPORT_SYMBOL(ccw_device_work
);
1422 EXPORT_SYMBOL(ccw_device_notify_work
);
1423 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);