2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
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>
19 #include "cio_debug.h"
24 int css_init_done = 0;
25 static int max_ssid = 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);
41 ret = fn(schid, data);
44 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
46 } while (schid.ssid++ < max_ssid);
50 static struct subchannel *
51 css_alloc_subchannel(struct subchannel_id schid)
53 struct subchannel *sch;
56 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
58 return ERR_PTR(-ENOMEM);
59 ret = cio_validate_subchannel (sch, schid);
65 if (sch->st != SUBCHANNEL_TYPE_IO) {
66 /* For now we ignore all non-io subchannels. */
68 return ERR_PTR(-EINVAL);
72 * Set intparm to subchannel address.
73 * This is fine even on 64bit since the subchannel is always located
76 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
77 ret = cio_modify(sch);
86 css_free_subchannel(struct subchannel *sch)
89 /* Reset intparm to zeroes. */
90 sch->schib.pmcw.intparm = 0;
98 css_subchannel_release(struct device *dev)
100 struct subchannel *sch;
102 sch = to_subchannel(dev);
103 if (!cio_is_console(sch->schid))
107 extern int css_get_ssd_info(struct subchannel *sch);
110 css_register_subchannel(struct subchannel *sch)
114 /* Initialize the subchannel structure */
115 sch->dev.parent = &css[0]->device;
116 sch->dev.bus = &css_bus_type;
117 sch->dev.release = &css_subchannel_release;
119 /* make it known to the system */
120 ret = device_register(&sch->dev);
122 printk (KERN_WARNING "%s: could not register %s\n",
123 __func__, sch->dev.bus_id);
125 css_get_ssd_info(sch);
130 css_probe_device(struct subchannel_id schid)
133 struct subchannel *sch;
135 sch = css_alloc_subchannel(schid);
138 ret = css_register_subchannel(sch);
140 css_free_subchannel(sch);
145 check_subchannel(struct device * dev, void * data)
147 struct subchannel *sch;
148 struct subchannel_id *schid = data;
150 sch = to_subchannel(dev);
151 return schid_equal(&sch->schid, schid);
155 get_subchannel_by_schid(struct subchannel_id schid)
159 dev = bus_find_device(&css_bus_type, NULL,
160 (void *)&schid, check_subchannel);
162 return dev ? to_subchannel(dev) : NULL;
167 css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
172 cc = stsch(schid, &schib);
177 if (sch && sch->schib.pmcw.dnv &&
178 (schib.pmcw.dev != sch->schib.pmcw.dev))
179 return CIO_REVALIDATE;
180 if (sch && !sch->lpm)
186 css_evaluate_subchannel(struct subchannel_id schid, int slow)
188 int event, ret, disc;
189 struct subchannel *sch;
192 sch = get_subchannel_by_schid(schid);
193 disc = sch ? device_is_disconnected(sch) : 0;
196 put_device(&sch->dev);
197 return 0; /* Already processed. */
200 * We've got a machine check, so running I/O won't get an interrupt.
201 * Kill any pending timers.
204 device_kill_pending_timer(sch);
205 if (!disc && !slow) {
207 put_device(&sch->dev);
208 return -EAGAIN; /* Will be done on the slow path. */
210 event = css_get_subchannel_status(sch, schid);
211 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
212 schid.ssid, schid.sch_no, event,
213 sch?(disc?"disconnected":"normal"):"unknown",
219 /* Never used this subchannel. Ignore. */
223 if (disc && (event == CIO_NO_PATH)) {
225 * Uargh, hack again. Because we don't get a machine
226 * check on configure on, our path bookkeeping can
227 * be out of date here (it's fine while we only do
228 * logical varying or get chsc machine checks). We
229 * need to force reprobing or we might miss devices
230 * coming operational again. It won't do harm in real
231 * no path situations.
233 spin_lock_irqsave(&sch->lock, flags);
234 device_trigger_reprobe(sch);
235 spin_unlock_irqrestore(&sch->lock, flags);
239 if (sch->driver && sch->driver->notify &&
240 sch->driver->notify(&sch->dev, event)) {
241 cio_disable_subchannel(sch);
242 device_set_disconnected(sch);
247 * Unregister subchannel.
248 * The device will be killed automatically.
250 cio_disable_subchannel(sch);
251 device_unregister(&sch->dev);
252 /* Reset intparm to zeroes. */
253 sch->schib.pmcw.intparm = 0;
255 put_device(&sch->dev);
260 * Revalidation machine check. Sick.
261 * We don't notify the driver since we have to throw the device
265 device_unregister(&sch->dev);
266 /* Reset intparm to zeroes. */
267 sch->schib.pmcw.intparm = 0;
269 put_device(&sch->dev);
270 ret = css_probe_device(schid);
273 * We can't immediately deregister the disconnected
274 * device since it might block.
276 spin_lock_irqsave(&sch->lock, flags);
277 device_trigger_reprobe(sch);
278 spin_unlock_irqrestore(&sch->lock, flags);
284 spin_lock_irqsave(&sch->lock, flags);
285 /* Get device operational again. */
286 device_trigger_reprobe(sch);
287 spin_unlock_irqrestore(&sch->lock, flags);
289 ret = sch ? 0 : css_probe_device(schid);
299 css_rescan_devices(struct subchannel_id schid, void *data)
301 return css_evaluate_subchannel(schid, 1);
304 struct slow_subchannel {
305 struct list_head slow_list;
306 struct subchannel_id schid;
309 static LIST_HEAD(slow_subchannels_head);
310 static DEFINE_SPINLOCK(slow_subchannel_lock);
313 css_trigger_slow_path(void)
315 CIO_TRACE_EVENT(4, "slowpath");
319 for_each_subchannel(css_rescan_devices, NULL);
323 spin_lock_irq(&slow_subchannel_lock);
324 while (!list_empty(&slow_subchannels_head)) {
325 struct slow_subchannel *slow_sch =
326 list_entry(slow_subchannels_head.next,
327 struct slow_subchannel, slow_list);
329 list_del_init(slow_subchannels_head.next);
330 spin_unlock_irq(&slow_subchannel_lock);
331 css_evaluate_subchannel(slow_sch->schid, 1);
332 spin_lock_irq(&slow_subchannel_lock);
335 spin_unlock_irq(&slow_subchannel_lock);
338 typedef void (*workfunc)(void *);
339 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
340 struct workqueue_struct *slow_path_wq;
343 * Rescan for new devices. FIXME: This is slow.
344 * This function is called when we have lost CRWs due to overflows and we have
345 * to do subchannel housekeeping.
348 css_reiterate_subchannels(void)
350 css_clear_subchannel_slow_list();
355 * Called from the machine check handler for subchannel report words.
358 css_process_crw(int rsid1, int rsid2)
361 struct subchannel_id mchk_schid;
363 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
367 /* We need to iterate all subchannels anyway. */
370 init_subchannel_id(&mchk_schid);
371 mchk_schid.sch_no = rsid1;
373 mchk_schid.ssid = (rsid2 >> 8) & 3;
376 * Since we are always presented with IPI in the CRW, we have to
377 * use stsch() to find out if the subchannel in question has come
380 ret = css_evaluate_subchannel(mchk_schid, 0);
381 if (ret == -EAGAIN) {
382 if (css_enqueue_subchannel_slow(mchk_schid)) {
383 css_clear_subchannel_slow_list();
391 __init_channel_subsystem(struct subchannel_id schid, void *data)
393 struct subchannel *sch;
396 if (cio_is_console(schid))
397 sch = cio_get_console_subchannel();
399 sch = css_alloc_subchannel(schid);
408 panic("Out of memory in init_channel_subsystem\n");
409 /* -ENXIO: no more subchannels. */
412 /* -EIO: this subchannel set not supported. */
420 * We register ALL valid subchannels in ioinfo, even those
421 * that have been present before init_channel_subsystem.
422 * These subchannels can't have been registered yet (kmalloc
423 * not working) so we do it now. This is true e.g. for the
424 * console subchannel.
426 css_register_subchannel(sch);
431 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
433 if (css_characteristics_avail && css_general_characteristics.mcss) {
434 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
435 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
438 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
440 css->global_pgid.pgid_high.cpu_addr = 0;
443 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
444 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
445 css->global_pgid.tod_high = tod_high;
450 channel_subsystem_release(struct device *dev)
452 struct channel_subsystem *css;
455 mutex_destroy(&css->mutex);
460 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
463 struct channel_subsystem *css = to_css(dev);
467 return sprintf(buf, "%x\n", css->cm_enabled);
471 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
472 const char *buf, size_t count)
474 struct channel_subsystem *css = to_css(dev);
479 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
482 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
487 return ret < 0 ? ret : count;
490 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
492 static inline void __init
497 memset(css[nr], 0, sizeof(struct channel_subsystem));
498 mutex_init(&css[nr]->mutex);
501 sprintf(css[nr]->device.bus_id, "css%x", nr);
502 css[nr]->device.release = channel_subsystem_release;
503 tod_high = (u32) (get_clock() >> 32);
504 css_generate_pgid(css[nr], tod_high);
508 * Now that the driver core is running, we can setup our channel subsystem.
509 * The struct subchannel's are created during probing (except for the
510 * static console subchannel).
513 init_channel_subsystem (void)
517 if (chsc_determine_css_characteristics() == 0)
518 css_characteristics_avail = 1;
520 if ((ret = bus_register(&css_bus_type)))
523 /* Try to enable MSS. */
524 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
526 case 0: /* Success. */
527 max_ssid = __MAX_SSID;
534 /* Setup css structure. */
535 for (i = 0; i <= __MAX_CSSID; i++) {
536 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
542 ret = device_register(&css[i]->device);
545 if (css_characteristics_avail && css_chsc_characteristics.secm)
546 device_create_file(&css[i]->device,
547 &dev_attr_cm_enable);
553 for_each_subchannel(__init_channel_subsystem, NULL);
560 if (css_characteristics_avail && css_chsc_characteristics.secm)
561 device_remove_file(&css[i]->device,
562 &dev_attr_cm_enable);
563 device_unregister(&css[i]->device);
566 bus_unregister(&css_bus_type);
572 * find a driver for a subchannel. They identify by the subchannel
573 * type with the exception that the console subchannel driver has its own
574 * subchannel type although the device is an i/o subchannel
577 css_bus_match (struct device *dev, struct device_driver *drv)
579 struct subchannel *sch = container_of (dev, struct subchannel, dev);
580 struct css_driver *driver = container_of (drv, struct css_driver, drv);
582 if (sch->st == driver->subchannel_type)
589 css_probe (struct device *dev)
591 struct subchannel *sch;
593 sch = to_subchannel(dev);
594 sch->driver = container_of (dev->driver, struct css_driver, drv);
595 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
599 css_remove (struct device *dev)
601 struct subchannel *sch;
603 sch = to_subchannel(dev);
604 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
608 css_shutdown (struct device *dev)
610 struct subchannel *sch;
612 sch = to_subchannel(dev);
613 if (sch->driver->shutdown)
614 sch->driver->shutdown(sch);
617 struct bus_type css_bus_type = {
619 .match = css_bus_match,
621 .remove = css_remove,
622 .shutdown = css_shutdown,
625 subsys_initcall(init_channel_subsystem);
628 css_enqueue_subchannel_slow(struct subchannel_id schid)
630 struct slow_subchannel *new_slow_sch;
633 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
636 new_slow_sch->schid = schid;
637 spin_lock_irqsave(&slow_subchannel_lock, flags);
638 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
639 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
644 css_clear_subchannel_slow_list(void)
648 spin_lock_irqsave(&slow_subchannel_lock, flags);
649 while (!list_empty(&slow_subchannels_head)) {
650 struct slow_subchannel *slow_sch =
651 list_entry(slow_subchannels_head.next,
652 struct slow_subchannel, slow_list);
654 list_del_init(slow_subchannels_head.next);
657 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
663 css_slow_subchannels_exist(void)
665 return (!list_empty(&slow_subchannels_head));
668 MODULE_LICENSE("GPL");
669 EXPORT_SYMBOL(css_bus_type);
670 EXPORT_SYMBOL_GPL(css_characteristics_avail);