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"
25 int css_init_done = 0;
26 static int need_reprobe = 0;
27 static int max_ssid = 0;
29 struct channel_subsystem *css[__MAX_CSSID + 1];
31 int css_characteristics_avail = 0;
34 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
36 struct subchannel_id schid;
39 init_subchannel_id(&schid);
43 ret = fn(schid, data);
46 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
48 } while (schid.ssid++ < max_ssid);
52 static struct subchannel *
53 css_alloc_subchannel(struct subchannel_id schid)
55 struct subchannel *sch;
58 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
60 return ERR_PTR(-ENOMEM);
61 ret = cio_validate_subchannel (sch, schid);
67 if (sch->st != SUBCHANNEL_TYPE_IO) {
68 /* For now we ignore all non-io subchannels. */
70 return ERR_PTR(-EINVAL);
74 * Set intparm to subchannel address.
75 * This is fine even on 64bit since the subchannel is always located
78 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
79 ret = cio_modify(sch);
88 css_free_subchannel(struct subchannel *sch)
91 /* Reset intparm to zeroes. */
92 sch->schib.pmcw.intparm = 0;
100 css_subchannel_release(struct device *dev)
102 struct subchannel *sch;
104 sch = to_subchannel(dev);
105 if (!cio_is_console(sch->schid))
109 extern int css_get_ssd_info(struct subchannel *sch);
112 css_register_subchannel(struct subchannel *sch)
116 /* Initialize the subchannel structure */
117 sch->dev.parent = &css[0]->device;
118 sch->dev.bus = &css_bus_type;
119 sch->dev.release = &css_subchannel_release;
121 /* make it known to the system */
122 ret = device_register(&sch->dev);
124 printk (KERN_WARNING "%s: could not register %s\n",
125 __func__, sch->dev.bus_id);
127 css_get_ssd_info(sch);
132 css_probe_device(struct subchannel_id schid)
135 struct subchannel *sch;
137 sch = css_alloc_subchannel(schid);
140 ret = css_register_subchannel(sch);
142 css_free_subchannel(sch);
147 check_subchannel(struct device * dev, void * data)
149 struct subchannel *sch;
150 struct subchannel_id *schid = data;
152 sch = to_subchannel(dev);
153 return schid_equal(&sch->schid, schid);
157 get_subchannel_by_schid(struct subchannel_id schid)
161 dev = bus_find_device(&css_bus_type, NULL,
162 (void *)&schid, check_subchannel);
164 return dev ? to_subchannel(dev) : NULL;
169 css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
174 cc = stsch(schid, &schib);
179 if (sch && sch->schib.pmcw.dnv &&
180 (schib.pmcw.dev != sch->schib.pmcw.dev))
181 return CIO_REVALIDATE;
182 if (sch && !sch->lpm)
188 css_evaluate_subchannel(struct subchannel_id schid, int slow)
190 int event, ret, disc;
191 struct subchannel *sch;
194 sch = get_subchannel_by_schid(schid);
195 disc = sch ? device_is_disconnected(sch) : 0;
198 put_device(&sch->dev);
199 return 0; /* Already processed. */
202 * We've got a machine check, so running I/O won't get an interrupt.
203 * Kill any pending timers.
206 device_kill_pending_timer(sch);
207 if (!disc && !slow) {
209 put_device(&sch->dev);
210 return -EAGAIN; /* Will be done on the slow path. */
212 event = css_get_subchannel_status(sch, schid);
213 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
214 schid.ssid, schid.sch_no, event,
215 sch?(disc?"disconnected":"normal"):"unknown",
221 /* Never used this subchannel. Ignore. */
225 if (disc && (event == CIO_NO_PATH)) {
227 * Uargh, hack again. Because we don't get a machine
228 * check on configure on, our path bookkeeping can
229 * be out of date here (it's fine while we only do
230 * logical varying or get chsc machine checks). We
231 * need to force reprobing or we might miss devices
232 * coming operational again. It won't do harm in real
233 * no path situations.
235 spin_lock_irqsave(&sch->lock, flags);
236 device_trigger_reprobe(sch);
237 spin_unlock_irqrestore(&sch->lock, flags);
241 if (sch->driver && sch->driver->notify &&
242 sch->driver->notify(&sch->dev, event)) {
243 cio_disable_subchannel(sch);
244 device_set_disconnected(sch);
249 * Unregister subchannel.
250 * The device will be killed automatically.
252 cio_disable_subchannel(sch);
253 device_unregister(&sch->dev);
254 /* Reset intparm to zeroes. */
255 sch->schib.pmcw.intparm = 0;
257 put_device(&sch->dev);
262 * Revalidation machine check. Sick.
263 * We don't notify the driver since we have to throw the device
267 device_unregister(&sch->dev);
268 /* Reset intparm to zeroes. */
269 sch->schib.pmcw.intparm = 0;
271 put_device(&sch->dev);
272 ret = css_probe_device(schid);
275 * We can't immediately deregister the disconnected
276 * device since it might block.
278 spin_lock_irqsave(&sch->lock, flags);
279 device_trigger_reprobe(sch);
280 spin_unlock_irqrestore(&sch->lock, flags);
286 spin_lock_irqsave(&sch->lock, flags);
287 /* Get device operational again. */
288 device_trigger_reprobe(sch);
289 spin_unlock_irqrestore(&sch->lock, flags);
291 ret = sch ? 0 : css_probe_device(schid);
301 css_rescan_devices(struct subchannel_id schid, void *data)
303 return css_evaluate_subchannel(schid, 1);
306 struct slow_subchannel {
307 struct list_head slow_list;
308 struct subchannel_id schid;
311 static LIST_HEAD(slow_subchannels_head);
312 static DEFINE_SPINLOCK(slow_subchannel_lock);
315 css_trigger_slow_path(void)
317 CIO_TRACE_EVENT(4, "slowpath");
321 for_each_subchannel(css_rescan_devices, NULL);
325 spin_lock_irq(&slow_subchannel_lock);
326 while (!list_empty(&slow_subchannels_head)) {
327 struct slow_subchannel *slow_sch =
328 list_entry(slow_subchannels_head.next,
329 struct slow_subchannel, slow_list);
331 list_del_init(slow_subchannels_head.next);
332 spin_unlock_irq(&slow_subchannel_lock);
333 css_evaluate_subchannel(slow_sch->schid, 1);
334 spin_lock_irq(&slow_subchannel_lock);
337 spin_unlock_irq(&slow_subchannel_lock);
340 typedef void (*workfunc)(void *);
341 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
342 struct workqueue_struct *slow_path_wq;
344 /* Reprobe subchannel if unregistered. */
345 static int reprobe_subchannel(struct subchannel_id schid, void *data)
347 struct subchannel *sch;
350 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
351 schid.ssid, schid.sch_no);
355 sch = get_subchannel_by_schid(schid);
358 put_device(&sch->dev);
362 ret = css_probe_device(schid);
368 /* These should abort looping */
377 /* Work function used to reprobe all unregistered subchannels. */
378 static void reprobe_all(void *data)
382 CIO_MSG_EVENT(2, "reprobe start\n");
385 /* Make sure initial subchannel scan is done. */
386 wait_event(ccw_device_init_wq,
387 atomic_read(&ccw_device_init_count) == 0);
388 ret = for_each_subchannel(reprobe_subchannel, NULL);
390 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
394 DECLARE_WORK(css_reprobe_work, reprobe_all, NULL);
396 /* Schedule reprobing of all unregistered subchannels. */
397 void css_schedule_reprobe(void)
400 queue_work(ccw_device_work, &css_reprobe_work);
403 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
406 * Rescan for new devices. FIXME: This is slow.
407 * This function is called when we have lost CRWs due to overflows and we have
408 * to do subchannel housekeeping.
411 css_reiterate_subchannels(void)
413 css_clear_subchannel_slow_list();
418 * Called from the machine check handler for subchannel report words.
421 css_process_crw(int rsid1, int rsid2)
424 struct subchannel_id mchk_schid;
426 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
430 /* We need to iterate all subchannels anyway. */
433 init_subchannel_id(&mchk_schid);
434 mchk_schid.sch_no = rsid1;
436 mchk_schid.ssid = (rsid2 >> 8) & 3;
439 * Since we are always presented with IPI in the CRW, we have to
440 * use stsch() to find out if the subchannel in question has come
443 ret = css_evaluate_subchannel(mchk_schid, 0);
444 if (ret == -EAGAIN) {
445 if (css_enqueue_subchannel_slow(mchk_schid)) {
446 css_clear_subchannel_slow_list();
454 __init_channel_subsystem(struct subchannel_id schid, void *data)
456 struct subchannel *sch;
459 if (cio_is_console(schid))
460 sch = cio_get_console_subchannel();
462 sch = css_alloc_subchannel(schid);
471 panic("Out of memory in init_channel_subsystem\n");
472 /* -ENXIO: no more subchannels. */
475 /* -EIO: this subchannel set not supported. */
483 * We register ALL valid subchannels in ioinfo, even those
484 * that have been present before init_channel_subsystem.
485 * These subchannels can't have been registered yet (kmalloc
486 * not working) so we do it now. This is true e.g. for the
487 * console subchannel.
489 css_register_subchannel(sch);
494 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
496 if (css_characteristics_avail && css_general_characteristics.mcss) {
497 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
498 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
501 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
503 css->global_pgid.pgid_high.cpu_addr = 0;
506 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
507 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
508 css->global_pgid.tod_high = tod_high;
513 channel_subsystem_release(struct device *dev)
515 struct channel_subsystem *css;
518 mutex_destroy(&css->mutex);
523 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
526 struct channel_subsystem *css = to_css(dev);
530 return sprintf(buf, "%x\n", css->cm_enabled);
534 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
537 struct channel_subsystem *css = to_css(dev);
542 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
545 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
550 return ret < 0 ? ret : count;
553 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
555 static inline void __init
560 memset(css[nr], 0, sizeof(struct channel_subsystem));
561 mutex_init(&css[nr]->mutex);
564 sprintf(css[nr]->device.bus_id, "css%x", nr);
565 css[nr]->device.release = channel_subsystem_release;
566 tod_high = (u32) (get_clock() >> 32);
567 css_generate_pgid(css[nr], tod_high);
571 * Now that the driver core is running, we can setup our channel subsystem.
572 * The struct subchannel's are created during probing (except for the
573 * static console subchannel).
576 init_channel_subsystem (void)
580 if (chsc_determine_css_characteristics() == 0)
581 css_characteristics_avail = 1;
583 if ((ret = bus_register(&css_bus_type)))
586 /* Try to enable MSS. */
587 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
589 case 0: /* Success. */
590 max_ssid = __MAX_SSID;
597 /* Setup css structure. */
598 for (i = 0; i <= __MAX_CSSID; i++) {
599 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
605 ret = device_register(&css[i]->device);
608 if (css_characteristics_avail && css_chsc_characteristics.secm)
609 device_create_file(&css[i]->device,
610 &dev_attr_cm_enable);
616 for_each_subchannel(__init_channel_subsystem, NULL);
623 if (css_characteristics_avail && css_chsc_characteristics.secm)
624 device_remove_file(&css[i]->device,
625 &dev_attr_cm_enable);
626 device_unregister(&css[i]->device);
629 bus_unregister(&css_bus_type);
635 * find a driver for a subchannel. They identify by the subchannel
636 * type with the exception that the console subchannel driver has its own
637 * subchannel type although the device is an i/o subchannel
640 css_bus_match (struct device *dev, struct device_driver *drv)
642 struct subchannel *sch = container_of (dev, struct subchannel, dev);
643 struct css_driver *driver = container_of (drv, struct css_driver, drv);
645 if (sch->st == driver->subchannel_type)
652 css_probe (struct device *dev)
654 struct subchannel *sch;
656 sch = to_subchannel(dev);
657 sch->driver = container_of (dev->driver, struct css_driver, drv);
658 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
662 css_remove (struct device *dev)
664 struct subchannel *sch;
666 sch = to_subchannel(dev);
667 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
671 css_shutdown (struct device *dev)
673 struct subchannel *sch;
675 sch = to_subchannel(dev);
676 if (sch->driver->shutdown)
677 sch->driver->shutdown(sch);
680 struct bus_type css_bus_type = {
682 .match = css_bus_match,
684 .remove = css_remove,
685 .shutdown = css_shutdown,
688 subsys_initcall(init_channel_subsystem);
691 css_enqueue_subchannel_slow(struct subchannel_id schid)
693 struct slow_subchannel *new_slow_sch;
696 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
699 new_slow_sch->schid = schid;
700 spin_lock_irqsave(&slow_subchannel_lock, flags);
701 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
702 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
707 css_clear_subchannel_slow_list(void)
711 spin_lock_irqsave(&slow_subchannel_lock, flags);
712 while (!list_empty(&slow_subchannels_head)) {
713 struct slow_subchannel *slow_sch =
714 list_entry(slow_subchannels_head.next,
715 struct slow_subchannel, slow_list);
717 list_del_init(slow_subchannels_head.next);
720 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
726 css_slow_subchannels_exist(void)
728 return (!list_empty(&slow_subchannels_head));
731 MODULE_LICENSE("GPL");
732 EXPORT_SYMBOL(css_bus_type);
733 EXPORT_SYMBOL_GPL(css_characteristics_avail);