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