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