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