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)) {
111 extern int css_get_ssd_info(struct subchannel *sch);
114 int css_sch_device_register(struct subchannel *sch)
118 mutex_lock(&sch->reg_mutex);
119 ret = device_register(&sch->dev);
120 mutex_unlock(&sch->reg_mutex);
124 void css_sch_device_unregister(struct subchannel *sch)
126 mutex_lock(&sch->reg_mutex);
127 device_unregister(&sch->dev);
128 mutex_unlock(&sch->reg_mutex);
132 css_register_subchannel(struct subchannel *sch)
136 /* Initialize the subchannel structure */
137 sch->dev.parent = &css[0]->device;
138 sch->dev.bus = &css_bus_type;
139 sch->dev.release = &css_subchannel_release;
140 sch->dev.groups = subch_attr_groups;
142 css_get_ssd_info(sch);
144 /* make it known to the system */
145 ret = css_sch_device_register(sch);
147 printk (KERN_WARNING "%s: could not register %s\n",
148 __func__, sch->dev.bus_id);
155 css_probe_device(struct subchannel_id schid)
158 struct subchannel *sch;
160 sch = css_alloc_subchannel(schid);
163 ret = css_register_subchannel(sch);
165 css_free_subchannel(sch);
170 check_subchannel(struct device * dev, void * data)
172 struct subchannel *sch;
173 struct subchannel_id *schid = data;
175 sch = to_subchannel(dev);
176 return schid_equal(&sch->schid, schid);
180 get_subchannel_by_schid(struct subchannel_id schid)
184 dev = bus_find_device(&css_bus_type, NULL,
185 &schid, check_subchannel);
187 return dev ? to_subchannel(dev) : NULL;
190 static inline int css_get_subchannel_status(struct subchannel *sch)
194 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
196 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
197 return CIO_REVALIDATE;
203 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
205 int event, ret, disc;
207 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
209 spin_lock_irqsave(sch->lock, flags);
210 disc = device_is_disconnected(sch);
212 /* Disconnected devices are evaluated directly only.*/
213 spin_unlock_irqrestore(sch->lock, flags);
216 /* No interrupt after machine check - kill pending timers. */
217 device_kill_pending_timer(sch);
218 if (!disc && !slow) {
219 /* Non-disconnected devices are evaluated on the slow path. */
220 spin_unlock_irqrestore(sch->lock, flags);
223 event = css_get_subchannel_status(sch);
224 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
225 sch->schid.ssid, sch->schid.sch_no, event,
226 disc ? "disconnected" : "normal",
227 slow ? "slow" : "fast");
228 /* Analyze subchannel status. */
233 /* Check if paths have become available. */
239 /* Prevent unwanted effects when opening lock. */
240 cio_disable_subchannel(sch);
241 device_set_disconnected(sch);
242 /* Ask driver what to do with device. */
244 if (sch->driver && sch->driver->notify) {
245 spin_unlock_irqrestore(sch->lock, flags);
246 ret = sch->driver->notify(&sch->dev, event);
247 spin_lock_irqsave(sch->lock, flags);
253 /* Device will be removed, so no notify necessary. */
255 /* Reprobe because immediate unregister might block. */
258 action = UNREGISTER_PROBE;
262 /* Get device operational again. */
266 /* Perform action. */
270 case UNREGISTER_PROBE:
271 /* Unregister device (will use subchannel lock). */
272 spin_unlock_irqrestore(sch->lock, flags);
273 css_sch_device_unregister(sch);
274 spin_lock_irqsave(sch->lock, flags);
276 /* Reset intparm to zeroes. */
277 sch->schib.pmcw.intparm = 0;
281 device_trigger_reprobe(sch);
286 spin_unlock_irqrestore(sch->lock, flags);
287 /* Probe if necessary. */
288 if (action == UNREGISTER_PROBE)
289 ret = css_probe_device(sch->schid);
294 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
299 /* Will be done on the slow path. */
302 if (stsch(schid, &schib) || !schib.pmcw.dnv) {
303 /* Unusable - ignore. */
306 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
307 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
309 return css_probe_device(schid);
312 static int css_evaluate_subchannel(struct subchannel_id schid, int slow)
314 struct subchannel *sch;
317 sch = get_subchannel_by_schid(schid);
319 ret = css_evaluate_known_subchannel(sch, slow);
320 put_device(&sch->dev);
322 ret = css_evaluate_new_subchannel(schid, slow);
328 css_rescan_devices(struct subchannel_id schid, void *data)
330 return css_evaluate_subchannel(schid, 1);
333 struct slow_subchannel {
334 struct list_head slow_list;
335 struct subchannel_id schid;
338 static LIST_HEAD(slow_subchannels_head);
339 static DEFINE_SPINLOCK(slow_subchannel_lock);
342 css_trigger_slow_path(struct work_struct *unused)
344 CIO_TRACE_EVENT(4, "slowpath");
348 for_each_subchannel(css_rescan_devices, NULL);
352 spin_lock_irq(&slow_subchannel_lock);
353 while (!list_empty(&slow_subchannels_head)) {
354 struct slow_subchannel *slow_sch =
355 list_entry(slow_subchannels_head.next,
356 struct slow_subchannel, slow_list);
358 list_del_init(slow_subchannels_head.next);
359 spin_unlock_irq(&slow_subchannel_lock);
360 css_evaluate_subchannel(slow_sch->schid, 1);
361 spin_lock_irq(&slow_subchannel_lock);
364 spin_unlock_irq(&slow_subchannel_lock);
367 DECLARE_WORK(slow_path_work, css_trigger_slow_path);
368 struct workqueue_struct *slow_path_wq;
370 /* Reprobe subchannel if unregistered. */
371 static int reprobe_subchannel(struct subchannel_id schid, void *data)
373 struct subchannel *sch;
376 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
377 schid.ssid, schid.sch_no);
381 sch = get_subchannel_by_schid(schid);
384 put_device(&sch->dev);
388 ret = css_probe_device(schid);
394 /* These should abort looping */
403 /* Work function used to reprobe all unregistered subchannels. */
404 static void reprobe_all(struct work_struct *unused)
408 CIO_MSG_EVENT(2, "reprobe start\n");
411 /* Make sure initial subchannel scan is done. */
412 wait_event(ccw_device_init_wq,
413 atomic_read(&ccw_device_init_count) == 0);
414 ret = for_each_subchannel(reprobe_subchannel, NULL);
416 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
420 DECLARE_WORK(css_reprobe_work, reprobe_all);
422 /* Schedule reprobing of all unregistered subchannels. */
423 void css_schedule_reprobe(void)
426 queue_work(ccw_device_work, &css_reprobe_work);
429 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
432 * Rescan for new devices. FIXME: This is slow.
433 * This function is called when we have lost CRWs due to overflows and we have
434 * to do subchannel housekeeping.
437 css_reiterate_subchannels(void)
439 css_clear_subchannel_slow_list();
444 * Called from the machine check handler for subchannel report words.
447 css_process_crw(int rsid1, int rsid2)
450 struct subchannel_id mchk_schid;
452 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
456 /* We need to iterate all subchannels anyway. */
459 init_subchannel_id(&mchk_schid);
460 mchk_schid.sch_no = rsid1;
462 mchk_schid.ssid = (rsid2 >> 8) & 3;
465 * Since we are always presented with IPI in the CRW, we have to
466 * use stsch() to find out if the subchannel in question has come
469 ret = css_evaluate_subchannel(mchk_schid, 0);
470 if (ret == -EAGAIN) {
471 if (css_enqueue_subchannel_slow(mchk_schid)) {
472 css_clear_subchannel_slow_list();
480 __init_channel_subsystem(struct subchannel_id schid, void *data)
482 struct subchannel *sch;
485 if (cio_is_console(schid))
486 sch = cio_get_console_subchannel();
488 sch = css_alloc_subchannel(schid);
497 panic("Out of memory in init_channel_subsystem\n");
498 /* -ENXIO: no more subchannels. */
501 /* -EIO: this subchannel set not supported. */
509 * We register ALL valid subchannels in ioinfo, even those
510 * that have been present before init_channel_subsystem.
511 * These subchannels can't have been registered yet (kmalloc
512 * not working) so we do it now. This is true e.g. for the
513 * console subchannel.
515 css_register_subchannel(sch);
520 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
522 if (css_characteristics_avail && css_general_characteristics.mcss) {
523 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
524 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
527 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
529 css->global_pgid.pgid_high.cpu_addr = 0;
532 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
533 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
534 css->global_pgid.tod_high = tod_high;
539 channel_subsystem_release(struct device *dev)
541 struct channel_subsystem *css;
544 mutex_destroy(&css->mutex);
549 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
552 struct channel_subsystem *css = to_css(dev);
556 return sprintf(buf, "%x\n", css->cm_enabled);
560 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
561 const char *buf, size_t count)
563 struct channel_subsystem *css = to_css(dev);
568 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
571 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
576 return ret < 0 ? ret : count;
579 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
581 static inline int __init setup_css(int nr)
586 memset(css[nr], 0, sizeof(struct channel_subsystem));
587 css[nr]->pseudo_subchannel =
588 kzalloc(sizeof(*css[nr]->pseudo_subchannel), GFP_KERNEL);
589 if (!css[nr]->pseudo_subchannel)
591 css[nr]->pseudo_subchannel->dev.parent = &css[nr]->device;
592 css[nr]->pseudo_subchannel->dev.release = css_subchannel_release;
593 sprintf(css[nr]->pseudo_subchannel->dev.bus_id, "defunct");
594 ret = cio_create_sch_lock(css[nr]->pseudo_subchannel);
596 kfree(css[nr]->pseudo_subchannel);
599 mutex_init(&css[nr]->mutex);
602 sprintf(css[nr]->device.bus_id, "css%x", nr);
603 css[nr]->device.release = channel_subsystem_release;
604 tod_high = (u32) (get_clock() >> 32);
605 css_generate_pgid(css[nr], tod_high);
610 * Now that the driver core is running, we can setup our channel subsystem.
611 * The struct subchannel's are created during probing (except for the
612 * static console subchannel).
615 init_channel_subsystem (void)
619 if (chsc_determine_css_characteristics() == 0)
620 css_characteristics_avail = 1;
622 if ((ret = bus_register(&css_bus_type)))
625 /* Try to enable MSS. */
626 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
628 case 0: /* Success. */
629 max_ssid = __MAX_SSID;
636 /* Setup css structure. */
637 for (i = 0; i <= __MAX_CSSID; i++) {
638 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
646 ret = device_register(&css[i]->device);
649 if (css_characteristics_avail &&
650 css_chsc_characteristics.secm) {
651 ret = device_create_file(&css[i]->device,
652 &dev_attr_cm_enable);
656 ret = device_register(&css[i]->pseudo_subchannel->dev);
664 for_each_subchannel(__init_channel_subsystem, NULL);
667 device_remove_file(&css[i]->device, &dev_attr_cm_enable);
669 device_unregister(&css[i]->device);
671 kfree(css[i]->pseudo_subchannel->lock);
672 kfree(css[i]->pseudo_subchannel);
678 device_unregister(&css[i]->pseudo_subchannel->dev);
679 if (css_characteristics_avail && css_chsc_characteristics.secm)
680 device_remove_file(&css[i]->device,
681 &dev_attr_cm_enable);
682 device_unregister(&css[i]->device);
685 bus_unregister(&css_bus_type);
690 int sch_is_pseudo_sch(struct subchannel *sch)
692 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
696 * find a driver for a subchannel. They identify by the subchannel
697 * type with the exception that the console subchannel driver has its own
698 * subchannel type although the device is an i/o subchannel
701 css_bus_match (struct device *dev, struct device_driver *drv)
703 struct subchannel *sch = container_of (dev, struct subchannel, dev);
704 struct css_driver *driver = container_of (drv, struct css_driver, drv);
706 if (sch->st == driver->subchannel_type)
713 css_probe (struct device *dev)
715 struct subchannel *sch;
717 sch = to_subchannel(dev);
718 sch->driver = container_of (dev->driver, struct css_driver, drv);
719 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
723 css_remove (struct device *dev)
725 struct subchannel *sch;
727 sch = to_subchannel(dev);
728 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
732 css_shutdown (struct device *dev)
734 struct subchannel *sch;
736 sch = to_subchannel(dev);
737 if (sch->driver->shutdown)
738 sch->driver->shutdown(sch);
741 struct bus_type css_bus_type = {
743 .match = css_bus_match,
745 .remove = css_remove,
746 .shutdown = css_shutdown,
749 subsys_initcall(init_channel_subsystem);
752 css_enqueue_subchannel_slow(struct subchannel_id schid)
754 struct slow_subchannel *new_slow_sch;
757 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
760 new_slow_sch->schid = schid;
761 spin_lock_irqsave(&slow_subchannel_lock, flags);
762 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
763 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
768 css_clear_subchannel_slow_list(void)
772 spin_lock_irqsave(&slow_subchannel_lock, flags);
773 while (!list_empty(&slow_subchannels_head)) {
774 struct slow_subchannel *slow_sch =
775 list_entry(slow_subchannels_head.next,
776 struct slow_subchannel, slow_list);
778 list_del_init(slow_subchannels_head.next);
781 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
787 css_slow_subchannels_exist(void)
789 return (!list_empty(&slow_subchannels_head));
792 MODULE_LICENSE("GPL");
793 EXPORT_SYMBOL(css_bus_type);
794 EXPORT_SYMBOL_GPL(css_characteristics_avail);