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>
18 #include "../s390mach.h"
21 #include "cio_debug.h"
28 int css_init_done = 0;
29 static int need_reprobe = 0;
30 static int max_ssid = 0;
32 struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
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);
161 * css_sch_device_unregister - unregister a subchannel
162 * @sch: subchannel to be unregistered
164 void css_sch_device_unregister(struct subchannel *sch)
166 mutex_lock(&sch->reg_mutex);
167 if (device_is_registered(&sch->dev))
168 device_unregister(&sch->dev);
169 mutex_unlock(&sch->reg_mutex);
171 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
173 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
178 memset(ssd, 0, sizeof(struct chsc_ssd_info));
179 ssd->path_mask = pmcw->pim;
180 for (i = 0; i < 8; i++) {
182 if (pmcw->pim & mask) {
183 chp_id_init(&ssd->chpid[i]);
184 ssd->chpid[i].id = pmcw->chpid[i];
189 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
194 for (i = 0; i < 8; i++) {
196 if (ssd->path_mask & mask)
197 if (!chp_is_registered(ssd->chpid[i]))
198 chp_new(ssd->chpid[i]);
202 void css_update_ssd_info(struct subchannel *sch)
206 if (cio_is_console(sch->schid)) {
207 /* Console is initialized too early for functions requiring
208 * memory allocation. */
209 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
211 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
213 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
214 ssd_register_chpids(&sch->ssd_info);
218 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
221 struct subchannel *sch = to_subchannel(dev);
223 return sprintf(buf, "%01x\n", sch->st);
226 static DEVICE_ATTR(type, 0444, type_show, NULL);
228 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
231 struct subchannel *sch = to_subchannel(dev);
233 return sprintf(buf, "css:t%01X\n", sch->st);
236 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
238 static struct attribute *subch_attrs[] = {
240 &dev_attr_modalias.attr,
244 static struct attribute_group subch_attr_group = {
245 .attrs = subch_attrs,
248 static struct attribute_group *default_subch_attr_groups[] = {
253 static int css_register_subchannel(struct subchannel *sch)
257 /* Initialize the subchannel structure */
258 sch->dev.parent = &channel_subsystems[0]->device;
259 sch->dev.bus = &css_bus_type;
260 sch->dev.release = &css_subchannel_release;
261 sch->dev.groups = default_subch_attr_groups;
263 * We don't want to generate uevents for I/O subchannels that don't
264 * have a working ccw device behind them since they will be
265 * unregistered before they can be used anyway, so we delay the add
266 * uevent until after device recognition was successful.
267 * Note that we suppress the uevent for all subchannel types;
268 * the subchannel driver can decide itself when it wants to inform
269 * userspace of its existence.
271 sch->dev.uevent_suppress = 1;
272 css_update_ssd_info(sch);
273 /* make it known to the system */
274 ret = css_sch_device_register(sch);
276 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
277 sch->schid.ssid, sch->schid.sch_no, ret);
282 * No driver matched. Generate the uevent now so that
283 * a fitting driver module may be loaded based on the
286 sch->dev.uevent_suppress = 0;
287 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
292 int css_probe_device(struct subchannel_id schid)
295 struct subchannel *sch;
297 sch = css_alloc_subchannel(schid);
300 ret = css_register_subchannel(sch);
302 css_free_subchannel(sch);
307 check_subchannel(struct device * dev, void * data)
309 struct subchannel *sch;
310 struct subchannel_id *schid = data;
312 sch = to_subchannel(dev);
313 return schid_equal(&sch->schid, schid);
317 get_subchannel_by_schid(struct subchannel_id schid)
321 dev = bus_find_device(&css_bus_type, NULL,
322 &schid, check_subchannel);
324 return dev ? to_subchannel(dev) : NULL;
328 * css_sch_is_valid() - check if a subchannel is valid
329 * @schib: subchannel information block for the subchannel
331 int css_sch_is_valid(struct schib *schib)
333 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
335 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
339 EXPORT_SYMBOL_GPL(css_sch_is_valid);
341 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
346 /* Will be done on the slow path. */
349 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
350 /* Unusable - ignore. */
353 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
354 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
356 return css_probe_device(schid);
359 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
364 if (sch->driver->sch_event)
365 ret = sch->driver->sch_event(sch, slow);
368 "Got subchannel machine check but "
369 "no sch_event handler provided.\n");
374 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
376 struct subchannel *sch;
379 sch = get_subchannel_by_schid(schid);
381 ret = css_evaluate_known_subchannel(sch, slow);
382 put_device(&sch->dev);
384 ret = css_evaluate_new_subchannel(schid, slow);
386 css_schedule_eval(schid);
389 static struct idset *slow_subchannel_set;
390 static spinlock_t slow_subchannel_lock;
392 static int __init slow_subchannel_init(void)
394 spin_lock_init(&slow_subchannel_lock);
395 slow_subchannel_set = idset_sch_new();
396 if (!slow_subchannel_set) {
397 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
403 static int slow_eval_known_fn(struct subchannel *sch, void *data)
408 spin_lock_irq(&slow_subchannel_lock);
409 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
410 idset_sch_del(slow_subchannel_set, sch->schid);
411 spin_unlock_irq(&slow_subchannel_lock);
413 rc = css_evaluate_known_subchannel(sch, 1);
415 css_schedule_eval(sch->schid);
420 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
425 spin_lock_irq(&slow_subchannel_lock);
426 eval = idset_sch_contains(slow_subchannel_set, schid);
427 idset_sch_del(slow_subchannel_set, schid);
428 spin_unlock_irq(&slow_subchannel_lock);
430 rc = css_evaluate_new_subchannel(schid, 1);
433 css_schedule_eval(schid);
439 /* These should abort looping */
448 static void css_slow_path_func(struct work_struct *unused)
450 CIO_TRACE_EVENT(4, "slowpath");
451 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
455 static DECLARE_WORK(slow_path_work, css_slow_path_func);
456 struct workqueue_struct *slow_path_wq;
458 void css_schedule_eval(struct subchannel_id schid)
462 spin_lock_irqsave(&slow_subchannel_lock, flags);
463 idset_sch_add(slow_subchannel_set, schid);
464 queue_work(slow_path_wq, &slow_path_work);
465 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
468 void css_schedule_eval_all(void)
472 spin_lock_irqsave(&slow_subchannel_lock, flags);
473 idset_fill(slow_subchannel_set);
474 queue_work(slow_path_wq, &slow_path_work);
475 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
478 void css_wait_for_slow_path(void)
480 flush_workqueue(slow_path_wq);
483 /* Reprobe subchannel if unregistered. */
484 static int reprobe_subchannel(struct subchannel_id schid, void *data)
488 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
489 schid.ssid, schid.sch_no);
493 ret = css_probe_device(schid);
500 /* These should abort looping */
509 /* Work function used to reprobe all unregistered subchannels. */
510 static void reprobe_all(struct work_struct *unused)
514 CIO_MSG_EVENT(4, "reprobe start\n");
517 /* Make sure initial subchannel scan is done. */
518 wait_event(ccw_device_init_wq,
519 atomic_read(&ccw_device_init_count) == 0);
520 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
522 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
526 static DECLARE_WORK(css_reprobe_work, reprobe_all);
528 /* Schedule reprobing of all unregistered subchannels. */
529 void css_schedule_reprobe(void)
532 queue_work(slow_path_wq, &css_reprobe_work);
535 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
538 * Called from the machine check handler for subchannel report words.
540 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
542 struct subchannel_id mchk_schid;
545 css_schedule_eval_all();
548 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
549 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
550 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
551 crw0->erc, crw0->rsid);
553 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
554 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
555 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
556 crw1->anc, crw1->erc, crw1->rsid);
557 init_subchannel_id(&mchk_schid);
558 mchk_schid.sch_no = crw0->rsid;
560 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
563 * Since we are always presented with IPI in the CRW, we have to
564 * use stsch() to find out if the subchannel in question has come
567 css_evaluate_subchannel(mchk_schid, 0);
571 __init_channel_subsystem(struct subchannel_id schid, void *data)
573 struct subchannel *sch;
576 if (cio_is_console(schid))
577 sch = cio_get_console_subchannel();
579 sch = css_alloc_subchannel(schid);
588 panic("Out of memory in init_channel_subsystem\n");
589 /* -ENXIO: no more subchannels. */
592 /* -EIO: this subchannel set not supported. */
600 * We register ALL valid subchannels in ioinfo, even those
601 * that have been present before init_channel_subsystem.
602 * These subchannels can't have been registered yet (kmalloc
603 * not working) so we do it now. This is true e.g. for the
604 * console subchannel.
606 css_register_subchannel(sch);
611 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
613 if (css_general_characteristics.mcss) {
614 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
615 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
618 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
620 css->global_pgid.pgid_high.cpu_addr = 0;
623 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
624 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
625 css->global_pgid.tod_high = tod_high;
630 channel_subsystem_release(struct device *dev)
632 struct channel_subsystem *css;
635 mutex_destroy(&css->mutex);
636 if (css->pseudo_subchannel) {
637 /* Implies that it has been generated but never registered. */
638 css_subchannel_release(&css->pseudo_subchannel->dev);
639 css->pseudo_subchannel = NULL;
645 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
648 struct channel_subsystem *css = to_css(dev);
653 mutex_lock(&css->mutex);
654 ret = sprintf(buf, "%x\n", css->cm_enabled);
655 mutex_unlock(&css->mutex);
660 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
661 const char *buf, size_t count)
663 struct channel_subsystem *css = to_css(dev);
667 ret = strict_strtoul(buf, 16, &val);
670 mutex_lock(&css->mutex);
673 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
676 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
681 mutex_unlock(&css->mutex);
682 return ret < 0 ? ret : count;
685 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
687 static int __init setup_css(int nr)
691 struct channel_subsystem *css;
693 css = channel_subsystems[nr];
694 memset(css, 0, sizeof(struct channel_subsystem));
695 css->pseudo_subchannel =
696 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
697 if (!css->pseudo_subchannel)
699 css->pseudo_subchannel->dev.parent = &css->device;
700 css->pseudo_subchannel->dev.release = css_subchannel_release;
701 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
702 ret = cio_create_sch_lock(css->pseudo_subchannel);
704 kfree(css->pseudo_subchannel);
707 mutex_init(&css->mutex);
710 dev_set_name(&css->device, "css%x", nr);
711 css->device.release = channel_subsystem_release;
712 tod_high = (u32) (get_clock() >> 32);
713 css_generate_pgid(css, tod_high);
717 static int css_reboot_event(struct notifier_block *this,
724 for (i = 0; i <= __MAX_CSSID; i++) {
725 struct channel_subsystem *css;
727 css = channel_subsystems[i];
728 mutex_lock(&css->mutex);
730 if (chsc_secm(css, 0))
732 mutex_unlock(&css->mutex);
738 static struct notifier_block css_reboot_notifier = {
739 .notifier_call = css_reboot_event,
743 * Now that the driver core is running, we can setup our channel subsystem.
744 * The struct subchannel's are created during probing (except for the
745 * static console subchannel).
748 init_channel_subsystem (void)
752 ret = chsc_determine_css_characteristics();
754 goto out; /* No need to continue. */
756 ret = chsc_alloc_sei_area();
760 ret = slow_subchannel_init();
764 ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
768 if ((ret = bus_register(&css_bus_type)))
771 /* Try to enable MSS. */
772 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
774 case 0: /* Success. */
775 max_ssid = __MAX_SSID;
782 /* Setup css structure. */
783 for (i = 0; i <= __MAX_CSSID; i++) {
784 struct channel_subsystem *css;
786 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
791 channel_subsystems[i] = css;
794 kfree(channel_subsystems[i]);
797 ret = device_register(&css->device);
799 put_device(&css->device);
802 if (css_chsc_characteristics.secm) {
803 ret = device_create_file(&css->device,
804 &dev_attr_cm_enable);
808 ret = device_register(&css->pseudo_subchannel->dev);
812 ret = register_reboot_notifier(&css_reboot_notifier);
817 /* Enable default isc for I/O subchannels. */
818 isc_register(IO_SCH_ISC);
820 for_each_subchannel(__init_channel_subsystem, NULL);
823 if (css_chsc_characteristics.secm)
824 device_remove_file(&channel_subsystems[i]->device,
825 &dev_attr_cm_enable);
827 device_unregister(&channel_subsystems[i]->device);
830 struct channel_subsystem *css;
833 css = channel_subsystems[i];
834 device_unregister(&css->pseudo_subchannel->dev);
835 css->pseudo_subchannel = NULL;
836 if (css_chsc_characteristics.secm)
837 device_remove_file(&css->device,
838 &dev_attr_cm_enable);
839 device_unregister(&css->device);
842 bus_unregister(&css_bus_type);
844 s390_unregister_crw_handler(CRW_RSC_CSS);
845 chsc_free_sei_area();
846 kfree(slow_subchannel_set);
847 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
852 int sch_is_pseudo_sch(struct subchannel *sch)
854 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
857 static int css_bus_match(struct device *dev, struct device_driver *drv)
859 struct subchannel *sch = to_subchannel(dev);
860 struct css_driver *driver = to_cssdriver(drv);
861 struct css_device_id *id;
863 for (id = driver->subchannel_type; id->match_flags; id++) {
864 if (sch->st == id->type)
871 static int css_probe(struct device *dev)
873 struct subchannel *sch;
876 sch = to_subchannel(dev);
877 sch->driver = to_cssdriver(dev->driver);
878 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
884 static int css_remove(struct device *dev)
886 struct subchannel *sch;
889 sch = to_subchannel(dev);
890 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
895 static void css_shutdown(struct device *dev)
897 struct subchannel *sch;
899 sch = to_subchannel(dev);
900 if (sch->driver && sch->driver->shutdown)
901 sch->driver->shutdown(sch);
904 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
906 struct subchannel *sch = to_subchannel(dev);
909 ret = add_uevent_var(env, "ST=%01X", sch->st);
912 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
916 struct bus_type css_bus_type = {
918 .match = css_bus_match,
920 .remove = css_remove,
921 .shutdown = css_shutdown,
922 .uevent = css_uevent,
926 * css_driver_register - register a css driver
927 * @cdrv: css driver to register
929 * This is mainly a wrapper around driver_register that sets name
930 * and bus_type in the embedded struct device_driver correctly.
932 int css_driver_register(struct css_driver *cdrv)
934 cdrv->drv.name = cdrv->name;
935 cdrv->drv.bus = &css_bus_type;
936 cdrv->drv.owner = cdrv->owner;
937 return driver_register(&cdrv->drv);
939 EXPORT_SYMBOL_GPL(css_driver_register);
942 * css_driver_unregister - unregister a css driver
943 * @cdrv: css driver to unregister
945 * This is a wrapper around driver_unregister.
947 void css_driver_unregister(struct css_driver *cdrv)
949 driver_unregister(&cdrv->drv);
951 EXPORT_SYMBOL_GPL(css_driver_unregister);
953 subsys_initcall(init_channel_subsystem);
955 MODULE_LICENSE("GPL");
956 EXPORT_SYMBOL(css_bus_type);