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>
17 #include "../s390mach.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 ssize_t type_show(struct device *dev, struct device_attribute *attr,
216 struct subchannel *sch = to_subchannel(dev);
218 return sprintf(buf, "%01x\n", sch->st);
221 static DEVICE_ATTR(type, 0444, type_show, NULL);
223 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
226 struct subchannel *sch = to_subchannel(dev);
228 return sprintf(buf, "css:t%01X\n", sch->st);
231 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
233 static struct attribute *subch_attrs[] = {
235 &dev_attr_modalias.attr,
239 static struct attribute_group subch_attr_group = {
240 .attrs = subch_attrs,
243 static struct attribute_group *default_subch_attr_groups[] = {
248 static int css_register_subchannel(struct subchannel *sch)
252 /* Initialize the subchannel structure */
253 sch->dev.parent = &channel_subsystems[0]->device;
254 sch->dev.bus = &css_bus_type;
255 sch->dev.release = &css_subchannel_release;
256 sch->dev.groups = default_subch_attr_groups;
258 * We don't want to generate uevents for I/O subchannels that don't
259 * have a working ccw device behind them since they will be
260 * unregistered before they can be used anyway, so we delay the add
261 * uevent until after device recognition was successful.
262 * Note that we suppress the uevent for all subchannel types;
263 * the subchannel driver can decide itself when it wants to inform
264 * userspace of its existence.
266 sch->dev.uevent_suppress = 1;
267 css_update_ssd_info(sch);
268 /* make it known to the system */
269 ret = css_sch_device_register(sch);
271 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
272 sch->schid.ssid, sch->schid.sch_no, ret);
277 * No driver matched. Generate the uevent now so that
278 * a fitting driver module may be loaded based on the
281 sch->dev.uevent_suppress = 0;
282 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
287 int css_probe_device(struct subchannel_id schid)
290 struct subchannel *sch;
292 sch = css_alloc_subchannel(schid);
295 ret = css_register_subchannel(sch);
297 css_free_subchannel(sch);
302 check_subchannel(struct device * dev, void * data)
304 struct subchannel *sch;
305 struct subchannel_id *schid = data;
307 sch = to_subchannel(dev);
308 return schid_equal(&sch->schid, schid);
312 get_subchannel_by_schid(struct subchannel_id schid)
316 dev = bus_find_device(&css_bus_type, NULL,
317 &schid, check_subchannel);
319 return dev ? to_subchannel(dev) : NULL;
323 * css_sch_is_valid() - check if a subchannel is valid
324 * @schib: subchannel information block for the subchannel
326 int css_sch_is_valid(struct schib *schib)
328 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
332 EXPORT_SYMBOL_GPL(css_sch_is_valid);
334 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
339 /* Will be done on the slow path. */
342 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
343 /* Unusable - ignore. */
346 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
347 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
349 return css_probe_device(schid);
352 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
357 if (sch->driver->sch_event)
358 ret = sch->driver->sch_event(sch, slow);
361 "Got subchannel machine check but "
362 "no sch_event handler provided.\n");
367 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
369 struct subchannel *sch;
372 sch = get_subchannel_by_schid(schid);
374 ret = css_evaluate_known_subchannel(sch, slow);
375 put_device(&sch->dev);
377 ret = css_evaluate_new_subchannel(schid, slow);
379 css_schedule_eval(schid);
382 static struct idset *slow_subchannel_set;
383 static spinlock_t slow_subchannel_lock;
385 static int __init slow_subchannel_init(void)
387 spin_lock_init(&slow_subchannel_lock);
388 slow_subchannel_set = idset_sch_new();
389 if (!slow_subchannel_set) {
390 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
396 static int slow_eval_known_fn(struct subchannel *sch, void *data)
401 spin_lock_irq(&slow_subchannel_lock);
402 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
403 idset_sch_del(slow_subchannel_set, sch->schid);
404 spin_unlock_irq(&slow_subchannel_lock);
406 rc = css_evaluate_known_subchannel(sch, 1);
408 css_schedule_eval(sch->schid);
413 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
418 spin_lock_irq(&slow_subchannel_lock);
419 eval = idset_sch_contains(slow_subchannel_set, schid);
420 idset_sch_del(slow_subchannel_set, schid);
421 spin_unlock_irq(&slow_subchannel_lock);
423 rc = css_evaluate_new_subchannel(schid, 1);
426 css_schedule_eval(schid);
432 /* These should abort looping */
441 static void css_slow_path_func(struct work_struct *unused)
443 CIO_TRACE_EVENT(4, "slowpath");
444 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
448 static DECLARE_WORK(slow_path_work, css_slow_path_func);
449 struct workqueue_struct *slow_path_wq;
451 void css_schedule_eval(struct subchannel_id schid)
455 spin_lock_irqsave(&slow_subchannel_lock, flags);
456 idset_sch_add(slow_subchannel_set, schid);
457 queue_work(slow_path_wq, &slow_path_work);
458 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
461 void css_schedule_eval_all(void)
465 spin_lock_irqsave(&slow_subchannel_lock, flags);
466 idset_fill(slow_subchannel_set);
467 queue_work(slow_path_wq, &slow_path_work);
468 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
471 void css_wait_for_slow_path(void)
473 flush_workqueue(ccw_device_notify_work);
474 flush_workqueue(slow_path_wq);
477 /* Reprobe subchannel if unregistered. */
478 static int reprobe_subchannel(struct subchannel_id schid, void *data)
482 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
483 schid.ssid, schid.sch_no);
487 ret = css_probe_device(schid);
494 /* These should abort looping */
503 /* Work function used to reprobe all unregistered subchannels. */
504 static void reprobe_all(struct work_struct *unused)
508 CIO_MSG_EVENT(4, "reprobe start\n");
511 /* Make sure initial subchannel scan is done. */
512 wait_event(ccw_device_init_wq,
513 atomic_read(&ccw_device_init_count) == 0);
514 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
516 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
520 static DECLARE_WORK(css_reprobe_work, reprobe_all);
522 /* Schedule reprobing of all unregistered subchannels. */
523 void css_schedule_reprobe(void)
526 queue_work(slow_path_wq, &css_reprobe_work);
529 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
532 * Called from the machine check handler for subchannel report words.
534 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
536 struct subchannel_id mchk_schid;
539 css_schedule_eval_all();
542 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
543 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
544 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
545 crw0->erc, crw0->rsid);
547 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
548 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
549 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
550 crw1->anc, crw1->erc, crw1->rsid);
551 init_subchannel_id(&mchk_schid);
552 mchk_schid.sch_no = crw0->rsid;
554 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
557 * Since we are always presented with IPI in the CRW, we have to
558 * use stsch() to find out if the subchannel in question has come
561 css_evaluate_subchannel(mchk_schid, 0);
565 __init_channel_subsystem(struct subchannel_id schid, void *data)
567 struct subchannel *sch;
570 if (cio_is_console(schid))
571 sch = cio_get_console_subchannel();
573 sch = css_alloc_subchannel(schid);
582 panic("Out of memory in init_channel_subsystem\n");
583 /* -ENXIO: no more subchannels. */
586 /* -EIO: this subchannel set not supported. */
594 * We register ALL valid subchannels in ioinfo, even those
595 * that have been present before init_channel_subsystem.
596 * These subchannels can't have been registered yet (kmalloc
597 * not working) so we do it now. This is true e.g. for the
598 * console subchannel.
600 css_register_subchannel(sch);
605 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
607 if (css_characteristics_avail && css_general_characteristics.mcss) {
608 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
609 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
612 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
614 css->global_pgid.pgid_high.cpu_addr = 0;
617 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
618 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
619 css->global_pgid.tod_high = tod_high;
624 channel_subsystem_release(struct device *dev)
626 struct channel_subsystem *css;
629 mutex_destroy(&css->mutex);
634 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
637 struct channel_subsystem *css = to_css(dev);
642 mutex_lock(&css->mutex);
643 ret = sprintf(buf, "%x\n", css->cm_enabled);
644 mutex_unlock(&css->mutex);
649 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
650 const char *buf, size_t count)
652 struct channel_subsystem *css = to_css(dev);
656 ret = strict_strtoul(buf, 16, &val);
659 mutex_lock(&css->mutex);
662 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
665 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
670 mutex_unlock(&css->mutex);
671 return ret < 0 ? ret : count;
674 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
676 static int __init setup_css(int nr)
680 struct channel_subsystem *css;
682 css = channel_subsystems[nr];
683 memset(css, 0, sizeof(struct channel_subsystem));
684 css->pseudo_subchannel =
685 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
686 if (!css->pseudo_subchannel)
688 css->pseudo_subchannel->dev.parent = &css->device;
689 css->pseudo_subchannel->dev.release = css_subchannel_release;
690 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
691 ret = cio_create_sch_lock(css->pseudo_subchannel);
693 kfree(css->pseudo_subchannel);
696 mutex_init(&css->mutex);
699 sprintf(css->device.bus_id, "css%x", nr);
700 css->device.release = channel_subsystem_release;
701 tod_high = (u32) (get_clock() >> 32);
702 css_generate_pgid(css, tod_high);
706 static int css_reboot_event(struct notifier_block *this,
713 for (i = 0; i <= __MAX_CSSID; i++) {
714 struct channel_subsystem *css;
716 css = channel_subsystems[i];
717 mutex_lock(&css->mutex);
719 if (chsc_secm(css, 0))
721 mutex_unlock(&css->mutex);
727 static struct notifier_block css_reboot_notifier = {
728 .notifier_call = css_reboot_event,
732 * Now that the driver core is running, we can setup our channel subsystem.
733 * The struct subchannel's are created during probing (except for the
734 * static console subchannel).
737 init_channel_subsystem (void)
741 ret = chsc_determine_css_characteristics();
743 goto out; /* No need to continue. */
745 css_characteristics_avail = 1;
747 ret = chsc_alloc_sei_area();
751 ret = slow_subchannel_init();
755 ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
759 if ((ret = bus_register(&css_bus_type)))
762 /* Try to enable MSS. */
763 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
765 case 0: /* Success. */
766 max_ssid = __MAX_SSID;
773 /* Setup css structure. */
774 for (i = 0; i <= __MAX_CSSID; i++) {
775 struct channel_subsystem *css;
777 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
782 channel_subsystems[i] = css;
786 ret = device_register(&css->device);
789 if (css_characteristics_avail &&
790 css_chsc_characteristics.secm) {
791 ret = device_create_file(&css->device,
792 &dev_attr_cm_enable);
796 ret = device_register(&css->pseudo_subchannel->dev);
800 ret = register_reboot_notifier(&css_reboot_notifier);
807 for_each_subchannel(__init_channel_subsystem, NULL);
810 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
812 device_remove_file(&channel_subsystems[i]->device,
813 &dev_attr_cm_enable);
815 device_unregister(&channel_subsystems[i]->device);
817 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
818 kfree(channel_subsystems[i]->pseudo_subchannel);
820 kfree(channel_subsystems[i]);
823 struct channel_subsystem *css;
826 css = channel_subsystems[i];
827 device_unregister(&css->pseudo_subchannel->dev);
828 if (css_characteristics_avail && css_chsc_characteristics.secm)
829 device_remove_file(&css->device,
830 &dev_attr_cm_enable);
831 device_unregister(&css->device);
834 bus_unregister(&css_bus_type);
836 s390_unregister_crw_handler(CRW_RSC_CSS);
837 chsc_free_sei_area();
838 kfree(slow_subchannel_set);
839 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
844 int sch_is_pseudo_sch(struct subchannel *sch)
846 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
850 * find a driver for a subchannel. They identify by the subchannel
851 * type with the exception that the console subchannel driver has its own
852 * subchannel type although the device is an i/o subchannel
855 css_bus_match (struct device *dev, struct device_driver *drv)
857 struct subchannel *sch = to_subchannel(dev);
858 struct css_driver *driver = to_cssdriver(drv);
860 if (sch->st == driver->subchannel_type)
866 static int css_probe(struct device *dev)
868 struct subchannel *sch;
871 sch = to_subchannel(dev);
872 sch->driver = to_cssdriver(dev->driver);
873 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
879 static int css_remove(struct device *dev)
881 struct subchannel *sch;
884 sch = to_subchannel(dev);
885 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
890 static void css_shutdown(struct device *dev)
892 struct subchannel *sch;
894 sch = to_subchannel(dev);
895 if (sch->driver && sch->driver->shutdown)
896 sch->driver->shutdown(sch);
899 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
901 struct subchannel *sch = to_subchannel(dev);
904 ret = add_uevent_var(env, "ST=%01X", sch->st);
907 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
911 struct bus_type css_bus_type = {
913 .match = css_bus_match,
915 .remove = css_remove,
916 .shutdown = css_shutdown,
917 .uevent = css_uevent,
921 * css_driver_register - register a css driver
922 * @cdrv: css driver to register
924 * This is mainly a wrapper around driver_register that sets name
925 * and bus_type in the embedded struct device_driver correctly.
927 int css_driver_register(struct css_driver *cdrv)
929 cdrv->drv.name = cdrv->name;
930 cdrv->drv.bus = &css_bus_type;
931 cdrv->drv.owner = cdrv->owner;
932 return driver_register(&cdrv->drv);
934 EXPORT_SYMBOL_GPL(css_driver_register);
937 * css_driver_unregister - unregister a css driver
938 * @cdrv: css driver to unregister
940 * This is a wrapper around driver_unregister.
942 void css_driver_unregister(struct css_driver *cdrv)
944 driver_unregister(&cdrv->drv);
946 EXPORT_SYMBOL_GPL(css_driver_unregister);
948 subsys_initcall(init_channel_subsystem);
950 MODULE_LICENSE("GPL");
951 EXPORT_SYMBOL(css_bus_type);
952 EXPORT_SYMBOL_GPL(css_characteristics_avail);