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