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);
57 int (*fn_known_sch)(struct subchannel *, void *);
58 int (*fn_unknown_sch)(struct subchannel_id, void *);
61 static int call_fn_known_sch(struct device *dev, void *data)
63 struct subchannel *sch = to_subchannel(dev);
64 struct cb_data *cb = data;
67 idset_sch_del(cb->set, sch->schid);
69 rc = cb->fn_known_sch(sch, cb->data);
73 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
75 struct cb_data *cb = data;
78 if (idset_sch_contains(cb->set, schid))
79 rc = cb->fn_unknown_sch(schid, cb->data);
83 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
84 int (*fn_unknown)(struct subchannel_id,
90 cb.set = idset_sch_new();
95 cb.fn_known_sch = fn_known;
96 cb.fn_unknown_sch = fn_unknown;
97 /* Process registered subchannels. */
98 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
101 /* Process unregistered subchannels. */
103 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
110 static struct subchannel *
111 css_alloc_subchannel(struct subchannel_id schid)
113 struct subchannel *sch;
116 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
118 return ERR_PTR(-ENOMEM);
119 ret = cio_validate_subchannel (sch, schid);
128 css_free_subchannel(struct subchannel *sch)
131 /* Reset intparm to zeroes. */
132 sch->schib.pmcw.intparm = 0;
140 css_subchannel_release(struct device *dev)
142 struct subchannel *sch;
144 sch = to_subchannel(dev);
145 if (!cio_is_console(sch->schid)) {
151 static int css_sch_device_register(struct subchannel *sch)
155 mutex_lock(&sch->reg_mutex);
156 ret = device_register(&sch->dev);
157 mutex_unlock(&sch->reg_mutex);
161 void css_sch_device_unregister(struct subchannel *sch)
163 mutex_lock(&sch->reg_mutex);
164 device_unregister(&sch->dev);
165 mutex_unlock(&sch->reg_mutex);
168 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
173 memset(ssd, 0, sizeof(struct chsc_ssd_info));
174 ssd->path_mask = pmcw->pim;
175 for (i = 0; i < 8; i++) {
177 if (pmcw->pim & mask) {
178 chp_id_init(&ssd->chpid[i]);
179 ssd->chpid[i].id = pmcw->chpid[i];
184 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
189 for (i = 0; i < 8; i++) {
191 if (ssd->path_mask & mask)
192 if (!chp_is_registered(ssd->chpid[i]))
193 chp_new(ssd->chpid[i]);
197 void css_update_ssd_info(struct subchannel *sch)
201 if (cio_is_console(sch->schid)) {
202 /* Console is initialized too early for functions requiring
203 * memory allocation. */
204 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
206 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
208 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
209 ssd_register_chpids(&sch->ssd_info);
213 static int css_register_subchannel(struct subchannel *sch)
217 /* Initialize the subchannel structure */
218 sch->dev.parent = &channel_subsystems[0]->device;
219 sch->dev.bus = &css_bus_type;
220 sch->dev.release = &css_subchannel_release;
221 sch->dev.groups = subch_attr_groups;
223 * We don't want to generate uevents for I/O subchannels that don't
224 * have a working ccw device behind them since they will be
225 * unregistered before they can be used anyway, so we delay the add
226 * uevent until after device recognition was successful.
228 if (!cio_is_console(sch->schid))
229 /* Console is special, no need to suppress. */
230 sch->dev.uevent_suppress = 1;
231 css_update_ssd_info(sch);
232 /* make it known to the system */
233 ret = css_sch_device_register(sch);
235 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
236 sch->schid.ssid, sch->schid.sch_no, ret);
242 static int css_probe_device(struct subchannel_id schid)
245 struct subchannel *sch;
247 sch = css_alloc_subchannel(schid);
250 ret = css_register_subchannel(sch);
252 css_free_subchannel(sch);
257 check_subchannel(struct device * dev, void * data)
259 struct subchannel *sch;
260 struct subchannel_id *schid = data;
262 sch = to_subchannel(dev);
263 return schid_equal(&sch->schid, schid);
267 get_subchannel_by_schid(struct subchannel_id schid)
271 dev = bus_find_device(&css_bus_type, NULL,
272 &schid, check_subchannel);
274 return dev ? to_subchannel(dev) : NULL;
278 * css_sch_is_valid() - check if a subchannel is valid
279 * @schib: subchannel information block for the subchannel
281 int css_sch_is_valid(struct schib *schib)
283 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
287 EXPORT_SYMBOL_GPL(css_sch_is_valid);
289 static int css_get_subchannel_status(struct subchannel *sch)
293 if (stsch(sch->schid, &schib))
295 if (!css_sch_is_valid(&schib))
297 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
298 return CIO_REVALIDATE;
304 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
306 int event, ret, disc;
308 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
310 spin_lock_irqsave(sch->lock, flags);
311 disc = device_is_disconnected(sch);
313 /* Disconnected devices are evaluated directly only.*/
314 spin_unlock_irqrestore(sch->lock, flags);
317 /* No interrupt after machine check - kill pending timers. */
318 device_kill_pending_timer(sch);
319 if (!disc && !slow) {
320 /* Non-disconnected devices are evaluated on the slow path. */
321 spin_unlock_irqrestore(sch->lock, flags);
324 event = css_get_subchannel_status(sch);
325 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
326 sch->schid.ssid, sch->schid.sch_no, event,
327 disc ? "disconnected" : "normal",
328 slow ? "slow" : "fast");
329 /* Analyze subchannel status. */
334 /* Check if paths have become available. */
340 /* Prevent unwanted effects when opening lock. */
341 cio_disable_subchannel(sch);
342 device_set_disconnected(sch);
343 /* Ask driver what to do with device. */
345 if (sch->driver && sch->driver->notify) {
346 spin_unlock_irqrestore(sch->lock, flags);
347 ret = sch->driver->notify(sch, event);
348 spin_lock_irqsave(sch->lock, flags);
354 /* Device will be removed, so no notify necessary. */
356 /* Reprobe because immediate unregister might block. */
359 action = UNREGISTER_PROBE;
363 /* Get device operational again. */
367 /* Perform action. */
371 case UNREGISTER_PROBE:
372 /* Unregister device (will use subchannel lock). */
373 spin_unlock_irqrestore(sch->lock, flags);
374 css_sch_device_unregister(sch);
375 spin_lock_irqsave(sch->lock, flags);
377 /* Reset intparm to zeroes. */
378 sch->schib.pmcw.intparm = 0;
382 device_trigger_reprobe(sch);
387 spin_unlock_irqrestore(sch->lock, flags);
388 /* Probe if necessary. */
389 if (action == UNREGISTER_PROBE)
390 ret = css_probe_device(sch->schid);
395 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
400 /* Will be done on the slow path. */
403 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
404 /* Unusable - ignore. */
407 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
408 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
410 return css_probe_device(schid);
413 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
415 struct subchannel *sch;
418 sch = get_subchannel_by_schid(schid);
420 ret = css_evaluate_known_subchannel(sch, slow);
421 put_device(&sch->dev);
423 ret = css_evaluate_new_subchannel(schid, slow);
425 css_schedule_eval(schid);
428 static struct idset *slow_subchannel_set;
429 static spinlock_t slow_subchannel_lock;
431 static int __init slow_subchannel_init(void)
433 spin_lock_init(&slow_subchannel_lock);
434 slow_subchannel_set = idset_sch_new();
435 if (!slow_subchannel_set) {
436 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
442 static int slow_eval_known_fn(struct subchannel *sch, void *data)
447 spin_lock_irq(&slow_subchannel_lock);
448 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
449 idset_sch_del(slow_subchannel_set, sch->schid);
450 spin_unlock_irq(&slow_subchannel_lock);
452 rc = css_evaluate_known_subchannel(sch, 1);
454 css_schedule_eval(sch->schid);
459 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
464 spin_lock_irq(&slow_subchannel_lock);
465 eval = idset_sch_contains(slow_subchannel_set, schid);
466 idset_sch_del(slow_subchannel_set, schid);
467 spin_unlock_irq(&slow_subchannel_lock);
469 rc = css_evaluate_new_subchannel(schid, 1);
472 css_schedule_eval(schid);
478 /* These should abort looping */
487 static void css_slow_path_func(struct work_struct *unused)
489 CIO_TRACE_EVENT(4, "slowpath");
490 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
494 static DECLARE_WORK(slow_path_work, css_slow_path_func);
495 struct workqueue_struct *slow_path_wq;
497 void css_schedule_eval(struct subchannel_id schid)
501 spin_lock_irqsave(&slow_subchannel_lock, flags);
502 idset_sch_add(slow_subchannel_set, schid);
503 queue_work(slow_path_wq, &slow_path_work);
504 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
507 void css_schedule_eval_all(void)
511 spin_lock_irqsave(&slow_subchannel_lock, flags);
512 idset_fill(slow_subchannel_set);
513 queue_work(slow_path_wq, &slow_path_work);
514 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
517 void css_wait_for_slow_path(void)
519 flush_workqueue(ccw_device_notify_work);
520 flush_workqueue(slow_path_wq);
523 /* Reprobe subchannel if unregistered. */
524 static int reprobe_subchannel(struct subchannel_id schid, void *data)
528 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
529 schid.ssid, schid.sch_no);
533 ret = css_probe_device(schid);
540 /* These should abort looping */
549 /* Work function used to reprobe all unregistered subchannels. */
550 static void reprobe_all(struct work_struct *unused)
554 CIO_MSG_EVENT(4, "reprobe start\n");
557 /* Make sure initial subchannel scan is done. */
558 wait_event(ccw_device_init_wq,
559 atomic_read(&ccw_device_init_count) == 0);
560 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
562 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
566 static DECLARE_WORK(css_reprobe_work, reprobe_all);
568 /* Schedule reprobing of all unregistered subchannels. */
569 void css_schedule_reprobe(void)
572 queue_work(slow_path_wq, &css_reprobe_work);
575 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
578 * Called from the machine check handler for subchannel report words.
580 void css_process_crw(int rsid1, int rsid2)
582 struct subchannel_id mchk_schid;
584 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
586 init_subchannel_id(&mchk_schid);
587 mchk_schid.sch_no = rsid1;
589 mchk_schid.ssid = (rsid2 >> 8) & 3;
592 * Since we are always presented with IPI in the CRW, we have to
593 * use stsch() to find out if the subchannel in question has come
596 css_evaluate_subchannel(mchk_schid, 0);
600 __init_channel_subsystem(struct subchannel_id schid, void *data)
602 struct subchannel *sch;
605 if (cio_is_console(schid))
606 sch = cio_get_console_subchannel();
608 sch = css_alloc_subchannel(schid);
617 panic("Out of memory in init_channel_subsystem\n");
618 /* -ENXIO: no more subchannels. */
621 /* -EIO: this subchannel set not supported. */
629 * We register ALL valid subchannels in ioinfo, even those
630 * that have been present before init_channel_subsystem.
631 * These subchannels can't have been registered yet (kmalloc
632 * not working) so we do it now. This is true e.g. for the
633 * console subchannel.
635 css_register_subchannel(sch);
640 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
642 if (css_characteristics_avail && css_general_characteristics.mcss) {
643 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
644 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
647 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
649 css->global_pgid.pgid_high.cpu_addr = 0;
652 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
653 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
654 css->global_pgid.tod_high = tod_high;
659 channel_subsystem_release(struct device *dev)
661 struct channel_subsystem *css;
664 mutex_destroy(&css->mutex);
669 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
672 struct channel_subsystem *css = to_css(dev);
677 mutex_lock(&css->mutex);
678 ret = sprintf(buf, "%x\n", css->cm_enabled);
679 mutex_unlock(&css->mutex);
684 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
687 struct channel_subsystem *css = to_css(dev);
691 ret = strict_strtoul(buf, 16, &val);
694 mutex_lock(&css->mutex);
697 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
700 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
705 mutex_unlock(&css->mutex);
706 return ret < 0 ? ret : count;
709 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
711 static int __init setup_css(int nr)
715 struct channel_subsystem *css;
717 css = channel_subsystems[nr];
718 memset(css, 0, sizeof(struct channel_subsystem));
719 css->pseudo_subchannel =
720 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
721 if (!css->pseudo_subchannel)
723 css->pseudo_subchannel->dev.parent = &css->device;
724 css->pseudo_subchannel->dev.release = css_subchannel_release;
725 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
726 ret = cio_create_sch_lock(css->pseudo_subchannel);
728 kfree(css->pseudo_subchannel);
731 mutex_init(&css->mutex);
734 sprintf(css->device.bus_id, "css%x", nr);
735 css->device.release = channel_subsystem_release;
736 tod_high = (u32) (get_clock() >> 32);
737 css_generate_pgid(css, tod_high);
741 static int css_reboot_event(struct notifier_block *this,
748 for (i = 0; i <= __MAX_CSSID; i++) {
749 struct channel_subsystem *css;
751 css = channel_subsystems[i];
752 mutex_lock(&css->mutex);
754 if (chsc_secm(css, 0))
756 mutex_unlock(&css->mutex);
762 static struct notifier_block css_reboot_notifier = {
763 .notifier_call = css_reboot_event,
767 * Now that the driver core is running, we can setup our channel subsystem.
768 * The struct subchannel's are created during probing (except for the
769 * static console subchannel).
772 init_channel_subsystem (void)
776 ret = chsc_determine_css_characteristics();
778 goto out; /* No need to continue. */
780 css_characteristics_avail = 1;
782 ret = chsc_alloc_sei_area();
786 ret = slow_subchannel_init();
790 if ((ret = bus_register(&css_bus_type)))
793 /* Try to enable MSS. */
794 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
796 case 0: /* Success. */
797 max_ssid = __MAX_SSID;
804 /* Setup css structure. */
805 for (i = 0; i <= __MAX_CSSID; i++) {
806 struct channel_subsystem *css;
808 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
813 channel_subsystems[i] = css;
817 ret = device_register(&css->device);
820 if (css_characteristics_avail &&
821 css_chsc_characteristics.secm) {
822 ret = device_create_file(&css->device,
823 &dev_attr_cm_enable);
827 ret = device_register(&css->pseudo_subchannel->dev);
831 ret = register_reboot_notifier(&css_reboot_notifier);
838 for_each_subchannel(__init_channel_subsystem, NULL);
841 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
843 device_remove_file(&channel_subsystems[i]->device,
844 &dev_attr_cm_enable);
846 device_unregister(&channel_subsystems[i]->device);
848 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
849 kfree(channel_subsystems[i]->pseudo_subchannel);
851 kfree(channel_subsystems[i]);
854 struct channel_subsystem *css;
857 css = channel_subsystems[i];
858 device_unregister(&css->pseudo_subchannel->dev);
859 if (css_characteristics_avail && css_chsc_characteristics.secm)
860 device_remove_file(&css->device,
861 &dev_attr_cm_enable);
862 device_unregister(&css->device);
865 bus_unregister(&css_bus_type);
867 chsc_free_sei_area();
868 kfree(slow_subchannel_set);
869 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
874 int sch_is_pseudo_sch(struct subchannel *sch)
876 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
880 * find a driver for a subchannel. They identify by the subchannel
881 * type with the exception that the console subchannel driver has its own
882 * subchannel type although the device is an i/o subchannel
885 css_bus_match (struct device *dev, struct device_driver *drv)
887 struct subchannel *sch = to_subchannel(dev);
888 struct css_driver *driver = to_cssdriver(drv);
890 if (sch->st == driver->subchannel_type)
896 static int css_probe(struct device *dev)
898 struct subchannel *sch;
901 sch = to_subchannel(dev);
902 sch->driver = to_cssdriver(dev->driver);
903 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
909 static int css_remove(struct device *dev)
911 struct subchannel *sch;
914 sch = to_subchannel(dev);
915 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
920 static void css_shutdown(struct device *dev)
922 struct subchannel *sch;
924 sch = to_subchannel(dev);
925 if (sch->driver && sch->driver->shutdown)
926 sch->driver->shutdown(sch);
929 struct bus_type css_bus_type = {
931 .match = css_bus_match,
933 .remove = css_remove,
934 .shutdown = css_shutdown,
938 * css_driver_register - register a css driver
939 * @cdrv: css driver to register
941 * This is mainly a wrapper around driver_register that sets name
942 * and bus_type in the embedded struct device_driver correctly.
944 int css_driver_register(struct css_driver *cdrv)
946 cdrv->drv.name = cdrv->name;
947 cdrv->drv.bus = &css_bus_type;
948 cdrv->drv.owner = cdrv->owner;
949 return driver_register(&cdrv->drv);
951 EXPORT_SYMBOL_GPL(css_driver_register);
954 * css_driver_unregister - unregister a css driver
955 * @cdrv: css driver to unregister
957 * This is a wrapper around driver_unregister.
959 void css_driver_unregister(struct css_driver *cdrv)
961 driver_unregister(&cdrv->drv);
963 EXPORT_SYMBOL_GPL(css_driver_unregister);
965 subsys_initcall(init_channel_subsystem);
967 MODULE_LICENSE("GPL");
968 EXPORT_SYMBOL(css_bus_type);
969 EXPORT_SYMBOL_GPL(css_characteristics_avail);