2 * driver for channel subsystem
4 * Copyright IBM Corp. 2002, 2010
12 #define KMSG_COMPONENT "cio"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/reboot.h>
22 #include <linux/suspend.h>
23 #include <linux/proc_fs.h>
29 #include "cio_debug.h"
36 int css_init_done = 0;
40 struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1];
41 static struct bus_type css_bus_type;
44 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
46 struct subchannel_id schid;
49 init_subchannel_id(&schid);
52 ret = fn(schid, data);
55 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
57 } while (schid.ssid++ < max_ssid);
64 int (*fn_known_sch)(struct subchannel *, void *);
65 int (*fn_unknown_sch)(struct subchannel_id, void *);
68 static int call_fn_known_sch(struct device *dev, void *data)
70 struct subchannel *sch = to_subchannel(dev);
71 struct cb_data *cb = data;
75 idset_sch_del(cb->set, sch->schid);
77 rc = cb->fn_known_sch(sch, cb->data);
81 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
83 struct cb_data *cb = data;
86 if (idset_sch_contains(cb->set, schid))
87 rc = cb->fn_unknown_sch(schid, cb->data);
91 static int call_fn_all_sch(struct subchannel_id schid, void *data)
93 struct cb_data *cb = data;
94 struct subchannel *sch;
97 sch = get_subchannel_by_schid(schid);
100 rc = cb->fn_known_sch(sch, cb->data);
101 put_device(&sch->dev);
103 if (cb->fn_unknown_sch)
104 rc = cb->fn_unknown_sch(schid, cb->data);
110 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
111 int (*fn_unknown)(struct subchannel_id,
118 cb.fn_known_sch = fn_known;
119 cb.fn_unknown_sch = fn_unknown;
121 if (fn_known && !fn_unknown) {
122 /* Skip idset allocation in case of known-only loop. */
124 return bus_for_each_dev(&css_bus_type, NULL, &cb,
128 cb.set = idset_sch_new();
130 /* fall back to brute force scanning in case of oom */
131 return for_each_subchannel(call_fn_all_sch, &cb);
135 /* Process registered subchannels. */
136 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
139 /* Process unregistered subchannels. */
141 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
148 static void css_sch_todo(struct work_struct *work);
150 static int css_sch_create_locks(struct subchannel *sch)
152 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
156 spin_lock_init(sch->lock);
157 mutex_init(&sch->reg_mutex);
162 static void css_subchannel_release(struct device *dev)
164 struct subchannel *sch = to_subchannel(dev);
166 sch->config.intparm = 0;
167 cio_commit_config(sch);
172 struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
174 struct subchannel *sch;
177 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
179 return ERR_PTR(-ENOMEM);
181 ret = cio_validate_subchannel(sch, schid);
185 ret = css_sch_create_locks(sch);
189 INIT_WORK(&sch->todo_work, css_sch_todo);
190 sch->dev.release = &css_subchannel_release;
191 device_initialize(&sch->dev);
199 static int css_sch_device_register(struct subchannel *sch)
203 mutex_lock(&sch->reg_mutex);
204 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
206 ret = device_add(&sch->dev);
207 mutex_unlock(&sch->reg_mutex);
212 * css_sch_device_unregister - unregister a subchannel
213 * @sch: subchannel to be unregistered
215 void css_sch_device_unregister(struct subchannel *sch)
217 mutex_lock(&sch->reg_mutex);
218 if (device_is_registered(&sch->dev))
219 device_unregister(&sch->dev);
220 mutex_unlock(&sch->reg_mutex);
222 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
224 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
229 memset(ssd, 0, sizeof(struct chsc_ssd_info));
230 ssd->path_mask = pmcw->pim;
231 for (i = 0; i < 8; i++) {
233 if (pmcw->pim & mask) {
234 chp_id_init(&ssd->chpid[i]);
235 ssd->chpid[i].id = pmcw->chpid[i];
240 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
245 for (i = 0; i < 8; i++) {
247 if (ssd->path_mask & mask)
248 if (!chp_is_registered(ssd->chpid[i]))
249 chp_new(ssd->chpid[i]);
253 void css_update_ssd_info(struct subchannel *sch)
257 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
259 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
261 ssd_register_chpids(&sch->ssd_info);
264 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
267 struct subchannel *sch = to_subchannel(dev);
269 return sprintf(buf, "%01x\n", sch->st);
272 static DEVICE_ATTR(type, 0444, type_show, NULL);
274 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
277 struct subchannel *sch = to_subchannel(dev);
279 return sprintf(buf, "css:t%01X\n", sch->st);
282 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
284 static struct attribute *subch_attrs[] = {
286 &dev_attr_modalias.attr,
290 static struct attribute_group subch_attr_group = {
291 .attrs = subch_attrs,
294 static const struct attribute_group *default_subch_attr_groups[] = {
299 static ssize_t chpids_show(struct device *dev,
300 struct device_attribute *attr,
303 struct subchannel *sch = to_subchannel(dev);
304 struct chsc_ssd_info *ssd = &sch->ssd_info;
309 for (chp = 0; chp < 8; chp++) {
311 if (ssd->path_mask & mask)
312 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
314 ret += sprintf(buf + ret, "00 ");
316 ret += sprintf(buf + ret, "\n");
319 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
321 static ssize_t pimpampom_show(struct device *dev,
322 struct device_attribute *attr,
325 struct subchannel *sch = to_subchannel(dev);
326 struct pmcw *pmcw = &sch->schib.pmcw;
328 return sprintf(buf, "%02x %02x %02x\n",
329 pmcw->pim, pmcw->pam, pmcw->pom);
331 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
333 static struct attribute *io_subchannel_type_attrs[] = {
334 &dev_attr_chpids.attr,
335 &dev_attr_pimpampom.attr,
338 ATTRIBUTE_GROUPS(io_subchannel_type);
340 static const struct device_type io_subchannel_type = {
341 .groups = io_subchannel_type_groups,
344 int css_register_subchannel(struct subchannel *sch)
348 /* Initialize the subchannel structure */
349 sch->dev.parent = &channel_subsystems[0]->device;
350 sch->dev.bus = &css_bus_type;
351 sch->dev.groups = default_subch_attr_groups;
353 if (sch->st == SUBCHANNEL_TYPE_IO)
354 sch->dev.type = &io_subchannel_type;
357 * We don't want to generate uevents for I/O subchannels that don't
358 * have a working ccw device behind them since they will be
359 * unregistered before they can be used anyway, so we delay the add
360 * uevent until after device recognition was successful.
361 * Note that we suppress the uevent for all subchannel types;
362 * the subchannel driver can decide itself when it wants to inform
363 * userspace of its existence.
365 dev_set_uevent_suppress(&sch->dev, 1);
366 css_update_ssd_info(sch);
367 /* make it known to the system */
368 ret = css_sch_device_register(sch);
370 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
371 sch->schid.ssid, sch->schid.sch_no, ret);
376 * No driver matched. Generate the uevent now so that
377 * a fitting driver module may be loaded based on the
380 dev_set_uevent_suppress(&sch->dev, 0);
381 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
386 static int css_probe_device(struct subchannel_id schid)
388 struct subchannel *sch;
391 sch = css_alloc_subchannel(schid);
395 ret = css_register_subchannel(sch);
397 put_device(&sch->dev);
403 check_subchannel(struct device * dev, void * data)
405 struct subchannel *sch;
406 struct subchannel_id *schid = data;
408 sch = to_subchannel(dev);
409 return schid_equal(&sch->schid, schid);
413 get_subchannel_by_schid(struct subchannel_id schid)
417 dev = bus_find_device(&css_bus_type, NULL,
418 &schid, check_subchannel);
420 return dev ? to_subchannel(dev) : NULL;
424 * css_sch_is_valid() - check if a subchannel is valid
425 * @schib: subchannel information block for the subchannel
427 int css_sch_is_valid(struct schib *schib)
429 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
431 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
435 EXPORT_SYMBOL_GPL(css_sch_is_valid);
437 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
442 /* Will be done on the slow path. */
445 if (stsch(schid, &schib)) {
446 /* Subchannel is not provided. */
449 if (!css_sch_is_valid(&schib)) {
450 /* Unusable - ignore. */
453 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
456 return css_probe_device(schid);
459 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
464 if (sch->driver->sch_event)
465 ret = sch->driver->sch_event(sch, slow);
468 "Got subchannel machine check but "
469 "no sch_event handler provided.\n");
471 if (ret != 0 && ret != -EAGAIN) {
472 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
473 sch->schid.ssid, sch->schid.sch_no, ret);
478 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
480 struct subchannel *sch;
483 sch = get_subchannel_by_schid(schid);
485 ret = css_evaluate_known_subchannel(sch, slow);
486 put_device(&sch->dev);
488 ret = css_evaluate_new_subchannel(schid, slow);
490 css_schedule_eval(schid);
494 * css_sched_sch_todo - schedule a subchannel operation
498 * Schedule the operation identified by @todo to be performed on the slow path
499 * workqueue. Do nothing if another operation with higher priority is already
500 * scheduled. Needs to be called with subchannel lock held.
502 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
504 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
505 sch->schid.ssid, sch->schid.sch_no, todo);
506 if (sch->todo >= todo)
508 /* Get workqueue ref. */
509 if (!get_device(&sch->dev))
512 if (!queue_work(cio_work_q, &sch->todo_work)) {
513 /* Already queued, release workqueue ref. */
514 put_device(&sch->dev);
517 EXPORT_SYMBOL_GPL(css_sched_sch_todo);
519 static void css_sch_todo(struct work_struct *work)
521 struct subchannel *sch;
525 sch = container_of(work, struct subchannel, todo_work);
527 spin_lock_irq(sch->lock);
529 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
530 sch->schid.sch_no, todo);
531 sch->todo = SCH_TODO_NOTHING;
532 spin_unlock_irq(sch->lock);
535 case SCH_TODO_NOTHING:
538 ret = css_evaluate_known_subchannel(sch, 1);
539 if (ret == -EAGAIN) {
540 spin_lock_irq(sch->lock);
541 css_sched_sch_todo(sch, todo);
542 spin_unlock_irq(sch->lock);
546 css_sch_device_unregister(sch);
549 /* Release workqueue ref. */
550 put_device(&sch->dev);
553 static struct idset *slow_subchannel_set;
554 static spinlock_t slow_subchannel_lock;
555 static wait_queue_head_t css_eval_wq;
556 static atomic_t css_eval_scheduled;
558 static int __init slow_subchannel_init(void)
560 spin_lock_init(&slow_subchannel_lock);
561 atomic_set(&css_eval_scheduled, 0);
562 init_waitqueue_head(&css_eval_wq);
563 slow_subchannel_set = idset_sch_new();
564 if (!slow_subchannel_set) {
565 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
571 static int slow_eval_known_fn(struct subchannel *sch, void *data)
576 spin_lock_irq(&slow_subchannel_lock);
577 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
578 idset_sch_del(slow_subchannel_set, sch->schid);
579 spin_unlock_irq(&slow_subchannel_lock);
581 rc = css_evaluate_known_subchannel(sch, 1);
583 css_schedule_eval(sch->schid);
588 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
593 spin_lock_irq(&slow_subchannel_lock);
594 eval = idset_sch_contains(slow_subchannel_set, schid);
595 idset_sch_del(slow_subchannel_set, schid);
596 spin_unlock_irq(&slow_subchannel_lock);
598 rc = css_evaluate_new_subchannel(schid, 1);
601 css_schedule_eval(schid);
607 /* These should abort looping */
608 spin_lock_irq(&slow_subchannel_lock);
609 idset_sch_del_subseq(slow_subchannel_set, schid);
610 spin_unlock_irq(&slow_subchannel_lock);
615 /* Allow scheduling here since the containing loop might
622 static void css_slow_path_func(struct work_struct *unused)
626 CIO_TRACE_EVENT(4, "slowpath");
627 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
629 spin_lock_irqsave(&slow_subchannel_lock, flags);
630 if (idset_is_empty(slow_subchannel_set)) {
631 atomic_set(&css_eval_scheduled, 0);
632 wake_up(&css_eval_wq);
634 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
637 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
638 struct workqueue_struct *cio_work_q;
640 void css_schedule_eval(struct subchannel_id schid)
644 spin_lock_irqsave(&slow_subchannel_lock, flags);
645 idset_sch_add(slow_subchannel_set, schid);
646 atomic_set(&css_eval_scheduled, 1);
647 queue_delayed_work(cio_work_q, &slow_path_work, 0);
648 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
651 void css_schedule_eval_all(void)
655 spin_lock_irqsave(&slow_subchannel_lock, flags);
656 idset_fill(slow_subchannel_set);
657 atomic_set(&css_eval_scheduled, 1);
658 queue_delayed_work(cio_work_q, &slow_path_work, 0);
659 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
662 static int __unset_registered(struct device *dev, void *data)
664 struct idset *set = data;
665 struct subchannel *sch = to_subchannel(dev);
667 idset_sch_del(set, sch->schid);
671 void css_schedule_eval_all_unreg(unsigned long delay)
674 struct idset *unreg_set;
676 /* Find unregistered subchannels. */
677 unreg_set = idset_sch_new();
680 css_schedule_eval_all();
683 idset_fill(unreg_set);
684 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
685 /* Apply to slow_subchannel_set. */
686 spin_lock_irqsave(&slow_subchannel_lock, flags);
687 idset_add_set(slow_subchannel_set, unreg_set);
688 atomic_set(&css_eval_scheduled, 1);
689 queue_delayed_work(cio_work_q, &slow_path_work, delay);
690 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
691 idset_free(unreg_set);
694 void css_wait_for_slow_path(void)
696 flush_workqueue(cio_work_q);
699 /* Schedule reprobing of all unregistered subchannels. */
700 void css_schedule_reprobe(void)
702 /* Schedule with a delay to allow merging of subsequent calls. */
703 css_schedule_eval_all_unreg(1 * HZ);
705 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
708 * Called from the machine check handler for subchannel report words.
710 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
712 struct subchannel_id mchk_schid;
713 struct subchannel *sch;
716 css_schedule_eval_all();
719 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
720 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
721 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
722 crw0->erc, crw0->rsid);
724 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
725 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
726 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
727 crw1->anc, crw1->erc, crw1->rsid);
728 init_subchannel_id(&mchk_schid);
729 mchk_schid.sch_no = crw0->rsid;
731 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
733 if (crw0->erc == CRW_ERC_PMOD) {
734 sch = get_subchannel_by_schid(mchk_schid);
736 css_update_ssd_info(sch);
737 put_device(&sch->dev);
741 * Since we are always presented with IPI in the CRW, we have to
742 * use stsch() to find out if the subchannel in question has come
745 css_evaluate_subchannel(mchk_schid, 0);
749 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
753 if (css_general_characteristics.mcss) {
754 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
755 css->global_pgid.pgid_high.ext_cssid.cssid =
756 (css->cssid < 0) ? 0 : css->cssid;
758 css->global_pgid.pgid_high.cpu_addr = stap();
761 css->global_pgid.cpu_id = cpu_id.ident;
762 css->global_pgid.cpu_model = cpu_id.machine;
763 css->global_pgid.tod_high = tod_high;
766 static void channel_subsystem_release(struct device *dev)
768 struct channel_subsystem *css = to_css(dev);
770 mutex_destroy(&css->mutex);
774 static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
777 struct channel_subsystem *css = to_css(dev);
782 return sprintf(buf, "%x\n", css->cssid);
784 static DEVICE_ATTR_RO(real_cssid);
786 static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
789 struct channel_subsystem *css = to_css(dev);
792 mutex_lock(&css->mutex);
793 ret = sprintf(buf, "%x\n", css->cm_enabled);
794 mutex_unlock(&css->mutex);
798 static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
799 const char *buf, size_t count)
801 struct channel_subsystem *css = to_css(dev);
805 ret = kstrtoul(buf, 16, &val);
808 mutex_lock(&css->mutex);
811 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
814 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
819 mutex_unlock(&css->mutex);
820 return ret < 0 ? ret : count;
822 static DEVICE_ATTR_RW(cm_enable);
824 static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
827 return css_chsc_characteristics.secm ? attr->mode : 0;
830 static struct attribute *cssdev_attrs[] = {
831 &dev_attr_real_cssid.attr,
835 static struct attribute_group cssdev_attr_group = {
836 .attrs = cssdev_attrs,
839 static struct attribute *cssdev_cm_attrs[] = {
840 &dev_attr_cm_enable.attr,
844 static struct attribute_group cssdev_cm_attr_group = {
845 .attrs = cssdev_cm_attrs,
846 .is_visible = cm_enable_mode,
849 static const struct attribute_group *cssdev_attr_groups[] = {
851 &cssdev_cm_attr_group,
855 static int __init setup_css(int nr)
857 struct channel_subsystem *css;
860 css = kzalloc(sizeof(*css), GFP_KERNEL);
864 channel_subsystems[nr] = css;
865 dev_set_name(&css->device, "css%x", nr);
866 css->device.groups = cssdev_attr_groups;
867 css->device.release = channel_subsystem_release;
869 mutex_init(&css->mutex);
870 css->cssid = chsc_get_cssid(nr);
871 css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
873 ret = device_register(&css->device);
875 put_device(&css->device);
879 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel),
881 if (!css->pseudo_subchannel) {
882 device_unregister(&css->device);
887 css->pseudo_subchannel->dev.parent = &css->device;
888 css->pseudo_subchannel->dev.release = css_subchannel_release;
889 mutex_init(&css->pseudo_subchannel->reg_mutex);
890 ret = css_sch_create_locks(css->pseudo_subchannel);
892 kfree(css->pseudo_subchannel);
893 device_unregister(&css->device);
897 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
898 ret = device_register(&css->pseudo_subchannel->dev);
900 put_device(&css->pseudo_subchannel->dev);
901 device_unregister(&css->device);
907 channel_subsystems[nr] = NULL;
911 static int css_reboot_event(struct notifier_block *this,
915 struct channel_subsystem *css;
920 mutex_lock(&css->mutex);
922 if (chsc_secm(css, 0))
924 mutex_unlock(&css->mutex);
930 static struct notifier_block css_reboot_notifier = {
931 .notifier_call = css_reboot_event,
935 * Since the css devices are neither on a bus nor have a class
936 * nor have a special device type, we cannot stop/restart channel
937 * path measurements via the normal suspend/resume callbacks, but have
940 static int css_power_event(struct notifier_block *this, unsigned long event,
943 struct channel_subsystem *css;
947 case PM_HIBERNATION_PREPARE:
948 case PM_SUSPEND_PREPARE:
951 mutex_lock(&css->mutex);
952 if (!css->cm_enabled) {
953 mutex_unlock(&css->mutex);
956 ret = __chsc_do_secm(css, 0);
957 ret = notifier_from_errno(ret);
958 mutex_unlock(&css->mutex);
961 case PM_POST_HIBERNATION:
962 case PM_POST_SUSPEND:
965 mutex_lock(&css->mutex);
966 if (!css->cm_enabled) {
967 mutex_unlock(&css->mutex);
970 ret = __chsc_do_secm(css, 1);
971 ret = notifier_from_errno(ret);
972 mutex_unlock(&css->mutex);
974 /* search for subchannels, which appeared during hibernation */
975 css_schedule_reprobe();
983 static struct notifier_block css_power_notifier = {
984 .notifier_call = css_power_event,
988 * Now that the driver core is running, we can setup our channel subsystem.
989 * The struct subchannel's are created during probing.
991 static int __init css_bus_init(void)
999 chsc_determine_css_characteristics();
1000 /* Try to enable MSS. */
1001 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
1005 max_ssid = __MAX_SSID;
1007 ret = slow_subchannel_init();
1011 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
1015 if ((ret = bus_register(&css_bus_type)))
1018 /* Setup css structure. */
1019 for (i = 0; i <= MAX_CSS_IDX; i++) {
1022 goto out_unregister;
1024 ret = register_reboot_notifier(&css_reboot_notifier);
1026 goto out_unregister;
1027 ret = register_pm_notifier(&css_power_notifier);
1029 unregister_reboot_notifier(&css_reboot_notifier);
1030 goto out_unregister;
1034 /* Enable default isc for I/O subchannels. */
1035 isc_register(IO_SCH_ISC);
1040 struct channel_subsystem *css = channel_subsystems[i];
1041 device_unregister(&css->pseudo_subchannel->dev);
1042 device_unregister(&css->device);
1044 bus_unregister(&css_bus_type);
1046 crw_unregister_handler(CRW_RSC_SCH);
1047 idset_free(slow_subchannel_set);
1048 chsc_init_cleanup();
1049 pr_alert("The CSS device driver initialization failed with "
1054 static void __init css_bus_cleanup(void)
1056 struct channel_subsystem *css;
1059 device_unregister(&css->pseudo_subchannel->dev);
1060 device_unregister(&css->device);
1062 bus_unregister(&css_bus_type);
1063 crw_unregister_handler(CRW_RSC_SCH);
1064 idset_free(slow_subchannel_set);
1065 chsc_init_cleanup();
1066 isc_unregister(IO_SCH_ISC);
1069 static int __init channel_subsystem_init(void)
1073 ret = css_bus_init();
1076 cio_work_q = create_singlethread_workqueue("cio");
1081 ret = io_subchannel_init();
1087 destroy_workqueue(cio_work_q);
1092 subsys_initcall(channel_subsystem_init);
1094 static int css_settle(struct device_driver *drv, void *unused)
1096 struct css_driver *cssdrv = to_cssdriver(drv);
1099 return cssdrv->settle();
1103 int css_complete_work(void)
1107 /* Wait for the evaluation of subchannels to finish. */
1108 ret = wait_event_interruptible(css_eval_wq,
1109 atomic_read(&css_eval_scheduled) == 0);
1112 flush_workqueue(cio_work_q);
1113 /* Wait for the subchannel type specific initialization to finish */
1114 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1119 * Wait for the initialization of devices to finish, to make sure we are
1120 * done with our setup if the search for the root device starts.
1122 static int __init channel_subsystem_init_sync(void)
1124 /* Register subchannels which are already in use. */
1125 cio_register_early_subchannels();
1126 /* Start initial subchannel evaluation. */
1127 css_schedule_eval_all();
1128 css_complete_work();
1131 subsys_initcall_sync(channel_subsystem_init_sync);
1133 void channel_subsystem_reinit(void)
1135 struct channel_path *chp;
1136 struct chp_id chpid;
1138 chsc_enable_facility(CHSC_SDA_OC_MSS);
1139 chp_id_for_each(&chpid) {
1140 chp = chpid_to_chp(chpid);
1142 chp_update_desc(chp);
1147 #ifdef CONFIG_PROC_FS
1148 static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1149 size_t count, loff_t *ppos)
1153 /* Handle pending CRW's. */
1154 crw_wait_for_channel_report();
1155 ret = css_complete_work();
1157 return ret ? ret : count;
1160 static const struct file_operations cio_settle_proc_fops = {
1161 .open = nonseekable_open,
1162 .write = cio_settle_write,
1163 .llseek = no_llseek,
1166 static int __init cio_settle_init(void)
1168 struct proc_dir_entry *entry;
1170 entry = proc_create("cio_settle", S_IWUSR, NULL,
1171 &cio_settle_proc_fops);
1176 device_initcall(cio_settle_init);
1177 #endif /*CONFIG_PROC_FS*/
1179 int sch_is_pseudo_sch(struct subchannel *sch)
1181 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1184 static int css_bus_match(struct device *dev, struct device_driver *drv)
1186 struct subchannel *sch = to_subchannel(dev);
1187 struct css_driver *driver = to_cssdriver(drv);
1188 struct css_device_id *id;
1190 for (id = driver->subchannel_type; id->match_flags; id++) {
1191 if (sch->st == id->type)
1198 static int css_probe(struct device *dev)
1200 struct subchannel *sch;
1203 sch = to_subchannel(dev);
1204 sch->driver = to_cssdriver(dev->driver);
1205 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1211 static int css_remove(struct device *dev)
1213 struct subchannel *sch;
1216 sch = to_subchannel(dev);
1217 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1222 static void css_shutdown(struct device *dev)
1224 struct subchannel *sch;
1226 sch = to_subchannel(dev);
1227 if (sch->driver && sch->driver->shutdown)
1228 sch->driver->shutdown(sch);
1231 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1233 struct subchannel *sch = to_subchannel(dev);
1236 ret = add_uevent_var(env, "ST=%01X", sch->st);
1239 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1243 static int css_pm_prepare(struct device *dev)
1245 struct subchannel *sch = to_subchannel(dev);
1246 struct css_driver *drv;
1248 if (mutex_is_locked(&sch->reg_mutex))
1250 if (!sch->dev.driver)
1252 drv = to_cssdriver(sch->dev.driver);
1253 /* Notify drivers that they may not register children. */
1254 return drv->prepare ? drv->prepare(sch) : 0;
1257 static void css_pm_complete(struct device *dev)
1259 struct subchannel *sch = to_subchannel(dev);
1260 struct css_driver *drv;
1262 if (!sch->dev.driver)
1264 drv = to_cssdriver(sch->dev.driver);
1269 static int css_pm_freeze(struct device *dev)
1271 struct subchannel *sch = to_subchannel(dev);
1272 struct css_driver *drv;
1274 if (!sch->dev.driver)
1276 drv = to_cssdriver(sch->dev.driver);
1277 return drv->freeze ? drv->freeze(sch) : 0;
1280 static int css_pm_thaw(struct device *dev)
1282 struct subchannel *sch = to_subchannel(dev);
1283 struct css_driver *drv;
1285 if (!sch->dev.driver)
1287 drv = to_cssdriver(sch->dev.driver);
1288 return drv->thaw ? drv->thaw(sch) : 0;
1291 static int css_pm_restore(struct device *dev)
1293 struct subchannel *sch = to_subchannel(dev);
1294 struct css_driver *drv;
1296 css_update_ssd_info(sch);
1297 if (!sch->dev.driver)
1299 drv = to_cssdriver(sch->dev.driver);
1300 return drv->restore ? drv->restore(sch) : 0;
1303 static const struct dev_pm_ops css_pm_ops = {
1304 .prepare = css_pm_prepare,
1305 .complete = css_pm_complete,
1306 .freeze = css_pm_freeze,
1307 .thaw = css_pm_thaw,
1308 .restore = css_pm_restore,
1311 static struct bus_type css_bus_type = {
1313 .match = css_bus_match,
1315 .remove = css_remove,
1316 .shutdown = css_shutdown,
1317 .uevent = css_uevent,
1322 * css_driver_register - register a css driver
1323 * @cdrv: css driver to register
1325 * This is mainly a wrapper around driver_register that sets name
1326 * and bus_type in the embedded struct device_driver correctly.
1328 int css_driver_register(struct css_driver *cdrv)
1330 cdrv->drv.bus = &css_bus_type;
1331 return driver_register(&cdrv->drv);
1333 EXPORT_SYMBOL_GPL(css_driver_register);
1336 * css_driver_unregister - unregister a css driver
1337 * @cdrv: css driver to unregister
1339 * This is a wrapper around driver_unregister.
1341 void css_driver_unregister(struct css_driver *cdrv)
1343 driver_unregister(&cdrv->drv);
1345 EXPORT_SYMBOL_GPL(css_driver_unregister);