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