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;
185 css_update_ssd_info(sch);
186 /* make it known to the system */
187 ret = css_sch_device_register(sch);
189 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
190 sch->schid.ssid, sch->schid.sch_no, ret);
196 static int css_probe_device(struct subchannel_id schid)
199 struct subchannel *sch;
201 sch = css_alloc_subchannel(schid);
204 ret = css_register_subchannel(sch);
206 css_free_subchannel(sch);
211 check_subchannel(struct device * dev, void * data)
213 struct subchannel *sch;
214 struct subchannel_id *schid = data;
216 sch = to_subchannel(dev);
217 return schid_equal(&sch->schid, schid);
221 get_subchannel_by_schid(struct subchannel_id schid)
225 dev = bus_find_device(&css_bus_type, NULL,
226 &schid, check_subchannel);
228 return dev ? to_subchannel(dev) : NULL;
231 static int css_get_subchannel_status(struct subchannel *sch)
235 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
237 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
238 return CIO_REVALIDATE;
244 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
246 int event, ret, disc;
248 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
250 spin_lock_irqsave(sch->lock, flags);
251 disc = device_is_disconnected(sch);
253 /* Disconnected devices are evaluated directly only.*/
254 spin_unlock_irqrestore(sch->lock, flags);
257 /* No interrupt after machine check - kill pending timers. */
258 device_kill_pending_timer(sch);
259 if (!disc && !slow) {
260 /* Non-disconnected devices are evaluated on the slow path. */
261 spin_unlock_irqrestore(sch->lock, flags);
264 event = css_get_subchannel_status(sch);
265 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
266 sch->schid.ssid, sch->schid.sch_no, event,
267 disc ? "disconnected" : "normal",
268 slow ? "slow" : "fast");
269 /* Analyze subchannel status. */
274 /* Check if paths have become available. */
280 /* Prevent unwanted effects when opening lock. */
281 cio_disable_subchannel(sch);
282 device_set_disconnected(sch);
283 /* Ask driver what to do with device. */
285 if (sch->driver && sch->driver->notify) {
286 spin_unlock_irqrestore(sch->lock, flags);
287 ret = sch->driver->notify(&sch->dev, event);
288 spin_lock_irqsave(sch->lock, flags);
294 /* Device will be removed, so no notify necessary. */
296 /* Reprobe because immediate unregister might block. */
299 action = UNREGISTER_PROBE;
303 /* Get device operational again. */
307 /* Perform action. */
311 case UNREGISTER_PROBE:
312 /* Unregister device (will use subchannel lock). */
313 spin_unlock_irqrestore(sch->lock, flags);
314 css_sch_device_unregister(sch);
315 spin_lock_irqsave(sch->lock, flags);
317 /* Reset intparm to zeroes. */
318 sch->schib.pmcw.intparm = 0;
322 device_trigger_reprobe(sch);
327 spin_unlock_irqrestore(sch->lock, flags);
328 /* Probe if necessary. */
329 if (action == UNREGISTER_PROBE)
330 ret = css_probe_device(sch->schid);
335 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
340 /* Will be done on the slow path. */
343 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
344 /* Unusable - ignore. */
347 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
348 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
350 return css_probe_device(schid);
353 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
355 struct subchannel *sch;
358 sch = get_subchannel_by_schid(schid);
360 ret = css_evaluate_known_subchannel(sch, slow);
361 put_device(&sch->dev);
363 ret = css_evaluate_new_subchannel(schid, slow);
365 css_schedule_eval(schid);
368 static struct idset *slow_subchannel_set;
369 static spinlock_t slow_subchannel_lock;
371 static int __init slow_subchannel_init(void)
373 spin_lock_init(&slow_subchannel_lock);
374 slow_subchannel_set = idset_sch_new();
375 if (!slow_subchannel_set) {
376 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
382 static void css_slow_path_func(struct work_struct *unused)
384 struct subchannel_id schid;
386 CIO_TRACE_EVENT(4, "slowpath");
387 spin_lock_irq(&slow_subchannel_lock);
388 init_subchannel_id(&schid);
389 while (idset_sch_get_first(slow_subchannel_set, &schid)) {
390 idset_sch_del(slow_subchannel_set, schid);
391 spin_unlock_irq(&slow_subchannel_lock);
392 css_evaluate_subchannel(schid, 1);
393 spin_lock_irq(&slow_subchannel_lock);
395 spin_unlock_irq(&slow_subchannel_lock);
398 static DECLARE_WORK(slow_path_work, css_slow_path_func);
399 struct workqueue_struct *slow_path_wq;
401 void css_schedule_eval(struct subchannel_id schid)
405 spin_lock_irqsave(&slow_subchannel_lock, flags);
406 idset_sch_add(slow_subchannel_set, schid);
407 queue_work(slow_path_wq, &slow_path_work);
408 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
411 void css_schedule_eval_all(void)
415 spin_lock_irqsave(&slow_subchannel_lock, flags);
416 idset_fill(slow_subchannel_set);
417 queue_work(slow_path_wq, &slow_path_work);
418 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
421 /* Reprobe subchannel if unregistered. */
422 static int reprobe_subchannel(struct subchannel_id schid, void *data)
424 struct subchannel *sch;
427 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
428 schid.ssid, schid.sch_no);
432 sch = get_subchannel_by_schid(schid);
435 put_device(&sch->dev);
439 ret = css_probe_device(schid);
445 /* These should abort looping */
454 /* Work function used to reprobe all unregistered subchannels. */
455 static void reprobe_all(struct work_struct *unused)
459 CIO_MSG_EVENT(2, "reprobe start\n");
462 /* Make sure initial subchannel scan is done. */
463 wait_event(ccw_device_init_wq,
464 atomic_read(&ccw_device_init_count) == 0);
465 ret = for_each_subchannel(reprobe_subchannel, NULL);
467 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
471 static DECLARE_WORK(css_reprobe_work, reprobe_all);
473 /* Schedule reprobing of all unregistered subchannels. */
474 void css_schedule_reprobe(void)
477 queue_work(ccw_device_work, &css_reprobe_work);
480 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
483 * Called from the machine check handler for subchannel report words.
485 void css_process_crw(int rsid1, int rsid2)
487 struct subchannel_id mchk_schid;
489 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
491 init_subchannel_id(&mchk_schid);
492 mchk_schid.sch_no = rsid1;
494 mchk_schid.ssid = (rsid2 >> 8) & 3;
497 * Since we are always presented with IPI in the CRW, we have to
498 * use stsch() to find out if the subchannel in question has come
501 css_evaluate_subchannel(mchk_schid, 0);
505 __init_channel_subsystem(struct subchannel_id schid, void *data)
507 struct subchannel *sch;
510 if (cio_is_console(schid))
511 sch = cio_get_console_subchannel();
513 sch = css_alloc_subchannel(schid);
522 panic("Out of memory in init_channel_subsystem\n");
523 /* -ENXIO: no more subchannels. */
526 /* -EIO: this subchannel set not supported. */
534 * We register ALL valid subchannels in ioinfo, even those
535 * that have been present before init_channel_subsystem.
536 * These subchannels can't have been registered yet (kmalloc
537 * not working) so we do it now. This is true e.g. for the
538 * console subchannel.
540 css_register_subchannel(sch);
545 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
547 if (css_characteristics_avail && css_general_characteristics.mcss) {
548 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
549 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
552 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
554 css->global_pgid.pgid_high.cpu_addr = 0;
557 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
558 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
559 css->global_pgid.tod_high = tod_high;
564 channel_subsystem_release(struct device *dev)
566 struct channel_subsystem *css;
569 mutex_destroy(&css->mutex);
574 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
577 struct channel_subsystem *css = to_css(dev);
581 return sprintf(buf, "%x\n", css->cm_enabled);
585 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
586 const char *buf, size_t count)
588 struct channel_subsystem *css = to_css(dev);
593 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
596 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
601 return ret < 0 ? ret : count;
604 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
606 static int __init setup_css(int nr)
610 struct channel_subsystem *css;
612 css = channel_subsystems[nr];
613 memset(css, 0, sizeof(struct channel_subsystem));
614 css->pseudo_subchannel =
615 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
616 if (!css->pseudo_subchannel)
618 css->pseudo_subchannel->dev.parent = &css->device;
619 css->pseudo_subchannel->dev.release = css_subchannel_release;
620 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
621 ret = cio_create_sch_lock(css->pseudo_subchannel);
623 kfree(css->pseudo_subchannel);
626 mutex_init(&css->mutex);
629 sprintf(css->device.bus_id, "css%x", nr);
630 css->device.release = channel_subsystem_release;
631 tod_high = (u32) (get_clock() >> 32);
632 css_generate_pgid(css, tod_high);
636 static int css_reboot_event(struct notifier_block *this,
643 for (i = 0; i <= __MAX_CSSID; i++) {
644 struct channel_subsystem *css;
646 css = channel_subsystems[i];
648 if (chsc_secm(css, 0))
655 static struct notifier_block css_reboot_notifier = {
656 .notifier_call = css_reboot_event,
660 * Now that the driver core is running, we can setup our channel subsystem.
661 * The struct subchannel's are created during probing (except for the
662 * static console subchannel).
665 init_channel_subsystem (void)
669 ret = chsc_determine_css_characteristics();
671 goto out; /* No need to continue. */
673 css_characteristics_avail = 1;
675 ret = chsc_alloc_sei_area();
679 ret = slow_subchannel_init();
683 if ((ret = bus_register(&css_bus_type)))
686 /* Try to enable MSS. */
687 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
689 case 0: /* Success. */
690 max_ssid = __MAX_SSID;
697 /* Setup css structure. */
698 for (i = 0; i <= __MAX_CSSID; i++) {
699 struct channel_subsystem *css;
701 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
706 channel_subsystems[i] = css;
710 ret = device_register(&css->device);
713 if (css_characteristics_avail &&
714 css_chsc_characteristics.secm) {
715 ret = device_create_file(&css->device,
716 &dev_attr_cm_enable);
720 ret = device_register(&css->pseudo_subchannel->dev);
724 ret = register_reboot_notifier(&css_reboot_notifier);
731 for_each_subchannel(__init_channel_subsystem, NULL);
734 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
736 device_remove_file(&channel_subsystems[i]->device,
737 &dev_attr_cm_enable);
739 device_unregister(&channel_subsystems[i]->device);
741 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
742 kfree(channel_subsystems[i]->pseudo_subchannel);
744 kfree(channel_subsystems[i]);
747 struct channel_subsystem *css;
750 css = channel_subsystems[i];
751 device_unregister(&css->pseudo_subchannel->dev);
752 if (css_characteristics_avail && css_chsc_characteristics.secm)
753 device_remove_file(&css->device,
754 &dev_attr_cm_enable);
755 device_unregister(&css->device);
758 bus_unregister(&css_bus_type);
760 chsc_free_sei_area();
761 kfree(slow_subchannel_set);
762 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
767 int sch_is_pseudo_sch(struct subchannel *sch)
769 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
773 * find a driver for a subchannel. They identify by the subchannel
774 * type with the exception that the console subchannel driver has its own
775 * subchannel type although the device is an i/o subchannel
778 css_bus_match (struct device *dev, struct device_driver *drv)
780 struct subchannel *sch = container_of (dev, struct subchannel, dev);
781 struct css_driver *driver = container_of (drv, struct css_driver, drv);
783 if (sch->st == driver->subchannel_type)
790 css_probe (struct device *dev)
792 struct subchannel *sch;
794 sch = to_subchannel(dev);
795 sch->driver = container_of (dev->driver, struct css_driver, drv);
796 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
800 css_remove (struct device *dev)
802 struct subchannel *sch;
804 sch = to_subchannel(dev);
805 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
809 css_shutdown (struct device *dev)
811 struct subchannel *sch;
813 sch = to_subchannel(dev);
814 if (sch->driver->shutdown)
815 sch->driver->shutdown(sch);
818 struct bus_type css_bus_type = {
820 .match = css_bus_match,
822 .remove = css_remove,
823 .shutdown = css_shutdown,
826 subsys_initcall(init_channel_subsystem);
828 MODULE_LICENSE("GPL");
829 EXPORT_SYMBOL(css_bus_type);
830 EXPORT_SYMBOL_GPL(css_characteristics_avail);