1
2
3
4
5
6
7
8
9
10
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14#include <linux/export.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/delay.h>
24#include <linux/timer.h>
25#include <linux/kernel_stat.h>
26#include <linux/sched/signal.h>
27#include <linux/dma-mapping.h>
28
29#include <asm/ccwdev.h>
30#include <asm/cio.h>
31#include <asm/param.h>
32#include <asm/cmb.h>
33#include <asm/isc.h>
34
35#include "chp.h"
36#include "cio.h"
37#include "cio_debug.h"
38#include "css.h"
39#include "device.h"
40#include "ioasm.h"
41#include "io_sch.h"
42#include "blacklist.h"
43#include "chsc.h"
44
45static struct timer_list recovery_timer;
46static DEFINE_SPINLOCK(recovery_lock);
47static int recovery_phase;
48static const unsigned long recovery_delay[] = { 3, 30, 300 };
49
50static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
51static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
52static struct bus_type ccw_bus_type;
53
54
55
56
57
58
59
60static int
61ccw_bus_match (struct device * dev, struct device_driver * drv)
62{
63 struct ccw_device *cdev = to_ccwdev(dev);
64 struct ccw_driver *cdrv = to_ccwdrv(drv);
65 const struct ccw_device_id *ids = cdrv->ids, *found;
66
67 if (!ids)
68 return 0;
69
70 found = ccw_device_id_match(ids, &cdev->id);
71 if (!found)
72 return 0;
73
74 cdev->id.driver_info = found->driver_info;
75
76 return 1;
77}
78
79
80
81
82static int snprint_alias(char *buf, size_t size,
83 struct ccw_device_id *id, const char *suffix)
84{
85 int len;
86
87 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
88 if (len > size)
89 return len;
90 buf += len;
91 size -= len;
92
93 if (id->dev_type != 0)
94 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
95 id->dev_model, suffix);
96 else
97 len += snprintf(buf, size, "dtdm%s", suffix);
98
99 return len;
100}
101
102
103
104static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
105{
106 struct ccw_device *cdev = to_ccwdev(dev);
107 struct ccw_device_id *id = &(cdev->id);
108 int ret;
109 char modalias_buf[30];
110
111
112 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
113 if (ret)
114 return ret;
115
116
117 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
118 if (ret)
119 return ret;
120
121
122
123 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
124 if (ret)
125 return ret;
126
127
128 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
129 if (ret)
130 return ret;
131
132
133 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
134 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
135 return ret;
136}
137
138static void io_subchannel_irq(struct subchannel *);
139static int io_subchannel_probe(struct subchannel *);
140static int io_subchannel_remove(struct subchannel *);
141static void io_subchannel_shutdown(struct subchannel *);
142static int io_subchannel_sch_event(struct subchannel *, int);
143static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
144 int);
145static void recovery_func(struct timer_list *unused);
146
147static struct css_device_id io_subchannel_ids[] = {
148 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
149 { },
150};
151
152static int io_subchannel_prepare(struct subchannel *sch)
153{
154 struct ccw_device *cdev;
155
156
157
158
159 cdev = sch_get_cdev(sch);
160 if (cdev && !device_is_registered(&cdev->dev))
161 return -EAGAIN;
162 return 0;
163}
164
165static int io_subchannel_settle(void)
166{
167 int ret;
168
169 ret = wait_event_interruptible(ccw_device_init_wq,
170 atomic_read(&ccw_device_init_count) == 0);
171 if (ret)
172 return -EINTR;
173 flush_workqueue(cio_work_q);
174 return 0;
175}
176
177static struct css_driver io_subchannel_driver = {
178 .drv = {
179 .owner = THIS_MODULE,
180 .name = "io_subchannel",
181 },
182 .subchannel_type = io_subchannel_ids,
183 .irq = io_subchannel_irq,
184 .sch_event = io_subchannel_sch_event,
185 .chp_event = io_subchannel_chp_event,
186 .probe = io_subchannel_probe,
187 .remove = io_subchannel_remove,
188 .shutdown = io_subchannel_shutdown,
189 .prepare = io_subchannel_prepare,
190 .settle = io_subchannel_settle,
191};
192
193int __init io_subchannel_init(void)
194{
195 int ret;
196
197 timer_setup(&recovery_timer, recovery_func, 0);
198 ret = bus_register(&ccw_bus_type);
199 if (ret)
200 return ret;
201 ret = css_driver_register(&io_subchannel_driver);
202 if (ret)
203 bus_unregister(&ccw_bus_type);
204
205 return ret;
206}
207
208
209
210
211static ssize_t
212devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
213{
214 struct ccw_device *cdev = to_ccwdev(dev);
215 struct ccw_device_id *id = &(cdev->id);
216
217 if (id->dev_type != 0)
218 return sprintf(buf, "%04x/%02x\n",
219 id->dev_type, id->dev_model);
220 else
221 return sprintf(buf, "n/a\n");
222}
223
224static ssize_t
225cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
226{
227 struct ccw_device *cdev = to_ccwdev(dev);
228 struct ccw_device_id *id = &(cdev->id);
229
230 return sprintf(buf, "%04x/%02x\n",
231 id->cu_type, id->cu_model);
232}
233
234static ssize_t
235modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
236{
237 struct ccw_device *cdev = to_ccwdev(dev);
238 struct ccw_device_id *id = &(cdev->id);
239 int len;
240
241 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
242
243 return len > PAGE_SIZE ? PAGE_SIZE : len;
244}
245
246static ssize_t
247online_show (struct device *dev, struct device_attribute *attr, char *buf)
248{
249 struct ccw_device *cdev = to_ccwdev(dev);
250
251 return sprintf(buf, cdev->online ? "1\n" : "0\n");
252}
253
254int ccw_device_is_orphan(struct ccw_device *cdev)
255{
256 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
257}
258
259static void ccw_device_unregister(struct ccw_device *cdev)
260{
261 if (device_is_registered(&cdev->dev)) {
262
263 device_del(&cdev->dev);
264 }
265 if (cdev->private->flags.initialized) {
266 cdev->private->flags.initialized = 0;
267
268 put_device(&cdev->dev);
269 }
270}
271
272static void io_subchannel_quiesce(struct subchannel *);
273
274
275
276
277
278
279
280
281
282
283
284
285int ccw_device_set_offline(struct ccw_device *cdev)
286{
287 struct subchannel *sch;
288 int ret, state;
289
290 if (!cdev)
291 return -ENODEV;
292 if (!cdev->online || !cdev->drv)
293 return -EINVAL;
294
295 if (cdev->drv->set_offline) {
296 ret = cdev->drv->set_offline(cdev);
297 if (ret != 0)
298 return ret;
299 }
300 spin_lock_irq(cdev->ccwlock);
301 sch = to_subchannel(cdev->dev.parent);
302 cdev->online = 0;
303
304 while (!dev_fsm_final_state(cdev) &&
305 cdev->private->state != DEV_STATE_DISCONNECTED) {
306 spin_unlock_irq(cdev->ccwlock);
307 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
308 cdev->private->state == DEV_STATE_DISCONNECTED));
309 spin_lock_irq(cdev->ccwlock);
310 }
311 do {
312 ret = ccw_device_offline(cdev);
313 if (!ret)
314 break;
315 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
316 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
317 cdev->private->dev_id.devno);
318 if (ret != -EBUSY)
319 goto error;
320 state = cdev->private->state;
321 spin_unlock_irq(cdev->ccwlock);
322 io_subchannel_quiesce(sch);
323 spin_lock_irq(cdev->ccwlock);
324 cdev->private->state = state;
325 } while (ret == -EBUSY);
326 spin_unlock_irq(cdev->ccwlock);
327 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
328 cdev->private->state == DEV_STATE_DISCONNECTED));
329
330 if (cdev->private->state == DEV_STATE_BOXED) {
331 pr_warn("%s: The device entered boxed state while being set offline\n",
332 dev_name(&cdev->dev));
333 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
334 pr_warn("%s: The device stopped operating while being set offline\n",
335 dev_name(&cdev->dev));
336 }
337
338 put_device(&cdev->dev);
339 return 0;
340
341error:
342 cdev->private->state = DEV_STATE_OFFLINE;
343 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
344 spin_unlock_irq(cdev->ccwlock);
345
346 put_device(&cdev->dev);
347 return -ENODEV;
348}
349
350
351
352
353
354
355
356
357
358
359
360
361
362int ccw_device_set_online(struct ccw_device *cdev)
363{
364 int ret;
365 int ret2;
366
367 if (!cdev)
368 return -ENODEV;
369 if (cdev->online || !cdev->drv)
370 return -EINVAL;
371
372 if (!get_device(&cdev->dev))
373 return -ENODEV;
374
375 spin_lock_irq(cdev->ccwlock);
376 ret = ccw_device_online(cdev);
377 spin_unlock_irq(cdev->ccwlock);
378 if (ret == 0)
379 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
380 else {
381 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
382 "device 0.%x.%04x\n",
383 ret, cdev->private->dev_id.ssid,
384 cdev->private->dev_id.devno);
385
386 put_device(&cdev->dev);
387 return ret;
388 }
389 spin_lock_irq(cdev->ccwlock);
390
391 if ((cdev->private->state != DEV_STATE_ONLINE) &&
392 (cdev->private->state != DEV_STATE_W4SENSE)) {
393 spin_unlock_irq(cdev->ccwlock);
394
395 if (cdev->private->state == DEV_STATE_BOXED) {
396 pr_warn("%s: Setting the device online failed because it is boxed\n",
397 dev_name(&cdev->dev));
398 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
399 pr_warn("%s: Setting the device online failed because it is not operational\n",
400 dev_name(&cdev->dev));
401 }
402
403 put_device(&cdev->dev);
404 return -ENODEV;
405 }
406 spin_unlock_irq(cdev->ccwlock);
407 if (cdev->drv->set_online)
408 ret = cdev->drv->set_online(cdev);
409 if (ret)
410 goto rollback;
411
412 spin_lock_irq(cdev->ccwlock);
413 cdev->online = 1;
414 spin_unlock_irq(cdev->ccwlock);
415 return 0;
416
417rollback:
418 spin_lock_irq(cdev->ccwlock);
419
420 while (!dev_fsm_final_state(cdev) &&
421 cdev->private->state != DEV_STATE_DISCONNECTED) {
422 spin_unlock_irq(cdev->ccwlock);
423 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
424 cdev->private->state == DEV_STATE_DISCONNECTED));
425 spin_lock_irq(cdev->ccwlock);
426 }
427 ret2 = ccw_device_offline(cdev);
428 if (ret2)
429 goto error;
430 spin_unlock_irq(cdev->ccwlock);
431 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
432 cdev->private->state == DEV_STATE_DISCONNECTED));
433
434 put_device(&cdev->dev);
435 return ret;
436
437error:
438 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
439 "device 0.%x.%04x\n",
440 ret2, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
442 cdev->private->state = DEV_STATE_OFFLINE;
443 spin_unlock_irq(cdev->ccwlock);
444
445 put_device(&cdev->dev);
446 return ret;
447}
448
449static int online_store_handle_offline(struct ccw_device *cdev)
450{
451 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
452 spin_lock_irq(cdev->ccwlock);
453 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
454 spin_unlock_irq(cdev->ccwlock);
455 return 0;
456 }
457 if (cdev->drv && cdev->drv->set_offline)
458 return ccw_device_set_offline(cdev);
459 return -EINVAL;
460}
461
462static int online_store_recog_and_online(struct ccw_device *cdev)
463{
464
465 if (cdev->private->state == DEV_STATE_BOXED) {
466 spin_lock_irq(cdev->ccwlock);
467 ccw_device_recognition(cdev);
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q,
470 cdev->private->flags.recog_done);
471 if (cdev->private->state != DEV_STATE_OFFLINE)
472
473 return -EAGAIN;
474 }
475 if (cdev->drv && cdev->drv->set_online)
476 return ccw_device_set_online(cdev);
477 return -EINVAL;
478}
479
480static int online_store_handle_online(struct ccw_device *cdev, int force)
481{
482 int ret;
483
484 ret = online_store_recog_and_online(cdev);
485 if (ret && !force)
486 return ret;
487 if (force && cdev->private->state == DEV_STATE_BOXED) {
488 ret = ccw_device_stlck(cdev);
489 if (ret)
490 return ret;
491 if (cdev->id.cu_type == 0)
492 cdev->private->state = DEV_STATE_NOT_OPER;
493 ret = online_store_recog_and_online(cdev);
494 if (ret)
495 return ret;
496 }
497 return 0;
498}
499
500static ssize_t online_store (struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
502{
503 struct ccw_device *cdev = to_ccwdev(dev);
504 int force, ret;
505 unsigned long i;
506
507
508 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
509 return -EAGAIN;
510
511 if (!dev_fsm_final_state(cdev) &&
512 cdev->private->state != DEV_STATE_DISCONNECTED) {
513 ret = -EAGAIN;
514 goto out;
515 }
516
517 if (work_pending(&cdev->private->todo_work)) {
518 ret = -EAGAIN;
519 goto out;
520 }
521 if (!strncmp(buf, "force\n", count)) {
522 force = 1;
523 i = 1;
524 ret = 0;
525 } else {
526 force = 0;
527 ret = kstrtoul(buf, 16, &i);
528 }
529 if (ret)
530 goto out;
531
532 device_lock(dev);
533 switch (i) {
534 case 0:
535 ret = online_store_handle_offline(cdev);
536 break;
537 case 1:
538 ret = online_store_handle_online(cdev, force);
539 break;
540 default:
541 ret = -EINVAL;
542 }
543 device_unlock(dev);
544
545out:
546 atomic_set(&cdev->private->onoff, 0);
547 return (ret < 0) ? ret : count;
548}
549
550static ssize_t
551available_show (struct device *dev, struct device_attribute *attr, char *buf)
552{
553 struct ccw_device *cdev = to_ccwdev(dev);
554 struct subchannel *sch;
555
556 if (ccw_device_is_orphan(cdev))
557 return sprintf(buf, "no device\n");
558 switch (cdev->private->state) {
559 case DEV_STATE_BOXED:
560 return sprintf(buf, "boxed\n");
561 case DEV_STATE_DISCONNECTED:
562 case DEV_STATE_DISCONNECTED_SENSE_ID:
563 case DEV_STATE_NOT_OPER:
564 sch = to_subchannel(dev->parent);
565 if (!sch->lpm)
566 return sprintf(buf, "no path\n");
567 else
568 return sprintf(buf, "no device\n");
569 default:
570
571 return sprintf(buf, "good\n");
572 }
573}
574
575static ssize_t
576initiate_logging(struct device *dev, struct device_attribute *attr,
577 const char *buf, size_t count)
578{
579 struct subchannel *sch = to_subchannel(dev);
580 int rc;
581
582 rc = chsc_siosl(sch->schid);
583 if (rc < 0) {
584 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
585 sch->schid.ssid, sch->schid.sch_no, rc);
586 return rc;
587 }
588 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
589 sch->schid.ssid, sch->schid.sch_no);
590 return count;
591}
592
593static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
594 char *buf)
595{
596 struct subchannel *sch = to_subchannel(dev);
597
598 return sprintf(buf, "%02x\n", sch->vpm);
599}
600
601static DEVICE_ATTR_RO(devtype);
602static DEVICE_ATTR_RO(cutype);
603static DEVICE_ATTR_RO(modalias);
604static DEVICE_ATTR_RW(online);
605static DEVICE_ATTR(availability, 0444, available_show, NULL);
606static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
607static DEVICE_ATTR_RO(vpm);
608
609static struct attribute *io_subchannel_attrs[] = {
610 &dev_attr_logging.attr,
611 &dev_attr_vpm.attr,
612 NULL,
613};
614
615static const struct attribute_group io_subchannel_attr_group = {
616 .attrs = io_subchannel_attrs,
617};
618
619static struct attribute * ccwdev_attrs[] = {
620 &dev_attr_devtype.attr,
621 &dev_attr_cutype.attr,
622 &dev_attr_modalias.attr,
623 &dev_attr_online.attr,
624 &dev_attr_cmb_enable.attr,
625 &dev_attr_availability.attr,
626 NULL,
627};
628
629static const struct attribute_group ccwdev_attr_group = {
630 .attrs = ccwdev_attrs,
631};
632
633static const struct attribute_group *ccwdev_attr_groups[] = {
634 &ccwdev_attr_group,
635 NULL,
636};
637
638static int ccw_device_add(struct ccw_device *cdev)
639{
640 struct device *dev = &cdev->dev;
641
642 dev->bus = &ccw_bus_type;
643 return device_add(dev);
644}
645
646static int match_dev_id(struct device *dev, const void *data)
647{
648 struct ccw_device *cdev = to_ccwdev(dev);
649 struct ccw_dev_id *dev_id = (void *)data;
650
651 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
652}
653
654
655
656
657
658
659
660
661
662
663
664struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
665{
666 struct device *dev;
667
668 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
669
670 return dev ? to_ccwdev(dev) : NULL;
671}
672EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
673
674static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
675{
676 int ret;
677
678 if (device_is_registered(&cdev->dev)) {
679 device_release_driver(&cdev->dev);
680 ret = device_attach(&cdev->dev);
681 WARN_ON(ret == -ENODEV);
682 }
683}
684
685static void
686ccw_device_release(struct device *dev)
687{
688 struct ccw_device *cdev;
689
690 cdev = to_ccwdev(dev);
691 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
692 sizeof(*cdev->private->dma_area));
693 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
694
695 put_device(cdev->dev.parent);
696 kfree(cdev->private);
697 kfree(cdev);
698}
699
700static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
701{
702 struct ccw_device *cdev;
703 struct gen_pool *dma_pool;
704
705 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
706 if (!cdev)
707 goto err_cdev;
708 cdev->private = kzalloc(sizeof(struct ccw_device_private),
709 GFP_KERNEL | GFP_DMA);
710 if (!cdev->private)
711 goto err_priv;
712 cdev->dev.coherent_dma_mask = sch->dev.coherent_dma_mask;
713 cdev->dev.dma_mask = &cdev->dev.coherent_dma_mask;
714 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
715 if (!dma_pool)
716 goto err_dma_pool;
717 cdev->private->dma_pool = dma_pool;
718 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
719 sizeof(*cdev->private->dma_area));
720 if (!cdev->private->dma_area)
721 goto err_dma_area;
722 return cdev;
723err_dma_area:
724 cio_gp_dma_destroy(dma_pool, &cdev->dev);
725err_dma_pool:
726 kfree(cdev->private);
727err_priv:
728 kfree(cdev);
729err_cdev:
730 return ERR_PTR(-ENOMEM);
731}
732
733static void ccw_device_todo(struct work_struct *work);
734
735static int io_subchannel_initialize_dev(struct subchannel *sch,
736 struct ccw_device *cdev)
737{
738 struct ccw_device_private *priv = cdev->private;
739 int ret;
740
741 priv->cdev = cdev;
742 priv->int_class = IRQIO_CIO;
743 priv->state = DEV_STATE_NOT_OPER;
744 priv->dev_id.devno = sch->schib.pmcw.dev;
745 priv->dev_id.ssid = sch->schid.ssid;
746
747 INIT_WORK(&priv->todo_work, ccw_device_todo);
748 INIT_LIST_HEAD(&priv->cmb_list);
749 init_waitqueue_head(&priv->wait_q);
750 timer_setup(&priv->timer, ccw_device_timeout, 0);
751
752 atomic_set(&priv->onoff, 0);
753 cdev->ccwlock = sch->lock;
754 cdev->dev.parent = &sch->dev;
755 cdev->dev.release = ccw_device_release;
756 cdev->dev.groups = ccwdev_attr_groups;
757
758 device_initialize(&cdev->dev);
759 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
760 cdev->private->dev_id.devno);
761 if (ret)
762 goto out_put;
763 if (!get_device(&sch->dev)) {
764 ret = -ENODEV;
765 goto out_put;
766 }
767 priv->flags.initialized = 1;
768 spin_lock_irq(sch->lock);
769 sch_set_cdev(sch, cdev);
770 spin_unlock_irq(sch->lock);
771 return 0;
772
773out_put:
774
775 put_device(&cdev->dev);
776 return ret;
777}
778
779static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
780{
781 struct ccw_device *cdev;
782 int ret;
783
784 cdev = io_subchannel_allocate_dev(sch);
785 if (!IS_ERR(cdev)) {
786 ret = io_subchannel_initialize_dev(sch, cdev);
787 if (ret)
788 cdev = ERR_PTR(ret);
789 }
790 return cdev;
791}
792
793static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
794
795static void sch_create_and_recog_new_device(struct subchannel *sch)
796{
797 struct ccw_device *cdev;
798
799
800 cdev = io_subchannel_create_ccwdev(sch);
801 if (IS_ERR(cdev)) {
802
803 css_sch_device_unregister(sch);
804 return;
805 }
806
807 io_subchannel_recog(cdev, sch);
808}
809
810
811
812
813static void io_subchannel_register(struct ccw_device *cdev)
814{
815 struct subchannel *sch;
816 int ret, adjust_init_count = 1;
817 unsigned long flags;
818
819 sch = to_subchannel(cdev->dev.parent);
820
821
822
823
824
825
826 if (!device_is_registered(&sch->dev))
827 goto out_err;
828 css_update_ssd_info(sch);
829
830
831
832
833
834
835 if (device_is_registered(&cdev->dev)) {
836 if (!cdev->drv) {
837 ret = device_reprobe(&cdev->dev);
838 if (ret)
839
840 CIO_MSG_EVENT(0, "device_reprobe() returned"
841 " %d for 0.%x.%04x\n", ret,
842 cdev->private->dev_id.ssid,
843 cdev->private->dev_id.devno);
844 }
845 adjust_init_count = 0;
846 goto out;
847 }
848
849
850
851
852 dev_set_uevent_suppress(&sch->dev, 0);
853 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
854
855 ret = ccw_device_add(cdev);
856 if (ret) {
857 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
858 cdev->private->dev_id.ssid,
859 cdev->private->dev_id.devno, ret);
860 spin_lock_irqsave(sch->lock, flags);
861 sch_set_cdev(sch, NULL);
862 spin_unlock_irqrestore(sch->lock, flags);
863
864 put_device(&cdev->dev);
865 goto out_err;
866 }
867out:
868 cdev->private->flags.recog_done = 1;
869 wake_up(&cdev->private->wait_q);
870out_err:
871 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
872 wake_up(&ccw_device_init_wq);
873}
874
875static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
876{
877 struct subchannel *sch;
878
879
880 if (!get_device(cdev->dev.parent))
881 return;
882 sch = to_subchannel(cdev->dev.parent);
883 css_sch_device_unregister(sch);
884
885 put_device(&sch->dev);
886}
887
888
889
890
891void
892io_subchannel_recog_done(struct ccw_device *cdev)
893{
894 if (css_init_done == 0) {
895 cdev->private->flags.recog_done = 1;
896 return;
897 }
898 switch (cdev->private->state) {
899 case DEV_STATE_BOXED:
900
901 case DEV_STATE_NOT_OPER:
902 cdev->private->flags.recog_done = 1;
903
904 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
905 if (atomic_dec_and_test(&ccw_device_init_count))
906 wake_up(&ccw_device_init_wq);
907 break;
908 case DEV_STATE_OFFLINE:
909
910
911
912
913 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
914 break;
915 }
916}
917
918static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
919{
920
921 atomic_inc(&ccw_device_init_count);
922
923
924 spin_lock_irq(sch->lock);
925 ccw_device_recognition(cdev);
926 spin_unlock_irq(sch->lock);
927}
928
929static int ccw_device_move_to_sch(struct ccw_device *cdev,
930 struct subchannel *sch)
931{
932 struct subchannel *old_sch;
933 int rc, old_enabled = 0;
934
935 old_sch = to_subchannel(cdev->dev.parent);
936
937 if (!get_device(&sch->dev))
938 return -ENODEV;
939
940 if (!sch_is_pseudo_sch(old_sch)) {
941 spin_lock_irq(old_sch->lock);
942 old_enabled = old_sch->schib.pmcw.ena;
943 rc = 0;
944 if (old_enabled)
945 rc = cio_disable_subchannel(old_sch);
946 spin_unlock_irq(old_sch->lock);
947 if (rc == -EBUSY) {
948
949 put_device(&sch->dev);
950 return rc;
951 }
952 }
953
954 mutex_lock(&sch->reg_mutex);
955 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
956 mutex_unlock(&sch->reg_mutex);
957 if (rc) {
958 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
959 cdev->private->dev_id.ssid,
960 cdev->private->dev_id.devno, sch->schid.ssid,
961 sch->schib.pmcw.dev, rc);
962 if (old_enabled) {
963
964 spin_lock_irq(old_sch->lock);
965 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
966 spin_unlock_irq(old_sch->lock);
967 }
968
969 put_device(&sch->dev);
970 return rc;
971 }
972
973 if (!sch_is_pseudo_sch(old_sch)) {
974 spin_lock_irq(old_sch->lock);
975 sch_set_cdev(old_sch, NULL);
976 spin_unlock_irq(old_sch->lock);
977 css_schedule_eval(old_sch->schid);
978 }
979
980 put_device(&old_sch->dev);
981
982 spin_lock_irq(sch->lock);
983 cdev->ccwlock = sch->lock;
984 if (!sch_is_pseudo_sch(sch))
985 sch_set_cdev(sch, cdev);
986 spin_unlock_irq(sch->lock);
987 if (!sch_is_pseudo_sch(sch))
988 css_update_ssd_info(sch);
989 return 0;
990}
991
992static int ccw_device_move_to_orph(struct ccw_device *cdev)
993{
994 struct subchannel *sch = to_subchannel(cdev->dev.parent);
995 struct channel_subsystem *css = to_css(sch->dev.parent);
996
997 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
998}
999
1000static void io_subchannel_irq(struct subchannel *sch)
1001{
1002 struct ccw_device *cdev;
1003
1004 cdev = sch_get_cdev(sch);
1005
1006 CIO_TRACE_EVENT(6, "IRQ");
1007 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1008 if (cdev)
1009 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1010 else
1011 inc_irq_stat(IRQIO_CIO);
1012}
1013
1014void io_subchannel_init_config(struct subchannel *sch)
1015{
1016 memset(&sch->config, 0, sizeof(sch->config));
1017 sch->config.csense = 1;
1018}
1019
1020static void io_subchannel_init_fields(struct subchannel *sch)
1021{
1022 if (cio_is_console(sch->schid))
1023 sch->opm = 0xff;
1024 else
1025 sch->opm = chp_get_sch_opm(sch);
1026 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1027 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1028
1029 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1030 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1031 sch->schib.pmcw.dev, sch->schid.ssid,
1032 sch->schid.sch_no, sch->schib.pmcw.pim,
1033 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1034
1035 io_subchannel_init_config(sch);
1036}
1037
1038
1039
1040
1041
1042static int io_subchannel_probe(struct subchannel *sch)
1043{
1044 struct io_subchannel_private *io_priv;
1045 struct ccw_device *cdev;
1046 int rc;
1047
1048 if (cio_is_console(sch->schid)) {
1049 rc = sysfs_create_group(&sch->dev.kobj,
1050 &io_subchannel_attr_group);
1051 if (rc)
1052 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1053 "attributes for subchannel "
1054 "0.%x.%04x (rc=%d)\n",
1055 sch->schid.ssid, sch->schid.sch_no, rc);
1056
1057
1058
1059
1060
1061 dev_set_uevent_suppress(&sch->dev, 0);
1062 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1063 cdev = sch_get_cdev(sch);
1064 rc = ccw_device_add(cdev);
1065 if (rc) {
1066
1067 put_device(&cdev->dev);
1068 goto out_schedule;
1069 }
1070 if (atomic_dec_and_test(&ccw_device_init_count))
1071 wake_up(&ccw_device_init_wq);
1072 return 0;
1073 }
1074 io_subchannel_init_fields(sch);
1075 rc = cio_commit_config(sch);
1076 if (rc)
1077 goto out_schedule;
1078 rc = sysfs_create_group(&sch->dev.kobj,
1079 &io_subchannel_attr_group);
1080 if (rc)
1081 goto out_schedule;
1082
1083 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1084 if (!io_priv)
1085 goto out_schedule;
1086
1087 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1088 sizeof(*io_priv->dma_area),
1089 &io_priv->dma_area_dma, GFP_KERNEL);
1090 if (!io_priv->dma_area) {
1091 kfree(io_priv);
1092 goto out_schedule;
1093 }
1094
1095 set_io_private(sch, io_priv);
1096 css_schedule_eval(sch->schid);
1097 return 0;
1098
1099out_schedule:
1100 spin_lock_irq(sch->lock);
1101 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1102 spin_unlock_irq(sch->lock);
1103 return 0;
1104}
1105
1106static int io_subchannel_remove(struct subchannel *sch)
1107{
1108 struct io_subchannel_private *io_priv = to_io_private(sch);
1109 struct ccw_device *cdev;
1110
1111 cdev = sch_get_cdev(sch);
1112 if (!cdev)
1113 goto out_free;
1114
1115 ccw_device_unregister(cdev);
1116 spin_lock_irq(sch->lock);
1117 sch_set_cdev(sch, NULL);
1118 set_io_private(sch, NULL);
1119 spin_unlock_irq(sch->lock);
1120out_free:
1121 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1122 io_priv->dma_area, io_priv->dma_area_dma);
1123 kfree(io_priv);
1124 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1125 return 0;
1126}
1127
1128static void io_subchannel_verify(struct subchannel *sch)
1129{
1130 struct ccw_device *cdev;
1131
1132 cdev = sch_get_cdev(sch);
1133 if (cdev)
1134 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1135}
1136
1137static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1138{
1139 struct ccw_device *cdev;
1140
1141 cdev = sch_get_cdev(sch);
1142 if (!cdev)
1143 return;
1144 if (cio_update_schib(sch))
1145 goto err;
1146
1147 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1148 goto out;
1149 if (cdev->private->state == DEV_STATE_ONLINE) {
1150 ccw_device_kill_io(cdev);
1151 goto out;
1152 }
1153 if (cio_clear(sch))
1154 goto err;
1155out:
1156
1157 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1158 return;
1159
1160err:
1161 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1162}
1163
1164static int io_subchannel_chp_event(struct subchannel *sch,
1165 struct chp_link *link, int event)
1166{
1167 struct ccw_device *cdev = sch_get_cdev(sch);
1168 int mask;
1169
1170 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1171 if (!mask)
1172 return 0;
1173 switch (event) {
1174 case CHP_VARY_OFF:
1175 sch->opm &= ~mask;
1176 sch->lpm &= ~mask;
1177 if (cdev)
1178 cdev->private->path_gone_mask |= mask;
1179 io_subchannel_terminate_path(sch, mask);
1180 break;
1181 case CHP_VARY_ON:
1182 sch->opm |= mask;
1183 sch->lpm |= mask;
1184 if (cdev)
1185 cdev->private->path_new_mask |= mask;
1186 io_subchannel_verify(sch);
1187 break;
1188 case CHP_OFFLINE:
1189 if (cio_update_schib(sch))
1190 return -ENODEV;
1191 if (cdev)
1192 cdev->private->path_gone_mask |= mask;
1193 io_subchannel_terminate_path(sch, mask);
1194 break;
1195 case CHP_ONLINE:
1196 if (cio_update_schib(sch))
1197 return -ENODEV;
1198 sch->lpm |= mask & sch->opm;
1199 if (cdev)
1200 cdev->private->path_new_mask |= mask;
1201 io_subchannel_verify(sch);
1202 break;
1203 }
1204 return 0;
1205}
1206
1207static void io_subchannel_quiesce(struct subchannel *sch)
1208{
1209 struct ccw_device *cdev;
1210 int ret;
1211
1212 spin_lock_irq(sch->lock);
1213 cdev = sch_get_cdev(sch);
1214 if (cio_is_console(sch->schid))
1215 goto out_unlock;
1216 if (!sch->schib.pmcw.ena)
1217 goto out_unlock;
1218 ret = cio_disable_subchannel(sch);
1219 if (ret != -EBUSY)
1220 goto out_unlock;
1221 if (cdev->handler)
1222 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1223 while (ret == -EBUSY) {
1224 cdev->private->state = DEV_STATE_QUIESCE;
1225 cdev->private->iretry = 255;
1226 ret = ccw_device_cancel_halt_clear(cdev);
1227 if (ret == -EBUSY) {
1228 ccw_device_set_timeout(cdev, HZ/10);
1229 spin_unlock_irq(sch->lock);
1230 wait_event(cdev->private->wait_q,
1231 cdev->private->state != DEV_STATE_QUIESCE);
1232 spin_lock_irq(sch->lock);
1233 }
1234 ret = cio_disable_subchannel(sch);
1235 }
1236out_unlock:
1237 spin_unlock_irq(sch->lock);
1238}
1239
1240static void io_subchannel_shutdown(struct subchannel *sch)
1241{
1242 io_subchannel_quiesce(sch);
1243}
1244
1245static int device_is_disconnected(struct ccw_device *cdev)
1246{
1247 if (!cdev)
1248 return 0;
1249 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1250 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1251}
1252
1253static int recovery_check(struct device *dev, void *data)
1254{
1255 struct ccw_device *cdev = to_ccwdev(dev);
1256 struct subchannel *sch;
1257 int *redo = data;
1258
1259 spin_lock_irq(cdev->ccwlock);
1260 switch (cdev->private->state) {
1261 case DEV_STATE_ONLINE:
1262 sch = to_subchannel(cdev->dev.parent);
1263 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1264 break;
1265
1266 case DEV_STATE_DISCONNECTED:
1267 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1268 cdev->private->dev_id.ssid,
1269 cdev->private->dev_id.devno);
1270 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1271 *redo = 1;
1272 break;
1273 case DEV_STATE_DISCONNECTED_SENSE_ID:
1274 *redo = 1;
1275 break;
1276 }
1277 spin_unlock_irq(cdev->ccwlock);
1278
1279 return 0;
1280}
1281
1282static void recovery_work_func(struct work_struct *unused)
1283{
1284 int redo = 0;
1285
1286 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1287 if (redo) {
1288 spin_lock_irq(&recovery_lock);
1289 if (!timer_pending(&recovery_timer)) {
1290 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1291 recovery_phase++;
1292 mod_timer(&recovery_timer, jiffies +
1293 recovery_delay[recovery_phase] * HZ);
1294 }
1295 spin_unlock_irq(&recovery_lock);
1296 } else
1297 CIO_MSG_EVENT(3, "recovery: end\n");
1298}
1299
1300static DECLARE_WORK(recovery_work, recovery_work_func);
1301
1302static void recovery_func(struct timer_list *unused)
1303{
1304
1305
1306
1307
1308 schedule_work(&recovery_work);
1309}
1310
1311void ccw_device_schedule_recovery(void)
1312{
1313 unsigned long flags;
1314
1315 CIO_MSG_EVENT(3, "recovery: schedule\n");
1316 spin_lock_irqsave(&recovery_lock, flags);
1317 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1318 recovery_phase = 0;
1319 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1320 }
1321 spin_unlock_irqrestore(&recovery_lock, flags);
1322}
1323
1324static int purge_fn(struct device *dev, void *data)
1325{
1326 struct ccw_device *cdev = to_ccwdev(dev);
1327 struct ccw_dev_id *id = &cdev->private->dev_id;
1328
1329 spin_lock_irq(cdev->ccwlock);
1330 if (is_blacklisted(id->ssid, id->devno) &&
1331 (cdev->private->state == DEV_STATE_OFFLINE) &&
1332 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1333 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1334 id->devno);
1335 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1336 atomic_set(&cdev->private->onoff, 0);
1337 }
1338 spin_unlock_irq(cdev->ccwlock);
1339
1340 if (signal_pending(current))
1341 return -EINTR;
1342
1343 return 0;
1344}
1345
1346
1347
1348
1349
1350
1351int ccw_purge_blacklisted(void)
1352{
1353 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1354 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1355 return 0;
1356}
1357
1358void ccw_device_set_disconnected(struct ccw_device *cdev)
1359{
1360 if (!cdev)
1361 return;
1362 ccw_device_set_timeout(cdev, 0);
1363 cdev->private->flags.fake_irb = 0;
1364 cdev->private->state = DEV_STATE_DISCONNECTED;
1365 if (cdev->online)
1366 ccw_device_schedule_recovery();
1367}
1368
1369void ccw_device_set_notoper(struct ccw_device *cdev)
1370{
1371 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1372
1373 CIO_TRACE_EVENT(2, "notoper");
1374 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1375 ccw_device_set_timeout(cdev, 0);
1376 cio_disable_subchannel(sch);
1377 cdev->private->state = DEV_STATE_NOT_OPER;
1378}
1379
1380enum io_sch_action {
1381 IO_SCH_UNREG,
1382 IO_SCH_ORPH_UNREG,
1383 IO_SCH_ATTACH,
1384 IO_SCH_UNREG_ATTACH,
1385 IO_SCH_ORPH_ATTACH,
1386 IO_SCH_REPROBE,
1387 IO_SCH_VERIFY,
1388 IO_SCH_DISC,
1389 IO_SCH_NOP,
1390};
1391
1392static enum io_sch_action sch_get_action(struct subchannel *sch)
1393{
1394 struct ccw_device *cdev;
1395
1396 cdev = sch_get_cdev(sch);
1397 if (cio_update_schib(sch)) {
1398
1399 if (!cdev)
1400 return IO_SCH_UNREG;
1401 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1402 return IO_SCH_UNREG;
1403 return IO_SCH_ORPH_UNREG;
1404 }
1405
1406 if (!cdev)
1407 return IO_SCH_ATTACH;
1408 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1409 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1410 return IO_SCH_UNREG_ATTACH;
1411 return IO_SCH_ORPH_ATTACH;
1412 }
1413 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1414 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1415 return IO_SCH_UNREG;
1416 return IO_SCH_DISC;
1417 }
1418 if (device_is_disconnected(cdev))
1419 return IO_SCH_REPROBE;
1420 if (cdev->online && !cdev->private->flags.resuming)
1421 return IO_SCH_VERIFY;
1422 if (cdev->private->state == DEV_STATE_NOT_OPER)
1423 return IO_SCH_UNREG_ATTACH;
1424 return IO_SCH_NOP;
1425}
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437static int io_subchannel_sch_event(struct subchannel *sch, int process)
1438{
1439 unsigned long flags;
1440 struct ccw_device *cdev;
1441 struct ccw_dev_id dev_id;
1442 enum io_sch_action action;
1443 int rc = -EAGAIN;
1444
1445 spin_lock_irqsave(sch->lock, flags);
1446 if (!device_is_registered(&sch->dev))
1447 goto out_unlock;
1448 if (work_pending(&sch->todo_work))
1449 goto out_unlock;
1450 cdev = sch_get_cdev(sch);
1451 if (cdev && work_pending(&cdev->private->todo_work))
1452 goto out_unlock;
1453 action = sch_get_action(sch);
1454 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1455 sch->schid.ssid, sch->schid.sch_no, process,
1456 action);
1457
1458 switch (action) {
1459 case IO_SCH_REPROBE:
1460
1461 ccw_device_trigger_reprobe(cdev);
1462 rc = 0;
1463 goto out_unlock;
1464 case IO_SCH_VERIFY:
1465
1466 io_subchannel_verify(sch);
1467 rc = 0;
1468 goto out_unlock;
1469 case IO_SCH_DISC:
1470 ccw_device_set_disconnected(cdev);
1471 rc = 0;
1472 goto out_unlock;
1473 case IO_SCH_ORPH_UNREG:
1474 case IO_SCH_ORPH_ATTACH:
1475 ccw_device_set_disconnected(cdev);
1476 break;
1477 case IO_SCH_UNREG_ATTACH:
1478 case IO_SCH_UNREG:
1479 if (!cdev)
1480 break;
1481 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1482
1483
1484
1485
1486
1487 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1488 } else
1489 ccw_device_set_notoper(cdev);
1490 break;
1491 case IO_SCH_NOP:
1492 rc = 0;
1493 goto out_unlock;
1494 default:
1495 break;
1496 }
1497 spin_unlock_irqrestore(sch->lock, flags);
1498
1499 if (!process)
1500 goto out;
1501
1502 switch (action) {
1503 case IO_SCH_ORPH_UNREG:
1504 case IO_SCH_ORPH_ATTACH:
1505
1506 rc = ccw_device_move_to_orph(cdev);
1507 if (rc)
1508 goto out;
1509 break;
1510 case IO_SCH_UNREG_ATTACH:
1511 spin_lock_irqsave(sch->lock, flags);
1512 if (cdev->private->flags.resuming) {
1513
1514 rc = 0;
1515 goto out_unlock;
1516 }
1517 sch_set_cdev(sch, NULL);
1518 spin_unlock_irqrestore(sch->lock, flags);
1519
1520 ccw_device_unregister(cdev);
1521 break;
1522 default:
1523 break;
1524 }
1525
1526 switch (action) {
1527 case IO_SCH_ORPH_UNREG:
1528 case IO_SCH_UNREG:
1529 if (!cdev || !cdev->private->flags.resuming)
1530 css_sch_device_unregister(sch);
1531 break;
1532 case IO_SCH_ORPH_ATTACH:
1533 case IO_SCH_UNREG_ATTACH:
1534 case IO_SCH_ATTACH:
1535 dev_id.ssid = sch->schid.ssid;
1536 dev_id.devno = sch->schib.pmcw.dev;
1537 cdev = get_ccwdev_by_dev_id(&dev_id);
1538 if (!cdev) {
1539 sch_create_and_recog_new_device(sch);
1540 break;
1541 }
1542 rc = ccw_device_move_to_sch(cdev, sch);
1543 if (rc) {
1544
1545 put_device(&cdev->dev);
1546 goto out;
1547 }
1548 spin_lock_irqsave(sch->lock, flags);
1549 ccw_device_trigger_reprobe(cdev);
1550 spin_unlock_irqrestore(sch->lock, flags);
1551
1552 put_device(&cdev->dev);
1553 break;
1554 default:
1555 break;
1556 }
1557 return 0;
1558
1559out_unlock:
1560 spin_unlock_irqrestore(sch->lock, flags);
1561out:
1562 return rc;
1563}
1564
1565static void ccw_device_set_int_class(struct ccw_device *cdev)
1566{
1567 struct ccw_driver *cdrv = cdev->drv;
1568
1569
1570
1571 if (cdrv->int_class != 0)
1572 cdev->private->int_class = cdrv->int_class;
1573 else
1574 cdev->private->int_class = IRQIO_CIO;
1575}
1576
1577#ifdef CONFIG_CCW_CONSOLE
1578int __init ccw_device_enable_console(struct ccw_device *cdev)
1579{
1580 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1581 int rc;
1582
1583 if (!cdev->drv || !cdev->handler)
1584 return -EINVAL;
1585
1586 io_subchannel_init_fields(sch);
1587 rc = cio_commit_config(sch);
1588 if (rc)
1589 return rc;
1590 sch->driver = &io_subchannel_driver;
1591 io_subchannel_recog(cdev, sch);
1592
1593 spin_lock_irq(cdev->ccwlock);
1594 while (!dev_fsm_final_state(cdev))
1595 ccw_device_wait_idle(cdev);
1596
1597
1598 get_device(&cdev->dev);
1599 rc = ccw_device_online(cdev);
1600 if (rc)
1601 goto out_unlock;
1602
1603 while (!dev_fsm_final_state(cdev))
1604 ccw_device_wait_idle(cdev);
1605
1606 if (cdev->private->state == DEV_STATE_ONLINE)
1607 cdev->online = 1;
1608 else
1609 rc = -EIO;
1610out_unlock:
1611 spin_unlock_irq(cdev->ccwlock);
1612 if (rc)
1613 put_device(&cdev->dev);
1614 return rc;
1615}
1616
1617struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1618{
1619 struct io_subchannel_private *io_priv;
1620 struct ccw_device *cdev;
1621 struct subchannel *sch;
1622
1623 sch = cio_probe_console();
1624 if (IS_ERR(sch))
1625 return ERR_CAST(sch);
1626
1627 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1628 if (!io_priv)
1629 goto err_priv;
1630 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1631 sizeof(*io_priv->dma_area),
1632 &io_priv->dma_area_dma, GFP_KERNEL);
1633 if (!io_priv->dma_area)
1634 goto err_dma_area;
1635 set_io_private(sch, io_priv);
1636 cdev = io_subchannel_create_ccwdev(sch);
1637 if (IS_ERR(cdev)) {
1638 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1639 io_priv->dma_area, io_priv->dma_area_dma);
1640 set_io_private(sch, NULL);
1641 put_device(&sch->dev);
1642 kfree(io_priv);
1643 return cdev;
1644 }
1645 cdev->drv = drv;
1646 ccw_device_set_int_class(cdev);
1647 return cdev;
1648
1649err_dma_area:
1650 kfree(io_priv);
1651err_priv:
1652 put_device(&sch->dev);
1653 return ERR_PTR(-ENOMEM);
1654}
1655
1656void __init ccw_device_destroy_console(struct ccw_device *cdev)
1657{
1658 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1659 struct io_subchannel_private *io_priv = to_io_private(sch);
1660
1661 set_io_private(sch, NULL);
1662 put_device(&sch->dev);
1663 put_device(&cdev->dev);
1664 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1665 io_priv->dma_area, io_priv->dma_area_dma);
1666 kfree(io_priv);
1667}
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677void ccw_device_wait_idle(struct ccw_device *cdev)
1678{
1679 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1680
1681 while (1) {
1682 cio_tsch(sch);
1683 if (sch->schib.scsw.cmd.actl == 0)
1684 break;
1685 udelay_simple(100);
1686 }
1687}
1688
1689static int ccw_device_pm_restore(struct device *dev);
1690
1691int ccw_device_force_console(struct ccw_device *cdev)
1692{
1693 return ccw_device_pm_restore(&cdev->dev);
1694}
1695EXPORT_SYMBOL_GPL(ccw_device_force_console);
1696#endif
1697
1698
1699
1700
1701static int
1702__ccwdev_check_busid(struct device *dev, void *id)
1703{
1704 char *bus_id;
1705
1706 bus_id = id;
1707
1708 return (strcmp(bus_id, dev_name(dev)) == 0);
1709}
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1724 const char *bus_id)
1725{
1726 struct device *dev;
1727
1728 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
1729 __ccwdev_check_busid);
1730
1731 return dev ? to_ccwdev(dev) : NULL;
1732}
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744static int
1745ccw_device_probe (struct device *dev)
1746{
1747 struct ccw_device *cdev = to_ccwdev(dev);
1748 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1749 int ret;
1750
1751 cdev->drv = cdrv;
1752 ccw_device_set_int_class(cdev);
1753 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1754 if (ret) {
1755 cdev->drv = NULL;
1756 cdev->private->int_class = IRQIO_CIO;
1757 return ret;
1758 }
1759
1760 return 0;
1761}
1762
1763static int ccw_device_remove(struct device *dev)
1764{
1765 struct ccw_device *cdev = to_ccwdev(dev);
1766 struct ccw_driver *cdrv = cdev->drv;
1767 struct subchannel *sch;
1768 int ret;
1769
1770 if (cdrv->remove)
1771 cdrv->remove(cdev);
1772
1773 spin_lock_irq(cdev->ccwlock);
1774 if (cdev->online) {
1775 cdev->online = 0;
1776 ret = ccw_device_offline(cdev);
1777 spin_unlock_irq(cdev->ccwlock);
1778 if (ret == 0)
1779 wait_event(cdev->private->wait_q,
1780 dev_fsm_final_state(cdev));
1781 else
1782 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1783 "device 0.%x.%04x\n",
1784 ret, cdev->private->dev_id.ssid,
1785 cdev->private->dev_id.devno);
1786
1787 put_device(&cdev->dev);
1788 spin_lock_irq(cdev->ccwlock);
1789 }
1790 ccw_device_set_timeout(cdev, 0);
1791 cdev->drv = NULL;
1792 cdev->private->int_class = IRQIO_CIO;
1793 sch = to_subchannel(cdev->dev.parent);
1794 spin_unlock_irq(cdev->ccwlock);
1795 io_subchannel_quiesce(sch);
1796 __disable_cmf(cdev);
1797
1798 return 0;
1799}
1800
1801static void ccw_device_shutdown(struct device *dev)
1802{
1803 struct ccw_device *cdev;
1804
1805 cdev = to_ccwdev(dev);
1806 if (cdev->drv && cdev->drv->shutdown)
1807 cdev->drv->shutdown(cdev);
1808 __disable_cmf(cdev);
1809}
1810
1811static int ccw_device_pm_prepare(struct device *dev)
1812{
1813 struct ccw_device *cdev = to_ccwdev(dev);
1814
1815 if (work_pending(&cdev->private->todo_work))
1816 return -EAGAIN;
1817
1818 if (atomic_read(&cdev->private->onoff))
1819 return -EAGAIN;
1820
1821 if (cdev->online && cdev->drv && cdev->drv->prepare)
1822 return cdev->drv->prepare(cdev);
1823
1824 return 0;
1825}
1826
1827static void ccw_device_pm_complete(struct device *dev)
1828{
1829 struct ccw_device *cdev = to_ccwdev(dev);
1830
1831 if (cdev->online && cdev->drv && cdev->drv->complete)
1832 cdev->drv->complete(cdev);
1833}
1834
1835static int ccw_device_pm_freeze(struct device *dev)
1836{
1837 struct ccw_device *cdev = to_ccwdev(dev);
1838 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1839 int ret, cm_enabled;
1840
1841
1842 if (!dev_fsm_final_state(cdev))
1843 return -EAGAIN;
1844 if (!cdev->online)
1845 return 0;
1846 if (cdev->drv && cdev->drv->freeze) {
1847 ret = cdev->drv->freeze(cdev);
1848 if (ret)
1849 return ret;
1850 }
1851
1852 spin_lock_irq(sch->lock);
1853 cm_enabled = cdev->private->cmb != NULL;
1854 spin_unlock_irq(sch->lock);
1855 if (cm_enabled) {
1856
1857 ret = ccw_set_cmf(cdev, 0);
1858 if (ret)
1859 return ret;
1860 }
1861
1862 spin_lock_irq(sch->lock);
1863 ret = cio_disable_subchannel(sch);
1864 spin_unlock_irq(sch->lock);
1865
1866 return ret;
1867}
1868
1869static int ccw_device_pm_thaw(struct device *dev)
1870{
1871 struct ccw_device *cdev = to_ccwdev(dev);
1872 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1873 int ret, cm_enabled;
1874
1875 if (!cdev->online)
1876 return 0;
1877
1878 spin_lock_irq(sch->lock);
1879
1880 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1881 cm_enabled = cdev->private->cmb != NULL;
1882 spin_unlock_irq(sch->lock);
1883 if (ret)
1884 return ret;
1885
1886 if (cm_enabled) {
1887 ret = ccw_set_cmf(cdev, 1);
1888 if (ret)
1889 return ret;
1890 }
1891
1892 if (cdev->drv && cdev->drv->thaw)
1893 ret = cdev->drv->thaw(cdev);
1894
1895 return ret;
1896}
1897
1898static void __ccw_device_pm_restore(struct ccw_device *cdev)
1899{
1900 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1901
1902 spin_lock_irq(sch->lock);
1903 if (cio_is_console(sch->schid)) {
1904 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1905 goto out_unlock;
1906 }
1907
1908
1909
1910
1911 cdev->private->flags.resuming = 1;
1912 cdev->private->path_new_mask = LPM_ANYPATH;
1913 css_sched_sch_todo(sch, SCH_TODO_EVAL);
1914 spin_unlock_irq(sch->lock);
1915 css_wait_for_slow_path();
1916
1917
1918 sch = to_subchannel(cdev->dev.parent);
1919 spin_lock_irq(sch->lock);
1920 if (cdev->private->state != DEV_STATE_ONLINE &&
1921 cdev->private->state != DEV_STATE_OFFLINE)
1922 goto out_unlock;
1923
1924 ccw_device_recognition(cdev);
1925 spin_unlock_irq(sch->lock);
1926 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1927 cdev->private->state == DEV_STATE_DISCONNECTED);
1928 spin_lock_irq(sch->lock);
1929
1930out_unlock:
1931 cdev->private->flags.resuming = 0;
1932 spin_unlock_irq(sch->lock);
1933}
1934
1935static int resume_handle_boxed(struct ccw_device *cdev)
1936{
1937 cdev->private->state = DEV_STATE_BOXED;
1938 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1939 return 0;
1940 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1941 return -ENODEV;
1942}
1943
1944static int resume_handle_disc(struct ccw_device *cdev)
1945{
1946 cdev->private->state = DEV_STATE_DISCONNECTED;
1947 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1948 return 0;
1949 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1950 return -ENODEV;
1951}
1952
1953static int ccw_device_pm_restore(struct device *dev)
1954{
1955 struct ccw_device *cdev = to_ccwdev(dev);
1956 struct subchannel *sch;
1957 int ret = 0;
1958
1959 __ccw_device_pm_restore(cdev);
1960 sch = to_subchannel(cdev->dev.parent);
1961 spin_lock_irq(sch->lock);
1962 if (cio_is_console(sch->schid))
1963 goto out_restore;
1964
1965
1966 switch (cdev->private->state) {
1967 case DEV_STATE_OFFLINE:
1968 case DEV_STATE_ONLINE:
1969 cdev->private->flags.donotify = 0;
1970 break;
1971 case DEV_STATE_BOXED:
1972 ret = resume_handle_boxed(cdev);
1973 if (ret)
1974 goto out_unlock;
1975 goto out_restore;
1976 default:
1977 ret = resume_handle_disc(cdev);
1978 if (ret)
1979 goto out_unlock;
1980 goto out_restore;
1981 }
1982
1983 if (!ccw_device_test_sense_data(cdev)) {
1984 ccw_device_update_sense_data(cdev);
1985 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1986 ret = -ENODEV;
1987 goto out_unlock;
1988 }
1989 if (!cdev->online)
1990 goto out_unlock;
1991
1992 if (ccw_device_online(cdev)) {
1993 ret = resume_handle_disc(cdev);
1994 if (ret)
1995 goto out_unlock;
1996 goto out_restore;
1997 }
1998 spin_unlock_irq(sch->lock);
1999 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2000 spin_lock_irq(sch->lock);
2001
2002 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
2003 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
2004 ret = -ENODEV;
2005 goto out_unlock;
2006 }
2007
2008
2009 if (cdev->private->cmb) {
2010 spin_unlock_irq(sch->lock);
2011 ret = ccw_set_cmf(cdev, 1);
2012 spin_lock_irq(sch->lock);
2013 if (ret) {
2014 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2015 "(rc=%d)\n", cdev->private->dev_id.ssid,
2016 cdev->private->dev_id.devno, ret);
2017 ret = 0;
2018 }
2019 }
2020
2021out_restore:
2022 spin_unlock_irq(sch->lock);
2023 if (cdev->online && cdev->drv && cdev->drv->restore)
2024 ret = cdev->drv->restore(cdev);
2025 return ret;
2026
2027out_unlock:
2028 spin_unlock_irq(sch->lock);
2029 return ret;
2030}
2031
2032static const struct dev_pm_ops ccw_pm_ops = {
2033 .prepare = ccw_device_pm_prepare,
2034 .complete = ccw_device_pm_complete,
2035 .freeze = ccw_device_pm_freeze,
2036 .thaw = ccw_device_pm_thaw,
2037 .restore = ccw_device_pm_restore,
2038};
2039
2040static struct bus_type ccw_bus_type = {
2041 .name = "ccw",
2042 .match = ccw_bus_match,
2043 .uevent = ccw_uevent,
2044 .probe = ccw_device_probe,
2045 .remove = ccw_device_remove,
2046 .shutdown = ccw_device_shutdown,
2047 .pm = &ccw_pm_ops,
2048};
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058int ccw_driver_register(struct ccw_driver *cdriver)
2059{
2060 struct device_driver *drv = &cdriver->driver;
2061
2062 drv->bus = &ccw_bus_type;
2063
2064 return driver_register(drv);
2065}
2066
2067
2068
2069
2070
2071
2072
2073void ccw_driver_unregister(struct ccw_driver *cdriver)
2074{
2075 driver_unregister(&cdriver->driver);
2076}
2077
2078static void ccw_device_todo(struct work_struct *work)
2079{
2080 struct ccw_device_private *priv;
2081 struct ccw_device *cdev;
2082 struct subchannel *sch;
2083 enum cdev_todo todo;
2084
2085 priv = container_of(work, struct ccw_device_private, todo_work);
2086 cdev = priv->cdev;
2087 sch = to_subchannel(cdev->dev.parent);
2088
2089 spin_lock_irq(cdev->ccwlock);
2090 todo = priv->todo;
2091 priv->todo = CDEV_TODO_NOTHING;
2092 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2093 priv->dev_id.ssid, priv->dev_id.devno, todo);
2094 spin_unlock_irq(cdev->ccwlock);
2095
2096 switch (todo) {
2097 case CDEV_TODO_ENABLE_CMF:
2098 cmf_reenable(cdev);
2099 break;
2100 case CDEV_TODO_REBIND:
2101 ccw_device_do_unbind_bind(cdev);
2102 break;
2103 case CDEV_TODO_REGISTER:
2104 io_subchannel_register(cdev);
2105 break;
2106 case CDEV_TODO_UNREG_EVAL:
2107 if (!sch_is_pseudo_sch(sch))
2108 css_schedule_eval(sch->schid);
2109
2110 case CDEV_TODO_UNREG:
2111 if (sch_is_pseudo_sch(sch))
2112 ccw_device_unregister(cdev);
2113 else
2114 ccw_device_call_sch_unregister(cdev);
2115 break;
2116 default:
2117 break;
2118 }
2119
2120 put_device(&cdev->dev);
2121}
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2133{
2134 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2135 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2136 todo);
2137 if (cdev->private->todo >= todo)
2138 return;
2139 cdev->private->todo = todo;
2140
2141 if (!get_device(&cdev->dev))
2142 return;
2143 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2144
2145 put_device(&cdev->dev);
2146 }
2147}
2148
2149
2150
2151
2152
2153
2154
2155
2156int ccw_device_siosl(struct ccw_device *cdev)
2157{
2158 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2159
2160 return chsc_siosl(sch->schid);
2161}
2162EXPORT_SYMBOL_GPL(ccw_device_siosl);
2163
2164EXPORT_SYMBOL(ccw_device_set_online);
2165EXPORT_SYMBOL(ccw_device_set_offline);
2166EXPORT_SYMBOL(ccw_driver_register);
2167EXPORT_SYMBOL(ccw_driver_unregister);
2168EXPORT_SYMBOL(get_ccwdev_by_busid);
2169