xref: /openbmc/linux/drivers/s390/cio/css.c (revision a2cce7a9)
1 /*
2  * driver for channel subsystem
3  *
4  * Copyright IBM Corp. 2002, 2010
5  *
6  * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7  *	      Cornelia Huck (cornelia.huck@de.ibm.com)
8  */
9 
10 #define KMSG_COMPONENT "cio"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/module.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/suspend.h>
21 #include <linux/proc_fs.h>
22 #include <asm/isc.h>
23 #include <asm/crw.h>
24 
25 #include "css.h"
26 #include "cio.h"
27 #include "cio_debug.h"
28 #include "ioasm.h"
29 #include "chsc.h"
30 #include "device.h"
31 #include "idset.h"
32 #include "chp.h"
33 
34 int css_init_done = 0;
35 int max_ssid;
36 
37 struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
38 static struct bus_type css_bus_type;
39 
40 int
41 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
42 {
43 	struct subchannel_id schid;
44 	int ret;
45 
46 	init_subchannel_id(&schid);
47 	ret = -ENODEV;
48 	do {
49 		do {
50 			ret = fn(schid, data);
51 			if (ret)
52 				break;
53 		} while (schid.sch_no++ < __MAX_SUBCHANNEL);
54 		schid.sch_no = 0;
55 	} while (schid.ssid++ < max_ssid);
56 	return ret;
57 }
58 
59 struct cb_data {
60 	void *data;
61 	struct idset *set;
62 	int (*fn_known_sch)(struct subchannel *, void *);
63 	int (*fn_unknown_sch)(struct subchannel_id, void *);
64 };
65 
66 static int call_fn_known_sch(struct device *dev, void *data)
67 {
68 	struct subchannel *sch = to_subchannel(dev);
69 	struct cb_data *cb = data;
70 	int rc = 0;
71 
72 	if (cb->set)
73 		idset_sch_del(cb->set, sch->schid);
74 	if (cb->fn_known_sch)
75 		rc = cb->fn_known_sch(sch, cb->data);
76 	return rc;
77 }
78 
79 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
80 {
81 	struct cb_data *cb = data;
82 	int rc = 0;
83 
84 	if (idset_sch_contains(cb->set, schid))
85 		rc = cb->fn_unknown_sch(schid, cb->data);
86 	return rc;
87 }
88 
89 static int call_fn_all_sch(struct subchannel_id schid, void *data)
90 {
91 	struct cb_data *cb = data;
92 	struct subchannel *sch;
93 	int rc = 0;
94 
95 	sch = get_subchannel_by_schid(schid);
96 	if (sch) {
97 		if (cb->fn_known_sch)
98 			rc = cb->fn_known_sch(sch, cb->data);
99 		put_device(&sch->dev);
100 	} else {
101 		if (cb->fn_unknown_sch)
102 			rc = cb->fn_unknown_sch(schid, cb->data);
103 	}
104 
105 	return rc;
106 }
107 
108 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
109 			       int (*fn_unknown)(struct subchannel_id,
110 			       void *), void *data)
111 {
112 	struct cb_data cb;
113 	int rc;
114 
115 	cb.data = data;
116 	cb.fn_known_sch = fn_known;
117 	cb.fn_unknown_sch = fn_unknown;
118 
119 	if (fn_known && !fn_unknown) {
120 		/* Skip idset allocation in case of known-only loop. */
121 		cb.set = NULL;
122 		return bus_for_each_dev(&css_bus_type, NULL, &cb,
123 					call_fn_known_sch);
124 	}
125 
126 	cb.set = idset_sch_new();
127 	if (!cb.set)
128 		/* fall back to brute force scanning in case of oom */
129 		return for_each_subchannel(call_fn_all_sch, &cb);
130 
131 	idset_fill(cb.set);
132 
133 	/* Process registered subchannels. */
134 	rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
135 	if (rc)
136 		goto out;
137 	/* Process unregistered subchannels. */
138 	if (fn_unknown)
139 		rc = for_each_subchannel(call_fn_unknown_sch, &cb);
140 out:
141 	idset_free(cb.set);
142 
143 	return rc;
144 }
145 
146 static void css_sch_todo(struct work_struct *work);
147 
148 static int css_sch_create_locks(struct subchannel *sch)
149 {
150 	sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
151 	if (!sch->lock)
152 		return -ENOMEM;
153 
154 	spin_lock_init(sch->lock);
155 	mutex_init(&sch->reg_mutex);
156 
157 	return 0;
158 }
159 
160 static void css_subchannel_release(struct device *dev)
161 {
162 	struct subchannel *sch = to_subchannel(dev);
163 
164 	sch->config.intparm = 0;
165 	cio_commit_config(sch);
166 	kfree(sch->lock);
167 	kfree(sch);
168 }
169 
170 struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
171 {
172 	struct subchannel *sch;
173 	int ret;
174 
175 	sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
176 	if (!sch)
177 		return ERR_PTR(-ENOMEM);
178 
179 	ret = cio_validate_subchannel(sch, schid);
180 	if (ret < 0)
181 		goto err;
182 
183 	ret = css_sch_create_locks(sch);
184 	if (ret)
185 		goto err;
186 
187 	INIT_WORK(&sch->todo_work, css_sch_todo);
188 	sch->dev.release = &css_subchannel_release;
189 	device_initialize(&sch->dev);
190 	return sch;
191 
192 err:
193 	kfree(sch);
194 	return ERR_PTR(ret);
195 }
196 
197 static int css_sch_device_register(struct subchannel *sch)
198 {
199 	int ret;
200 
201 	mutex_lock(&sch->reg_mutex);
202 	dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
203 		     sch->schid.sch_no);
204 	ret = device_add(&sch->dev);
205 	mutex_unlock(&sch->reg_mutex);
206 	return ret;
207 }
208 
209 /**
210  * css_sch_device_unregister - unregister a subchannel
211  * @sch: subchannel to be unregistered
212  */
213 void css_sch_device_unregister(struct subchannel *sch)
214 {
215 	mutex_lock(&sch->reg_mutex);
216 	if (device_is_registered(&sch->dev))
217 		device_unregister(&sch->dev);
218 	mutex_unlock(&sch->reg_mutex);
219 }
220 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
221 
222 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
223 {
224 	int i;
225 	int mask;
226 
227 	memset(ssd, 0, sizeof(struct chsc_ssd_info));
228 	ssd->path_mask = pmcw->pim;
229 	for (i = 0; i < 8; i++) {
230 		mask = 0x80 >> i;
231 		if (pmcw->pim & mask) {
232 			chp_id_init(&ssd->chpid[i]);
233 			ssd->chpid[i].id = pmcw->chpid[i];
234 		}
235 	}
236 }
237 
238 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
239 {
240 	int i;
241 	int mask;
242 
243 	for (i = 0; i < 8; i++) {
244 		mask = 0x80 >> i;
245 		if (ssd->path_mask & mask)
246 			if (!chp_is_registered(ssd->chpid[i]))
247 				chp_new(ssd->chpid[i]);
248 	}
249 }
250 
251 void css_update_ssd_info(struct subchannel *sch)
252 {
253 	int ret;
254 
255 	ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
256 	if (ret)
257 		ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
258 
259 	ssd_register_chpids(&sch->ssd_info);
260 }
261 
262 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
263 			 char *buf)
264 {
265 	struct subchannel *sch = to_subchannel(dev);
266 
267 	return sprintf(buf, "%01x\n", sch->st);
268 }
269 
270 static DEVICE_ATTR(type, 0444, type_show, NULL);
271 
272 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
273 			     char *buf)
274 {
275 	struct subchannel *sch = to_subchannel(dev);
276 
277 	return sprintf(buf, "css:t%01X\n", sch->st);
278 }
279 
280 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
281 
282 static struct attribute *subch_attrs[] = {
283 	&dev_attr_type.attr,
284 	&dev_attr_modalias.attr,
285 	NULL,
286 };
287 
288 static struct attribute_group subch_attr_group = {
289 	.attrs = subch_attrs,
290 };
291 
292 static const struct attribute_group *default_subch_attr_groups[] = {
293 	&subch_attr_group,
294 	NULL,
295 };
296 
297 int css_register_subchannel(struct subchannel *sch)
298 {
299 	int ret;
300 
301 	/* Initialize the subchannel structure */
302 	sch->dev.parent = &channel_subsystems[0]->device;
303 	sch->dev.bus = &css_bus_type;
304 	sch->dev.groups = default_subch_attr_groups;
305 	/*
306 	 * We don't want to generate uevents for I/O subchannels that don't
307 	 * have a working ccw device behind them since they will be
308 	 * unregistered before they can be used anyway, so we delay the add
309 	 * uevent until after device recognition was successful.
310 	 * Note that we suppress the uevent for all subchannel types;
311 	 * the subchannel driver can decide itself when it wants to inform
312 	 * userspace of its existence.
313 	 */
314 	dev_set_uevent_suppress(&sch->dev, 1);
315 	css_update_ssd_info(sch);
316 	/* make it known to the system */
317 	ret = css_sch_device_register(sch);
318 	if (ret) {
319 		CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
320 			      sch->schid.ssid, sch->schid.sch_no, ret);
321 		return ret;
322 	}
323 	if (!sch->driver) {
324 		/*
325 		 * No driver matched. Generate the uevent now so that
326 		 * a fitting driver module may be loaded based on the
327 		 * modalias.
328 		 */
329 		dev_set_uevent_suppress(&sch->dev, 0);
330 		kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
331 	}
332 	return ret;
333 }
334 
335 static int css_probe_device(struct subchannel_id schid)
336 {
337 	struct subchannel *sch;
338 	int ret;
339 
340 	sch = css_alloc_subchannel(schid);
341 	if (IS_ERR(sch))
342 		return PTR_ERR(sch);
343 
344 	ret = css_register_subchannel(sch);
345 	if (ret)
346 		put_device(&sch->dev);
347 
348 	return ret;
349 }
350 
351 static int
352 check_subchannel(struct device * dev, void * data)
353 {
354 	struct subchannel *sch;
355 	struct subchannel_id *schid = data;
356 
357 	sch = to_subchannel(dev);
358 	return schid_equal(&sch->schid, schid);
359 }
360 
361 struct subchannel *
362 get_subchannel_by_schid(struct subchannel_id schid)
363 {
364 	struct device *dev;
365 
366 	dev = bus_find_device(&css_bus_type, NULL,
367 			      &schid, check_subchannel);
368 
369 	return dev ? to_subchannel(dev) : NULL;
370 }
371 
372 /**
373  * css_sch_is_valid() - check if a subchannel is valid
374  * @schib: subchannel information block for the subchannel
375  */
376 int css_sch_is_valid(struct schib *schib)
377 {
378 	if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
379 		return 0;
380 	if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
381 		return 0;
382 	return 1;
383 }
384 EXPORT_SYMBOL_GPL(css_sch_is_valid);
385 
386 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
387 {
388 	struct schib schib;
389 
390 	if (!slow) {
391 		/* Will be done on the slow path. */
392 		return -EAGAIN;
393 	}
394 	if (stsch_err(schid, &schib)) {
395 		/* Subchannel is not provided. */
396 		return -ENXIO;
397 	}
398 	if (!css_sch_is_valid(&schib)) {
399 		/* Unusable - ignore. */
400 		return 0;
401 	}
402 	CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
403 		      schid.sch_no);
404 
405 	return css_probe_device(schid);
406 }
407 
408 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
409 {
410 	int ret = 0;
411 
412 	if (sch->driver) {
413 		if (sch->driver->sch_event)
414 			ret = sch->driver->sch_event(sch, slow);
415 		else
416 			dev_dbg(&sch->dev,
417 				"Got subchannel machine check but "
418 				"no sch_event handler provided.\n");
419 	}
420 	if (ret != 0 && ret != -EAGAIN) {
421 		CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
422 			      sch->schid.ssid, sch->schid.sch_no, ret);
423 	}
424 	return ret;
425 }
426 
427 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
428 {
429 	struct subchannel *sch;
430 	int ret;
431 
432 	sch = get_subchannel_by_schid(schid);
433 	if (sch) {
434 		ret = css_evaluate_known_subchannel(sch, slow);
435 		put_device(&sch->dev);
436 	} else
437 		ret = css_evaluate_new_subchannel(schid, slow);
438 	if (ret == -EAGAIN)
439 		css_schedule_eval(schid);
440 }
441 
442 /**
443  * css_sched_sch_todo - schedule a subchannel operation
444  * @sch: subchannel
445  * @todo: todo
446  *
447  * Schedule the operation identified by @todo to be performed on the slow path
448  * workqueue. Do nothing if another operation with higher priority is already
449  * scheduled. Needs to be called with subchannel lock held.
450  */
451 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
452 {
453 	CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
454 		      sch->schid.ssid, sch->schid.sch_no, todo);
455 	if (sch->todo >= todo)
456 		return;
457 	/* Get workqueue ref. */
458 	if (!get_device(&sch->dev))
459 		return;
460 	sch->todo = todo;
461 	if (!queue_work(cio_work_q, &sch->todo_work)) {
462 		/* Already queued, release workqueue ref. */
463 		put_device(&sch->dev);
464 	}
465 }
466 EXPORT_SYMBOL_GPL(css_sched_sch_todo);
467 
468 static void css_sch_todo(struct work_struct *work)
469 {
470 	struct subchannel *sch;
471 	enum sch_todo todo;
472 	int ret;
473 
474 	sch = container_of(work, struct subchannel, todo_work);
475 	/* Find out todo. */
476 	spin_lock_irq(sch->lock);
477 	todo = sch->todo;
478 	CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
479 		      sch->schid.sch_no, todo);
480 	sch->todo = SCH_TODO_NOTHING;
481 	spin_unlock_irq(sch->lock);
482 	/* Perform todo. */
483 	switch (todo) {
484 	case SCH_TODO_NOTHING:
485 		break;
486 	case SCH_TODO_EVAL:
487 		ret = css_evaluate_known_subchannel(sch, 1);
488 		if (ret == -EAGAIN) {
489 			spin_lock_irq(sch->lock);
490 			css_sched_sch_todo(sch, todo);
491 			spin_unlock_irq(sch->lock);
492 		}
493 		break;
494 	case SCH_TODO_UNREG:
495 		css_sch_device_unregister(sch);
496 		break;
497 	}
498 	/* Release workqueue ref. */
499 	put_device(&sch->dev);
500 }
501 
502 static struct idset *slow_subchannel_set;
503 static spinlock_t slow_subchannel_lock;
504 static wait_queue_head_t css_eval_wq;
505 static atomic_t css_eval_scheduled;
506 
507 static int __init slow_subchannel_init(void)
508 {
509 	spin_lock_init(&slow_subchannel_lock);
510 	atomic_set(&css_eval_scheduled, 0);
511 	init_waitqueue_head(&css_eval_wq);
512 	slow_subchannel_set = idset_sch_new();
513 	if (!slow_subchannel_set) {
514 		CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
515 		return -ENOMEM;
516 	}
517 	return 0;
518 }
519 
520 static int slow_eval_known_fn(struct subchannel *sch, void *data)
521 {
522 	int eval;
523 	int rc;
524 
525 	spin_lock_irq(&slow_subchannel_lock);
526 	eval = idset_sch_contains(slow_subchannel_set, sch->schid);
527 	idset_sch_del(slow_subchannel_set, sch->schid);
528 	spin_unlock_irq(&slow_subchannel_lock);
529 	if (eval) {
530 		rc = css_evaluate_known_subchannel(sch, 1);
531 		if (rc == -EAGAIN)
532 			css_schedule_eval(sch->schid);
533 	}
534 	return 0;
535 }
536 
537 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
538 {
539 	int eval;
540 	int rc = 0;
541 
542 	spin_lock_irq(&slow_subchannel_lock);
543 	eval = idset_sch_contains(slow_subchannel_set, schid);
544 	idset_sch_del(slow_subchannel_set, schid);
545 	spin_unlock_irq(&slow_subchannel_lock);
546 	if (eval) {
547 		rc = css_evaluate_new_subchannel(schid, 1);
548 		switch (rc) {
549 		case -EAGAIN:
550 			css_schedule_eval(schid);
551 			rc = 0;
552 			break;
553 		case -ENXIO:
554 		case -ENOMEM:
555 		case -EIO:
556 			/* These should abort looping */
557 			spin_lock_irq(&slow_subchannel_lock);
558 			idset_sch_del_subseq(slow_subchannel_set, schid);
559 			spin_unlock_irq(&slow_subchannel_lock);
560 			break;
561 		default:
562 			rc = 0;
563 		}
564 		/* Allow scheduling here since the containing loop might
565 		 * take a while.  */
566 		cond_resched();
567 	}
568 	return rc;
569 }
570 
571 static void css_slow_path_func(struct work_struct *unused)
572 {
573 	unsigned long flags;
574 
575 	CIO_TRACE_EVENT(4, "slowpath");
576 	for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
577 				   NULL);
578 	spin_lock_irqsave(&slow_subchannel_lock, flags);
579 	if (idset_is_empty(slow_subchannel_set)) {
580 		atomic_set(&css_eval_scheduled, 0);
581 		wake_up(&css_eval_wq);
582 	}
583 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
584 }
585 
586 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
587 struct workqueue_struct *cio_work_q;
588 
589 void css_schedule_eval(struct subchannel_id schid)
590 {
591 	unsigned long flags;
592 
593 	spin_lock_irqsave(&slow_subchannel_lock, flags);
594 	idset_sch_add(slow_subchannel_set, schid);
595 	atomic_set(&css_eval_scheduled, 1);
596 	queue_delayed_work(cio_work_q, &slow_path_work, 0);
597 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
598 }
599 
600 void css_schedule_eval_all(void)
601 {
602 	unsigned long flags;
603 
604 	spin_lock_irqsave(&slow_subchannel_lock, flags);
605 	idset_fill(slow_subchannel_set);
606 	atomic_set(&css_eval_scheduled, 1);
607 	queue_delayed_work(cio_work_q, &slow_path_work, 0);
608 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
609 }
610 
611 static int __unset_registered(struct device *dev, void *data)
612 {
613 	struct idset *set = data;
614 	struct subchannel *sch = to_subchannel(dev);
615 
616 	idset_sch_del(set, sch->schid);
617 	return 0;
618 }
619 
620 void css_schedule_eval_all_unreg(unsigned long delay)
621 {
622 	unsigned long flags;
623 	struct idset *unreg_set;
624 
625 	/* Find unregistered subchannels. */
626 	unreg_set = idset_sch_new();
627 	if (!unreg_set) {
628 		/* Fallback. */
629 		css_schedule_eval_all();
630 		return;
631 	}
632 	idset_fill(unreg_set);
633 	bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
634 	/* Apply to slow_subchannel_set. */
635 	spin_lock_irqsave(&slow_subchannel_lock, flags);
636 	idset_add_set(slow_subchannel_set, unreg_set);
637 	atomic_set(&css_eval_scheduled, 1);
638 	queue_delayed_work(cio_work_q, &slow_path_work, delay);
639 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
640 	idset_free(unreg_set);
641 }
642 
643 void css_wait_for_slow_path(void)
644 {
645 	flush_workqueue(cio_work_q);
646 }
647 
648 /* Schedule reprobing of all unregistered subchannels. */
649 void css_schedule_reprobe(void)
650 {
651 	/* Schedule with a delay to allow merging of subsequent calls. */
652 	css_schedule_eval_all_unreg(1 * HZ);
653 }
654 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
655 
656 /*
657  * Called from the machine check handler for subchannel report words.
658  */
659 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
660 {
661 	struct subchannel_id mchk_schid;
662 	struct subchannel *sch;
663 
664 	if (overflow) {
665 		css_schedule_eval_all();
666 		return;
667 	}
668 	CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
669 		      "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
670 		      crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
671 		      crw0->erc, crw0->rsid);
672 	if (crw1)
673 		CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
674 			      "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
675 			      crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
676 			      crw1->anc, crw1->erc, crw1->rsid);
677 	init_subchannel_id(&mchk_schid);
678 	mchk_schid.sch_no = crw0->rsid;
679 	if (crw1)
680 		mchk_schid.ssid = (crw1->rsid >> 4) & 3;
681 
682 	if (crw0->erc == CRW_ERC_PMOD) {
683 		sch = get_subchannel_by_schid(mchk_schid);
684 		if (sch) {
685 			css_update_ssd_info(sch);
686 			put_device(&sch->dev);
687 		}
688 	}
689 	/*
690 	 * Since we are always presented with IPI in the CRW, we have to
691 	 * use stsch() to find out if the subchannel in question has come
692 	 * or gone.
693 	 */
694 	css_evaluate_subchannel(mchk_schid, 0);
695 }
696 
697 static void __init
698 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
699 {
700 	struct cpuid cpu_id;
701 
702 	if (css_general_characteristics.mcss) {
703 		css->global_pgid.pgid_high.ext_cssid.version = 0x80;
704 		css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
705 	} else {
706 #ifdef CONFIG_SMP
707 		css->global_pgid.pgid_high.cpu_addr = stap();
708 #else
709 		css->global_pgid.pgid_high.cpu_addr = 0;
710 #endif
711 	}
712 	get_cpu_id(&cpu_id);
713 	css->global_pgid.cpu_id = cpu_id.ident;
714 	css->global_pgid.cpu_model = cpu_id.machine;
715 	css->global_pgid.tod_high = tod_high;
716 
717 }
718 
719 static void
720 channel_subsystem_release(struct device *dev)
721 {
722 	struct channel_subsystem *css;
723 
724 	css = to_css(dev);
725 	mutex_destroy(&css->mutex);
726 	if (css->pseudo_subchannel) {
727 		/* Implies that it has been generated but never registered. */
728 		css_subchannel_release(&css->pseudo_subchannel->dev);
729 		css->pseudo_subchannel = NULL;
730 	}
731 	kfree(css);
732 }
733 
734 static ssize_t
735 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
736 		   char *buf)
737 {
738 	struct channel_subsystem *css = to_css(dev);
739 	int ret;
740 
741 	if (!css)
742 		return 0;
743 	mutex_lock(&css->mutex);
744 	ret = sprintf(buf, "%x\n", css->cm_enabled);
745 	mutex_unlock(&css->mutex);
746 	return ret;
747 }
748 
749 static ssize_t
750 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
751 		    const char *buf, size_t count)
752 {
753 	struct channel_subsystem *css = to_css(dev);
754 	int ret;
755 	unsigned long val;
756 
757 	ret = kstrtoul(buf, 16, &val);
758 	if (ret)
759 		return ret;
760 	mutex_lock(&css->mutex);
761 	switch (val) {
762 	case 0:
763 		ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
764 		break;
765 	case 1:
766 		ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
767 		break;
768 	default:
769 		ret = -EINVAL;
770 	}
771 	mutex_unlock(&css->mutex);
772 	return ret < 0 ? ret : count;
773 }
774 
775 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
776 
777 static int __init setup_css(int nr)
778 {
779 	u32 tod_high;
780 	int ret;
781 	struct channel_subsystem *css;
782 
783 	css = channel_subsystems[nr];
784 	memset(css, 0, sizeof(struct channel_subsystem));
785 	css->pseudo_subchannel =
786 		kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
787 	if (!css->pseudo_subchannel)
788 		return -ENOMEM;
789 	css->pseudo_subchannel->dev.parent = &css->device;
790 	css->pseudo_subchannel->dev.release = css_subchannel_release;
791 	dev_set_name(&css->pseudo_subchannel->dev, "defunct");
792 	mutex_init(&css->pseudo_subchannel->reg_mutex);
793 	ret = css_sch_create_locks(css->pseudo_subchannel);
794 	if (ret) {
795 		kfree(css->pseudo_subchannel);
796 		return ret;
797 	}
798 	mutex_init(&css->mutex);
799 	css->valid = 1;
800 	css->cssid = nr;
801 	dev_set_name(&css->device, "css%x", nr);
802 	css->device.release = channel_subsystem_release;
803 	tod_high = (u32) (get_tod_clock() >> 32);
804 	css_generate_pgid(css, tod_high);
805 	return 0;
806 }
807 
808 static int css_reboot_event(struct notifier_block *this,
809 			    unsigned long event,
810 			    void *ptr)
811 {
812 	int ret, i;
813 
814 	ret = NOTIFY_DONE;
815 	for (i = 0; i <= __MAX_CSSID; i++) {
816 		struct channel_subsystem *css;
817 
818 		css = channel_subsystems[i];
819 		mutex_lock(&css->mutex);
820 		if (css->cm_enabled)
821 			if (chsc_secm(css, 0))
822 				ret = NOTIFY_BAD;
823 		mutex_unlock(&css->mutex);
824 	}
825 
826 	return ret;
827 }
828 
829 static struct notifier_block css_reboot_notifier = {
830 	.notifier_call = css_reboot_event,
831 };
832 
833 /*
834  * Since the css devices are neither on a bus nor have a class
835  * nor have a special device type, we cannot stop/restart channel
836  * path measurements via the normal suspend/resume callbacks, but have
837  * to use notifiers.
838  */
839 static int css_power_event(struct notifier_block *this, unsigned long event,
840 			   void *ptr)
841 {
842 	int ret, i;
843 
844 	switch (event) {
845 	case PM_HIBERNATION_PREPARE:
846 	case PM_SUSPEND_PREPARE:
847 		ret = NOTIFY_DONE;
848 		for (i = 0; i <= __MAX_CSSID; i++) {
849 			struct channel_subsystem *css;
850 
851 			css = channel_subsystems[i];
852 			mutex_lock(&css->mutex);
853 			if (!css->cm_enabled) {
854 				mutex_unlock(&css->mutex);
855 				continue;
856 			}
857 			ret = __chsc_do_secm(css, 0);
858 			ret = notifier_from_errno(ret);
859 			mutex_unlock(&css->mutex);
860 		}
861 		break;
862 	case PM_POST_HIBERNATION:
863 	case PM_POST_SUSPEND:
864 		ret = NOTIFY_DONE;
865 		for (i = 0; i <= __MAX_CSSID; i++) {
866 			struct channel_subsystem *css;
867 
868 			css = channel_subsystems[i];
869 			mutex_lock(&css->mutex);
870 			if (!css->cm_enabled) {
871 				mutex_unlock(&css->mutex);
872 				continue;
873 			}
874 			ret = __chsc_do_secm(css, 1);
875 			ret = notifier_from_errno(ret);
876 			mutex_unlock(&css->mutex);
877 		}
878 		/* search for subchannels, which appeared during hibernation */
879 		css_schedule_reprobe();
880 		break;
881 	default:
882 		ret = NOTIFY_DONE;
883 	}
884 	return ret;
885 
886 }
887 static struct notifier_block css_power_notifier = {
888 	.notifier_call = css_power_event,
889 };
890 
891 /*
892  * Now that the driver core is running, we can setup our channel subsystem.
893  * The struct subchannel's are created during probing.
894  */
895 static int __init css_bus_init(void)
896 {
897 	int ret, i;
898 
899 	ret = chsc_init();
900 	if (ret)
901 		return ret;
902 
903 	chsc_determine_css_characteristics();
904 	/* Try to enable MSS. */
905 	ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
906 	if (ret)
907 		max_ssid = 0;
908 	else /* Success. */
909 		max_ssid = __MAX_SSID;
910 
911 	ret = slow_subchannel_init();
912 	if (ret)
913 		goto out;
914 
915 	ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
916 	if (ret)
917 		goto out;
918 
919 	if ((ret = bus_register(&css_bus_type)))
920 		goto out;
921 
922 	/* Setup css structure. */
923 	for (i = 0; i <= __MAX_CSSID; i++) {
924 		struct channel_subsystem *css;
925 
926 		css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
927 		if (!css) {
928 			ret = -ENOMEM;
929 			goto out_unregister;
930 		}
931 		channel_subsystems[i] = css;
932 		ret = setup_css(i);
933 		if (ret) {
934 			kfree(channel_subsystems[i]);
935 			goto out_unregister;
936 		}
937 		ret = device_register(&css->device);
938 		if (ret) {
939 			put_device(&css->device);
940 			goto out_unregister;
941 		}
942 		if (css_chsc_characteristics.secm) {
943 			ret = device_create_file(&css->device,
944 						 &dev_attr_cm_enable);
945 			if (ret)
946 				goto out_device;
947 		}
948 		ret = device_register(&css->pseudo_subchannel->dev);
949 		if (ret) {
950 			put_device(&css->pseudo_subchannel->dev);
951 			goto out_file;
952 		}
953 	}
954 	ret = register_reboot_notifier(&css_reboot_notifier);
955 	if (ret)
956 		goto out_unregister;
957 	ret = register_pm_notifier(&css_power_notifier);
958 	if (ret) {
959 		unregister_reboot_notifier(&css_reboot_notifier);
960 		goto out_unregister;
961 	}
962 	css_init_done = 1;
963 
964 	/* Enable default isc for I/O subchannels. */
965 	isc_register(IO_SCH_ISC);
966 
967 	return 0;
968 out_file:
969 	if (css_chsc_characteristics.secm)
970 		device_remove_file(&channel_subsystems[i]->device,
971 				   &dev_attr_cm_enable);
972 out_device:
973 	device_unregister(&channel_subsystems[i]->device);
974 out_unregister:
975 	while (i > 0) {
976 		struct channel_subsystem *css;
977 
978 		i--;
979 		css = channel_subsystems[i];
980 		device_unregister(&css->pseudo_subchannel->dev);
981 		css->pseudo_subchannel = NULL;
982 		if (css_chsc_characteristics.secm)
983 			device_remove_file(&css->device,
984 					   &dev_attr_cm_enable);
985 		device_unregister(&css->device);
986 	}
987 	bus_unregister(&css_bus_type);
988 out:
989 	crw_unregister_handler(CRW_RSC_SCH);
990 	idset_free(slow_subchannel_set);
991 	chsc_init_cleanup();
992 	pr_alert("The CSS device driver initialization failed with "
993 		 "errno=%d\n", ret);
994 	return ret;
995 }
996 
997 static void __init css_bus_cleanup(void)
998 {
999 	struct channel_subsystem *css;
1000 	int i;
1001 
1002 	for (i = 0; i <= __MAX_CSSID; i++) {
1003 		css = channel_subsystems[i];
1004 		device_unregister(&css->pseudo_subchannel->dev);
1005 		css->pseudo_subchannel = NULL;
1006 		if (css_chsc_characteristics.secm)
1007 			device_remove_file(&css->device, &dev_attr_cm_enable);
1008 		device_unregister(&css->device);
1009 	}
1010 	bus_unregister(&css_bus_type);
1011 	crw_unregister_handler(CRW_RSC_SCH);
1012 	idset_free(slow_subchannel_set);
1013 	chsc_init_cleanup();
1014 	isc_unregister(IO_SCH_ISC);
1015 }
1016 
1017 static int __init channel_subsystem_init(void)
1018 {
1019 	int ret;
1020 
1021 	ret = css_bus_init();
1022 	if (ret)
1023 		return ret;
1024 	cio_work_q = create_singlethread_workqueue("cio");
1025 	if (!cio_work_q) {
1026 		ret = -ENOMEM;
1027 		goto out_bus;
1028 	}
1029 	ret = io_subchannel_init();
1030 	if (ret)
1031 		goto out_wq;
1032 
1033 	return ret;
1034 out_wq:
1035 	destroy_workqueue(cio_work_q);
1036 out_bus:
1037 	css_bus_cleanup();
1038 	return ret;
1039 }
1040 subsys_initcall(channel_subsystem_init);
1041 
1042 static int css_settle(struct device_driver *drv, void *unused)
1043 {
1044 	struct css_driver *cssdrv = to_cssdriver(drv);
1045 
1046 	if (cssdrv->settle)
1047 		return cssdrv->settle();
1048 	return 0;
1049 }
1050 
1051 int css_complete_work(void)
1052 {
1053 	int ret;
1054 
1055 	/* Wait for the evaluation of subchannels to finish. */
1056 	ret = wait_event_interruptible(css_eval_wq,
1057 				       atomic_read(&css_eval_scheduled) == 0);
1058 	if (ret)
1059 		return -EINTR;
1060 	flush_workqueue(cio_work_q);
1061 	/* Wait for the subchannel type specific initialization to finish */
1062 	return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1063 }
1064 
1065 
1066 /*
1067  * Wait for the initialization of devices to finish, to make sure we are
1068  * done with our setup if the search for the root device starts.
1069  */
1070 static int __init channel_subsystem_init_sync(void)
1071 {
1072 	/* Register subchannels which are already in use. */
1073 	cio_register_early_subchannels();
1074 	/* Start initial subchannel evaluation. */
1075 	css_schedule_eval_all();
1076 	css_complete_work();
1077 	return 0;
1078 }
1079 subsys_initcall_sync(channel_subsystem_init_sync);
1080 
1081 void channel_subsystem_reinit(void)
1082 {
1083 	struct channel_path *chp;
1084 	struct chp_id chpid;
1085 
1086 	chsc_enable_facility(CHSC_SDA_OC_MSS);
1087 	chp_id_for_each(&chpid) {
1088 		chp = chpid_to_chp(chpid);
1089 		if (chp)
1090 			chp_update_desc(chp);
1091 	}
1092 }
1093 
1094 #ifdef CONFIG_PROC_FS
1095 static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1096 				size_t count, loff_t *ppos)
1097 {
1098 	int ret;
1099 
1100 	/* Handle pending CRW's. */
1101 	crw_wait_for_channel_report();
1102 	ret = css_complete_work();
1103 
1104 	return ret ? ret : count;
1105 }
1106 
1107 static const struct file_operations cio_settle_proc_fops = {
1108 	.open = nonseekable_open,
1109 	.write = cio_settle_write,
1110 	.llseek = no_llseek,
1111 };
1112 
1113 static int __init cio_settle_init(void)
1114 {
1115 	struct proc_dir_entry *entry;
1116 
1117 	entry = proc_create("cio_settle", S_IWUSR, NULL,
1118 			    &cio_settle_proc_fops);
1119 	if (!entry)
1120 		return -ENOMEM;
1121 	return 0;
1122 }
1123 device_initcall(cio_settle_init);
1124 #endif /*CONFIG_PROC_FS*/
1125 
1126 int sch_is_pseudo_sch(struct subchannel *sch)
1127 {
1128 	return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1129 }
1130 
1131 static int css_bus_match(struct device *dev, struct device_driver *drv)
1132 {
1133 	struct subchannel *sch = to_subchannel(dev);
1134 	struct css_driver *driver = to_cssdriver(drv);
1135 	struct css_device_id *id;
1136 
1137 	for (id = driver->subchannel_type; id->match_flags; id++) {
1138 		if (sch->st == id->type)
1139 			return 1;
1140 	}
1141 
1142 	return 0;
1143 }
1144 
1145 static int css_probe(struct device *dev)
1146 {
1147 	struct subchannel *sch;
1148 	int ret;
1149 
1150 	sch = to_subchannel(dev);
1151 	sch->driver = to_cssdriver(dev->driver);
1152 	ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1153 	if (ret)
1154 		sch->driver = NULL;
1155 	return ret;
1156 }
1157 
1158 static int css_remove(struct device *dev)
1159 {
1160 	struct subchannel *sch;
1161 	int ret;
1162 
1163 	sch = to_subchannel(dev);
1164 	ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1165 	sch->driver = NULL;
1166 	return ret;
1167 }
1168 
1169 static void css_shutdown(struct device *dev)
1170 {
1171 	struct subchannel *sch;
1172 
1173 	sch = to_subchannel(dev);
1174 	if (sch->driver && sch->driver->shutdown)
1175 		sch->driver->shutdown(sch);
1176 }
1177 
1178 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1179 {
1180 	struct subchannel *sch = to_subchannel(dev);
1181 	int ret;
1182 
1183 	ret = add_uevent_var(env, "ST=%01X", sch->st);
1184 	if (ret)
1185 		return ret;
1186 	ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1187 	return ret;
1188 }
1189 
1190 static int css_pm_prepare(struct device *dev)
1191 {
1192 	struct subchannel *sch = to_subchannel(dev);
1193 	struct css_driver *drv;
1194 
1195 	if (mutex_is_locked(&sch->reg_mutex))
1196 		return -EAGAIN;
1197 	if (!sch->dev.driver)
1198 		return 0;
1199 	drv = to_cssdriver(sch->dev.driver);
1200 	/* Notify drivers that they may not register children. */
1201 	return drv->prepare ? drv->prepare(sch) : 0;
1202 }
1203 
1204 static void css_pm_complete(struct device *dev)
1205 {
1206 	struct subchannel *sch = to_subchannel(dev);
1207 	struct css_driver *drv;
1208 
1209 	if (!sch->dev.driver)
1210 		return;
1211 	drv = to_cssdriver(sch->dev.driver);
1212 	if (drv->complete)
1213 		drv->complete(sch);
1214 }
1215 
1216 static int css_pm_freeze(struct device *dev)
1217 {
1218 	struct subchannel *sch = to_subchannel(dev);
1219 	struct css_driver *drv;
1220 
1221 	if (!sch->dev.driver)
1222 		return 0;
1223 	drv = to_cssdriver(sch->dev.driver);
1224 	return drv->freeze ? drv->freeze(sch) : 0;
1225 }
1226 
1227 static int css_pm_thaw(struct device *dev)
1228 {
1229 	struct subchannel *sch = to_subchannel(dev);
1230 	struct css_driver *drv;
1231 
1232 	if (!sch->dev.driver)
1233 		return 0;
1234 	drv = to_cssdriver(sch->dev.driver);
1235 	return drv->thaw ? drv->thaw(sch) : 0;
1236 }
1237 
1238 static int css_pm_restore(struct device *dev)
1239 {
1240 	struct subchannel *sch = to_subchannel(dev);
1241 	struct css_driver *drv;
1242 
1243 	css_update_ssd_info(sch);
1244 	if (!sch->dev.driver)
1245 		return 0;
1246 	drv = to_cssdriver(sch->dev.driver);
1247 	return drv->restore ? drv->restore(sch) : 0;
1248 }
1249 
1250 static const struct dev_pm_ops css_pm_ops = {
1251 	.prepare = css_pm_prepare,
1252 	.complete = css_pm_complete,
1253 	.freeze = css_pm_freeze,
1254 	.thaw = css_pm_thaw,
1255 	.restore = css_pm_restore,
1256 };
1257 
1258 static struct bus_type css_bus_type = {
1259 	.name     = "css",
1260 	.match    = css_bus_match,
1261 	.probe    = css_probe,
1262 	.remove   = css_remove,
1263 	.shutdown = css_shutdown,
1264 	.uevent   = css_uevent,
1265 	.pm = &css_pm_ops,
1266 };
1267 
1268 /**
1269  * css_driver_register - register a css driver
1270  * @cdrv: css driver to register
1271  *
1272  * This is mainly a wrapper around driver_register that sets name
1273  * and bus_type in the embedded struct device_driver correctly.
1274  */
1275 int css_driver_register(struct css_driver *cdrv)
1276 {
1277 	cdrv->drv.bus = &css_bus_type;
1278 	return driver_register(&cdrv->drv);
1279 }
1280 EXPORT_SYMBOL_GPL(css_driver_register);
1281 
1282 /**
1283  * css_driver_unregister - unregister a css driver
1284  * @cdrv: css driver to unregister
1285  *
1286  * This is a wrapper around driver_unregister.
1287  */
1288 void css_driver_unregister(struct css_driver *cdrv)
1289 {
1290 	driver_unregister(&cdrv->drv);
1291 }
1292 EXPORT_SYMBOL_GPL(css_driver_unregister);
1293 
1294 MODULE_LICENSE("GPL");
1295