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