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