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