MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / s390 / cio / device.c
blobd734f4a0ecac23cea1d821b316a563b71087ccc6
1 /*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
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 */
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/device.h>
22 #include <linux/workqueue.h>
23 #include <linux/timer.h>
24 #include <linux/kernel_stat.h>
26 #include <asm/ccwdev.h>
27 #include <asm/cio.h>
28 #include <asm/param.h> /* HZ */
29 #include <asm/cmb.h>
30 #include <asm/isc.h>
32 #include "chp.h"
33 #include "cio.h"
34 #include "cio_debug.h"
35 #include "css.h"
36 #include "device.h"
37 #include "ioasm.h"
38 #include "io_sch.h"
39 #include "blacklist.h"
40 #include "chsc.h"
42 static struct timer_list recovery_timer;
43 static DEFINE_SPINLOCK(recovery_lock);
44 static int recovery_phase;
45 static const unsigned long recovery_delay[] = { 3, 30, 300 };
47 /******************* bus type handling ***********************/
49 /* The Linux driver model distinguishes between a bus type and
50 * the bus itself. Of course we only have one channel
51 * subsystem driver and one channel system per machine, but
52 * we still use the abstraction. T.R. says it's a good idea. */
53 static int
54 ccw_bus_match (struct device * dev, struct device_driver * drv)
56 struct ccw_device *cdev = to_ccwdev(dev);
57 struct ccw_driver *cdrv = to_ccwdrv(drv);
58 const struct ccw_device_id *ids = cdrv->ids, *found;
60 if (!ids)
61 return 0;
63 found = ccw_device_id_match(ids, &cdev->id);
64 if (!found)
65 return 0;
67 cdev->id.driver_info = found->driver_info;
69 return 1;
72 /* Store modalias string delimited by prefix/suffix string into buffer with
73 * specified size. Return length of resulting string (excluding trailing '\0')
74 * even if string doesn't fit buffer (snprintf semantics). */
75 static int snprint_alias(char *buf, size_t size,
76 struct ccw_device_id *id, const char *suffix)
78 int len;
80 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
81 if (len > size)
82 return len;
83 buf += len;
84 size -= len;
86 if (id->dev_type != 0)
87 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
88 id->dev_model, suffix);
89 else
90 len += snprintf(buf, size, "dtdm%s", suffix);
92 return len;
95 /* Set up environment variables for ccw device uevent. Return 0 on success,
96 * non-zero otherwise. */
97 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
99 struct ccw_device *cdev = to_ccwdev(dev);
100 struct ccw_device_id *id = &(cdev->id);
101 int ret;
102 char modalias_buf[30];
104 /* CU_TYPE= */
105 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
106 if (ret)
107 return ret;
109 /* CU_MODEL= */
110 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
111 if (ret)
112 return ret;
114 /* The next two can be zero, that's ok for us */
115 /* DEV_TYPE= */
116 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
117 if (ret)
118 return ret;
120 /* DEV_MODEL= */
121 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
122 if (ret)
123 return ret;
125 /* MODALIAS= */
126 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
127 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
128 return ret;
131 static struct bus_type ccw_bus_type;
133 static void io_subchannel_irq(struct subchannel *);
134 static int io_subchannel_probe(struct subchannel *);
135 static int io_subchannel_remove(struct subchannel *);
136 static void io_subchannel_shutdown(struct subchannel *);
137 static int io_subchannel_sch_event(struct subchannel *, int);
138 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
139 int);
140 static void recovery_func(unsigned long data);
141 wait_queue_head_t ccw_device_init_wq;
142 atomic_t ccw_device_init_count;
144 static struct css_device_id io_subchannel_ids[] = {
145 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
146 { /* end of list */ },
148 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
150 static int io_subchannel_prepare(struct subchannel *sch)
152 struct ccw_device *cdev;
154 * Don't allow suspend while a ccw device registration
155 * is still outstanding.
157 cdev = sch_get_cdev(sch);
158 if (cdev && !device_is_registered(&cdev->dev))
159 return -EAGAIN;
160 return 0;
163 static int io_subchannel_settle(void)
165 int ret;
167 ret = wait_event_interruptible(ccw_device_init_wq,
168 atomic_read(&ccw_device_init_count) == 0);
169 if (ret)
170 return -EINTR;
171 flush_workqueue(cio_work_q);
172 return 0;
175 static struct css_driver io_subchannel_driver = {
176 .drv = {
177 .owner = THIS_MODULE,
178 .name = "io_subchannel",
180 .subchannel_type = io_subchannel_ids,
181 .irq = io_subchannel_irq,
182 .sch_event = io_subchannel_sch_event,
183 .chp_event = io_subchannel_chp_event,
184 .probe = io_subchannel_probe,
185 .remove = io_subchannel_remove,
186 .shutdown = io_subchannel_shutdown,
187 .prepare = io_subchannel_prepare,
188 .settle = io_subchannel_settle,
191 int __init io_subchannel_init(void)
193 int ret;
195 init_waitqueue_head(&ccw_device_init_wq);
196 atomic_set(&ccw_device_init_count, 0);
197 setup_timer(&recovery_timer, recovery_func, 0);
199 ret = bus_register(&ccw_bus_type);
200 if (ret)
201 return ret;
202 ret = css_driver_register(&io_subchannel_driver);
203 if (ret)
204 bus_unregister(&ccw_bus_type);
206 return ret;
210 /************************ device handling **************************/
213 * A ccw_device has some interfaces in sysfs in addition to the
214 * standard ones.
215 * The following entries are designed to export the information which
216 * resided in 2.4 in /proc/subchannels. Subchannel and device number
217 * are obvious, so they don't have an entry :)
218 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
220 static ssize_t
221 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
223 struct subchannel *sch = to_subchannel(dev);
224 struct chsc_ssd_info *ssd = &sch->ssd_info;
225 ssize_t ret = 0;
226 int chp;
227 int mask;
229 for (chp = 0; chp < 8; chp++) {
230 mask = 0x80 >> chp;
231 if (ssd->path_mask & mask)
232 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
233 else
234 ret += sprintf(buf + ret, "00 ");
236 ret += sprintf (buf+ret, "\n");
237 return min((ssize_t)PAGE_SIZE, ret);
240 static ssize_t
241 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
243 struct subchannel *sch = to_subchannel(dev);
244 struct pmcw *pmcw = &sch->schib.pmcw;
246 return sprintf (buf, "%02x %02x %02x\n",
247 pmcw->pim, pmcw->pam, pmcw->pom);
250 static ssize_t
251 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
253 struct ccw_device *cdev = to_ccwdev(dev);
254 struct ccw_device_id *id = &(cdev->id);
256 if (id->dev_type != 0)
257 return sprintf(buf, "%04x/%02x\n",
258 id->dev_type, id->dev_model);
259 else
260 return sprintf(buf, "n/a\n");
263 static ssize_t
264 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
266 struct ccw_device *cdev = to_ccwdev(dev);
267 struct ccw_device_id *id = &(cdev->id);
269 return sprintf(buf, "%04x/%02x\n",
270 id->cu_type, id->cu_model);
273 static ssize_t
274 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
276 struct ccw_device *cdev = to_ccwdev(dev);
277 struct ccw_device_id *id = &(cdev->id);
278 int len;
280 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
282 return len > PAGE_SIZE ? PAGE_SIZE : len;
285 static ssize_t
286 online_show (struct device *dev, struct device_attribute *attr, char *buf)
288 struct ccw_device *cdev = to_ccwdev(dev);
290 return sprintf(buf, cdev->online ? "1\n" : "0\n");
293 int ccw_device_is_orphan(struct ccw_device *cdev)
295 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
298 static void ccw_device_unregister(struct ccw_device *cdev)
300 if (device_is_registered(&cdev->dev)) {
301 /* Undo device_add(). */
302 device_del(&cdev->dev);
304 if (cdev->private->flags.initialized) {
305 cdev->private->flags.initialized = 0;
306 /* Release reference from device_initialize(). */
307 put_device(&cdev->dev);
311 static void io_subchannel_quiesce(struct subchannel *);
314 * ccw_device_set_offline() - disable a ccw device for I/O
315 * @cdev: target ccw device
317 * This function calls the driver's set_offline() function for @cdev, if
318 * given, and then disables @cdev.
319 * Returns:
320 * %0 on success and a negative error value on failure.
321 * Context:
322 * enabled, ccw device lock not held
324 int ccw_device_set_offline(struct ccw_device *cdev)
326 struct subchannel *sch;
327 int ret, state;
329 if (!cdev)
330 return -ENODEV;
331 if (!cdev->online || !cdev->drv)
332 return -EINVAL;
334 if (cdev->drv->set_offline) {
335 ret = cdev->drv->set_offline(cdev);
336 if (ret != 0)
337 return ret;
339 cdev->online = 0;
340 spin_lock_irq(cdev->ccwlock);
341 sch = to_subchannel(cdev->dev.parent);
342 /* Wait until a final state or DISCONNECTED is reached */
343 while (!dev_fsm_final_state(cdev) &&
344 cdev->private->state != DEV_STATE_DISCONNECTED) {
345 spin_unlock_irq(cdev->ccwlock);
346 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
347 cdev->private->state == DEV_STATE_DISCONNECTED));
348 spin_lock_irq(cdev->ccwlock);
350 do {
351 ret = ccw_device_offline(cdev);
352 if (!ret)
353 break;
354 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
355 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
356 cdev->private->dev_id.devno);
357 if (ret != -EBUSY)
358 goto error;
359 state = cdev->private->state;
360 spin_unlock_irq(cdev->ccwlock);
361 io_subchannel_quiesce(sch);
362 spin_lock_irq(cdev->ccwlock);
363 cdev->private->state = state;
364 } while (ret == -EBUSY);
365 spin_unlock_irq(cdev->ccwlock);
366 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
367 cdev->private->state == DEV_STATE_DISCONNECTED));
368 /* Inform the user if set offline failed. */
369 if (cdev->private->state == DEV_STATE_BOXED) {
370 pr_warning("%s: The device entered boxed state while "
371 "being set offline\n", dev_name(&cdev->dev));
372 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
373 pr_warning("%s: The device stopped operating while "
374 "being set offline\n", dev_name(&cdev->dev));
376 /* Give up reference from ccw_device_set_online(). */
377 put_device(&cdev->dev);
378 return 0;
380 error:
381 cdev->private->state = DEV_STATE_OFFLINE;
382 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
383 spin_unlock_irq(cdev->ccwlock);
384 /* Give up reference from ccw_device_set_online(). */
385 put_device(&cdev->dev);
386 return -ENODEV;
390 * ccw_device_set_online() - enable a ccw device for I/O
391 * @cdev: target ccw device
393 * This function first enables @cdev and then calls the driver's set_online()
394 * function for @cdev, if given. If set_online() returns an error, @cdev is
395 * disabled again.
396 * Returns:
397 * %0 on success and a negative error value on failure.
398 * Context:
399 * enabled, ccw device lock not held
401 int ccw_device_set_online(struct ccw_device *cdev)
403 int ret;
404 int ret2;
406 if (!cdev)
407 return -ENODEV;
408 if (cdev->online || !cdev->drv)
409 return -EINVAL;
410 /* Hold on to an extra reference while device is online. */
411 if (!get_device(&cdev->dev))
412 return -ENODEV;
414 spin_lock_irq(cdev->ccwlock);
415 ret = ccw_device_online(cdev);
416 spin_unlock_irq(cdev->ccwlock);
417 if (ret == 0)
418 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
419 else {
420 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
421 "device 0.%x.%04x\n",
422 ret, cdev->private->dev_id.ssid,
423 cdev->private->dev_id.devno);
424 /* Give up online reference since onlining failed. */
425 put_device(&cdev->dev);
426 return ret;
428 spin_lock_irq(cdev->ccwlock);
429 /* Check if online processing was successful */
430 if ((cdev->private->state != DEV_STATE_ONLINE) &&
431 (cdev->private->state != DEV_STATE_W4SENSE)) {
432 spin_unlock_irq(cdev->ccwlock);
433 /* Inform the user that set online failed. */
434 if (cdev->private->state == DEV_STATE_BOXED) {
435 pr_warning("%s: Setting the device online failed "
436 "because it is boxed\n",
437 dev_name(&cdev->dev));
438 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
439 pr_warning("%s: Setting the device online failed "
440 "because it is not operational\n",
441 dev_name(&cdev->dev));
443 /* Give up online reference since onlining failed. */
444 put_device(&cdev->dev);
445 return -ENODEV;
447 spin_unlock_irq(cdev->ccwlock);
448 if (cdev->drv->set_online)
449 ret = cdev->drv->set_online(cdev);
450 if (ret)
451 goto rollback;
452 cdev->online = 1;
453 return 0;
455 rollback:
456 spin_lock_irq(cdev->ccwlock);
457 /* Wait until a final state or DISCONNECTED is reached */
458 while (!dev_fsm_final_state(cdev) &&
459 cdev->private->state != DEV_STATE_DISCONNECTED) {
460 spin_unlock_irq(cdev->ccwlock);
461 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
462 cdev->private->state == DEV_STATE_DISCONNECTED));
463 spin_lock_irq(cdev->ccwlock);
465 ret2 = ccw_device_offline(cdev);
466 if (ret2)
467 goto error;
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
470 cdev->private->state == DEV_STATE_DISCONNECTED));
471 /* Give up online reference since onlining failed. */
472 put_device(&cdev->dev);
473 return ret;
475 error:
476 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
477 "device 0.%x.%04x\n",
478 ret2, cdev->private->dev_id.ssid,
479 cdev->private->dev_id.devno);
480 cdev->private->state = DEV_STATE_OFFLINE;
481 spin_unlock_irq(cdev->ccwlock);
482 /* Give up online reference since onlining failed. */
483 put_device(&cdev->dev);
484 return ret;
487 static int online_store_handle_offline(struct ccw_device *cdev)
489 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
490 spin_lock_irq(cdev->ccwlock);
491 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
492 spin_unlock_irq(cdev->ccwlock);
493 return 0;
495 if (cdev->drv && cdev->drv->set_offline)
496 return ccw_device_set_offline(cdev);
497 return -EINVAL;
500 static int online_store_recog_and_online(struct ccw_device *cdev)
502 /* Do device recognition, if needed. */
503 if (cdev->private->state == DEV_STATE_BOXED) {
504 spin_lock_irq(cdev->ccwlock);
505 ccw_device_recognition(cdev);
506 spin_unlock_irq(cdev->ccwlock);
507 wait_event(cdev->private->wait_q,
508 cdev->private->flags.recog_done);
509 if (cdev->private->state != DEV_STATE_OFFLINE)
510 /* recognition failed */
511 return -EAGAIN;
513 if (cdev->drv && cdev->drv->set_online)
514 return ccw_device_set_online(cdev);
515 return -EINVAL;
518 static int online_store_handle_online(struct ccw_device *cdev, int force)
520 int ret;
522 ret = online_store_recog_and_online(cdev);
523 if (ret && !force)
524 return ret;
525 if (force && cdev->private->state == DEV_STATE_BOXED) {
526 ret = ccw_device_stlck(cdev);
527 if (ret)
528 return ret;
529 if (cdev->id.cu_type == 0)
530 cdev->private->state = DEV_STATE_NOT_OPER;
531 ret = online_store_recog_and_online(cdev);
532 if (ret)
533 return ret;
535 return 0;
538 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
539 const char *buf, size_t count)
541 struct ccw_device *cdev = to_ccwdev(dev);
542 int force, ret;
543 unsigned long i;
545 /* Prevent conflict between multiple on-/offline processing requests. */
546 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
547 return -EAGAIN;
548 /* Prevent conflict between internal I/Os and on-/offline processing. */
549 if (!dev_fsm_final_state(cdev) &&
550 cdev->private->state != DEV_STATE_DISCONNECTED) {
551 ret = -EAGAIN;
552 goto out_onoff;
554 /* Prevent conflict between pending work and on-/offline processing.*/
555 if (work_pending(&cdev->private->todo_work)) {
556 ret = -EAGAIN;
557 goto out_onoff;
560 if (cdev->drv && !try_module_get(cdev->drv->driver.owner)) {
561 ret = -EINVAL;
562 goto out_onoff;
564 if (!strncmp(buf, "force\n", count)) {
565 force = 1;
566 i = 1;
567 ret = 0;
568 } else {
569 force = 0;
570 ret = strict_strtoul(buf, 16, &i);
572 if (ret)
573 goto out;
574 switch (i) {
575 case 0:
576 ret = online_store_handle_offline(cdev);
577 break;
578 case 1:
579 ret = online_store_handle_online(cdev, force);
580 break;
581 default:
582 ret = -EINVAL;
584 out:
585 if (cdev->drv)
586 module_put(cdev->drv->driver.owner);
587 out_onoff:
588 atomic_set(&cdev->private->onoff, 0);
589 return (ret < 0) ? ret : count;
592 static ssize_t
593 available_show (struct device *dev, struct device_attribute *attr, char *buf)
595 struct ccw_device *cdev = to_ccwdev(dev);
596 struct subchannel *sch;
598 if (ccw_device_is_orphan(cdev))
599 return sprintf(buf, "no device\n");
600 switch (cdev->private->state) {
601 case DEV_STATE_BOXED:
602 return sprintf(buf, "boxed\n");
603 case DEV_STATE_DISCONNECTED:
604 case DEV_STATE_DISCONNECTED_SENSE_ID:
605 case DEV_STATE_NOT_OPER:
606 sch = to_subchannel(dev->parent);
607 if (!sch->lpm)
608 return sprintf(buf, "no path\n");
609 else
610 return sprintf(buf, "no device\n");
611 default:
612 /* All other states considered fine. */
613 return sprintf(buf, "good\n");
617 static ssize_t
618 initiate_logging(struct device *dev, struct device_attribute *attr,
619 const char *buf, size_t count)
621 struct subchannel *sch = to_subchannel(dev);
622 int rc;
624 rc = chsc_siosl(sch->schid);
625 if (rc < 0) {
626 pr_warning("Logging for subchannel 0.%x.%04x failed with "
627 "errno=%d\n",
628 sch->schid.ssid, sch->schid.sch_no, rc);
629 return rc;
631 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
632 sch->schid.ssid, sch->schid.sch_no);
633 return count;
636 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
637 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
638 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
639 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
640 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
641 static DEVICE_ATTR(online, 0644, online_show, online_store);
642 static DEVICE_ATTR(availability, 0444, available_show, NULL);
643 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
645 static struct attribute *io_subchannel_attrs[] = {
646 &dev_attr_chpids.attr,
647 &dev_attr_pimpampom.attr,
648 &dev_attr_logging.attr,
649 NULL,
652 static struct attribute_group io_subchannel_attr_group = {
653 .attrs = io_subchannel_attrs,
656 static struct attribute * ccwdev_attrs[] = {
657 &dev_attr_devtype.attr,
658 &dev_attr_cutype.attr,
659 &dev_attr_modalias.attr,
660 &dev_attr_online.attr,
661 &dev_attr_cmb_enable.attr,
662 &dev_attr_availability.attr,
663 NULL,
666 static struct attribute_group ccwdev_attr_group = {
667 .attrs = ccwdev_attrs,
670 static const struct attribute_group *ccwdev_attr_groups[] = {
671 &ccwdev_attr_group,
672 NULL,
675 /* this is a simple abstraction for device_register that sets the
676 * correct bus type and adds the bus specific files */
677 static int ccw_device_register(struct ccw_device *cdev)
679 struct device *dev = &cdev->dev;
680 int ret;
682 dev->bus = &ccw_bus_type;
683 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
684 cdev->private->dev_id.devno);
685 if (ret)
686 return ret;
687 return device_add(dev);
690 static int match_dev_id(struct device *dev, void *data)
692 struct ccw_device *cdev = to_ccwdev(dev);
693 struct ccw_dev_id *dev_id = data;
695 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
698 static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
700 struct device *dev;
702 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
704 return dev ? to_ccwdev(dev) : NULL;
707 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
709 int ret;
711 if (device_is_registered(&cdev->dev)) {
712 device_release_driver(&cdev->dev);
713 ret = device_attach(&cdev->dev);
714 WARN_ON(ret == -ENODEV);
718 static void
719 ccw_device_release(struct device *dev)
721 struct ccw_device *cdev;
723 cdev = to_ccwdev(dev);
724 /* Release reference of parent subchannel. */
725 put_device(cdev->dev.parent);
726 kfree(cdev->private);
727 kfree(cdev);
730 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
732 struct ccw_device *cdev;
734 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
735 if (cdev) {
736 cdev->private = kzalloc(sizeof(struct ccw_device_private),
737 GFP_KERNEL | GFP_DMA);
738 if (cdev->private)
739 return cdev;
741 kfree(cdev);
742 return ERR_PTR(-ENOMEM);
745 static void ccw_device_todo(struct work_struct *work);
747 static int io_subchannel_initialize_dev(struct subchannel *sch,
748 struct ccw_device *cdev)
750 cdev->private->cdev = cdev;
751 cdev->private->int_class = IOINT_CIO;
752 atomic_set(&cdev->private->onoff, 0);
753 cdev->dev.parent = &sch->dev;
754 cdev->dev.release = ccw_device_release;
755 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
756 cdev->dev.groups = ccwdev_attr_groups;
757 /* Do first half of device_register. */
758 device_initialize(&cdev->dev);
759 if (!get_device(&sch->dev)) {
760 /* Release reference from device_initialize(). */
761 put_device(&cdev->dev);
762 return -ENODEV;
764 cdev->private->flags.initialized = 1;
765 return 0;
768 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
770 struct ccw_device *cdev;
771 int ret;
773 cdev = io_subchannel_allocate_dev(sch);
774 if (!IS_ERR(cdev)) {
775 ret = io_subchannel_initialize_dev(sch, cdev);
776 if (ret)
777 cdev = ERR_PTR(ret);
779 return cdev;
782 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
784 static void sch_create_and_recog_new_device(struct subchannel *sch)
786 struct ccw_device *cdev;
788 /* Need to allocate a new ccw device. */
789 cdev = io_subchannel_create_ccwdev(sch);
790 if (IS_ERR(cdev)) {
791 /* OK, we did everything we could... */
792 css_sch_device_unregister(sch);
793 return;
795 /* Start recognition for the new ccw device. */
796 io_subchannel_recog(cdev, sch);
800 * Register recognized device.
802 static void io_subchannel_register(struct ccw_device *cdev)
804 struct subchannel *sch;
805 int ret, adjust_init_count = 1;
806 unsigned long flags;
808 sch = to_subchannel(cdev->dev.parent);
810 * Check if subchannel is still registered. It may have become
811 * unregistered if a machine check hit us after finishing
812 * device recognition but before the register work could be
813 * queued.
815 if (!device_is_registered(&sch->dev))
816 goto out_err;
817 css_update_ssd_info(sch);
819 * io_subchannel_register() will also be called after device
820 * recognition has been done for a boxed device (which will already
821 * be registered). We need to reprobe since we may now have sense id
822 * information.
824 if (device_is_registered(&cdev->dev)) {
825 if (!cdev->drv) {
826 ret = device_reprobe(&cdev->dev);
827 if (ret)
828 /* We can't do much here. */
829 CIO_MSG_EVENT(0, "device_reprobe() returned"
830 " %d for 0.%x.%04x\n", ret,
831 cdev->private->dev_id.ssid,
832 cdev->private->dev_id.devno);
834 adjust_init_count = 0;
835 goto out;
838 * Now we know this subchannel will stay, we can throw
839 * our delayed uevent.
841 dev_set_uevent_suppress(&sch->dev, 0);
842 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
843 /* make it known to the system */
844 ret = ccw_device_register(cdev);
845 if (ret) {
846 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
847 cdev->private->dev_id.ssid,
848 cdev->private->dev_id.devno, ret);
849 spin_lock_irqsave(sch->lock, flags);
850 sch_set_cdev(sch, NULL);
851 spin_unlock_irqrestore(sch->lock, flags);
852 /* Release initial device reference. */
853 put_device(&cdev->dev);
854 goto out_err;
856 out:
857 cdev->private->flags.recog_done = 1;
858 wake_up(&cdev->private->wait_q);
859 out_err:
860 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
861 wake_up(&ccw_device_init_wq);
864 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
866 struct subchannel *sch;
868 /* Get subchannel reference for local processing. */
869 if (!get_device(cdev->dev.parent))
870 return;
871 sch = to_subchannel(cdev->dev.parent);
872 css_sch_device_unregister(sch);
873 /* Release subchannel reference for local processing. */
874 put_device(&sch->dev);
878 * subchannel recognition done. Called from the state machine.
880 void
881 io_subchannel_recog_done(struct ccw_device *cdev)
883 if (css_init_done == 0) {
884 cdev->private->flags.recog_done = 1;
885 return;
887 switch (cdev->private->state) {
888 case DEV_STATE_BOXED:
889 /* Device did not respond in time. */
890 case DEV_STATE_NOT_OPER:
891 cdev->private->flags.recog_done = 1;
892 /* Remove device found not operational. */
893 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
894 if (atomic_dec_and_test(&ccw_device_init_count))
895 wake_up(&ccw_device_init_wq);
896 break;
897 case DEV_STATE_OFFLINE:
899 * We can't register the device in interrupt context so
900 * we schedule a work item.
902 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
903 break;
907 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
909 struct ccw_device_private *priv;
911 cdev->ccwlock = sch->lock;
913 /* Init private data. */
914 priv = cdev->private;
915 priv->dev_id.devno = sch->schib.pmcw.dev;
916 priv->dev_id.ssid = sch->schid.ssid;
917 priv->schid = sch->schid;
918 priv->state = DEV_STATE_NOT_OPER;
919 INIT_LIST_HEAD(&priv->cmb_list);
920 init_waitqueue_head(&priv->wait_q);
921 init_timer(&priv->timer);
923 /* Increase counter of devices currently in recognition. */
924 atomic_inc(&ccw_device_init_count);
926 /* Start async. device sensing. */
927 spin_lock_irq(sch->lock);
928 sch_set_cdev(sch, cdev);
929 ccw_device_recognition(cdev);
930 spin_unlock_irq(sch->lock);
933 static int ccw_device_move_to_sch(struct ccw_device *cdev,
934 struct subchannel *sch)
936 struct subchannel *old_sch;
937 int rc, old_enabled = 0;
939 old_sch = to_subchannel(cdev->dev.parent);
940 /* Obtain child reference for new parent. */
941 if (!get_device(&sch->dev))
942 return -ENODEV;
944 if (!sch_is_pseudo_sch(old_sch)) {
945 spin_lock_irq(old_sch->lock);
946 old_enabled = old_sch->schib.pmcw.ena;
947 rc = 0;
948 if (old_enabled)
949 rc = cio_disable_subchannel(old_sch);
950 spin_unlock_irq(old_sch->lock);
951 if (rc == -EBUSY) {
952 /* Release child reference for new parent. */
953 put_device(&sch->dev);
954 return rc;
958 mutex_lock(&sch->reg_mutex);
959 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
960 mutex_unlock(&sch->reg_mutex);
961 if (rc) {
962 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
963 cdev->private->dev_id.ssid,
964 cdev->private->dev_id.devno, sch->schid.ssid,
965 sch->schib.pmcw.dev, rc);
966 if (old_enabled) {
967 /* Try to reenable the old subchannel. */
968 spin_lock_irq(old_sch->lock);
969 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
970 spin_unlock_irq(old_sch->lock);
972 /* Release child reference for new parent. */
973 put_device(&sch->dev);
974 return rc;
976 /* Clean up old subchannel. */
977 if (!sch_is_pseudo_sch(old_sch)) {
978 spin_lock_irq(old_sch->lock);
979 sch_set_cdev(old_sch, NULL);
980 spin_unlock_irq(old_sch->lock);
981 css_schedule_eval(old_sch->schid);
983 /* Release child reference for old parent. */
984 put_device(&old_sch->dev);
985 /* Initialize new subchannel. */
986 spin_lock_irq(sch->lock);
987 cdev->private->schid = sch->schid;
988 cdev->ccwlock = sch->lock;
989 if (!sch_is_pseudo_sch(sch))
990 sch_set_cdev(sch, cdev);
991 spin_unlock_irq(sch->lock);
992 if (!sch_is_pseudo_sch(sch))
993 css_update_ssd_info(sch);
994 return 0;
997 static int ccw_device_move_to_orph(struct ccw_device *cdev)
999 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1000 struct channel_subsystem *css = to_css(sch->dev.parent);
1002 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1005 static void io_subchannel_irq(struct subchannel *sch)
1007 struct ccw_device *cdev;
1009 cdev = sch_get_cdev(sch);
1011 CIO_TRACE_EVENT(6, "IRQ");
1012 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1013 if (cdev)
1014 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1015 else
1016 kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
1019 void io_subchannel_init_config(struct subchannel *sch)
1021 memset(&sch->config, 0, sizeof(sch->config));
1022 sch->config.csense = 1;
1025 static void io_subchannel_init_fields(struct subchannel *sch)
1027 if (cio_is_console(sch->schid))
1028 sch->opm = 0xff;
1029 else
1030 sch->opm = chp_get_sch_opm(sch);
1031 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1032 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1034 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1035 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1036 sch->schib.pmcw.dev, sch->schid.ssid,
1037 sch->schid.sch_no, sch->schib.pmcw.pim,
1038 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1040 io_subchannel_init_config(sch);
1044 * Note: We always return 0 so that we bind to the device even on error.
1045 * This is needed so that our remove function is called on unregister.
1047 static int io_subchannel_probe(struct subchannel *sch)
1049 struct io_subchannel_private *io_priv;
1050 struct ccw_device *cdev;
1051 int rc;
1053 if (cio_is_console(sch->schid)) {
1054 rc = sysfs_create_group(&sch->dev.kobj,
1055 &io_subchannel_attr_group);
1056 if (rc)
1057 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1058 "attributes for subchannel "
1059 "0.%x.%04x (rc=%d)\n",
1060 sch->schid.ssid, sch->schid.sch_no, rc);
1062 * The console subchannel already has an associated ccw_device.
1063 * Throw the delayed uevent for the subchannel, register
1064 * the ccw_device and exit.
1066 dev_set_uevent_suppress(&sch->dev, 0);
1067 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1068 cdev = sch_get_cdev(sch);
1069 cdev->dev.groups = ccwdev_attr_groups;
1070 device_initialize(&cdev->dev);
1071 cdev->private->flags.initialized = 1;
1072 ccw_device_register(cdev);
1074 * Check if the device is already online. If it is
1075 * the reference count needs to be corrected since we
1076 * didn't obtain a reference in ccw_device_set_online.
1078 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1079 cdev->private->state != DEV_STATE_OFFLINE &&
1080 cdev->private->state != DEV_STATE_BOXED)
1081 get_device(&cdev->dev);
1082 return 0;
1084 io_subchannel_init_fields(sch);
1085 rc = cio_commit_config(sch);
1086 if (rc)
1087 goto out_schedule;
1088 rc = sysfs_create_group(&sch->dev.kobj,
1089 &io_subchannel_attr_group);
1090 if (rc)
1091 goto out_schedule;
1092 /* Allocate I/O subchannel private data. */
1093 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1094 if (!io_priv)
1095 goto out_schedule;
1097 set_io_private(sch, io_priv);
1098 css_schedule_eval(sch->schid);
1099 return 0;
1101 out_schedule:
1102 spin_lock_irq(sch->lock);
1103 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1104 spin_unlock_irq(sch->lock);
1105 return 0;
1108 static int
1109 io_subchannel_remove (struct subchannel *sch)
1111 struct io_subchannel_private *io_priv = to_io_private(sch);
1112 struct ccw_device *cdev;
1114 cdev = sch_get_cdev(sch);
1115 if (!cdev)
1116 goto out_free;
1117 io_subchannel_quiesce(sch);
1118 /* Set ccw device to not operational and drop reference. */
1119 spin_lock_irq(cdev->ccwlock);
1120 sch_set_cdev(sch, NULL);
1121 set_io_private(sch, NULL);
1122 cdev->private->state = DEV_STATE_NOT_OPER;
1123 spin_unlock_irq(cdev->ccwlock);
1124 ccw_device_unregister(cdev);
1125 out_free:
1126 kfree(io_priv);
1127 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1128 return 0;
1131 static void io_subchannel_verify(struct subchannel *sch)
1133 struct ccw_device *cdev;
1135 cdev = sch_get_cdev(sch);
1136 if (cdev)
1137 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1140 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1142 struct ccw_device *cdev;
1144 cdev = sch_get_cdev(sch);
1145 if (!cdev)
1146 return;
1147 if (cio_update_schib(sch))
1148 goto err;
1149 /* Check for I/O on path. */
1150 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1151 goto out;
1152 if (cdev->private->state == DEV_STATE_ONLINE) {
1153 ccw_device_kill_io(cdev);
1154 goto out;
1156 if (cio_clear(sch))
1157 goto err;
1158 out:
1159 /* Trigger path verification. */
1160 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1161 return;
1163 err:
1164 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1167 static int io_subchannel_chp_event(struct subchannel *sch,
1168 struct chp_link *link, int event)
1170 struct ccw_device *cdev = sch_get_cdev(sch);
1171 int mask;
1173 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1174 if (!mask)
1175 return 0;
1176 switch (event) {
1177 case CHP_VARY_OFF:
1178 sch->opm &= ~mask;
1179 sch->lpm &= ~mask;
1180 if (cdev)
1181 cdev->private->path_gone_mask |= mask;
1182 io_subchannel_terminate_path(sch, mask);
1183 break;
1184 case CHP_VARY_ON:
1185 sch->opm |= mask;
1186 sch->lpm |= mask;
1187 if (cdev)
1188 cdev->private->path_new_mask |= mask;
1189 io_subchannel_verify(sch);
1190 break;
1191 case CHP_OFFLINE:
1192 if (cio_update_schib(sch))
1193 return -ENODEV;
1194 if (cdev)
1195 cdev->private->path_gone_mask |= mask;
1196 io_subchannel_terminate_path(sch, mask);
1197 break;
1198 case CHP_ONLINE:
1199 if (cio_update_schib(sch))
1200 return -ENODEV;
1201 sch->lpm |= mask & sch->opm;
1202 if (cdev)
1203 cdev->private->path_new_mask |= mask;
1204 io_subchannel_verify(sch);
1205 break;
1207 return 0;
1210 static void io_subchannel_quiesce(struct subchannel *sch)
1212 struct ccw_device *cdev;
1213 int ret;
1215 spin_lock_irq(sch->lock);
1216 cdev = sch_get_cdev(sch);
1217 if (cio_is_console(sch->schid))
1218 goto out_unlock;
1219 if (!sch->schib.pmcw.ena)
1220 goto out_unlock;
1221 ret = cio_disable_subchannel(sch);
1222 if (ret != -EBUSY)
1223 goto out_unlock;
1224 if (cdev->handler)
1225 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1226 while (ret == -EBUSY) {
1227 cdev->private->state = DEV_STATE_QUIESCE;
1228 cdev->private->iretry = 255;
1229 ret = ccw_device_cancel_halt_clear(cdev);
1230 if (ret == -EBUSY) {
1231 ccw_device_set_timeout(cdev, HZ/10);
1232 spin_unlock_irq(sch->lock);
1233 wait_event(cdev->private->wait_q,
1234 cdev->private->state != DEV_STATE_QUIESCE);
1235 spin_lock_irq(sch->lock);
1237 ret = cio_disable_subchannel(sch);
1239 out_unlock:
1240 spin_unlock_irq(sch->lock);
1243 static void io_subchannel_shutdown(struct subchannel *sch)
1245 io_subchannel_quiesce(sch);
1248 static int device_is_disconnected(struct ccw_device *cdev)
1250 if (!cdev)
1251 return 0;
1252 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1253 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1256 static int recovery_check(struct device *dev, void *data)
1258 struct ccw_device *cdev = to_ccwdev(dev);
1259 int *redo = data;
1261 spin_lock_irq(cdev->ccwlock);
1262 switch (cdev->private->state) {
1263 case DEV_STATE_DISCONNECTED:
1264 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1265 cdev->private->dev_id.ssid,
1266 cdev->private->dev_id.devno);
1267 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1268 *redo = 1;
1269 break;
1270 case DEV_STATE_DISCONNECTED_SENSE_ID:
1271 *redo = 1;
1272 break;
1274 spin_unlock_irq(cdev->ccwlock);
1276 return 0;
1279 static void recovery_work_func(struct work_struct *unused)
1281 int redo = 0;
1283 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1284 if (redo) {
1285 spin_lock_irq(&recovery_lock);
1286 if (!timer_pending(&recovery_timer)) {
1287 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1288 recovery_phase++;
1289 mod_timer(&recovery_timer, jiffies +
1290 recovery_delay[recovery_phase] * HZ);
1292 spin_unlock_irq(&recovery_lock);
1293 } else
1294 CIO_MSG_EVENT(4, "recovery: end\n");
1297 static DECLARE_WORK(recovery_work, recovery_work_func);
1299 static void recovery_func(unsigned long data)
1302 * We can't do our recovery in softirq context and it's not
1303 * performance critical, so we schedule it.
1305 schedule_work(&recovery_work);
1308 static void ccw_device_schedule_recovery(void)
1310 unsigned long flags;
1312 CIO_MSG_EVENT(4, "recovery: schedule\n");
1313 spin_lock_irqsave(&recovery_lock, flags);
1314 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1315 recovery_phase = 0;
1316 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1318 spin_unlock_irqrestore(&recovery_lock, flags);
1321 static int purge_fn(struct device *dev, void *data)
1323 struct ccw_device *cdev = to_ccwdev(dev);
1324 struct ccw_dev_id *id = &cdev->private->dev_id;
1326 spin_lock_irq(cdev->ccwlock);
1327 if (is_blacklisted(id->ssid, id->devno) &&
1328 (cdev->private->state == DEV_STATE_OFFLINE) &&
1329 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1330 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1331 id->devno);
1332 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1333 atomic_set(&cdev->private->onoff, 0);
1335 spin_unlock_irq(cdev->ccwlock);
1336 /* Abort loop in case of pending signal. */
1337 if (signal_pending(current))
1338 return -EINTR;
1340 return 0;
1344 * ccw_purge_blacklisted - purge unused, blacklisted devices
1346 * Unregister all ccw devices that are offline and on the blacklist.
1348 int ccw_purge_blacklisted(void)
1350 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1351 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1352 return 0;
1355 void ccw_device_set_disconnected(struct ccw_device *cdev)
1357 if (!cdev)
1358 return;
1359 ccw_device_set_timeout(cdev, 0);
1360 cdev->private->flags.fake_irb = 0;
1361 cdev->private->state = DEV_STATE_DISCONNECTED;
1362 if (cdev->online)
1363 ccw_device_schedule_recovery();
1366 void ccw_device_set_notoper(struct ccw_device *cdev)
1368 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1370 CIO_TRACE_EVENT(2, "notoper");
1371 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1372 ccw_device_set_timeout(cdev, 0);
1373 cio_disable_subchannel(sch);
1374 cdev->private->state = DEV_STATE_NOT_OPER;
1377 enum io_sch_action {
1378 IO_SCH_UNREG,
1379 IO_SCH_ORPH_UNREG,
1380 IO_SCH_ATTACH,
1381 IO_SCH_UNREG_ATTACH,
1382 IO_SCH_ORPH_ATTACH,
1383 IO_SCH_REPROBE,
1384 IO_SCH_VERIFY,
1385 IO_SCH_DISC,
1386 IO_SCH_NOP,
1389 static enum io_sch_action sch_get_action(struct subchannel *sch)
1391 struct ccw_device *cdev;
1393 cdev = sch_get_cdev(sch);
1394 if (cio_update_schib(sch)) {
1395 /* Not operational. */
1396 if (!cdev)
1397 return IO_SCH_UNREG;
1398 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1399 return IO_SCH_UNREG;
1400 return IO_SCH_ORPH_UNREG;
1402 /* Operational. */
1403 if (!cdev)
1404 return IO_SCH_ATTACH;
1405 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1406 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1407 return IO_SCH_UNREG_ATTACH;
1408 return IO_SCH_ORPH_ATTACH;
1410 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1411 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1412 return IO_SCH_UNREG;
1413 return IO_SCH_DISC;
1415 if (device_is_disconnected(cdev))
1416 return IO_SCH_REPROBE;
1417 if (cdev->online)
1418 return IO_SCH_VERIFY;
1419 return IO_SCH_NOP;
1423 * io_subchannel_sch_event - process subchannel event
1424 * @sch: subchannel
1425 * @process: non-zero if function is called in process context
1427 * An unspecified event occurred for this subchannel. Adjust data according
1428 * to the current operational state of the subchannel and device. Return
1429 * zero when the event has been handled sufficiently or -EAGAIN when this
1430 * function should be called again in process context.
1432 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1434 unsigned long flags;
1435 struct ccw_device *cdev;
1436 struct ccw_dev_id dev_id;
1437 enum io_sch_action action;
1438 int rc = -EAGAIN;
1440 spin_lock_irqsave(sch->lock, flags);
1441 if (!device_is_registered(&sch->dev))
1442 goto out_unlock;
1443 if (work_pending(&sch->todo_work))
1444 goto out_unlock;
1445 cdev = sch_get_cdev(sch);
1446 if (cdev && work_pending(&cdev->private->todo_work))
1447 goto out_unlock;
1448 action = sch_get_action(sch);
1449 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1450 sch->schid.ssid, sch->schid.sch_no, process,
1451 action);
1452 /* Perform immediate actions while holding the lock. */
1453 switch (action) {
1454 case IO_SCH_REPROBE:
1455 /* Trigger device recognition. */
1456 ccw_device_trigger_reprobe(cdev);
1457 rc = 0;
1458 goto out_unlock;
1459 case IO_SCH_VERIFY:
1460 if (cdev->private->flags.resuming == 1) {
1461 if (cio_enable_subchannel(sch, (u32)(addr_t)sch)) {
1462 ccw_device_set_notoper(cdev);
1463 break;
1466 /* Trigger path verification. */
1467 io_subchannel_verify(sch);
1468 rc = 0;
1469 goto out_unlock;
1470 case IO_SCH_DISC:
1471 ccw_device_set_disconnected(cdev);
1472 rc = 0;
1473 goto out_unlock;
1474 case IO_SCH_ORPH_UNREG:
1475 case IO_SCH_ORPH_ATTACH:
1476 ccw_device_set_disconnected(cdev);
1477 break;
1478 case IO_SCH_UNREG_ATTACH:
1479 case IO_SCH_UNREG:
1480 if (!cdev)
1481 break;
1482 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1484 * Note: delayed work triggered by this event
1485 * and repeated calls to sch_event are synchronized
1486 * by the above check for work_pending(cdev).
1488 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1489 } else
1490 ccw_device_set_notoper(cdev);
1491 break;
1492 case IO_SCH_NOP:
1493 rc = 0;
1494 goto out_unlock;
1495 default:
1496 break;
1498 spin_unlock_irqrestore(sch->lock, flags);
1499 /* All other actions require process context. */
1500 if (!process)
1501 goto out;
1502 /* Handle attached ccw device. */
1503 switch (action) {
1504 case IO_SCH_ORPH_UNREG:
1505 case IO_SCH_ORPH_ATTACH:
1506 /* Move ccw device to orphanage. */
1507 rc = ccw_device_move_to_orph(cdev);
1508 if (rc)
1509 goto out;
1510 break;
1511 case IO_SCH_UNREG_ATTACH:
1512 if (cdev->private->flags.resuming) {
1513 /* Device will be handled later. */
1514 rc = 0;
1515 goto out;
1517 /* Unregister ccw device. */
1518 ccw_device_unregister(cdev);
1519 break;
1520 default:
1521 break;
1523 /* Handle subchannel. */
1524 switch (action) {
1525 case IO_SCH_ORPH_UNREG:
1526 case IO_SCH_UNREG:
1527 if (!cdev || !cdev->private->flags.resuming)
1528 css_sch_device_unregister(sch);
1529 break;
1530 case IO_SCH_ORPH_ATTACH:
1531 case IO_SCH_UNREG_ATTACH:
1532 case IO_SCH_ATTACH:
1533 dev_id.ssid = sch->schid.ssid;
1534 dev_id.devno = sch->schib.pmcw.dev;
1535 cdev = get_ccwdev_by_dev_id(&dev_id);
1536 if (!cdev) {
1537 sch_create_and_recog_new_device(sch);
1538 break;
1540 rc = ccw_device_move_to_sch(cdev, sch);
1541 if (rc) {
1542 /* Release reference from get_ccwdev_by_dev_id() */
1543 put_device(&cdev->dev);
1544 goto out;
1546 spin_lock_irqsave(sch->lock, flags);
1547 ccw_device_trigger_reprobe(cdev);
1548 spin_unlock_irqrestore(sch->lock, flags);
1549 /* Release reference from get_ccwdev_by_dev_id() */
1550 put_device(&cdev->dev);
1551 break;
1552 default:
1553 break;
1555 return 0;
1557 out_unlock:
1558 spin_unlock_irqrestore(sch->lock, flags);
1559 out:
1560 return rc;
1563 #ifdef CONFIG_CCW_CONSOLE
1564 static struct ccw_device console_cdev;
1565 static struct ccw_device_private console_private;
1566 static int console_cdev_in_use;
1568 static DEFINE_SPINLOCK(ccw_console_lock);
1570 spinlock_t * cio_get_console_lock(void)
1572 return &ccw_console_lock;
1575 static int ccw_device_console_enable(struct ccw_device *cdev,
1576 struct subchannel *sch)
1578 struct io_subchannel_private *io_priv = cio_get_console_priv();
1579 int rc;
1581 /* Attach subchannel private data. */
1582 memset(io_priv, 0, sizeof(*io_priv));
1583 set_io_private(sch, io_priv);
1584 io_subchannel_init_fields(sch);
1585 rc = cio_commit_config(sch);
1586 if (rc)
1587 return rc;
1588 sch->driver = &io_subchannel_driver;
1589 /* Initialize the ccw_device structure. */
1590 cdev->dev.parent= &sch->dev;
1591 sch_set_cdev(sch, cdev);
1592 io_subchannel_recog(cdev, sch);
1593 /* Now wait for the async. recognition to come to an end. */
1594 spin_lock_irq(cdev->ccwlock);
1595 while (!dev_fsm_final_state(cdev))
1596 wait_cons_dev();
1597 rc = -EIO;
1598 if (cdev->private->state != DEV_STATE_OFFLINE)
1599 goto out_unlock;
1600 ccw_device_online(cdev);
1601 while (!dev_fsm_final_state(cdev))
1602 wait_cons_dev();
1603 if (cdev->private->state != DEV_STATE_ONLINE)
1604 goto out_unlock;
1605 rc = 0;
1606 out_unlock:
1607 spin_unlock_irq(cdev->ccwlock);
1608 return rc;
1611 struct ccw_device *
1612 ccw_device_probe_console(void)
1614 struct subchannel *sch;
1615 int ret;
1617 if (xchg(&console_cdev_in_use, 1) != 0)
1618 return ERR_PTR(-EBUSY);
1619 sch = cio_probe_console();
1620 if (IS_ERR(sch)) {
1621 console_cdev_in_use = 0;
1622 return (void *) sch;
1624 memset(&console_cdev, 0, sizeof(struct ccw_device));
1625 memset(&console_private, 0, sizeof(struct ccw_device_private));
1626 console_cdev.private = &console_private;
1627 console_private.cdev = &console_cdev;
1628 console_private.int_class = IOINT_CIO;
1629 ret = ccw_device_console_enable(&console_cdev, sch);
1630 if (ret) {
1631 cio_release_console();
1632 console_cdev_in_use = 0;
1633 return ERR_PTR(ret);
1635 console_cdev.online = 1;
1636 return &console_cdev;
1639 static int ccw_device_pm_restore(struct device *dev);
1641 int ccw_device_force_console(void)
1643 if (!console_cdev_in_use)
1644 return -ENODEV;
1645 return ccw_device_pm_restore(&console_cdev.dev);
1647 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1648 #endif
1651 * get ccw_device matching the busid, but only if owned by cdrv
1653 static int
1654 __ccwdev_check_busid(struct device *dev, void *id)
1656 char *bus_id;
1658 bus_id = id;
1660 return (strcmp(bus_id, dev_name(dev)) == 0);
1665 * get_ccwdev_by_busid() - obtain device from a bus id
1666 * @cdrv: driver the device is owned by
1667 * @bus_id: bus id of the device to be searched
1669 * This function searches all devices owned by @cdrv for a device with a bus
1670 * id matching @bus_id.
1671 * Returns:
1672 * If a match is found, its reference count of the found device is increased
1673 * and it is returned; else %NULL is returned.
1675 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1676 const char *bus_id)
1678 struct device *dev;
1679 struct device_driver *drv;
1681 drv = get_driver(&cdrv->driver);
1682 if (!drv)
1683 return NULL;
1685 dev = driver_find_device(drv, NULL, (void *)bus_id,
1686 __ccwdev_check_busid);
1687 put_driver(drv);
1689 return dev ? to_ccwdev(dev) : NULL;
1692 /************************** device driver handling ************************/
1694 /* This is the implementation of the ccw_driver class. The probe, remove
1695 * and release methods are initially very similar to the device_driver
1696 * implementations, with the difference that they have ccw_device
1697 * arguments.
1699 * A ccw driver also contains the information that is needed for
1700 * device matching.
1702 static int
1703 ccw_device_probe (struct device *dev)
1705 struct ccw_device *cdev = to_ccwdev(dev);
1706 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1707 int ret;
1709 cdev->drv = cdrv; /* to let the driver call _set_online */
1710 /* Note: we interpret class 0 in this context as an uninitialized
1711 * field since it translates to a non-I/O interrupt class. */
1712 if (cdrv->int_class != 0)
1713 cdev->private->int_class = cdrv->int_class;
1714 else
1715 cdev->private->int_class = IOINT_CIO;
1717 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1719 if (ret) {
1720 cdev->drv = NULL;
1721 cdev->private->int_class = IOINT_CIO;
1722 return ret;
1725 return 0;
1728 static int
1729 ccw_device_remove (struct device *dev)
1731 struct ccw_device *cdev = to_ccwdev(dev);
1732 struct ccw_driver *cdrv = cdev->drv;
1733 int ret;
1735 if (cdrv->remove)
1736 cdrv->remove(cdev);
1737 if (cdev->online) {
1738 cdev->online = 0;
1739 spin_lock_irq(cdev->ccwlock);
1740 ret = ccw_device_offline(cdev);
1741 spin_unlock_irq(cdev->ccwlock);
1742 if (ret == 0)
1743 wait_event(cdev->private->wait_q,
1744 dev_fsm_final_state(cdev));
1745 else
1746 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1747 "device 0.%x.%04x\n",
1748 ret, cdev->private->dev_id.ssid,
1749 cdev->private->dev_id.devno);
1750 /* Give up reference obtained in ccw_device_set_online(). */
1751 put_device(&cdev->dev);
1753 ccw_device_set_timeout(cdev, 0);
1754 cdev->drv = NULL;
1755 cdev->private->int_class = IOINT_CIO;
1756 return 0;
1759 static void ccw_device_shutdown(struct device *dev)
1761 struct ccw_device *cdev;
1763 cdev = to_ccwdev(dev);
1764 if (cdev->drv && cdev->drv->shutdown)
1765 cdev->drv->shutdown(cdev);
1766 disable_cmf(cdev);
1769 static int ccw_device_pm_prepare(struct device *dev)
1771 struct ccw_device *cdev = to_ccwdev(dev);
1773 if (work_pending(&cdev->private->todo_work))
1774 return -EAGAIN;
1775 /* Fail while device is being set online/offline. */
1776 if (atomic_read(&cdev->private->onoff))
1777 return -EAGAIN;
1779 if (cdev->online && cdev->drv && cdev->drv->prepare)
1780 return cdev->drv->prepare(cdev);
1782 return 0;
1785 static void ccw_device_pm_complete(struct device *dev)
1787 struct ccw_device *cdev = to_ccwdev(dev);
1789 if (cdev->online && cdev->drv && cdev->drv->complete)
1790 cdev->drv->complete(cdev);
1793 static int ccw_device_pm_freeze(struct device *dev)
1795 struct ccw_device *cdev = to_ccwdev(dev);
1796 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1797 int ret, cm_enabled;
1799 /* Fail suspend while device is in transistional state. */
1800 if (!dev_fsm_final_state(cdev))
1801 return -EAGAIN;
1802 if (!cdev->online)
1803 return 0;
1804 if (cdev->drv && cdev->drv->freeze) {
1805 ret = cdev->drv->freeze(cdev);
1806 if (ret)
1807 return ret;
1810 spin_lock_irq(sch->lock);
1811 cm_enabled = cdev->private->cmb != NULL;
1812 spin_unlock_irq(sch->lock);
1813 if (cm_enabled) {
1814 /* Don't have the css write on memory. */
1815 ret = ccw_set_cmf(cdev, 0);
1816 if (ret)
1817 return ret;
1819 /* From here on, disallow device driver I/O. */
1820 spin_lock_irq(sch->lock);
1821 ret = cio_disable_subchannel(sch);
1822 spin_unlock_irq(sch->lock);
1824 return ret;
1827 static int ccw_device_pm_thaw(struct device *dev)
1829 struct ccw_device *cdev = to_ccwdev(dev);
1830 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1831 int ret, cm_enabled;
1833 if (!cdev->online)
1834 return 0;
1836 spin_lock_irq(sch->lock);
1837 /* Allow device driver I/O again. */
1838 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1839 cm_enabled = cdev->private->cmb != NULL;
1840 spin_unlock_irq(sch->lock);
1841 if (ret)
1842 return ret;
1844 if (cm_enabled) {
1845 ret = ccw_set_cmf(cdev, 1);
1846 if (ret)
1847 return ret;
1850 if (cdev->drv && cdev->drv->thaw)
1851 ret = cdev->drv->thaw(cdev);
1853 return ret;
1856 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1858 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1860 spin_lock_irq(sch->lock);
1861 if (cio_is_console(sch->schid)) {
1862 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1863 goto out_unlock;
1866 * While we were sleeping, devices may have gone or become
1867 * available again. Kick re-detection.
1869 cdev->private->flags.resuming = 1;
1870 cdev->private->path_new_mask = LPM_ANYPATH;
1871 css_schedule_eval(sch->schid);
1872 spin_unlock_irq(sch->lock);
1873 css_complete_work();
1875 /* cdev may have been moved to a different subchannel. */
1876 sch = to_subchannel(cdev->dev.parent);
1877 spin_lock_irq(sch->lock);
1878 if (cdev->private->state != DEV_STATE_ONLINE &&
1879 cdev->private->state != DEV_STATE_OFFLINE)
1880 goto out_unlock;
1882 ccw_device_recognition(cdev);
1883 spin_unlock_irq(sch->lock);
1884 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1885 cdev->private->state == DEV_STATE_DISCONNECTED);
1886 spin_lock_irq(sch->lock);
1888 out_unlock:
1889 cdev->private->flags.resuming = 0;
1890 spin_unlock_irq(sch->lock);
1893 static int resume_handle_boxed(struct ccw_device *cdev)
1895 cdev->private->state = DEV_STATE_BOXED;
1896 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1897 return 0;
1898 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1899 return -ENODEV;
1902 static int resume_handle_disc(struct ccw_device *cdev)
1904 cdev->private->state = DEV_STATE_DISCONNECTED;
1905 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1906 return 0;
1907 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1908 return -ENODEV;
1911 static int ccw_device_pm_restore(struct device *dev)
1913 struct ccw_device *cdev = to_ccwdev(dev);
1914 struct subchannel *sch;
1915 int ret = 0;
1917 __ccw_device_pm_restore(cdev);
1918 sch = to_subchannel(cdev->dev.parent);
1919 spin_lock_irq(sch->lock);
1920 if (cio_is_console(sch->schid))
1921 goto out_restore;
1923 /* check recognition results */
1924 switch (cdev->private->state) {
1925 case DEV_STATE_OFFLINE:
1926 case DEV_STATE_ONLINE:
1927 cdev->private->flags.donotify = 0;
1928 break;
1929 case DEV_STATE_BOXED:
1930 ret = resume_handle_boxed(cdev);
1931 if (ret)
1932 goto out_unlock;
1933 goto out_restore;
1934 default:
1935 ret = resume_handle_disc(cdev);
1936 if (ret)
1937 goto out_unlock;
1938 goto out_restore;
1940 /* check if the device type has changed */
1941 if (!ccw_device_test_sense_data(cdev)) {
1942 ccw_device_update_sense_data(cdev);
1943 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1944 ret = -ENODEV;
1945 goto out_unlock;
1947 if (!cdev->online)
1948 goto out_unlock;
1950 if (ccw_device_online(cdev)) {
1951 ret = resume_handle_disc(cdev);
1952 if (ret)
1953 goto out_unlock;
1954 goto out_restore;
1956 spin_unlock_irq(sch->lock);
1957 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1958 spin_lock_irq(sch->lock);
1960 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1961 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1962 ret = -ENODEV;
1963 goto out_unlock;
1966 /* reenable cmf, if needed */
1967 if (cdev->private->cmb) {
1968 spin_unlock_irq(sch->lock);
1969 ret = ccw_set_cmf(cdev, 1);
1970 spin_lock_irq(sch->lock);
1971 if (ret) {
1972 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1973 "(rc=%d)\n", cdev->private->dev_id.ssid,
1974 cdev->private->dev_id.devno, ret);
1975 ret = 0;
1979 out_restore:
1980 spin_unlock_irq(sch->lock);
1981 if (cdev->online && cdev->drv && cdev->drv->restore)
1982 ret = cdev->drv->restore(cdev);
1983 return ret;
1985 out_unlock:
1986 spin_unlock_irq(sch->lock);
1987 return ret;
1990 static const struct dev_pm_ops ccw_pm_ops = {
1991 .prepare = ccw_device_pm_prepare,
1992 .complete = ccw_device_pm_complete,
1993 .freeze = ccw_device_pm_freeze,
1994 .thaw = ccw_device_pm_thaw,
1995 .restore = ccw_device_pm_restore,
1998 static struct bus_type ccw_bus_type = {
1999 .name = "ccw",
2000 .match = ccw_bus_match,
2001 .uevent = ccw_uevent,
2002 .probe = ccw_device_probe,
2003 .remove = ccw_device_remove,
2004 .shutdown = ccw_device_shutdown,
2005 .pm = &ccw_pm_ops,
2009 * ccw_driver_register() - register a ccw driver
2010 * @cdriver: driver to be registered
2012 * This function is mainly a wrapper around driver_register().
2013 * Returns:
2014 * %0 on success and a negative error value on failure.
2016 int ccw_driver_register(struct ccw_driver *cdriver)
2018 struct device_driver *drv = &cdriver->driver;
2020 drv->bus = &ccw_bus_type;
2022 return driver_register(drv);
2026 * ccw_driver_unregister() - deregister a ccw driver
2027 * @cdriver: driver to be deregistered
2029 * This function is mainly a wrapper around driver_unregister().
2031 void ccw_driver_unregister(struct ccw_driver *cdriver)
2033 driver_unregister(&cdriver->driver);
2036 /* Helper func for qdio. */
2037 struct subchannel_id
2038 ccw_device_get_subchannel_id(struct ccw_device *cdev)
2040 struct subchannel *sch;
2042 sch = to_subchannel(cdev->dev.parent);
2043 return sch->schid;
2046 static void ccw_device_todo(struct work_struct *work)
2048 struct ccw_device_private *priv;
2049 struct ccw_device *cdev;
2050 struct subchannel *sch;
2051 enum cdev_todo todo;
2053 priv = container_of(work, struct ccw_device_private, todo_work);
2054 cdev = priv->cdev;
2055 sch = to_subchannel(cdev->dev.parent);
2056 /* Find out todo. */
2057 spin_lock_irq(cdev->ccwlock);
2058 todo = priv->todo;
2059 priv->todo = CDEV_TODO_NOTHING;
2060 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2061 priv->dev_id.ssid, priv->dev_id.devno, todo);
2062 spin_unlock_irq(cdev->ccwlock);
2063 /* Perform todo. */
2064 switch (todo) {
2065 case CDEV_TODO_ENABLE_CMF:
2066 cmf_reenable(cdev);
2067 break;
2068 case CDEV_TODO_REBIND:
2069 ccw_device_do_unbind_bind(cdev);
2070 break;
2071 case CDEV_TODO_REGISTER:
2072 io_subchannel_register(cdev);
2073 break;
2074 case CDEV_TODO_UNREG_EVAL:
2075 if (!sch_is_pseudo_sch(sch))
2076 css_schedule_eval(sch->schid);
2077 /* fall-through */
2078 case CDEV_TODO_UNREG:
2079 if (sch_is_pseudo_sch(sch))
2080 ccw_device_unregister(cdev);
2081 else
2082 ccw_device_call_sch_unregister(cdev);
2083 break;
2084 default:
2085 break;
2087 /* Release workqueue ref. */
2088 put_device(&cdev->dev);
2092 * ccw_device_sched_todo - schedule ccw device operation
2093 * @cdev: ccw device
2094 * @todo: todo
2096 * Schedule the operation identified by @todo to be performed on the slow path
2097 * workqueue. Do nothing if another operation with higher priority is already
2098 * scheduled. Needs to be called with ccwdev lock held.
2100 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2102 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2103 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2104 todo);
2105 if (cdev->private->todo >= todo)
2106 return;
2107 cdev->private->todo = todo;
2108 /* Get workqueue ref. */
2109 if (!get_device(&cdev->dev))
2110 return;
2111 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2112 /* Already queued, release workqueue ref. */
2113 put_device(&cdev->dev);
2118 * ccw_device_siosl() - initiate logging
2119 * @cdev: ccw device
2121 * This function is used to invoke model-dependent logging within the channel
2122 * subsystem.
2124 int ccw_device_siosl(struct ccw_device *cdev)
2126 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2128 return chsc_siosl(sch->schid);
2130 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2132 MODULE_LICENSE("GPL");
2133 EXPORT_SYMBOL(ccw_device_set_online);
2134 EXPORT_SYMBOL(ccw_device_set_offline);
2135 EXPORT_SYMBOL(ccw_driver_register);
2136 EXPORT_SYMBOL(ccw_driver_unregister);
2137 EXPORT_SYMBOL(get_ccwdev_by_busid);
2138 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);