[S390] cio: Fix I/O subchannel refcounting.
[safe/jmp/linux-2.6] / drivers / s390 / cio / device.c
1 /*
2  *  drivers/s390/cio/device.c
3  *  bus driver for ccw devices
4  *
5  *    Copyright IBM Corp. 2002,2008
6  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
7  *               Cornelia Huck (cornelia.huck@de.ibm.com)
8  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/workqueue.h>
19 #include <linux/timer.h>
20
21 #include <asm/ccwdev.h>
22 #include <asm/cio.h>
23 #include <asm/param.h>          /* HZ */
24 #include <asm/cmb.h>
25 #include <asm/isc.h>
26
27 #include "chp.h"
28 #include "cio.h"
29 #include "cio_debug.h"
30 #include "css.h"
31 #include "device.h"
32 #include "ioasm.h"
33 #include "io_sch.h"
34 #include "blacklist.h"
35
36 static struct timer_list recovery_timer;
37 static DEFINE_SPINLOCK(recovery_lock);
38 static int recovery_phase;
39 static const unsigned long recovery_delay[] = { 3, 30, 300 };
40
41 /******************* bus type handling ***********************/
42
43 /* The Linux driver model distinguishes between a bus type and
44  * the bus itself. Of course we only have one channel
45  * subsystem driver and one channel system per machine, but
46  * we still use the abstraction. T.R. says it's a good idea. */
47 static int
48 ccw_bus_match (struct device * dev, struct device_driver * drv)
49 {
50         struct ccw_device *cdev = to_ccwdev(dev);
51         struct ccw_driver *cdrv = to_ccwdrv(drv);
52         const struct ccw_device_id *ids = cdrv->ids, *found;
53
54         if (!ids)
55                 return 0;
56
57         found = ccw_device_id_match(ids, &cdev->id);
58         if (!found)
59                 return 0;
60
61         cdev->id.driver_info = found->driver_info;
62
63         return 1;
64 }
65
66 /* Store modalias string delimited by prefix/suffix string into buffer with
67  * specified size. Return length of resulting string (excluding trailing '\0')
68  * even if string doesn't fit buffer (snprintf semantics). */
69 static int snprint_alias(char *buf, size_t size,
70                          struct ccw_device_id *id, const char *suffix)
71 {
72         int len;
73
74         len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
75         if (len > size)
76                 return len;
77         buf += len;
78         size -= len;
79
80         if (id->dev_type != 0)
81                 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82                                 id->dev_model, suffix);
83         else
84                 len += snprintf(buf, size, "dtdm%s", suffix);
85
86         return len;
87 }
88
89 /* Set up environment variables for ccw device uevent. Return 0 on success,
90  * non-zero otherwise. */
91 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
92 {
93         struct ccw_device *cdev = to_ccwdev(dev);
94         struct ccw_device_id *id = &(cdev->id);
95         int ret;
96         char modalias_buf[30];
97
98         /* CU_TYPE= */
99         ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
100         if (ret)
101                 return ret;
102
103         /* CU_MODEL= */
104         ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
105         if (ret)
106                 return ret;
107
108         /* The next two can be zero, that's ok for us */
109         /* DEV_TYPE= */
110         ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
111         if (ret)
112                 return ret;
113
114         /* DEV_MODEL= */
115         ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
116         if (ret)
117                 return ret;
118
119         /* MODALIAS=  */
120         snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
121         ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
122         return ret;
123 }
124
125 struct bus_type ccw_bus_type;
126
127 static void io_subchannel_irq(struct subchannel *);
128 static int io_subchannel_probe(struct subchannel *);
129 static int io_subchannel_remove(struct subchannel *);
130 static void io_subchannel_shutdown(struct subchannel *);
131 static int io_subchannel_sch_event(struct subchannel *, int);
132 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
133                                    int);
134
135 static struct css_device_id io_subchannel_ids[] = {
136         { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
137         { /* end of list */ },
138 };
139 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
140
141 static struct css_driver io_subchannel_driver = {
142         .owner = THIS_MODULE,
143         .subchannel_type = io_subchannel_ids,
144         .name = "io_subchannel",
145         .irq = io_subchannel_irq,
146         .sch_event = io_subchannel_sch_event,
147         .chp_event = io_subchannel_chp_event,
148         .probe = io_subchannel_probe,
149         .remove = io_subchannel_remove,
150         .shutdown = io_subchannel_shutdown,
151 };
152
153 struct workqueue_struct *ccw_device_work;
154 wait_queue_head_t ccw_device_init_wq;
155 atomic_t ccw_device_init_count;
156
157 static void recovery_func(unsigned long data);
158
159 static int __init
160 init_ccw_bus_type (void)
161 {
162         int ret;
163
164         init_waitqueue_head(&ccw_device_init_wq);
165         atomic_set(&ccw_device_init_count, 0);
166         setup_timer(&recovery_timer, recovery_func, 0);
167
168         ccw_device_work = create_singlethread_workqueue("cio");
169         if (!ccw_device_work)
170                 return -ENOMEM; /* FIXME: better errno ? */
171         slow_path_wq = create_singlethread_workqueue("kslowcrw");
172         if (!slow_path_wq) {
173                 ret = -ENOMEM; /* FIXME: better errno ? */
174                 goto out_err;
175         }
176         if ((ret = bus_register (&ccw_bus_type)))
177                 goto out_err;
178
179         ret = css_driver_register(&io_subchannel_driver);
180         if (ret)
181                 goto out_err;
182
183         wait_event(ccw_device_init_wq,
184                    atomic_read(&ccw_device_init_count) == 0);
185         flush_workqueue(ccw_device_work);
186         return 0;
187 out_err:
188         if (ccw_device_work)
189                 destroy_workqueue(ccw_device_work);
190         if (slow_path_wq)
191                 destroy_workqueue(slow_path_wq);
192         return ret;
193 }
194
195 static void __exit
196 cleanup_ccw_bus_type (void)
197 {
198         css_driver_unregister(&io_subchannel_driver);
199         bus_unregister(&ccw_bus_type);
200         destroy_workqueue(ccw_device_work);
201 }
202
203 subsys_initcall(init_ccw_bus_type);
204 module_exit(cleanup_ccw_bus_type);
205
206 /************************ device handling **************************/
207
208 /*
209  * A ccw_device has some interfaces in sysfs in addition to the
210  * standard ones.
211  * The following entries are designed to export the information which
212  * resided in 2.4 in /proc/subchannels. Subchannel and device number
213  * are obvious, so they don't have an entry :)
214  * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
215  */
216 static ssize_t
217 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
218 {
219         struct subchannel *sch = to_subchannel(dev);
220         struct chsc_ssd_info *ssd = &sch->ssd_info;
221         ssize_t ret = 0;
222         int chp;
223         int mask;
224
225         for (chp = 0; chp < 8; chp++) {
226                 mask = 0x80 >> chp;
227                 if (ssd->path_mask & mask)
228                         ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
229                 else
230                         ret += sprintf(buf + ret, "00 ");
231         }
232         ret += sprintf (buf+ret, "\n");
233         return min((ssize_t)PAGE_SIZE, ret);
234 }
235
236 static ssize_t
237 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
238 {
239         struct subchannel *sch = to_subchannel(dev);
240         struct pmcw *pmcw = &sch->schib.pmcw;
241
242         return sprintf (buf, "%02x %02x %02x\n",
243                         pmcw->pim, pmcw->pam, pmcw->pom);
244 }
245
246 static ssize_t
247 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
248 {
249         struct ccw_device *cdev = to_ccwdev(dev);
250         struct ccw_device_id *id = &(cdev->id);
251
252         if (id->dev_type != 0)
253                 return sprintf(buf, "%04x/%02x\n",
254                                 id->dev_type, id->dev_model);
255         else
256                 return sprintf(buf, "n/a\n");
257 }
258
259 static ssize_t
260 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
261 {
262         struct ccw_device *cdev = to_ccwdev(dev);
263         struct ccw_device_id *id = &(cdev->id);
264
265         return sprintf(buf, "%04x/%02x\n",
266                        id->cu_type, id->cu_model);
267 }
268
269 static ssize_t
270 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
271 {
272         struct ccw_device *cdev = to_ccwdev(dev);
273         struct ccw_device_id *id = &(cdev->id);
274         int len;
275
276         len = snprint_alias(buf, PAGE_SIZE, id, "\n");
277
278         return len > PAGE_SIZE ? PAGE_SIZE : len;
279 }
280
281 static ssize_t
282 online_show (struct device *dev, struct device_attribute *attr, char *buf)
283 {
284         struct ccw_device *cdev = to_ccwdev(dev);
285
286         return sprintf(buf, cdev->online ? "1\n" : "0\n");
287 }
288
289 int ccw_device_is_orphan(struct ccw_device *cdev)
290 {
291         return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
292 }
293
294 static void ccw_device_unregister(struct ccw_device *cdev)
295 {
296         if (test_and_clear_bit(1, &cdev->private->registered))
297                 device_del(&cdev->dev);
298 }
299
300 static void ccw_device_remove_orphan_cb(struct work_struct *work)
301 {
302         struct ccw_device_private *priv;
303         struct ccw_device *cdev;
304
305         priv = container_of(work, struct ccw_device_private, kick_work);
306         cdev = priv->cdev;
307         ccw_device_unregister(cdev);
308         put_device(&cdev->dev);
309         /* Release cdev reference for workqueue processing. */
310         put_device(&cdev->dev);
311 }
312
313 static void ccw_device_call_sch_unregister(struct work_struct *work);
314
315 static void
316 ccw_device_remove_disconnected(struct ccw_device *cdev)
317 {
318         unsigned long flags;
319
320         /*
321          * Forced offline in disconnected state means
322          * 'throw away device'.
323          */
324         /* Get cdev reference for workqueue processing. */
325         if (!get_device(&cdev->dev))
326                 return;
327         if (ccw_device_is_orphan(cdev)) {
328                 /*
329                  * Deregister ccw device.
330                  * Unfortunately, we cannot do this directly from the
331                  * attribute method.
332                  */
333                 spin_lock_irqsave(cdev->ccwlock, flags);
334                 cdev->private->state = DEV_STATE_NOT_OPER;
335                 spin_unlock_irqrestore(cdev->ccwlock, flags);
336                 PREPARE_WORK(&cdev->private->kick_work,
337                                 ccw_device_remove_orphan_cb);
338         } else
339                 /* Deregister subchannel, which will kill the ccw device. */
340                 PREPARE_WORK(&cdev->private->kick_work,
341                                 ccw_device_call_sch_unregister);
342         queue_work(slow_path_wq, &cdev->private->kick_work);
343 }
344
345 /**
346  * ccw_device_set_offline() - disable a ccw device for I/O
347  * @cdev: target ccw device
348  *
349  * This function calls the driver's set_offline() function for @cdev, if
350  * given, and then disables @cdev.
351  * Returns:
352  *   %0 on success and a negative error value on failure.
353  * Context:
354  *  enabled, ccw device lock not held
355  */
356 int ccw_device_set_offline(struct ccw_device *cdev)
357 {
358         int ret;
359
360         if (!cdev)
361                 return -ENODEV;
362         if (!cdev->online || !cdev->drv)
363                 return -EINVAL;
364
365         if (cdev->drv->set_offline) {
366                 ret = cdev->drv->set_offline(cdev);
367                 if (ret != 0)
368                         return ret;
369         }
370         cdev->online = 0;
371         spin_lock_irq(cdev->ccwlock);
372         ret = ccw_device_offline(cdev);
373         if (ret == -ENODEV) {
374                 if (cdev->private->state != DEV_STATE_NOT_OPER) {
375                         cdev->private->state = DEV_STATE_OFFLINE;
376                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
377                 }
378                 spin_unlock_irq(cdev->ccwlock);
379                 /* Give up reference from ccw_device_set_online(). */
380                 put_device(&cdev->dev);
381                 return ret;
382         }
383         spin_unlock_irq(cdev->ccwlock);
384         if (ret == 0) {
385                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
386                 /* Give up reference from ccw_device_set_online(). */
387                 put_device(&cdev->dev);
388         } else {
389                 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
390                               "device 0.%x.%04x\n",
391                               ret, cdev->private->dev_id.ssid,
392                               cdev->private->dev_id.devno);
393                 cdev->online = 1;
394         }
395         return ret;
396 }
397
398 /**
399  * ccw_device_set_online() - enable a ccw device for I/O
400  * @cdev: target ccw device
401  *
402  * This function first enables @cdev and then calls the driver's set_online()
403  * function for @cdev, if given. If set_online() returns an error, @cdev is
404  * disabled again.
405  * Returns:
406  *   %0 on success and a negative error value on failure.
407  * Context:
408  *  enabled, ccw device lock not held
409  */
410 int ccw_device_set_online(struct ccw_device *cdev)
411 {
412         int ret;
413
414         if (!cdev)
415                 return -ENODEV;
416         if (cdev->online || !cdev->drv)
417                 return -EINVAL;
418         /* Hold on to an extra reference while device is online. */
419         if (!get_device(&cdev->dev))
420                 return -ENODEV;
421
422         spin_lock_irq(cdev->ccwlock);
423         ret = ccw_device_online(cdev);
424         spin_unlock_irq(cdev->ccwlock);
425         if (ret == 0)
426                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
427         else {
428                 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
429                               "device 0.%x.%04x\n",
430                               ret, cdev->private->dev_id.ssid,
431                               cdev->private->dev_id.devno);
432                 /* Give up online reference since onlining failed. */
433                 put_device(&cdev->dev);
434                 return ret;
435         }
436         if (cdev->private->state != DEV_STATE_ONLINE) {
437                 /* Give up online reference since onlining failed. */
438                 put_device(&cdev->dev);
439                 return -ENODEV;
440         }
441         if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
442                 cdev->online = 1;
443                 return 0;
444         }
445         spin_lock_irq(cdev->ccwlock);
446         ret = ccw_device_offline(cdev);
447         spin_unlock_irq(cdev->ccwlock);
448         if (ret == 0)
449                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
450         else
451                 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
452                               "device 0.%x.%04x\n",
453                               ret, cdev->private->dev_id.ssid,
454                               cdev->private->dev_id.devno);
455         /* Give up online reference since onlining failed. */
456         put_device(&cdev->dev);
457         return (ret == 0) ? -ENODEV : ret;
458 }
459
460 static void online_store_handle_offline(struct ccw_device *cdev)
461 {
462         if (cdev->private->state == DEV_STATE_DISCONNECTED)
463                 ccw_device_remove_disconnected(cdev);
464         else if (cdev->drv && cdev->drv->set_offline)
465                 ccw_device_set_offline(cdev);
466 }
467
468 static int online_store_recog_and_online(struct ccw_device *cdev)
469 {
470         int ret;
471
472         /* Do device recognition, if needed. */
473         if (cdev->id.cu_type == 0) {
474                 ret = ccw_device_recognition(cdev);
475                 if (ret) {
476                         CIO_MSG_EVENT(0, "Couldn't start recognition "
477                                       "for device 0.%x.%04x (ret=%d)\n",
478                                       cdev->private->dev_id.ssid,
479                                       cdev->private->dev_id.devno, ret);
480                         return ret;
481                 }
482                 wait_event(cdev->private->wait_q,
483                            cdev->private->flags.recog_done);
484         }
485         if (cdev->drv && cdev->drv->set_online)
486                 ccw_device_set_online(cdev);
487         return 0;
488 }
489 static int online_store_handle_online(struct ccw_device *cdev, int force)
490 {
491         int ret;
492
493         ret = online_store_recog_and_online(cdev);
494         if (ret)
495                 return ret;
496         if (force && cdev->private->state == DEV_STATE_BOXED) {
497                 ret = ccw_device_stlck(cdev);
498                 if (ret)
499                         return ret;
500                 if (cdev->id.cu_type == 0)
501                         cdev->private->state = DEV_STATE_NOT_OPER;
502                 online_store_recog_and_online(cdev);
503         }
504         return 0;
505 }
506
507 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
508                              const char *buf, size_t count)
509 {
510         struct ccw_device *cdev = to_ccwdev(dev);
511         int force, ret;
512         unsigned long i;
513
514         if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
515                 return -EAGAIN;
516
517         if (cdev->drv && !try_module_get(cdev->drv->owner)) {
518                 atomic_set(&cdev->private->onoff, 0);
519                 return -EINVAL;
520         }
521         if (!strncmp(buf, "force\n", count)) {
522                 force = 1;
523                 i = 1;
524                 ret = 0;
525         } else {
526                 force = 0;
527                 ret = strict_strtoul(buf, 16, &i);
528         }
529         if (ret)
530                 goto out;
531         switch (i) {
532         case 0:
533                 online_store_handle_offline(cdev);
534                 ret = count;
535                 break;
536         case 1:
537                 ret = online_store_handle_online(cdev, force);
538                 if (!ret)
539                         ret = count;
540                 break;
541         default:
542                 ret = -EINVAL;
543         }
544 out:
545         if (cdev->drv)
546                 module_put(cdev->drv->owner);
547         atomic_set(&cdev->private->onoff, 0);
548         return ret;
549 }
550
551 static ssize_t
552 available_show (struct device *dev, struct device_attribute *attr, char *buf)
553 {
554         struct ccw_device *cdev = to_ccwdev(dev);
555         struct subchannel *sch;
556
557         if (ccw_device_is_orphan(cdev))
558                 return sprintf(buf, "no device\n");
559         switch (cdev->private->state) {
560         case DEV_STATE_BOXED:
561                 return sprintf(buf, "boxed\n");
562         case DEV_STATE_DISCONNECTED:
563         case DEV_STATE_DISCONNECTED_SENSE_ID:
564         case DEV_STATE_NOT_OPER:
565                 sch = to_subchannel(dev->parent);
566                 if (!sch->lpm)
567                         return sprintf(buf, "no path\n");
568                 else
569                         return sprintf(buf, "no device\n");
570         default:
571                 /* All other states considered fine. */
572                 return sprintf(buf, "good\n");
573         }
574 }
575
576 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
577 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
578 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
579 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
580 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
581 static DEVICE_ATTR(online, 0644, online_show, online_store);
582 static DEVICE_ATTR(availability, 0444, available_show, NULL);
583
584 static struct attribute *io_subchannel_attrs[] = {
585         &dev_attr_chpids.attr,
586         &dev_attr_pimpampom.attr,
587         NULL,
588 };
589
590 static struct attribute_group io_subchannel_attr_group = {
591         .attrs = io_subchannel_attrs,
592 };
593
594 static struct attribute * ccwdev_attrs[] = {
595         &dev_attr_devtype.attr,
596         &dev_attr_cutype.attr,
597         &dev_attr_modalias.attr,
598         &dev_attr_online.attr,
599         &dev_attr_cmb_enable.attr,
600         &dev_attr_availability.attr,
601         NULL,
602 };
603
604 static struct attribute_group ccwdev_attr_group = {
605         .attrs = ccwdev_attrs,
606 };
607
608 static struct attribute_group *ccwdev_attr_groups[] = {
609         &ccwdev_attr_group,
610         NULL,
611 };
612
613 /* this is a simple abstraction for device_register that sets the
614  * correct bus type and adds the bus specific files */
615 static int ccw_device_register(struct ccw_device *cdev)
616 {
617         struct device *dev = &cdev->dev;
618         int ret;
619
620         dev->bus = &ccw_bus_type;
621
622         if ((ret = device_add(dev)))
623                 return ret;
624
625         set_bit(1, &cdev->private->registered);
626         return ret;
627 }
628
629 struct match_data {
630         struct ccw_dev_id dev_id;
631         struct ccw_device * sibling;
632 };
633
634 static int
635 match_devno(struct device * dev, void * data)
636 {
637         struct match_data * d = data;
638         struct ccw_device * cdev;
639
640         cdev = to_ccwdev(dev);
641         if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
642             !ccw_device_is_orphan(cdev) &&
643             ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
644             (cdev != d->sibling))
645                 return 1;
646         return 0;
647 }
648
649 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
650                                                      struct ccw_device *sibling)
651 {
652         struct device *dev;
653         struct match_data data;
654
655         data.dev_id = *dev_id;
656         data.sibling = sibling;
657         dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
658
659         return dev ? to_ccwdev(dev) : NULL;
660 }
661
662 static int match_orphan(struct device *dev, void *data)
663 {
664         struct ccw_dev_id *dev_id;
665         struct ccw_device *cdev;
666
667         dev_id = data;
668         cdev = to_ccwdev(dev);
669         return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
670 }
671
672 static struct ccw_device *
673 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
674                               struct ccw_dev_id *dev_id)
675 {
676         struct device *dev;
677
678         dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
679                                 match_orphan);
680
681         return dev ? to_ccwdev(dev) : NULL;
682 }
683
684 static void
685 ccw_device_add_changed(struct work_struct *work)
686 {
687         struct ccw_device_private *priv;
688         struct ccw_device *cdev;
689
690         priv = container_of(work, struct ccw_device_private, kick_work);
691         cdev = priv->cdev;
692         if (device_add(&cdev->dev)) {
693                 put_device(&cdev->dev);
694                 return;
695         }
696         set_bit(1, &cdev->private->registered);
697 }
698
699 void ccw_device_do_unreg_rereg(struct work_struct *work)
700 {
701         struct ccw_device_private *priv;
702         struct ccw_device *cdev;
703         struct subchannel *sch;
704
705         priv = container_of(work, struct ccw_device_private, kick_work);
706         cdev = priv->cdev;
707         sch = to_subchannel(cdev->dev.parent);
708
709         ccw_device_unregister(cdev);
710         PREPARE_WORK(&cdev->private->kick_work,
711                      ccw_device_add_changed);
712         queue_work(ccw_device_work, &cdev->private->kick_work);
713 }
714
715 static void
716 ccw_device_release(struct device *dev)
717 {
718         struct ccw_device *cdev;
719
720         cdev = to_ccwdev(dev);
721         /* Release reference of parent subchannel. */
722         put_device(cdev->dev.parent);
723         kfree(cdev->private);
724         kfree(cdev);
725 }
726
727 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
728 {
729         struct ccw_device *cdev;
730
731         cdev  = kzalloc(sizeof(*cdev), GFP_KERNEL);
732         if (cdev) {
733                 cdev->private = kzalloc(sizeof(struct ccw_device_private),
734                                         GFP_KERNEL | GFP_DMA);
735                 if (cdev->private)
736                         return cdev;
737         }
738         kfree(cdev);
739         return ERR_PTR(-ENOMEM);
740 }
741
742 static int io_subchannel_initialize_dev(struct subchannel *sch,
743                                         struct ccw_device *cdev)
744 {
745         cdev->private->cdev = cdev;
746         atomic_set(&cdev->private->onoff, 0);
747         cdev->dev.parent = &sch->dev;
748         cdev->dev.release = ccw_device_release;
749         INIT_WORK(&cdev->private->kick_work, NULL);
750         cdev->dev.groups = ccwdev_attr_groups;
751         /* Do first half of device_register. */
752         device_initialize(&cdev->dev);
753         if (!get_device(&sch->dev)) {
754                 if (cdev->dev.release)
755                         cdev->dev.release(&cdev->dev);
756                 return -ENODEV;
757         }
758         return 0;
759 }
760
761 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
762 {
763         struct ccw_device *cdev;
764         int ret;
765
766         cdev = io_subchannel_allocate_dev(sch);
767         if (!IS_ERR(cdev)) {
768                 ret = io_subchannel_initialize_dev(sch, cdev);
769                 if (ret) {
770                         kfree(cdev);
771                         cdev = ERR_PTR(ret);
772                 }
773         }
774         return cdev;
775 }
776
777 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
778
779 static void sch_attach_device(struct subchannel *sch,
780                               struct ccw_device *cdev)
781 {
782         css_update_ssd_info(sch);
783         spin_lock_irq(sch->lock);
784         sch_set_cdev(sch, cdev);
785         cdev->private->schid = sch->schid;
786         cdev->ccwlock = sch->lock;
787         ccw_device_trigger_reprobe(cdev);
788         spin_unlock_irq(sch->lock);
789 }
790
791 static void sch_attach_disconnected_device(struct subchannel *sch,
792                                            struct ccw_device *cdev)
793 {
794         struct subchannel *other_sch;
795         int ret;
796
797         /* Get reference for new parent. */
798         if (!get_device(&sch->dev))
799                 return;
800         other_sch = to_subchannel(cdev->dev.parent);
801         /* Note: device_move() changes cdev->dev.parent */
802         ret = device_move(&cdev->dev, &sch->dev);
803         if (ret) {
804                 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
805                               "(ret=%d)!\n", cdev->private->dev_id.ssid,
806                               cdev->private->dev_id.devno, ret);
807                 /* Put reference for new parent. */
808                 put_device(&sch->dev);
809                 return;
810         }
811         sch_set_cdev(other_sch, NULL);
812         /* No need to keep a subchannel without ccw device around. */
813         css_sch_device_unregister(other_sch);
814         sch_attach_device(sch, cdev);
815         /* Put reference for old parent. */
816         put_device(&other_sch->dev);
817 }
818
819 static void sch_attach_orphaned_device(struct subchannel *sch,
820                                        struct ccw_device *cdev)
821 {
822         int ret;
823         struct subchannel *pseudo_sch;
824
825         /* Get reference for new parent. */
826         if (!get_device(&sch->dev))
827                 return;
828         pseudo_sch = to_subchannel(cdev->dev.parent);
829         /*
830          * Try to move the ccw device to its new subchannel.
831          * Note: device_move() changes cdev->dev.parent
832          */
833         ret = device_move(&cdev->dev, &sch->dev);
834         if (ret) {
835                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
836                               "failed (ret=%d)!\n",
837                               cdev->private->dev_id.ssid,
838                               cdev->private->dev_id.devno, ret);
839                 /* Put reference for new parent. */
840                 put_device(&sch->dev);
841                 return;
842         }
843         sch_attach_device(sch, cdev);
844         /* Put reference on pseudo subchannel. */
845         put_device(&pseudo_sch->dev);
846 }
847
848 static void sch_create_and_recog_new_device(struct subchannel *sch)
849 {
850         struct ccw_device *cdev;
851
852         /* Need to allocate a new ccw device. */
853         cdev = io_subchannel_create_ccwdev(sch);
854         if (IS_ERR(cdev)) {
855                 /* OK, we did everything we could... */
856                 css_sch_device_unregister(sch);
857                 return;
858         }
859         spin_lock_irq(sch->lock);
860         sch_set_cdev(sch, cdev);
861         spin_unlock_irq(sch->lock);
862         /* Start recognition for the new ccw device. */
863         if (io_subchannel_recog(cdev, sch)) {
864                 spin_lock_irq(sch->lock);
865                 sch_set_cdev(sch, NULL);
866                 spin_unlock_irq(sch->lock);
867                 css_sch_device_unregister(sch);
868                 /* Put reference from io_subchannel_create_ccwdev(). */
869                 put_device(&sch->dev);
870                 /* Give up initial reference. */
871                 put_device(&cdev->dev);
872         }
873 }
874
875
876 void ccw_device_move_to_orphanage(struct work_struct *work)
877 {
878         struct ccw_device_private *priv;
879         struct ccw_device *cdev;
880         struct ccw_device *replacing_cdev;
881         struct subchannel *sch;
882         int ret;
883         struct channel_subsystem *css;
884         struct ccw_dev_id dev_id;
885
886         priv = container_of(work, struct ccw_device_private, kick_work);
887         cdev = priv->cdev;
888         sch = to_subchannel(cdev->dev.parent);
889         css = to_css(sch->dev.parent);
890         dev_id.devno = sch->schib.pmcw.dev;
891         dev_id.ssid = sch->schid.ssid;
892
893         /* Increase refcount for pseudo subchannel. */
894         get_device(&css->pseudo_subchannel->dev);
895         /*
896          * Move the orphaned ccw device to the orphanage so the replacing
897          * ccw device can take its place on the subchannel.
898          * Note: device_move() changes cdev->dev.parent
899          */
900         ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
901         if (ret) {
902                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
903                               "(ret=%d)!\n", cdev->private->dev_id.ssid,
904                               cdev->private->dev_id.devno, ret);
905                 /* Decrease refcount for pseudo subchannel again. */
906                 put_device(&css->pseudo_subchannel->dev);
907                 return;
908         }
909         cdev->ccwlock = css->pseudo_subchannel->lock;
910         /*
911          * Search for the replacing ccw device
912          * - among the disconnected devices
913          * - in the orphanage
914          */
915         replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
916         if (replacing_cdev) {
917                 sch_attach_disconnected_device(sch, replacing_cdev);
918                 /* Release reference from get_disc_ccwdev_by_dev_id() */
919                 put_device(&replacing_cdev->dev);
920                 /* Release reference of subchannel from old cdev. */
921                 put_device(&sch->dev);
922                 return;
923         }
924         replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
925         if (replacing_cdev) {
926                 sch_attach_orphaned_device(sch, replacing_cdev);
927                 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
928                 put_device(&replacing_cdev->dev);
929                 /* Release reference of subchannel from old cdev. */
930                 put_device(&sch->dev);
931                 return;
932         }
933         sch_create_and_recog_new_device(sch);
934         /* Release reference of subchannel from old cdev. */
935         put_device(&sch->dev);
936 }
937
938 /*
939  * Register recognized device.
940  */
941 static void
942 io_subchannel_register(struct work_struct *work)
943 {
944         struct ccw_device_private *priv;
945         struct ccw_device *cdev;
946         struct subchannel *sch;
947         int ret;
948         unsigned long flags;
949
950         priv = container_of(work, struct ccw_device_private, kick_work);
951         cdev = priv->cdev;
952         sch = to_subchannel(cdev->dev.parent);
953         css_update_ssd_info(sch);
954         /*
955          * io_subchannel_register() will also be called after device
956          * recognition has been done for a boxed device (which will already
957          * be registered). We need to reprobe since we may now have sense id
958          * information.
959          */
960         if (klist_node_attached(&cdev->dev.knode_parent)) {
961                 if (!cdev->drv) {
962                         ret = device_reprobe(&cdev->dev);
963                         if (ret)
964                                 /* We can't do much here. */
965                                 CIO_MSG_EVENT(0, "device_reprobe() returned"
966                                               " %d for 0.%x.%04x\n", ret,
967                                               cdev->private->dev_id.ssid,
968                                               cdev->private->dev_id.devno);
969                 }
970                 goto out;
971         }
972         /*
973          * Now we know this subchannel will stay, we can throw
974          * our delayed uevent.
975          */
976         sch->dev.uevent_suppress = 0;
977         kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
978         /* make it known to the system */
979         ret = ccw_device_register(cdev);
980         if (ret) {
981                 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
982                               cdev->private->dev_id.ssid,
983                               cdev->private->dev_id.devno, ret);
984                 spin_lock_irqsave(sch->lock, flags);
985                 sch_set_cdev(sch, NULL);
986                 spin_unlock_irqrestore(sch->lock, flags);
987                 /* Release reference for workqueue processing. */
988                 put_device(&cdev->dev);
989                 /* Release initial device reference. */
990                 put_device(&cdev->dev);
991                 if (atomic_dec_and_test(&ccw_device_init_count))
992                         wake_up(&ccw_device_init_wq);
993                 return;
994         }
995         put_device(&cdev->dev);
996 out:
997         cdev->private->flags.recog_done = 1;
998         wake_up(&cdev->private->wait_q);
999         if (atomic_dec_and_test(&ccw_device_init_count))
1000                 wake_up(&ccw_device_init_wq);
1001 }
1002
1003 static void ccw_device_call_sch_unregister(struct work_struct *work)
1004 {
1005         struct ccw_device_private *priv;
1006         struct ccw_device *cdev;
1007         struct subchannel *sch;
1008
1009         priv = container_of(work, struct ccw_device_private, kick_work);
1010         cdev = priv->cdev;
1011         /* Get subchannel reference for local processing. */
1012         if (!get_device(cdev->dev.parent))
1013                 return;
1014         sch = to_subchannel(cdev->dev.parent);
1015         css_sch_device_unregister(sch);
1016         /* Reset intparm to zeroes. */
1017         sch->schib.pmcw.intparm = 0;
1018         cio_modify(sch);
1019         /* Release cdev reference for workqueue processing.*/
1020         put_device(&cdev->dev);
1021         /* Release subchannel reference for local processing. */
1022         put_device(&sch->dev);
1023 }
1024
1025 /*
1026  * subchannel recognition done. Called from the state machine.
1027  */
1028 void
1029 io_subchannel_recog_done(struct ccw_device *cdev)
1030 {
1031         struct subchannel *sch;
1032
1033         if (css_init_done == 0) {
1034                 cdev->private->flags.recog_done = 1;
1035                 return;
1036         }
1037         switch (cdev->private->state) {
1038         case DEV_STATE_NOT_OPER:
1039                 cdev->private->flags.recog_done = 1;
1040                 /* Remove device found not operational. */
1041                 if (!get_device(&cdev->dev))
1042                         break;
1043                 sch = to_subchannel(cdev->dev.parent);
1044                 PREPARE_WORK(&cdev->private->kick_work,
1045                              ccw_device_call_sch_unregister);
1046                 queue_work(slow_path_wq, &cdev->private->kick_work);
1047                 if (atomic_dec_and_test(&ccw_device_init_count))
1048                         wake_up(&ccw_device_init_wq);
1049                 break;
1050         case DEV_STATE_BOXED:
1051                 /* Device did not respond in time. */
1052         case DEV_STATE_OFFLINE:
1053                 /* 
1054                  * We can't register the device in interrupt context so
1055                  * we schedule a work item.
1056                  */
1057                 if (!get_device(&cdev->dev))
1058                         break;
1059                 PREPARE_WORK(&cdev->private->kick_work,
1060                              io_subchannel_register);
1061                 queue_work(slow_path_wq, &cdev->private->kick_work);
1062                 break;
1063         }
1064 }
1065
1066 static int
1067 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1068 {
1069         int rc;
1070         struct ccw_device_private *priv;
1071
1072         sch_set_cdev(sch, cdev);
1073         cdev->ccwlock = sch->lock;
1074
1075         /* Init private data. */
1076         priv = cdev->private;
1077         priv->dev_id.devno = sch->schib.pmcw.dev;
1078         priv->dev_id.ssid = sch->schid.ssid;
1079         priv->schid = sch->schid;
1080         priv->state = DEV_STATE_NOT_OPER;
1081         INIT_LIST_HEAD(&priv->cmb_list);
1082         init_waitqueue_head(&priv->wait_q);
1083         init_timer(&priv->timer);
1084
1085         /* Set an initial name for the device. */
1086         if (cio_is_console(sch->schid))
1087                 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1088         else
1089                 dev_set_name(&cdev->dev, "0.%x.%04x",
1090                              sch->schid.ssid, sch->schib.pmcw.dev);
1091
1092         /* Increase counter of devices currently in recognition. */
1093         atomic_inc(&ccw_device_init_count);
1094
1095         /* Start async. device sensing. */
1096         spin_lock_irq(sch->lock);
1097         rc = ccw_device_recognition(cdev);
1098         spin_unlock_irq(sch->lock);
1099         if (rc) {
1100                 if (atomic_dec_and_test(&ccw_device_init_count))
1101                         wake_up(&ccw_device_init_wq);
1102         }
1103         return rc;
1104 }
1105
1106 static void ccw_device_move_to_sch(struct work_struct *work)
1107 {
1108         struct ccw_device_private *priv;
1109         int rc;
1110         struct subchannel *sch;
1111         struct ccw_device *cdev;
1112         struct subchannel *former_parent;
1113
1114         priv = container_of(work, struct ccw_device_private, kick_work);
1115         sch = priv->sch;
1116         cdev = priv->cdev;
1117         former_parent = to_subchannel(cdev->dev.parent);
1118         /* Get reference for new parent. */
1119         if (!get_device(&sch->dev))
1120                 return;
1121         mutex_lock(&sch->reg_mutex);
1122         /*
1123          * Try to move the ccw device to its new subchannel.
1124          * Note: device_move() changes cdev->dev.parent
1125          */
1126         rc = device_move(&cdev->dev, &sch->dev);
1127         mutex_unlock(&sch->reg_mutex);
1128         if (rc) {
1129                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1130                               "0.%x.%04x failed (ret=%d)!\n",
1131                               cdev->private->dev_id.ssid,
1132                               cdev->private->dev_id.devno, sch->schid.ssid,
1133                               sch->schid.sch_no, rc);
1134                 css_sch_device_unregister(sch);
1135                 /* Put reference for new parent again. */
1136                 put_device(&sch->dev);
1137                 goto out;
1138         }
1139         if (!sch_is_pseudo_sch(former_parent)) {
1140                 spin_lock_irq(former_parent->lock);
1141                 sch_set_cdev(former_parent, NULL);
1142                 spin_unlock_irq(former_parent->lock);
1143                 css_sch_device_unregister(former_parent);
1144                 /* Reset intparm to zeroes. */
1145                 former_parent->schib.pmcw.intparm = 0;
1146                 cio_modify(former_parent);
1147         }
1148         sch_attach_device(sch, cdev);
1149 out:
1150         /* Put reference for old parent. */
1151         put_device(&former_parent->dev);
1152         put_device(&cdev->dev);
1153 }
1154
1155 static void io_subchannel_irq(struct subchannel *sch)
1156 {
1157         struct ccw_device *cdev;
1158
1159         cdev = sch_get_cdev(sch);
1160
1161         CIO_TRACE_EVENT(3, "IRQ");
1162         CIO_TRACE_EVENT(3, dev_name(&sch->dev));
1163         if (cdev)
1164                 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1165 }
1166
1167 static void io_subchannel_init_fields(struct subchannel *sch)
1168 {
1169         if (cio_is_console(sch->schid))
1170                 sch->opm = 0xff;
1171         else
1172                 sch->opm = chp_get_sch_opm(sch);
1173         sch->lpm = sch->schib.pmcw.pam & sch->opm;
1174         sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1175
1176         CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1177                       " - PIM = %02X, PAM = %02X, POM = %02X\n",
1178                       sch->schib.pmcw.dev, sch->schid.ssid,
1179                       sch->schid.sch_no, sch->schib.pmcw.pim,
1180                       sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1181         /* Initially set up some fields in the pmcw. */
1182         sch->schib.pmcw.ena = 0;
1183         sch->schib.pmcw.csense = 1;     /* concurrent sense */
1184         if ((sch->lpm & (sch->lpm - 1)) != 0)
1185                 sch->schib.pmcw.mp = 1; /* multipath mode */
1186         /* clean up possible residual cmf stuff */
1187         sch->schib.pmcw.mme = 0;
1188         sch->schib.pmcw.mbfc = 0;
1189         sch->schib.pmcw.mbi = 0;
1190         sch->schib.mba = 0;
1191 }
1192
1193 static int io_subchannel_probe(struct subchannel *sch)
1194 {
1195         struct ccw_device *cdev;
1196         int rc;
1197         unsigned long flags;
1198         struct ccw_dev_id dev_id;
1199
1200         cdev = sch_get_cdev(sch);
1201         if (cdev) {
1202                 rc = sysfs_create_group(&sch->dev.kobj,
1203                                         &io_subchannel_attr_group);
1204                 if (rc)
1205                         CIO_MSG_EVENT(0, "Failed to create io subchannel "
1206                                       "attributes for subchannel "
1207                                       "0.%x.%04x (rc=%d)\n",
1208                                       sch->schid.ssid, sch->schid.sch_no, rc);
1209                 /*
1210                  * This subchannel already has an associated ccw_device.
1211                  * Throw the delayed uevent for the subchannel, register
1212                  * the ccw_device and exit. This happens for all early
1213                  * devices, e.g. the console.
1214                  */
1215                 sch->dev.uevent_suppress = 0;
1216                 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1217                 cdev->dev.groups = ccwdev_attr_groups;
1218                 device_initialize(&cdev->dev);
1219                 ccw_device_register(cdev);
1220                 /*
1221                  * Check if the device is already online. If it is
1222                  * the reference count needs to be corrected since we
1223                  * didn't obtain a reference in ccw_device_set_online.
1224                  */
1225                 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1226                     cdev->private->state != DEV_STATE_OFFLINE &&
1227                     cdev->private->state != DEV_STATE_BOXED)
1228                         get_device(&cdev->dev);
1229                 return 0;
1230         }
1231         io_subchannel_init_fields(sch);
1232         rc = sysfs_create_group(&sch->dev.kobj,
1233                                 &io_subchannel_attr_group);
1234         if (rc)
1235                 return rc;
1236         /* Allocate I/O subchannel private data. */
1237         sch->private = kzalloc(sizeof(struct io_subchannel_private),
1238                                GFP_KERNEL | GFP_DMA);
1239         if (!sch->private) {
1240                 rc = -ENOMEM;
1241                 goto out_err;
1242         }
1243         /*
1244          * First check if a fitting device may be found amongst the
1245          * disconnected devices or in the orphanage.
1246          */
1247         dev_id.devno = sch->schib.pmcw.dev;
1248         dev_id.ssid = sch->schid.ssid;
1249         cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1250         if (!cdev)
1251                 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1252                                                      &dev_id);
1253         if (cdev) {
1254                 /*
1255                  * Schedule moving the device until when we have a registered
1256                  * subchannel to move to and succeed the probe. We can
1257                  * unregister later again, when the probe is through.
1258                  */
1259                 cdev->private->sch = sch;
1260                 PREPARE_WORK(&cdev->private->kick_work,
1261                              ccw_device_move_to_sch);
1262                 queue_work(slow_path_wq, &cdev->private->kick_work);
1263                 return 0;
1264         }
1265         cdev = io_subchannel_create_ccwdev(sch);
1266         if (IS_ERR(cdev)) {
1267                 rc = PTR_ERR(cdev);
1268                 goto out_err;
1269         }
1270         rc = io_subchannel_recog(cdev, sch);
1271         if (rc) {
1272                 spin_lock_irqsave(sch->lock, flags);
1273                 sch_set_cdev(sch, NULL);
1274                 spin_unlock_irqrestore(sch->lock, flags);
1275                 if (cdev->dev.release)
1276                         cdev->dev.release(&cdev->dev);
1277                 goto out_err;
1278         }
1279         return 0;
1280 out_err:
1281         kfree(sch->private);
1282         sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1283         return rc;
1284 }
1285
1286 static int
1287 io_subchannel_remove (struct subchannel *sch)
1288 {
1289         struct ccw_device *cdev;
1290         unsigned long flags;
1291
1292         cdev = sch_get_cdev(sch);
1293         if (!cdev)
1294                 return 0;
1295         /* Set ccw device to not operational and drop reference. */
1296         spin_lock_irqsave(cdev->ccwlock, flags);
1297         sch_set_cdev(sch, NULL);
1298         cdev->private->state = DEV_STATE_NOT_OPER;
1299         spin_unlock_irqrestore(cdev->ccwlock, flags);
1300         ccw_device_unregister(cdev);
1301         put_device(&cdev->dev);
1302         kfree(sch->private);
1303         sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1304         return 0;
1305 }
1306
1307 static int io_subchannel_notify(struct subchannel *sch, int event)
1308 {
1309         struct ccw_device *cdev;
1310
1311         cdev = sch_get_cdev(sch);
1312         if (!cdev)
1313                 return 0;
1314         return ccw_device_notify(cdev, event);
1315 }
1316
1317 static void io_subchannel_verify(struct subchannel *sch)
1318 {
1319         struct ccw_device *cdev;
1320
1321         cdev = sch_get_cdev(sch);
1322         if (cdev)
1323                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1324 }
1325
1326 static int check_for_io_on_path(struct subchannel *sch, int mask)
1327 {
1328         int cc;
1329
1330         cc = stsch(sch->schid, &sch->schib);
1331         if (cc)
1332                 return 0;
1333         if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1334                 return 1;
1335         return 0;
1336 }
1337
1338 static void terminate_internal_io(struct subchannel *sch,
1339                                   struct ccw_device *cdev)
1340 {
1341         if (cio_clear(sch)) {
1342                 /* Recheck device in case clear failed. */
1343                 sch->lpm = 0;
1344                 if (cdev->online)
1345                         dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1346                 else
1347                         css_schedule_eval(sch->schid);
1348                 return;
1349         }
1350         cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1351         /* Request retry of internal operation. */
1352         cdev->private->flags.intretry = 1;
1353         /* Call handler. */
1354         if (cdev->handler)
1355                 cdev->handler(cdev, cdev->private->intparm,
1356                               ERR_PTR(-EIO));
1357 }
1358
1359 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1360 {
1361         struct ccw_device *cdev;
1362
1363         cdev = sch_get_cdev(sch);
1364         if (!cdev)
1365                 return;
1366         if (check_for_io_on_path(sch, mask)) {
1367                 if (cdev->private->state == DEV_STATE_ONLINE)
1368                         ccw_device_kill_io(cdev);
1369                 else {
1370                         terminate_internal_io(sch, cdev);
1371                         /* Re-start path verification. */
1372                         dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1373                 }
1374         } else
1375                 /* trigger path verification. */
1376                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1377
1378 }
1379
1380 static int io_subchannel_chp_event(struct subchannel *sch,
1381                                    struct chp_link *link, int event)
1382 {
1383         int mask;
1384
1385         mask = chp_ssd_get_mask(&sch->ssd_info, link);
1386         if (!mask)
1387                 return 0;
1388         switch (event) {
1389         case CHP_VARY_OFF:
1390                 sch->opm &= ~mask;
1391                 sch->lpm &= ~mask;
1392                 io_subchannel_terminate_path(sch, mask);
1393                 break;
1394         case CHP_VARY_ON:
1395                 sch->opm |= mask;
1396                 sch->lpm |= mask;
1397                 io_subchannel_verify(sch);
1398                 break;
1399         case CHP_OFFLINE:
1400                 if (stsch(sch->schid, &sch->schib))
1401                         return -ENXIO;
1402                 if (!css_sch_is_valid(&sch->schib))
1403                         return -ENODEV;
1404                 io_subchannel_terminate_path(sch, mask);
1405                 break;
1406         case CHP_ONLINE:
1407                 if (stsch(sch->schid, &sch->schib))
1408                         return -ENXIO;
1409                 sch->lpm |= mask & sch->opm;
1410                 io_subchannel_verify(sch);
1411                 break;
1412         }
1413         return 0;
1414 }
1415
1416 static void
1417 io_subchannel_shutdown(struct subchannel *sch)
1418 {
1419         struct ccw_device *cdev;
1420         int ret;
1421
1422         cdev = sch_get_cdev(sch);
1423
1424         if (cio_is_console(sch->schid))
1425                 return;
1426         if (!sch->schib.pmcw.ena)
1427                 /* Nothing to do. */
1428                 return;
1429         ret = cio_disable_subchannel(sch);
1430         if (ret != -EBUSY)
1431                 /* Subchannel is disabled, we're done. */
1432                 return;
1433         cdev->private->state = DEV_STATE_QUIESCE;
1434         if (cdev->handler)
1435                 cdev->handler(cdev, cdev->private->intparm,
1436                               ERR_PTR(-EIO));
1437         ret = ccw_device_cancel_halt_clear(cdev);
1438         if (ret == -EBUSY) {
1439                 ccw_device_set_timeout(cdev, HZ/10);
1440                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1441         }
1442         cio_disable_subchannel(sch);
1443 }
1444
1445 static int io_subchannel_get_status(struct subchannel *sch)
1446 {
1447         struct schib schib;
1448
1449         if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1450                 return CIO_GONE;
1451         if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1452                 return CIO_REVALIDATE;
1453         if (!sch->lpm)
1454                 return CIO_NO_PATH;
1455         return CIO_OPER;
1456 }
1457
1458 static int device_is_disconnected(struct ccw_device *cdev)
1459 {
1460         if (!cdev)
1461                 return 0;
1462         return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1463                 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1464 }
1465
1466 static int recovery_check(struct device *dev, void *data)
1467 {
1468         struct ccw_device *cdev = to_ccwdev(dev);
1469         int *redo = data;
1470
1471         spin_lock_irq(cdev->ccwlock);
1472         switch (cdev->private->state) {
1473         case DEV_STATE_DISCONNECTED:
1474                 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1475                               cdev->private->dev_id.ssid,
1476                               cdev->private->dev_id.devno);
1477                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1478                 *redo = 1;
1479                 break;
1480         case DEV_STATE_DISCONNECTED_SENSE_ID:
1481                 *redo = 1;
1482                 break;
1483         }
1484         spin_unlock_irq(cdev->ccwlock);
1485
1486         return 0;
1487 }
1488
1489 static void recovery_work_func(struct work_struct *unused)
1490 {
1491         int redo = 0;
1492
1493         bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1494         if (redo) {
1495                 spin_lock_irq(&recovery_lock);
1496                 if (!timer_pending(&recovery_timer)) {
1497                         if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1498                                 recovery_phase++;
1499                         mod_timer(&recovery_timer, jiffies +
1500                                   recovery_delay[recovery_phase] * HZ);
1501                 }
1502                 spin_unlock_irq(&recovery_lock);
1503         } else
1504                 CIO_MSG_EVENT(4, "recovery: end\n");
1505 }
1506
1507 static DECLARE_WORK(recovery_work, recovery_work_func);
1508
1509 static void recovery_func(unsigned long data)
1510 {
1511         /*
1512          * We can't do our recovery in softirq context and it's not
1513          * performance critical, so we schedule it.
1514          */
1515         schedule_work(&recovery_work);
1516 }
1517
1518 static void ccw_device_schedule_recovery(void)
1519 {
1520         unsigned long flags;
1521
1522         CIO_MSG_EVENT(4, "recovery: schedule\n");
1523         spin_lock_irqsave(&recovery_lock, flags);
1524         if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1525                 recovery_phase = 0;
1526                 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1527         }
1528         spin_unlock_irqrestore(&recovery_lock, flags);
1529 }
1530
1531 static int purge_fn(struct device *dev, void *data)
1532 {
1533         struct ccw_device *cdev = to_ccwdev(dev);
1534         struct ccw_device_private *priv = cdev->private;
1535         int unreg;
1536
1537         spin_lock_irq(cdev->ccwlock);
1538         unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1539                 (priv->state == DEV_STATE_OFFLINE);
1540         spin_unlock_irq(cdev->ccwlock);
1541         if (!unreg)
1542                 goto out;
1543         if (!get_device(&cdev->dev))
1544                 goto out;
1545         CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1546                       priv->dev_id.devno);
1547         PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
1548         queue_work(slow_path_wq, &cdev->private->kick_work);
1549
1550 out:
1551         /* Abort loop in case of pending signal. */
1552         if (signal_pending(current))
1553                 return -EINTR;
1554
1555         return 0;
1556 }
1557
1558 /**
1559  * ccw_purge_blacklisted - purge unused, blacklisted devices
1560  *
1561  * Unregister all ccw devices that are offline and on the blacklist.
1562  */
1563 int ccw_purge_blacklisted(void)
1564 {
1565         CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1566         bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1567         return 0;
1568 }
1569
1570 static void device_set_disconnected(struct ccw_device *cdev)
1571 {
1572         if (!cdev)
1573                 return;
1574         ccw_device_set_timeout(cdev, 0);
1575         cdev->private->flags.fake_irb = 0;
1576         cdev->private->state = DEV_STATE_DISCONNECTED;
1577         if (cdev->online)
1578                 ccw_device_schedule_recovery();
1579 }
1580
1581 void ccw_device_set_notoper(struct ccw_device *cdev)
1582 {
1583         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1584
1585         CIO_TRACE_EVENT(2, "notoper");
1586         CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1587         ccw_device_set_timeout(cdev, 0);
1588         cio_disable_subchannel(sch);
1589         cdev->private->state = DEV_STATE_NOT_OPER;
1590 }
1591
1592 static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1593 {
1594         int event, ret, disc;
1595         unsigned long flags;
1596         enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1597         struct ccw_device *cdev;
1598
1599         spin_lock_irqsave(sch->lock, flags);
1600         cdev = sch_get_cdev(sch);
1601         disc = device_is_disconnected(cdev);
1602         if (disc && slow) {
1603                 /* Disconnected devices are evaluated directly only.*/
1604                 spin_unlock_irqrestore(sch->lock, flags);
1605                 return 0;
1606         }
1607         /* No interrupt after machine check - kill pending timers. */
1608         if (cdev)
1609                 ccw_device_set_timeout(cdev, 0);
1610         if (!disc && !slow) {
1611                 /* Non-disconnected devices are evaluated on the slow path. */
1612                 spin_unlock_irqrestore(sch->lock, flags);
1613                 return -EAGAIN;
1614         }
1615         event = io_subchannel_get_status(sch);
1616         CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1617                       sch->schid.ssid, sch->schid.sch_no, event,
1618                       disc ? "disconnected" : "normal",
1619                       slow ? "slow" : "fast");
1620         /* Analyze subchannel status. */
1621         action = NONE;
1622         switch (event) {
1623         case CIO_NO_PATH:
1624                 if (disc) {
1625                         /* Check if paths have become available. */
1626                         action = REPROBE;
1627                         break;
1628                 }
1629                 /* fall through */
1630         case CIO_GONE:
1631                 /* Ask driver what to do with device. */
1632                 if (io_subchannel_notify(sch, event))
1633                         action = DISC;
1634                 else
1635                         action = UNREGISTER;
1636                 break;
1637         case CIO_REVALIDATE:
1638                 /* Device will be removed, so no notify necessary. */
1639                 if (disc)
1640                         /* Reprobe because immediate unregister might block. */
1641                         action = REPROBE;
1642                 else
1643                         action = UNREGISTER_PROBE;
1644                 break;
1645         case CIO_OPER:
1646                 if (disc)
1647                         /* Get device operational again. */
1648                         action = REPROBE;
1649                 break;
1650         }
1651         /* Perform action. */
1652         ret = 0;
1653         switch (action) {
1654         case UNREGISTER:
1655         case UNREGISTER_PROBE:
1656                 ccw_device_set_notoper(cdev);
1657                 /* Unregister device (will use subchannel lock). */
1658                 spin_unlock_irqrestore(sch->lock, flags);
1659                 css_sch_device_unregister(sch);
1660                 spin_lock_irqsave(sch->lock, flags);
1661
1662                 /* Reset intparm to zeroes. */
1663                 sch->schib.pmcw.intparm = 0;
1664                 cio_modify(sch);
1665                 break;
1666         case REPROBE:
1667                 ccw_device_trigger_reprobe(cdev);
1668                 break;
1669         case DISC:
1670                 device_set_disconnected(cdev);
1671                 break;
1672         default:
1673                 break;
1674         }
1675         spin_unlock_irqrestore(sch->lock, flags);
1676         /* Probe if necessary. */
1677         if (action == UNREGISTER_PROBE)
1678                 ret = css_probe_device(sch->schid);
1679
1680         return ret;
1681 }
1682
1683 #ifdef CONFIG_CCW_CONSOLE
1684 static struct ccw_device console_cdev;
1685 static char console_cdev_name[10] = "0.x.xxxx";
1686 static struct ccw_device_private console_private;
1687 static int console_cdev_in_use;
1688
1689 static DEFINE_SPINLOCK(ccw_console_lock);
1690
1691 spinlock_t * cio_get_console_lock(void)
1692 {
1693         return &ccw_console_lock;
1694 }
1695
1696 static int ccw_device_console_enable(struct ccw_device *cdev,
1697                                      struct subchannel *sch)
1698 {
1699         int rc;
1700
1701         /* Attach subchannel private data. */
1702         sch->private = cio_get_console_priv();
1703         memset(sch->private, 0, sizeof(struct io_subchannel_private));
1704         io_subchannel_init_fields(sch);
1705         sch->driver = &io_subchannel_driver;
1706         /* Initialize the ccw_device structure. */
1707         cdev->dev.parent= &sch->dev;
1708         rc = io_subchannel_recog(cdev, sch);
1709         if (rc)
1710                 return rc;
1711
1712         /* Now wait for the async. recognition to come to an end. */
1713         spin_lock_irq(cdev->ccwlock);
1714         while (!dev_fsm_final_state(cdev))
1715                 wait_cons_dev();
1716         rc = -EIO;
1717         if (cdev->private->state != DEV_STATE_OFFLINE)
1718                 goto out_unlock;
1719         ccw_device_online(cdev);
1720         while (!dev_fsm_final_state(cdev))
1721                 wait_cons_dev();
1722         if (cdev->private->state != DEV_STATE_ONLINE)
1723                 goto out_unlock;
1724         rc = 0;
1725 out_unlock:
1726         spin_unlock_irq(cdev->ccwlock);
1727         return 0;
1728 }
1729
1730 struct ccw_device *
1731 ccw_device_probe_console(void)
1732 {
1733         struct subchannel *sch;
1734         int ret;
1735
1736         if (xchg(&console_cdev_in_use, 1) != 0)
1737                 return ERR_PTR(-EBUSY);
1738         sch = cio_probe_console();
1739         if (IS_ERR(sch)) {
1740                 console_cdev_in_use = 0;
1741                 return (void *) sch;
1742         }
1743         memset(&console_cdev, 0, sizeof(struct ccw_device));
1744         memset(&console_private, 0, sizeof(struct ccw_device_private));
1745         console_cdev.private = &console_private;
1746         console_private.cdev = &console_cdev;
1747         ret = ccw_device_console_enable(&console_cdev, sch);
1748         if (ret) {
1749                 cio_release_console();
1750                 console_cdev_in_use = 0;
1751                 return ERR_PTR(ret);
1752         }
1753         console_cdev.online = 1;
1754         return &console_cdev;
1755 }
1756
1757
1758 const char *cio_get_console_cdev_name(struct subchannel *sch)
1759 {
1760         snprintf(console_cdev_name, 10, "0.%x.%04x",
1761                  sch->schid.ssid, sch->schib.pmcw.dev);
1762         return (const char *)console_cdev_name;
1763 }
1764 #endif
1765
1766 /*
1767  * get ccw_device matching the busid, but only if owned by cdrv
1768  */
1769 static int
1770 __ccwdev_check_busid(struct device *dev, void *id)
1771 {
1772         char *bus_id;
1773
1774         bus_id = id;
1775
1776         return (strcmp(bus_id, dev_name(dev)) == 0);
1777 }
1778
1779
1780 /**
1781  * get_ccwdev_by_busid() - obtain device from a bus id
1782  * @cdrv: driver the device is owned by
1783  * @bus_id: bus id of the device to be searched
1784  *
1785  * This function searches all devices owned by @cdrv for a device with a bus
1786  * id matching @bus_id.
1787  * Returns:
1788  *  If a match is found, its reference count of the found device is increased
1789  *  and it is returned; else %NULL is returned.
1790  */
1791 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1792                                        const char *bus_id)
1793 {
1794         struct device *dev;
1795         struct device_driver *drv;
1796
1797         drv = get_driver(&cdrv->driver);
1798         if (!drv)
1799                 return NULL;
1800
1801         dev = driver_find_device(drv, NULL, (void *)bus_id,
1802                                  __ccwdev_check_busid);
1803         put_driver(drv);
1804
1805         return dev ? to_ccwdev(dev) : NULL;
1806 }
1807
1808 /************************** device driver handling ************************/
1809
1810 /* This is the implementation of the ccw_driver class. The probe, remove
1811  * and release methods are initially very similar to the device_driver
1812  * implementations, with the difference that they have ccw_device
1813  * arguments.
1814  *
1815  * A ccw driver also contains the information that is needed for
1816  * device matching.
1817  */
1818 static int
1819 ccw_device_probe (struct device *dev)
1820 {
1821         struct ccw_device *cdev = to_ccwdev(dev);
1822         struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1823         int ret;
1824
1825         cdev->drv = cdrv; /* to let the driver call _set_online */
1826
1827         ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1828
1829         if (ret) {
1830                 cdev->drv = NULL;
1831                 return ret;
1832         }
1833
1834         return 0;
1835 }
1836
1837 static int
1838 ccw_device_remove (struct device *dev)
1839 {
1840         struct ccw_device *cdev = to_ccwdev(dev);
1841         struct ccw_driver *cdrv = cdev->drv;
1842         int ret;
1843
1844         if (cdrv->remove)
1845                 cdrv->remove(cdev);
1846         if (cdev->online) {
1847                 cdev->online = 0;
1848                 spin_lock_irq(cdev->ccwlock);
1849                 ret = ccw_device_offline(cdev);
1850                 spin_unlock_irq(cdev->ccwlock);
1851                 if (ret == 0)
1852                         wait_event(cdev->private->wait_q,
1853                                    dev_fsm_final_state(cdev));
1854                 else
1855                         CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1856                                       "device 0.%x.%04x\n",
1857                                       ret, cdev->private->dev_id.ssid,
1858                                       cdev->private->dev_id.devno);
1859                 /* Give up reference obtained in ccw_device_set_online(). */
1860                 put_device(&cdev->dev);
1861         }
1862         ccw_device_set_timeout(cdev, 0);
1863         cdev->drv = NULL;
1864         return 0;
1865 }
1866
1867 static void ccw_device_shutdown(struct device *dev)
1868 {
1869         struct ccw_device *cdev;
1870
1871         cdev = to_ccwdev(dev);
1872         if (cdev->drv && cdev->drv->shutdown)
1873                 cdev->drv->shutdown(cdev);
1874         disable_cmf(cdev);
1875 }
1876
1877 struct bus_type ccw_bus_type = {
1878         .name   = "ccw",
1879         .match  = ccw_bus_match,
1880         .uevent = ccw_uevent,
1881         .probe  = ccw_device_probe,
1882         .remove = ccw_device_remove,
1883         .shutdown = ccw_device_shutdown,
1884 };
1885
1886 /**
1887  * ccw_driver_register() - register a ccw driver
1888  * @cdriver: driver to be registered
1889  *
1890  * This function is mainly a wrapper around driver_register().
1891  * Returns:
1892  *   %0 on success and a negative error value on failure.
1893  */
1894 int ccw_driver_register(struct ccw_driver *cdriver)
1895 {
1896         struct device_driver *drv = &cdriver->driver;
1897
1898         drv->bus = &ccw_bus_type;
1899         drv->name = cdriver->name;
1900         drv->owner = cdriver->owner;
1901
1902         return driver_register(drv);
1903 }
1904
1905 /**
1906  * ccw_driver_unregister() - deregister a ccw driver
1907  * @cdriver: driver to be deregistered
1908  *
1909  * This function is mainly a wrapper around driver_unregister().
1910  */
1911 void ccw_driver_unregister(struct ccw_driver *cdriver)
1912 {
1913         driver_unregister(&cdriver->driver);
1914 }
1915
1916 /* Helper func for qdio. */
1917 struct subchannel_id
1918 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1919 {
1920         struct subchannel *sch;
1921
1922         sch = to_subchannel(cdev->dev.parent);
1923         return sch->schid;
1924 }
1925
1926 MODULE_LICENSE("GPL");
1927 EXPORT_SYMBOL(ccw_device_set_online);
1928 EXPORT_SYMBOL(ccw_device_set_offline);
1929 EXPORT_SYMBOL(ccw_driver_register);
1930 EXPORT_SYMBOL(ccw_driver_unregister);
1931 EXPORT_SYMBOL(get_ccwdev_by_busid);
1932 EXPORT_SYMBOL(ccw_bus_type);
1933 EXPORT_SYMBOL(ccw_device_work);
1934 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);