projects/linux·blob master · refs

1415 lines · 32.4 KiB

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 ([email protected])
8 * Cornelia Huck ([email protected])
9 */
10
11#define pr_fmt(fmt) "cio: " fmt
12
13#include <linux/export.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/slab.h>
17#include <linux/errno.h>
18#include <linux/list.h>
19#include <linux/reboot.h>
20#include <linux/proc_fs.h>
21#include <linux/genalloc.h>
22#include <linux/dma-mapping.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
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 const 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 void css_sch_create_locks(struct subchannel *sch)
151{
152 spin_lock_init(&sch->lock);
153 mutex_init(&sch->reg_mutex);
154}
155
156static void css_subchannel_release(struct device *dev)
157{
158 struct subchannel *sch = to_subchannel(dev);
159
160 sch->config.intparm = 0;
161 cio_commit_config(sch);
162 kfree(sch);
163}
164
165static int css_validate_subchannel(struct subchannel_id schid,
166 struct schib *schib)
167{
168 int err;
169
170 switch (schib->pmcw.st) {
171 case SUBCHANNEL_TYPE_IO:
172 case SUBCHANNEL_TYPE_MSG:
173 if (!css_sch_is_valid(schib))
174 err = -ENODEV;
175 else if (is_blacklisted(schid.ssid, schib->pmcw.dev)) {
176 CIO_MSG_EVENT(6, "Blacklisted device detected "
177 "at devno %04X, subchannel set %x\n",
178 schib->pmcw.dev, schid.ssid);
179 err = -ENODEV;
180 } else
181 err = 0;
182 break;
183 default:
184 err = 0;
185 }
186 if (err)
187 goto out;
188
189 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
190 schid.ssid, schid.sch_no, schib->pmcw.st);
191out:
192 return err;
193}
194
195struct subchannel *css_alloc_subchannel(struct subchannel_id schid,
196 struct schib *schib)
197{
198 struct subchannel *sch;
199 int ret;
200
201 ret = css_validate_subchannel(schid, schib);
202 if (ret < 0)
203 return ERR_PTR(ret);
204
205 sch = kzalloc_obj(*sch, GFP_KERNEL | GFP_DMA);
206 if (!sch)
207 return ERR_PTR(-ENOMEM);
208
209 sch->schid = schid;
210 sch->schib = *schib;
211 sch->st = schib->pmcw.st;
212
213 css_sch_create_locks(sch);
214
215 INIT_WORK(&sch->todo_work, css_sch_todo);
216 sch->dev.release = &css_subchannel_release;
217 sch->dev.dma_mask = &sch->dma_mask;
218 device_initialize(&sch->dev);
219 /*
220 * The physical addresses for some of the dma structures that can
221 * belong to a subchannel need to fit 31 bit width (e.g. ccw).
222 */
223 ret = dma_set_coherent_mask(&sch->dev, DMA_BIT_MASK(31));
224 if (ret)
225 goto err;
226 /*
227 * But we don't have such restrictions imposed on the stuff that
228 * is handled by the streaming API.
229 */
230 ret = dma_set_mask(&sch->dev, DMA_BIT_MASK(64));
231 if (ret)
232 goto err;
233
234 return sch;
235
236err:
237 put_device(&sch->dev);
238 return ERR_PTR(ret);
239}
240
241static int css_sch_device_register(struct subchannel *sch)
242{
243 int ret;
244
245 mutex_lock(&sch->reg_mutex);
246 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
247 sch->schid.sch_no);
248 ret = device_add(&sch->dev);
249 mutex_unlock(&sch->reg_mutex);
250 return ret;
251}
252
253/**
254 * css_sch_device_unregister - unregister a subchannel
255 * @sch: subchannel to be unregistered
256 */
257void css_sch_device_unregister(struct subchannel *sch)
258{
259 mutex_lock(&sch->reg_mutex);
260 if (device_is_registered(&sch->dev))
261 device_unregister(&sch->dev);
262 mutex_unlock(&sch->reg_mutex);
263}
264EXPORT_SYMBOL_GPL(css_sch_device_unregister);
265
266static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
267{
268 int i;
269 int mask;
270
271 memset(ssd, 0, sizeof(struct chsc_ssd_info));
272 ssd->path_mask = pmcw->pim;
273 for (i = 0; i < 8; i++) {
274 mask = 0x80 >> i;
275 if (pmcw->pim & mask) {
276 chp_id_init(&ssd->chpid[i]);
277 ssd->chpid[i].id = pmcw->chpid[i];
278 }
279 }
280}
281
282static void ssd_register_chpids(struct chsc_ssd_info *ssd)
283{
284 int i;
285 int mask;
286
287 for (i = 0; i < 8; i++) {
288 mask = 0x80 >> i;
289 if (ssd->path_mask & mask)
290 chp_new(ssd->chpid[i]);
291 }
292}
293
294void css_update_ssd_info(struct subchannel *sch)
295{
296 int ret;
297
298 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
299 if (ret)
300 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
301
302 ssd_register_chpids(&sch->ssd_info);
303}
304
305static ssize_t type_show(struct device *dev, struct device_attribute *attr,
306 char *buf)
307{
308 struct subchannel *sch = to_subchannel(dev);
309
310 return sysfs_emit(buf, "%01x\n", sch->st);
311}
312
313static DEVICE_ATTR_RO(type);
314
315static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
316 char *buf)
317{
318 struct subchannel *sch = to_subchannel(dev);
319
320 return sysfs_emit(buf, "css:t%01X\n", sch->st);
321}
322
323static DEVICE_ATTR_RO(modalias);
324
325static struct attribute *subch_attrs[] = {
326 &dev_attr_type.attr,
327 &dev_attr_modalias.attr,
328 NULL,
329};
330
331static struct attribute_group subch_attr_group = {
332 .attrs = subch_attrs,
333};
334
335static const struct attribute_group *default_subch_attr_groups[] = {
336 &subch_attr_group,
337 NULL,
338};
339
340static ssize_t chpids_show(struct device *dev,
341 struct device_attribute *attr,
342 char *buf)
343{
344 struct subchannel *sch = to_subchannel(dev);
345 struct chsc_ssd_info *ssd = &sch->ssd_info;
346 ssize_t ret = 0;
347 int mask;
348 int chp;
349
350 for (chp = 0; chp < 8; chp++) {
351 mask = 0x80 >> chp;
352 if (ssd->path_mask & mask)
353 ret += sysfs_emit_at(buf, ret, "%02x ", ssd->chpid[chp].id);
354 else
355 ret += sysfs_emit_at(buf, ret, "00 ");
356 }
357 ret += sysfs_emit_at(buf, ret, "\n");
358 return ret;
359}
360static DEVICE_ATTR_RO(chpids);
361
362static ssize_t pimpampom_show(struct device *dev,
363 struct device_attribute *attr,
364 char *buf)
365{
366 struct subchannel *sch = to_subchannel(dev);
367 struct pmcw *pmcw = &sch->schib.pmcw;
368
369 return sysfs_emit(buf, "%02x %02x %02x\n",
370 pmcw->pim, pmcw->pam, pmcw->pom);
371}
372static DEVICE_ATTR_RO(pimpampom);
373
374static ssize_t dev_busid_show(struct device *dev,
375 struct device_attribute *attr,
376 char *buf)
377{
378 struct subchannel *sch = to_subchannel(dev);
379 struct pmcw *pmcw = &sch->schib.pmcw;
380
381 if ((pmcw->st == SUBCHANNEL_TYPE_IO && pmcw->dnv) ||
382 (pmcw->st == SUBCHANNEL_TYPE_MSG && pmcw->w))
383 return sysfs_emit(buf, "0.%x.%04x\n", sch->schid.ssid,
384 pmcw->dev);
385 else
386 return sysfs_emit(buf, "none\n");
387}
388static DEVICE_ATTR_RO(dev_busid);
389
390static struct attribute *io_subchannel_type_attrs[] = {
391 &dev_attr_chpids.attr,
392 &dev_attr_pimpampom.attr,
393 &dev_attr_dev_busid.attr,
394 NULL,
395};
396ATTRIBUTE_GROUPS(io_subchannel_type);
397
398static const struct device_type io_subchannel_type = {
399 .groups = io_subchannel_type_groups,
400};
401
402int css_register_subchannel(struct subchannel *sch)
403{
404 int ret;
405
406 /* Initialize the subchannel structure */
407 sch->dev.parent = &channel_subsystems[0]->device;
408 sch->dev.bus = &css_bus_type;
409 sch->dev.groups = default_subch_attr_groups;
410
411 if (sch->st == SUBCHANNEL_TYPE_IO)
412 sch->dev.type = &io_subchannel_type;
413
414 css_update_ssd_info(sch);
415 /* make it known to the system */
416 ret = css_sch_device_register(sch);
417 if (ret) {
418 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
419 sch->schid.ssid, sch->schid.sch_no, ret);
420 return ret;
421 }
422 return ret;
423}
424
425static int css_probe_device(struct subchannel_id schid, struct schib *schib)
426{
427 struct subchannel *sch;
428 int ret;
429
430 sch = css_alloc_subchannel(schid, schib);
431 if (IS_ERR(sch))
432 return PTR_ERR(sch);
433
434 ret = css_register_subchannel(sch);
435 if (ret)
436 put_device(&sch->dev);
437
438 return ret;
439}
440
441static int
442check_subchannel(struct device *dev, const void *data)
443{
444 struct subchannel *sch;
445 struct subchannel_id *schid = (void *)data;
446
447 sch = to_subchannel(dev);
448 return schid_equal(&sch->schid, schid);
449}
450
451struct subchannel *
452get_subchannel_by_schid(struct subchannel_id schid)
453{
454 struct device *dev;
455
456 dev = bus_find_device(&css_bus_type, NULL,
457 &schid, check_subchannel);
458
459 return dev ? to_subchannel(dev) : NULL;
460}
461
462/**
463 * css_sch_is_valid() - check if a subchannel is valid
464 * @schib: subchannel information block for the subchannel
465 */
466int css_sch_is_valid(struct schib *schib)
467{
468 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
469 return 0;
470 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
471 return 0;
472 return 1;
473}
474EXPORT_SYMBOL_GPL(css_sch_is_valid);
475
476static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
477{
478 struct schib schib;
479 int ccode;
480
481 if (!slow) {
482 /* Will be done on the slow path. */
483 return -EAGAIN;
484 }
485 /*
486 * The first subchannel that is not-operational (ccode==3)
487 * indicates that there aren't any more devices available.
488 * If stsch gets an exception, it means the current subchannel set
489 * is not valid.
490 */
491 ccode = stsch(schid, &schib);
492 if (ccode)
493 return (ccode == 3) ? -ENXIO : ccode;
494
495 return css_probe_device(schid, &schib);
496}
497
498static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
499{
500 int ret = 0;
501
502 if (sch->driver) {
503 if (sch->driver->sch_event)
504 ret = sch->driver->sch_event(sch, slow);
505 else
506 dev_dbg(&sch->dev,
507 "Got subchannel machine check but "
508 "no sch_event handler provided.\n");
509 }
510 if (ret != 0 && ret != -EAGAIN) {
511 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
512 sch->schid.ssid, sch->schid.sch_no, ret);
513 }
514 return ret;
515}
516
517static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
518{
519 struct subchannel *sch;
520 int ret;
521
522 sch = get_subchannel_by_schid(schid);
523 if (sch) {
524 ret = css_evaluate_known_subchannel(sch, slow);
525 put_device(&sch->dev);
526 } else
527 ret = css_evaluate_new_subchannel(schid, slow);
528 if (ret == -EAGAIN)
529 css_schedule_eval(schid);
530}
531
532/**
533 * css_sched_sch_todo - schedule a subchannel operation
534 * @sch: subchannel
535 * @todo: todo
536 *
537 * Schedule the operation identified by @todo to be performed on the slow path
538 * workqueue. Do nothing if another operation with higher priority is already
539 * scheduled. Needs to be called with subchannel lock held.
540 */
541void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
542{
543 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
544 sch->schid.ssid, sch->schid.sch_no, todo);
545 if (sch->todo >= todo)
546 return;
547 /* Get workqueue ref. */
548 if (!get_device(&sch->dev))
549 return;
550 sch->todo = todo;
551 if (!queue_work(cio_work_q, &sch->todo_work)) {
552 /* Already queued, release workqueue ref. */
553 put_device(&sch->dev);
554 }
555}
556EXPORT_SYMBOL_GPL(css_sched_sch_todo);
557
558static void css_sch_todo(struct work_struct *work)
559{
560 struct subchannel *sch;
561 enum sch_todo todo;
562 int ret;
563
564 sch = container_of(work, struct subchannel, todo_work);
565 /* Find out todo. */
566 spin_lock_irq(&sch->lock);
567 todo = sch->todo;
568 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
569 sch->schid.sch_no, todo);
570 sch->todo = SCH_TODO_NOTHING;
571 spin_unlock_irq(&sch->lock);
572 /* Perform todo. */
573 switch (todo) {
574 case SCH_TODO_NOTHING:
575 break;
576 case SCH_TODO_EVAL:
577 ret = css_evaluate_known_subchannel(sch, 1);
578 if (ret == -EAGAIN) {
579 spin_lock_irq(&sch->lock);
580 css_sched_sch_todo(sch, todo);
581 spin_unlock_irq(&sch->lock);
582 }
583 break;
584 case SCH_TODO_UNREG:
585 css_sch_device_unregister(sch);
586 break;
587 }
588 /* Release workqueue ref. */
589 put_device(&sch->dev);
590}
591
592static struct idset *slow_subchannel_set;
593static DEFINE_SPINLOCK(slow_subchannel_lock);
594static DECLARE_WAIT_QUEUE_HEAD(css_eval_wq);
595static atomic_t css_eval_scheduled;
596
597static int __init slow_subchannel_init(void)
598{
599 atomic_set(&css_eval_scheduled, 0);
600 slow_subchannel_set = idset_sch_new();
601 if (!slow_subchannel_set) {
602 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
603 return -ENOMEM;
604 }
605 return 0;
606}
607
608static int slow_eval_known_fn(struct subchannel *sch, void *data)
609{
610 int eval;
611 int rc;
612
613 spin_lock_irq(&slow_subchannel_lock);
614 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
615 idset_sch_del(slow_subchannel_set, sch->schid);
616 spin_unlock_irq(&slow_subchannel_lock);
617 if (eval) {
618 rc = css_evaluate_known_subchannel(sch, 1);
619 if (rc == -EAGAIN)
620 css_schedule_eval(sch->schid);
621 }
622 return 0;
623}
624
625static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
626{
627 int eval;
628 int rc = 0;
629
630 spin_lock_irq(&slow_subchannel_lock);
631 eval = idset_sch_contains(slow_subchannel_set, schid);
632 idset_sch_del(slow_subchannel_set, schid);
633 spin_unlock_irq(&slow_subchannel_lock);
634 if (eval) {
635 rc = css_evaluate_new_subchannel(schid, 1);
636 switch (rc) {
637 case -EAGAIN:
638 css_schedule_eval(schid);
639 rc = 0;
640 break;
641 case -ENXIO:
642 case -ENOMEM:
643 case -EIO:
644 /* These should abort looping */
645 spin_lock_irq(&slow_subchannel_lock);
646 idset_sch_del_subseq(slow_subchannel_set, schid);
647 spin_unlock_irq(&slow_subchannel_lock);
648 break;
649 default:
650 rc = 0;
651 }
652 }
653 return rc;
654}
655
656static void css_slow_path_func(struct work_struct *unused)
657{
658 unsigned long flags;
659
660 CIO_TRACE_EVENT(4, "slowpath");
661 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
662 NULL);
663 spin_lock_irqsave(&slow_subchannel_lock, flags);
664 if (idset_is_empty(slow_subchannel_set)) {
665 atomic_set(&css_eval_scheduled, 0);
666 wake_up(&css_eval_wq);
667 }
668 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
669}
670
671static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
672struct workqueue_struct *cio_work_q;
673
674void css_schedule_eval(struct subchannel_id schid)
675{
676 unsigned long flags;
677
678 spin_lock_irqsave(&slow_subchannel_lock, flags);
679 idset_sch_add(slow_subchannel_set, schid);
680 atomic_set(&css_eval_scheduled, 1);
681 queue_delayed_work(cio_work_q, &slow_path_work, 0);
682 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
683}
684
685void css_schedule_eval_all(void)
686{
687 unsigned long flags;
688
689 spin_lock_irqsave(&slow_subchannel_lock, flags);
690 idset_fill(slow_subchannel_set);
691 atomic_set(&css_eval_scheduled, 1);
692 queue_delayed_work(cio_work_q, &slow_path_work, 0);
693 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
694}
695
696static int __unset_validpath(struct device *dev, void *data)
697{
698 struct idset *set = data;
699 struct subchannel *sch = to_subchannel(dev);
700 struct pmcw *pmcw = &sch->schib.pmcw;
701
702 /* Here we want to make sure that we are considering only those subchannels
703 * which do not have an operational device attached to it. This can be found
704 * with the help of PAM and POM values of pmcw. OPM provides the information
705 * about any path which is currently vary-off, so that we should not consider.
706 */
707 if (sch->st == SUBCHANNEL_TYPE_IO &&
708 (sch->opm & pmcw->pam & pmcw->pom))
709 idset_sch_del(set, sch->schid);
710
711 return 0;
712}
713
714static int __unset_online(struct device *dev, void *data)
715{
716 struct idset *set = data;
717 struct subchannel *sch = to_subchannel(dev);
718
719 if (sch->st == SUBCHANNEL_TYPE_IO && sch->config.ena)
720 idset_sch_del(set, sch->schid);
721
722 return 0;
723}
724
725void css_schedule_eval_cond(enum css_eval_cond cond, unsigned long delay)
726{
727 unsigned long flags;
728 struct idset *set;
729
730 /* Find unregistered subchannels. */
731 set = idset_sch_new();
732 if (!set) {
733 /* Fallback. */
734 css_schedule_eval_all();
735 return;
736 }
737 idset_fill(set);
738 switch (cond) {
739 case CSS_EVAL_NO_PATH:
740 bus_for_each_dev(&css_bus_type, NULL, set, __unset_validpath);
741 break;
742 case CSS_EVAL_NOT_ONLINE:
743 bus_for_each_dev(&css_bus_type, NULL, set, __unset_online);
744 break;
745 default:
746 break;
747 }
748
749 /* Apply to slow_subchannel_set. */
750 spin_lock_irqsave(&slow_subchannel_lock, flags);
751 idset_add_set(slow_subchannel_set, set);
752 atomic_set(&css_eval_scheduled, 1);
753 queue_delayed_work(cio_work_q, &slow_path_work, delay);
754 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
755 idset_free(set);
756}
757
758void css_wait_for_slow_path(void)
759{
760 flush_workqueue(cio_work_q);
761}
762
763/* Schedule reprobing of all subchannels with no valid operational path. */
764void css_schedule_reprobe(void)
765{
766 /* Schedule with a delay to allow merging of subsequent calls. */
767 css_schedule_eval_cond(CSS_EVAL_NO_PATH, 1 * HZ);
768}
769EXPORT_SYMBOL_GPL(css_schedule_reprobe);
770
771/*
772 * Called from the machine check handler for subchannel report words.
773 */
774static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
775{
776 struct subchannel_id mchk_schid;
777 struct subchannel *sch;
778
779 if (overflow) {
780 css_schedule_eval_all();
781 return;
782 }
783 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
784 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
785 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
786 crw0->erc, crw0->rsid);
787 if (crw1)
788 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
789 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
790 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
791 crw1->anc, crw1->erc, crw1->rsid);
792 init_subchannel_id(&mchk_schid);
793 mchk_schid.sch_no = crw0->rsid;
794 if (crw1)
795 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
796
797 if (crw0->erc == CRW_ERC_PMOD) {
798 sch = get_subchannel_by_schid(mchk_schid);
799 if (sch) {
800 css_update_ssd_info(sch);
801 put_device(&sch->dev);
802 }
803 }
804 /*
805 * Since we are always presented with IPI in the CRW, we have to
806 * use stsch() to find out if the subchannel in question has come
807 * or gone.
808 */
809 css_evaluate_subchannel(mchk_schid, 0);
810}
811
812static void __init
813css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
814{
815 struct cpuid cpu_id;
816
817 if (css_general_characteristics.mcss) {
818 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
819 css->global_pgid.pgid_high.ext_cssid.cssid =
820 css->id_valid ? css->cssid : 0;
821 } else {
822 css->global_pgid.pgid_high.cpu_addr = stap();
823 }
824 get_cpu_id(&cpu_id);
825 css->global_pgid.cpu_id = cpu_id.ident;
826 css->global_pgid.cpu_model = cpu_id.machine;
827 css->global_pgid.tod_high = tod_high;
828}
829
830static void channel_subsystem_release(struct device *dev)
831{
832 struct channel_subsystem *css = to_css(dev);
833
834 mutex_destroy(&css->mutex);
835 kfree(css);
836}
837
838static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
839 char *buf)
840{
841 struct channel_subsystem *css = to_css(dev);
842
843 if (!css->id_valid)
844 return -EINVAL;
845
846 return sysfs_emit(buf, "%x\n", css->cssid);
847}
848static DEVICE_ATTR_RO(real_cssid);
849
850static ssize_t rescan_store(struct device *dev, struct device_attribute *a,
851 const char *buf, size_t count)
852{
853 CIO_TRACE_EVENT(4, "usr-rescan");
854
855 css_schedule_eval_all();
856 css_complete_work();
857
858 return count;
859}
860static DEVICE_ATTR_WO(rescan);
861
862static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
863 char *buf)
864{
865 struct channel_subsystem *css = to_css(dev);
866 int ret;
867
868 mutex_lock(&css->mutex);
869 ret = sysfs_emit(buf, "%x\n", css->cm_enabled);
870 mutex_unlock(&css->mutex);
871 return ret;
872}
873
874static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
875 const char *buf, size_t count)
876{
877 struct channel_subsystem *css = to_css(dev);
878 unsigned long val;
879 int ret;
880
881 ret = kstrtoul(buf, 16, &val);
882 if (ret)
883 return ret;
884 mutex_lock(&css->mutex);
885 switch (val) {
886 case 0:
887 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
888 break;
889 case 1:
890 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
891 break;
892 default:
893 ret = -EINVAL;
894 }
895 mutex_unlock(&css->mutex);
896 return ret < 0 ? ret : count;
897}
898static DEVICE_ATTR_RW(cm_enable);
899
900static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
901 int index)
902{
903 return css_chsc_characteristics.secm ? attr->mode : 0;
904}
905
906static struct attribute *cssdev_attrs[] = {
907 &dev_attr_real_cssid.attr,
908 &dev_attr_rescan.attr,
909 NULL,
910};
911
912static struct attribute_group cssdev_attr_group = {
913 .attrs = cssdev_attrs,
914};
915
916static struct attribute *cssdev_cm_attrs[] = {
917 &dev_attr_cm_enable.attr,
918 NULL,
919};
920
921static struct attribute_group cssdev_cm_attr_group = {
922 .attrs = cssdev_cm_attrs,
923 .is_visible = cm_enable_mode,
924};
925
926static const struct attribute_group *cssdev_attr_groups[] = {
927 &cssdev_attr_group,
928 &cssdev_cm_attr_group,
929 NULL,
930};
931
932static int __init setup_css(int nr)
933{
934 struct channel_subsystem *css;
935 int ret;
936
937 css = kzalloc_obj(*css);
938 if (!css)
939 return -ENOMEM;
940
941 channel_subsystems[nr] = css;
942 dev_set_name(&css->device, "css%x", nr);
943 css->device.groups = cssdev_attr_groups;
944 css->device.release = channel_subsystem_release;
945 /*
946 * We currently allocate notifier bits with this (using
947 * css->device as the device argument with the DMA API)
948 * and are fine with 64 bit addresses.
949 */
950 ret = dma_coerce_mask_and_coherent(&css->device, DMA_BIT_MASK(64));
951 if (ret) {
952 kfree(css);
953 goto out_err;
954 }
955
956 mutex_init(&css->mutex);
957 ret = chsc_get_cssid_iid(nr, &css->cssid, &css->iid);
958 if (!ret) {
959 css->id_valid = true;
960 pr_info("Partition identifier %01x.%01x\n", css->cssid,
961 css->iid);
962 }
963 css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
964
965 ret = device_register(&css->device);
966 if (ret) {
967 put_device(&css->device);
968 goto out_err;
969 }
970
971 css->pseudo_subchannel = kzalloc_obj(*css->pseudo_subchannel);
972 if (!css->pseudo_subchannel) {
973 device_unregister(&css->device);
974 ret = -ENOMEM;
975 goto out_err;
976 }
977
978 css->pseudo_subchannel->dev.parent = &css->device;
979 css->pseudo_subchannel->dev.release = css_subchannel_release;
980 mutex_init(&css->pseudo_subchannel->reg_mutex);
981 css_sch_create_locks(css->pseudo_subchannel);
982
983 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
984 ret = device_register(&css->pseudo_subchannel->dev);
985 if (ret) {
986 put_device(&css->pseudo_subchannel->dev);
987 device_unregister(&css->device);
988 goto out_err;
989 }
990
991 return ret;
992out_err:
993 channel_subsystems[nr] = NULL;
994 return ret;
995}
996
997static int css_reboot_event(struct notifier_block *this,
998 unsigned long event,
999 void *ptr)
1000{
1001 struct channel_subsystem *css;
1002 int ret;
1003
1004 ret = NOTIFY_DONE;
1005 for_each_css(css) {
1006 mutex_lock(&css->mutex);
1007 if (css->cm_enabled)
1008 if (chsc_secm(css, 0))
1009 ret = NOTIFY_BAD;
1010 mutex_unlock(&css->mutex);
1011 }
1012
1013 return ret;
1014}
1015
1016static struct notifier_block css_reboot_notifier = {
1017 .notifier_call = css_reboot_event,
1018};
1019
1020#define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
1021static struct gen_pool *cio_dma_pool;
1022
1023/* Currently cio supports only a single css */
1024struct device *cio_get_dma_css_dev(void)
1025{
1026 return &channel_subsystems[0]->device;
1027}
1028
1029struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
1030{
1031 struct gen_pool *gp_dma;
1032 void *cpu_addr;
1033 dma_addr_t dma_addr;
1034 int i;
1035
1036 gp_dma = gen_pool_create(3, -1);
1037 if (!gp_dma)
1038 return NULL;
1039 for (i = 0; i < nr_pages; ++i) {
1040 cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
1041 CIO_DMA_GFP);
1042 if (!cpu_addr)
1043 return gp_dma;
1044 gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
1045 dma_addr, PAGE_SIZE, -1);
1046 }
1047 return gp_dma;
1048}
1049
1050static void __gp_dma_free_dma(struct gen_pool *pool,
1051 struct gen_pool_chunk *chunk, void *data)
1052{
1053 size_t chunk_size = chunk->end_addr - chunk->start_addr + 1;
1054
1055 dma_free_coherent((struct device *) data, chunk_size,
1056 (void *) chunk->start_addr,
1057 (dma_addr_t) chunk->phys_addr);
1058}
1059
1060void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev)
1061{
1062 if (!gp_dma)
1063 return;
1064 /* this is quite ugly but no better idea */
1065 gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev);
1066 gen_pool_destroy(gp_dma);
1067}
1068
1069static int cio_dma_pool_init(void)
1070{
1071 /* No need to free up the resources: compiled in */
1072 cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
1073 if (!cio_dma_pool)
1074 return -ENOMEM;
1075 return 0;
1076}
1077
1078void *__cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
1079 size_t size, dma32_t *dma_handle)
1080{
1081 dma_addr_t dma_addr;
1082 size_t chunk_size;
1083 void *addr;
1084
1085 if (!gp_dma)
1086 return NULL;
1087 addr = gen_pool_dma_alloc(gp_dma, size, &dma_addr);
1088 while (!addr) {
1089 chunk_size = round_up(size, PAGE_SIZE);
1090 addr = dma_alloc_coherent(dma_dev, chunk_size, &dma_addr, CIO_DMA_GFP);
1091 if (!addr)
1092 return NULL;
1093 gen_pool_add_virt(gp_dma, (unsigned long)addr, dma_addr, chunk_size, -1);
1094 addr = gen_pool_dma_alloc(gp_dma, size, dma_handle ? &dma_addr : NULL);
1095 }
1096 if (dma_handle)
1097 *dma_handle = (__force dma32_t)dma_addr;
1098 return addr;
1099}
1100
1101void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
1102 size_t size)
1103{
1104 return __cio_gp_dma_zalloc(gp_dma, dma_dev, size, NULL);
1105}
1106
1107void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
1108{
1109 if (!cpu_addr)
1110 return;
1111 memset(cpu_addr, 0, size);
1112 gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
1113}
1114
1115/*
1116 * Allocate dma memory from the css global pool. Intended for memory not
1117 * specific to any single device within the css. The allocated memory
1118 * is not guaranteed to be 31-bit addressable.
1119 *
1120 * Caution: Not suitable for early stuff like console.
1121 */
1122void *cio_dma_zalloc(size_t size)
1123{
1124 return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);
1125}
1126
1127void cio_dma_free(void *cpu_addr, size_t size)
1128{
1129 cio_gp_dma_free(cio_dma_pool, cpu_addr, size);
1130}
1131
1132/*
1133 * Now that the driver core is running, we can setup our channel subsystem.
1134 * The struct subchannel's are created during probing.
1135 */
1136static int __init css_bus_init(void)
1137{
1138 int ret, i;
1139
1140 ret = chsc_init();
1141 if (ret)
1142 return ret;
1143
1144 chsc_determine_css_characteristics();
1145 /* Try to enable MSS. */
1146 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
1147 if (ret)
1148 max_ssid = 0;
1149 else /* Success. */
1150 max_ssid = __MAX_SSID;
1151
1152 ret = slow_subchannel_init();
1153 if (ret)
1154 goto out;
1155
1156 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
1157 if (ret)
1158 goto out;
1159
1160 if ((ret = bus_register(&css_bus_type)))
1161 goto out;
1162
1163 /* Setup css structure. */
1164 for (i = 0; i <= MAX_CSS_IDX; i++) {
1165 ret = setup_css(i);
1166 if (ret)
1167 goto out_unregister;
1168 }
1169 ret = register_reboot_notifier(&css_reboot_notifier);
1170 if (ret)
1171 goto out_unregister;
1172 ret = cio_dma_pool_init();
1173 if (ret)
1174 goto out_unregister_rn;
1175 airq_init();
1176 css_init_done = 1;
1177
1178 /* Enable default isc for I/O subchannels. */
1179 isc_register(IO_SCH_ISC);
1180
1181 return 0;
1182out_unregister_rn:
1183 unregister_reboot_notifier(&css_reboot_notifier);
1184out_unregister:
1185 while (i-- > 0) {
1186 struct channel_subsystem *css = channel_subsystems[i];
1187 device_unregister(&css->pseudo_subchannel->dev);
1188 device_unregister(&css->device);
1189 }
1190 bus_unregister(&css_bus_type);
1191out:
1192 crw_unregister_handler(CRW_RSC_SCH);
1193 idset_free(slow_subchannel_set);
1194 chsc_init_cleanup();
1195 pr_alert("The CSS device driver initialization failed with "
1196 "errno=%d\n", ret);
1197 return ret;
1198}
1199
1200static void __init css_bus_cleanup(void)
1201{
1202 struct channel_subsystem *css;
1203
1204 for_each_css(css) {
1205 device_unregister(&css->pseudo_subchannel->dev);
1206 device_unregister(&css->device);
1207 }
1208 bus_unregister(&css_bus_type);
1209 crw_unregister_handler(CRW_RSC_SCH);
1210 idset_free(slow_subchannel_set);
1211 chsc_init_cleanup();
1212 isc_unregister(IO_SCH_ISC);
1213}
1214
1215static int __init channel_subsystem_init(void)
1216{
1217 int ret;
1218
1219 ret = css_bus_init();
1220 if (ret)
1221 return ret;
1222 cio_work_q = create_singlethread_workqueue("cio");
1223 if (!cio_work_q) {
1224 ret = -ENOMEM;
1225 goto out_bus;
1226 }
1227 ret = io_subchannel_init();
1228 if (ret)
1229 goto out_wq;
1230
1231 /* Register subchannels which are already in use. */
1232 cio_register_early_subchannels();
1233 /* Start initial subchannel evaluation. */
1234 css_schedule_eval_all();
1235
1236 return ret;
1237out_wq:
1238 destroy_workqueue(cio_work_q);
1239out_bus:
1240 css_bus_cleanup();
1241 return ret;
1242}
1243subsys_initcall(channel_subsystem_init);
1244
1245static int css_settle(struct device_driver *drv, void *unused)
1246{
1247 struct css_driver *cssdrv = to_cssdriver(drv);
1248
1249 if (cssdrv->settle)
1250 return cssdrv->settle();
1251 return 0;
1252}
1253
1254int css_complete_work(void)
1255{
1256 int ret;
1257
1258 /* Wait for the evaluation of subchannels to finish. */
1259 ret = wait_event_interruptible(css_eval_wq,
1260 atomic_read(&css_eval_scheduled) == 0);
1261 if (ret)
1262 return -EINTR;
1263 flush_workqueue(cio_work_q);
1264 /* Wait for the subchannel type specific initialization to finish */
1265 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1266}
1267
1268
1269/*
1270 * Wait for the initialization of devices to finish, to make sure we are
1271 * done with our setup if the search for the root device starts.
1272 */
1273static int __init channel_subsystem_init_sync(void)
1274{
1275 css_complete_work();
1276 return 0;
1277}
1278subsys_initcall_sync(channel_subsystem_init_sync);
1279
1280#ifdef CONFIG_PROC_FS
1281static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1282 size_t count, loff_t *ppos)
1283{
1284 int ret;
1285
1286 /* Handle pending CRW's. */
1287 crw_wait_for_channel_report();
1288 ret = css_complete_work();
1289
1290 return ret ? ret : count;
1291}
1292
1293static const struct proc_ops cio_settle_proc_ops = {
1294 .proc_open = nonseekable_open,
1295 .proc_write = cio_settle_write,
1296};
1297
1298static int __init cio_settle_init(void)
1299{
1300 struct proc_dir_entry *entry;
1301
1302 entry = proc_create("cio_settle", S_IWUSR, NULL, &cio_settle_proc_ops);
1303 if (!entry)
1304 return -ENOMEM;
1305 return 0;
1306}
1307device_initcall(cio_settle_init);
1308#endif /*CONFIG_PROC_FS*/
1309
1310int sch_is_pseudo_sch(struct subchannel *sch)
1311{
1312 if (!sch->dev.parent)
1313 return 0;
1314 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1315}
1316
1317static int css_bus_match(struct device *dev, const struct device_driver *drv)
1318{
1319 struct subchannel *sch = to_subchannel(dev);
1320 const struct css_driver *driver = to_cssdriver(drv);
1321 struct css_device_id *id;
1322 int ret;
1323
1324 /* When driver_override is set, only bind to the matching driver */
1325 ret = device_match_driver_override(dev, drv);
1326 if (ret == 0)
1327 return 0;
1328
1329 for (id = driver->subchannel_type; id->match_flags; id++) {
1330 if (sch->st == id->type)
1331 return 1;
1332 }
1333
1334 return 0;
1335}
1336
1337static int css_probe(struct device *dev)
1338{
1339 struct subchannel *sch;
1340 int ret;
1341
1342 sch = to_subchannel(dev);
1343 sch->driver = to_cssdriver(dev->driver);
1344 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1345 if (ret)
1346 sch->driver = NULL;
1347 return ret;
1348}
1349
1350static void css_remove(struct device *dev)
1351{
1352 struct subchannel *sch;
1353
1354 sch = to_subchannel(dev);
1355 if (sch->driver->remove)
1356 sch->driver->remove(sch);
1357 sch->driver = NULL;
1358}
1359
1360static void css_shutdown(struct device *dev)
1361{
1362 struct subchannel *sch;
1363
1364 sch = to_subchannel(dev);
1365 if (sch->driver && sch->driver->shutdown)
1366 sch->driver->shutdown(sch);
1367}
1368
1369static int css_uevent(const struct device *dev, struct kobj_uevent_env *env)
1370{
1371 const struct subchannel *sch = to_subchannel(dev);
1372 int ret;
1373
1374 ret = add_uevent_var(env, "ST=%01X", sch->st);
1375 if (ret)
1376 return ret;
1377 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1378 return ret;
1379}
1380
1381static const struct bus_type css_bus_type = {
1382 .name = "css",
1383 .driver_override = true,
1384 .match = css_bus_match,
1385 .probe = css_probe,
1386 .remove = css_remove,
1387 .shutdown = css_shutdown,
1388 .uevent = css_uevent,
1389};
1390
1391/**
1392 * css_driver_register - register a css driver
1393 * @cdrv: css driver to register
1394 *
1395 * This is mainly a wrapper around driver_register that sets name
1396 * and bus_type in the embedded struct device_driver correctly.
1397 */
1398int css_driver_register(struct css_driver *cdrv)
1399{
1400 cdrv->drv.bus = &css_bus_type;
1401 return driver_register(&cdrv->drv);
1402}
1403EXPORT_SYMBOL_GPL(css_driver_register);
1404
1405/**
1406 * css_driver_unregister - unregister a css driver
1407 * @cdrv: css driver to unregister
1408 *
1409 * This is a wrapper around driver_unregister.
1410 */
1411void css_driver_unregister(struct css_driver *cdrv)
1412{
1413 driver_unregister(&cdrv->drv);
1414}
1415EXPORT_SYMBOL_GPL(css_driver_unregister);