2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/reboot.h>
19 #include "cio_debug.h"
26 int css_init_done = 0;
27 static int need_reprobe = 0;
28 static int max_ssid = 0;
30 struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
32 int css_characteristics_avail = 0;
35 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
37 struct subchannel_id schid;
40 init_subchannel_id(&schid);
44 ret = fn(schid, data);
47 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
49 } while (schid.ssid++ < max_ssid);
56 int (*fn_known_sch)(struct subchannel *, void *);
57 int (*fn_unknown_sch)(struct subchannel_id, void *);
60 static int call_fn_known_sch(struct device *dev, void *data)
62 struct subchannel *sch = to_subchannel(dev);
63 struct cb_data *cb = data;
66 idset_sch_del(cb->set, sch->schid);
68 rc = cb->fn_known_sch(sch, cb->data);
72 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
74 struct cb_data *cb = data;
77 if (idset_sch_contains(cb->set, schid))
78 rc = cb->fn_unknown_sch(schid, cb->data);
82 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
83 int (*fn_unknown)(struct subchannel_id,
89 cb.set = idset_sch_new();
94 cb.fn_known_sch = fn_known;
95 cb.fn_unknown_sch = fn_unknown;
96 /* Process registered subchannels. */
97 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
100 /* Process unregistered subchannels. */
102 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
109 static struct subchannel *
110 css_alloc_subchannel(struct subchannel_id schid)
112 struct subchannel *sch;
115 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
117 return ERR_PTR(-ENOMEM);
118 ret = cio_validate_subchannel (sch, schid);
127 css_free_subchannel(struct subchannel *sch)
130 /* Reset intparm to zeroes. */
131 sch->schib.pmcw.intparm = 0;
139 css_subchannel_release(struct device *dev)
141 struct subchannel *sch;
143 sch = to_subchannel(dev);
144 if (!cio_is_console(sch->schid)) {
150 static int css_sch_device_register(struct subchannel *sch)
154 mutex_lock(&sch->reg_mutex);
155 ret = device_register(&sch->dev);
156 mutex_unlock(&sch->reg_mutex);
160 void css_sch_device_unregister(struct subchannel *sch)
162 mutex_lock(&sch->reg_mutex);
163 device_unregister(&sch->dev);
164 mutex_unlock(&sch->reg_mutex);
167 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
172 memset(ssd, 0, sizeof(struct chsc_ssd_info));
173 ssd->path_mask = pmcw->pim;
174 for (i = 0; i < 8; i++) {
176 if (pmcw->pim & mask) {
177 chp_id_init(&ssd->chpid[i]);
178 ssd->chpid[i].id = pmcw->chpid[i];
183 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
188 for (i = 0; i < 8; i++) {
190 if (ssd->path_mask & mask)
191 if (!chp_is_registered(ssd->chpid[i]))
192 chp_new(ssd->chpid[i]);
196 void css_update_ssd_info(struct subchannel *sch)
200 if (cio_is_console(sch->schid)) {
201 /* Console is initialized too early for functions requiring
202 * memory allocation. */
203 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
205 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
207 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
208 ssd_register_chpids(&sch->ssd_info);
212 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
215 struct subchannel *sch = to_subchannel(dev);
217 return sprintf(buf, "%01x\n", sch->st);
220 static DEVICE_ATTR(type, 0444, type_show, NULL);
222 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
225 struct subchannel *sch = to_subchannel(dev);
227 return sprintf(buf, "css:t%01X\n", sch->st);
230 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
232 static struct attribute *subch_attrs[] = {
234 &dev_attr_modalias.attr,
238 static struct attribute_group subch_attr_group = {
239 .attrs = subch_attrs,
242 static struct attribute_group *default_subch_attr_groups[] = {
247 static int css_register_subchannel(struct subchannel *sch)
251 /* Initialize the subchannel structure */
252 sch->dev.parent = &channel_subsystems[0]->device;
253 sch->dev.bus = &css_bus_type;
254 sch->dev.release = &css_subchannel_release;
255 sch->dev.groups = default_subch_attr_groups;
257 * We don't want to generate uevents for I/O subchannels that don't
258 * have a working ccw device behind them since they will be
259 * unregistered before they can be used anyway, so we delay the add
260 * uevent until after device recognition was successful.
261 * Note that we suppress the uevent for all subchannel types;
262 * the subchannel driver can decide itself when it wants to inform
263 * userspace of its existence.
265 sch->dev.uevent_suppress = 1;
266 css_update_ssd_info(sch);
267 /* make it known to the system */
268 ret = css_sch_device_register(sch);
270 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
271 sch->schid.ssid, sch->schid.sch_no, ret);
276 * No driver matched. Generate the uevent now so that
277 * a fitting driver module may be loaded based on the
280 sch->dev.uevent_suppress = 0;
281 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
286 int css_probe_device(struct subchannel_id schid)
289 struct subchannel *sch;
291 sch = css_alloc_subchannel(schid);
294 ret = css_register_subchannel(sch);
296 css_free_subchannel(sch);
301 check_subchannel(struct device * dev, void * data)
303 struct subchannel *sch;
304 struct subchannel_id *schid = data;
306 sch = to_subchannel(dev);
307 return schid_equal(&sch->schid, schid);
311 get_subchannel_by_schid(struct subchannel_id schid)
315 dev = bus_find_device(&css_bus_type, NULL,
316 &schid, check_subchannel);
318 return dev ? to_subchannel(dev) : NULL;
322 * css_sch_is_valid() - check if a subchannel is valid
323 * @schib: subchannel information block for the subchannel
325 int css_sch_is_valid(struct schib *schib)
327 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
331 EXPORT_SYMBOL_GPL(css_sch_is_valid);
333 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
338 /* Will be done on the slow path. */
341 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
342 /* Unusable - ignore. */
345 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
346 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
348 return css_probe_device(schid);
351 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
356 if (sch->driver->sch_event)
357 ret = sch->driver->sch_event(sch, slow);
360 "Got subchannel machine check but "
361 "no sch_event handler provided.\n");
366 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
368 struct subchannel *sch;
371 sch = get_subchannel_by_schid(schid);
373 ret = css_evaluate_known_subchannel(sch, slow);
374 put_device(&sch->dev);
376 ret = css_evaluate_new_subchannel(schid, slow);
378 css_schedule_eval(schid);
381 static struct idset *slow_subchannel_set;
382 static spinlock_t slow_subchannel_lock;
384 static int __init slow_subchannel_init(void)
386 spin_lock_init(&slow_subchannel_lock);
387 slow_subchannel_set = idset_sch_new();
388 if (!slow_subchannel_set) {
389 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
395 static int slow_eval_known_fn(struct subchannel *sch, void *data)
400 spin_lock_irq(&slow_subchannel_lock);
401 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
402 idset_sch_del(slow_subchannel_set, sch->schid);
403 spin_unlock_irq(&slow_subchannel_lock);
405 rc = css_evaluate_known_subchannel(sch, 1);
407 css_schedule_eval(sch->schid);
412 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
417 spin_lock_irq(&slow_subchannel_lock);
418 eval = idset_sch_contains(slow_subchannel_set, schid);
419 idset_sch_del(slow_subchannel_set, schid);
420 spin_unlock_irq(&slow_subchannel_lock);
422 rc = css_evaluate_new_subchannel(schid, 1);
425 css_schedule_eval(schid);
431 /* These should abort looping */
440 static void css_slow_path_func(struct work_struct *unused)
442 CIO_TRACE_EVENT(4, "slowpath");
443 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
447 static DECLARE_WORK(slow_path_work, css_slow_path_func);
448 struct workqueue_struct *slow_path_wq;
450 void css_schedule_eval(struct subchannel_id schid)
454 spin_lock_irqsave(&slow_subchannel_lock, flags);
455 idset_sch_add(slow_subchannel_set, schid);
456 queue_work(slow_path_wq, &slow_path_work);
457 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
460 void css_schedule_eval_all(void)
464 spin_lock_irqsave(&slow_subchannel_lock, flags);
465 idset_fill(slow_subchannel_set);
466 queue_work(slow_path_wq, &slow_path_work);
467 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
470 void css_wait_for_slow_path(void)
472 flush_workqueue(ccw_device_notify_work);
473 flush_workqueue(slow_path_wq);
476 /* Reprobe subchannel if unregistered. */
477 static int reprobe_subchannel(struct subchannel_id schid, void *data)
481 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
482 schid.ssid, schid.sch_no);
486 ret = css_probe_device(schid);
493 /* These should abort looping */
502 /* Work function used to reprobe all unregistered subchannels. */
503 static void reprobe_all(struct work_struct *unused)
507 CIO_MSG_EVENT(4, "reprobe start\n");
510 /* Make sure initial subchannel scan is done. */
511 wait_event(ccw_device_init_wq,
512 atomic_read(&ccw_device_init_count) == 0);
513 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
515 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
519 static DECLARE_WORK(css_reprobe_work, reprobe_all);
521 /* Schedule reprobing of all unregistered subchannels. */
522 void css_schedule_reprobe(void)
525 queue_work(slow_path_wq, &css_reprobe_work);
528 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
531 * Called from the machine check handler for subchannel report words.
533 void css_process_crw(int rsid1, int rsid2)
535 struct subchannel_id mchk_schid;
537 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
539 init_subchannel_id(&mchk_schid);
540 mchk_schid.sch_no = rsid1;
542 mchk_schid.ssid = (rsid2 >> 8) & 3;
545 * Since we are always presented with IPI in the CRW, we have to
546 * use stsch() to find out if the subchannel in question has come
549 css_evaluate_subchannel(mchk_schid, 0);
553 __init_channel_subsystem(struct subchannel_id schid, void *data)
555 struct subchannel *sch;
558 if (cio_is_console(schid))
559 sch = cio_get_console_subchannel();
561 sch = css_alloc_subchannel(schid);
570 panic("Out of memory in init_channel_subsystem\n");
571 /* -ENXIO: no more subchannels. */
574 /* -EIO: this subchannel set not supported. */
582 * We register ALL valid subchannels in ioinfo, even those
583 * that have been present before init_channel_subsystem.
584 * These subchannels can't have been registered yet (kmalloc
585 * not working) so we do it now. This is true e.g. for the
586 * console subchannel.
588 css_register_subchannel(sch);
593 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
595 if (css_characteristics_avail && css_general_characteristics.mcss) {
596 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
597 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
600 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
602 css->global_pgid.pgid_high.cpu_addr = 0;
605 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
606 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
607 css->global_pgid.tod_high = tod_high;
612 channel_subsystem_release(struct device *dev)
614 struct channel_subsystem *css;
617 mutex_destroy(&css->mutex);
622 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
625 struct channel_subsystem *css = to_css(dev);
630 mutex_lock(&css->mutex);
631 ret = sprintf(buf, "%x\n", css->cm_enabled);
632 mutex_unlock(&css->mutex);
637 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
638 const char *buf, size_t count)
640 struct channel_subsystem *css = to_css(dev);
644 ret = strict_strtoul(buf, 16, &val);
647 mutex_lock(&css->mutex);
650 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
653 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
658 mutex_unlock(&css->mutex);
659 return ret < 0 ? ret : count;
662 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
664 static int __init setup_css(int nr)
668 struct channel_subsystem *css;
670 css = channel_subsystems[nr];
671 memset(css, 0, sizeof(struct channel_subsystem));
672 css->pseudo_subchannel =
673 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
674 if (!css->pseudo_subchannel)
676 css->pseudo_subchannel->dev.parent = &css->device;
677 css->pseudo_subchannel->dev.release = css_subchannel_release;
678 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
679 ret = cio_create_sch_lock(css->pseudo_subchannel);
681 kfree(css->pseudo_subchannel);
684 mutex_init(&css->mutex);
687 sprintf(css->device.bus_id, "css%x", nr);
688 css->device.release = channel_subsystem_release;
689 tod_high = (u32) (get_clock() >> 32);
690 css_generate_pgid(css, tod_high);
694 static int css_reboot_event(struct notifier_block *this,
701 for (i = 0; i <= __MAX_CSSID; i++) {
702 struct channel_subsystem *css;
704 css = channel_subsystems[i];
705 mutex_lock(&css->mutex);
707 if (chsc_secm(css, 0))
709 mutex_unlock(&css->mutex);
715 static struct notifier_block css_reboot_notifier = {
716 .notifier_call = css_reboot_event,
720 * Now that the driver core is running, we can setup our channel subsystem.
721 * The struct subchannel's are created during probing (except for the
722 * static console subchannel).
725 init_channel_subsystem (void)
729 ret = chsc_determine_css_characteristics();
731 goto out; /* No need to continue. */
733 css_characteristics_avail = 1;
735 ret = chsc_alloc_sei_area();
739 ret = slow_subchannel_init();
743 if ((ret = bus_register(&css_bus_type)))
746 /* Try to enable MSS. */
747 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
749 case 0: /* Success. */
750 max_ssid = __MAX_SSID;
757 /* Setup css structure. */
758 for (i = 0; i <= __MAX_CSSID; i++) {
759 struct channel_subsystem *css;
761 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
766 channel_subsystems[i] = css;
770 ret = device_register(&css->device);
773 if (css_characteristics_avail &&
774 css_chsc_characteristics.secm) {
775 ret = device_create_file(&css->device,
776 &dev_attr_cm_enable);
780 ret = device_register(&css->pseudo_subchannel->dev);
784 ret = register_reboot_notifier(&css_reboot_notifier);
791 for_each_subchannel(__init_channel_subsystem, NULL);
794 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
796 device_remove_file(&channel_subsystems[i]->device,
797 &dev_attr_cm_enable);
799 device_unregister(&channel_subsystems[i]->device);
801 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
802 kfree(channel_subsystems[i]->pseudo_subchannel);
804 kfree(channel_subsystems[i]);
807 struct channel_subsystem *css;
810 css = channel_subsystems[i];
811 device_unregister(&css->pseudo_subchannel->dev);
812 if (css_characteristics_avail && css_chsc_characteristics.secm)
813 device_remove_file(&css->device,
814 &dev_attr_cm_enable);
815 device_unregister(&css->device);
818 bus_unregister(&css_bus_type);
820 chsc_free_sei_area();
821 kfree(slow_subchannel_set);
822 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
827 int sch_is_pseudo_sch(struct subchannel *sch)
829 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
833 * find a driver for a subchannel. They identify by the subchannel
834 * type with the exception that the console subchannel driver has its own
835 * subchannel type although the device is an i/o subchannel
838 css_bus_match (struct device *dev, struct device_driver *drv)
840 struct subchannel *sch = to_subchannel(dev);
841 struct css_driver *driver = to_cssdriver(drv);
843 if (sch->st == driver->subchannel_type)
849 static int css_probe(struct device *dev)
851 struct subchannel *sch;
854 sch = to_subchannel(dev);
855 sch->driver = to_cssdriver(dev->driver);
856 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
862 static int css_remove(struct device *dev)
864 struct subchannel *sch;
867 sch = to_subchannel(dev);
868 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
873 static void css_shutdown(struct device *dev)
875 struct subchannel *sch;
877 sch = to_subchannel(dev);
878 if (sch->driver && sch->driver->shutdown)
879 sch->driver->shutdown(sch);
882 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
884 struct subchannel *sch = to_subchannel(dev);
887 ret = add_uevent_var(env, "ST=%01X", sch->st);
890 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
894 struct bus_type css_bus_type = {
896 .match = css_bus_match,
898 .remove = css_remove,
899 .shutdown = css_shutdown,
900 .uevent = css_uevent,
904 * css_driver_register - register a css driver
905 * @cdrv: css driver to register
907 * This is mainly a wrapper around driver_register that sets name
908 * and bus_type in the embedded struct device_driver correctly.
910 int css_driver_register(struct css_driver *cdrv)
912 cdrv->drv.name = cdrv->name;
913 cdrv->drv.bus = &css_bus_type;
914 cdrv->drv.owner = cdrv->owner;
915 return driver_register(&cdrv->drv);
917 EXPORT_SYMBOL_GPL(css_driver_register);
920 * css_driver_unregister - unregister a css driver
921 * @cdrv: css driver to unregister
923 * This is a wrapper around driver_unregister.
925 void css_driver_unregister(struct css_driver *cdrv)
927 driver_unregister(&cdrv->drv);
929 EXPORT_SYMBOL_GPL(css_driver_unregister);
931 subsys_initcall(init_channel_subsystem);
933 MODULE_LICENSE("GPL");
934 EXPORT_SYMBOL(css_bus_type);
935 EXPORT_SYMBOL_GPL(css_characteristics_avail);