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