[S390] Get rid of a lot of sparse warnings.
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / s390 / cio / device.c
blob78ed65bf2f99091144f3c4f5f28dc90144b6b936
1 /*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
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>
22 #include <asm/cio.h>
23 #include <asm/param.h> /* HZ */
25 #include "cio.h"
26 #include "cio_debug.h"
27 #include "css.h"
28 #include "device.h"
29 #include "ioasm.h"
31 /******************* bus type handling ***********************/
33 /* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
37 static int
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;
44 if (!ids)
45 return 0;
47 found = ccw_device_id_match(ids, &cdev->id);
48 if (!found)
49 return 0;
51 cdev->id.driver_info = found->driver_info;
53 return 1;
56 /* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
59 static int snprint_alias(char *buf, size_t size, const char *prefix,
60 struct ccw_device_id *id, const char *suffix)
62 int len;
64 len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
65 id->cu_model);
66 if (len > size)
67 return len;
68 buf += len;
69 size -= len;
71 if (id->dev_type != 0)
72 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
73 id->dev_model, suffix);
74 else
75 len += snprintf(buf, size, "dtdm%s", suffix);
77 return len;
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);
87 int i = 0;
88 int len;
90 /* CU_TYPE= */
91 len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
92 if (len > buffer_size || i >= num_envp)
93 return -ENOMEM;
94 envp[i++] = buffer;
95 buffer += len;
96 buffer_size -= len;
98 /* CU_MODEL= */
99 len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
100 if (len > buffer_size || i >= num_envp)
101 return -ENOMEM;
102 envp[i++] = buffer;
103 buffer += len;
104 buffer_size -= len;
106 /* The next two can be zero, that's ok for us */
107 /* DEV_TYPE= */
108 len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
109 if (len > buffer_size || i >= num_envp)
110 return -ENOMEM;
111 envp[i++] = buffer;
112 buffer += len;
113 buffer_size -= len;
115 /* DEV_MODEL= */
116 len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
117 (unsigned char) id->dev_model) + 1;
118 if (len > buffer_size || i >= num_envp)
119 return -ENOMEM;
120 envp[i++] = buffer;
121 buffer += len;
122 buffer_size -= len;
124 /* MODALIAS= */
125 len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
126 if (len > buffer_size || i >= num_envp)
127 return -ENOMEM;
128 envp[i++] = buffer;
129 buffer += len;
130 buffer_size -= len;
132 envp[i] = NULL;
134 return 0;
137 struct bus_type ccw_bus_type;
139 static int io_subchannel_probe (struct subchannel *);
140 static int io_subchannel_remove (struct subchannel *);
141 static int io_subchannel_notify(struct device *, int);
142 static void io_subchannel_verify(struct device *);
143 static void io_subchannel_ioterm(struct device *);
144 static void io_subchannel_shutdown(struct subchannel *);
146 struct css_driver io_subchannel_driver = {
147 .subchannel_type = SUBCHANNEL_TYPE_IO,
148 .drv = {
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;
166 static int __init
167 init_ccw_bus_type (void)
169 int ret;
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 ? */
180 goto out_err;
182 slow_path_wq = create_singlethread_workqueue("kslowcrw");
183 if (!slow_path_wq) {
184 ret = -ENOMEM; /* FIXME: better errno ? */
185 goto out_err;
187 if ((ret = bus_register (&ccw_bus_type)))
188 goto out_err;
190 if ((ret = driver_register(&io_subchannel_driver.drv)))
191 goto out_err;
193 wait_event(ccw_device_init_wq,
194 atomic_read(&ccw_device_init_count) == 0);
195 flush_workqueue(ccw_device_work);
196 return 0;
197 out_err:
198 if (ccw_device_work)
199 destroy_workqueue(ccw_device_work);
200 if (ccw_device_notify_work)
201 destroy_workqueue(ccw_device_notify_work);
202 if (slow_path_wq)
203 destroy_workqueue(slow_path_wq);
204 return ret;
207 static void __exit
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
223 * standard ones.
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?
229 static ssize_t
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;
234 ssize_t ret = 0;
235 int chp;
237 if (ssd)
238 for (chp = 0; chp < 8; chp++)
239 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
240 else
241 ret += sprintf (buf, "n/a");
242 ret += sprintf (buf+ret, "\n");
243 return min((ssize_t)PAGE_SIZE, ret);
246 static ssize_t
247 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
249 struct subchannel *sch = to_subchannel(dev);
250 struct pmcw *pmcw = &sch->schib.pmcw;
252 return sprintf (buf, "%02x %02x %02x\n",
253 pmcw->pim, pmcw->pam, pmcw->pom);
256 static ssize_t
257 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
259 struct ccw_device *cdev = to_ccwdev(dev);
260 struct ccw_device_id *id = &(cdev->id);
262 if (id->dev_type != 0)
263 return sprintf(buf, "%04x/%02x\n",
264 id->dev_type, id->dev_model);
265 else
266 return sprintf(buf, "n/a\n");
269 static ssize_t
270 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
275 return sprintf(buf, "%04x/%02x\n",
276 id->cu_type, id->cu_model);
279 static ssize_t
280 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
282 struct ccw_device *cdev = to_ccwdev(dev);
283 struct ccw_device_id *id = &(cdev->id);
284 int len;
286 len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
288 return len > PAGE_SIZE ? PAGE_SIZE : len;
291 static ssize_t
292 online_show (struct device *dev, struct device_attribute *attr, char *buf)
294 struct ccw_device *cdev = to_ccwdev(dev);
296 return sprintf(buf, cdev->online ? "1\n" : "0\n");
299 int ccw_device_is_orphan(struct ccw_device *cdev)
301 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
304 static void ccw_device_unregister(struct work_struct *work)
306 struct ccw_device_private *priv;
307 struct ccw_device *cdev;
309 priv = container_of(work, struct ccw_device_private, kick_work);
310 cdev = priv->cdev;
311 if (test_and_clear_bit(1, &cdev->private->registered))
312 device_unregister(&cdev->dev);
313 put_device(&cdev->dev);
316 static void
317 ccw_device_remove_disconnected(struct ccw_device *cdev)
319 struct subchannel *sch;
320 unsigned long flags;
322 * Forced offline in disconnected state means
323 * 'throw away device'.
325 if (ccw_device_is_orphan(cdev)) {
326 /* Deregister ccw device. */
327 spin_lock_irqsave(cdev->ccwlock, flags);
328 cdev->private->state = DEV_STATE_NOT_OPER;
329 spin_unlock_irqrestore(cdev->ccwlock, flags);
330 if (get_device(&cdev->dev)) {
331 PREPARE_WORK(&cdev->private->kick_work,
332 ccw_device_unregister);
333 queue_work(ccw_device_work, &cdev->private->kick_work);
335 return ;
337 sch = to_subchannel(cdev->dev.parent);
338 css_sch_device_unregister(sch);
339 /* Reset intparm to zeroes. */
340 sch->schib.pmcw.intparm = 0;
341 cio_modify(sch);
342 put_device(&sch->dev);
346 ccw_device_set_offline(struct ccw_device *cdev)
348 int ret;
350 if (!cdev)
351 return -ENODEV;
352 if (!cdev->online || !cdev->drv)
353 return -EINVAL;
355 if (cdev->drv->set_offline) {
356 ret = cdev->drv->set_offline(cdev);
357 if (ret != 0)
358 return ret;
360 cdev->online = 0;
361 spin_lock_irq(cdev->ccwlock);
362 ret = ccw_device_offline(cdev);
363 if (ret == -ENODEV) {
364 if (cdev->private->state != DEV_STATE_NOT_OPER) {
365 cdev->private->state = DEV_STATE_OFFLINE;
366 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
368 spin_unlock_irq(cdev->ccwlock);
369 return ret;
371 spin_unlock_irq(cdev->ccwlock);
372 if (ret == 0)
373 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
374 else {
375 pr_debug("ccw_device_offline returned %d, device %s\n",
376 ret, cdev->dev.bus_id);
377 cdev->online = 1;
379 return ret;
383 ccw_device_set_online(struct ccw_device *cdev)
385 int ret;
387 if (!cdev)
388 return -ENODEV;
389 if (cdev->online || !cdev->drv)
390 return -EINVAL;
392 spin_lock_irq(cdev->ccwlock);
393 ret = ccw_device_online(cdev);
394 spin_unlock_irq(cdev->ccwlock);
395 if (ret == 0)
396 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
397 else {
398 pr_debug("ccw_device_online returned %d, device %s\n",
399 ret, cdev->dev.bus_id);
400 return ret;
402 if (cdev->private->state != DEV_STATE_ONLINE)
403 return -ENODEV;
404 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
405 cdev->online = 1;
406 return 0;
408 spin_lock_irq(cdev->ccwlock);
409 ret = ccw_device_offline(cdev);
410 spin_unlock_irq(cdev->ccwlock);
411 if (ret == 0)
412 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
413 else
414 pr_debug("ccw_device_offline returned %d, device %s\n",
415 ret, cdev->dev.bus_id);
416 return (ret == 0) ? -ENODEV : ret;
419 static ssize_t
420 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
422 struct ccw_device *cdev = to_ccwdev(dev);
423 int i, force, ret;
424 char *tmp;
426 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
427 return -EAGAIN;
429 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
430 atomic_set(&cdev->private->onoff, 0);
431 return -EINVAL;
433 if (!strncmp(buf, "force\n", count)) {
434 force = 1;
435 i = 1;
436 } else {
437 force = 0;
438 i = simple_strtoul(buf, &tmp, 16);
440 if (i == 1) {
441 /* Do device recognition, if needed. */
442 if (cdev->id.cu_type == 0) {
443 ret = ccw_device_recognition(cdev);
444 if (ret) {
445 printk(KERN_WARNING"Couldn't start recognition "
446 "for device %s (ret=%d)\n",
447 cdev->dev.bus_id, ret);
448 goto out;
450 wait_event(cdev->private->wait_q,
451 cdev->private->flags.recog_done);
453 if (cdev->drv && cdev->drv->set_online)
454 ccw_device_set_online(cdev);
455 } else if (i == 0) {
456 if (cdev->private->state == DEV_STATE_DISCONNECTED)
457 ccw_device_remove_disconnected(cdev);
458 else if (cdev->drv && cdev->drv->set_offline)
459 ccw_device_set_offline(cdev);
461 if (force && cdev->private->state == DEV_STATE_BOXED) {
462 ret = ccw_device_stlck(cdev);
463 if (ret) {
464 printk(KERN_WARNING"ccw_device_stlck for device %s "
465 "returned %d!\n", cdev->dev.bus_id, ret);
466 goto out;
468 /* Do device recognition, if needed. */
469 if (cdev->id.cu_type == 0) {
470 cdev->private->state = DEV_STATE_NOT_OPER;
471 ret = ccw_device_recognition(cdev);
472 if (ret) {
473 printk(KERN_WARNING"Couldn't start recognition "
474 "for device %s (ret=%d)\n",
475 cdev->dev.bus_id, ret);
476 goto out;
478 wait_event(cdev->private->wait_q,
479 cdev->private->flags.recog_done);
481 if (cdev->drv && cdev->drv->set_online)
482 ccw_device_set_online(cdev);
484 out:
485 if (cdev->drv)
486 module_put(cdev->drv->owner);
487 atomic_set(&cdev->private->onoff, 0);
488 return count;
491 static ssize_t
492 available_show (struct device *dev, struct device_attribute *attr, char *buf)
494 struct ccw_device *cdev = to_ccwdev(dev);
495 struct subchannel *sch;
497 if (ccw_device_is_orphan(cdev))
498 return sprintf(buf, "no device\n");
499 switch (cdev->private->state) {
500 case DEV_STATE_BOXED:
501 return sprintf(buf, "boxed\n");
502 case DEV_STATE_DISCONNECTED:
503 case DEV_STATE_DISCONNECTED_SENSE_ID:
504 case DEV_STATE_NOT_OPER:
505 sch = to_subchannel(dev->parent);
506 if (!sch->lpm)
507 return sprintf(buf, "no path\n");
508 else
509 return sprintf(buf, "no device\n");
510 default:
511 /* All other states considered fine. */
512 return sprintf(buf, "good\n");
516 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
517 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
518 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
519 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
520 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
521 static DEVICE_ATTR(online, 0644, online_show, online_store);
522 extern struct device_attribute dev_attr_cmb_enable;
523 static DEVICE_ATTR(availability, 0444, available_show, NULL);
525 static struct attribute * subch_attrs[] = {
526 &dev_attr_chpids.attr,
527 &dev_attr_pimpampom.attr,
528 NULL,
531 static struct attribute_group subch_attr_group = {
532 .attrs = subch_attrs,
535 struct attribute_group *subch_attr_groups[] = {
536 &subch_attr_group,
537 NULL,
540 static struct attribute * ccwdev_attrs[] = {
541 &dev_attr_devtype.attr,
542 &dev_attr_cutype.attr,
543 &dev_attr_modalias.attr,
544 &dev_attr_online.attr,
545 &dev_attr_cmb_enable.attr,
546 &dev_attr_availability.attr,
547 NULL,
550 static struct attribute_group ccwdev_attr_group = {
551 .attrs = ccwdev_attrs,
554 static inline int
555 device_add_files (struct device *dev)
557 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
560 static inline void
561 device_remove_files(struct device *dev)
563 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
566 /* this is a simple abstraction for device_register that sets the
567 * correct bus type and adds the bus specific files */
568 static int ccw_device_register(struct ccw_device *cdev)
570 struct device *dev = &cdev->dev;
571 int ret;
573 dev->bus = &ccw_bus_type;
575 if ((ret = device_add(dev)))
576 return ret;
578 set_bit(1, &cdev->private->registered);
579 if ((ret = device_add_files(dev))) {
580 if (test_and_clear_bit(1, &cdev->private->registered))
581 device_del(dev);
583 return ret;
586 struct match_data {
587 struct ccw_dev_id dev_id;
588 struct ccw_device * sibling;
591 static int
592 match_devno(struct device * dev, void * data)
594 struct match_data * d = data;
595 struct ccw_device * cdev;
597 cdev = to_ccwdev(dev);
598 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
599 !ccw_device_is_orphan(cdev) &&
600 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
601 (cdev != d->sibling))
602 return 1;
603 return 0;
606 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
607 struct ccw_device *sibling)
609 struct device *dev;
610 struct match_data data;
612 data.dev_id = *dev_id;
613 data.sibling = sibling;
614 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
616 return dev ? to_ccwdev(dev) : NULL;
619 static int match_orphan(struct device *dev, void *data)
621 struct ccw_dev_id *dev_id;
622 struct ccw_device *cdev;
624 dev_id = data;
625 cdev = to_ccwdev(dev);
626 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
629 static struct ccw_device *
630 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
631 struct ccw_dev_id *dev_id)
633 struct device *dev;
635 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
636 match_orphan);
638 return dev ? to_ccwdev(dev) : NULL;
641 static void
642 ccw_device_add_changed(struct work_struct *work)
644 struct ccw_device_private *priv;
645 struct ccw_device *cdev;
647 priv = container_of(work, struct ccw_device_private, kick_work);
648 cdev = priv->cdev;
649 if (device_add(&cdev->dev)) {
650 put_device(&cdev->dev);
651 return;
653 set_bit(1, &cdev->private->registered);
654 if (device_add_files(&cdev->dev)) {
655 if (test_and_clear_bit(1, &cdev->private->registered))
656 device_unregister(&cdev->dev);
660 void ccw_device_do_unreg_rereg(struct work_struct *work)
662 struct ccw_device_private *priv;
663 struct ccw_device *cdev;
664 struct subchannel *sch;
666 priv = container_of(work, struct ccw_device_private, kick_work);
667 cdev = priv->cdev;
668 sch = to_subchannel(cdev->dev.parent);
670 device_remove_files(&cdev->dev);
671 if (test_and_clear_bit(1, &cdev->private->registered))
672 device_del(&cdev->dev);
673 PREPARE_WORK(&cdev->private->kick_work,
674 ccw_device_add_changed);
675 queue_work(ccw_device_work, &cdev->private->kick_work);
678 static void
679 ccw_device_release(struct device *dev)
681 struct ccw_device *cdev;
683 cdev = to_ccwdev(dev);
684 kfree(cdev->private);
685 kfree(cdev);
688 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
690 struct ccw_device *cdev;
692 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
693 if (cdev) {
694 cdev->private = kzalloc(sizeof(struct ccw_device_private),
695 GFP_KERNEL | GFP_DMA);
696 if (cdev->private)
697 return cdev;
699 kfree(cdev);
700 return ERR_PTR(-ENOMEM);
703 static int io_subchannel_initialize_dev(struct subchannel *sch,
704 struct ccw_device *cdev)
706 cdev->private->cdev = cdev;
707 atomic_set(&cdev->private->onoff, 0);
708 cdev->dev.parent = &sch->dev;
709 cdev->dev.release = ccw_device_release;
710 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
711 /* Do first half of device_register. */
712 device_initialize(&cdev->dev);
713 if (!get_device(&sch->dev)) {
714 if (cdev->dev.release)
715 cdev->dev.release(&cdev->dev);
716 return -ENODEV;
718 return 0;
721 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
723 struct ccw_device *cdev;
724 int ret;
726 cdev = io_subchannel_allocate_dev(sch);
727 if (!IS_ERR(cdev)) {
728 ret = io_subchannel_initialize_dev(sch, cdev);
729 if (ret) {
730 kfree(cdev);
731 cdev = ERR_PTR(ret);
734 return cdev;
737 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
739 static void sch_attach_device(struct subchannel *sch,
740 struct ccw_device *cdev)
742 spin_lock_irq(sch->lock);
743 sch->dev.driver_data = cdev;
744 cdev->private->schid = sch->schid;
745 cdev->ccwlock = sch->lock;
746 device_trigger_reprobe(sch);
747 spin_unlock_irq(sch->lock);
750 static void sch_attach_disconnected_device(struct subchannel *sch,
751 struct ccw_device *cdev)
753 struct subchannel *other_sch;
754 int ret;
756 other_sch = to_subchannel(get_device(cdev->dev.parent));
757 ret = device_move(&cdev->dev, &sch->dev);
758 if (ret) {
759 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
760 "(ret=%d)!\n", cdev->private->dev_id.ssid,
761 cdev->private->dev_id.devno, ret);
762 put_device(&other_sch->dev);
763 return;
765 other_sch->dev.driver_data = NULL;
766 /* No need to keep a subchannel without ccw device around. */
767 css_sch_device_unregister(other_sch);
768 put_device(&other_sch->dev);
769 sch_attach_device(sch, cdev);
772 static void sch_attach_orphaned_device(struct subchannel *sch,
773 struct ccw_device *cdev)
775 int ret;
777 /* Try to move the ccw device to its new subchannel. */
778 ret = device_move(&cdev->dev, &sch->dev);
779 if (ret) {
780 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
781 "failed (ret=%d)!\n",
782 cdev->private->dev_id.ssid,
783 cdev->private->dev_id.devno, ret);
784 return;
786 sch_attach_device(sch, cdev);
789 static void sch_create_and_recog_new_device(struct subchannel *sch)
791 struct ccw_device *cdev;
793 /* Need to allocate a new ccw device. */
794 cdev = io_subchannel_create_ccwdev(sch);
795 if (IS_ERR(cdev)) {
796 /* OK, we did everything we could... */
797 css_sch_device_unregister(sch);
798 return;
800 spin_lock_irq(sch->lock);
801 sch->dev.driver_data = cdev;
802 spin_unlock_irq(sch->lock);
803 /* Start recognition for the new ccw device. */
804 if (io_subchannel_recog(cdev, sch)) {
805 spin_lock_irq(sch->lock);
806 sch->dev.driver_data = NULL;
807 spin_unlock_irq(sch->lock);
808 if (cdev->dev.release)
809 cdev->dev.release(&cdev->dev);
810 css_sch_device_unregister(sch);
815 void ccw_device_move_to_orphanage(struct work_struct *work)
817 struct ccw_device_private *priv;
818 struct ccw_device *cdev;
819 struct ccw_device *replacing_cdev;
820 struct subchannel *sch;
821 int ret;
822 struct channel_subsystem *css;
823 struct ccw_dev_id dev_id;
825 priv = container_of(work, struct ccw_device_private, kick_work);
826 cdev = priv->cdev;
827 sch = to_subchannel(cdev->dev.parent);
828 css = to_css(sch->dev.parent);
829 dev_id.devno = sch->schib.pmcw.dev;
830 dev_id.ssid = sch->schid.ssid;
833 * Move the orphaned ccw device to the orphanage so the replacing
834 * ccw device can take its place on the subchannel.
836 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
837 if (ret) {
838 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
839 "(ret=%d)!\n", cdev->private->dev_id.ssid,
840 cdev->private->dev_id.devno, ret);
841 return;
843 cdev->ccwlock = css->pseudo_subchannel->lock;
845 * Search for the replacing ccw device
846 * - among the disconnected devices
847 * - in the orphanage
849 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
850 if (replacing_cdev) {
851 sch_attach_disconnected_device(sch, replacing_cdev);
852 return;
854 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
855 if (replacing_cdev) {
856 sch_attach_orphaned_device(sch, replacing_cdev);
857 return;
859 sch_create_and_recog_new_device(sch);
863 * Register recognized device.
865 static void
866 io_subchannel_register(struct work_struct *work)
868 struct ccw_device_private *priv;
869 struct ccw_device *cdev;
870 struct subchannel *sch;
871 int ret;
872 unsigned long flags;
874 priv = container_of(work, struct ccw_device_private, kick_work);
875 cdev = priv->cdev;
876 sch = to_subchannel(cdev->dev.parent);
879 * io_subchannel_register() will also be called after device
880 * recognition has been done for a boxed device (which will already
881 * be registered). We need to reprobe since we may now have sense id
882 * information.
884 if (klist_node_attached(&cdev->dev.knode_parent)) {
885 if (!cdev->drv) {
886 ret = device_reprobe(&cdev->dev);
887 if (ret)
888 /* We can't do much here. */
889 dev_info(&cdev->dev, "device_reprobe() returned"
890 " %d\n", ret);
892 goto out;
894 /* make it known to the system */
895 ret = ccw_device_register(cdev);
896 if (ret) {
897 printk (KERN_WARNING "%s: could not register %s\n",
898 __func__, cdev->dev.bus_id);
899 put_device(&cdev->dev);
900 spin_lock_irqsave(sch->lock, flags);
901 sch->dev.driver_data = NULL;
902 spin_unlock_irqrestore(sch->lock, flags);
903 kfree (cdev->private);
904 kfree (cdev);
905 put_device(&sch->dev);
906 if (atomic_dec_and_test(&ccw_device_init_count))
907 wake_up(&ccw_device_init_wq);
908 return;
910 put_device(&cdev->dev);
911 out:
912 cdev->private->flags.recog_done = 1;
913 put_device(&sch->dev);
914 wake_up(&cdev->private->wait_q);
915 if (atomic_dec_and_test(&ccw_device_init_count))
916 wake_up(&ccw_device_init_wq);
919 void
920 ccw_device_call_sch_unregister(struct work_struct *work)
922 struct ccw_device_private *priv;
923 struct ccw_device *cdev;
924 struct subchannel *sch;
926 priv = container_of(work, struct ccw_device_private, kick_work);
927 cdev = priv->cdev;
928 sch = to_subchannel(cdev->dev.parent);
929 css_sch_device_unregister(sch);
930 /* Reset intparm to zeroes. */
931 sch->schib.pmcw.intparm = 0;
932 cio_modify(sch);
933 put_device(&cdev->dev);
934 put_device(&sch->dev);
938 * subchannel recognition done. Called from the state machine.
940 void
941 io_subchannel_recog_done(struct ccw_device *cdev)
943 struct subchannel *sch;
945 if (css_init_done == 0) {
946 cdev->private->flags.recog_done = 1;
947 return;
949 switch (cdev->private->state) {
950 case DEV_STATE_NOT_OPER:
951 cdev->private->flags.recog_done = 1;
952 /* Remove device found not operational. */
953 if (!get_device(&cdev->dev))
954 break;
955 sch = to_subchannel(cdev->dev.parent);
956 PREPARE_WORK(&cdev->private->kick_work,
957 ccw_device_call_sch_unregister);
958 queue_work(slow_path_wq, &cdev->private->kick_work);
959 if (atomic_dec_and_test(&ccw_device_init_count))
960 wake_up(&ccw_device_init_wq);
961 break;
962 case DEV_STATE_BOXED:
963 /* Device did not respond in time. */
964 case DEV_STATE_OFFLINE:
966 * We can't register the device in interrupt context so
967 * we schedule a work item.
969 if (!get_device(&cdev->dev))
970 break;
971 PREPARE_WORK(&cdev->private->kick_work,
972 io_subchannel_register);
973 queue_work(slow_path_wq, &cdev->private->kick_work);
974 break;
978 static int
979 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
981 int rc;
982 struct ccw_device_private *priv;
984 sch->dev.driver_data = cdev;
985 sch->driver = &io_subchannel_driver;
986 cdev->ccwlock = sch->lock;
988 /* Init private data. */
989 priv = cdev->private;
990 priv->dev_id.devno = sch->schib.pmcw.dev;
991 priv->dev_id.ssid = sch->schid.ssid;
992 priv->schid = sch->schid;
993 priv->state = DEV_STATE_NOT_OPER;
994 INIT_LIST_HEAD(&priv->cmb_list);
995 init_waitqueue_head(&priv->wait_q);
996 init_timer(&priv->timer);
998 /* Set an initial name for the device. */
999 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1000 sch->schid.ssid, sch->schib.pmcw.dev);
1002 /* Increase counter of devices currently in recognition. */
1003 atomic_inc(&ccw_device_init_count);
1005 /* Start async. device sensing. */
1006 spin_lock_irq(sch->lock);
1007 rc = ccw_device_recognition(cdev);
1008 spin_unlock_irq(sch->lock);
1009 if (rc) {
1010 if (atomic_dec_and_test(&ccw_device_init_count))
1011 wake_up(&ccw_device_init_wq);
1013 return rc;
1016 static void ccw_device_move_to_sch(struct work_struct *work)
1018 struct ccw_device_private *priv;
1019 int rc;
1020 struct subchannel *sch;
1021 struct ccw_device *cdev;
1022 struct subchannel *former_parent;
1024 priv = container_of(work, struct ccw_device_private, kick_work);
1025 sch = priv->sch;
1026 cdev = priv->cdev;
1027 former_parent = ccw_device_is_orphan(cdev) ?
1028 NULL : to_subchannel(get_device(cdev->dev.parent));
1029 mutex_lock(&sch->reg_mutex);
1030 /* Try to move the ccw device to its new subchannel. */
1031 rc = device_move(&cdev->dev, &sch->dev);
1032 mutex_unlock(&sch->reg_mutex);
1033 if (rc) {
1034 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1035 "0.%x.%04x failed (ret=%d)!\n",
1036 cdev->private->dev_id.ssid,
1037 cdev->private->dev_id.devno, sch->schid.ssid,
1038 sch->schid.sch_no, rc);
1039 css_sch_device_unregister(sch);
1040 goto out;
1042 if (former_parent) {
1043 spin_lock_irq(former_parent->lock);
1044 former_parent->dev.driver_data = NULL;
1045 spin_unlock_irq(former_parent->lock);
1046 css_sch_device_unregister(former_parent);
1047 /* Reset intparm to zeroes. */
1048 former_parent->schib.pmcw.intparm = 0;
1049 cio_modify(former_parent);
1051 sch_attach_device(sch, cdev);
1052 out:
1053 if (former_parent)
1054 put_device(&former_parent->dev);
1055 put_device(&cdev->dev);
1058 static int
1059 io_subchannel_probe (struct subchannel *sch)
1061 struct ccw_device *cdev;
1062 int rc;
1063 unsigned long flags;
1064 struct ccw_dev_id dev_id;
1066 if (sch->dev.driver_data) {
1068 * This subchannel already has an associated ccw_device.
1069 * Register it and exit. This happens for all early
1070 * device, e.g. the console.
1072 cdev = sch->dev.driver_data;
1073 device_initialize(&cdev->dev);
1074 ccw_device_register(cdev);
1076 * Check if the device is already online. If it is
1077 * the reference count needs to be corrected
1078 * (see ccw_device_online and css_init_done for the
1079 * ugly details).
1081 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1082 cdev->private->state != DEV_STATE_OFFLINE &&
1083 cdev->private->state != DEV_STATE_BOXED)
1084 get_device(&cdev->dev);
1085 return 0;
1088 * First check if a fitting device may be found amongst the
1089 * disconnected devices or in the orphanage.
1091 dev_id.devno = sch->schib.pmcw.dev;
1092 dev_id.ssid = sch->schid.ssid;
1093 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1094 if (!cdev)
1095 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1096 &dev_id);
1097 if (cdev) {
1099 * Schedule moving the device until when we have a registered
1100 * subchannel to move to and succeed the probe. We can
1101 * unregister later again, when the probe is through.
1103 cdev->private->sch = sch;
1104 PREPARE_WORK(&cdev->private->kick_work,
1105 ccw_device_move_to_sch);
1106 queue_work(slow_path_wq, &cdev->private->kick_work);
1107 return 0;
1109 cdev = io_subchannel_create_ccwdev(sch);
1110 if (IS_ERR(cdev))
1111 return PTR_ERR(cdev);
1113 rc = io_subchannel_recog(cdev, sch);
1114 if (rc) {
1115 spin_lock_irqsave(sch->lock, flags);
1116 sch->dev.driver_data = NULL;
1117 spin_unlock_irqrestore(sch->lock, flags);
1118 if (cdev->dev.release)
1119 cdev->dev.release(&cdev->dev);
1122 return rc;
1125 static int
1126 io_subchannel_remove (struct subchannel *sch)
1128 struct ccw_device *cdev;
1129 unsigned long flags;
1131 if (!sch->dev.driver_data)
1132 return 0;
1133 cdev = sch->dev.driver_data;
1134 /* Set ccw device to not operational and drop reference. */
1135 spin_lock_irqsave(cdev->ccwlock, flags);
1136 sch->dev.driver_data = NULL;
1137 cdev->private->state = DEV_STATE_NOT_OPER;
1138 spin_unlock_irqrestore(cdev->ccwlock, flags);
1140 * Put unregistration on workqueue to avoid livelocks on the css bus
1141 * semaphore.
1143 if (get_device(&cdev->dev)) {
1144 PREPARE_WORK(&cdev->private->kick_work,
1145 ccw_device_unregister);
1146 queue_work(ccw_device_work, &cdev->private->kick_work);
1148 return 0;
1151 static int
1152 io_subchannel_notify(struct device *dev, int event)
1154 struct ccw_device *cdev;
1156 cdev = dev->driver_data;
1157 if (!cdev)
1158 return 0;
1159 if (!cdev->drv)
1160 return 0;
1161 if (!cdev->online)
1162 return 0;
1163 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1166 static void
1167 io_subchannel_verify(struct device *dev)
1169 struct ccw_device *cdev;
1171 cdev = dev->driver_data;
1172 if (cdev)
1173 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1176 static void
1177 io_subchannel_ioterm(struct device *dev)
1179 struct ccw_device *cdev;
1181 cdev = dev->driver_data;
1182 if (!cdev)
1183 return;
1184 /* Internal I/O will be retried by the interrupt handler. */
1185 if (cdev->private->flags.intretry)
1186 return;
1187 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1188 if (cdev->handler)
1189 cdev->handler(cdev, cdev->private->intparm,
1190 ERR_PTR(-EIO));
1193 static void
1194 io_subchannel_shutdown(struct subchannel *sch)
1196 struct ccw_device *cdev;
1197 int ret;
1199 cdev = sch->dev.driver_data;
1201 if (cio_is_console(sch->schid))
1202 return;
1203 if (!sch->schib.pmcw.ena)
1204 /* Nothing to do. */
1205 return;
1206 ret = cio_disable_subchannel(sch);
1207 if (ret != -EBUSY)
1208 /* Subchannel is disabled, we're done. */
1209 return;
1210 cdev->private->state = DEV_STATE_QUIESCE;
1211 if (cdev->handler)
1212 cdev->handler(cdev, cdev->private->intparm,
1213 ERR_PTR(-EIO));
1214 ret = ccw_device_cancel_halt_clear(cdev);
1215 if (ret == -EBUSY) {
1216 ccw_device_set_timeout(cdev, HZ/10);
1217 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1219 cio_disable_subchannel(sch);
1222 #ifdef CONFIG_CCW_CONSOLE
1223 static struct ccw_device console_cdev;
1224 static struct ccw_device_private console_private;
1225 static int console_cdev_in_use;
1227 static DEFINE_SPINLOCK(ccw_console_lock);
1229 spinlock_t * cio_get_console_lock(void)
1231 return &ccw_console_lock;
1234 static int
1235 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1237 int rc;
1239 /* Initialize the ccw_device structure. */
1240 cdev->dev.parent= &sch->dev;
1241 rc = io_subchannel_recog(cdev, sch);
1242 if (rc)
1243 return rc;
1245 /* Now wait for the async. recognition to come to an end. */
1246 spin_lock_irq(cdev->ccwlock);
1247 while (!dev_fsm_final_state(cdev))
1248 wait_cons_dev();
1249 rc = -EIO;
1250 if (cdev->private->state != DEV_STATE_OFFLINE)
1251 goto out_unlock;
1252 ccw_device_online(cdev);
1253 while (!dev_fsm_final_state(cdev))
1254 wait_cons_dev();
1255 if (cdev->private->state != DEV_STATE_ONLINE)
1256 goto out_unlock;
1257 rc = 0;
1258 out_unlock:
1259 spin_unlock_irq(cdev->ccwlock);
1260 return 0;
1263 struct ccw_device *
1264 ccw_device_probe_console(void)
1266 struct subchannel *sch;
1267 int ret;
1269 if (xchg(&console_cdev_in_use, 1) != 0)
1270 return ERR_PTR(-EBUSY);
1271 sch = cio_probe_console();
1272 if (IS_ERR(sch)) {
1273 console_cdev_in_use = 0;
1274 return (void *) sch;
1276 memset(&console_cdev, 0, sizeof(struct ccw_device));
1277 memset(&console_private, 0, sizeof(struct ccw_device_private));
1278 console_cdev.private = &console_private;
1279 console_private.cdev = &console_cdev;
1280 ret = ccw_device_console_enable(&console_cdev, sch);
1281 if (ret) {
1282 cio_release_console();
1283 console_cdev_in_use = 0;
1284 return ERR_PTR(ret);
1286 console_cdev.online = 1;
1287 return &console_cdev;
1289 #endif
1292 * get ccw_device matching the busid, but only if owned by cdrv
1294 static int
1295 __ccwdev_check_busid(struct device *dev, void *id)
1297 char *bus_id;
1299 bus_id = id;
1301 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1305 struct ccw_device *
1306 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1308 struct device *dev;
1309 struct device_driver *drv;
1311 drv = get_driver(&cdrv->driver);
1312 if (!drv)
1313 return NULL;
1315 dev = driver_find_device(drv, NULL, (void *)bus_id,
1316 __ccwdev_check_busid);
1317 put_driver(drv);
1319 return dev ? to_ccwdev(dev) : NULL;
1322 /************************** device driver handling ************************/
1324 /* This is the implementation of the ccw_driver class. The probe, remove
1325 * and release methods are initially very similar to the device_driver
1326 * implementations, with the difference that they have ccw_device
1327 * arguments.
1329 * A ccw driver also contains the information that is needed for
1330 * device matching.
1332 static int
1333 ccw_device_probe (struct device *dev)
1335 struct ccw_device *cdev = to_ccwdev(dev);
1336 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1337 int ret;
1339 cdev->drv = cdrv; /* to let the driver call _set_online */
1341 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1343 if (ret) {
1344 cdev->drv = NULL;
1345 return ret;
1348 return 0;
1351 static int
1352 ccw_device_remove (struct device *dev)
1354 struct ccw_device *cdev = to_ccwdev(dev);
1355 struct ccw_driver *cdrv = cdev->drv;
1356 int ret;
1358 pr_debug("removing device %s\n", cdev->dev.bus_id);
1359 if (cdrv->remove)
1360 cdrv->remove(cdev);
1361 if (cdev->online) {
1362 cdev->online = 0;
1363 spin_lock_irq(cdev->ccwlock);
1364 ret = ccw_device_offline(cdev);
1365 spin_unlock_irq(cdev->ccwlock);
1366 if (ret == 0)
1367 wait_event(cdev->private->wait_q,
1368 dev_fsm_final_state(cdev));
1369 else
1370 //FIXME: we can't fail!
1371 pr_debug("ccw_device_offline returned %d, device %s\n",
1372 ret, cdev->dev.bus_id);
1374 ccw_device_set_timeout(cdev, 0);
1375 cdev->drv = NULL;
1376 return 0;
1379 struct bus_type ccw_bus_type = {
1380 .name = "ccw",
1381 .match = ccw_bus_match,
1382 .uevent = ccw_uevent,
1383 .probe = ccw_device_probe,
1384 .remove = ccw_device_remove,
1388 ccw_driver_register (struct ccw_driver *cdriver)
1390 struct device_driver *drv = &cdriver->driver;
1392 drv->bus = &ccw_bus_type;
1393 drv->name = cdriver->name;
1395 return driver_register(drv);
1398 void
1399 ccw_driver_unregister (struct ccw_driver *cdriver)
1401 driver_unregister(&cdriver->driver);
1404 /* Helper func for qdio. */
1405 struct subchannel_id
1406 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1408 struct subchannel *sch;
1410 sch = to_subchannel(cdev->dev.parent);
1411 return sch->schid;
1414 MODULE_LICENSE("GPL");
1415 EXPORT_SYMBOL(ccw_device_set_online);
1416 EXPORT_SYMBOL(ccw_device_set_offline);
1417 EXPORT_SYMBOL(ccw_driver_register);
1418 EXPORT_SYMBOL(ccw_driver_unregister);
1419 EXPORT_SYMBOL(get_ccwdev_by_busid);
1420 EXPORT_SYMBOL(ccw_bus_type);
1421 EXPORT_SYMBOL(ccw_device_work);
1422 EXPORT_SYMBOL(ccw_device_notify_work);
1423 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);