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
;
55 /* Store modalias string delimited by prefix/suffix string into buffer with
56 * specified size. Return length of resulting string (excluding trailing '\0')
57 * even if string doesn't fit buffer (snprintf semantics). */
58 static int snprint_alias(char *buf
, size_t size
, const char *prefix
,
59 struct ccw_device_id
*id
, const char *suffix
)
63 len
= snprintf(buf
, size
, "%sccw:t%04Xm%02X", prefix
, id
->cu_type
,
70 if (id
->dev_type
!= 0)
71 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
72 id
->dev_model
, suffix
);
74 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
79 /* Set up environment variables for ccw device uevent. Return 0 on success,
80 * non-zero otherwise. */
81 static int ccw_uevent(struct device
*dev
, char **envp
, int num_envp
,
82 char *buffer
, int buffer_size
)
84 struct ccw_device
*cdev
= to_ccwdev(dev
);
85 struct ccw_device_id
*id
= &(cdev
->id
);
90 len
= snprintf(buffer
, buffer_size
, "CU_TYPE=%04X", id
->cu_type
) + 1;
91 if (len
> buffer_size
|| i
>= num_envp
)
98 len
= snprintf(buffer
, buffer_size
, "CU_MODEL=%02X", id
->cu_model
) + 1;
99 if (len
> buffer_size
|| i
>= num_envp
)
105 /* The next two can be zero, that's ok for us */
107 len
= snprintf(buffer
, buffer_size
, "DEV_TYPE=%04X", id
->dev_type
) + 1;
108 if (len
> buffer_size
|| i
>= num_envp
)
115 len
= snprintf(buffer
, buffer_size
, "DEV_MODEL=%02X",
116 (unsigned char) id
->dev_model
) + 1;
117 if (len
> buffer_size
|| i
>= num_envp
)
124 len
= snprint_alias(buffer
, buffer_size
, "MODALIAS=", id
, "") + 1;
125 if (len
> buffer_size
|| i
>= num_envp
)
136 struct bus_type ccw_bus_type
;
138 static int io_subchannel_probe (struct subchannel
*);
139 static int io_subchannel_remove (struct subchannel
*);
140 void io_subchannel_irq (struct device
*);
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
;
237 for (chp
= 0; chp
< 8; chp
++)
238 ret
+= sprintf (buf
+ret
, "%02x ", ssd
->chpid
[chp
]);
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") + 1;
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");
298 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
300 struct subchannel
*sch
;
302 * Forced offline in disconnected state means
303 * 'throw away device'.
305 sch
= to_subchannel(cdev
->dev
.parent
);
306 css_sch_device_unregister(sch
);
307 /* Reset intparm to zeroes. */
308 sch
->schib
.pmcw
.intparm
= 0;
310 put_device(&sch
->dev
);
314 ccw_device_set_offline(struct ccw_device
*cdev
)
320 if (!cdev
->online
|| !cdev
->drv
)
323 if (cdev
->drv
->set_offline
) {
324 ret
= cdev
->drv
->set_offline(cdev
);
329 spin_lock_irq(cdev
->ccwlock
);
330 ret
= ccw_device_offline(cdev
);
331 if (ret
== -ENODEV
) {
332 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
333 cdev
->private->state
= DEV_STATE_OFFLINE
;
334 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
336 spin_unlock_irq(cdev
->ccwlock
);
339 spin_unlock_irq(cdev
->ccwlock
);
341 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
343 pr_debug("ccw_device_offline returned %d, device %s\n",
344 ret
, cdev
->dev
.bus_id
);
351 ccw_device_set_online(struct ccw_device
*cdev
)
357 if (cdev
->online
|| !cdev
->drv
)
360 spin_lock_irq(cdev
->ccwlock
);
361 ret
= ccw_device_online(cdev
);
362 spin_unlock_irq(cdev
->ccwlock
);
364 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
366 pr_debug("ccw_device_online returned %d, device %s\n",
367 ret
, cdev
->dev
.bus_id
);
370 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
372 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
376 spin_lock_irq(cdev
->ccwlock
);
377 ret
= ccw_device_offline(cdev
);
378 spin_unlock_irq(cdev
->ccwlock
);
380 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
382 pr_debug("ccw_device_offline returned %d, device %s\n",
383 ret
, cdev
->dev
.bus_id
);
384 return (ret
== 0) ? -ENODEV
: ret
;
388 online_store (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
390 struct ccw_device
*cdev
= to_ccwdev(dev
);
394 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
397 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
398 atomic_set(&cdev
->private->onoff
, 0);
401 if (!strncmp(buf
, "force\n", count
)) {
406 i
= simple_strtoul(buf
, &tmp
, 16);
409 /* Do device recognition, if needed. */
410 if (cdev
->id
.cu_type
== 0) {
411 ret
= ccw_device_recognition(cdev
);
413 printk(KERN_WARNING
"Couldn't start recognition "
414 "for device %s (ret=%d)\n",
415 cdev
->dev
.bus_id
, ret
);
418 wait_event(cdev
->private->wait_q
,
419 cdev
->private->flags
.recog_done
);
421 if (cdev
->drv
&& cdev
->drv
->set_online
)
422 ccw_device_set_online(cdev
);
424 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
425 ccw_device_remove_disconnected(cdev
);
426 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
427 ccw_device_set_offline(cdev
);
429 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
430 ret
= ccw_device_stlck(cdev
);
432 printk(KERN_WARNING
"ccw_device_stlck for device %s "
433 "returned %d!\n", cdev
->dev
.bus_id
, ret
);
436 /* Do device recognition, if needed. */
437 if (cdev
->id
.cu_type
== 0) {
438 cdev
->private->state
= DEV_STATE_NOT_OPER
;
439 ret
= ccw_device_recognition(cdev
);
441 printk(KERN_WARNING
"Couldn't start recognition "
442 "for device %s (ret=%d)\n",
443 cdev
->dev
.bus_id
, ret
);
446 wait_event(cdev
->private->wait_q
,
447 cdev
->private->flags
.recog_done
);
449 if (cdev
->drv
&& cdev
->drv
->set_online
)
450 ccw_device_set_online(cdev
);
454 module_put(cdev
->drv
->owner
);
455 atomic_set(&cdev
->private->onoff
, 0);
460 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
462 struct ccw_device
*cdev
= to_ccwdev(dev
);
463 struct subchannel
*sch
;
465 switch (cdev
->private->state
) {
466 case DEV_STATE_BOXED
:
467 return sprintf(buf
, "boxed\n");
468 case DEV_STATE_DISCONNECTED
:
469 case DEV_STATE_DISCONNECTED_SENSE_ID
:
470 case DEV_STATE_NOT_OPER
:
471 sch
= to_subchannel(dev
->parent
);
473 return sprintf(buf
, "no path\n");
475 return sprintf(buf
, "no device\n");
477 /* All other states considered fine. */
478 return sprintf(buf
, "good\n");
482 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
483 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
484 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
485 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
486 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
487 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
488 extern struct device_attribute dev_attr_cmb_enable
;
489 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
491 static struct attribute
* subch_attrs
[] = {
492 &dev_attr_chpids
.attr
,
493 &dev_attr_pimpampom
.attr
,
497 static struct attribute_group subch_attr_group
= {
498 .attrs
= subch_attrs
,
502 subchannel_add_files (struct device
*dev
)
504 return sysfs_create_group(&dev
->kobj
, &subch_attr_group
);
507 static struct attribute
* ccwdev_attrs
[] = {
508 &dev_attr_devtype
.attr
,
509 &dev_attr_cutype
.attr
,
510 &dev_attr_modalias
.attr
,
511 &dev_attr_online
.attr
,
512 &dev_attr_cmb_enable
.attr
,
513 &dev_attr_availability
.attr
,
517 static struct attribute_group ccwdev_attr_group
= {
518 .attrs
= ccwdev_attrs
,
522 device_add_files (struct device
*dev
)
524 return sysfs_create_group(&dev
->kobj
, &ccwdev_attr_group
);
528 device_remove_files(struct device
*dev
)
530 sysfs_remove_group(&dev
->kobj
, &ccwdev_attr_group
);
533 /* this is a simple abstraction for device_register that sets the
534 * correct bus type and adds the bus specific files */
535 static int ccw_device_register(struct ccw_device
*cdev
)
537 struct device
*dev
= &cdev
->dev
;
540 dev
->bus
= &ccw_bus_type
;
542 if ((ret
= device_add(dev
)))
545 set_bit(1, &cdev
->private->registered
);
546 if ((ret
= device_add_files(dev
))) {
547 if (test_and_clear_bit(1, &cdev
->private->registered
))
554 struct ccw_dev_id dev_id
;
555 struct ccw_device
* sibling
;
559 match_devno(struct device
* dev
, void * data
)
561 struct match_data
* d
= data
;
562 struct ccw_device
* cdev
;
564 cdev
= to_ccwdev(dev
);
565 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
566 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
567 (cdev
!= d
->sibling
)) {
568 cdev
->private->state
= DEV_STATE_NOT_OPER
;
574 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
575 struct ccw_device
*sibling
)
578 struct match_data data
;
580 data
.dev_id
= *dev_id
;
581 data
.sibling
= sibling
;
582 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
584 return dev
? to_ccwdev(dev
) : NULL
;
588 ccw_device_add_changed(struct work_struct
*work
)
590 struct ccw_device_private
*priv
;
591 struct ccw_device
*cdev
;
593 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
595 if (device_add(&cdev
->dev
)) {
596 put_device(&cdev
->dev
);
599 set_bit(1, &cdev
->private->registered
);
600 if (device_add_files(&cdev
->dev
)) {
601 if (test_and_clear_bit(1, &cdev
->private->registered
))
602 device_unregister(&cdev
->dev
);
606 extern int css_get_ssd_info(struct subchannel
*sch
);
609 ccw_device_do_unreg_rereg(struct work_struct
*work
)
611 struct ccw_device_private
*priv
;
612 struct ccw_device
*cdev
;
613 struct subchannel
*sch
;
616 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
618 sch
= to_subchannel(cdev
->dev
.parent
);
619 if (cdev
->private->dev_id
.devno
!= sch
->schib
.pmcw
.dev
) {
621 * The device number has changed. This is usually only when
622 * a device has been detached under VM and then re-appeared
623 * on another subchannel because of a different attachment
624 * order than before. Ideally, we should should just switch
625 * subchannels, but unfortunately, this is not possible with
626 * the current implementation.
627 * Instead, we search for the old subchannel for this device
628 * number and deregister so there are no collisions with the
629 * newly registered ccw_device.
630 * FIXME: Find another solution so the block layer doesn't
631 * get possibly sick...
633 struct ccw_device
*other_cdev
;
634 struct ccw_dev_id dev_id
;
637 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
638 dev_id
.ssid
= sch
->schid
.ssid
;
639 other_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
641 struct subchannel
*other_sch
;
643 other_sch
= to_subchannel(other_cdev
->dev
.parent
);
644 if (get_device(&other_sch
->dev
)) {
645 stsch(other_sch
->schid
, &other_sch
->schib
);
646 if (other_sch
->schib
.pmcw
.dnv
) {
647 other_sch
->schib
.pmcw
.intparm
= 0;
648 cio_modify(other_sch
);
650 css_sch_device_unregister(other_sch
);
653 /* Update ssd info here. */
654 css_get_ssd_info(sch
);
655 cdev
->private->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
658 device_remove_files(&cdev
->dev
);
659 if (test_and_clear_bit(1, &cdev
->private->registered
))
660 device_del(&cdev
->dev
);
662 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
663 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
664 PREPARE_WORK(&cdev
->private->kick_work
,
665 ccw_device_add_changed
);
666 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
670 ccw_device_release(struct device
*dev
)
672 struct ccw_device
*cdev
;
674 cdev
= to_ccwdev(dev
);
675 kfree(cdev
->private);
680 * Register recognized device.
683 io_subchannel_register(struct work_struct
*work
)
685 struct ccw_device_private
*priv
;
686 struct ccw_device
*cdev
;
687 struct subchannel
*sch
;
691 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
693 sch
= to_subchannel(cdev
->dev
.parent
);
696 * io_subchannel_register() will also be called after device
697 * recognition has been done for a boxed device (which will already
698 * be registered). We need to reprobe since we may now have sense id
701 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
703 ret
= device_reprobe(&cdev
->dev
);
705 /* We can't do much here. */
706 dev_info(&cdev
->dev
, "device_reprobe() returned"
711 /* make it known to the system */
712 ret
= ccw_device_register(cdev
);
714 printk (KERN_WARNING
"%s: could not register %s\n",
715 __func__
, cdev
->dev
.bus_id
);
716 put_device(&cdev
->dev
);
717 spin_lock_irqsave(&sch
->lock
, flags
);
718 sch
->dev
.driver_data
= NULL
;
719 spin_unlock_irqrestore(&sch
->lock
, flags
);
720 kfree (cdev
->private);
722 put_device(&sch
->dev
);
723 if (atomic_dec_and_test(&ccw_device_init_count
))
724 wake_up(&ccw_device_init_wq
);
728 ret
= subchannel_add_files(cdev
->dev
.parent
);
730 printk(KERN_WARNING
"%s: could not add attributes to %s\n",
731 __func__
, sch
->dev
.bus_id
);
732 put_device(&cdev
->dev
);
734 cdev
->private->flags
.recog_done
= 1;
735 put_device(&sch
->dev
);
736 wake_up(&cdev
->private->wait_q
);
737 if (atomic_dec_and_test(&ccw_device_init_count
))
738 wake_up(&ccw_device_init_wq
);
742 ccw_device_call_sch_unregister(struct work_struct
*work
)
744 struct ccw_device_private
*priv
;
745 struct ccw_device
*cdev
;
746 struct subchannel
*sch
;
748 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
750 sch
= to_subchannel(cdev
->dev
.parent
);
751 css_sch_device_unregister(sch
);
752 /* Reset intparm to zeroes. */
753 sch
->schib
.pmcw
.intparm
= 0;
755 put_device(&cdev
->dev
);
756 put_device(&sch
->dev
);
760 * subchannel recognition done. Called from the state machine.
763 io_subchannel_recog_done(struct ccw_device
*cdev
)
765 struct subchannel
*sch
;
767 if (css_init_done
== 0) {
768 cdev
->private->flags
.recog_done
= 1;
771 switch (cdev
->private->state
) {
772 case DEV_STATE_NOT_OPER
:
773 cdev
->private->flags
.recog_done
= 1;
774 /* Remove device found not operational. */
775 if (!get_device(&cdev
->dev
))
777 sch
= to_subchannel(cdev
->dev
.parent
);
778 PREPARE_WORK(&cdev
->private->kick_work
,
779 ccw_device_call_sch_unregister
);
780 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
781 if (atomic_dec_and_test(&ccw_device_init_count
))
782 wake_up(&ccw_device_init_wq
);
784 case DEV_STATE_BOXED
:
785 /* Device did not respond in time. */
786 case DEV_STATE_OFFLINE
:
788 * We can't register the device in interrupt context so
789 * we schedule a work item.
791 if (!get_device(&cdev
->dev
))
793 PREPARE_WORK(&cdev
->private->kick_work
,
794 io_subchannel_register
);
795 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
801 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
804 struct ccw_device_private
*priv
;
806 sch
->dev
.driver_data
= cdev
;
807 sch
->driver
= &io_subchannel_driver
;
808 cdev
->ccwlock
= &sch
->lock
;
810 /* Init private data. */
811 priv
= cdev
->private;
812 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
813 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
814 priv
->schid
= sch
->schid
;
815 priv
->state
= DEV_STATE_NOT_OPER
;
816 INIT_LIST_HEAD(&priv
->cmb_list
);
817 init_waitqueue_head(&priv
->wait_q
);
818 init_timer(&priv
->timer
);
820 /* Set an initial name for the device. */
821 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
822 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
824 /* Increase counter of devices currently in recognition. */
825 atomic_inc(&ccw_device_init_count
);
827 /* Start async. device sensing. */
828 spin_lock_irq(&sch
->lock
);
829 rc
= ccw_device_recognition(cdev
);
830 spin_unlock_irq(&sch
->lock
);
832 if (atomic_dec_and_test(&ccw_device_init_count
))
833 wake_up(&ccw_device_init_wq
);
839 io_subchannel_probe (struct subchannel
*sch
)
841 struct ccw_device
*cdev
;
845 if (sch
->dev
.driver_data
) {
847 * This subchannel already has an associated ccw_device.
848 * Register it and exit. This happens for all early
849 * device, e.g. the console.
851 cdev
= sch
->dev
.driver_data
;
852 device_initialize(&cdev
->dev
);
853 ccw_device_register(cdev
);
854 subchannel_add_files(&sch
->dev
);
856 * Check if the device is already online. If it is
857 * the reference count needs to be corrected
858 * (see ccw_device_online and css_init_done for the
861 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
862 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
863 cdev
->private->state
!= DEV_STATE_BOXED
)
864 get_device(&cdev
->dev
);
867 cdev
= kzalloc (sizeof(*cdev
), GFP_KERNEL
);
870 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
871 GFP_KERNEL
| GFP_DMA
);
872 if (!cdev
->private) {
876 cdev
->private->cdev
= cdev
;
877 atomic_set(&cdev
->private->onoff
, 0);
878 cdev
->dev
.parent
= &sch
->dev
;
879 cdev
->dev
.release
= ccw_device_release
;
880 INIT_LIST_HEAD(&cdev
->private->kick_work
.entry
);
881 /* Do first half of device_register. */
882 device_initialize(&cdev
->dev
);
884 if (!get_device(&sch
->dev
)) {
885 if (cdev
->dev
.release
)
886 cdev
->dev
.release(&cdev
->dev
);
890 rc
= io_subchannel_recog(cdev
, sch
);
892 spin_lock_irqsave(&sch
->lock
, flags
);
893 sch
->dev
.driver_data
= NULL
;
894 spin_unlock_irqrestore(&sch
->lock
, flags
);
895 if (cdev
->dev
.release
)
896 cdev
->dev
.release(&cdev
->dev
);
902 static void ccw_device_unregister(struct work_struct
*work
)
904 struct ccw_device_private
*priv
;
905 struct ccw_device
*cdev
;
907 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
909 if (test_and_clear_bit(1, &cdev
->private->registered
))
910 device_unregister(&cdev
->dev
);
911 put_device(&cdev
->dev
);
915 io_subchannel_remove (struct subchannel
*sch
)
917 struct ccw_device
*cdev
;
920 if (!sch
->dev
.driver_data
)
922 cdev
= sch
->dev
.driver_data
;
923 /* Set ccw device to not operational and drop reference. */
924 spin_lock_irqsave(cdev
->ccwlock
, flags
);
925 sch
->dev
.driver_data
= NULL
;
926 cdev
->private->state
= DEV_STATE_NOT_OPER
;
927 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
929 * Put unregistration on workqueue to avoid livelocks on the css bus
932 if (get_device(&cdev
->dev
)) {
933 PREPARE_WORK(&cdev
->private->kick_work
,
934 ccw_device_unregister
);
935 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
941 io_subchannel_notify(struct device
*dev
, int event
)
943 struct ccw_device
*cdev
;
945 cdev
= dev
->driver_data
;
952 return cdev
->drv
->notify
? cdev
->drv
->notify(cdev
, event
) : 0;
956 io_subchannel_verify(struct device
*dev
)
958 struct ccw_device
*cdev
;
960 cdev
= dev
->driver_data
;
962 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
966 io_subchannel_ioterm(struct device
*dev
)
968 struct ccw_device
*cdev
;
970 cdev
= dev
->driver_data
;
973 /* Internal I/O will be retried by the interrupt handler. */
974 if (cdev
->private->flags
.intretry
)
976 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
978 cdev
->handler(cdev
, cdev
->private->intparm
,
983 io_subchannel_shutdown(struct subchannel
*sch
)
985 struct ccw_device
*cdev
;
988 cdev
= sch
->dev
.driver_data
;
990 if (cio_is_console(sch
->schid
))
992 if (!sch
->schib
.pmcw
.ena
)
995 ret
= cio_disable_subchannel(sch
);
997 /* Subchannel is disabled, we're done. */
999 cdev
->private->state
= DEV_STATE_QUIESCE
;
1001 cdev
->handler(cdev
, cdev
->private->intparm
,
1003 ret
= ccw_device_cancel_halt_clear(cdev
);
1004 if (ret
== -EBUSY
) {
1005 ccw_device_set_timeout(cdev
, HZ
/10);
1006 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1008 cio_disable_subchannel(sch
);
1011 #ifdef CONFIG_CCW_CONSOLE
1012 static struct ccw_device console_cdev
;
1013 static struct ccw_device_private console_private
;
1014 static int console_cdev_in_use
;
1017 ccw_device_console_enable (struct ccw_device
*cdev
, struct subchannel
*sch
)
1021 /* Initialize the ccw_device structure. */
1022 cdev
->dev
.parent
= &sch
->dev
;
1023 rc
= io_subchannel_recog(cdev
, sch
);
1027 /* Now wait for the async. recognition to come to an end. */
1028 spin_lock_irq(cdev
->ccwlock
);
1029 while (!dev_fsm_final_state(cdev
))
1032 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1034 ccw_device_online(cdev
);
1035 while (!dev_fsm_final_state(cdev
))
1037 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1041 spin_unlock_irq(cdev
->ccwlock
);
1046 ccw_device_probe_console(void)
1048 struct subchannel
*sch
;
1051 if (xchg(&console_cdev_in_use
, 1) != 0)
1052 return ERR_PTR(-EBUSY
);
1053 sch
= cio_probe_console();
1055 console_cdev_in_use
= 0;
1056 return (void *) sch
;
1058 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1059 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1060 console_cdev
.private = &console_private
;
1061 console_private
.cdev
= &console_cdev
;
1062 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1064 cio_release_console();
1065 console_cdev_in_use
= 0;
1066 return ERR_PTR(ret
);
1068 console_cdev
.online
= 1;
1069 return &console_cdev
;
1074 * get ccw_device matching the busid, but only if owned by cdrv
1077 __ccwdev_check_busid(struct device
*dev
, void *id
)
1083 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1088 get_ccwdev_by_busid(struct ccw_driver
*cdrv
, const char *bus_id
)
1091 struct device_driver
*drv
;
1093 drv
= get_driver(&cdrv
->driver
);
1097 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1098 __ccwdev_check_busid
);
1101 return dev
? to_ccwdev(dev
) : NULL
;
1104 /************************** device driver handling ************************/
1106 /* This is the implementation of the ccw_driver class. The probe, remove
1107 * and release methods are initially very similar to the device_driver
1108 * implementations, with the difference that they have ccw_device
1111 * A ccw driver also contains the information that is needed for
1115 ccw_device_probe (struct device
*dev
)
1117 struct ccw_device
*cdev
= to_ccwdev(dev
);
1118 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1121 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1123 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1134 ccw_device_remove (struct device
*dev
)
1136 struct ccw_device
*cdev
= to_ccwdev(dev
);
1137 struct ccw_driver
*cdrv
= cdev
->drv
;
1140 pr_debug("removing device %s\n", cdev
->dev
.bus_id
);
1145 spin_lock_irq(cdev
->ccwlock
);
1146 ret
= ccw_device_offline(cdev
);
1147 spin_unlock_irq(cdev
->ccwlock
);
1149 wait_event(cdev
->private->wait_q
,
1150 dev_fsm_final_state(cdev
));
1152 //FIXME: we can't fail!
1153 pr_debug("ccw_device_offline returned %d, device %s\n",
1154 ret
, cdev
->dev
.bus_id
);
1156 ccw_device_set_timeout(cdev
, 0);
1161 struct bus_type ccw_bus_type
= {
1163 .match
= ccw_bus_match
,
1164 .uevent
= ccw_uevent
,
1165 .probe
= ccw_device_probe
,
1166 .remove
= ccw_device_remove
,
1170 ccw_driver_register (struct ccw_driver
*cdriver
)
1172 struct device_driver
*drv
= &cdriver
->driver
;
1174 drv
->bus
= &ccw_bus_type
;
1175 drv
->name
= cdriver
->name
;
1177 return driver_register(drv
);
1181 ccw_driver_unregister (struct ccw_driver
*cdriver
)
1183 driver_unregister(&cdriver
->driver
);
1186 /* Helper func for qdio. */
1187 struct subchannel_id
1188 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1190 struct subchannel
*sch
;
1192 sch
= to_subchannel(cdev
->dev
.parent
);
1196 MODULE_LICENSE("GPL");
1197 EXPORT_SYMBOL(ccw_device_set_online
);
1198 EXPORT_SYMBOL(ccw_device_set_offline
);
1199 EXPORT_SYMBOL(ccw_driver_register
);
1200 EXPORT_SYMBOL(ccw_driver_unregister
);
1201 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1202 EXPORT_SYMBOL(ccw_bus_type
);
1203 EXPORT_SYMBOL(ccw_device_work
);
1204 EXPORT_SYMBOL(ccw_device_notify_work
);
1205 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);