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)
10 #define KMSG_COMPONENT "cio"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/list.h>
19 #include <linux/reboot.h>
25 #include "cio_debug.h"
32 int css_init_done = 0;
33 static int need_reprobe = 0;
34 static int max_ssid = 0;
36 struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
39 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
41 struct subchannel_id schid;
44 init_subchannel_id(&schid);
48 ret = fn(schid, data);
51 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
53 } while (schid.ssid++ < max_ssid);
60 int (*fn_known_sch)(struct subchannel *, void *);
61 int (*fn_unknown_sch)(struct subchannel_id, void *);
64 static int call_fn_known_sch(struct device *dev, void *data)
66 struct subchannel *sch = to_subchannel(dev);
67 struct cb_data *cb = data;
70 idset_sch_del(cb->set, sch->schid);
72 rc = cb->fn_known_sch(sch, cb->data);
76 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
78 struct cb_data *cb = data;
81 if (idset_sch_contains(cb->set, schid))
82 rc = cb->fn_unknown_sch(schid, cb->data);
86 static int call_fn_all_sch(struct subchannel_id schid, void *data)
88 struct cb_data *cb = data;
89 struct subchannel *sch;
92 sch = get_subchannel_by_schid(schid);
95 rc = cb->fn_known_sch(sch, cb->data);
96 put_device(&sch->dev);
98 if (cb->fn_unknown_sch)
99 rc = cb->fn_unknown_sch(schid, cb->data);
105 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
106 int (*fn_unknown)(struct subchannel_id,
113 cb.fn_known_sch = fn_known;
114 cb.fn_unknown_sch = fn_unknown;
116 cb.set = idset_sch_new();
118 /* fall back to brute force scanning in case of oom */
119 return for_each_subchannel(call_fn_all_sch, &cb);
123 /* Process registered subchannels. */
124 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
127 /* Process unregistered subchannels. */
129 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
136 static struct subchannel *
137 css_alloc_subchannel(struct subchannel_id schid)
139 struct subchannel *sch;
142 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
144 return ERR_PTR(-ENOMEM);
145 ret = cio_validate_subchannel (sch, schid);
154 css_free_subchannel(struct subchannel *sch)
157 /* Reset intparm to zeroes. */
158 sch->config.intparm = 0;
159 cio_commit_config(sch);
166 css_subchannel_release(struct device *dev)
168 struct subchannel *sch;
170 sch = to_subchannel(dev);
171 if (!cio_is_console(sch->schid)) {
177 static int css_sch_device_register(struct subchannel *sch)
181 mutex_lock(&sch->reg_mutex);
182 ret = device_register(&sch->dev);
183 mutex_unlock(&sch->reg_mutex);
188 * css_sch_device_unregister - unregister a subchannel
189 * @sch: subchannel to be unregistered
191 void css_sch_device_unregister(struct subchannel *sch)
193 mutex_lock(&sch->reg_mutex);
194 if (device_is_registered(&sch->dev))
195 device_unregister(&sch->dev);
196 mutex_unlock(&sch->reg_mutex);
198 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
200 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
205 memset(ssd, 0, sizeof(struct chsc_ssd_info));
206 ssd->path_mask = pmcw->pim;
207 for (i = 0; i < 8; i++) {
209 if (pmcw->pim & mask) {
210 chp_id_init(&ssd->chpid[i]);
211 ssd->chpid[i].id = pmcw->chpid[i];
216 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
221 for (i = 0; i < 8; i++) {
223 if (ssd->path_mask & mask)
224 if (!chp_is_registered(ssd->chpid[i]))
225 chp_new(ssd->chpid[i]);
229 void css_update_ssd_info(struct subchannel *sch)
233 if (cio_is_console(sch->schid)) {
234 /* Console is initialized too early for functions requiring
235 * memory allocation. */
236 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
238 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
240 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
241 ssd_register_chpids(&sch->ssd_info);
245 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
248 struct subchannel *sch = to_subchannel(dev);
250 return sprintf(buf, "%01x\n", sch->st);
253 static DEVICE_ATTR(type, 0444, type_show, NULL);
255 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
258 struct subchannel *sch = to_subchannel(dev);
260 return sprintf(buf, "css:t%01X\n", sch->st);
263 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
265 static struct attribute *subch_attrs[] = {
267 &dev_attr_modalias.attr,
271 static struct attribute_group subch_attr_group = {
272 .attrs = subch_attrs,
275 static struct attribute_group *default_subch_attr_groups[] = {
280 static int css_register_subchannel(struct subchannel *sch)
284 /* Initialize the subchannel structure */
285 sch->dev.parent = &channel_subsystems[0]->device;
286 sch->dev.bus = &css_bus_type;
287 sch->dev.release = &css_subchannel_release;
288 sch->dev.groups = default_subch_attr_groups;
290 * We don't want to generate uevents for I/O subchannels that don't
291 * have a working ccw device behind them since they will be
292 * unregistered before they can be used anyway, so we delay the add
293 * uevent until after device recognition was successful.
294 * Note that we suppress the uevent for all subchannel types;
295 * the subchannel driver can decide itself when it wants to inform
296 * userspace of its existence.
298 sch->dev.uevent_suppress = 1;
299 css_update_ssd_info(sch);
300 /* make it known to the system */
301 ret = css_sch_device_register(sch);
303 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
304 sch->schid.ssid, sch->schid.sch_no, ret);
309 * No driver matched. Generate the uevent now so that
310 * a fitting driver module may be loaded based on the
313 sch->dev.uevent_suppress = 0;
314 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
319 int css_probe_device(struct subchannel_id schid)
322 struct subchannel *sch;
324 sch = css_alloc_subchannel(schid);
327 ret = css_register_subchannel(sch);
329 css_free_subchannel(sch);
334 check_subchannel(struct device * dev, void * data)
336 struct subchannel *sch;
337 struct subchannel_id *schid = data;
339 sch = to_subchannel(dev);
340 return schid_equal(&sch->schid, schid);
344 get_subchannel_by_schid(struct subchannel_id schid)
348 dev = bus_find_device(&css_bus_type, NULL,
349 &schid, check_subchannel);
351 return dev ? to_subchannel(dev) : NULL;
355 * css_sch_is_valid() - check if a subchannel is valid
356 * @schib: subchannel information block for the subchannel
358 int css_sch_is_valid(struct schib *schib)
360 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
362 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
366 EXPORT_SYMBOL_GPL(css_sch_is_valid);
368 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
373 /* Will be done on the slow path. */
376 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
377 /* Unusable - ignore. */
380 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
381 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
383 return css_probe_device(schid);
386 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
391 if (sch->driver->sch_event)
392 ret = sch->driver->sch_event(sch, slow);
395 "Got subchannel machine check but "
396 "no sch_event handler provided.\n");
401 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
403 struct subchannel *sch;
406 sch = get_subchannel_by_schid(schid);
408 ret = css_evaluate_known_subchannel(sch, slow);
409 put_device(&sch->dev);
411 ret = css_evaluate_new_subchannel(schid, slow);
413 css_schedule_eval(schid);
416 static struct idset *slow_subchannel_set;
417 static spinlock_t slow_subchannel_lock;
419 static int __init slow_subchannel_init(void)
421 spin_lock_init(&slow_subchannel_lock);
422 slow_subchannel_set = idset_sch_new();
423 if (!slow_subchannel_set) {
424 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
430 static int slow_eval_known_fn(struct subchannel *sch, void *data)
435 spin_lock_irq(&slow_subchannel_lock);
436 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
437 idset_sch_del(slow_subchannel_set, sch->schid);
438 spin_unlock_irq(&slow_subchannel_lock);
440 rc = css_evaluate_known_subchannel(sch, 1);
442 css_schedule_eval(sch->schid);
447 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
452 spin_lock_irq(&slow_subchannel_lock);
453 eval = idset_sch_contains(slow_subchannel_set, schid);
454 idset_sch_del(slow_subchannel_set, schid);
455 spin_unlock_irq(&slow_subchannel_lock);
457 rc = css_evaluate_new_subchannel(schid, 1);
460 css_schedule_eval(schid);
466 /* These should abort looping */
475 static void css_slow_path_func(struct work_struct *unused)
477 CIO_TRACE_EVENT(4, "slowpath");
478 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
482 static DECLARE_WORK(slow_path_work, css_slow_path_func);
483 struct workqueue_struct *slow_path_wq;
485 void css_schedule_eval(struct subchannel_id schid)
489 spin_lock_irqsave(&slow_subchannel_lock, flags);
490 idset_sch_add(slow_subchannel_set, schid);
491 queue_work(slow_path_wq, &slow_path_work);
492 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
495 void css_schedule_eval_all(void)
499 spin_lock_irqsave(&slow_subchannel_lock, flags);
500 idset_fill(slow_subchannel_set);
501 queue_work(slow_path_wq, &slow_path_work);
502 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
505 void css_wait_for_slow_path(void)
507 flush_workqueue(slow_path_wq);
510 /* Reprobe subchannel if unregistered. */
511 static int reprobe_subchannel(struct subchannel_id schid, void *data)
515 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
516 schid.ssid, schid.sch_no);
520 ret = css_probe_device(schid);
527 /* These should abort looping */
536 /* Work function used to reprobe all unregistered subchannels. */
537 static void reprobe_all(struct work_struct *unused)
541 CIO_MSG_EVENT(4, "reprobe start\n");
544 /* Make sure initial subchannel scan is done. */
545 wait_event(ccw_device_init_wq,
546 atomic_read(&ccw_device_init_count) == 0);
547 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
549 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
553 static DECLARE_WORK(css_reprobe_work, reprobe_all);
555 /* Schedule reprobing of all unregistered subchannels. */
556 void css_schedule_reprobe(void)
559 queue_work(slow_path_wq, &css_reprobe_work);
562 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
565 * Called from the machine check handler for subchannel report words.
567 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
569 struct subchannel_id mchk_schid;
572 css_schedule_eval_all();
575 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
576 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
577 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
578 crw0->erc, crw0->rsid);
580 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
581 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
582 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
583 crw1->anc, crw1->erc, crw1->rsid);
584 init_subchannel_id(&mchk_schid);
585 mchk_schid.sch_no = crw0->rsid;
587 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
590 * Since we are always presented with IPI in the CRW, we have to
591 * use stsch() to find out if the subchannel in question has come
594 css_evaluate_subchannel(mchk_schid, 0);
598 __init_channel_subsystem(struct subchannel_id schid, void *data)
600 struct subchannel *sch;
603 if (cio_is_console(schid))
604 sch = cio_get_console_subchannel();
606 sch = css_alloc_subchannel(schid);
615 panic("Out of memory in init_channel_subsystem\n");
616 /* -ENXIO: no more subchannels. */
619 /* -EIO: this subchannel set not supported. */
627 * We register ALL valid subchannels in ioinfo, even those
628 * that have been present before init_channel_subsystem.
629 * These subchannels can't have been registered yet (kmalloc
630 * not working) so we do it now. This is true e.g. for the
631 * console subchannel.
633 css_register_subchannel(sch);
638 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
640 if (css_general_characteristics.mcss) {
641 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
642 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
645 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
647 css->global_pgid.pgid_high.cpu_addr = 0;
650 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
651 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
652 css->global_pgid.tod_high = tod_high;
657 channel_subsystem_release(struct device *dev)
659 struct channel_subsystem *css;
662 mutex_destroy(&css->mutex);
663 if (css->pseudo_subchannel) {
664 /* Implies that it has been generated but never registered. */
665 css_subchannel_release(&css->pseudo_subchannel->dev);
666 css->pseudo_subchannel = NULL;
672 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
675 struct channel_subsystem *css = to_css(dev);
680 mutex_lock(&css->mutex);
681 ret = sprintf(buf, "%x\n", css->cm_enabled);
682 mutex_unlock(&css->mutex);
687 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
690 struct channel_subsystem *css = to_css(dev);
694 ret = strict_strtoul(buf, 16, &val);
697 mutex_lock(&css->mutex);
700 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
703 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
708 mutex_unlock(&css->mutex);
709 return ret < 0 ? ret : count;
712 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
714 static int __init setup_css(int nr)
718 struct channel_subsystem *css;
720 css = channel_subsystems[nr];
721 memset(css, 0, sizeof(struct channel_subsystem));
722 css->pseudo_subchannel =
723 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
724 if (!css->pseudo_subchannel)
726 css->pseudo_subchannel->dev.parent = &css->device;
727 css->pseudo_subchannel->dev.release = css_subchannel_release;
728 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
729 ret = cio_create_sch_lock(css->pseudo_subchannel);
731 kfree(css->pseudo_subchannel);
734 mutex_init(&css->mutex);
737 dev_set_name(&css->device, "css%x", nr);
738 css->device.release = channel_subsystem_release;
739 tod_high = (u32) (get_clock() >> 32);
740 css_generate_pgid(css, tod_high);
744 static int css_reboot_event(struct notifier_block *this,
751 for (i = 0; i <= __MAX_CSSID; i++) {
752 struct channel_subsystem *css;
754 css = channel_subsystems[i];
755 mutex_lock(&css->mutex);
757 if (chsc_secm(css, 0))
759 mutex_unlock(&css->mutex);
765 static struct notifier_block css_reboot_notifier = {
766 .notifier_call = css_reboot_event,
770 * Now that the driver core is running, we can setup our channel subsystem.
771 * The struct subchannel's are created during probing (except for the
772 * static console subchannel).
775 init_channel_subsystem (void)
779 ret = chsc_determine_css_characteristics();
781 goto out; /* No need to continue. */
783 ret = chsc_alloc_sei_area();
787 ret = slow_subchannel_init();
791 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
795 if ((ret = bus_register(&css_bus_type)))
798 /* Try to enable MSS. */
799 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
801 case 0: /* Success. */
802 max_ssid = __MAX_SSID;
809 /* Setup css structure. */
810 for (i = 0; i <= __MAX_CSSID; i++) {
811 struct channel_subsystem *css;
813 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
818 channel_subsystems[i] = css;
821 kfree(channel_subsystems[i]);
824 ret = device_register(&css->device);
826 put_device(&css->device);
829 if (css_chsc_characteristics.secm) {
830 ret = device_create_file(&css->device,
831 &dev_attr_cm_enable);
835 ret = device_register(&css->pseudo_subchannel->dev);
839 ret = register_reboot_notifier(&css_reboot_notifier);
844 /* Enable default isc for I/O subchannels. */
845 isc_register(IO_SCH_ISC);
847 for_each_subchannel(__init_channel_subsystem, NULL);
850 if (css_chsc_characteristics.secm)
851 device_remove_file(&channel_subsystems[i]->device,
852 &dev_attr_cm_enable);
854 device_unregister(&channel_subsystems[i]->device);
857 struct channel_subsystem *css;
860 css = channel_subsystems[i];
861 device_unregister(&css->pseudo_subchannel->dev);
862 css->pseudo_subchannel = NULL;
863 if (css_chsc_characteristics.secm)
864 device_remove_file(&css->device,
865 &dev_attr_cm_enable);
866 device_unregister(&css->device);
869 bus_unregister(&css_bus_type);
871 crw_unregister_handler(CRW_RSC_CSS);
872 chsc_free_sei_area();
873 kfree(slow_subchannel_set);
874 pr_alert("The CSS device driver initialization failed with "
879 int sch_is_pseudo_sch(struct subchannel *sch)
881 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
884 static int css_bus_match(struct device *dev, struct device_driver *drv)
886 struct subchannel *sch = to_subchannel(dev);
887 struct css_driver *driver = to_cssdriver(drv);
888 struct css_device_id *id;
890 for (id = driver->subchannel_type; id->match_flags; id++) {
891 if (sch->st == id->type)
898 static int css_probe(struct device *dev)
900 struct subchannel *sch;
903 sch = to_subchannel(dev);
904 sch->driver = to_cssdriver(dev->driver);
905 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
911 static int css_remove(struct device *dev)
913 struct subchannel *sch;
916 sch = to_subchannel(dev);
917 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
922 static void css_shutdown(struct device *dev)
924 struct subchannel *sch;
926 sch = to_subchannel(dev);
927 if (sch->driver && sch->driver->shutdown)
928 sch->driver->shutdown(sch);
931 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
933 struct subchannel *sch = to_subchannel(dev);
936 ret = add_uevent_var(env, "ST=%01X", sch->st);
939 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
943 struct bus_type css_bus_type = {
945 .match = css_bus_match,
947 .remove = css_remove,
948 .shutdown = css_shutdown,
949 .uevent = css_uevent,
953 * css_driver_register - register a css driver
954 * @cdrv: css driver to register
956 * This is mainly a wrapper around driver_register that sets name
957 * and bus_type in the embedded struct device_driver correctly.
959 int css_driver_register(struct css_driver *cdrv)
961 cdrv->drv.name = cdrv->name;
962 cdrv->drv.bus = &css_bus_type;
963 cdrv->drv.owner = cdrv->owner;
964 return driver_register(&cdrv->drv);
966 EXPORT_SYMBOL_GPL(css_driver_register);
969 * css_driver_unregister - unregister a css driver
970 * @cdrv: css driver to unregister
972 * This is a wrapper around driver_unregister.
974 void css_driver_unregister(struct css_driver *cdrv)
976 driver_unregister(&cdrv->drv);
978 EXPORT_SYMBOL_GPL(css_driver_unregister);
980 subsys_initcall(init_channel_subsystem);
982 MODULE_LICENSE("GPL");
983 EXPORT_SYMBOL(css_bus_type);