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