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