2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
6 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
9 * Cornelia Huck (cohuck@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/list.h>
20 #include "cio_debug.h"
25 int css_init_done = 0;
27 struct channel_subsystem *css[__MAX_CSSID + 1];
29 int css_characteristics_avail = 0;
32 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
34 struct subchannel_id schid;
37 init_subchannel_id(&schid);
40 ret = fn(schid, data);
43 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
47 static struct subchannel *
48 css_alloc_subchannel(struct subchannel_id schid)
50 struct subchannel *sch;
53 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
55 return ERR_PTR(-ENOMEM);
56 ret = cio_validate_subchannel (sch, schid);
62 if (sch->st != SUBCHANNEL_TYPE_IO) {
63 /* For now we ignore all non-io subchannels. */
65 return ERR_PTR(-EINVAL);
69 * Set intparm to subchannel address.
70 * This is fine even on 64bit since the subchannel is always located
73 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
74 ret = cio_modify(sch);
83 css_free_subchannel(struct subchannel *sch)
86 /* Reset intparm to zeroes. */
87 sch->schib.pmcw.intparm = 0;
95 css_subchannel_release(struct device *dev)
97 struct subchannel *sch;
99 sch = to_subchannel(dev);
100 if (!cio_is_console(sch->schid))
104 extern int css_get_ssd_info(struct subchannel *sch);
107 css_register_subchannel(struct subchannel *sch)
111 /* Initialize the subchannel structure */
112 sch->dev.parent = &css[0]->device;
113 sch->dev.bus = &css_bus_type;
114 sch->dev.release = &css_subchannel_release;
116 /* make it known to the system */
117 ret = device_register(&sch->dev);
119 printk (KERN_WARNING "%s: could not register %s\n",
120 __func__, sch->dev.bus_id);
122 css_get_ssd_info(sch);
127 css_probe_device(struct subchannel_id schid)
130 struct subchannel *sch;
132 sch = css_alloc_subchannel(schid);
135 ret = css_register_subchannel(sch);
137 css_free_subchannel(sch);
142 check_subchannel(struct device * dev, void * data)
144 struct subchannel *sch;
145 struct subchannel_id *schid = data;
147 sch = to_subchannel(dev);
148 return schid_equal(&sch->schid, schid);
152 get_subchannel_by_schid(struct subchannel_id schid)
156 dev = bus_find_device(&css_bus_type, NULL,
157 (void *)&schid, check_subchannel);
159 return dev ? to_subchannel(dev) : NULL;
164 css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
169 cc = stsch(schid, &schib);
174 if (sch && sch->schib.pmcw.dnv &&
175 (schib.pmcw.dev != sch->schib.pmcw.dev))
176 return CIO_REVALIDATE;
177 if (sch && !sch->lpm)
183 css_evaluate_subchannel(struct subchannel_id schid, int slow)
185 int event, ret, disc;
186 struct subchannel *sch;
189 sch = get_subchannel_by_schid(schid);
190 disc = sch ? device_is_disconnected(sch) : 0;
193 put_device(&sch->dev);
194 return 0; /* Already processed. */
197 * We've got a machine check, so running I/O won't get an interrupt.
198 * Kill any pending timers.
201 device_kill_pending_timer(sch);
202 if (!disc && !slow) {
204 put_device(&sch->dev);
205 return -EAGAIN; /* Will be done on the slow path. */
207 event = css_get_subchannel_status(sch, schid);
208 CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
210 sch?(disc?"disconnected":"normal"):"unknown",
216 /* Never used this subchannel. Ignore. */
220 if (disc && (event == CIO_NO_PATH)) {
222 * Uargh, hack again. Because we don't get a machine
223 * check on configure on, our path bookkeeping can
224 * be out of date here (it's fine while we only do
225 * logical varying or get chsc machine checks). We
226 * need to force reprobing or we might miss devices
227 * coming operational again. It won't do harm in real
228 * no path situations.
230 spin_lock_irqsave(&sch->lock, flags);
231 device_trigger_reprobe(sch);
232 spin_unlock_irqrestore(&sch->lock, flags);
236 if (sch->driver && sch->driver->notify &&
237 sch->driver->notify(&sch->dev, event)) {
238 cio_disable_subchannel(sch);
239 device_set_disconnected(sch);
244 * Unregister subchannel.
245 * The device will be killed automatically.
247 cio_disable_subchannel(sch);
248 device_unregister(&sch->dev);
249 /* Reset intparm to zeroes. */
250 sch->schib.pmcw.intparm = 0;
252 put_device(&sch->dev);
257 * Revalidation machine check. Sick.
258 * We don't notify the driver since we have to throw the device
262 device_unregister(&sch->dev);
263 /* Reset intparm to zeroes. */
264 sch->schib.pmcw.intparm = 0;
266 put_device(&sch->dev);
267 ret = css_probe_device(schid);
270 * We can't immediately deregister the disconnected
271 * device since it might block.
273 spin_lock_irqsave(&sch->lock, flags);
274 device_trigger_reprobe(sch);
275 spin_unlock_irqrestore(&sch->lock, flags);
281 spin_lock_irqsave(&sch->lock, flags);
282 /* Get device operational again. */
283 device_trigger_reprobe(sch);
284 spin_unlock_irqrestore(&sch->lock, flags);
286 ret = sch ? 0 : css_probe_device(schid);
296 css_rescan_devices(struct subchannel_id schid, void *data)
298 return css_evaluate_subchannel(schid, 1);
301 struct slow_subchannel {
302 struct list_head slow_list;
303 struct subchannel_id schid;
306 static LIST_HEAD(slow_subchannels_head);
307 static DEFINE_SPINLOCK(slow_subchannel_lock);
310 css_trigger_slow_path(void)
312 CIO_TRACE_EVENT(4, "slowpath");
316 for_each_subchannel(css_rescan_devices, NULL);
320 spin_lock_irq(&slow_subchannel_lock);
321 while (!list_empty(&slow_subchannels_head)) {
322 struct slow_subchannel *slow_sch =
323 list_entry(slow_subchannels_head.next,
324 struct slow_subchannel, slow_list);
326 list_del_init(slow_subchannels_head.next);
327 spin_unlock_irq(&slow_subchannel_lock);
328 css_evaluate_subchannel(slow_sch->schid, 1);
329 spin_lock_irq(&slow_subchannel_lock);
332 spin_unlock_irq(&slow_subchannel_lock);
335 typedef void (*workfunc)(void *);
336 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
337 struct workqueue_struct *slow_path_wq;
340 * Rescan for new devices. FIXME: This is slow.
341 * This function is called when we have lost CRWs due to overflows and we have
342 * to do subchannel housekeeping.
345 css_reiterate_subchannels(void)
347 css_clear_subchannel_slow_list();
352 * Called from the machine check handler for subchannel report words.
355 css_process_crw(int irq)
358 struct subchannel_id mchk_schid;
360 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
363 /* We need to iterate all subchannels anyway. */
366 init_subchannel_id(&mchk_schid);
367 mchk_schid.sch_no = irq;
369 * Since we are always presented with IPI in the CRW, we have to
370 * use stsch() to find out if the subchannel in question has come
373 ret = css_evaluate_subchannel(mchk_schid, 0);
374 if (ret == -EAGAIN) {
375 if (css_enqueue_subchannel_slow(mchk_schid)) {
376 css_clear_subchannel_slow_list();
384 __init_channel_subsystem(struct subchannel_id schid, void *data)
386 struct subchannel *sch;
389 if (cio_is_console(schid))
390 sch = cio_get_console_subchannel();
392 sch = css_alloc_subchannel(schid);
401 panic("Out of memory in init_channel_subsystem\n");
402 /* -ENXIO: no more subchannels. */
410 * We register ALL valid subchannels in ioinfo, even those
411 * that have been present before init_channel_subsystem.
412 * These subchannels can't have been registered yet (kmalloc
413 * not working) so we do it now. This is true e.g. for the
414 * console subchannel.
416 css_register_subchannel(sch);
421 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
423 if (css_characteristics_avail && css_general_characteristics.mcss) {
424 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
425 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
428 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
430 css->global_pgid.pgid_high.cpu_addr = 0;
433 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
434 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
435 css->global_pgid.tod_high = tod_high;
439 static inline void __init
444 memset(css[nr], 0, sizeof(struct channel_subsystem));
447 sprintf(css[nr]->device.bus_id, "css%x", nr);
448 tod_high = (u32) (get_clock() >> 32);
449 css_generate_pgid(css[nr], tod_high);
453 * Now that the driver core is running, we can setup our channel subsystem.
454 * The struct subchannel's are created during probing (except for the
455 * static console subchannel).
458 init_channel_subsystem (void)
462 if (chsc_determine_css_characteristics() == 0)
463 css_characteristics_avail = 1;
465 if ((ret = bus_register(&css_bus_type)))
468 /* Setup css structure. */
469 for (i = 0; i <= __MAX_CSSID; i++) {
470 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
476 ret = device_register(&css[i]->device);
484 for_each_subchannel(__init_channel_subsystem, NULL);
491 device_unregister(&css[i]->device);
493 bus_unregister(&css_bus_type);
499 * find a driver for a subchannel. They identify by the subchannel
500 * type with the exception that the console subchannel driver has its own
501 * subchannel type although the device is an i/o subchannel
504 css_bus_match (struct device *dev, struct device_driver *drv)
506 struct subchannel *sch = container_of (dev, struct subchannel, dev);
507 struct css_driver *driver = container_of (drv, struct css_driver, drv);
509 if (sch->st == driver->subchannel_type)
515 struct bus_type css_bus_type = {
517 .match = &css_bus_match,
520 subsys_initcall(init_channel_subsystem);
523 css_enqueue_subchannel_slow(struct subchannel_id schid)
525 struct slow_subchannel *new_slow_sch;
528 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
531 memset(new_slow_sch, 0, sizeof(struct slow_subchannel));
532 new_slow_sch->schid = schid;
533 spin_lock_irqsave(&slow_subchannel_lock, flags);
534 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
535 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
540 css_clear_subchannel_slow_list(void)
544 spin_lock_irqsave(&slow_subchannel_lock, flags);
545 while (!list_empty(&slow_subchannels_head)) {
546 struct slow_subchannel *slow_sch =
547 list_entry(slow_subchannels_head.next,
548 struct slow_subchannel, slow_list);
550 list_del_init(slow_subchannels_head.next);
553 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
559 css_slow_subchannels_exist(void)
561 return (!list_empty(&slow_subchannels_head));
564 MODULE_LICENSE("GPL");
565 EXPORT_SYMBOL(css_bus_type);
566 EXPORT_SYMBOL_GPL(css_characteristics_avail);