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 */
30 /******************* bus type handling ***********************/
32 /* The Linux driver model distinguishes between a bus type and
33 * the bus itself. Of course we only have one channel
34 * subsystem driver and one channel system per machine, but
35 * we still use the abstraction. T.R. says it's a good idea. */
37 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
39 struct ccw_device
*cdev
= to_ccwdev(dev
);
40 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
41 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
46 found
= ccw_device_id_match(ids
, &cdev
->id
);
50 cdev
->id
.driver_info
= found
->driver_info
;
56 * Hotplugging interface for ccw devices.
57 * Heavily modeled on pci and usb hotplug.
60 ccw_uevent (struct device
*dev
, char **envp
, int num_envp
,
61 char *buffer
, int buffer_size
)
63 struct ccw_device
*cdev
= to_ccwdev(dev
);
70 /* what we want to pass to /sbin/hotplug */
73 length
+= scnprintf(buffer
, buffer_size
- length
, "CU_TYPE=%04X",
75 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
81 length
+= scnprintf(buffer
, buffer_size
- length
, "CU_MODEL=%02X",
83 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
88 /* The next two can be zero, that's ok for us */
90 length
+= scnprintf(buffer
, buffer_size
- length
, "DEV_TYPE=%04X",
92 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
98 length
+= scnprintf(buffer
, buffer_size
- length
, "DEV_MODEL=%02X",
100 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
108 struct bus_type ccw_bus_type
;
110 static int io_subchannel_probe (struct subchannel
*);
111 static int io_subchannel_remove (struct subchannel
*);
112 void io_subchannel_irq (struct device
*);
113 static int io_subchannel_notify(struct device
*, int);
114 static void io_subchannel_verify(struct device
*);
115 static void io_subchannel_ioterm(struct device
*);
116 static void io_subchannel_shutdown(struct subchannel
*);
118 struct css_driver io_subchannel_driver
= {
119 .subchannel_type
= SUBCHANNEL_TYPE_IO
,
121 .name
= "io_subchannel",
122 .bus
= &css_bus_type
,
124 .irq
= io_subchannel_irq
,
125 .notify
= io_subchannel_notify
,
126 .verify
= io_subchannel_verify
,
127 .termination
= io_subchannel_ioterm
,
128 .probe
= io_subchannel_probe
,
129 .remove
= io_subchannel_remove
,
130 .shutdown
= io_subchannel_shutdown
,
133 struct workqueue_struct
*ccw_device_work
;
134 struct workqueue_struct
*ccw_device_notify_work
;
135 wait_queue_head_t ccw_device_init_wq
;
136 atomic_t ccw_device_init_count
;
139 init_ccw_bus_type (void)
143 init_waitqueue_head(&ccw_device_init_wq
);
144 atomic_set(&ccw_device_init_count
, 0);
146 ccw_device_work
= create_singlethread_workqueue("cio");
147 if (!ccw_device_work
)
148 return -ENOMEM
; /* FIXME: better errno ? */
149 ccw_device_notify_work
= create_singlethread_workqueue("cio_notify");
150 if (!ccw_device_notify_work
) {
151 ret
= -ENOMEM
; /* FIXME: better errno ? */
154 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
156 ret
= -ENOMEM
; /* FIXME: better errno ? */
159 if ((ret
= bus_register (&ccw_bus_type
)))
162 if ((ret
= driver_register(&io_subchannel_driver
.drv
)))
165 wait_event(ccw_device_init_wq
,
166 atomic_read(&ccw_device_init_count
) == 0);
167 flush_workqueue(ccw_device_work
);
171 destroy_workqueue(ccw_device_work
);
172 if (ccw_device_notify_work
)
173 destroy_workqueue(ccw_device_notify_work
);
175 destroy_workqueue(slow_path_wq
);
180 cleanup_ccw_bus_type (void)
182 driver_unregister(&io_subchannel_driver
.drv
);
183 bus_unregister(&ccw_bus_type
);
184 destroy_workqueue(ccw_device_notify_work
);
185 destroy_workqueue(ccw_device_work
);
188 subsys_initcall(init_ccw_bus_type
);
189 module_exit(cleanup_ccw_bus_type
);
191 /************************ device handling **************************/
194 * A ccw_device has some interfaces in sysfs in addition to the
196 * The following entries are designed to export the information which
197 * resided in 2.4 in /proc/subchannels. Subchannel and device number
198 * are obvious, so they don't have an entry :)
199 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
202 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
204 struct subchannel
*sch
= to_subchannel(dev
);
205 struct ssd_info
*ssd
= &sch
->ssd_info
;
209 for (chp
= 0; chp
< 8; chp
++)
210 ret
+= sprintf (buf
+ret
, "%02x ", ssd
->chpid
[chp
]);
212 ret
+= sprintf (buf
+ret
, "\n");
213 return min((ssize_t
)PAGE_SIZE
, ret
);
217 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
219 struct subchannel
*sch
= to_subchannel(dev
);
220 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
222 return sprintf (buf
, "%02x %02x %02x\n",
223 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
227 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
229 struct ccw_device
*cdev
= to_ccwdev(dev
);
230 struct ccw_device_id
*id
= &(cdev
->id
);
232 if (id
->dev_type
!= 0)
233 return sprintf(buf
, "%04x/%02x\n",
234 id
->dev_type
, id
->dev_model
);
236 return sprintf(buf
, "n/a\n");
240 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
242 struct ccw_device
*cdev
= to_ccwdev(dev
);
243 struct ccw_device_id
*id
= &(cdev
->id
);
245 return sprintf(buf
, "%04x/%02x\n",
246 id
->cu_type
, id
->cu_model
);
250 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
252 struct ccw_device
*cdev
= to_ccwdev(dev
);
253 struct ccw_device_id
*id
= &(cdev
->id
);
256 ret
= sprintf(buf
, "ccw:t%04Xm%02X",
257 id
->cu_type
, id
->cu_model
);
258 if (id
->dev_type
!= 0)
259 ret
+= sprintf(buf
+ ret
, "dt%04Xdm%02X\n",
260 id
->dev_type
, id
->dev_model
);
262 ret
+= sprintf(buf
+ ret
, "dtdm\n");
267 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
269 struct ccw_device
*cdev
= to_ccwdev(dev
);
271 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
275 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
277 struct subchannel
*sch
;
279 * Forced offline in disconnected state means
280 * 'throw away device'.
282 sch
= to_subchannel(cdev
->dev
.parent
);
283 css_sch_device_unregister(sch
);
284 /* Reset intparm to zeroes. */
285 sch
->schib
.pmcw
.intparm
= 0;
287 put_device(&sch
->dev
);
291 ccw_device_set_offline(struct ccw_device
*cdev
)
297 if (!cdev
->online
|| !cdev
->drv
)
300 if (cdev
->drv
->set_offline
) {
301 ret
= cdev
->drv
->set_offline(cdev
);
306 spin_lock_irq(cdev
->ccwlock
);
307 ret
= ccw_device_offline(cdev
);
308 if (ret
== -ENODEV
) {
309 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
310 cdev
->private->state
= DEV_STATE_OFFLINE
;
311 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
313 spin_unlock_irq(cdev
->ccwlock
);
316 spin_unlock_irq(cdev
->ccwlock
);
318 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
320 pr_debug("ccw_device_offline returned %d, device %s\n",
321 ret
, cdev
->dev
.bus_id
);
328 ccw_device_set_online(struct ccw_device
*cdev
)
334 if (cdev
->online
|| !cdev
->drv
)
337 spin_lock_irq(cdev
->ccwlock
);
338 ret
= ccw_device_online(cdev
);
339 spin_unlock_irq(cdev
->ccwlock
);
341 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
343 pr_debug("ccw_device_online returned %d, device %s\n",
344 ret
, cdev
->dev
.bus_id
);
347 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
349 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
353 spin_lock_irq(cdev
->ccwlock
);
354 ret
= ccw_device_offline(cdev
);
355 spin_unlock_irq(cdev
->ccwlock
);
357 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
359 pr_debug("ccw_device_offline returned %d, device %s\n",
360 ret
, cdev
->dev
.bus_id
);
361 return (ret
== 0) ? -ENODEV
: ret
;
365 online_store (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
367 struct ccw_device
*cdev
= to_ccwdev(dev
);
371 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
374 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
375 atomic_set(&cdev
->private->onoff
, 0);
378 if (!strncmp(buf
, "force\n", count
)) {
383 i
= simple_strtoul(buf
, &tmp
, 16);
386 /* Do device recognition, if needed. */
387 if (cdev
->id
.cu_type
== 0) {
388 ret
= ccw_device_recognition(cdev
);
390 printk(KERN_WARNING
"Couldn't start recognition "
391 "for device %s (ret=%d)\n",
392 cdev
->dev
.bus_id
, ret
);
395 wait_event(cdev
->private->wait_q
,
396 cdev
->private->flags
.recog_done
);
398 if (cdev
->drv
&& cdev
->drv
->set_online
)
399 ccw_device_set_online(cdev
);
401 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
402 ccw_device_remove_disconnected(cdev
);
403 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
404 ccw_device_set_offline(cdev
);
406 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
407 ret
= ccw_device_stlck(cdev
);
409 printk(KERN_WARNING
"ccw_device_stlck for device %s "
410 "returned %d!\n", cdev
->dev
.bus_id
, ret
);
413 /* Do device recognition, if needed. */
414 if (cdev
->id
.cu_type
== 0) {
415 cdev
->private->state
= DEV_STATE_NOT_OPER
;
416 ret
= ccw_device_recognition(cdev
);
418 printk(KERN_WARNING
"Couldn't start recognition "
419 "for device %s (ret=%d)\n",
420 cdev
->dev
.bus_id
, ret
);
423 wait_event(cdev
->private->wait_q
,
424 cdev
->private->flags
.recog_done
);
426 if (cdev
->drv
&& cdev
->drv
->set_online
)
427 ccw_device_set_online(cdev
);
431 module_put(cdev
->drv
->owner
);
432 atomic_set(&cdev
->private->onoff
, 0);
437 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
439 struct ccw_device
*cdev
= to_ccwdev(dev
);
440 struct subchannel
*sch
;
442 switch (cdev
->private->state
) {
443 case DEV_STATE_BOXED
:
444 return sprintf(buf
, "boxed\n");
445 case DEV_STATE_DISCONNECTED
:
446 case DEV_STATE_DISCONNECTED_SENSE_ID
:
447 case DEV_STATE_NOT_OPER
:
448 sch
= to_subchannel(dev
->parent
);
450 return sprintf(buf
, "no path\n");
452 return sprintf(buf
, "no device\n");
454 /* All other states considered fine. */
455 return sprintf(buf
, "good\n");
459 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
460 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
461 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
462 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
463 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
464 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
465 extern struct device_attribute dev_attr_cmb_enable
;
466 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
468 static struct attribute
* subch_attrs
[] = {
469 &dev_attr_chpids
.attr
,
470 &dev_attr_pimpampom
.attr
,
474 static struct attribute_group subch_attr_group
= {
475 .attrs
= subch_attrs
,
479 subchannel_add_files (struct device
*dev
)
481 return sysfs_create_group(&dev
->kobj
, &subch_attr_group
);
484 static struct attribute
* ccwdev_attrs
[] = {
485 &dev_attr_devtype
.attr
,
486 &dev_attr_cutype
.attr
,
487 &dev_attr_modalias
.attr
,
488 &dev_attr_online
.attr
,
489 &dev_attr_cmb_enable
.attr
,
490 &dev_attr_availability
.attr
,
494 static struct attribute_group ccwdev_attr_group
= {
495 .attrs
= ccwdev_attrs
,
499 device_add_files (struct device
*dev
)
501 return sysfs_create_group(&dev
->kobj
, &ccwdev_attr_group
);
505 device_remove_files(struct device
*dev
)
507 sysfs_remove_group(&dev
->kobj
, &ccwdev_attr_group
);
510 /* this is a simple abstraction for device_register that sets the
511 * correct bus type and adds the bus specific files */
513 ccw_device_register(struct ccw_device
*cdev
)
515 struct device
*dev
= &cdev
->dev
;
518 dev
->bus
= &ccw_bus_type
;
520 if ((ret
= device_add(dev
)))
523 set_bit(1, &cdev
->private->registered
);
524 if ((ret
= device_add_files(dev
))) {
525 if (test_and_clear_bit(1, &cdev
->private->registered
))
534 struct ccw_device
* sibling
;
538 match_devno(struct device
* dev
, void * data
)
540 struct match_data
* d
= (struct match_data
*)data
;
541 struct ccw_device
* cdev
;
543 cdev
= to_ccwdev(dev
);
544 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
545 (cdev
->private->devno
== d
->devno
) &&
546 (cdev
->private->ssid
== d
->ssid
) &&
547 (cdev
!= d
->sibling
)) {
548 cdev
->private->state
= DEV_STATE_NOT_OPER
;
554 static struct ccw_device
*
555 get_disc_ccwdev_by_devno(unsigned int devno
, unsigned int ssid
,
556 struct ccw_device
*sibling
)
559 struct match_data data
;
563 data
.sibling
= sibling
;
564 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
566 return dev
? to_ccwdev(dev
) : NULL
;
570 ccw_device_add_changed(void *data
)
573 struct ccw_device
*cdev
;
575 cdev
= (struct ccw_device
*)data
;
576 if (device_add(&cdev
->dev
)) {
577 put_device(&cdev
->dev
);
580 set_bit(1, &cdev
->private->registered
);
581 if (device_add_files(&cdev
->dev
)) {
582 if (test_and_clear_bit(1, &cdev
->private->registered
))
583 device_unregister(&cdev
->dev
);
587 extern int css_get_ssd_info(struct subchannel
*sch
);
590 ccw_device_do_unreg_rereg(void *data
)
592 struct ccw_device
*cdev
;
593 struct subchannel
*sch
;
596 cdev
= (struct ccw_device
*)data
;
597 sch
= to_subchannel(cdev
->dev
.parent
);
598 if (cdev
->private->devno
!= sch
->schib
.pmcw
.dev
) {
600 * The device number has changed. This is usually only when
601 * a device has been detached under VM and then re-appeared
602 * on another subchannel because of a different attachment
603 * order than before. Ideally, we should should just switch
604 * subchannels, but unfortunately, this is not possible with
605 * the current implementation.
606 * Instead, we search for the old subchannel for this device
607 * number and deregister so there are no collisions with the
608 * newly registered ccw_device.
609 * FIXME: Find another solution so the block layer doesn't
610 * get possibly sick...
612 struct ccw_device
*other_cdev
;
615 other_cdev
= get_disc_ccwdev_by_devno(sch
->schib
.pmcw
.dev
,
616 sch
->schid
.ssid
, cdev
);
618 struct subchannel
*other_sch
;
620 other_sch
= to_subchannel(other_cdev
->dev
.parent
);
621 if (get_device(&other_sch
->dev
)) {
622 stsch(other_sch
->schid
, &other_sch
->schib
);
623 if (other_sch
->schib
.pmcw
.dnv
) {
624 other_sch
->schib
.pmcw
.intparm
= 0;
625 cio_modify(other_sch
);
627 css_sch_device_unregister(other_sch
);
630 /* Update ssd info here. */
631 css_get_ssd_info(sch
);
632 cdev
->private->devno
= sch
->schib
.pmcw
.dev
;
635 device_remove_files(&cdev
->dev
);
636 if (test_and_clear_bit(1, &cdev
->private->registered
))
637 device_del(&cdev
->dev
);
639 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
640 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
641 PREPARE_WORK(&cdev
->private->kick_work
,
642 ccw_device_add_changed
, (void *)cdev
);
643 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
647 ccw_device_release(struct device
*dev
)
649 struct ccw_device
*cdev
;
651 cdev
= to_ccwdev(dev
);
652 kfree(cdev
->private);
657 * Register recognized device.
660 io_subchannel_register(void *data
)
662 struct ccw_device
*cdev
;
663 struct subchannel
*sch
;
667 cdev
= (struct ccw_device
*) data
;
668 sch
= to_subchannel(cdev
->dev
.parent
);
670 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
671 bus_rescan_devices(&ccw_bus_type
);
674 /* make it known to the system */
675 ret
= ccw_device_register(cdev
);
677 printk (KERN_WARNING
"%s: could not register %s\n",
678 __func__
, cdev
->dev
.bus_id
);
679 put_device(&cdev
->dev
);
680 spin_lock_irqsave(&sch
->lock
, flags
);
681 sch
->dev
.driver_data
= NULL
;
682 spin_unlock_irqrestore(&sch
->lock
, flags
);
683 kfree (cdev
->private);
685 put_device(&sch
->dev
);
686 if (atomic_dec_and_test(&ccw_device_init_count
))
687 wake_up(&ccw_device_init_wq
);
691 ret
= subchannel_add_files(cdev
->dev
.parent
);
693 printk(KERN_WARNING
"%s: could not add attributes to %s\n",
694 __func__
, sch
->dev
.bus_id
);
695 put_device(&cdev
->dev
);
697 cdev
->private->flags
.recog_done
= 1;
698 put_device(&sch
->dev
);
699 wake_up(&cdev
->private->wait_q
);
700 if (atomic_dec_and_test(&ccw_device_init_count
))
701 wake_up(&ccw_device_init_wq
);
705 ccw_device_call_sch_unregister(void *data
)
707 struct ccw_device
*cdev
= data
;
708 struct subchannel
*sch
;
710 sch
= to_subchannel(cdev
->dev
.parent
);
711 css_sch_device_unregister(sch
);
712 /* Reset intparm to zeroes. */
713 sch
->schib
.pmcw
.intparm
= 0;
715 put_device(&cdev
->dev
);
716 put_device(&sch
->dev
);
720 * subchannel recognition done. Called from the state machine.
723 io_subchannel_recog_done(struct ccw_device
*cdev
)
725 struct subchannel
*sch
;
727 if (css_init_done
== 0) {
728 cdev
->private->flags
.recog_done
= 1;
731 switch (cdev
->private->state
) {
732 case DEV_STATE_NOT_OPER
:
733 cdev
->private->flags
.recog_done
= 1;
734 /* Remove device found not operational. */
735 if (!get_device(&cdev
->dev
))
737 sch
= to_subchannel(cdev
->dev
.parent
);
738 PREPARE_WORK(&cdev
->private->kick_work
,
739 ccw_device_call_sch_unregister
, (void *) cdev
);
740 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
741 if (atomic_dec_and_test(&ccw_device_init_count
))
742 wake_up(&ccw_device_init_wq
);
744 case DEV_STATE_BOXED
:
745 /* Device did not respond in time. */
746 case DEV_STATE_OFFLINE
:
748 * We can't register the device in interrupt context so
749 * we schedule a work item.
751 if (!get_device(&cdev
->dev
))
753 PREPARE_WORK(&cdev
->private->kick_work
,
754 io_subchannel_register
, (void *) cdev
);
755 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
761 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
764 struct ccw_device_private
*priv
;
766 sch
->dev
.driver_data
= cdev
;
767 sch
->driver
= &io_subchannel_driver
;
768 cdev
->ccwlock
= &sch
->lock
;
770 /* Init private data. */
771 priv
= cdev
->private;
772 priv
->devno
= sch
->schib
.pmcw
.dev
;
773 priv
->ssid
= sch
->schid
.ssid
;
774 priv
->sch_no
= sch
->schid
.sch_no
;
775 priv
->state
= DEV_STATE_NOT_OPER
;
776 INIT_LIST_HEAD(&priv
->cmb_list
);
777 init_waitqueue_head(&priv
->wait_q
);
778 init_timer(&priv
->timer
);
780 /* Set an initial name for the device. */
781 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
782 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
784 /* Increase counter of devices currently in recognition. */
785 atomic_inc(&ccw_device_init_count
);
787 /* Start async. device sensing. */
788 spin_lock_irq(&sch
->lock
);
789 rc
= ccw_device_recognition(cdev
);
790 spin_unlock_irq(&sch
->lock
);
792 if (atomic_dec_and_test(&ccw_device_init_count
))
793 wake_up(&ccw_device_init_wq
);
799 io_subchannel_probe (struct subchannel
*sch
)
801 struct ccw_device
*cdev
;
805 if (sch
->dev
.driver_data
) {
807 * This subchannel already has an associated ccw_device.
808 * Register it and exit. This happens for all early
809 * device, e.g. the console.
811 cdev
= sch
->dev
.driver_data
;
812 device_initialize(&cdev
->dev
);
813 ccw_device_register(cdev
);
814 subchannel_add_files(&sch
->dev
);
816 * Check if the device is already online. If it is
817 * the reference count needs to be corrected
818 * (see ccw_device_online and css_init_done for the
821 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
822 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
823 cdev
->private->state
!= DEV_STATE_BOXED
)
824 get_device(&cdev
->dev
);
827 cdev
= kzalloc (sizeof(*cdev
), GFP_KERNEL
);
830 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
831 GFP_KERNEL
| GFP_DMA
);
832 if (!cdev
->private) {
836 atomic_set(&cdev
->private->onoff
, 0);
837 cdev
->dev
.parent
= &sch
->dev
;
838 cdev
->dev
.release
= ccw_device_release
;
839 INIT_LIST_HEAD(&cdev
->private->kick_work
.entry
);
840 /* Do first half of device_register. */
841 device_initialize(&cdev
->dev
);
843 if (!get_device(&sch
->dev
)) {
844 if (cdev
->dev
.release
)
845 cdev
->dev
.release(&cdev
->dev
);
849 rc
= io_subchannel_recog(cdev
, sch
);
851 spin_lock_irqsave(&sch
->lock
, flags
);
852 sch
->dev
.driver_data
= NULL
;
853 spin_unlock_irqrestore(&sch
->lock
, flags
);
854 if (cdev
->dev
.release
)
855 cdev
->dev
.release(&cdev
->dev
);
862 ccw_device_unregister(void *data
)
864 struct ccw_device
*cdev
;
866 cdev
= (struct ccw_device
*)data
;
867 if (test_and_clear_bit(1, &cdev
->private->registered
))
868 device_unregister(&cdev
->dev
);
869 put_device(&cdev
->dev
);
873 io_subchannel_remove (struct subchannel
*sch
)
875 struct ccw_device
*cdev
;
878 if (!sch
->dev
.driver_data
)
880 cdev
= sch
->dev
.driver_data
;
881 /* Set ccw device to not operational and drop reference. */
882 spin_lock_irqsave(cdev
->ccwlock
, flags
);
883 sch
->dev
.driver_data
= NULL
;
884 cdev
->private->state
= DEV_STATE_NOT_OPER
;
885 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
887 * Put unregistration on workqueue to avoid livelocks on the css bus
890 if (get_device(&cdev
->dev
)) {
891 PREPARE_WORK(&cdev
->private->kick_work
,
892 ccw_device_unregister
, (void *) cdev
);
893 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
899 io_subchannel_notify(struct device
*dev
, int event
)
901 struct ccw_device
*cdev
;
903 cdev
= dev
->driver_data
;
910 return cdev
->drv
->notify
? cdev
->drv
->notify(cdev
, event
) : 0;
914 io_subchannel_verify(struct device
*dev
)
916 struct ccw_device
*cdev
;
918 cdev
= dev
->driver_data
;
920 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
924 io_subchannel_ioterm(struct device
*dev
)
926 struct ccw_device
*cdev
;
928 cdev
= dev
->driver_data
;
931 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
933 cdev
->handler(cdev
, cdev
->private->intparm
,
938 io_subchannel_shutdown(struct subchannel
*sch
)
940 struct ccw_device
*cdev
;
943 cdev
= sch
->dev
.driver_data
;
945 if (cio_is_console(sch
->schid
))
947 if (!sch
->schib
.pmcw
.ena
)
950 ret
= cio_disable_subchannel(sch
);
952 /* Subchannel is disabled, we're done. */
954 cdev
->private->state
= DEV_STATE_QUIESCE
;
956 cdev
->handler(cdev
, cdev
->private->intparm
,
958 ret
= ccw_device_cancel_halt_clear(cdev
);
960 ccw_device_set_timeout(cdev
, HZ
/10);
961 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
963 cio_disable_subchannel(sch
);
966 #ifdef CONFIG_CCW_CONSOLE
967 static struct ccw_device console_cdev
;
968 static struct ccw_device_private console_private
;
969 static int console_cdev_in_use
;
972 ccw_device_console_enable (struct ccw_device
*cdev
, struct subchannel
*sch
)
976 /* Initialize the ccw_device structure. */
977 cdev
->dev
.parent
= &sch
->dev
;
978 rc
= io_subchannel_recog(cdev
, sch
);
982 /* Now wait for the async. recognition to come to an end. */
983 spin_lock_irq(cdev
->ccwlock
);
984 while (!dev_fsm_final_state(cdev
))
987 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
989 ccw_device_online(cdev
);
990 while (!dev_fsm_final_state(cdev
))
992 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
996 spin_unlock_irq(cdev
->ccwlock
);
1001 ccw_device_probe_console(void)
1003 struct subchannel
*sch
;
1006 if (xchg(&console_cdev_in_use
, 1) != 0)
1007 return ERR_PTR(-EBUSY
);
1008 sch
= cio_probe_console();
1010 console_cdev_in_use
= 0;
1011 return (void *) sch
;
1013 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1014 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1015 console_cdev
.private = &console_private
;
1016 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1018 cio_release_console();
1019 console_cdev_in_use
= 0;
1020 return ERR_PTR(ret
);
1022 console_cdev
.online
= 1;
1023 return &console_cdev
;
1028 * get ccw_device matching the busid, but only if owned by cdrv
1031 __ccwdev_check_busid(struct device
*dev
, void *id
)
1035 bus_id
= (char *)id
;
1037 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1042 get_ccwdev_by_busid(struct ccw_driver
*cdrv
, const char *bus_id
)
1045 struct device_driver
*drv
;
1047 drv
= get_driver(&cdrv
->driver
);
1051 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1052 __ccwdev_check_busid
);
1055 return dev
? to_ccwdev(dev
) : NULL
;
1058 /************************** device driver handling ************************/
1060 /* This is the implementation of the ccw_driver class. The probe, remove
1061 * and release methods are initially very similar to the device_driver
1062 * implementations, with the difference that they have ccw_device
1065 * A ccw driver also contains the information that is needed for
1069 ccw_device_probe (struct device
*dev
)
1071 struct ccw_device
*cdev
= to_ccwdev(dev
);
1072 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1075 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1077 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1088 ccw_device_remove (struct device
*dev
)
1090 struct ccw_device
*cdev
= to_ccwdev(dev
);
1091 struct ccw_driver
*cdrv
= cdev
->drv
;
1094 pr_debug("removing device %s\n", cdev
->dev
.bus_id
);
1099 spin_lock_irq(cdev
->ccwlock
);
1100 ret
= ccw_device_offline(cdev
);
1101 spin_unlock_irq(cdev
->ccwlock
);
1103 wait_event(cdev
->private->wait_q
,
1104 dev_fsm_final_state(cdev
));
1106 //FIXME: we can't fail!
1107 pr_debug("ccw_device_offline returned %d, device %s\n",
1108 ret
, cdev
->dev
.bus_id
);
1110 ccw_device_set_timeout(cdev
, 0);
1115 struct bus_type ccw_bus_type
= {
1117 .match
= ccw_bus_match
,
1118 .uevent
= ccw_uevent
,
1119 .probe
= ccw_device_probe
,
1120 .remove
= ccw_device_remove
,
1124 ccw_driver_register (struct ccw_driver
*cdriver
)
1126 struct device_driver
*drv
= &cdriver
->driver
;
1128 drv
->bus
= &ccw_bus_type
;
1129 drv
->name
= cdriver
->name
;
1131 return driver_register(drv
);
1135 ccw_driver_unregister (struct ccw_driver
*cdriver
)
1137 driver_unregister(&cdriver
->driver
);
1140 /* Helper func for qdio. */
1141 struct subchannel_id
1142 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1144 struct subchannel
*sch
;
1146 sch
= to_subchannel(cdev
->dev
.parent
);
1150 MODULE_LICENSE("GPL");
1151 EXPORT_SYMBOL(ccw_device_set_online
);
1152 EXPORT_SYMBOL(ccw_device_set_offline
);
1153 EXPORT_SYMBOL(ccw_driver_register
);
1154 EXPORT_SYMBOL(ccw_driver_unregister
);
1155 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1156 EXPORT_SYMBOL(ccw_bus_type
);
1157 EXPORT_SYMBOL(ccw_device_work
);
1158 EXPORT_SYMBOL(ccw_device_notify_work
);
1159 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);