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/device.h>
17#include <linux/slab.h>
18#include <linux/errno.h>
19#include <linux/list.h>
20#include <linux/reboot.h>
21#include <linux/proc_fs.h>
22#include <linux/genalloc.h>
23#include <linux/dma-mapping.h>
24#include <asm/isc.h>
25#include <asm/crw.h>
26
27#include "css.h"
28#include "cio.h"
29#include "blacklist.h"
30#include "cio_debug.h"
31#include "ioasm.h"
32#include "chsc.h"
33#include "device.h"
34#include "idset.h"
35#include "chp.h"
36
37int css_init_done = 0;
38int max_ssid;
39
40#define MAX_CSS_IDX 0
41struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1];
42static struct bus_type css_bus_type;
43
44int
45for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
46{
47 struct subchannel_id schid;
48 int ret;
49
50 init_subchannel_id(&schid);
51 do {
52 do {
53 ret = fn(schid, data);
54 if (ret)
55 break;
56 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
57 schid.sch_no = 0;
58 } while (schid.ssid++ < max_ssid);
59 return ret;
60}
61
62struct cb_data {
63 void *data;
64 struct idset *set;
65 int (*fn_known_sch)(struct subchannel *, void *);
66 int (*fn_unknown_sch)(struct subchannel_id, void *);
67};
68
69static int call_fn_known_sch(struct device *dev, void *data)
70{
71 struct subchannel *sch = to_subchannel(dev);
72 struct cb_data *cb = data;
73 int rc = 0;
74
75 if (cb->set)
76 idset_sch_del(cb->set, sch->schid);
77 if (cb->fn_known_sch)
78 rc = cb->fn_known_sch(sch, cb->data);
79 return rc;
80}
81
82static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
83{
84 struct cb_data *cb = data;
85 int rc = 0;
86
87 if (idset_sch_contains(cb->set, schid))
88 rc = cb->fn_unknown_sch(schid, cb->data);
89 return rc;
90}
91
92static int call_fn_all_sch(struct subchannel_id schid, void *data)
93{
94 struct cb_data *cb = data;
95 struct subchannel *sch;
96 int rc = 0;
97
98 sch = get_subchannel_by_schid(schid);
99 if (sch) {
100 if (cb->fn_known_sch)
101 rc = cb->fn_known_sch(sch, cb->data);
102 put_device(&sch->dev);
103 } else {
104 if (cb->fn_unknown_sch)
105 rc = cb->fn_unknown_sch(schid, cb->data);
106 }
107
108 return rc;
109}
110
111int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
112 int (*fn_unknown)(struct subchannel_id,
113 void *), void *data)
114{
115 struct cb_data cb;
116 int rc;
117
118 cb.data = data;
119 cb.fn_known_sch = fn_known;
120 cb.fn_unknown_sch = fn_unknown;
121
122 if (fn_known && !fn_unknown) {
123
124 cb.set = NULL;
125 return bus_for_each_dev(&css_bus_type, NULL, &cb,
126 call_fn_known_sch);
127 }
128
129 cb.set = idset_sch_new();
130 if (!cb.set)
131
132 return for_each_subchannel(call_fn_all_sch, &cb);
133
134 idset_fill(cb.set);
135
136
137 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
138 if (rc)
139 goto out;
140
141 if (fn_unknown)
142 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
143out:
144 idset_free(cb.set);
145
146 return rc;
147}
148
149static void css_sch_todo(struct work_struct *work);
150
151static int css_sch_create_locks(struct subchannel *sch)
152{
153 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
154 if (!sch->lock)
155 return -ENOMEM;
156
157 spin_lock_init(sch->lock);
158 mutex_init(&sch->reg_mutex);
159
160 return 0;
161}
162
163static void css_subchannel_release(struct device *dev)
164{
165 struct subchannel *sch = to_subchannel(dev);
166
167 sch->config.intparm = 0;
168 cio_commit_config(sch);
169 kfree(sch->driver_override);
170 kfree(sch->lock);
171 kfree(sch);
172}
173
174static int css_validate_subchannel(struct subchannel_id schid,
175 struct schib *schib)
176{
177 int err;
178
179 switch (schib->pmcw.st) {
180 case SUBCHANNEL_TYPE_IO:
181 case SUBCHANNEL_TYPE_MSG:
182 if (!css_sch_is_valid(schib))
183 err = -ENODEV;
184 else if (is_blacklisted(schid.ssid, schib->pmcw.dev)) {
185 CIO_MSG_EVENT(6, "Blacklisted device detected "
186 "at devno %04X, subchannel set %x\n",
187 schib->pmcw.dev, schid.ssid);
188 err = -ENODEV;
189 } else
190 err = 0;
191 break;
192 default:
193 err = 0;
194 }
195 if (err)
196 goto out;
197
198 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
199 schid.ssid, schid.sch_no, schib->pmcw.st);
200out:
201 return err;
202}
203
204struct subchannel *css_alloc_subchannel(struct subchannel_id schid,
205 struct schib *schib)
206{
207 struct subchannel *sch;
208 int ret;
209
210 ret = css_validate_subchannel(schid, schib);
211 if (ret < 0)
212 return ERR_PTR(ret);
213
214 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
215 if (!sch)
216 return ERR_PTR(-ENOMEM);
217
218 sch->schid = schid;
219 sch->schib = *schib;
220 sch->st = schib->pmcw.st;
221
222 ret = css_sch_create_locks(sch);
223 if (ret)
224 goto err;
225
226 INIT_WORK(&sch->todo_work, css_sch_todo);
227 sch->dev.release = &css_subchannel_release;
228 sch->dev.dma_mask = &sch->dma_mask;
229 device_initialize(&sch->dev);
230
231
232
233
234 ret = dma_set_coherent_mask(&sch->dev, DMA_BIT_MASK(31));
235 if (ret)
236 goto err;
237
238
239
240
241 ret = dma_set_mask(&sch->dev, DMA_BIT_MASK(64));
242 if (ret)
243 goto err;
244
245 return sch;
246
247err:
248 kfree(sch);
249 return ERR_PTR(ret);
250}
251
252static int css_sch_device_register(struct subchannel *sch)
253{
254 int ret;
255
256 mutex_lock(&sch->reg_mutex);
257 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
258 sch->schid.sch_no);
259 ret = device_add(&sch->dev);
260 mutex_unlock(&sch->reg_mutex);
261 return ret;
262}
263
264
265
266
267
268void css_sch_device_unregister(struct subchannel *sch)
269{
270 mutex_lock(&sch->reg_mutex);
271 if (device_is_registered(&sch->dev))
272 device_unregister(&sch->dev);
273 mutex_unlock(&sch->reg_mutex);
274}
275EXPORT_SYMBOL_GPL(css_sch_device_unregister);
276
277static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
278{
279 int i;
280 int mask;
281
282 memset(ssd, 0, sizeof(struct chsc_ssd_info));
283 ssd->path_mask = pmcw->pim;
284 for (i = 0; i < 8; i++) {
285 mask = 0x80 >> i;
286 if (pmcw->pim & mask) {
287 chp_id_init(&ssd->chpid[i]);
288 ssd->chpid[i].id = pmcw->chpid[i];
289 }
290 }
291}
292
293static void ssd_register_chpids(struct chsc_ssd_info *ssd)
294{
295 int i;
296 int mask;
297
298 for (i = 0; i < 8; i++) {
299 mask = 0x80 >> i;
300 if (ssd->path_mask & mask)
301 chp_new(ssd->chpid[i]);
302 }
303}
304
305void css_update_ssd_info(struct subchannel *sch)
306{
307 int ret;
308
309 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
310 if (ret)
311 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
312
313 ssd_register_chpids(&sch->ssd_info);
314}
315
316static ssize_t type_show(struct device *dev, struct device_attribute *attr,
317 char *buf)
318{
319 struct subchannel *sch = to_subchannel(dev);
320
321 return sprintf(buf, "%01x\n", sch->st);
322}
323
324static DEVICE_ATTR_RO(type);
325
326static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
327 char *buf)
328{
329 struct subchannel *sch = to_subchannel(dev);
330
331 return sprintf(buf, "css:t%01X\n", sch->st);
332}
333
334static DEVICE_ATTR_RO(modalias);
335
336static ssize_t driver_override_store(struct device *dev,
337 struct device_attribute *attr,
338 const char *buf, size_t count)
339{
340 struct subchannel *sch = to_subchannel(dev);
341 char *driver_override, *old, *cp;
342
343
344 if (count >= (PAGE_SIZE - 1))
345 return -EINVAL;
346
347 driver_override = kstrndup(buf, count, GFP_KERNEL);
348 if (!driver_override)
349 return -ENOMEM;
350
351 cp = strchr(driver_override, '\n');
352 if (cp)
353 *cp = '\0';
354
355 device_lock(dev);
356 old = sch->driver_override;
357 if (strlen(driver_override)) {
358 sch->driver_override = driver_override;
359 } else {
360 kfree(driver_override);
361 sch->driver_override = NULL;
362 }
363 device_unlock(dev);
364
365 kfree(old);
366
367 return count;
368}
369
370static ssize_t driver_override_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
373 struct subchannel *sch = to_subchannel(dev);
374 ssize_t len;
375
376 device_lock(dev);
377 len = snprintf(buf, PAGE_SIZE, "%s\n", sch->driver_override);
378 device_unlock(dev);
379 return len;
380}
381static DEVICE_ATTR_RW(driver_override);
382
383static struct attribute *subch_attrs[] = {
384 &dev_attr_type.attr,
385 &dev_attr_modalias.attr,
386 &dev_attr_driver_override.attr,
387 NULL,
388};
389
390static struct attribute_group subch_attr_group = {
391 .attrs = subch_attrs,
392};
393
394static const struct attribute_group *default_subch_attr_groups[] = {
395 &subch_attr_group,
396 NULL,
397};
398
399static ssize_t chpids_show(struct device *dev,
400 struct device_attribute *attr,
401 char *buf)
402{
403 struct subchannel *sch = to_subchannel(dev);
404 struct chsc_ssd_info *ssd = &sch->ssd_info;
405 ssize_t ret = 0;
406 int mask;
407 int chp;
408
409 for (chp = 0; chp < 8; chp++) {
410 mask = 0x80 >> chp;
411 if (ssd->path_mask & mask)
412 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
413 else
414 ret += sprintf(buf + ret, "00 ");
415 }
416 ret += sprintf(buf + ret, "\n");
417 return ret;
418}
419static DEVICE_ATTR_RO(chpids);
420
421static ssize_t pimpampom_show(struct device *dev,
422 struct device_attribute *attr,
423 char *buf)
424{
425 struct subchannel *sch = to_subchannel(dev);
426 struct pmcw *pmcw = &sch->schib.pmcw;
427
428 return sprintf(buf, "%02x %02x %02x\n",
429 pmcw->pim, pmcw->pam, pmcw->pom);
430}
431static DEVICE_ATTR_RO(pimpampom);
432
433static struct attribute *io_subchannel_type_attrs[] = {
434 &dev_attr_chpids.attr,
435 &dev_attr_pimpampom.attr,
436 NULL,
437};
438ATTRIBUTE_GROUPS(io_subchannel_type);
439
440static const struct device_type io_subchannel_type = {
441 .groups = io_subchannel_type_groups,
442};
443
444int css_register_subchannel(struct subchannel *sch)
445{
446 int ret;
447
448
449 sch->dev.parent = &channel_subsystems[0]->device;
450 sch->dev.bus = &css_bus_type;
451 sch->dev.groups = default_subch_attr_groups;
452
453 if (sch->st == SUBCHANNEL_TYPE_IO)
454 sch->dev.type = &io_subchannel_type;
455
456
457
458
459
460
461
462
463
464
465 dev_set_uevent_suppress(&sch->dev, 1);
466 css_update_ssd_info(sch);
467
468 ret = css_sch_device_register(sch);
469 if (ret) {
470 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
471 sch->schid.ssid, sch->schid.sch_no, ret);
472 return ret;
473 }
474 if (!sch->driver) {
475
476
477
478
479
480 dev_set_uevent_suppress(&sch->dev, 0);
481 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
482 }
483 return ret;
484}
485
486static int css_probe_device(struct subchannel_id schid, struct schib *schib)
487{
488 struct subchannel *sch;
489 int ret;
490
491 sch = css_alloc_subchannel(schid, schib);
492 if (IS_ERR(sch))
493 return PTR_ERR(sch);
494
495 ret = css_register_subchannel(sch);
496 if (ret)
497 put_device(&sch->dev);
498
499 return ret;
500}
501
502static int
503check_subchannel(struct device *dev, const void *data)
504{
505 struct subchannel *sch;
506 struct subchannel_id *schid = (void *)data;
507
508 sch = to_subchannel(dev);
509 return schid_equal(&sch->schid, schid);
510}
511
512struct subchannel *
513get_subchannel_by_schid(struct subchannel_id schid)
514{
515 struct device *dev;
516
517 dev = bus_find_device(&css_bus_type, NULL,
518 &schid, check_subchannel);
519
520 return dev ? to_subchannel(dev) : NULL;
521}
522
523
524
525
526
527int css_sch_is_valid(struct schib *schib)
528{
529 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
530 return 0;
531 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
532 return 0;
533 return 1;
534}
535EXPORT_SYMBOL_GPL(css_sch_is_valid);
536
537static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
538{
539 struct schib schib;
540 int ccode;
541
542 if (!slow) {
543
544 return -EAGAIN;
545 }
546
547
548
549
550
551
552 ccode = stsch(schid, &schib);
553 if (ccode)
554 return (ccode == 3) ? -ENXIO : ccode;
555
556 return css_probe_device(schid, &schib);
557}
558
559static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
560{
561 int ret = 0;
562
563 if (sch->driver) {
564 if (sch->driver->sch_event)
565 ret = sch->driver->sch_event(sch, slow);
566 else
567 dev_dbg(&sch->dev,
568 "Got subchannel machine check but "
569 "no sch_event handler provided.\n");
570 }
571 if (ret != 0 && ret != -EAGAIN) {
572 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
573 sch->schid.ssid, sch->schid.sch_no, ret);
574 }
575 return ret;
576}
577
578static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
579{
580 struct subchannel *sch;
581 int ret;
582
583 sch = get_subchannel_by_schid(schid);
584 if (sch) {
585 ret = css_evaluate_known_subchannel(sch, slow);
586 put_device(&sch->dev);
587 } else
588 ret = css_evaluate_new_subchannel(schid, slow);
589 if (ret == -EAGAIN)
590 css_schedule_eval(schid);
591}
592
593
594
595
596
597
598
599
600
601
602void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
603{
604 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
605 sch->schid.ssid, sch->schid.sch_no, todo);
606 if (sch->todo >= todo)
607 return;
608
609 if (!get_device(&sch->dev))
610 return;
611 sch->todo = todo;
612 if (!queue_work(cio_work_q, &sch->todo_work)) {
613
614 put_device(&sch->dev);
615 }
616}
617EXPORT_SYMBOL_GPL(css_sched_sch_todo);
618
619static void css_sch_todo(struct work_struct *work)
620{
621 struct subchannel *sch;
622 enum sch_todo todo;
623 int ret;
624
625 sch = container_of(work, struct subchannel, todo_work);
626
627 spin_lock_irq(sch->lock);
628 todo = sch->todo;
629 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
630 sch->schid.sch_no, todo);
631 sch->todo = SCH_TODO_NOTHING;
632 spin_unlock_irq(sch->lock);
633
634 switch (todo) {
635 case SCH_TODO_NOTHING:
636 break;
637 case SCH_TODO_EVAL:
638 ret = css_evaluate_known_subchannel(sch, 1);
639 if (ret == -EAGAIN) {
640 spin_lock_irq(sch->lock);
641 css_sched_sch_todo(sch, todo);
642 spin_unlock_irq(sch->lock);
643 }
644 break;
645 case SCH_TODO_UNREG:
646 css_sch_device_unregister(sch);
647 break;
648 }
649
650 put_device(&sch->dev);
651}
652
653static struct idset *slow_subchannel_set;
654static spinlock_t slow_subchannel_lock;
655static wait_queue_head_t css_eval_wq;
656static atomic_t css_eval_scheduled;
657
658static int __init slow_subchannel_init(void)
659{
660 spin_lock_init(&slow_subchannel_lock);
661 atomic_set(&css_eval_scheduled, 0);
662 init_waitqueue_head(&css_eval_wq);
663 slow_subchannel_set = idset_sch_new();
664 if (!slow_subchannel_set) {
665 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
666 return -ENOMEM;
667 }
668 return 0;
669}
670
671static int slow_eval_known_fn(struct subchannel *sch, void *data)
672{
673 int eval;
674 int rc;
675
676 spin_lock_irq(&slow_subchannel_lock);
677 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
678 idset_sch_del(slow_subchannel_set, sch->schid);
679 spin_unlock_irq(&slow_subchannel_lock);
680 if (eval) {
681 rc = css_evaluate_known_subchannel(sch, 1);
682 if (rc == -EAGAIN)
683 css_schedule_eval(sch->schid);
684
685
686
687
688 cond_resched();
689 }
690 return 0;
691}
692
693static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
694{
695 int eval;
696 int rc = 0;
697
698 spin_lock_irq(&slow_subchannel_lock);
699 eval = idset_sch_contains(slow_subchannel_set, schid);
700 idset_sch_del(slow_subchannel_set, schid);
701 spin_unlock_irq(&slow_subchannel_lock);
702 if (eval) {
703 rc = css_evaluate_new_subchannel(schid, 1);
704 switch (rc) {
705 case -EAGAIN:
706 css_schedule_eval(schid);
707 rc = 0;
708 break;
709 case -ENXIO:
710 case -ENOMEM:
711 case -EIO:
712
713 spin_lock_irq(&slow_subchannel_lock);
714 idset_sch_del_subseq(slow_subchannel_set, schid);
715 spin_unlock_irq(&slow_subchannel_lock);
716 break;
717 default:
718 rc = 0;
719 }
720
721
722 cond_resched();
723 }
724 return rc;
725}
726
727static void css_slow_path_func(struct work_struct *unused)
728{
729 unsigned long flags;
730
731 CIO_TRACE_EVENT(4, "slowpath");
732 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
733 NULL);
734 spin_lock_irqsave(&slow_subchannel_lock, flags);
735 if (idset_is_empty(slow_subchannel_set)) {
736 atomic_set(&css_eval_scheduled, 0);
737 wake_up(&css_eval_wq);
738 }
739 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
740}
741
742static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
743struct workqueue_struct *cio_work_q;
744
745void css_schedule_eval(struct subchannel_id schid)
746{
747 unsigned long flags;
748
749 spin_lock_irqsave(&slow_subchannel_lock, flags);
750 idset_sch_add(slow_subchannel_set, schid);
751 atomic_set(&css_eval_scheduled, 1);
752 queue_delayed_work(cio_work_q, &slow_path_work, 0);
753 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
754}
755
756void css_schedule_eval_all(void)
757{
758 unsigned long flags;
759
760 spin_lock_irqsave(&slow_subchannel_lock, flags);
761 idset_fill(slow_subchannel_set);
762 atomic_set(&css_eval_scheduled, 1);
763 queue_delayed_work(cio_work_q, &slow_path_work, 0);
764 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
765}
766
767static int __unset_registered(struct device *dev, void *data)
768{
769 struct idset *set = data;
770 struct subchannel *sch = to_subchannel(dev);
771
772 idset_sch_del(set, sch->schid);
773 return 0;
774}
775
776void css_schedule_eval_all_unreg(unsigned long delay)
777{
778 unsigned long flags;
779 struct idset *unreg_set;
780
781
782 unreg_set = idset_sch_new();
783 if (!unreg_set) {
784
785 css_schedule_eval_all();
786 return;
787 }
788 idset_fill(unreg_set);
789 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
790
791 spin_lock_irqsave(&slow_subchannel_lock, flags);
792 idset_add_set(slow_subchannel_set, unreg_set);
793 atomic_set(&css_eval_scheduled, 1);
794 queue_delayed_work(cio_work_q, &slow_path_work, delay);
795 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
796 idset_free(unreg_set);
797}
798
799void css_wait_for_slow_path(void)
800{
801 flush_workqueue(cio_work_q);
802}
803
804
805void css_schedule_reprobe(void)
806{
807
808 css_schedule_eval_all_unreg(1 * HZ);
809}
810EXPORT_SYMBOL_GPL(css_schedule_reprobe);
811
812
813
814
815static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
816{
817 struct subchannel_id mchk_schid;
818 struct subchannel *sch;
819
820 if (overflow) {
821 css_schedule_eval_all();
822 return;
823 }
824 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
825 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
826 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
827 crw0->erc, crw0->rsid);
828 if (crw1)
829 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
830 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
831 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
832 crw1->anc, crw1->erc, crw1->rsid);
833 init_subchannel_id(&mchk_schid);
834 mchk_schid.sch_no = crw0->rsid;
835 if (crw1)
836 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
837
838 if (crw0->erc == CRW_ERC_PMOD) {
839 sch = get_subchannel_by_schid(mchk_schid);
840 if (sch) {
841 css_update_ssd_info(sch);
842 put_device(&sch->dev);
843 }
844 }
845
846
847
848
849
850 css_evaluate_subchannel(mchk_schid, 0);
851}
852
853static void __init
854css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
855{
856 struct cpuid cpu_id;
857
858 if (css_general_characteristics.mcss) {
859 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
860 css->global_pgid.pgid_high.ext_cssid.cssid =
861 css->id_valid ? css->cssid : 0;
862 } else {
863 css->global_pgid.pgid_high.cpu_addr = stap();
864 }
865 get_cpu_id(&cpu_id);
866 css->global_pgid.cpu_id = cpu_id.ident;
867 css->global_pgid.cpu_model = cpu_id.machine;
868 css->global_pgid.tod_high = tod_high;
869}
870
871static void channel_subsystem_release(struct device *dev)
872{
873 struct channel_subsystem *css = to_css(dev);
874
875 mutex_destroy(&css->mutex);
876 kfree(css);
877}
878
879static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
880 char *buf)
881{
882 struct channel_subsystem *css = to_css(dev);
883
884 if (!css->id_valid)
885 return -EINVAL;
886
887 return sprintf(buf, "%x\n", css->cssid);
888}
889static DEVICE_ATTR_RO(real_cssid);
890
891static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
892 char *buf)
893{
894 struct channel_subsystem *css = to_css(dev);
895 int ret;
896
897 mutex_lock(&css->mutex);
898 ret = sprintf(buf, "%x\n", css->cm_enabled);
899 mutex_unlock(&css->mutex);
900 return ret;
901}
902
903static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
904 const char *buf, size_t count)
905{
906 struct channel_subsystem *css = to_css(dev);
907 unsigned long val;
908 int ret;
909
910 ret = kstrtoul(buf, 16, &val);
911 if (ret)
912 return ret;
913 mutex_lock(&css->mutex);
914 switch (val) {
915 case 0:
916 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
917 break;
918 case 1:
919 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
920 break;
921 default:
922 ret = -EINVAL;
923 }
924 mutex_unlock(&css->mutex);
925 return ret < 0 ? ret : count;
926}
927static DEVICE_ATTR_RW(cm_enable);
928
929static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
930 int index)
931{
932 return css_chsc_characteristics.secm ? attr->mode : 0;
933}
934
935static struct attribute *cssdev_attrs[] = {
936 &dev_attr_real_cssid.attr,
937 NULL,
938};
939
940static struct attribute_group cssdev_attr_group = {
941 .attrs = cssdev_attrs,
942};
943
944static struct attribute *cssdev_cm_attrs[] = {
945 &dev_attr_cm_enable.attr,
946 NULL,
947};
948
949static struct attribute_group cssdev_cm_attr_group = {
950 .attrs = cssdev_cm_attrs,
951 .is_visible = cm_enable_mode,
952};
953
954static const struct attribute_group *cssdev_attr_groups[] = {
955 &cssdev_attr_group,
956 &cssdev_cm_attr_group,
957 NULL,
958};
959
960static int __init setup_css(int nr)
961{
962 struct channel_subsystem *css;
963 int ret;
964
965 css = kzalloc(sizeof(*css), GFP_KERNEL);
966 if (!css)
967 return -ENOMEM;
968
969 channel_subsystems[nr] = css;
970 dev_set_name(&css->device, "css%x", nr);
971 css->device.groups = cssdev_attr_groups;
972 css->device.release = channel_subsystem_release;
973
974
975
976
977
978 ret = dma_coerce_mask_and_coherent(&css->device, DMA_BIT_MASK(64));
979 if (ret) {
980 kfree(css);
981 goto out_err;
982 }
983
984 mutex_init(&css->mutex);
985 ret = chsc_get_cssid_iid(nr, &css->cssid, &css->iid);
986 if (!ret) {
987 css->id_valid = true;
988 pr_info("Partition identifier %01x.%01x\n", css->cssid,
989 css->iid);
990 }
991 css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
992
993 ret = device_register(&css->device);
994 if (ret) {
995 put_device(&css->device);
996 goto out_err;
997 }
998
999 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel),
1000 GFP_KERNEL);
1001 if (!css->pseudo_subchannel) {
1002 device_unregister(&css->device);
1003 ret = -ENOMEM;
1004 goto out_err;
1005 }
1006
1007 css->pseudo_subchannel->dev.parent = &css->device;
1008 css->pseudo_subchannel->dev.release = css_subchannel_release;
1009 mutex_init(&css->pseudo_subchannel->reg_mutex);
1010 ret = css_sch_create_locks(css->pseudo_subchannel);
1011 if (ret) {
1012 kfree(css->pseudo_subchannel);
1013 device_unregister(&css->device);
1014 goto out_err;
1015 }
1016
1017 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
1018 ret = device_register(&css->pseudo_subchannel->dev);
1019 if (ret) {
1020 put_device(&css->pseudo_subchannel->dev);
1021 device_unregister(&css->device);
1022 goto out_err;
1023 }
1024
1025 return ret;
1026out_err:
1027 channel_subsystems[nr] = NULL;
1028 return ret;
1029}
1030
1031static int css_reboot_event(struct notifier_block *this,
1032 unsigned long event,
1033 void *ptr)
1034{
1035 struct channel_subsystem *css;
1036 int ret;
1037
1038 ret = NOTIFY_DONE;
1039 for_each_css(css) {
1040 mutex_lock(&css->mutex);
1041 if (css->cm_enabled)
1042 if (chsc_secm(css, 0))
1043 ret = NOTIFY_BAD;
1044 mutex_unlock(&css->mutex);
1045 }
1046
1047 return ret;
1048}
1049
1050static struct notifier_block css_reboot_notifier = {
1051 .notifier_call = css_reboot_event,
1052};
1053
1054#define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
1055static struct gen_pool *cio_dma_pool;
1056
1057
1058struct device *cio_get_dma_css_dev(void)
1059{
1060 return &channel_subsystems[0]->device;
1061}
1062
1063struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
1064{
1065 struct gen_pool *gp_dma;
1066 void *cpu_addr;
1067 dma_addr_t dma_addr;
1068 int i;
1069
1070 gp_dma = gen_pool_create(3, -1);
1071 if (!gp_dma)
1072 return NULL;
1073 for (i = 0; i < nr_pages; ++i) {
1074 cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
1075 CIO_DMA_GFP);
1076 if (!cpu_addr)
1077 return gp_dma;
1078 gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
1079 dma_addr, PAGE_SIZE, -1);
1080 }
1081 return gp_dma;
1082}
1083
1084static void __gp_dma_free_dma(struct gen_pool *pool,
1085 struct gen_pool_chunk *chunk, void *data)
1086{
1087 size_t chunk_size = chunk->end_addr - chunk->start_addr + 1;
1088
1089 dma_free_coherent((struct device *) data, chunk_size,
1090 (void *) chunk->start_addr,
1091 (dma_addr_t) chunk->phys_addr);
1092}
1093
1094void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev)
1095{
1096 if (!gp_dma)
1097 return;
1098
1099 gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev);
1100 gen_pool_destroy(gp_dma);
1101}
1102
1103static int cio_dma_pool_init(void)
1104{
1105
1106 cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
1107 if (!cio_dma_pool)
1108 return -ENOMEM;
1109 return 0;
1110}
1111
1112void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
1113 size_t size)
1114{
1115 dma_addr_t dma_addr;
1116 unsigned long addr;
1117 size_t chunk_size;
1118
1119 if (!gp_dma)
1120 return NULL;
1121 addr = gen_pool_alloc(gp_dma, size);
1122 while (!addr) {
1123 chunk_size = round_up(size, PAGE_SIZE);
1124 addr = (unsigned long) dma_alloc_coherent(dma_dev,
1125 chunk_size, &dma_addr, CIO_DMA_GFP);
1126 if (!addr)
1127 return NULL;
1128 gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
1129 addr = gen_pool_alloc(gp_dma, size);
1130 }
1131 return (void *) addr;
1132}
1133
1134void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
1135{
1136 if (!cpu_addr)
1137 return;
1138 memset(cpu_addr, 0, size);
1139 gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
1140}
1141
1142
1143
1144
1145
1146
1147
1148
1149void *cio_dma_zalloc(size_t size)
1150{
1151 return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);
1152}
1153
1154void cio_dma_free(void *cpu_addr, size_t size)
1155{
1156 cio_gp_dma_free(cio_dma_pool, cpu_addr, size);
1157}
1158
1159
1160
1161
1162
1163static int __init css_bus_init(void)
1164{
1165 int ret, i;
1166
1167 ret = chsc_init();
1168 if (ret)
1169 return ret;
1170
1171 chsc_determine_css_characteristics();
1172
1173 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
1174 if (ret)
1175 max_ssid = 0;
1176 else
1177 max_ssid = __MAX_SSID;
1178
1179 ret = slow_subchannel_init();
1180 if (ret)
1181 goto out;
1182
1183 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
1184 if (ret)
1185 goto out;
1186
1187 if ((ret = bus_register(&css_bus_type)))
1188 goto out;
1189
1190
1191 for (i = 0; i <= MAX_CSS_IDX; i++) {
1192 ret = setup_css(i);
1193 if (ret)
1194 goto out_unregister;
1195 }
1196 ret = register_reboot_notifier(&css_reboot_notifier);
1197 if (ret)
1198 goto out_unregister;
1199 ret = cio_dma_pool_init();
1200 if (ret)
1201 goto out_unregister_rn;
1202 airq_init();
1203 css_init_done = 1;
1204
1205
1206 isc_register(IO_SCH_ISC);
1207
1208 return 0;
1209out_unregister_rn:
1210 unregister_reboot_notifier(&css_reboot_notifier);
1211out_unregister:
1212 while (i-- > 0) {
1213 struct channel_subsystem *css = channel_subsystems[i];
1214 device_unregister(&css->pseudo_subchannel->dev);
1215 device_unregister(&css->device);
1216 }
1217 bus_unregister(&css_bus_type);
1218out:
1219 crw_unregister_handler(CRW_RSC_SCH);
1220 idset_free(slow_subchannel_set);
1221 chsc_init_cleanup();
1222 pr_alert("The CSS device driver initialization failed with "
1223 "errno=%d\n", ret);
1224 return ret;
1225}
1226
1227static void __init css_bus_cleanup(void)
1228{
1229 struct channel_subsystem *css;
1230
1231 for_each_css(css) {
1232 device_unregister(&css->pseudo_subchannel->dev);
1233 device_unregister(&css->device);
1234 }
1235 bus_unregister(&css_bus_type);
1236 crw_unregister_handler(CRW_RSC_SCH);
1237 idset_free(slow_subchannel_set);
1238 chsc_init_cleanup();
1239 isc_unregister(IO_SCH_ISC);
1240}
1241
1242static int __init channel_subsystem_init(void)
1243{
1244 int ret;
1245
1246 ret = css_bus_init();
1247 if (ret)
1248 return ret;
1249 cio_work_q = create_singlethread_workqueue("cio");
1250 if (!cio_work_q) {
1251 ret = -ENOMEM;
1252 goto out_bus;
1253 }
1254 ret = io_subchannel_init();
1255 if (ret)
1256 goto out_wq;
1257
1258
1259 cio_register_early_subchannels();
1260
1261 css_schedule_eval_all();
1262
1263 return ret;
1264out_wq:
1265 destroy_workqueue(cio_work_q);
1266out_bus:
1267 css_bus_cleanup();
1268 return ret;
1269}
1270subsys_initcall(channel_subsystem_init);
1271
1272static int css_settle(struct device_driver *drv, void *unused)
1273{
1274 struct css_driver *cssdrv = to_cssdriver(drv);
1275
1276 if (cssdrv->settle)
1277 return cssdrv->settle();
1278 return 0;
1279}
1280
1281int css_complete_work(void)
1282{
1283 int ret;
1284
1285
1286 ret = wait_event_interruptible(css_eval_wq,
1287 atomic_read(&css_eval_scheduled) == 0);
1288 if (ret)
1289 return -EINTR;
1290 flush_workqueue(cio_work_q);
1291
1292 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1293}
1294
1295
1296
1297
1298
1299
1300static int __init channel_subsystem_init_sync(void)
1301{
1302 css_complete_work();
1303 return 0;
1304}
1305subsys_initcall_sync(channel_subsystem_init_sync);
1306
1307#ifdef CONFIG_PROC_FS
1308static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1309 size_t count, loff_t *ppos)
1310{
1311 int ret;
1312
1313
1314 crw_wait_for_channel_report();
1315 ret = css_complete_work();
1316
1317 return ret ? ret : count;
1318}
1319
1320static const struct proc_ops cio_settle_proc_ops = {
1321 .proc_open = nonseekable_open,
1322 .proc_write = cio_settle_write,
1323 .proc_lseek = no_llseek,
1324};
1325
1326static int __init cio_settle_init(void)
1327{
1328 struct proc_dir_entry *entry;
1329
1330 entry = proc_create("cio_settle", S_IWUSR, NULL, &cio_settle_proc_ops);
1331 if (!entry)
1332 return -ENOMEM;
1333 return 0;
1334}
1335device_initcall(cio_settle_init);
1336#endif
1337
1338int sch_is_pseudo_sch(struct subchannel *sch)
1339{
1340 if (!sch->dev.parent)
1341 return 0;
1342 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1343}
1344
1345static int css_bus_match(struct device *dev, struct device_driver *drv)
1346{
1347 struct subchannel *sch = to_subchannel(dev);
1348 struct css_driver *driver = to_cssdriver(drv);
1349 struct css_device_id *id;
1350
1351
1352 if (sch->driver_override && strcmp(sch->driver_override, drv->name))
1353 return 0;
1354
1355 for (id = driver->subchannel_type; id->match_flags; id++) {
1356 if (sch->st == id->type)
1357 return 1;
1358 }
1359
1360 return 0;
1361}
1362
1363static int css_probe(struct device *dev)
1364{
1365 struct subchannel *sch;
1366 int ret;
1367
1368 sch = to_subchannel(dev);
1369 sch->driver = to_cssdriver(dev->driver);
1370 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1371 if (ret)
1372 sch->driver = NULL;
1373 return ret;
1374}
1375
1376static int css_remove(struct device *dev)
1377{
1378 struct subchannel *sch;
1379 int ret;
1380
1381 sch = to_subchannel(dev);
1382 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1383 sch->driver = NULL;
1384 return ret;
1385}
1386
1387static void css_shutdown(struct device *dev)
1388{
1389 struct subchannel *sch;
1390
1391 sch = to_subchannel(dev);
1392 if (sch->driver && sch->driver->shutdown)
1393 sch->driver->shutdown(sch);
1394}
1395
1396static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1397{
1398 struct subchannel *sch = to_subchannel(dev);
1399 int ret;
1400
1401 ret = add_uevent_var(env, "ST=%01X", sch->st);
1402 if (ret)
1403 return ret;
1404 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1405 return ret;
1406}
1407
1408static struct bus_type css_bus_type = {
1409 .name = "css",
1410 .match = css_bus_match,
1411 .probe = css_probe,
1412 .remove = css_remove,
1413 .shutdown = css_shutdown,
1414 .uevent = css_uevent,
1415};
1416
1417
1418
1419
1420
1421
1422
1423
1424int css_driver_register(struct css_driver *cdrv)
1425{
1426 cdrv->drv.bus = &css_bus_type;
1427 return driver_register(&cdrv->drv);
1428}
1429EXPORT_SYMBOL_GPL(css_driver_register);
1430
1431
1432
1433
1434
1435
1436
1437void css_driver_unregister(struct css_driver *cdrv)
1438{
1439 driver_unregister(&cdrv->drv);
1440}
1441EXPORT_SYMBOL_GPL(css_driver_unregister);
1442