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