1 // SPDX-License-Identifier: GPL-2.0
3 * driver for channel subsystem
5 * Copyright IBM Corp. 2002, 2010
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <linux/list.h>
20 #include <linux/reboot.h>
21 #include <linux/suspend.h>
22 #include <linux/proc_fs.h>
28 #include "cio_debug.h"
35 int css_init_done = 0;
39 struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1];
40 static struct bus_type css_bus_type;
43 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
45 struct subchannel_id schid;
48 init_subchannel_id(&schid);
51 ret = fn(schid, data);
54 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
56 } while (schid.ssid++ < max_ssid);
63 int (*fn_known_sch)(struct subchannel *, void *);
64 int (*fn_unknown_sch)(struct subchannel_id, void *);
67 static int call_fn_known_sch(struct device *dev, void *data)
69 struct subchannel *sch = to_subchannel(dev);
70 struct cb_data *cb = data;
74 idset_sch_del(cb->set, sch->schid);
76 rc = cb->fn_known_sch(sch, cb->data);
80 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
82 struct cb_data *cb = data;
85 if (idset_sch_contains(cb->set, schid))
86 rc = cb->fn_unknown_sch(schid, cb->data);
90 static int call_fn_all_sch(struct subchannel_id schid, void *data)
92 struct cb_data *cb = data;
93 struct subchannel *sch;
96 sch = get_subchannel_by_schid(schid);
99 rc = cb->fn_known_sch(sch, cb->data);
100 put_device(&sch->dev);
102 if (cb->fn_unknown_sch)
103 rc = cb->fn_unknown_sch(schid, cb->data);
109 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
110 int (*fn_unknown)(struct subchannel_id,
117 cb.fn_known_sch = fn_known;
118 cb.fn_unknown_sch = fn_unknown;
120 if (fn_known && !fn_unknown) {
121 /* Skip idset allocation in case of known-only loop. */
123 return bus_for_each_dev(&css_bus_type, NULL, &cb,
127 cb.set = idset_sch_new();
129 /* fall back to brute force scanning in case of oom */
130 return for_each_subchannel(call_fn_all_sch, &cb);
134 /* Process registered subchannels. */
135 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
138 /* Process unregistered subchannels. */
140 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
147 static void css_sch_todo(struct work_struct *work);
149 static int css_sch_create_locks(struct subchannel *sch)
151 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
155 spin_lock_init(sch->lock);
156 mutex_init(&sch->reg_mutex);
161 static void css_subchannel_release(struct device *dev)
163 struct subchannel *sch = to_subchannel(dev);
165 sch->config.intparm = 0;
166 cio_commit_config(sch);
171 struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
173 struct subchannel *sch;
176 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
178 return ERR_PTR(-ENOMEM);
180 ret = cio_validate_subchannel(sch, schid);
184 ret = css_sch_create_locks(sch);
188 INIT_WORK(&sch->todo_work, css_sch_todo);
189 sch->dev.release = &css_subchannel_release;
190 device_initialize(&sch->dev);
198 static int css_sch_device_register(struct subchannel *sch)
202 mutex_lock(&sch->reg_mutex);
203 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
205 ret = device_add(&sch->dev);
206 mutex_unlock(&sch->reg_mutex);
211 * css_sch_device_unregister - unregister a subchannel
212 * @sch: subchannel to be unregistered
214 void css_sch_device_unregister(struct subchannel *sch)
216 mutex_lock(&sch->reg_mutex);
217 if (device_is_registered(&sch->dev))
218 device_unregister(&sch->dev);
219 mutex_unlock(&sch->reg_mutex);
221 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
223 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
228 memset(ssd, 0, sizeof(struct chsc_ssd_info));
229 ssd->path_mask = pmcw->pim;
230 for (i = 0; i < 8; i++) {
232 if (pmcw->pim & mask) {
233 chp_id_init(&ssd->chpid[i]);
234 ssd->chpid[i].id = pmcw->chpid[i];
239 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
244 for (i = 0; i < 8; i++) {
246 if (ssd->path_mask & mask)
247 if (!chp_is_registered(ssd->chpid[i]))
248 chp_new(ssd->chpid[i]);
252 void css_update_ssd_info(struct subchannel *sch)
256 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
258 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
260 ssd_register_chpids(&sch->ssd_info);
263 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
266 struct subchannel *sch = to_subchannel(dev);
268 return sprintf(buf, "%01x\n", sch->st);
271 static DEVICE_ATTR(type, 0444, type_show, NULL);
273 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
276 struct subchannel *sch = to_subchannel(dev);
278 return sprintf(buf, "css:t%01X\n", sch->st);
281 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
283 static struct attribute *subch_attrs[] = {
285 &dev_attr_modalias.attr,
289 static struct attribute_group subch_attr_group = {
290 .attrs = subch_attrs,
293 static const struct attribute_group *default_subch_attr_groups[] = {
298 static ssize_t chpids_show(struct device *dev,
299 struct device_attribute *attr,
302 struct subchannel *sch = to_subchannel(dev);
303 struct chsc_ssd_info *ssd = &sch->ssd_info;
308 for (chp = 0; chp < 8; chp++) {
310 if (ssd->path_mask & mask)
311 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
313 ret += sprintf(buf + ret, "00 ");
315 ret += sprintf(buf + ret, "\n");
318 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
320 static ssize_t pimpampom_show(struct device *dev,
321 struct device_attribute *attr,
324 struct subchannel *sch = to_subchannel(dev);
325 struct pmcw *pmcw = &sch->schib.pmcw;
327 return sprintf(buf, "%02x %02x %02x\n",
328 pmcw->pim, pmcw->pam, pmcw->pom);
330 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
332 static struct attribute *io_subchannel_type_attrs[] = {
333 &dev_attr_chpids.attr,
334 &dev_attr_pimpampom.attr,
337 ATTRIBUTE_GROUPS(io_subchannel_type);
339 static const struct device_type io_subchannel_type = {
340 .groups = io_subchannel_type_groups,
343 int css_register_subchannel(struct subchannel *sch)
347 /* Initialize the subchannel structure */
348 sch->dev.parent = &channel_subsystems[0]->device;
349 sch->dev.bus = &css_bus_type;
350 sch->dev.groups = default_subch_attr_groups;
352 if (sch->st == SUBCHANNEL_TYPE_IO)
353 sch->dev.type = &io_subchannel_type;
356 * We don't want to generate uevents for I/O subchannels that don't
357 * have a working ccw device behind them since they will be
358 * unregistered before they can be used anyway, so we delay the add
359 * uevent until after device recognition was successful.
360 * Note that we suppress the uevent for all subchannel types;
361 * the subchannel driver can decide itself when it wants to inform
362 * userspace of its existence.
364 dev_set_uevent_suppress(&sch->dev, 1);
365 css_update_ssd_info(sch);
366 /* make it known to the system */
367 ret = css_sch_device_register(sch);
369 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
370 sch->schid.ssid, sch->schid.sch_no, ret);
375 * No driver matched. Generate the uevent now so that
376 * a fitting driver module may be loaded based on the
379 dev_set_uevent_suppress(&sch->dev, 0);
380 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
385 static int css_probe_device(struct subchannel_id schid)
387 struct subchannel *sch;
390 sch = css_alloc_subchannel(schid);
394 ret = css_register_subchannel(sch);
396 put_device(&sch->dev);
402 check_subchannel(struct device * dev, void * data)
404 struct subchannel *sch;
405 struct subchannel_id *schid = data;
407 sch = to_subchannel(dev);
408 return schid_equal(&sch->schid, schid);
412 get_subchannel_by_schid(struct subchannel_id schid)
416 dev = bus_find_device(&css_bus_type, NULL,
417 &schid, check_subchannel);
419 return dev ? to_subchannel(dev) : NULL;
423 * css_sch_is_valid() - check if a subchannel is valid
424 * @schib: subchannel information block for the subchannel
426 int css_sch_is_valid(struct schib *schib)
428 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
430 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
434 EXPORT_SYMBOL_GPL(css_sch_is_valid);
436 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
441 /* Will be done on the slow path. */
444 if (stsch(schid, &schib)) {
445 /* Subchannel is not provided. */
448 if (!css_sch_is_valid(&schib)) {
449 /* Unusable - ignore. */
452 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
455 return css_probe_device(schid);
458 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
463 if (sch->driver->sch_event)
464 ret = sch->driver->sch_event(sch, slow);
467 "Got subchannel machine check but "
468 "no sch_event handler provided.\n");
470 if (ret != 0 && ret != -EAGAIN) {
471 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
472 sch->schid.ssid, sch->schid.sch_no, ret);
477 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
479 struct subchannel *sch;
482 sch = get_subchannel_by_schid(schid);
484 ret = css_evaluate_known_subchannel(sch, slow);
485 put_device(&sch->dev);
487 ret = css_evaluate_new_subchannel(schid, slow);
489 css_schedule_eval(schid);
493 * css_sched_sch_todo - schedule a subchannel operation
497 * Schedule the operation identified by @todo to be performed on the slow path
498 * workqueue. Do nothing if another operation with higher priority is already
499 * scheduled. Needs to be called with subchannel lock held.
501 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
503 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
504 sch->schid.ssid, sch->schid.sch_no, todo);
505 if (sch->todo >= todo)
507 /* Get workqueue ref. */
508 if (!get_device(&sch->dev))
511 if (!queue_work(cio_work_q, &sch->todo_work)) {
512 /* Already queued, release workqueue ref. */
513 put_device(&sch->dev);
516 EXPORT_SYMBOL_GPL(css_sched_sch_todo);
518 static void css_sch_todo(struct work_struct *work)
520 struct subchannel *sch;
524 sch = container_of(work, struct subchannel, todo_work);
526 spin_lock_irq(sch->lock);
528 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
529 sch->schid.sch_no, todo);
530 sch->todo = SCH_TODO_NOTHING;
531 spin_unlock_irq(sch->lock);
534 case SCH_TODO_NOTHING:
537 ret = css_evaluate_known_subchannel(sch, 1);
538 if (ret == -EAGAIN) {
539 spin_lock_irq(sch->lock);
540 css_sched_sch_todo(sch, todo);
541 spin_unlock_irq(sch->lock);
545 css_sch_device_unregister(sch);
548 /* Release workqueue ref. */
549 put_device(&sch->dev);
552 static struct idset *slow_subchannel_set;
553 static spinlock_t slow_subchannel_lock;
554 static wait_queue_head_t css_eval_wq;
555 static atomic_t css_eval_scheduled;
557 static int __init slow_subchannel_init(void)
559 spin_lock_init(&slow_subchannel_lock);
560 atomic_set(&css_eval_scheduled, 0);
561 init_waitqueue_head(&css_eval_wq);
562 slow_subchannel_set = idset_sch_new();
563 if (!slow_subchannel_set) {
564 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
570 static int slow_eval_known_fn(struct subchannel *sch, void *data)
575 spin_lock_irq(&slow_subchannel_lock);
576 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
577 idset_sch_del(slow_subchannel_set, sch->schid);
578 spin_unlock_irq(&slow_subchannel_lock);
580 rc = css_evaluate_known_subchannel(sch, 1);
582 css_schedule_eval(sch->schid);
587 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
592 spin_lock_irq(&slow_subchannel_lock);
593 eval = idset_sch_contains(slow_subchannel_set, schid);
594 idset_sch_del(slow_subchannel_set, schid);
595 spin_unlock_irq(&slow_subchannel_lock);
597 rc = css_evaluate_new_subchannel(schid, 1);
600 css_schedule_eval(schid);
606 /* These should abort looping */
607 spin_lock_irq(&slow_subchannel_lock);
608 idset_sch_del_subseq(slow_subchannel_set, schid);
609 spin_unlock_irq(&slow_subchannel_lock);
614 /* Allow scheduling here since the containing loop might
621 static void css_slow_path_func(struct work_struct *unused)
625 CIO_TRACE_EVENT(4, "slowpath");
626 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
628 spin_lock_irqsave(&slow_subchannel_lock, flags);
629 if (idset_is_empty(slow_subchannel_set)) {
630 atomic_set(&css_eval_scheduled, 0);
631 wake_up(&css_eval_wq);
633 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
636 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
637 struct workqueue_struct *cio_work_q;
639 void css_schedule_eval(struct subchannel_id schid)
643 spin_lock_irqsave(&slow_subchannel_lock, flags);
644 idset_sch_add(slow_subchannel_set, schid);
645 atomic_set(&css_eval_scheduled, 1);
646 queue_delayed_work(cio_work_q, &slow_path_work, 0);
647 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
650 void css_schedule_eval_all(void)
654 spin_lock_irqsave(&slow_subchannel_lock, flags);
655 idset_fill(slow_subchannel_set);
656 atomic_set(&css_eval_scheduled, 1);
657 queue_delayed_work(cio_work_q, &slow_path_work, 0);
658 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
661 static int __unset_registered(struct device *dev, void *data)
663 struct idset *set = data;
664 struct subchannel *sch = to_subchannel(dev);
666 idset_sch_del(set, sch->schid);
670 void css_schedule_eval_all_unreg(unsigned long delay)
673 struct idset *unreg_set;
675 /* Find unregistered subchannels. */
676 unreg_set = idset_sch_new();
679 css_schedule_eval_all();
682 idset_fill(unreg_set);
683 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
684 /* Apply to slow_subchannel_set. */
685 spin_lock_irqsave(&slow_subchannel_lock, flags);
686 idset_add_set(slow_subchannel_set, unreg_set);
687 atomic_set(&css_eval_scheduled, 1);
688 queue_delayed_work(cio_work_q, &slow_path_work, delay);
689 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
690 idset_free(unreg_set);
693 void css_wait_for_slow_path(void)
695 flush_workqueue(cio_work_q);
698 /* Schedule reprobing of all unregistered subchannels. */
699 void css_schedule_reprobe(void)
701 /* Schedule with a delay to allow merging of subsequent calls. */
702 css_schedule_eval_all_unreg(1 * HZ);
704 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
707 * Called from the machine check handler for subchannel report words.
709 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
711 struct subchannel_id mchk_schid;
712 struct subchannel *sch;
715 css_schedule_eval_all();
718 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
719 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
720 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
721 crw0->erc, crw0->rsid);
723 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
724 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
725 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
726 crw1->anc, crw1->erc, crw1->rsid);
727 init_subchannel_id(&mchk_schid);
728 mchk_schid.sch_no = crw0->rsid;
730 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
732 if (crw0->erc == CRW_ERC_PMOD) {
733 sch = get_subchannel_by_schid(mchk_schid);
735 css_update_ssd_info(sch);
736 put_device(&sch->dev);
740 * Since we are always presented with IPI in the CRW, we have to
741 * use stsch() to find out if the subchannel in question has come
744 css_evaluate_subchannel(mchk_schid, 0);
748 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
752 if (css_general_characteristics.mcss) {
753 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
754 css->global_pgid.pgid_high.ext_cssid.cssid =
755 (css->cssid < 0) ? 0 : css->cssid;
757 css->global_pgid.pgid_high.cpu_addr = stap();
760 css->global_pgid.cpu_id = cpu_id.ident;
761 css->global_pgid.cpu_model = cpu_id.machine;
762 css->global_pgid.tod_high = tod_high;
765 static void channel_subsystem_release(struct device *dev)
767 struct channel_subsystem *css = to_css(dev);
769 mutex_destroy(&css->mutex);
773 static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
776 struct channel_subsystem *css = to_css(dev);
781 return sprintf(buf, "%x\n", css->cssid);
783 static DEVICE_ATTR_RO(real_cssid);
785 static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
788 struct channel_subsystem *css = to_css(dev);
791 mutex_lock(&css->mutex);
792 ret = sprintf(buf, "%x\n", css->cm_enabled);
793 mutex_unlock(&css->mutex);
797 static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
798 const char *buf, size_t count)
800 struct channel_subsystem *css = to_css(dev);
804 ret = kstrtoul(buf, 16, &val);
807 mutex_lock(&css->mutex);
810 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
813 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
818 mutex_unlock(&css->mutex);
819 return ret < 0 ? ret : count;
821 static DEVICE_ATTR_RW(cm_enable);
823 static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
826 return css_chsc_characteristics.secm ? attr->mode : 0;
829 static struct attribute *cssdev_attrs[] = {
830 &dev_attr_real_cssid.attr,
834 static struct attribute_group cssdev_attr_group = {
835 .attrs = cssdev_attrs,
838 static struct attribute *cssdev_cm_attrs[] = {
839 &dev_attr_cm_enable.attr,
843 static struct attribute_group cssdev_cm_attr_group = {
844 .attrs = cssdev_cm_attrs,
845 .is_visible = cm_enable_mode,
848 static const struct attribute_group *cssdev_attr_groups[] = {
850 &cssdev_cm_attr_group,
854 static int __init setup_css(int nr)
856 struct channel_subsystem *css;
859 css = kzalloc(sizeof(*css), GFP_KERNEL);
863 channel_subsystems[nr] = css;
864 dev_set_name(&css->device, "css%x", nr);
865 css->device.groups = cssdev_attr_groups;
866 css->device.release = channel_subsystem_release;
868 mutex_init(&css->mutex);
869 css->cssid = chsc_get_cssid(nr);
870 css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
872 ret = device_register(&css->device);
874 put_device(&css->device);
878 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel),
880 if (!css->pseudo_subchannel) {
881 device_unregister(&css->device);
886 css->pseudo_subchannel->dev.parent = &css->device;
887 css->pseudo_subchannel->dev.release = css_subchannel_release;
888 mutex_init(&css->pseudo_subchannel->reg_mutex);
889 ret = css_sch_create_locks(css->pseudo_subchannel);
891 kfree(css->pseudo_subchannel);
892 device_unregister(&css->device);
896 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
897 ret = device_register(&css->pseudo_subchannel->dev);
899 put_device(&css->pseudo_subchannel->dev);
900 device_unregister(&css->device);
906 channel_subsystems[nr] = NULL;
910 static int css_reboot_event(struct notifier_block *this,
914 struct channel_subsystem *css;
919 mutex_lock(&css->mutex);
921 if (chsc_secm(css, 0))
923 mutex_unlock(&css->mutex);
929 static struct notifier_block css_reboot_notifier = {
930 .notifier_call = css_reboot_event,
934 * Since the css devices are neither on a bus nor have a class
935 * nor have a special device type, we cannot stop/restart channel
936 * path measurements via the normal suspend/resume callbacks, but have
939 static int css_power_event(struct notifier_block *this, unsigned long event,
942 struct channel_subsystem *css;
946 case PM_HIBERNATION_PREPARE:
947 case PM_SUSPEND_PREPARE:
950 mutex_lock(&css->mutex);
951 if (!css->cm_enabled) {
952 mutex_unlock(&css->mutex);
955 ret = __chsc_do_secm(css, 0);
956 ret = notifier_from_errno(ret);
957 mutex_unlock(&css->mutex);
960 case PM_POST_HIBERNATION:
961 case PM_POST_SUSPEND:
964 mutex_lock(&css->mutex);
965 if (!css->cm_enabled) {
966 mutex_unlock(&css->mutex);
969 ret = __chsc_do_secm(css, 1);
970 ret = notifier_from_errno(ret);
971 mutex_unlock(&css->mutex);
973 /* search for subchannels, which appeared during hibernation */
974 css_schedule_reprobe();
982 static struct notifier_block css_power_notifier = {
983 .notifier_call = css_power_event,
987 * Now that the driver core is running, we can setup our channel subsystem.
988 * The struct subchannel's are created during probing.
990 static int __init css_bus_init(void)
998 chsc_determine_css_characteristics();
999 /* Try to enable MSS. */
1000 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
1004 max_ssid = __MAX_SSID;
1006 ret = slow_subchannel_init();
1010 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
1014 if ((ret = bus_register(&css_bus_type)))
1017 /* Setup css structure. */
1018 for (i = 0; i <= MAX_CSS_IDX; i++) {
1021 goto out_unregister;
1023 ret = register_reboot_notifier(&css_reboot_notifier);
1025 goto out_unregister;
1026 ret = register_pm_notifier(&css_power_notifier);
1028 unregister_reboot_notifier(&css_reboot_notifier);
1029 goto out_unregister;
1033 /* Enable default isc for I/O subchannels. */
1034 isc_register(IO_SCH_ISC);
1039 struct channel_subsystem *css = channel_subsystems[i];
1040 device_unregister(&css->pseudo_subchannel->dev);
1041 device_unregister(&css->device);
1043 bus_unregister(&css_bus_type);
1045 crw_unregister_handler(CRW_RSC_SCH);
1046 idset_free(slow_subchannel_set);
1047 chsc_init_cleanup();
1048 pr_alert("The CSS device driver initialization failed with "
1053 static void __init css_bus_cleanup(void)
1055 struct channel_subsystem *css;
1058 device_unregister(&css->pseudo_subchannel->dev);
1059 device_unregister(&css->device);
1061 bus_unregister(&css_bus_type);
1062 crw_unregister_handler(CRW_RSC_SCH);
1063 idset_free(slow_subchannel_set);
1064 chsc_init_cleanup();
1065 isc_unregister(IO_SCH_ISC);
1068 static int __init channel_subsystem_init(void)
1072 ret = css_bus_init();
1075 cio_work_q = create_singlethread_workqueue("cio");
1080 ret = io_subchannel_init();
1086 destroy_workqueue(cio_work_q);
1091 subsys_initcall(channel_subsystem_init);
1093 static int css_settle(struct device_driver *drv, void *unused)
1095 struct css_driver *cssdrv = to_cssdriver(drv);
1098 return cssdrv->settle();
1102 int css_complete_work(void)
1106 /* Wait for the evaluation of subchannels to finish. */
1107 ret = wait_event_interruptible(css_eval_wq,
1108 atomic_read(&css_eval_scheduled) == 0);
1111 flush_workqueue(cio_work_q);
1112 /* Wait for the subchannel type specific initialization to finish */
1113 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1118 * Wait for the initialization of devices to finish, to make sure we are
1119 * done with our setup if the search for the root device starts.
1121 static int __init channel_subsystem_init_sync(void)
1123 /* Register subchannels which are already in use. */
1124 cio_register_early_subchannels();
1125 /* Start initial subchannel evaluation. */
1126 css_schedule_eval_all();
1127 css_complete_work();
1130 subsys_initcall_sync(channel_subsystem_init_sync);
1132 void channel_subsystem_reinit(void)
1134 struct channel_path *chp;
1135 struct chp_id chpid;
1137 chsc_enable_facility(CHSC_SDA_OC_MSS);
1138 chp_id_for_each(&chpid) {
1139 chp = chpid_to_chp(chpid);
1141 chp_update_desc(chp);
1146 #ifdef CONFIG_PROC_FS
1147 static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1148 size_t count, loff_t *ppos)
1152 /* Handle pending CRW's. */
1153 crw_wait_for_channel_report();
1154 ret = css_complete_work();
1156 return ret ? ret : count;
1159 static const struct file_operations cio_settle_proc_fops = {
1160 .open = nonseekable_open,
1161 .write = cio_settle_write,
1162 .llseek = no_llseek,
1165 static int __init cio_settle_init(void)
1167 struct proc_dir_entry *entry;
1169 entry = proc_create("cio_settle", S_IWUSR, NULL,
1170 &cio_settle_proc_fops);
1175 device_initcall(cio_settle_init);
1176 #endif /*CONFIG_PROC_FS*/
1178 int sch_is_pseudo_sch(struct subchannel *sch)
1180 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1183 static int css_bus_match(struct device *dev, struct device_driver *drv)
1185 struct subchannel *sch = to_subchannel(dev);
1186 struct css_driver *driver = to_cssdriver(drv);
1187 struct css_device_id *id;
1189 for (id = driver->subchannel_type; id->match_flags; id++) {
1190 if (sch->st == id->type)
1197 static int css_probe(struct device *dev)
1199 struct subchannel *sch;
1202 sch = to_subchannel(dev);
1203 sch->driver = to_cssdriver(dev->driver);
1204 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1210 static int css_remove(struct device *dev)
1212 struct subchannel *sch;
1215 sch = to_subchannel(dev);
1216 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1221 static void css_shutdown(struct device *dev)
1223 struct subchannel *sch;
1225 sch = to_subchannel(dev);
1226 if (sch->driver && sch->driver->shutdown)
1227 sch->driver->shutdown(sch);
1230 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1232 struct subchannel *sch = to_subchannel(dev);
1235 ret = add_uevent_var(env, "ST=%01X", sch->st);
1238 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1242 static int css_pm_prepare(struct device *dev)
1244 struct subchannel *sch = to_subchannel(dev);
1245 struct css_driver *drv;
1247 if (mutex_is_locked(&sch->reg_mutex))
1249 if (!sch->dev.driver)
1251 drv = to_cssdriver(sch->dev.driver);
1252 /* Notify drivers that they may not register children. */
1253 return drv->prepare ? drv->prepare(sch) : 0;
1256 static void css_pm_complete(struct device *dev)
1258 struct subchannel *sch = to_subchannel(dev);
1259 struct css_driver *drv;
1261 if (!sch->dev.driver)
1263 drv = to_cssdriver(sch->dev.driver);
1268 static int css_pm_freeze(struct device *dev)
1270 struct subchannel *sch = to_subchannel(dev);
1271 struct css_driver *drv;
1273 if (!sch->dev.driver)
1275 drv = to_cssdriver(sch->dev.driver);
1276 return drv->freeze ? drv->freeze(sch) : 0;
1279 static int css_pm_thaw(struct device *dev)
1281 struct subchannel *sch = to_subchannel(dev);
1282 struct css_driver *drv;
1284 if (!sch->dev.driver)
1286 drv = to_cssdriver(sch->dev.driver);
1287 return drv->thaw ? drv->thaw(sch) : 0;
1290 static int css_pm_restore(struct device *dev)
1292 struct subchannel *sch = to_subchannel(dev);
1293 struct css_driver *drv;
1295 css_update_ssd_info(sch);
1296 if (!sch->dev.driver)
1298 drv = to_cssdriver(sch->dev.driver);
1299 return drv->restore ? drv->restore(sch) : 0;
1302 static const struct dev_pm_ops css_pm_ops = {
1303 .prepare = css_pm_prepare,
1304 .complete = css_pm_complete,
1305 .freeze = css_pm_freeze,
1306 .thaw = css_pm_thaw,
1307 .restore = css_pm_restore,
1310 static struct bus_type css_bus_type = {
1312 .match = css_bus_match,
1314 .remove = css_remove,
1315 .shutdown = css_shutdown,
1316 .uevent = css_uevent,
1321 * css_driver_register - register a css driver
1322 * @cdrv: css driver to register
1324 * This is mainly a wrapper around driver_register that sets name
1325 * and bus_type in the embedded struct device_driver correctly.
1327 int css_driver_register(struct css_driver *cdrv)
1329 cdrv->drv.bus = &css_bus_type;
1330 return driver_register(&cdrv->drv);
1332 EXPORT_SYMBOL_GPL(css_driver_register);
1335 * css_driver_unregister - unregister a css driver
1336 * @cdrv: css driver to unregister
1338 * This is a wrapper around driver_unregister.
1340 void css_driver_unregister(struct css_driver *cdrv)
1342 driver_unregister(&cdrv->drv);
1344 EXPORT_SYMBOL_GPL(css_driver_unregister);