[S390] cio: Use isc_{register,unregister}.
[pandora-kernel.git] / drivers / s390 / cio / css.c
1 /*
2  *  drivers/s390/cio/css.c
3  *  driver for channel subsystem
4  *
5  *    Copyright IBM Corp. 2002,2008
6  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
7  *               Cornelia Huck (cornelia.huck@de.ibm.com)
8  */
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/reboot.h>
16 #include <asm/isc.h>
17
18 #include "../s390mach.h"
19 #include "css.h"
20 #include "cio.h"
21 #include "cio_debug.h"
22 #include "ioasm.h"
23 #include "chsc.h"
24 #include "device.h"
25 #include "idset.h"
26 #include "chp.h"
27
28 int css_init_done = 0;
29 static int need_reprobe = 0;
30 static int max_ssid = 0;
31
32 struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
33
34 int
35 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
36 {
37         struct subchannel_id schid;
38         int ret;
39
40         init_subchannel_id(&schid);
41         ret = -ENODEV;
42         do {
43                 do {
44                         ret = fn(schid, data);
45                         if (ret)
46                                 break;
47                 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
48                 schid.sch_no = 0;
49         } while (schid.ssid++ < max_ssid);
50         return ret;
51 }
52
53 struct cb_data {
54         void *data;
55         struct idset *set;
56         int (*fn_known_sch)(struct subchannel *, void *);
57         int (*fn_unknown_sch)(struct subchannel_id, void *);
58 };
59
60 static int call_fn_known_sch(struct device *dev, void *data)
61 {
62         struct subchannel *sch = to_subchannel(dev);
63         struct cb_data *cb = data;
64         int rc = 0;
65
66         idset_sch_del(cb->set, sch->schid);
67         if (cb->fn_known_sch)
68                 rc = cb->fn_known_sch(sch, cb->data);
69         return rc;
70 }
71
72 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
73 {
74         struct cb_data *cb = data;
75         int rc = 0;
76
77         if (idset_sch_contains(cb->set, schid))
78                 rc = cb->fn_unknown_sch(schid, cb->data);
79         return rc;
80 }
81
82 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
83                                int (*fn_unknown)(struct subchannel_id,
84                                void *), void *data)
85 {
86         struct cb_data cb;
87         int rc;
88
89         cb.set = idset_sch_new();
90         if (!cb.set)
91                 return -ENOMEM;
92         idset_fill(cb.set);
93         cb.data = data;
94         cb.fn_known_sch = fn_known;
95         cb.fn_unknown_sch = fn_unknown;
96         /* Process registered subchannels. */
97         rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
98         if (rc)
99                 goto out;
100         /* Process unregistered subchannels. */
101         if (fn_unknown)
102                 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
103 out:
104         idset_free(cb.set);
105
106         return rc;
107 }
108
109 static struct subchannel *
110 css_alloc_subchannel(struct subchannel_id schid)
111 {
112         struct subchannel *sch;
113         int ret;
114
115         sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
116         if (sch == NULL)
117                 return ERR_PTR(-ENOMEM);
118         ret = cio_validate_subchannel (sch, schid);
119         if (ret < 0) {
120                 kfree(sch);
121                 return ERR_PTR(ret);
122         }
123         return sch;
124 }
125
126 static void
127 css_free_subchannel(struct subchannel *sch)
128 {
129         if (sch) {
130                 /* Reset intparm to zeroes. */
131                 sch->schib.pmcw.intparm = 0;
132                 cio_modify(sch);
133                 kfree(sch->lock);
134                 kfree(sch);
135         }
136 }
137
138 static void
139 css_subchannel_release(struct device *dev)
140 {
141         struct subchannel *sch;
142
143         sch = to_subchannel(dev);
144         if (!cio_is_console(sch->schid)) {
145                 kfree(sch->lock);
146                 kfree(sch);
147         }
148 }
149
150 static int css_sch_device_register(struct subchannel *sch)
151 {
152         int ret;
153
154         mutex_lock(&sch->reg_mutex);
155         ret = device_register(&sch->dev);
156         mutex_unlock(&sch->reg_mutex);
157         return ret;
158 }
159
160 /**
161  * css_sch_device_unregister - unregister a subchannel
162  * @sch: subchannel to be unregistered
163  */
164 void css_sch_device_unregister(struct subchannel *sch)
165 {
166         mutex_lock(&sch->reg_mutex);
167         device_unregister(&sch->dev);
168         mutex_unlock(&sch->reg_mutex);
169 }
170 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
171
172 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
173 {
174         int i;
175         int mask;
176
177         memset(ssd, 0, sizeof(struct chsc_ssd_info));
178         ssd->path_mask = pmcw->pim;
179         for (i = 0; i < 8; i++) {
180                 mask = 0x80 >> i;
181                 if (pmcw->pim & mask) {
182                         chp_id_init(&ssd->chpid[i]);
183                         ssd->chpid[i].id = pmcw->chpid[i];
184                 }
185         }
186 }
187
188 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
189 {
190         int i;
191         int mask;
192
193         for (i = 0; i < 8; i++) {
194                 mask = 0x80 >> i;
195                 if (ssd->path_mask & mask)
196                         if (!chp_is_registered(ssd->chpid[i]))
197                                 chp_new(ssd->chpid[i]);
198         }
199 }
200
201 void css_update_ssd_info(struct subchannel *sch)
202 {
203         int ret;
204
205         if (cio_is_console(sch->schid)) {
206                 /* Console is initialized too early for functions requiring
207                  * memory allocation. */
208                 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
209         } else {
210                 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
211                 if (ret)
212                         ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
213                 ssd_register_chpids(&sch->ssd_info);
214         }
215 }
216
217 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
218                          char *buf)
219 {
220         struct subchannel *sch = to_subchannel(dev);
221
222         return sprintf(buf, "%01x\n", sch->st);
223 }
224
225 static DEVICE_ATTR(type, 0444, type_show, NULL);
226
227 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
228                              char *buf)
229 {
230         struct subchannel *sch = to_subchannel(dev);
231
232         return sprintf(buf, "css:t%01X\n", sch->st);
233 }
234
235 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
236
237 static struct attribute *subch_attrs[] = {
238         &dev_attr_type.attr,
239         &dev_attr_modalias.attr,
240         NULL,
241 };
242
243 static struct attribute_group subch_attr_group = {
244         .attrs = subch_attrs,
245 };
246
247 static struct attribute_group *default_subch_attr_groups[] = {
248         &subch_attr_group,
249         NULL,
250 };
251
252 static int css_register_subchannel(struct subchannel *sch)
253 {
254         int ret;
255
256         /* Initialize the subchannel structure */
257         sch->dev.parent = &channel_subsystems[0]->device;
258         sch->dev.bus = &css_bus_type;
259         sch->dev.release = &css_subchannel_release;
260         sch->dev.groups = default_subch_attr_groups;
261         /*
262          * We don't want to generate uevents for I/O subchannels that don't
263          * have a working ccw device behind them since they will be
264          * unregistered before they can be used anyway, so we delay the add
265          * uevent until after device recognition was successful.
266          * Note that we suppress the uevent for all subchannel types;
267          * the subchannel driver can decide itself when it wants to inform
268          * userspace of its existence.
269          */
270         sch->dev.uevent_suppress = 1;
271         css_update_ssd_info(sch);
272         /* make it known to the system */
273         ret = css_sch_device_register(sch);
274         if (ret) {
275                 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
276                               sch->schid.ssid, sch->schid.sch_no, ret);
277                 return ret;
278         }
279         if (!sch->driver) {
280                 /*
281                  * No driver matched. Generate the uevent now so that
282                  * a fitting driver module may be loaded based on the
283                  * modalias.
284                  */
285                 sch->dev.uevent_suppress = 0;
286                 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
287         }
288         return ret;
289 }
290
291 int css_probe_device(struct subchannel_id schid)
292 {
293         int ret;
294         struct subchannel *sch;
295
296         sch = css_alloc_subchannel(schid);
297         if (IS_ERR(sch))
298                 return PTR_ERR(sch);
299         ret = css_register_subchannel(sch);
300         if (ret)
301                 css_free_subchannel(sch);
302         return ret;
303 }
304
305 static int
306 check_subchannel(struct device * dev, void * data)
307 {
308         struct subchannel *sch;
309         struct subchannel_id *schid = data;
310
311         sch = to_subchannel(dev);
312         return schid_equal(&sch->schid, schid);
313 }
314
315 struct subchannel *
316 get_subchannel_by_schid(struct subchannel_id schid)
317 {
318         struct device *dev;
319
320         dev = bus_find_device(&css_bus_type, NULL,
321                               &schid, check_subchannel);
322
323         return dev ? to_subchannel(dev) : NULL;
324 }
325
326 /**
327  * css_sch_is_valid() - check if a subchannel is valid
328  * @schib: subchannel information block for the subchannel
329  */
330 int css_sch_is_valid(struct schib *schib)
331 {
332         if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
333                 return 0;
334         if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
335                 return 0;
336         return 1;
337 }
338 EXPORT_SYMBOL_GPL(css_sch_is_valid);
339
340 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
341 {
342         struct schib schib;
343
344         if (!slow) {
345                 /* Will be done on the slow path. */
346                 return -EAGAIN;
347         }
348         if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
349                 /* Unusable - ignore. */
350                 return 0;
351         }
352         CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
353                          "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
354
355         return css_probe_device(schid);
356 }
357
358 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
359 {
360         int ret = 0;
361
362         if (sch->driver) {
363                 if (sch->driver->sch_event)
364                         ret = sch->driver->sch_event(sch, slow);
365                 else
366                         dev_dbg(&sch->dev,
367                                 "Got subchannel machine check but "
368                                 "no sch_event handler provided.\n");
369         }
370         return ret;
371 }
372
373 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
374 {
375         struct subchannel *sch;
376         int ret;
377
378         sch = get_subchannel_by_schid(schid);
379         if (sch) {
380                 ret = css_evaluate_known_subchannel(sch, slow);
381                 put_device(&sch->dev);
382         } else
383                 ret = css_evaluate_new_subchannel(schid, slow);
384         if (ret == -EAGAIN)
385                 css_schedule_eval(schid);
386 }
387
388 static struct idset *slow_subchannel_set;
389 static spinlock_t slow_subchannel_lock;
390
391 static int __init slow_subchannel_init(void)
392 {
393         spin_lock_init(&slow_subchannel_lock);
394         slow_subchannel_set = idset_sch_new();
395         if (!slow_subchannel_set) {
396                 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
397                 return -ENOMEM;
398         }
399         return 0;
400 }
401
402 static int slow_eval_known_fn(struct subchannel *sch, void *data)
403 {
404         int eval;
405         int rc;
406
407         spin_lock_irq(&slow_subchannel_lock);
408         eval = idset_sch_contains(slow_subchannel_set, sch->schid);
409         idset_sch_del(slow_subchannel_set, sch->schid);
410         spin_unlock_irq(&slow_subchannel_lock);
411         if (eval) {
412                 rc = css_evaluate_known_subchannel(sch, 1);
413                 if (rc == -EAGAIN)
414                         css_schedule_eval(sch->schid);
415         }
416         return 0;
417 }
418
419 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
420 {
421         int eval;
422         int rc = 0;
423
424         spin_lock_irq(&slow_subchannel_lock);
425         eval = idset_sch_contains(slow_subchannel_set, schid);
426         idset_sch_del(slow_subchannel_set, schid);
427         spin_unlock_irq(&slow_subchannel_lock);
428         if (eval) {
429                 rc = css_evaluate_new_subchannel(schid, 1);
430                 switch (rc) {
431                 case -EAGAIN:
432                         css_schedule_eval(schid);
433                         rc = 0;
434                         break;
435                 case -ENXIO:
436                 case -ENOMEM:
437                 case -EIO:
438                         /* These should abort looping */
439                         break;
440                 default:
441                         rc = 0;
442                 }
443         }
444         return rc;
445 }
446
447 static void css_slow_path_func(struct work_struct *unused)
448 {
449         CIO_TRACE_EVENT(4, "slowpath");
450         for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
451                                    NULL);
452 }
453
454 static DECLARE_WORK(slow_path_work, css_slow_path_func);
455 struct workqueue_struct *slow_path_wq;
456
457 void css_schedule_eval(struct subchannel_id schid)
458 {
459         unsigned long flags;
460
461         spin_lock_irqsave(&slow_subchannel_lock, flags);
462         idset_sch_add(slow_subchannel_set, schid);
463         queue_work(slow_path_wq, &slow_path_work);
464         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
465 }
466
467 void css_schedule_eval_all(void)
468 {
469         unsigned long flags;
470
471         spin_lock_irqsave(&slow_subchannel_lock, flags);
472         idset_fill(slow_subchannel_set);
473         queue_work(slow_path_wq, &slow_path_work);
474         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
475 }
476
477 void css_wait_for_slow_path(void)
478 {
479         flush_workqueue(ccw_device_notify_work);
480         flush_workqueue(slow_path_wq);
481 }
482
483 /* Reprobe subchannel if unregistered. */
484 static int reprobe_subchannel(struct subchannel_id schid, void *data)
485 {
486         int ret;
487
488         CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
489                       schid.ssid, schid.sch_no);
490         if (need_reprobe)
491                 return -EAGAIN;
492
493         ret = css_probe_device(schid);
494         switch (ret) {
495         case 0:
496                 break;
497         case -ENXIO:
498         case -ENOMEM:
499         case -EIO:
500                 /* These should abort looping */
501                 break;
502         default:
503                 ret = 0;
504         }
505
506         return ret;
507 }
508
509 /* Work function used to reprobe all unregistered subchannels. */
510 static void reprobe_all(struct work_struct *unused)
511 {
512         int ret;
513
514         CIO_MSG_EVENT(4, "reprobe start\n");
515
516         need_reprobe = 0;
517         /* Make sure initial subchannel scan is done. */
518         wait_event(ccw_device_init_wq,
519                    atomic_read(&ccw_device_init_count) == 0);
520         ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
521
522         CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
523                       need_reprobe);
524 }
525
526 static DECLARE_WORK(css_reprobe_work, reprobe_all);
527
528 /* Schedule reprobing of all unregistered subchannels. */
529 void css_schedule_reprobe(void)
530 {
531         need_reprobe = 1;
532         queue_work(slow_path_wq, &css_reprobe_work);
533 }
534
535 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
536
537 /*
538  * Called from the machine check handler for subchannel report words.
539  */
540 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
541 {
542         struct subchannel_id mchk_schid;
543
544         if (overflow) {
545                 css_schedule_eval_all();
546                 return;
547         }
548         CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
549                       "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
550                       crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
551                       crw0->erc, crw0->rsid);
552         if (crw1)
553                 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
554                               "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
555                               crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
556                               crw1->anc, crw1->erc, crw1->rsid);
557         init_subchannel_id(&mchk_schid);
558         mchk_schid.sch_no = crw0->rsid;
559         if (crw1)
560                 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
561
562         /*
563          * Since we are always presented with IPI in the CRW, we have to
564          * use stsch() to find out if the subchannel in question has come
565          * or gone.
566          */
567         css_evaluate_subchannel(mchk_schid, 0);
568 }
569
570 static int __init
571 __init_channel_subsystem(struct subchannel_id schid, void *data)
572 {
573         struct subchannel *sch;
574         int ret;
575
576         if (cio_is_console(schid))
577                 sch = cio_get_console_subchannel();
578         else {
579                 sch = css_alloc_subchannel(schid);
580                 if (IS_ERR(sch))
581                         ret = PTR_ERR(sch);
582                 else
583                         ret = 0;
584                 switch (ret) {
585                 case 0:
586                         break;
587                 case -ENOMEM:
588                         panic("Out of memory in init_channel_subsystem\n");
589                 /* -ENXIO: no more subchannels. */
590                 case -ENXIO:
591                         return ret;
592                 /* -EIO: this subchannel set not supported. */
593                 case -EIO:
594                         return ret;
595                 default:
596                         return 0;
597                 }
598         }
599         /*
600          * We register ALL valid subchannels in ioinfo, even those
601          * that have been present before init_channel_subsystem.
602          * These subchannels can't have been registered yet (kmalloc
603          * not working) so we do it now. This is true e.g. for the
604          * console subchannel.
605          */
606         css_register_subchannel(sch);
607         return 0;
608 }
609
610 static void __init
611 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
612 {
613         if (css_general_characteristics.mcss) {
614                 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
615                 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
616         } else {
617 #ifdef CONFIG_SMP
618                 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
619 #else
620                 css->global_pgid.pgid_high.cpu_addr = 0;
621 #endif
622         }
623         css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
624         css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
625         css->global_pgid.tod_high = tod_high;
626
627 }
628
629 static void
630 channel_subsystem_release(struct device *dev)
631 {
632         struct channel_subsystem *css;
633
634         css = to_css(dev);
635         mutex_destroy(&css->mutex);
636         kfree(css);
637 }
638
639 static ssize_t
640 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
641                    char *buf)
642 {
643         struct channel_subsystem *css = to_css(dev);
644         int ret;
645
646         if (!css)
647                 return 0;
648         mutex_lock(&css->mutex);
649         ret = sprintf(buf, "%x\n", css->cm_enabled);
650         mutex_unlock(&css->mutex);
651         return ret;
652 }
653
654 static ssize_t
655 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
656                     const char *buf, size_t count)
657 {
658         struct channel_subsystem *css = to_css(dev);
659         int ret;
660         unsigned long val;
661
662         ret = strict_strtoul(buf, 16, &val);
663         if (ret)
664                 return ret;
665         mutex_lock(&css->mutex);
666         switch (val) {
667         case 0:
668                 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
669                 break;
670         case 1:
671                 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
672                 break;
673         default:
674                 ret = -EINVAL;
675         }
676         mutex_unlock(&css->mutex);
677         return ret < 0 ? ret : count;
678 }
679
680 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
681
682 static int __init setup_css(int nr)
683 {
684         u32 tod_high;
685         int ret;
686         struct channel_subsystem *css;
687
688         css = channel_subsystems[nr];
689         memset(css, 0, sizeof(struct channel_subsystem));
690         css->pseudo_subchannel =
691                 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
692         if (!css->pseudo_subchannel)
693                 return -ENOMEM;
694         css->pseudo_subchannel->dev.parent = &css->device;
695         css->pseudo_subchannel->dev.release = css_subchannel_release;
696         sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
697         ret = cio_create_sch_lock(css->pseudo_subchannel);
698         if (ret) {
699                 kfree(css->pseudo_subchannel);
700                 return ret;
701         }
702         mutex_init(&css->mutex);
703         css->valid = 1;
704         css->cssid = nr;
705         sprintf(css->device.bus_id, "css%x", nr);
706         css->device.release = channel_subsystem_release;
707         tod_high = (u32) (get_clock() >> 32);
708         css_generate_pgid(css, tod_high);
709         return 0;
710 }
711
712 static int css_reboot_event(struct notifier_block *this,
713                             unsigned long event,
714                             void *ptr)
715 {
716         int ret, i;
717
718         ret = NOTIFY_DONE;
719         for (i = 0; i <= __MAX_CSSID; i++) {
720                 struct channel_subsystem *css;
721
722                 css = channel_subsystems[i];
723                 mutex_lock(&css->mutex);
724                 if (css->cm_enabled)
725                         if (chsc_secm(css, 0))
726                                 ret = NOTIFY_BAD;
727                 mutex_unlock(&css->mutex);
728         }
729
730         return ret;
731 }
732
733 static struct notifier_block css_reboot_notifier = {
734         .notifier_call = css_reboot_event,
735 };
736
737 /*
738  * Now that the driver core is running, we can setup our channel subsystem.
739  * The struct subchannel's are created during probing (except for the
740  * static console subchannel).
741  */
742 static int __init
743 init_channel_subsystem (void)
744 {
745         int ret, i;
746
747         ret = chsc_determine_css_characteristics();
748         if (ret == -ENOMEM)
749                 goto out; /* No need to continue. */
750
751         ret = chsc_alloc_sei_area();
752         if (ret)
753                 goto out;
754
755         ret = slow_subchannel_init();
756         if (ret)
757                 goto out;
758
759         ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
760         if (ret)
761                 goto out;
762
763         if ((ret = bus_register(&css_bus_type)))
764                 goto out;
765
766         /* Try to enable MSS. */
767         ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
768         switch (ret) {
769         case 0: /* Success. */
770                 max_ssid = __MAX_SSID;
771                 break;
772         case -ENOMEM:
773                 goto out_bus;
774         default:
775                 max_ssid = 0;
776         }
777         /* Setup css structure. */
778         for (i = 0; i <= __MAX_CSSID; i++) {
779                 struct channel_subsystem *css;
780
781                 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
782                 if (!css) {
783                         ret = -ENOMEM;
784                         goto out_unregister;
785                 }
786                 channel_subsystems[i] = css;
787                 ret = setup_css(i);
788                 if (ret)
789                         goto out_free;
790                 ret = device_register(&css->device);
791                 if (ret)
792                         goto out_free_all;
793                 if (css_chsc_characteristics.secm) {
794                         ret = device_create_file(&css->device,
795                                                  &dev_attr_cm_enable);
796                         if (ret)
797                                 goto out_device;
798                 }
799                 ret = device_register(&css->pseudo_subchannel->dev);
800                 if (ret)
801                         goto out_file;
802         }
803         ret = register_reboot_notifier(&css_reboot_notifier);
804         if (ret)
805                 goto out_pseudo;
806         css_init_done = 1;
807
808         /* Enable default isc for I/O subchannels. */
809         isc_register(IO_SCH_ISC);
810
811         for_each_subchannel(__init_channel_subsystem, NULL);
812         return 0;
813 out_pseudo:
814         device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
815 out_file:
816         device_remove_file(&channel_subsystems[i]->device,
817                            &dev_attr_cm_enable);
818 out_device:
819         device_unregister(&channel_subsystems[i]->device);
820 out_free_all:
821         kfree(channel_subsystems[i]->pseudo_subchannel->lock);
822         kfree(channel_subsystems[i]->pseudo_subchannel);
823 out_free:
824         kfree(channel_subsystems[i]);
825 out_unregister:
826         while (i > 0) {
827                 struct channel_subsystem *css;
828
829                 i--;
830                 css = channel_subsystems[i];
831                 device_unregister(&css->pseudo_subchannel->dev);
832                 if (css_chsc_characteristics.secm)
833                         device_remove_file(&css->device,
834                                            &dev_attr_cm_enable);
835                 device_unregister(&css->device);
836         }
837 out_bus:
838         bus_unregister(&css_bus_type);
839 out:
840         s390_unregister_crw_handler(CRW_RSC_CSS);
841         chsc_free_sei_area();
842         kfree(slow_subchannel_set);
843         printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
844                ret);
845         return ret;
846 }
847
848 int sch_is_pseudo_sch(struct subchannel *sch)
849 {
850         return sch == to_css(sch->dev.parent)->pseudo_subchannel;
851 }
852
853 /*
854  * find a driver for a subchannel. They identify by the subchannel
855  * type with the exception that the console subchannel driver has its own
856  * subchannel type although the device is an i/o subchannel
857  */
858 static int
859 css_bus_match (struct device *dev, struct device_driver *drv)
860 {
861         struct subchannel *sch = to_subchannel(dev);
862         struct css_driver *driver = to_cssdriver(drv);
863
864         if (sch->st == driver->subchannel_type)
865                 return 1;
866
867         return 0;
868 }
869
870 static int css_probe(struct device *dev)
871 {
872         struct subchannel *sch;
873         int ret;
874
875         sch = to_subchannel(dev);
876         sch->driver = to_cssdriver(dev->driver);
877         ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
878         if (ret)
879                 sch->driver = NULL;
880         return ret;
881 }
882
883 static int css_remove(struct device *dev)
884 {
885         struct subchannel *sch;
886         int ret;
887
888         sch = to_subchannel(dev);
889         ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
890         sch->driver = NULL;
891         return ret;
892 }
893
894 static void css_shutdown(struct device *dev)
895 {
896         struct subchannel *sch;
897
898         sch = to_subchannel(dev);
899         if (sch->driver && sch->driver->shutdown)
900                 sch->driver->shutdown(sch);
901 }
902
903 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
904 {
905         struct subchannel *sch = to_subchannel(dev);
906         int ret;
907
908         ret = add_uevent_var(env, "ST=%01X", sch->st);
909         if (ret)
910                 return ret;
911         ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
912         return ret;
913 }
914
915 struct bus_type css_bus_type = {
916         .name     = "css",
917         .match    = css_bus_match,
918         .probe    = css_probe,
919         .remove   = css_remove,
920         .shutdown = css_shutdown,
921         .uevent   = css_uevent,
922 };
923
924 /**
925  * css_driver_register - register a css driver
926  * @cdrv: css driver to register
927  *
928  * This is mainly a wrapper around driver_register that sets name
929  * and bus_type in the embedded struct device_driver correctly.
930  */
931 int css_driver_register(struct css_driver *cdrv)
932 {
933         cdrv->drv.name = cdrv->name;
934         cdrv->drv.bus = &css_bus_type;
935         cdrv->drv.owner = cdrv->owner;
936         return driver_register(&cdrv->drv);
937 }
938 EXPORT_SYMBOL_GPL(css_driver_register);
939
940 /**
941  * css_driver_unregister - unregister a css driver
942  * @cdrv: css driver to unregister
943  *
944  * This is a wrapper around driver_unregister.
945  */
946 void css_driver_unregister(struct css_driver *cdrv)
947 {
948         driver_unregister(&cdrv->drv);
949 }
950 EXPORT_SYMBOL_GPL(css_driver_unregister);
951
952 subsys_initcall(init_channel_subsystem);
953
954 MODULE_LICENSE("GPL");
955 EXPORT_SYMBOL(css_bus_type);