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>
16 #include <linux/reboot.h>
20 #include "cio_debug.h"
27 int css_init_done = 0;
28 static int need_reprobe = 0;
29 static int max_ssid = 0;
31 struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
33 int css_characteristics_avail = 0;
36 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
38 struct subchannel_id schid;
41 init_subchannel_id(&schid);
45 ret = fn(schid, data);
48 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
50 } while (schid.ssid++ < max_ssid);
54 static struct subchannel *
55 css_alloc_subchannel(struct subchannel_id schid)
57 struct subchannel *sch;
60 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
62 return ERR_PTR(-ENOMEM);
63 ret = cio_validate_subchannel (sch, schid);
69 if (sch->st != SUBCHANNEL_TYPE_IO) {
70 /* For now we ignore all non-io subchannels. */
72 return ERR_PTR(-EINVAL);
76 * Set intparm to subchannel address.
77 * This is fine even on 64bit since the subchannel is always located
80 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
81 ret = cio_modify(sch);
91 css_free_subchannel(struct subchannel *sch)
94 /* Reset intparm to zeroes. */
95 sch->schib.pmcw.intparm = 0;
103 css_subchannel_release(struct device *dev)
105 struct subchannel *sch;
107 sch = to_subchannel(dev);
108 if (!cio_is_console(sch->schid)) {
114 static 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);
131 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
136 memset(ssd, 0, sizeof(struct chsc_ssd_info));
137 ssd->path_mask = pmcw->pim;
138 for (i = 0; i < 8; i++) {
140 if (pmcw->pim & mask) {
141 chp_id_init(&ssd->chpid[i]);
142 ssd->chpid[i].id = pmcw->chpid[i];
147 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
152 for (i = 0; i < 8; i++) {
154 if (ssd->path_mask & mask)
155 if (!chp_is_registered(ssd->chpid[i]))
156 chp_new(ssd->chpid[i]);
160 void css_update_ssd_info(struct subchannel *sch)
164 if (cio_is_console(sch->schid)) {
165 /* Console is initialized too early for functions requiring
166 * memory allocation. */
167 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
169 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
171 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
172 ssd_register_chpids(&sch->ssd_info);
176 static int css_register_subchannel(struct subchannel *sch)
180 /* Initialize the subchannel structure */
181 sch->dev.parent = &channel_subsystems[0]->device;
182 sch->dev.bus = &css_bus_type;
183 sch->dev.release = &css_subchannel_release;
184 sch->dev.groups = subch_attr_groups;
186 * We don't want to generate uevents for I/O subchannels that don't
187 * have a working ccw device behind them since they will be
188 * unregistered before they can be used anyway, so we delay the add
189 * uevent until after device recognition was successful.
191 if (!cio_is_console(sch->schid))
192 /* Console is special, no need to suppress. */
193 sch->dev.uevent_suppress = 1;
194 css_update_ssd_info(sch);
195 /* make it known to the system */
196 ret = css_sch_device_register(sch);
198 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
199 sch->schid.ssid, sch->schid.sch_no, ret);
205 static int css_probe_device(struct subchannel_id schid)
208 struct subchannel *sch;
210 sch = css_alloc_subchannel(schid);
213 ret = css_register_subchannel(sch);
215 css_free_subchannel(sch);
220 check_subchannel(struct device * dev, void * data)
222 struct subchannel *sch;
223 struct subchannel_id *schid = data;
225 sch = to_subchannel(dev);
226 return schid_equal(&sch->schid, schid);
230 get_subchannel_by_schid(struct subchannel_id schid)
234 dev = bus_find_device(&css_bus_type, NULL,
235 &schid, check_subchannel);
237 return dev ? to_subchannel(dev) : NULL;
240 static int css_get_subchannel_status(struct subchannel *sch)
244 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
246 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
247 return CIO_REVALIDATE;
253 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
255 int event, ret, disc;
257 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
259 spin_lock_irqsave(sch->lock, flags);
260 disc = device_is_disconnected(sch);
262 /* Disconnected devices are evaluated directly only.*/
263 spin_unlock_irqrestore(sch->lock, flags);
266 /* No interrupt after machine check - kill pending timers. */
267 device_kill_pending_timer(sch);
268 if (!disc && !slow) {
269 /* Non-disconnected devices are evaluated on the slow path. */
270 spin_unlock_irqrestore(sch->lock, flags);
273 event = css_get_subchannel_status(sch);
274 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
275 sch->schid.ssid, sch->schid.sch_no, event,
276 disc ? "disconnected" : "normal",
277 slow ? "slow" : "fast");
278 /* Analyze subchannel status. */
283 /* Check if paths have become available. */
289 /* Prevent unwanted effects when opening lock. */
290 cio_disable_subchannel(sch);
291 device_set_disconnected(sch);
292 /* Ask driver what to do with device. */
294 if (sch->driver && sch->driver->notify) {
295 spin_unlock_irqrestore(sch->lock, flags);
296 ret = sch->driver->notify(&sch->dev, event);
297 spin_lock_irqsave(sch->lock, flags);
303 /* Device will be removed, so no notify necessary. */
305 /* Reprobe because immediate unregister might block. */
308 action = UNREGISTER_PROBE;
312 /* Get device operational again. */
316 /* Perform action. */
320 case UNREGISTER_PROBE:
321 /* Unregister device (will use subchannel lock). */
322 spin_unlock_irqrestore(sch->lock, flags);
323 css_sch_device_unregister(sch);
324 spin_lock_irqsave(sch->lock, flags);
326 /* Reset intparm to zeroes. */
327 sch->schib.pmcw.intparm = 0;
331 device_trigger_reprobe(sch);
336 spin_unlock_irqrestore(sch->lock, flags);
337 /* Probe if necessary. */
338 if (action == UNREGISTER_PROBE)
339 ret = css_probe_device(sch->schid);
344 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
349 /* Will be done on the slow path. */
352 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
353 /* Unusable - ignore. */
356 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
357 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
359 return css_probe_device(schid);
362 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
364 struct subchannel *sch;
367 sch = get_subchannel_by_schid(schid);
369 ret = css_evaluate_known_subchannel(sch, slow);
370 put_device(&sch->dev);
372 ret = css_evaluate_new_subchannel(schid, slow);
374 css_schedule_eval(schid);
377 static struct idset *slow_subchannel_set;
378 static spinlock_t slow_subchannel_lock;
380 static int __init slow_subchannel_init(void)
382 spin_lock_init(&slow_subchannel_lock);
383 slow_subchannel_set = idset_sch_new();
384 if (!slow_subchannel_set) {
385 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
391 static void css_slow_path_func(struct work_struct *unused)
393 struct subchannel_id schid;
395 CIO_TRACE_EVENT(4, "slowpath");
396 spin_lock_irq(&slow_subchannel_lock);
397 init_subchannel_id(&schid);
398 while (idset_sch_get_first(slow_subchannel_set, &schid)) {
399 idset_sch_del(slow_subchannel_set, schid);
400 spin_unlock_irq(&slow_subchannel_lock);
401 css_evaluate_subchannel(schid, 1);
402 spin_lock_irq(&slow_subchannel_lock);
404 spin_unlock_irq(&slow_subchannel_lock);
407 static DECLARE_WORK(slow_path_work, css_slow_path_func);
408 struct workqueue_struct *slow_path_wq;
410 void css_schedule_eval(struct subchannel_id schid)
414 spin_lock_irqsave(&slow_subchannel_lock, flags);
415 idset_sch_add(slow_subchannel_set, schid);
416 queue_work(slow_path_wq, &slow_path_work);
417 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
420 void css_schedule_eval_all(void)
424 spin_lock_irqsave(&slow_subchannel_lock, flags);
425 idset_fill(slow_subchannel_set);
426 queue_work(slow_path_wq, &slow_path_work);
427 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
430 /* Reprobe subchannel if unregistered. */
431 static int reprobe_subchannel(struct subchannel_id schid, void *data)
433 struct subchannel *sch;
436 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
437 schid.ssid, schid.sch_no);
441 sch = get_subchannel_by_schid(schid);
444 put_device(&sch->dev);
448 ret = css_probe_device(schid);
454 /* These should abort looping */
463 /* Work function used to reprobe all unregistered subchannels. */
464 static void reprobe_all(struct work_struct *unused)
468 CIO_MSG_EVENT(2, "reprobe start\n");
471 /* Make sure initial subchannel scan is done. */
472 wait_event(ccw_device_init_wq,
473 atomic_read(&ccw_device_init_count) == 0);
474 ret = for_each_subchannel(reprobe_subchannel, NULL);
476 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
480 static DECLARE_WORK(css_reprobe_work, reprobe_all);
482 /* Schedule reprobing of all unregistered subchannels. */
483 void css_schedule_reprobe(void)
486 queue_work(slow_path_wq, &css_reprobe_work);
489 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
492 * Called from the machine check handler for subchannel report words.
494 void css_process_crw(int rsid1, int rsid2)
496 struct subchannel_id mchk_schid;
498 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
500 init_subchannel_id(&mchk_schid);
501 mchk_schid.sch_no = rsid1;
503 mchk_schid.ssid = (rsid2 >> 8) & 3;
506 * Since we are always presented with IPI in the CRW, we have to
507 * use stsch() to find out if the subchannel in question has come
510 css_evaluate_subchannel(mchk_schid, 0);
514 __init_channel_subsystem(struct subchannel_id schid, void *data)
516 struct subchannel *sch;
519 if (cio_is_console(schid))
520 sch = cio_get_console_subchannel();
522 sch = css_alloc_subchannel(schid);
531 panic("Out of memory in init_channel_subsystem\n");
532 /* -ENXIO: no more subchannels. */
535 /* -EIO: this subchannel set not supported. */
543 * We register ALL valid subchannels in ioinfo, even those
544 * that have been present before init_channel_subsystem.
545 * These subchannels can't have been registered yet (kmalloc
546 * not working) so we do it now. This is true e.g. for the
547 * console subchannel.
549 css_register_subchannel(sch);
554 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
556 if (css_characteristics_avail && css_general_characteristics.mcss) {
557 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
558 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
561 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
563 css->global_pgid.pgid_high.cpu_addr = 0;
566 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
567 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
568 css->global_pgid.tod_high = tod_high;
573 channel_subsystem_release(struct device *dev)
575 struct channel_subsystem *css;
578 mutex_destroy(&css->mutex);
583 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
586 struct channel_subsystem *css = to_css(dev);
590 return sprintf(buf, "%x\n", css->cm_enabled);
594 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
597 struct channel_subsystem *css = to_css(dev);
602 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
605 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
610 return ret < 0 ? ret : count;
613 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
615 static int __init setup_css(int nr)
619 struct channel_subsystem *css;
621 css = channel_subsystems[nr];
622 memset(css, 0, sizeof(struct channel_subsystem));
623 css->pseudo_subchannel =
624 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
625 if (!css->pseudo_subchannel)
627 css->pseudo_subchannel->dev.parent = &css->device;
628 css->pseudo_subchannel->dev.release = css_subchannel_release;
629 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
630 ret = cio_create_sch_lock(css->pseudo_subchannel);
632 kfree(css->pseudo_subchannel);
635 mutex_init(&css->mutex);
638 sprintf(css->device.bus_id, "css%x", nr);
639 css->device.release = channel_subsystem_release;
640 tod_high = (u32) (get_clock() >> 32);
641 css_generate_pgid(css, tod_high);
645 static int css_reboot_event(struct notifier_block *this,
652 for (i = 0; i <= __MAX_CSSID; i++) {
653 struct channel_subsystem *css;
655 css = channel_subsystems[i];
657 if (chsc_secm(css, 0))
664 static struct notifier_block css_reboot_notifier = {
665 .notifier_call = css_reboot_event,
669 * Now that the driver core is running, we can setup our channel subsystem.
670 * The struct subchannel's are created during probing (except for the
671 * static console subchannel).
674 init_channel_subsystem (void)
678 ret = chsc_determine_css_characteristics();
680 goto out; /* No need to continue. */
682 css_characteristics_avail = 1;
684 ret = chsc_alloc_sei_area();
688 ret = slow_subchannel_init();
692 if ((ret = bus_register(&css_bus_type)))
695 /* Try to enable MSS. */
696 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
698 case 0: /* Success. */
699 max_ssid = __MAX_SSID;
706 /* Setup css structure. */
707 for (i = 0; i <= __MAX_CSSID; i++) {
708 struct channel_subsystem *css;
710 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
715 channel_subsystems[i] = css;
719 ret = device_register(&css->device);
722 if (css_characteristics_avail &&
723 css_chsc_characteristics.secm) {
724 ret = device_create_file(&css->device,
725 &dev_attr_cm_enable);
729 ret = device_register(&css->pseudo_subchannel->dev);
733 ret = register_reboot_notifier(&css_reboot_notifier);
740 for_each_subchannel(__init_channel_subsystem, NULL);
743 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
745 device_remove_file(&channel_subsystems[i]->device,
746 &dev_attr_cm_enable);
748 device_unregister(&channel_subsystems[i]->device);
750 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
751 kfree(channel_subsystems[i]->pseudo_subchannel);
753 kfree(channel_subsystems[i]);
756 struct channel_subsystem *css;
759 css = channel_subsystems[i];
760 device_unregister(&css->pseudo_subchannel->dev);
761 if (css_characteristics_avail && css_chsc_characteristics.secm)
762 device_remove_file(&css->device,
763 &dev_attr_cm_enable);
764 device_unregister(&css->device);
767 bus_unregister(&css_bus_type);
769 chsc_free_sei_area();
770 kfree(slow_subchannel_set);
771 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
776 int sch_is_pseudo_sch(struct subchannel *sch)
778 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
782 * find a driver for a subchannel. They identify by the subchannel
783 * type with the exception that the console subchannel driver has its own
784 * subchannel type although the device is an i/o subchannel
787 css_bus_match (struct device *dev, struct device_driver *drv)
789 struct subchannel *sch = container_of (dev, struct subchannel, dev);
790 struct css_driver *driver = container_of (drv, struct css_driver, drv);
792 if (sch->st == driver->subchannel_type)
799 css_probe (struct device *dev)
801 struct subchannel *sch;
803 sch = to_subchannel(dev);
804 sch->driver = container_of (dev->driver, struct css_driver, drv);
805 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
809 css_remove (struct device *dev)
811 struct subchannel *sch;
813 sch = to_subchannel(dev);
814 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
818 css_shutdown (struct device *dev)
820 struct subchannel *sch;
822 sch = to_subchannel(dev);
823 if (sch->driver->shutdown)
824 sch->driver->shutdown(sch);
827 struct bus_type css_bus_type = {
829 .match = css_bus_match,
831 .remove = css_remove,
832 .shutdown = css_shutdown,
835 subsys_initcall(init_channel_subsystem);
837 MODULE_LICENSE("GPL");
838 EXPORT_SYMBOL(css_bus_type);
839 EXPORT_SYMBOL_GPL(css_characteristics_avail);