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