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>
22 #include "../s390mach.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 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
87 int (*fn_unknown)(struct subchannel_id,
93 cb.set = idset_sch_new();
98 cb.fn_known_sch = fn_known;
99 cb.fn_unknown_sch = fn_unknown;
100 /* Process registered subchannels. */
101 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
104 /* Process unregistered subchannels. */
106 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
113 static struct subchannel *
114 css_alloc_subchannel(struct subchannel_id schid)
116 struct subchannel *sch;
119 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
121 return ERR_PTR(-ENOMEM);
122 ret = cio_validate_subchannel (sch, schid);
131 css_free_subchannel(struct subchannel *sch)
134 /* Reset intparm to zeroes. */
135 sch->config.intparm = 0;
136 cio_commit_config(sch);
143 css_subchannel_release(struct device *dev)
145 struct subchannel *sch;
147 sch = to_subchannel(dev);
148 if (!cio_is_console(sch->schid)) {
154 static int css_sch_device_register(struct subchannel *sch)
158 mutex_lock(&sch->reg_mutex);
159 ret = device_register(&sch->dev);
160 mutex_unlock(&sch->reg_mutex);
165 * css_sch_device_unregister - unregister a subchannel
166 * @sch: subchannel to be unregistered
168 void css_sch_device_unregister(struct subchannel *sch)
170 mutex_lock(&sch->reg_mutex);
171 if (device_is_registered(&sch->dev))
172 device_unregister(&sch->dev);
173 mutex_unlock(&sch->reg_mutex);
175 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
177 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
182 memset(ssd, 0, sizeof(struct chsc_ssd_info));
183 ssd->path_mask = pmcw->pim;
184 for (i = 0; i < 8; i++) {
186 if (pmcw->pim & mask) {
187 chp_id_init(&ssd->chpid[i]);
188 ssd->chpid[i].id = pmcw->chpid[i];
193 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
198 for (i = 0; i < 8; i++) {
200 if (ssd->path_mask & mask)
201 if (!chp_is_registered(ssd->chpid[i]))
202 chp_new(ssd->chpid[i]);
206 void css_update_ssd_info(struct subchannel *sch)
210 if (cio_is_console(sch->schid)) {
211 /* Console is initialized too early for functions requiring
212 * memory allocation. */
213 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
215 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
217 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
218 ssd_register_chpids(&sch->ssd_info);
222 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
225 struct subchannel *sch = to_subchannel(dev);
227 return sprintf(buf, "%01x\n", sch->st);
230 static DEVICE_ATTR(type, 0444, type_show, NULL);
232 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
235 struct subchannel *sch = to_subchannel(dev);
237 return sprintf(buf, "css:t%01X\n", sch->st);
240 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
242 static struct attribute *subch_attrs[] = {
244 &dev_attr_modalias.attr,
248 static struct attribute_group subch_attr_group = {
249 .attrs = subch_attrs,
252 static struct attribute_group *default_subch_attr_groups[] = {
257 static int css_register_subchannel(struct subchannel *sch)
261 /* Initialize the subchannel structure */
262 sch->dev.parent = &channel_subsystems[0]->device;
263 sch->dev.bus = &css_bus_type;
264 sch->dev.release = &css_subchannel_release;
265 sch->dev.groups = default_subch_attr_groups;
267 * We don't want to generate uevents for I/O subchannels that don't
268 * have a working ccw device behind them since they will be
269 * unregistered before they can be used anyway, so we delay the add
270 * uevent until after device recognition was successful.
271 * Note that we suppress the uevent for all subchannel types;
272 * the subchannel driver can decide itself when it wants to inform
273 * userspace of its existence.
275 sch->dev.uevent_suppress = 1;
276 css_update_ssd_info(sch);
277 /* make it known to the system */
278 ret = css_sch_device_register(sch);
280 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
281 sch->schid.ssid, sch->schid.sch_no, ret);
286 * No driver matched. Generate the uevent now so that
287 * a fitting driver module may be loaded based on the
290 sch->dev.uevent_suppress = 0;
291 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
296 int css_probe_device(struct subchannel_id schid)
299 struct subchannel *sch;
301 sch = css_alloc_subchannel(schid);
304 ret = css_register_subchannel(sch);
306 css_free_subchannel(sch);
311 check_subchannel(struct device * dev, void * data)
313 struct subchannel *sch;
314 struct subchannel_id *schid = data;
316 sch = to_subchannel(dev);
317 return schid_equal(&sch->schid, schid);
321 get_subchannel_by_schid(struct subchannel_id schid)
325 dev = bus_find_device(&css_bus_type, NULL,
326 &schid, check_subchannel);
328 return dev ? to_subchannel(dev) : NULL;
332 * css_sch_is_valid() - check if a subchannel is valid
333 * @schib: subchannel information block for the subchannel
335 int css_sch_is_valid(struct schib *schib)
337 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
339 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
343 EXPORT_SYMBOL_GPL(css_sch_is_valid);
345 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
350 /* Will be done on the slow path. */
353 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
354 /* Unusable - ignore. */
357 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
358 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
360 return css_probe_device(schid);
363 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
368 if (sch->driver->sch_event)
369 ret = sch->driver->sch_event(sch, slow);
372 "Got subchannel machine check but "
373 "no sch_event handler provided.\n");
378 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
380 struct subchannel *sch;
383 sch = get_subchannel_by_schid(schid);
385 ret = css_evaluate_known_subchannel(sch, slow);
386 put_device(&sch->dev);
388 ret = css_evaluate_new_subchannel(schid, slow);
390 css_schedule_eval(schid);
393 static struct idset *slow_subchannel_set;
394 static spinlock_t slow_subchannel_lock;
396 static int __init slow_subchannel_init(void)
398 spin_lock_init(&slow_subchannel_lock);
399 slow_subchannel_set = idset_sch_new();
400 if (!slow_subchannel_set) {
401 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
407 static int slow_eval_known_fn(struct subchannel *sch, void *data)
412 spin_lock_irq(&slow_subchannel_lock);
413 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
414 idset_sch_del(slow_subchannel_set, sch->schid);
415 spin_unlock_irq(&slow_subchannel_lock);
417 rc = css_evaluate_known_subchannel(sch, 1);
419 css_schedule_eval(sch->schid);
424 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
429 spin_lock_irq(&slow_subchannel_lock);
430 eval = idset_sch_contains(slow_subchannel_set, schid);
431 idset_sch_del(slow_subchannel_set, schid);
432 spin_unlock_irq(&slow_subchannel_lock);
434 rc = css_evaluate_new_subchannel(schid, 1);
437 css_schedule_eval(schid);
443 /* These should abort looping */
452 static void css_slow_path_func(struct work_struct *unused)
454 CIO_TRACE_EVENT(4, "slowpath");
455 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
459 static DECLARE_WORK(slow_path_work, css_slow_path_func);
460 struct workqueue_struct *slow_path_wq;
462 void css_schedule_eval(struct subchannel_id schid)
466 spin_lock_irqsave(&slow_subchannel_lock, flags);
467 idset_sch_add(slow_subchannel_set, schid);
468 queue_work(slow_path_wq, &slow_path_work);
469 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
472 void css_schedule_eval_all(void)
476 spin_lock_irqsave(&slow_subchannel_lock, flags);
477 idset_fill(slow_subchannel_set);
478 queue_work(slow_path_wq, &slow_path_work);
479 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
482 void css_wait_for_slow_path(void)
484 flush_workqueue(slow_path_wq);
487 /* Reprobe subchannel if unregistered. */
488 static int reprobe_subchannel(struct subchannel_id schid, void *data)
492 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
493 schid.ssid, schid.sch_no);
497 ret = css_probe_device(schid);
504 /* These should abort looping */
513 /* Work function used to reprobe all unregistered subchannels. */
514 static void reprobe_all(struct work_struct *unused)
518 CIO_MSG_EVENT(4, "reprobe start\n");
521 /* Make sure initial subchannel scan is done. */
522 wait_event(ccw_device_init_wq,
523 atomic_read(&ccw_device_init_count) == 0);
524 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
526 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
530 static DECLARE_WORK(css_reprobe_work, reprobe_all);
532 /* Schedule reprobing of all unregistered subchannels. */
533 void css_schedule_reprobe(void)
536 queue_work(slow_path_wq, &css_reprobe_work);
539 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
542 * Called from the machine check handler for subchannel report words.
544 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
546 struct subchannel_id mchk_schid;
549 css_schedule_eval_all();
552 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
553 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
554 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
555 crw0->erc, crw0->rsid);
557 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
558 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
559 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
560 crw1->anc, crw1->erc, crw1->rsid);
561 init_subchannel_id(&mchk_schid);
562 mchk_schid.sch_no = crw0->rsid;
564 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
567 * Since we are always presented with IPI in the CRW, we have to
568 * use stsch() to find out if the subchannel in question has come
571 css_evaluate_subchannel(mchk_schid, 0);
575 __init_channel_subsystem(struct subchannel_id schid, void *data)
577 struct subchannel *sch;
580 if (cio_is_console(schid))
581 sch = cio_get_console_subchannel();
583 sch = css_alloc_subchannel(schid);
592 panic("Out of memory in init_channel_subsystem\n");
593 /* -ENXIO: no more subchannels. */
596 /* -EIO: this subchannel set not supported. */
604 * We register ALL valid subchannels in ioinfo, even those
605 * that have been present before init_channel_subsystem.
606 * These subchannels can't have been registered yet (kmalloc
607 * not working) so we do it now. This is true e.g. for the
608 * console subchannel.
610 css_register_subchannel(sch);
615 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
617 if (css_general_characteristics.mcss) {
618 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
619 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
622 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
624 css->global_pgid.pgid_high.cpu_addr = 0;
627 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
628 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
629 css->global_pgid.tod_high = tod_high;
634 channel_subsystem_release(struct device *dev)
636 struct channel_subsystem *css;
639 mutex_destroy(&css->mutex);
640 if (css->pseudo_subchannel) {
641 /* Implies that it has been generated but never registered. */
642 css_subchannel_release(&css->pseudo_subchannel->dev);
643 css->pseudo_subchannel = NULL;
649 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
652 struct channel_subsystem *css = to_css(dev);
657 mutex_lock(&css->mutex);
658 ret = sprintf(buf, "%x\n", css->cm_enabled);
659 mutex_unlock(&css->mutex);
664 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
665 const char *buf, size_t count)
667 struct channel_subsystem *css = to_css(dev);
671 ret = strict_strtoul(buf, 16, &val);
674 mutex_lock(&css->mutex);
677 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
680 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
685 mutex_unlock(&css->mutex);
686 return ret < 0 ? ret : count;
689 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
691 static int __init setup_css(int nr)
695 struct channel_subsystem *css;
697 css = channel_subsystems[nr];
698 memset(css, 0, sizeof(struct channel_subsystem));
699 css->pseudo_subchannel =
700 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
701 if (!css->pseudo_subchannel)
703 css->pseudo_subchannel->dev.parent = &css->device;
704 css->pseudo_subchannel->dev.release = css_subchannel_release;
705 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
706 ret = cio_create_sch_lock(css->pseudo_subchannel);
708 kfree(css->pseudo_subchannel);
711 mutex_init(&css->mutex);
714 dev_set_name(&css->device, "css%x", nr);
715 css->device.release = channel_subsystem_release;
716 tod_high = (u32) (get_clock() >> 32);
717 css_generate_pgid(css, tod_high);
721 static int css_reboot_event(struct notifier_block *this,
728 for (i = 0; i <= __MAX_CSSID; i++) {
729 struct channel_subsystem *css;
731 css = channel_subsystems[i];
732 mutex_lock(&css->mutex);
734 if (chsc_secm(css, 0))
736 mutex_unlock(&css->mutex);
742 static struct notifier_block css_reboot_notifier = {
743 .notifier_call = css_reboot_event,
747 * Now that the driver core is running, we can setup our channel subsystem.
748 * The struct subchannel's are created during probing (except for the
749 * static console subchannel).
752 init_channel_subsystem (void)
756 ret = chsc_determine_css_characteristics();
758 goto out; /* No need to continue. */
760 ret = chsc_alloc_sei_area();
764 ret = slow_subchannel_init();
768 ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
772 if ((ret = bus_register(&css_bus_type)))
775 /* Try to enable MSS. */
776 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
778 case 0: /* Success. */
779 max_ssid = __MAX_SSID;
786 /* Setup css structure. */
787 for (i = 0; i <= __MAX_CSSID; i++) {
788 struct channel_subsystem *css;
790 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
795 channel_subsystems[i] = css;
798 kfree(channel_subsystems[i]);
801 ret = device_register(&css->device);
803 put_device(&css->device);
806 if (css_chsc_characteristics.secm) {
807 ret = device_create_file(&css->device,
808 &dev_attr_cm_enable);
812 ret = device_register(&css->pseudo_subchannel->dev);
816 ret = register_reboot_notifier(&css_reboot_notifier);
821 /* Enable default isc for I/O subchannels. */
822 isc_register(IO_SCH_ISC);
824 for_each_subchannel(__init_channel_subsystem, NULL);
827 if (css_chsc_characteristics.secm)
828 device_remove_file(&channel_subsystems[i]->device,
829 &dev_attr_cm_enable);
831 device_unregister(&channel_subsystems[i]->device);
834 struct channel_subsystem *css;
837 css = channel_subsystems[i];
838 device_unregister(&css->pseudo_subchannel->dev);
839 css->pseudo_subchannel = NULL;
840 if (css_chsc_characteristics.secm)
841 device_remove_file(&css->device,
842 &dev_attr_cm_enable);
843 device_unregister(&css->device);
846 bus_unregister(&css_bus_type);
848 s390_unregister_crw_handler(CRW_RSC_CSS);
849 chsc_free_sei_area();
850 kfree(slow_subchannel_set);
851 pr_alert("The CSS device driver initialization failed with "
856 int sch_is_pseudo_sch(struct subchannel *sch)
858 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
861 static int css_bus_match(struct device *dev, struct device_driver *drv)
863 struct subchannel *sch = to_subchannel(dev);
864 struct css_driver *driver = to_cssdriver(drv);
865 struct css_device_id *id;
867 for (id = driver->subchannel_type; id->match_flags; id++) {
868 if (sch->st == id->type)
875 static int css_probe(struct device *dev)
877 struct subchannel *sch;
880 sch = to_subchannel(dev);
881 sch->driver = to_cssdriver(dev->driver);
882 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
888 static int css_remove(struct device *dev)
890 struct subchannel *sch;
893 sch = to_subchannel(dev);
894 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
899 static void css_shutdown(struct device *dev)
901 struct subchannel *sch;
903 sch = to_subchannel(dev);
904 if (sch->driver && sch->driver->shutdown)
905 sch->driver->shutdown(sch);
908 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
910 struct subchannel *sch = to_subchannel(dev);
913 ret = add_uevent_var(env, "ST=%01X", sch->st);
916 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
920 struct bus_type css_bus_type = {
922 .match = css_bus_match,
924 .remove = css_remove,
925 .shutdown = css_shutdown,
926 .uevent = css_uevent,
930 * css_driver_register - register a css driver
931 * @cdrv: css driver to register
933 * This is mainly a wrapper around driver_register that sets name
934 * and bus_type in the embedded struct device_driver correctly.
936 int css_driver_register(struct css_driver *cdrv)
938 cdrv->drv.name = cdrv->name;
939 cdrv->drv.bus = &css_bus_type;
940 cdrv->drv.owner = cdrv->owner;
941 return driver_register(&cdrv->drv);
943 EXPORT_SYMBOL_GPL(css_driver_register);
946 * css_driver_unregister - unregister a css driver
947 * @cdrv: css driver to unregister
949 * This is a wrapper around driver_unregister.
951 void css_driver_unregister(struct css_driver *cdrv)
953 driver_unregister(&cdrv->drv);
955 EXPORT_SYMBOL_GPL(css_driver_unregister);
957 subsys_initcall(init_channel_subsystem);
959 MODULE_LICENSE("GPL");
960 EXPORT_SYMBOL(css_bus_type);