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 */
27 #include "cio_debug.h"
32 /******************* bus type handling ***********************/
34 /* The Linux driver model distinguishes between a bus type and
35 * the bus itself. Of course we only have one channel
36 * subsystem driver and one channel system per machine, but
37 * we still use the abstraction. T.R. says it's a good idea. */
39 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
41 struct ccw_device
*cdev
= to_ccwdev(dev
);
42 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
43 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
48 found
= ccw_device_id_match(ids
, &cdev
->id
);
52 cdev
->id
.driver_info
= found
->driver_info
;
57 /* Store modalias string delimited by prefix/suffix string into buffer with
58 * specified size. Return length of resulting string (excluding trailing '\0')
59 * even if string doesn't fit buffer (snprintf semantics). */
60 static int snprint_alias(char *buf
, size_t size
,
61 struct ccw_device_id
*id
, const char *suffix
)
65 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
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
);
90 char modalias_buf
[30];
93 ret
= add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
94 "CU_TYPE=%04X", id
->cu_type
);
99 ret
= add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
100 "CU_MODEL=%02X", id
->cu_model
);
104 /* The next two can be zero, that's ok for us */
106 ret
= add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
107 "DEV_TYPE=%04X", id
->dev_type
);
112 ret
= add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
113 "DEV_MODEL=%02X", id
->dev_model
);
118 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
119 ret
= add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
120 "MODALIAS=%s", modalias_buf
);
127 struct bus_type ccw_bus_type
;
129 static int io_subchannel_probe (struct subchannel
*);
130 static int io_subchannel_remove (struct subchannel
*);
131 static int io_subchannel_notify(struct device
*, int);
132 static void io_subchannel_verify(struct device
*);
133 static void io_subchannel_ioterm(struct device
*);
134 static void io_subchannel_shutdown(struct subchannel
*);
136 static struct css_driver io_subchannel_driver
= {
137 .subchannel_type
= SUBCHANNEL_TYPE_IO
,
139 .name
= "io_subchannel",
140 .bus
= &css_bus_type
,
142 .irq
= io_subchannel_irq
,
143 .notify
= io_subchannel_notify
,
144 .verify
= io_subchannel_verify
,
145 .termination
= io_subchannel_ioterm
,
146 .probe
= io_subchannel_probe
,
147 .remove
= io_subchannel_remove
,
148 .shutdown
= io_subchannel_shutdown
,
151 struct workqueue_struct
*ccw_device_work
;
152 struct workqueue_struct
*ccw_device_notify_work
;
153 wait_queue_head_t ccw_device_init_wq
;
154 atomic_t ccw_device_init_count
;
157 init_ccw_bus_type (void)
161 init_waitqueue_head(&ccw_device_init_wq
);
162 atomic_set(&ccw_device_init_count
, 0);
164 ccw_device_work
= create_singlethread_workqueue("cio");
165 if (!ccw_device_work
)
166 return -ENOMEM
; /* FIXME: better errno ? */
167 ccw_device_notify_work
= create_singlethread_workqueue("cio_notify");
168 if (!ccw_device_notify_work
) {
169 ret
= -ENOMEM
; /* FIXME: better errno ? */
172 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
174 ret
= -ENOMEM
; /* FIXME: better errno ? */
177 if ((ret
= bus_register (&ccw_bus_type
)))
180 if ((ret
= driver_register(&io_subchannel_driver
.drv
)))
183 wait_event(ccw_device_init_wq
,
184 atomic_read(&ccw_device_init_count
) == 0);
185 flush_workqueue(ccw_device_work
);
189 destroy_workqueue(ccw_device_work
);
190 if (ccw_device_notify_work
)
191 destroy_workqueue(ccw_device_notify_work
);
193 destroy_workqueue(slow_path_wq
);
198 cleanup_ccw_bus_type (void)
200 driver_unregister(&io_subchannel_driver
.drv
);
201 bus_unregister(&ccw_bus_type
);
202 destroy_workqueue(ccw_device_notify_work
);
203 destroy_workqueue(ccw_device_work
);
206 subsys_initcall(init_ccw_bus_type
);
207 module_exit(cleanup_ccw_bus_type
);
209 /************************ device handling **************************/
212 * A ccw_device has some interfaces in sysfs in addition to the
214 * The following entries are designed to export the information which
215 * resided in 2.4 in /proc/subchannels. Subchannel and device number
216 * are obvious, so they don't have an entry :)
217 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
220 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
222 struct subchannel
*sch
= to_subchannel(dev
);
223 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
228 for (chp
= 0; chp
< 8; chp
++) {
230 if (ssd
->path_mask
& mask
)
231 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
233 ret
+= sprintf(buf
+ ret
, "00 ");
235 ret
+= sprintf (buf
+ret
, "\n");
236 return min((ssize_t
)PAGE_SIZE
, ret
);
240 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
242 struct subchannel
*sch
= to_subchannel(dev
);
243 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
245 return sprintf (buf
, "%02x %02x %02x\n",
246 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
250 devtype_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
);
255 if (id
->dev_type
!= 0)
256 return sprintf(buf
, "%04x/%02x\n",
257 id
->dev_type
, id
->dev_model
);
259 return sprintf(buf
, "n/a\n");
263 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
265 struct ccw_device
*cdev
= to_ccwdev(dev
);
266 struct ccw_device_id
*id
= &(cdev
->id
);
268 return sprintf(buf
, "%04x/%02x\n",
269 id
->cu_type
, id
->cu_model
);
273 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
275 struct ccw_device
*cdev
= to_ccwdev(dev
);
276 struct ccw_device_id
*id
= &(cdev
->id
);
279 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
281 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
285 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
287 struct ccw_device
*cdev
= to_ccwdev(dev
);
289 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
292 int ccw_device_is_orphan(struct ccw_device
*cdev
)
294 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
297 static void ccw_device_unregister(struct ccw_device
*cdev
)
299 if (test_and_clear_bit(1, &cdev
->private->registered
))
300 device_del(&cdev
->dev
);
303 static void ccw_device_remove_orphan_cb(struct device
*dev
)
305 struct ccw_device
*cdev
= to_ccwdev(dev
);
307 ccw_device_unregister(cdev
);
308 put_device(&cdev
->dev
);
311 static void ccw_device_remove_sch_cb(struct device
*dev
)
313 struct subchannel
*sch
;
315 sch
= to_subchannel(dev
);
316 css_sch_device_unregister(sch
);
317 /* Reset intparm to zeroes. */
318 sch
->schib
.pmcw
.intparm
= 0;
320 put_device(&sch
->dev
);
324 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
330 * Forced offline in disconnected state means
331 * 'throw away device'.
333 if (ccw_device_is_orphan(cdev
)) {
335 * Deregister ccw device.
336 * Unfortunately, we cannot do this directly from the
339 spin_lock_irqsave(cdev
->ccwlock
, flags
);
340 cdev
->private->state
= DEV_STATE_NOT_OPER
;
341 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
342 rc
= device_schedule_callback(&cdev
->dev
,
343 ccw_device_remove_orphan_cb
);
345 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
347 cdev
->private->dev_id
.ssid
,
348 cdev
->private->dev_id
.devno
);
351 /* Deregister subchannel, which will kill the ccw device. */
352 rc
= device_schedule_callback(cdev
->dev
.parent
,
353 ccw_device_remove_sch_cb
);
355 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
357 cdev
->private->dev_id
.ssid
,
358 cdev
->private->dev_id
.devno
);
362 * ccw_device_set_offline() - disable a ccw device for I/O
363 * @cdev: target ccw device
365 * This function calls the driver's set_offline() function for @cdev, if
366 * given, and then disables @cdev.
368 * %0 on success and a negative error value on failure.
370 * enabled, ccw device lock not held
372 int ccw_device_set_offline(struct ccw_device
*cdev
)
378 if (!cdev
->online
|| !cdev
->drv
)
381 if (cdev
->drv
->set_offline
) {
382 ret
= cdev
->drv
->set_offline(cdev
);
387 spin_lock_irq(cdev
->ccwlock
);
388 ret
= ccw_device_offline(cdev
);
389 if (ret
== -ENODEV
) {
390 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
391 cdev
->private->state
= DEV_STATE_OFFLINE
;
392 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
394 spin_unlock_irq(cdev
->ccwlock
);
397 spin_unlock_irq(cdev
->ccwlock
);
399 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
401 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
402 "device 0.%x.%04x\n",
403 ret
, cdev
->private->dev_id
.ssid
,
404 cdev
->private->dev_id
.devno
);
411 * ccw_device_set_online() - enable a ccw device for I/O
412 * @cdev: target ccw device
414 * This function first enables @cdev and then calls the driver's set_online()
415 * function for @cdev, if given. If set_online() returns an error, @cdev is
418 * %0 on success and a negative error value on failure.
420 * enabled, ccw device lock not held
422 int ccw_device_set_online(struct ccw_device
*cdev
)
428 if (cdev
->online
|| !cdev
->drv
)
431 spin_lock_irq(cdev
->ccwlock
);
432 ret
= ccw_device_online(cdev
);
433 spin_unlock_irq(cdev
->ccwlock
);
435 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
437 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
438 "device 0.%x.%04x\n",
439 ret
, cdev
->private->dev_id
.ssid
,
440 cdev
->private->dev_id
.devno
);
443 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
445 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
449 spin_lock_irq(cdev
->ccwlock
);
450 ret
= ccw_device_offline(cdev
);
451 spin_unlock_irq(cdev
->ccwlock
);
453 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
455 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
456 "device 0.%x.%04x\n",
457 ret
, cdev
->private->dev_id
.ssid
,
458 cdev
->private->dev_id
.devno
);
459 return (ret
== 0) ? -ENODEV
: ret
;
462 static void online_store_handle_offline(struct ccw_device
*cdev
)
464 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
465 ccw_device_remove_disconnected(cdev
);
466 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
467 ccw_device_set_offline(cdev
);
470 static int online_store_recog_and_online(struct ccw_device
*cdev
)
474 /* Do device recognition, if needed. */
475 if (cdev
->id
.cu_type
== 0) {
476 ret
= ccw_device_recognition(cdev
);
478 CIO_MSG_EVENT(0, "Couldn't start recognition "
479 "for device 0.%x.%04x (ret=%d)\n",
480 cdev
->private->dev_id
.ssid
,
481 cdev
->private->dev_id
.devno
, ret
);
484 wait_event(cdev
->private->wait_q
,
485 cdev
->private->flags
.recog_done
);
487 if (cdev
->drv
&& cdev
->drv
->set_online
)
488 ccw_device_set_online(cdev
);
491 static void online_store_handle_online(struct ccw_device
*cdev
, int force
)
495 ret
= online_store_recog_and_online(cdev
);
498 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
499 ret
= ccw_device_stlck(cdev
);
502 "ccw_device_stlck returned %d!\n", ret
);
505 if (cdev
->id
.cu_type
== 0)
506 cdev
->private->state
= DEV_STATE_NOT_OPER
;
507 online_store_recog_and_online(cdev
);
512 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
513 const char *buf
, size_t count
)
515 struct ccw_device
*cdev
= to_ccwdev(dev
);
519 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
522 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
523 atomic_set(&cdev
->private->onoff
, 0);
526 if (!strncmp(buf
, "force\n", count
)) {
531 i
= simple_strtoul(buf
, &tmp
, 16);
536 online_store_handle_offline(cdev
);
539 online_store_handle_online(cdev
, force
);
545 module_put(cdev
->drv
->owner
);
546 atomic_set(&cdev
->private->onoff
, 0);
551 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
553 struct ccw_device
*cdev
= to_ccwdev(dev
);
554 struct subchannel
*sch
;
556 if (ccw_device_is_orphan(cdev
))
557 return sprintf(buf
, "no device\n");
558 switch (cdev
->private->state
) {
559 case DEV_STATE_BOXED
:
560 return sprintf(buf
, "boxed\n");
561 case DEV_STATE_DISCONNECTED
:
562 case DEV_STATE_DISCONNECTED_SENSE_ID
:
563 case DEV_STATE_NOT_OPER
:
564 sch
= to_subchannel(dev
->parent
);
566 return sprintf(buf
, "no path\n");
568 return sprintf(buf
, "no device\n");
570 /* All other states considered fine. */
571 return sprintf(buf
, "good\n");
575 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
576 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
577 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
578 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
579 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
580 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
581 extern struct device_attribute dev_attr_cmb_enable
;
582 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
584 static struct attribute
* subch_attrs
[] = {
585 &dev_attr_chpids
.attr
,
586 &dev_attr_pimpampom
.attr
,
590 static struct attribute_group subch_attr_group
= {
591 .attrs
= subch_attrs
,
594 struct attribute_group
*subch_attr_groups
[] = {
599 static struct attribute
* ccwdev_attrs
[] = {
600 &dev_attr_devtype
.attr
,
601 &dev_attr_cutype
.attr
,
602 &dev_attr_modalias
.attr
,
603 &dev_attr_online
.attr
,
604 &dev_attr_cmb_enable
.attr
,
605 &dev_attr_availability
.attr
,
609 static struct attribute_group ccwdev_attr_group
= {
610 .attrs
= ccwdev_attrs
,
613 static struct attribute_group
*ccwdev_attr_groups
[] = {
618 /* this is a simple abstraction for device_register that sets the
619 * correct bus type and adds the bus specific files */
620 static int ccw_device_register(struct ccw_device
*cdev
)
622 struct device
*dev
= &cdev
->dev
;
625 dev
->bus
= &ccw_bus_type
;
627 if ((ret
= device_add(dev
)))
630 set_bit(1, &cdev
->private->registered
);
635 struct ccw_dev_id dev_id
;
636 struct ccw_device
* sibling
;
640 match_devno(struct device
* dev
, void * data
)
642 struct match_data
* d
= data
;
643 struct ccw_device
* cdev
;
645 cdev
= to_ccwdev(dev
);
646 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
647 !ccw_device_is_orphan(cdev
) &&
648 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
649 (cdev
!= d
->sibling
))
654 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
655 struct ccw_device
*sibling
)
658 struct match_data data
;
660 data
.dev_id
= *dev_id
;
661 data
.sibling
= sibling
;
662 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
664 return dev
? to_ccwdev(dev
) : NULL
;
667 static int match_orphan(struct device
*dev
, void *data
)
669 struct ccw_dev_id
*dev_id
;
670 struct ccw_device
*cdev
;
673 cdev
= to_ccwdev(dev
);
674 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
677 static struct ccw_device
*
678 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
679 struct ccw_dev_id
*dev_id
)
683 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
686 return dev
? to_ccwdev(dev
) : NULL
;
690 ccw_device_add_changed(struct work_struct
*work
)
692 struct ccw_device_private
*priv
;
693 struct ccw_device
*cdev
;
695 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
697 if (device_add(&cdev
->dev
)) {
698 put_device(&cdev
->dev
);
701 set_bit(1, &cdev
->private->registered
);
704 void ccw_device_do_unreg_rereg(struct work_struct
*work
)
706 struct ccw_device_private
*priv
;
707 struct ccw_device
*cdev
;
708 struct subchannel
*sch
;
710 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
712 sch
= to_subchannel(cdev
->dev
.parent
);
714 ccw_device_unregister(cdev
);
715 PREPARE_WORK(&cdev
->private->kick_work
,
716 ccw_device_add_changed
);
717 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
721 ccw_device_release(struct device
*dev
)
723 struct ccw_device
*cdev
;
725 cdev
= to_ccwdev(dev
);
726 kfree(cdev
->private);
730 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
732 struct ccw_device
*cdev
;
734 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
736 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
737 GFP_KERNEL
| GFP_DMA
);
742 return ERR_PTR(-ENOMEM
);
745 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
746 struct ccw_device
*cdev
)
748 cdev
->private->cdev
= cdev
;
749 atomic_set(&cdev
->private->onoff
, 0);
750 cdev
->dev
.parent
= &sch
->dev
;
751 cdev
->dev
.release
= ccw_device_release
;
752 INIT_LIST_HEAD(&cdev
->private->kick_work
.entry
);
753 cdev
->dev
.groups
= ccwdev_attr_groups
;
754 /* Do first half of device_register. */
755 device_initialize(&cdev
->dev
);
756 if (!get_device(&sch
->dev
)) {
757 if (cdev
->dev
.release
)
758 cdev
->dev
.release(&cdev
->dev
);
764 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
766 struct ccw_device
*cdev
;
769 cdev
= io_subchannel_allocate_dev(sch
);
771 ret
= io_subchannel_initialize_dev(sch
, cdev
);
780 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
782 static void sch_attach_device(struct subchannel
*sch
,
783 struct ccw_device
*cdev
)
785 css_update_ssd_info(sch
);
786 spin_lock_irq(sch
->lock
);
787 sch
->dev
.driver_data
= cdev
;
788 cdev
->private->schid
= sch
->schid
;
789 cdev
->ccwlock
= sch
->lock
;
790 device_trigger_reprobe(sch
);
791 spin_unlock_irq(sch
->lock
);
794 static void sch_attach_disconnected_device(struct subchannel
*sch
,
795 struct ccw_device
*cdev
)
797 struct subchannel
*other_sch
;
800 other_sch
= to_subchannel(get_device(cdev
->dev
.parent
));
801 ret
= device_move(&cdev
->dev
, &sch
->dev
);
803 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
804 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
805 cdev
->private->dev_id
.devno
, ret
);
806 put_device(&other_sch
->dev
);
809 other_sch
->dev
.driver_data
= NULL
;
810 /* No need to keep a subchannel without ccw device around. */
811 css_sch_device_unregister(other_sch
);
812 put_device(&other_sch
->dev
);
813 sch_attach_device(sch
, cdev
);
816 static void sch_attach_orphaned_device(struct subchannel
*sch
,
817 struct ccw_device
*cdev
)
821 /* Try to move the ccw device to its new subchannel. */
822 ret
= device_move(&cdev
->dev
, &sch
->dev
);
824 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
825 "failed (ret=%d)!\n",
826 cdev
->private->dev_id
.ssid
,
827 cdev
->private->dev_id
.devno
, ret
);
830 sch_attach_device(sch
, cdev
);
833 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
835 struct ccw_device
*cdev
;
837 /* Need to allocate a new ccw device. */
838 cdev
= io_subchannel_create_ccwdev(sch
);
840 /* OK, we did everything we could... */
841 css_sch_device_unregister(sch
);
844 spin_lock_irq(sch
->lock
);
845 sch
->dev
.driver_data
= cdev
;
846 spin_unlock_irq(sch
->lock
);
847 /* Start recognition for the new ccw device. */
848 if (io_subchannel_recog(cdev
, sch
)) {
849 spin_lock_irq(sch
->lock
);
850 sch
->dev
.driver_data
= NULL
;
851 spin_unlock_irq(sch
->lock
);
852 if (cdev
->dev
.release
)
853 cdev
->dev
.release(&cdev
->dev
);
854 css_sch_device_unregister(sch
);
859 void ccw_device_move_to_orphanage(struct work_struct
*work
)
861 struct ccw_device_private
*priv
;
862 struct ccw_device
*cdev
;
863 struct ccw_device
*replacing_cdev
;
864 struct subchannel
*sch
;
866 struct channel_subsystem
*css
;
867 struct ccw_dev_id dev_id
;
869 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
871 sch
= to_subchannel(cdev
->dev
.parent
);
872 css
= to_css(sch
->dev
.parent
);
873 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
874 dev_id
.ssid
= sch
->schid
.ssid
;
877 * Move the orphaned ccw device to the orphanage so the replacing
878 * ccw device can take its place on the subchannel.
880 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
);
882 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
883 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
884 cdev
->private->dev_id
.devno
, ret
);
887 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
889 * Search for the replacing ccw device
890 * - among the disconnected devices
893 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
894 if (replacing_cdev
) {
895 sch_attach_disconnected_device(sch
, replacing_cdev
);
898 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
899 if (replacing_cdev
) {
900 sch_attach_orphaned_device(sch
, replacing_cdev
);
903 sch_create_and_recog_new_device(sch
);
907 * Register recognized device.
910 io_subchannel_register(struct work_struct
*work
)
912 struct ccw_device_private
*priv
;
913 struct ccw_device
*cdev
;
914 struct subchannel
*sch
;
918 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
920 sch
= to_subchannel(cdev
->dev
.parent
);
921 css_update_ssd_info(sch
);
923 * io_subchannel_register() will also be called after device
924 * recognition has been done for a boxed device (which will already
925 * be registered). We need to reprobe since we may now have sense id
928 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
930 ret
= device_reprobe(&cdev
->dev
);
932 /* We can't do much here. */
933 CIO_MSG_EVENT(2, "device_reprobe() returned"
934 " %d for 0.%x.%04x\n", ret
,
935 cdev
->private->dev_id
.ssid
,
936 cdev
->private->dev_id
.devno
);
941 * Now we know this subchannel will stay, we can throw
942 * our delayed uevent.
944 sch
->dev
.uevent_suppress
= 0;
945 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
946 /* make it known to the system */
947 ret
= ccw_device_register(cdev
);
949 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
950 cdev
->private->dev_id
.ssid
,
951 cdev
->private->dev_id
.devno
, ret
);
952 put_device(&cdev
->dev
);
953 spin_lock_irqsave(sch
->lock
, flags
);
954 sch
->dev
.driver_data
= NULL
;
955 spin_unlock_irqrestore(sch
->lock
, flags
);
956 kfree (cdev
->private);
958 put_device(&sch
->dev
);
959 if (atomic_dec_and_test(&ccw_device_init_count
))
960 wake_up(&ccw_device_init_wq
);
963 put_device(&cdev
->dev
);
965 cdev
->private->flags
.recog_done
= 1;
966 put_device(&sch
->dev
);
967 wake_up(&cdev
->private->wait_q
);
968 if (atomic_dec_and_test(&ccw_device_init_count
))
969 wake_up(&ccw_device_init_wq
);
973 ccw_device_call_sch_unregister(struct work_struct
*work
)
975 struct ccw_device_private
*priv
;
976 struct ccw_device
*cdev
;
977 struct subchannel
*sch
;
979 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
981 sch
= to_subchannel(cdev
->dev
.parent
);
982 css_sch_device_unregister(sch
);
983 /* Reset intparm to zeroes. */
984 sch
->schib
.pmcw
.intparm
= 0;
986 put_device(&cdev
->dev
);
987 put_device(&sch
->dev
);
991 * subchannel recognition done. Called from the state machine.
994 io_subchannel_recog_done(struct ccw_device
*cdev
)
996 struct subchannel
*sch
;
998 if (css_init_done
== 0) {
999 cdev
->private->flags
.recog_done
= 1;
1002 switch (cdev
->private->state
) {
1003 case DEV_STATE_NOT_OPER
:
1004 cdev
->private->flags
.recog_done
= 1;
1005 /* Remove device found not operational. */
1006 if (!get_device(&cdev
->dev
))
1008 sch
= to_subchannel(cdev
->dev
.parent
);
1009 PREPARE_WORK(&cdev
->private->kick_work
,
1010 ccw_device_call_sch_unregister
);
1011 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1012 if (atomic_dec_and_test(&ccw_device_init_count
))
1013 wake_up(&ccw_device_init_wq
);
1015 case DEV_STATE_BOXED
:
1016 /* Device did not respond in time. */
1017 case DEV_STATE_OFFLINE
:
1019 * We can't register the device in interrupt context so
1020 * we schedule a work item.
1022 if (!get_device(&cdev
->dev
))
1024 PREPARE_WORK(&cdev
->private->kick_work
,
1025 io_subchannel_register
);
1026 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1032 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
1035 struct ccw_device_private
*priv
;
1037 sch
->dev
.driver_data
= cdev
;
1038 sch
->driver
= &io_subchannel_driver
;
1039 cdev
->ccwlock
= sch
->lock
;
1041 /* Init private data. */
1042 priv
= cdev
->private;
1043 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1044 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
1045 priv
->schid
= sch
->schid
;
1046 priv
->state
= DEV_STATE_NOT_OPER
;
1047 INIT_LIST_HEAD(&priv
->cmb_list
);
1048 init_waitqueue_head(&priv
->wait_q
);
1049 init_timer(&priv
->timer
);
1051 /* Set an initial name for the device. */
1052 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
1053 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1055 /* Increase counter of devices currently in recognition. */
1056 atomic_inc(&ccw_device_init_count
);
1058 /* Start async. device sensing. */
1059 spin_lock_irq(sch
->lock
);
1060 rc
= ccw_device_recognition(cdev
);
1061 spin_unlock_irq(sch
->lock
);
1063 if (atomic_dec_and_test(&ccw_device_init_count
))
1064 wake_up(&ccw_device_init_wq
);
1069 static void ccw_device_move_to_sch(struct work_struct
*work
)
1071 struct ccw_device_private
*priv
;
1073 struct subchannel
*sch
;
1074 struct ccw_device
*cdev
;
1075 struct subchannel
*former_parent
;
1077 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1080 former_parent
= ccw_device_is_orphan(cdev
) ?
1081 NULL
: to_subchannel(get_device(cdev
->dev
.parent
));
1082 mutex_lock(&sch
->reg_mutex
);
1083 /* Try to move the ccw device to its new subchannel. */
1084 rc
= device_move(&cdev
->dev
, &sch
->dev
);
1085 mutex_unlock(&sch
->reg_mutex
);
1087 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1088 "0.%x.%04x failed (ret=%d)!\n",
1089 cdev
->private->dev_id
.ssid
,
1090 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1091 sch
->schid
.sch_no
, rc
);
1092 css_sch_device_unregister(sch
);
1095 if (former_parent
) {
1096 spin_lock_irq(former_parent
->lock
);
1097 former_parent
->dev
.driver_data
= NULL
;
1098 spin_unlock_irq(former_parent
->lock
);
1099 css_sch_device_unregister(former_parent
);
1100 /* Reset intparm to zeroes. */
1101 former_parent
->schib
.pmcw
.intparm
= 0;
1102 cio_modify(former_parent
);
1104 sch_attach_device(sch
, cdev
);
1107 put_device(&former_parent
->dev
);
1108 put_device(&cdev
->dev
);
1112 io_subchannel_probe (struct subchannel
*sch
)
1114 struct ccw_device
*cdev
;
1116 unsigned long flags
;
1117 struct ccw_dev_id dev_id
;
1119 if (sch
->dev
.driver_data
) {
1121 * This subchannel already has an associated ccw_device.
1122 * Register it and exit. This happens for all early
1123 * device, e.g. the console.
1125 cdev
= sch
->dev
.driver_data
;
1126 cdev
->dev
.groups
= ccwdev_attr_groups
;
1127 device_initialize(&cdev
->dev
);
1128 ccw_device_register(cdev
);
1130 * Check if the device is already online. If it is
1131 * the reference count needs to be corrected
1132 * (see ccw_device_online and css_init_done for the
1135 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1136 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1137 cdev
->private->state
!= DEV_STATE_BOXED
)
1138 get_device(&cdev
->dev
);
1142 * First check if a fitting device may be found amongst the
1143 * disconnected devices or in the orphanage.
1145 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1146 dev_id
.ssid
= sch
->schid
.ssid
;
1147 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1149 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1153 * Schedule moving the device until when we have a registered
1154 * subchannel to move to and succeed the probe. We can
1155 * unregister later again, when the probe is through.
1157 cdev
->private->sch
= sch
;
1158 PREPARE_WORK(&cdev
->private->kick_work
,
1159 ccw_device_move_to_sch
);
1160 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1163 cdev
= io_subchannel_create_ccwdev(sch
);
1165 return PTR_ERR(cdev
);
1167 rc
= io_subchannel_recog(cdev
, sch
);
1169 spin_lock_irqsave(sch
->lock
, flags
);
1170 sch
->dev
.driver_data
= NULL
;
1171 spin_unlock_irqrestore(sch
->lock
, flags
);
1172 if (cdev
->dev
.release
)
1173 cdev
->dev
.release(&cdev
->dev
);
1180 io_subchannel_remove (struct subchannel
*sch
)
1182 struct ccw_device
*cdev
;
1183 unsigned long flags
;
1185 if (!sch
->dev
.driver_data
)
1187 cdev
= sch
->dev
.driver_data
;
1188 /* Set ccw device to not operational and drop reference. */
1189 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1190 sch
->dev
.driver_data
= NULL
;
1191 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1192 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1193 ccw_device_unregister(cdev
);
1194 put_device(&cdev
->dev
);
1199 io_subchannel_notify(struct device
*dev
, int event
)
1201 struct ccw_device
*cdev
;
1203 cdev
= dev
->driver_data
;
1210 return cdev
->drv
->notify
? cdev
->drv
->notify(cdev
, event
) : 0;
1214 io_subchannel_verify(struct device
*dev
)
1216 struct ccw_device
*cdev
;
1218 cdev
= dev
->driver_data
;
1220 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1224 io_subchannel_ioterm(struct device
*dev
)
1226 struct ccw_device
*cdev
;
1228 cdev
= dev
->driver_data
;
1231 /* Internal I/O will be retried by the interrupt handler. */
1232 if (cdev
->private->flags
.intretry
)
1234 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1236 cdev
->handler(cdev
, cdev
->private->intparm
,
1241 io_subchannel_shutdown(struct subchannel
*sch
)
1243 struct ccw_device
*cdev
;
1246 cdev
= sch
->dev
.driver_data
;
1248 if (cio_is_console(sch
->schid
))
1250 if (!sch
->schib
.pmcw
.ena
)
1251 /* Nothing to do. */
1253 ret
= cio_disable_subchannel(sch
);
1255 /* Subchannel is disabled, we're done. */
1257 cdev
->private->state
= DEV_STATE_QUIESCE
;
1259 cdev
->handler(cdev
, cdev
->private->intparm
,
1261 ret
= ccw_device_cancel_halt_clear(cdev
);
1262 if (ret
== -EBUSY
) {
1263 ccw_device_set_timeout(cdev
, HZ
/10);
1264 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1266 cio_disable_subchannel(sch
);
1269 #ifdef CONFIG_CCW_CONSOLE
1270 static struct ccw_device console_cdev
;
1271 static struct ccw_device_private console_private
;
1272 static int console_cdev_in_use
;
1274 static DEFINE_SPINLOCK(ccw_console_lock
);
1276 spinlock_t
* cio_get_console_lock(void)
1278 return &ccw_console_lock
;
1282 ccw_device_console_enable (struct ccw_device
*cdev
, struct subchannel
*sch
)
1286 /* Initialize the ccw_device structure. */
1287 cdev
->dev
.parent
= &sch
->dev
;
1288 rc
= io_subchannel_recog(cdev
, sch
);
1292 /* Now wait for the async. recognition to come to an end. */
1293 spin_lock_irq(cdev
->ccwlock
);
1294 while (!dev_fsm_final_state(cdev
))
1297 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1299 ccw_device_online(cdev
);
1300 while (!dev_fsm_final_state(cdev
))
1302 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1306 spin_unlock_irq(cdev
->ccwlock
);
1311 ccw_device_probe_console(void)
1313 struct subchannel
*sch
;
1316 if (xchg(&console_cdev_in_use
, 1) != 0)
1317 return ERR_PTR(-EBUSY
);
1318 sch
= cio_probe_console();
1320 console_cdev_in_use
= 0;
1321 return (void *) sch
;
1323 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1324 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1325 console_cdev
.private = &console_private
;
1326 console_private
.cdev
= &console_cdev
;
1327 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1329 cio_release_console();
1330 console_cdev_in_use
= 0;
1331 return ERR_PTR(ret
);
1333 console_cdev
.online
= 1;
1334 return &console_cdev
;
1339 * get ccw_device matching the busid, but only if owned by cdrv
1342 __ccwdev_check_busid(struct device
*dev
, void *id
)
1348 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1353 * get_ccwdev_by_busid() - obtain device from a bus id
1354 * @cdrv: driver the device is owned by
1355 * @bus_id: bus id of the device to be searched
1357 * This function searches all devices owned by @cdrv for a device with a bus
1358 * id matching @bus_id.
1360 * If a match is found, its reference count of the found device is increased
1361 * and it is returned; else %NULL is returned.
1363 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1367 struct device_driver
*drv
;
1369 drv
= get_driver(&cdrv
->driver
);
1373 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1374 __ccwdev_check_busid
);
1377 return dev
? to_ccwdev(dev
) : NULL
;
1380 /************************** device driver handling ************************/
1382 /* This is the implementation of the ccw_driver class. The probe, remove
1383 * and release methods are initially very similar to the device_driver
1384 * implementations, with the difference that they have ccw_device
1387 * A ccw driver also contains the information that is needed for
1391 ccw_device_probe (struct device
*dev
)
1393 struct ccw_device
*cdev
= to_ccwdev(dev
);
1394 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1397 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1399 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1410 ccw_device_remove (struct device
*dev
)
1412 struct ccw_device
*cdev
= to_ccwdev(dev
);
1413 struct ccw_driver
*cdrv
= cdev
->drv
;
1420 spin_lock_irq(cdev
->ccwlock
);
1421 ret
= ccw_device_offline(cdev
);
1422 spin_unlock_irq(cdev
->ccwlock
);
1424 wait_event(cdev
->private->wait_q
,
1425 dev_fsm_final_state(cdev
));
1427 //FIXME: we can't fail!
1428 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1429 "device 0.%x.%04x\n",
1430 ret
, cdev
->private->dev_id
.ssid
,
1431 cdev
->private->dev_id
.devno
);
1433 ccw_device_set_timeout(cdev
, 0);
1438 static void ccw_device_shutdown(struct device
*dev
)
1440 struct ccw_device
*cdev
;
1442 cdev
= to_ccwdev(dev
);
1443 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1444 cdev
->drv
->shutdown(cdev
);
1448 struct bus_type ccw_bus_type
= {
1450 .match
= ccw_bus_match
,
1451 .uevent
= ccw_uevent
,
1452 .probe
= ccw_device_probe
,
1453 .remove
= ccw_device_remove
,
1454 .shutdown
= ccw_device_shutdown
,
1458 * ccw_driver_register() - register a ccw driver
1459 * @cdriver: driver to be registered
1461 * This function is mainly a wrapper around driver_register().
1463 * %0 on success and a negative error value on failure.
1465 int ccw_driver_register(struct ccw_driver
*cdriver
)
1467 struct device_driver
*drv
= &cdriver
->driver
;
1469 drv
->bus
= &ccw_bus_type
;
1470 drv
->name
= cdriver
->name
;
1472 return driver_register(drv
);
1476 * ccw_driver_unregister() - deregister a ccw driver
1477 * @cdriver: driver to be deregistered
1479 * This function is mainly a wrapper around driver_unregister().
1481 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
1483 driver_unregister(&cdriver
->driver
);
1486 /* Helper func for qdio. */
1487 struct subchannel_id
1488 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1490 struct subchannel
*sch
;
1492 sch
= to_subchannel(cdev
->dev
.parent
);
1496 MODULE_LICENSE("GPL");
1497 EXPORT_SYMBOL(ccw_device_set_online
);
1498 EXPORT_SYMBOL(ccw_device_set_offline
);
1499 EXPORT_SYMBOL(ccw_driver_register
);
1500 EXPORT_SYMBOL(ccw_driver_unregister
);
1501 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1502 EXPORT_SYMBOL(ccw_bus_type
);
1503 EXPORT_SYMBOL(ccw_device_work
);
1504 EXPORT_SYMBOL(ccw_device_notify_work
);
1505 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);