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