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