xref: /linux/drivers/s390/cio/css.c (revision 6e8331ac6973435b1e7604c30f2ad394035b46e1)
1 /*
2  *  drivers/s390/cio/css.c
3  *  driver for channel subsystem
4  *
5  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6  *			 IBM Corporation
7  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
8  *		 Cornelia Huck (cornelia.huck@de.ibm.com)
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/list.h>
16 
17 #include "css.h"
18 #include "cio.h"
19 #include "cio_debug.h"
20 #include "ioasm.h"
21 #include "chsc.h"
22 #include "device.h"
23 
24 int need_rescan = 0;
25 int css_init_done = 0;
26 static int need_reprobe = 0;
27 static int max_ssid = 0;
28 
29 struct channel_subsystem *css[__MAX_CSSID + 1];
30 
31 int css_characteristics_avail = 0;
32 
33 inline int
34 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
35 {
36 	struct subchannel_id schid;
37 	int ret;
38 
39 	init_subchannel_id(&schid);
40 	ret = -ENODEV;
41 	do {
42 		do {
43 			ret = fn(schid, data);
44 			if (ret)
45 				break;
46 		} while (schid.sch_no++ < __MAX_SUBCHANNEL);
47 		schid.sch_no = 0;
48 	} while (schid.ssid++ < max_ssid);
49 	return ret;
50 }
51 
52 static struct subchannel *
53 css_alloc_subchannel(struct subchannel_id schid)
54 {
55 	struct subchannel *sch;
56 	int ret;
57 
58 	sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
59 	if (sch == NULL)
60 		return ERR_PTR(-ENOMEM);
61 	ret = cio_validate_subchannel (sch, schid);
62 	if (ret < 0) {
63 		kfree(sch);
64 		return ERR_PTR(ret);
65 	}
66 
67 	if (sch->st != SUBCHANNEL_TYPE_IO) {
68 		/* For now we ignore all non-io subchannels. */
69 		kfree(sch);
70 		return ERR_PTR(-EINVAL);
71 	}
72 
73 	/*
74 	 * Set intparm to subchannel address.
75 	 * This is fine even on 64bit since the subchannel is always located
76 	 * under 2G.
77 	 */
78 	sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
79 	ret = cio_modify(sch);
80 	if (ret) {
81 		kfree(sch);
82 		return ERR_PTR(ret);
83 	}
84 	return sch;
85 }
86 
87 static void
88 css_free_subchannel(struct subchannel *sch)
89 {
90 	if (sch) {
91 		/* Reset intparm to zeroes. */
92 		sch->schib.pmcw.intparm = 0;
93 		cio_modify(sch);
94 		kfree(sch);
95 	}
96 
97 }
98 
99 static void
100 css_subchannel_release(struct device *dev)
101 {
102 	struct subchannel *sch;
103 
104 	sch = to_subchannel(dev);
105 	if (!cio_is_console(sch->schid))
106 		kfree(sch);
107 }
108 
109 extern int css_get_ssd_info(struct subchannel *sch);
110 
111 
112 int css_sch_device_register(struct subchannel *sch)
113 {
114 	int ret;
115 
116 	mutex_lock(&sch->reg_mutex);
117 	ret = device_register(&sch->dev);
118 	mutex_unlock(&sch->reg_mutex);
119 	return ret;
120 }
121 
122 void css_sch_device_unregister(struct subchannel *sch)
123 {
124 	mutex_lock(&sch->reg_mutex);
125 	device_unregister(&sch->dev);
126 	mutex_unlock(&sch->reg_mutex);
127 }
128 
129 static int
130 css_register_subchannel(struct subchannel *sch)
131 {
132 	int ret;
133 
134 	/* Initialize the subchannel structure */
135 	sch->dev.parent = &css[0]->device;
136 	sch->dev.bus = &css_bus_type;
137 	sch->dev.release = &css_subchannel_release;
138 
139 	/* make it known to the system */
140 	ret = css_sch_device_register(sch);
141 	if (ret)
142 		printk (KERN_WARNING "%s: could not register %s\n",
143 			__func__, sch->dev.bus_id);
144 	else
145 		css_get_ssd_info(sch);
146 	return ret;
147 }
148 
149 int
150 css_probe_device(struct subchannel_id schid)
151 {
152 	int ret;
153 	struct subchannel *sch;
154 
155 	sch = css_alloc_subchannel(schid);
156 	if (IS_ERR(sch))
157 		return PTR_ERR(sch);
158 	ret = css_register_subchannel(sch);
159 	if (ret)
160 		css_free_subchannel(sch);
161 	return ret;
162 }
163 
164 static int
165 check_subchannel(struct device * dev, void * data)
166 {
167 	struct subchannel *sch;
168 	struct subchannel_id *schid = data;
169 
170 	sch = to_subchannel(dev);
171 	return schid_equal(&sch->schid, schid);
172 }
173 
174 struct subchannel *
175 get_subchannel_by_schid(struct subchannel_id schid)
176 {
177 	struct device *dev;
178 
179 	dev = bus_find_device(&css_bus_type, NULL,
180 			      (void *)&schid, check_subchannel);
181 
182 	return dev ? to_subchannel(dev) : NULL;
183 }
184 
185 
186 static inline int
187 css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
188 {
189 	struct schib schib;
190 	int cc;
191 
192 	cc = stsch(schid, &schib);
193 	if (cc)
194 		return CIO_GONE;
195 	if (!schib.pmcw.dnv)
196 		return CIO_GONE;
197 	if (sch && sch->schib.pmcw.dnv &&
198 	    (schib.pmcw.dev != sch->schib.pmcw.dev))
199 		return CIO_REVALIDATE;
200 	if (sch && !sch->lpm)
201 		return CIO_NO_PATH;
202 	return CIO_OPER;
203 }
204 
205 static int
206 css_evaluate_subchannel(struct subchannel_id schid, int slow)
207 {
208 	int event, ret, disc;
209 	struct subchannel *sch;
210 	unsigned long flags;
211 
212 	sch = get_subchannel_by_schid(schid);
213 	disc = sch ? device_is_disconnected(sch) : 0;
214 	if (disc && slow) {
215 		if (sch)
216 			put_device(&sch->dev);
217 		return 0; /* Already processed. */
218 	}
219 	/*
220 	 * We've got a machine check, so running I/O won't get an interrupt.
221 	 * Kill any pending timers.
222 	 */
223 	if (sch)
224 		device_kill_pending_timer(sch);
225 	if (!disc && !slow) {
226 		if (sch)
227 			put_device(&sch->dev);
228 		return -EAGAIN; /* Will be done on the slow path. */
229 	}
230 	event = css_get_subchannel_status(sch, schid);
231 	CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
232 		      schid.ssid, schid.sch_no, event,
233 		      sch?(disc?"disconnected":"normal"):"unknown",
234 		      slow?"slow":"fast");
235 	switch (event) {
236 	case CIO_NO_PATH:
237 	case CIO_GONE:
238 		if (!sch) {
239 			/* Never used this subchannel. Ignore. */
240 			ret = 0;
241 			break;
242 		}
243 		if (disc && (event == CIO_NO_PATH)) {
244 			/*
245 			 * Uargh, hack again. Because we don't get a machine
246 			 * check on configure on, our path bookkeeping can
247 			 * be out of date here (it's fine while we only do
248 			 * logical varying or get chsc machine checks). We
249 			 * need to force reprobing or we might miss devices
250 			 * coming operational again. It won't do harm in real
251 			 * no path situations.
252 			 */
253 			spin_lock_irqsave(&sch->lock, flags);
254 			device_trigger_reprobe(sch);
255 			spin_unlock_irqrestore(&sch->lock, flags);
256 			ret = 0;
257 			break;
258 		}
259 		if (sch->driver && sch->driver->notify &&
260 		    sch->driver->notify(&sch->dev, event)) {
261 			cio_disable_subchannel(sch);
262 			device_set_disconnected(sch);
263 			ret = 0;
264 			break;
265 		}
266 		/*
267 		 * Unregister subchannel.
268 		 * The device will be killed automatically.
269 		 */
270 		cio_disable_subchannel(sch);
271 		css_sch_device_unregister(sch);
272 		/* Reset intparm to zeroes. */
273 		sch->schib.pmcw.intparm = 0;
274 		cio_modify(sch);
275 		put_device(&sch->dev);
276 		ret = 0;
277 		break;
278 	case CIO_REVALIDATE:
279 		/*
280 		 * Revalidation machine check. Sick.
281 		 * We don't notify the driver since we have to throw the device
282 		 * away in any case.
283 		 */
284 		if (!disc) {
285 			css_sch_device_unregister(sch);
286 			/* Reset intparm to zeroes. */
287 			sch->schib.pmcw.intparm = 0;
288 			cio_modify(sch);
289 			put_device(&sch->dev);
290 			ret = css_probe_device(schid);
291 		} else {
292 			/*
293 			 * We can't immediately deregister the disconnected
294 			 * device since it might block.
295 			 */
296 			spin_lock_irqsave(&sch->lock, flags);
297 			device_trigger_reprobe(sch);
298 			spin_unlock_irqrestore(&sch->lock, flags);
299 			ret = 0;
300 		}
301 		break;
302 	case CIO_OPER:
303 		if (disc) {
304 			spin_lock_irqsave(&sch->lock, flags);
305 			/* Get device operational again. */
306 			device_trigger_reprobe(sch);
307 			spin_unlock_irqrestore(&sch->lock, flags);
308 		}
309 		ret = sch ? 0 : css_probe_device(schid);
310 		break;
311 	default:
312 		BUG();
313 		ret = 0;
314 	}
315 	return ret;
316 }
317 
318 static int
319 css_rescan_devices(struct subchannel_id schid, void *data)
320 {
321 	return css_evaluate_subchannel(schid, 1);
322 }
323 
324 struct slow_subchannel {
325 	struct list_head slow_list;
326 	struct subchannel_id schid;
327 };
328 
329 static LIST_HEAD(slow_subchannels_head);
330 static DEFINE_SPINLOCK(slow_subchannel_lock);
331 
332 static void
333 css_trigger_slow_path(void)
334 {
335 	CIO_TRACE_EVENT(4, "slowpath");
336 
337 	if (need_rescan) {
338 		need_rescan = 0;
339 		for_each_subchannel(css_rescan_devices, NULL);
340 		return;
341 	}
342 
343 	spin_lock_irq(&slow_subchannel_lock);
344 	while (!list_empty(&slow_subchannels_head)) {
345 		struct slow_subchannel *slow_sch =
346 			list_entry(slow_subchannels_head.next,
347 				   struct slow_subchannel, slow_list);
348 
349 		list_del_init(slow_subchannels_head.next);
350 		spin_unlock_irq(&slow_subchannel_lock);
351 		css_evaluate_subchannel(slow_sch->schid, 1);
352 		spin_lock_irq(&slow_subchannel_lock);
353 		kfree(slow_sch);
354 	}
355 	spin_unlock_irq(&slow_subchannel_lock);
356 }
357 
358 typedef void (*workfunc)(void *);
359 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
360 struct workqueue_struct *slow_path_wq;
361 
362 /* Reprobe subchannel if unregistered. */
363 static int reprobe_subchannel(struct subchannel_id schid, void *data)
364 {
365 	struct subchannel *sch;
366 	int ret;
367 
368 	CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
369 		  schid.ssid, schid.sch_no);
370 	if (need_reprobe)
371 		return -EAGAIN;
372 
373 	sch = get_subchannel_by_schid(schid);
374 	if (sch) {
375 		/* Already known. */
376 		put_device(&sch->dev);
377 		return 0;
378 	}
379 
380 	ret = css_probe_device(schid);
381 	switch (ret) {
382 	case 0:
383 		break;
384 	case -ENXIO:
385 	case -ENOMEM:
386 		/* These should abort looping */
387 		break;
388 	default:
389 		ret = 0;
390 	}
391 
392 	return ret;
393 }
394 
395 /* Work function used to reprobe all unregistered subchannels. */
396 static void reprobe_all(void *data)
397 {
398 	int ret;
399 
400 	CIO_MSG_EVENT(2, "reprobe start\n");
401 
402 	need_reprobe = 0;
403 	/* Make sure initial subchannel scan is done. */
404 	wait_event(ccw_device_init_wq,
405 		   atomic_read(&ccw_device_init_count) == 0);
406 	ret = for_each_subchannel(reprobe_subchannel, NULL);
407 
408 	CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
409 		      need_reprobe);
410 }
411 
412 DECLARE_WORK(css_reprobe_work, reprobe_all, NULL);
413 
414 /* Schedule reprobing of all unregistered subchannels. */
415 void css_schedule_reprobe(void)
416 {
417 	need_reprobe = 1;
418 	queue_work(ccw_device_work, &css_reprobe_work);
419 }
420 
421 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
422 
423 /*
424  * Rescan for new devices. FIXME: This is slow.
425  * This function is called when we have lost CRWs due to overflows and we have
426  * to do subchannel housekeeping.
427  */
428 void
429 css_reiterate_subchannels(void)
430 {
431 	css_clear_subchannel_slow_list();
432 	need_rescan = 1;
433 }
434 
435 /*
436  * Called from the machine check handler for subchannel report words.
437  */
438 int
439 css_process_crw(int rsid1, int rsid2)
440 {
441 	int ret;
442 	struct subchannel_id mchk_schid;
443 
444 	CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
445 		      rsid1, rsid2);
446 
447 	if (need_rescan)
448 		/* We need to iterate all subchannels anyway. */
449 		return -EAGAIN;
450 
451 	init_subchannel_id(&mchk_schid);
452 	mchk_schid.sch_no = rsid1;
453 	if (rsid2 != 0)
454 		mchk_schid.ssid = (rsid2 >> 8) & 3;
455 
456 	/*
457 	 * Since we are always presented with IPI in the CRW, we have to
458 	 * use stsch() to find out if the subchannel in question has come
459 	 * or gone.
460 	 */
461 	ret = css_evaluate_subchannel(mchk_schid, 0);
462 	if (ret == -EAGAIN) {
463 		if (css_enqueue_subchannel_slow(mchk_schid)) {
464 			css_clear_subchannel_slow_list();
465 			need_rescan = 1;
466 		}
467 	}
468 	return ret;
469 }
470 
471 static int __init
472 __init_channel_subsystem(struct subchannel_id schid, void *data)
473 {
474 	struct subchannel *sch;
475 	int ret;
476 
477 	if (cio_is_console(schid))
478 		sch = cio_get_console_subchannel();
479 	else {
480 		sch = css_alloc_subchannel(schid);
481 		if (IS_ERR(sch))
482 			ret = PTR_ERR(sch);
483 		else
484 			ret = 0;
485 		switch (ret) {
486 		case 0:
487 			break;
488 		case -ENOMEM:
489 			panic("Out of memory in init_channel_subsystem\n");
490 		/* -ENXIO: no more subchannels. */
491 		case -ENXIO:
492 			return ret;
493 		/* -EIO: this subchannel set not supported. */
494 		case -EIO:
495 			return ret;
496 		default:
497 			return 0;
498 		}
499 	}
500 	/*
501 	 * We register ALL valid subchannels in ioinfo, even those
502 	 * that have been present before init_channel_subsystem.
503 	 * These subchannels can't have been registered yet (kmalloc
504 	 * not working) so we do it now. This is true e.g. for the
505 	 * console subchannel.
506 	 */
507 	css_register_subchannel(sch);
508 	return 0;
509 }
510 
511 static void __init
512 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
513 {
514 	if (css_characteristics_avail && css_general_characteristics.mcss) {
515 		css->global_pgid.pgid_high.ext_cssid.version = 0x80;
516 		css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
517 	} else {
518 #ifdef CONFIG_SMP
519 		css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
520 #else
521 		css->global_pgid.pgid_high.cpu_addr = 0;
522 #endif
523 	}
524 	css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
525 	css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
526 	css->global_pgid.tod_high = tod_high;
527 
528 }
529 
530 static void
531 channel_subsystem_release(struct device *dev)
532 {
533 	struct channel_subsystem *css;
534 
535 	css = to_css(dev);
536 	mutex_destroy(&css->mutex);
537 	kfree(css);
538 }
539 
540 static ssize_t
541 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
542 		   char *buf)
543 {
544 	struct channel_subsystem *css = to_css(dev);
545 
546 	if (!css)
547 		return 0;
548 	return sprintf(buf, "%x\n", css->cm_enabled);
549 }
550 
551 static ssize_t
552 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
553 		    const char *buf, size_t count)
554 {
555 	struct channel_subsystem *css = to_css(dev);
556 	int ret;
557 
558 	switch (buf[0]) {
559 	case '0':
560 		ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
561 		break;
562 	case '1':
563 		ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
564 		break;
565 	default:
566 		ret = -EINVAL;
567 	}
568 	return ret < 0 ? ret : count;
569 }
570 
571 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
572 
573 static inline void __init
574 setup_css(int nr)
575 {
576 	u32 tod_high;
577 
578 	memset(css[nr], 0, sizeof(struct channel_subsystem));
579 	mutex_init(&css[nr]->mutex);
580 	css[nr]->valid = 1;
581 	css[nr]->cssid = nr;
582 	sprintf(css[nr]->device.bus_id, "css%x", nr);
583 	css[nr]->device.release = channel_subsystem_release;
584 	tod_high = (u32) (get_clock() >> 32);
585 	css_generate_pgid(css[nr], tod_high);
586 }
587 
588 /*
589  * Now that the driver core is running, we can setup our channel subsystem.
590  * The struct subchannel's are created during probing (except for the
591  * static console subchannel).
592  */
593 static int __init
594 init_channel_subsystem (void)
595 {
596 	int ret, i;
597 
598 	if (chsc_determine_css_characteristics() == 0)
599 		css_characteristics_avail = 1;
600 
601 	if ((ret = bus_register(&css_bus_type)))
602 		goto out;
603 
604 	/* Try to enable MSS. */
605 	ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
606 	switch (ret) {
607 	case 0: /* Success. */
608 		max_ssid = __MAX_SSID;
609 		break;
610 	case -ENOMEM:
611 		goto out_bus;
612 	default:
613 		max_ssid = 0;
614 	}
615 	/* Setup css structure. */
616 	for (i = 0; i <= __MAX_CSSID; i++) {
617 		css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
618 		if (!css[i]) {
619 			ret = -ENOMEM;
620 			goto out_unregister;
621 		}
622 		setup_css(i);
623 		ret = device_register(&css[i]->device);
624 		if (ret)
625 			goto out_free;
626 		if (css_characteristics_avail &&
627 		    css_chsc_characteristics.secm) {
628 			ret = device_create_file(&css[i]->device,
629 						 &dev_attr_cm_enable);
630 			if (ret)
631 				goto out_device;
632 		}
633 	}
634 	css_init_done = 1;
635 
636 	ctl_set_bit(6, 28);
637 
638 	for_each_subchannel(__init_channel_subsystem, NULL);
639 	return 0;
640 out_device:
641 	device_unregister(&css[i]->device);
642 out_free:
643 	kfree(css[i]);
644 out_unregister:
645 	while (i > 0) {
646 		i--;
647 		if (css_characteristics_avail && css_chsc_characteristics.secm)
648 			device_remove_file(&css[i]->device,
649 					   &dev_attr_cm_enable);
650 		device_unregister(&css[i]->device);
651 	}
652 out_bus:
653 	bus_unregister(&css_bus_type);
654 out:
655 	return ret;
656 }
657 
658 /*
659  * find a driver for a subchannel. They identify by the subchannel
660  * type with the exception that the console subchannel driver has its own
661  * subchannel type although the device is an i/o subchannel
662  */
663 static int
664 css_bus_match (struct device *dev, struct device_driver *drv)
665 {
666 	struct subchannel *sch = container_of (dev, struct subchannel, dev);
667 	struct css_driver *driver = container_of (drv, struct css_driver, drv);
668 
669 	if (sch->st == driver->subchannel_type)
670 		return 1;
671 
672 	return 0;
673 }
674 
675 static int
676 css_probe (struct device *dev)
677 {
678 	struct subchannel *sch;
679 
680 	sch = to_subchannel(dev);
681 	sch->driver = container_of (dev->driver, struct css_driver, drv);
682 	return (sch->driver->probe ? sch->driver->probe(sch) : 0);
683 }
684 
685 static int
686 css_remove (struct device *dev)
687 {
688 	struct subchannel *sch;
689 
690 	sch = to_subchannel(dev);
691 	return (sch->driver->remove ? sch->driver->remove(sch) : 0);
692 }
693 
694 static void
695 css_shutdown (struct device *dev)
696 {
697 	struct subchannel *sch;
698 
699 	sch = to_subchannel(dev);
700 	if (sch->driver->shutdown)
701 		sch->driver->shutdown(sch);
702 }
703 
704 struct bus_type css_bus_type = {
705 	.name     = "css",
706 	.match    = css_bus_match,
707 	.probe    = css_probe,
708 	.remove   = css_remove,
709 	.shutdown = css_shutdown,
710 };
711 
712 subsys_initcall(init_channel_subsystem);
713 
714 int
715 css_enqueue_subchannel_slow(struct subchannel_id schid)
716 {
717 	struct slow_subchannel *new_slow_sch;
718 	unsigned long flags;
719 
720 	new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
721 	if (!new_slow_sch)
722 		return -ENOMEM;
723 	new_slow_sch->schid = schid;
724 	spin_lock_irqsave(&slow_subchannel_lock, flags);
725 	list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
726 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
727 	return 0;
728 }
729 
730 void
731 css_clear_subchannel_slow_list(void)
732 {
733 	unsigned long flags;
734 
735 	spin_lock_irqsave(&slow_subchannel_lock, flags);
736 	while (!list_empty(&slow_subchannels_head)) {
737 		struct slow_subchannel *slow_sch =
738 			list_entry(slow_subchannels_head.next,
739 				   struct slow_subchannel, slow_list);
740 
741 		list_del_init(slow_subchannels_head.next);
742 		kfree(slow_sch);
743 	}
744 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
745 }
746 
747 
748 
749 int
750 css_slow_subchannels_exist(void)
751 {
752 	return (!list_empty(&slow_subchannels_head));
753 }
754 
755 MODULE_LICENSE("GPL");
756 EXPORT_SYMBOL(css_bus_type);
757 EXPORT_SYMBOL_GPL(css_characteristics_avail);
758