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/config.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/spinlock.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/device.h>
20 #include <linux/workqueue.h>
22 #include <asm/ccwdev.h>
24 #include <asm/param.h> /* HZ */
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
;
57 * Hotplugging interface for ccw devices.
58 * Heavily modeled on pci and usb hotplug.
61 ccw_uevent (struct device
*dev
, char **envp
, int num_envp
,
62 char *buffer
, int buffer_size
)
64 struct ccw_device
*cdev
= to_ccwdev(dev
);
71 /* what we want to pass to /sbin/hotplug */
74 length
+= scnprintf(buffer
, buffer_size
- length
, "CU_TYPE=%04X",
76 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
82 length
+= scnprintf(buffer
, buffer_size
- length
, "CU_MODEL=%02X",
84 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
89 /* The next two can be zero, that's ok for us */
91 length
+= scnprintf(buffer
, buffer_size
- length
, "DEV_TYPE=%04X",
93 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
99 length
+= scnprintf(buffer
, buffer_size
- length
, "DEV_MODEL=%02X",
101 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
109 struct bus_type ccw_bus_type
;
111 static int io_subchannel_probe (struct subchannel
*);
112 static int io_subchannel_remove (struct subchannel
*);
113 void io_subchannel_irq (struct device
*);
114 static int io_subchannel_notify(struct device
*, int);
115 static void io_subchannel_verify(struct device
*);
116 static void io_subchannel_ioterm(struct device
*);
117 static void io_subchannel_shutdown(struct subchannel
*);
119 struct css_driver io_subchannel_driver
= {
120 .subchannel_type
= SUBCHANNEL_TYPE_IO
,
122 .name
= "io_subchannel",
123 .bus
= &css_bus_type
,
125 .irq
= io_subchannel_irq
,
126 .notify
= io_subchannel_notify
,
127 .verify
= io_subchannel_verify
,
128 .termination
= io_subchannel_ioterm
,
129 .probe
= io_subchannel_probe
,
130 .remove
= io_subchannel_remove
,
131 .shutdown
= io_subchannel_shutdown
,
134 struct workqueue_struct
*ccw_device_work
;
135 struct workqueue_struct
*ccw_device_notify_work
;
136 static wait_queue_head_t ccw_device_init_wq
;
137 static atomic_t ccw_device_init_count
;
140 init_ccw_bus_type (void)
144 init_waitqueue_head(&ccw_device_init_wq
);
145 atomic_set(&ccw_device_init_count
, 0);
147 ccw_device_work
= create_singlethread_workqueue("cio");
148 if (!ccw_device_work
)
149 return -ENOMEM
; /* FIXME: better errno ? */
150 ccw_device_notify_work
= create_singlethread_workqueue("cio_notify");
151 if (!ccw_device_notify_work
) {
152 ret
= -ENOMEM
; /* FIXME: better errno ? */
155 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
157 ret
= -ENOMEM
; /* FIXME: better errno ? */
160 if ((ret
= bus_register (&ccw_bus_type
)))
163 if ((ret
= driver_register(&io_subchannel_driver
.drv
)))
166 wait_event(ccw_device_init_wq
,
167 atomic_read(&ccw_device_init_count
) == 0);
168 flush_workqueue(ccw_device_work
);
172 destroy_workqueue(ccw_device_work
);
173 if (ccw_device_notify_work
)
174 destroy_workqueue(ccw_device_notify_work
);
176 destroy_workqueue(slow_path_wq
);
181 cleanup_ccw_bus_type (void)
183 driver_unregister(&io_subchannel_driver
.drv
);
184 bus_unregister(&ccw_bus_type
);
185 destroy_workqueue(ccw_device_notify_work
);
186 destroy_workqueue(ccw_device_work
);
189 subsys_initcall(init_ccw_bus_type
);
190 module_exit(cleanup_ccw_bus_type
);
192 /************************ device handling **************************/
195 * A ccw_device has some interfaces in sysfs in addition to the
197 * The following entries are designed to export the information which
198 * resided in 2.4 in /proc/subchannels. Subchannel and device number
199 * are obvious, so they don't have an entry :)
200 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
203 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
205 struct subchannel
*sch
= to_subchannel(dev
);
206 struct ssd_info
*ssd
= &sch
->ssd_info
;
210 for (chp
= 0; chp
< 8; chp
++)
211 ret
+= sprintf (buf
+ret
, "%02x ", ssd
->chpid
[chp
]);
213 ret
+= sprintf (buf
+ret
, "\n");
214 return min((ssize_t
)PAGE_SIZE
, ret
);
218 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
220 struct subchannel
*sch
= to_subchannel(dev
);
221 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
223 return sprintf (buf
, "%02x %02x %02x\n",
224 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
228 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
230 struct ccw_device
*cdev
= to_ccwdev(dev
);
231 struct ccw_device_id
*id
= &(cdev
->id
);
233 if (id
->dev_type
!= 0)
234 return sprintf(buf
, "%04x/%02x\n",
235 id
->dev_type
, id
->dev_model
);
237 return sprintf(buf
, "n/a\n");
241 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
243 struct ccw_device
*cdev
= to_ccwdev(dev
);
244 struct ccw_device_id
*id
= &(cdev
->id
);
246 return sprintf(buf
, "%04x/%02x\n",
247 id
->cu_type
, id
->cu_model
);
251 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
253 struct ccw_device
*cdev
= to_ccwdev(dev
);
254 struct ccw_device_id
*id
= &(cdev
->id
);
257 ret
= sprintf(buf
, "ccw:t%04Xm%02X",
258 id
->cu_type
, id
->cu_model
);
259 if (id
->dev_type
!= 0)
260 ret
+= sprintf(buf
+ ret
, "dt%04Xdm%02X\n",
261 id
->dev_type
, id
->dev_model
);
263 ret
+= sprintf(buf
+ ret
, "dtdm\n");
268 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
270 struct ccw_device
*cdev
= to_ccwdev(dev
);
272 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
276 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
278 struct subchannel
*sch
;
280 * Forced offline in disconnected state means
281 * 'throw away device'.
283 sch
= to_subchannel(cdev
->dev
.parent
);
284 device_unregister(&sch
->dev
);
285 /* Reset intparm to zeroes. */
286 sch
->schib
.pmcw
.intparm
= 0;
288 put_device(&sch
->dev
);
292 ccw_device_set_offline(struct ccw_device
*cdev
)
298 if (!cdev
->online
|| !cdev
->drv
)
301 if (cdev
->drv
->set_offline
) {
302 ret
= cdev
->drv
->set_offline(cdev
);
307 spin_lock_irq(cdev
->ccwlock
);
308 ret
= ccw_device_offline(cdev
);
309 if (ret
== -ENODEV
) {
310 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
311 cdev
->private->state
= DEV_STATE_OFFLINE
;
312 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
314 spin_unlock_irq(cdev
->ccwlock
);
317 spin_unlock_irq(cdev
->ccwlock
);
319 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
321 pr_debug("ccw_device_offline returned %d, device %s\n",
322 ret
, cdev
->dev
.bus_id
);
329 ccw_device_set_online(struct ccw_device
*cdev
)
335 if (cdev
->online
|| !cdev
->drv
)
338 spin_lock_irq(cdev
->ccwlock
);
339 ret
= ccw_device_online(cdev
);
340 spin_unlock_irq(cdev
->ccwlock
);
342 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
344 pr_debug("ccw_device_online returned %d, device %s\n",
345 ret
, cdev
->dev
.bus_id
);
348 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
350 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
354 spin_lock_irq(cdev
->ccwlock
);
355 ret
= ccw_device_offline(cdev
);
356 spin_unlock_irq(cdev
->ccwlock
);
358 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
360 pr_debug("ccw_device_offline returned %d, device %s\n",
361 ret
, cdev
->dev
.bus_id
);
362 return (ret
== 0) ? -ENODEV
: ret
;
366 online_store (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
368 struct ccw_device
*cdev
= to_ccwdev(dev
);
372 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
375 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
376 atomic_set(&cdev
->private->onoff
, 0);
379 if (!strncmp(buf
, "force\n", count
)) {
384 i
= simple_strtoul(buf
, &tmp
, 16);
387 /* Do device recognition, if needed. */
388 if (cdev
->id
.cu_type
== 0) {
389 ret
= ccw_device_recognition(cdev
);
391 printk(KERN_WARNING
"Couldn't start recognition "
392 "for device %s (ret=%d)\n",
393 cdev
->dev
.bus_id
, ret
);
396 wait_event(cdev
->private->wait_q
,
397 cdev
->private->flags
.recog_done
);
399 if (cdev
->drv
&& cdev
->drv
->set_online
)
400 ccw_device_set_online(cdev
);
402 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
403 ccw_device_remove_disconnected(cdev
);
404 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
405 ccw_device_set_offline(cdev
);
407 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
408 ret
= ccw_device_stlck(cdev
);
410 printk(KERN_WARNING
"ccw_device_stlck for device %s "
411 "returned %d!\n", cdev
->dev
.bus_id
, ret
);
414 /* Do device recognition, if needed. */
415 if (cdev
->id
.cu_type
== 0) {
416 cdev
->private->state
= DEV_STATE_NOT_OPER
;
417 ret
= ccw_device_recognition(cdev
);
419 printk(KERN_WARNING
"Couldn't start recognition "
420 "for device %s (ret=%d)\n",
421 cdev
->dev
.bus_id
, ret
);
424 wait_event(cdev
->private->wait_q
,
425 cdev
->private->flags
.recog_done
);
427 if (cdev
->drv
&& cdev
->drv
->set_online
)
428 ccw_device_set_online(cdev
);
432 module_put(cdev
->drv
->owner
);
433 atomic_set(&cdev
->private->onoff
, 0);
438 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
440 struct ccw_device
*cdev
= to_ccwdev(dev
);
441 struct subchannel
*sch
;
443 switch (cdev
->private->state
) {
444 case DEV_STATE_BOXED
:
445 return sprintf(buf
, "boxed\n");
446 case DEV_STATE_DISCONNECTED
:
447 case DEV_STATE_DISCONNECTED_SENSE_ID
:
448 case DEV_STATE_NOT_OPER
:
449 sch
= to_subchannel(dev
->parent
);
451 return sprintf(buf
, "no path\n");
453 return sprintf(buf
, "no device\n");
455 /* All other states considered fine. */
456 return sprintf(buf
, "good\n");
460 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
461 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
462 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
463 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
464 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
465 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
466 extern struct device_attribute dev_attr_cmb_enable
;
467 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
469 static struct attribute
* subch_attrs
[] = {
470 &dev_attr_chpids
.attr
,
471 &dev_attr_pimpampom
.attr
,
475 static struct attribute_group subch_attr_group
= {
476 .attrs
= subch_attrs
,
480 subchannel_add_files (struct device
*dev
)
482 return sysfs_create_group(&dev
->kobj
, &subch_attr_group
);
485 static struct attribute
* ccwdev_attrs
[] = {
486 &dev_attr_devtype
.attr
,
487 &dev_attr_cutype
.attr
,
488 &dev_attr_modalias
.attr
,
489 &dev_attr_online
.attr
,
490 &dev_attr_cmb_enable
.attr
,
491 &dev_attr_availability
.attr
,
495 static struct attribute_group ccwdev_attr_group
= {
496 .attrs
= ccwdev_attrs
,
500 device_add_files (struct device
*dev
)
502 return sysfs_create_group(&dev
->kobj
, &ccwdev_attr_group
);
506 device_remove_files(struct device
*dev
)
508 sysfs_remove_group(&dev
->kobj
, &ccwdev_attr_group
);
511 /* this is a simple abstraction for device_register that sets the
512 * correct bus type and adds the bus specific files */
514 ccw_device_register(struct ccw_device
*cdev
)
516 struct device
*dev
= &cdev
->dev
;
519 dev
->bus
= &ccw_bus_type
;
521 if ((ret
= device_add(dev
)))
524 set_bit(1, &cdev
->private->registered
);
525 if ((ret
= device_add_files(dev
))) {
526 if (test_and_clear_bit(1, &cdev
->private->registered
))
535 struct ccw_device
* sibling
;
539 match_devno(struct device
* dev
, void * data
)
541 struct match_data
* d
= (struct match_data
*)data
;
542 struct ccw_device
* cdev
;
544 cdev
= to_ccwdev(dev
);
545 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
546 (cdev
->private->devno
== d
->devno
) &&
547 (cdev
->private->ssid
== d
->ssid
) &&
548 (cdev
!= d
->sibling
)) {
549 cdev
->private->state
= DEV_STATE_NOT_OPER
;
555 static struct ccw_device
*
556 get_disc_ccwdev_by_devno(unsigned int devno
, unsigned int ssid
,
557 struct ccw_device
*sibling
)
560 struct match_data data
= {
566 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
568 return dev
? to_ccwdev(dev
) : NULL
;
572 ccw_device_add_changed(void *data
)
575 struct ccw_device
*cdev
;
577 cdev
= (struct ccw_device
*)data
;
578 if (device_add(&cdev
->dev
)) {
579 put_device(&cdev
->dev
);
582 set_bit(1, &cdev
->private->registered
);
583 if (device_add_files(&cdev
->dev
)) {
584 if (test_and_clear_bit(1, &cdev
->private->registered
))
585 device_unregister(&cdev
->dev
);
589 extern int css_get_ssd_info(struct subchannel
*sch
);
592 ccw_device_do_unreg_rereg(void *data
)
594 struct ccw_device
*cdev
;
595 struct subchannel
*sch
;
598 cdev
= (struct ccw_device
*)data
;
599 sch
= to_subchannel(cdev
->dev
.parent
);
600 if (cdev
->private->devno
!= sch
->schib
.pmcw
.dev
) {
602 * The device number has changed. This is usually only when
603 * a device has been detached under VM and then re-appeared
604 * on another subchannel because of a different attachment
605 * order than before. Ideally, we should should just switch
606 * subchannels, but unfortunately, this is not possible with
607 * the current implementation.
608 * Instead, we search for the old subchannel for this device
609 * number and deregister so there are no collisions with the
610 * newly registered ccw_device.
611 * FIXME: Find another solution so the block layer doesn't
612 * get possibly sick...
614 struct ccw_device
*other_cdev
;
617 other_cdev
= get_disc_ccwdev_by_devno(sch
->schib
.pmcw
.dev
,
618 sch
->schid
.ssid
, cdev
);
620 struct subchannel
*other_sch
;
622 other_sch
= to_subchannel(other_cdev
->dev
.parent
);
623 if (get_device(&other_sch
->dev
)) {
624 stsch(other_sch
->schid
, &other_sch
->schib
);
625 if (other_sch
->schib
.pmcw
.dnv
) {
626 other_sch
->schib
.pmcw
.intparm
= 0;
627 cio_modify(other_sch
);
629 device_unregister(&other_sch
->dev
);
632 /* Update ssd info here. */
633 css_get_ssd_info(sch
);
634 cdev
->private->devno
= sch
->schib
.pmcw
.dev
;
637 device_remove_files(&cdev
->dev
);
638 if (test_and_clear_bit(1, &cdev
->private->registered
))
639 device_del(&cdev
->dev
);
641 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
642 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
643 PREPARE_WORK(&cdev
->private->kick_work
,
644 ccw_device_add_changed
, (void *)cdev
);
645 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
649 ccw_device_release(struct device
*dev
)
651 struct ccw_device
*cdev
;
653 cdev
= to_ccwdev(dev
);
654 kfree(cdev
->private);
659 * Register recognized device.
662 io_subchannel_register(void *data
)
664 struct ccw_device
*cdev
;
665 struct subchannel
*sch
;
669 cdev
= (struct ccw_device
*) data
;
670 sch
= to_subchannel(cdev
->dev
.parent
);
672 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
673 bus_rescan_devices(&ccw_bus_type
);
676 /* make it known to the system */
677 ret
= ccw_device_register(cdev
);
679 printk (KERN_WARNING
"%s: could not register %s\n",
680 __func__
, cdev
->dev
.bus_id
);
681 put_device(&cdev
->dev
);
682 spin_lock_irqsave(&sch
->lock
, flags
);
683 sch
->dev
.driver_data
= NULL
;
684 spin_unlock_irqrestore(&sch
->lock
, flags
);
685 kfree (cdev
->private);
687 put_device(&sch
->dev
);
688 if (atomic_dec_and_test(&ccw_device_init_count
))
689 wake_up(&ccw_device_init_wq
);
693 ret
= subchannel_add_files(cdev
->dev
.parent
);
695 printk(KERN_WARNING
"%s: could not add attributes to %s\n",
696 __func__
, sch
->dev
.bus_id
);
697 put_device(&cdev
->dev
);
699 cdev
->private->flags
.recog_done
= 1;
700 put_device(&sch
->dev
);
701 wake_up(&cdev
->private->wait_q
);
702 if (atomic_dec_and_test(&ccw_device_init_count
))
703 wake_up(&ccw_device_init_wq
);
707 ccw_device_call_sch_unregister(void *data
)
709 struct ccw_device
*cdev
= data
;
710 struct subchannel
*sch
;
712 sch
= to_subchannel(cdev
->dev
.parent
);
713 device_unregister(&sch
->dev
);
714 /* Reset intparm to zeroes. */
715 sch
->schib
.pmcw
.intparm
= 0;
717 put_device(&cdev
->dev
);
718 put_device(&sch
->dev
);
722 * subchannel recognition done. Called from the state machine.
725 io_subchannel_recog_done(struct ccw_device
*cdev
)
727 struct subchannel
*sch
;
729 if (css_init_done
== 0) {
730 cdev
->private->flags
.recog_done
= 1;
733 switch (cdev
->private->state
) {
734 case DEV_STATE_NOT_OPER
:
735 cdev
->private->flags
.recog_done
= 1;
736 /* Remove device found not operational. */
737 if (!get_device(&cdev
->dev
))
739 sch
= to_subchannel(cdev
->dev
.parent
);
740 PREPARE_WORK(&cdev
->private->kick_work
,
741 ccw_device_call_sch_unregister
, (void *) cdev
);
742 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
743 if (atomic_dec_and_test(&ccw_device_init_count
))
744 wake_up(&ccw_device_init_wq
);
746 case DEV_STATE_BOXED
:
747 /* Device did not respond in time. */
748 case DEV_STATE_OFFLINE
:
750 * We can't register the device in interrupt context so
751 * we schedule a work item.
753 if (!get_device(&cdev
->dev
))
755 PREPARE_WORK(&cdev
->private->kick_work
,
756 io_subchannel_register
, (void *) cdev
);
757 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
763 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
766 struct ccw_device_private
*priv
;
768 sch
->dev
.driver_data
= cdev
;
769 sch
->driver
= &io_subchannel_driver
;
770 cdev
->ccwlock
= &sch
->lock
;
772 /* Init private data. */
773 priv
= cdev
->private;
774 priv
->devno
= sch
->schib
.pmcw
.dev
;
775 priv
->ssid
= sch
->schid
.ssid
;
776 priv
->sch_no
= sch
->schid
.sch_no
;
777 priv
->state
= DEV_STATE_NOT_OPER
;
778 INIT_LIST_HEAD(&priv
->cmb_list
);
779 init_waitqueue_head(&priv
->wait_q
);
780 init_timer(&priv
->timer
);
782 /* Set an initial name for the device. */
783 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
784 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
786 /* Increase counter of devices currently in recognition. */
787 atomic_inc(&ccw_device_init_count
);
789 /* Start async. device sensing. */
790 spin_lock_irq(&sch
->lock
);
791 rc
= ccw_device_recognition(cdev
);
792 spin_unlock_irq(&sch
->lock
);
794 if (atomic_dec_and_test(&ccw_device_init_count
))
795 wake_up(&ccw_device_init_wq
);
801 io_subchannel_probe (struct subchannel
*sch
)
803 struct ccw_device
*cdev
;
807 if (sch
->dev
.driver_data
) {
809 * This subchannel already has an associated ccw_device.
810 * Register it and exit. This happens for all early
811 * device, e.g. the console.
813 cdev
= sch
->dev
.driver_data
;
814 device_initialize(&cdev
->dev
);
815 ccw_device_register(cdev
);
816 subchannel_add_files(&sch
->dev
);
818 * Check if the device is already online. If it is
819 * the reference count needs to be corrected
820 * (see ccw_device_online and css_init_done for the
823 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
824 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
825 cdev
->private->state
!= DEV_STATE_BOXED
)
826 get_device(&cdev
->dev
);
829 cdev
= kzalloc (sizeof(*cdev
), GFP_KERNEL
);
832 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
833 GFP_KERNEL
| GFP_DMA
);
834 if (!cdev
->private) {
838 atomic_set(&cdev
->private->onoff
, 0);
839 cdev
->dev
= (struct device
) {
841 .release
= ccw_device_release
,
843 INIT_LIST_HEAD(&cdev
->private->kick_work
.entry
);
844 /* Do first half of device_register. */
845 device_initialize(&cdev
->dev
);
847 if (!get_device(&sch
->dev
)) {
848 if (cdev
->dev
.release
)
849 cdev
->dev
.release(&cdev
->dev
);
853 rc
= io_subchannel_recog(cdev
, sch
);
855 spin_lock_irqsave(&sch
->lock
, flags
);
856 sch
->dev
.driver_data
= NULL
;
857 spin_unlock_irqrestore(&sch
->lock
, flags
);
858 if (cdev
->dev
.release
)
859 cdev
->dev
.release(&cdev
->dev
);
866 ccw_device_unregister(void *data
)
868 struct ccw_device
*cdev
;
870 cdev
= (struct ccw_device
*)data
;
871 if (test_and_clear_bit(1, &cdev
->private->registered
))
872 device_unregister(&cdev
->dev
);
873 put_device(&cdev
->dev
);
877 io_subchannel_remove (struct subchannel
*sch
)
879 struct ccw_device
*cdev
;
882 if (!sch
->dev
.driver_data
)
884 cdev
= sch
->dev
.driver_data
;
885 /* Set ccw device to not operational and drop reference. */
886 spin_lock_irqsave(cdev
->ccwlock
, flags
);
887 sch
->dev
.driver_data
= NULL
;
888 cdev
->private->state
= DEV_STATE_NOT_OPER
;
889 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
891 * Put unregistration on workqueue to avoid livelocks on the css bus
894 if (get_device(&cdev
->dev
)) {
895 PREPARE_WORK(&cdev
->private->kick_work
,
896 ccw_device_unregister
, (void *) cdev
);
897 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
903 io_subchannel_notify(struct device
*dev
, int event
)
905 struct ccw_device
*cdev
;
907 cdev
= dev
->driver_data
;
914 return cdev
->drv
->notify
? cdev
->drv
->notify(cdev
, event
) : 0;
918 io_subchannel_verify(struct device
*dev
)
920 struct ccw_device
*cdev
;
922 cdev
= dev
->driver_data
;
924 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
928 io_subchannel_ioterm(struct device
*dev
)
930 struct ccw_device
*cdev
;
932 cdev
= dev
->driver_data
;
935 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
937 cdev
->handler(cdev
, cdev
->private->intparm
,
942 io_subchannel_shutdown(struct subchannel
*sch
)
944 struct ccw_device
*cdev
;
947 cdev
= sch
->dev
.driver_data
;
949 if (cio_is_console(sch
->schid
))
951 if (!sch
->schib
.pmcw
.ena
)
954 ret
= cio_disable_subchannel(sch
);
956 /* Subchannel is disabled, we're done. */
958 cdev
->private->state
= DEV_STATE_QUIESCE
;
960 cdev
->handler(cdev
, cdev
->private->intparm
,
962 ret
= ccw_device_cancel_halt_clear(cdev
);
964 ccw_device_set_timeout(cdev
, HZ
/10);
965 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
967 cio_disable_subchannel(sch
);
970 #ifdef CONFIG_CCW_CONSOLE
971 static struct ccw_device console_cdev
;
972 static struct ccw_device_private console_private
;
973 static int console_cdev_in_use
;
976 ccw_device_console_enable (struct ccw_device
*cdev
, struct subchannel
*sch
)
980 /* Initialize the ccw_device structure. */
981 cdev
->dev
= (struct device
) {
984 rc
= io_subchannel_recog(cdev
, sch
);
988 /* Now wait for the async. recognition to come to an end. */
989 spin_lock_irq(cdev
->ccwlock
);
990 while (!dev_fsm_final_state(cdev
))
993 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
995 ccw_device_online(cdev
);
996 while (!dev_fsm_final_state(cdev
))
998 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1002 spin_unlock_irq(cdev
->ccwlock
);
1007 ccw_device_probe_console(void)
1009 struct subchannel
*sch
;
1012 if (xchg(&console_cdev_in_use
, 1) != 0)
1013 return ERR_PTR(-EBUSY
);
1014 sch
= cio_probe_console();
1016 console_cdev_in_use
= 0;
1017 return (void *) sch
;
1019 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1020 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1021 console_cdev
.private = &console_private
;
1022 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1024 cio_release_console();
1025 console_cdev_in_use
= 0;
1026 return ERR_PTR(ret
);
1028 console_cdev
.online
= 1;
1029 return &console_cdev
;
1034 * get ccw_device matching the busid, but only if owned by cdrv
1037 __ccwdev_check_busid(struct device
*dev
, void *id
)
1041 bus_id
= (char *)id
;
1043 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1048 get_ccwdev_by_busid(struct ccw_driver
*cdrv
, const char *bus_id
)
1051 struct device_driver
*drv
;
1053 drv
= get_driver(&cdrv
->driver
);
1057 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1058 __ccwdev_check_busid
);
1061 return dev
? to_ccwdev(dev
) : 0;
1064 /************************** device driver handling ************************/
1066 /* This is the implementation of the ccw_driver class. The probe, remove
1067 * and release methods are initially very similar to the device_driver
1068 * implementations, with the difference that they have ccw_device
1071 * A ccw driver also contains the information that is needed for
1075 ccw_device_probe (struct device
*dev
)
1077 struct ccw_device
*cdev
= to_ccwdev(dev
);
1078 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1081 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1083 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1094 ccw_device_remove (struct device
*dev
)
1096 struct ccw_device
*cdev
= to_ccwdev(dev
);
1097 struct ccw_driver
*cdrv
= cdev
->drv
;
1100 pr_debug("removing device %s\n", cdev
->dev
.bus_id
);
1105 spin_lock_irq(cdev
->ccwlock
);
1106 ret
= ccw_device_offline(cdev
);
1107 spin_unlock_irq(cdev
->ccwlock
);
1109 wait_event(cdev
->private->wait_q
,
1110 dev_fsm_final_state(cdev
));
1112 //FIXME: we can't fail!
1113 pr_debug("ccw_device_offline returned %d, device %s\n",
1114 ret
, cdev
->dev
.bus_id
);
1116 ccw_device_set_timeout(cdev
, 0);
1121 struct bus_type ccw_bus_type
= {
1123 .match
= ccw_bus_match
,
1124 .uevent
= ccw_uevent
,
1125 .probe
= ccw_device_probe
,
1126 .remove
= ccw_device_remove
,
1130 ccw_driver_register (struct ccw_driver
*cdriver
)
1132 struct device_driver
*drv
= &cdriver
->driver
;
1134 drv
->bus
= &ccw_bus_type
;
1135 drv
->name
= cdriver
->name
;
1137 return driver_register(drv
);
1141 ccw_driver_unregister (struct ccw_driver
*cdriver
)
1143 driver_unregister(&cdriver
->driver
);
1146 /* Helper func for qdio. */
1147 struct subchannel_id
1148 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1150 struct subchannel
*sch
;
1152 sch
= to_subchannel(cdev
->dev
.parent
);
1156 MODULE_LICENSE("GPL");
1157 EXPORT_SYMBOL(ccw_device_set_online
);
1158 EXPORT_SYMBOL(ccw_device_set_offline
);
1159 EXPORT_SYMBOL(ccw_driver_register
);
1160 EXPORT_SYMBOL(ccw_driver_unregister
);
1161 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1162 EXPORT_SYMBOL(ccw_bus_type
);
1163 EXPORT_SYMBOL(ccw_device_work
);
1164 EXPORT_SYMBOL(ccw_device_notify_work
);
1165 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);