1 // SPDX-License-Identifier: GPL-2.0
3 * driver for channel subsystem
5 * Copyright IBM Corp. 2002, 2010
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>
23 #include <linux/genalloc.h>
24 #include <linux/dma-mapping.h>
30 #include "blacklist.h"
31 #include "cio_debug.h"
38 int css_init_done = 0;
42 struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1];
43 static struct bus_type css_bus_type;
46 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
48 struct subchannel_id schid;
51 init_subchannel_id(&schid);
54 ret = fn(schid, data);
57 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
59 } while (schid.ssid++ < max_ssid);
66 int (*fn_known_sch)(struct subchannel *, void *);
67 int (*fn_unknown_sch)(struct subchannel_id, void *);
70 static int call_fn_known_sch(struct device *dev, void *data)
72 struct subchannel *sch = to_subchannel(dev);
73 struct cb_data *cb = data;
77 idset_sch_del(cb->set, sch->schid);
79 rc = cb->fn_known_sch(sch, cb->data);
83 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
85 struct cb_data *cb = data;
88 if (idset_sch_contains(cb->set, schid))
89 rc = cb->fn_unknown_sch(schid, cb->data);
93 static int call_fn_all_sch(struct subchannel_id schid, void *data)
95 struct cb_data *cb = data;
96 struct subchannel *sch;
99 sch = get_subchannel_by_schid(schid);
101 if (cb->fn_known_sch)
102 rc = cb->fn_known_sch(sch, cb->data);
103 put_device(&sch->dev);
105 if (cb->fn_unknown_sch)
106 rc = cb->fn_unknown_sch(schid, cb->data);
112 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
113 int (*fn_unknown)(struct subchannel_id,
120 cb.fn_known_sch = fn_known;
121 cb.fn_unknown_sch = fn_unknown;
123 if (fn_known && !fn_unknown) {
124 /* Skip idset allocation in case of known-only loop. */
126 return bus_for_each_dev(&css_bus_type, NULL, &cb,
130 cb.set = idset_sch_new();
132 /* fall back to brute force scanning in case of oom */
133 return for_each_subchannel(call_fn_all_sch, &cb);
137 /* Process registered subchannels. */
138 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
141 /* Process unregistered subchannels. */
143 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
150 static void css_sch_todo(struct work_struct *work);
152 static int css_sch_create_locks(struct subchannel *sch)
154 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
158 spin_lock_init(sch->lock);
159 mutex_init(&sch->reg_mutex);
164 static void css_subchannel_release(struct device *dev)
166 struct subchannel *sch = to_subchannel(dev);
168 sch->config.intparm = 0;
169 cio_commit_config(sch);
170 kfree(sch->driver_override);
175 static int css_validate_subchannel(struct subchannel_id schid,
180 switch (schib->pmcw.st) {
181 case SUBCHANNEL_TYPE_IO:
182 case SUBCHANNEL_TYPE_MSG:
183 if (!css_sch_is_valid(schib))
185 else if (is_blacklisted(schid.ssid, schib->pmcw.dev)) {
186 CIO_MSG_EVENT(6, "Blacklisted device detected "
187 "at devno %04X, subchannel set %x\n",
188 schib->pmcw.dev, schid.ssid);
199 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
200 schid.ssid, schid.sch_no, schib->pmcw.st);
205 struct subchannel *css_alloc_subchannel(struct subchannel_id schid,
208 struct subchannel *sch;
211 ret = css_validate_subchannel(schid, schib);
215 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
217 return ERR_PTR(-ENOMEM);
221 sch->st = schib->pmcw.st;
223 ret = css_sch_create_locks(sch);
227 INIT_WORK(&sch->todo_work, css_sch_todo);
228 sch->dev.release = &css_subchannel_release;
229 device_initialize(&sch->dev);
231 * The physical addresses of some the dma structures that can
232 * belong to a subchannel need to fit 31 bit width (e.g. ccw).
234 sch->dev.coherent_dma_mask = DMA_BIT_MASK(31);
236 * But we don't have such restrictions imposed on the stuff that
237 * is handled by the streaming API.
239 sch->dma_mask = DMA_BIT_MASK(64);
240 sch->dev.dma_mask = &sch->dma_mask;
248 static int css_sch_device_register(struct subchannel *sch)
252 mutex_lock(&sch->reg_mutex);
253 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
255 ret = device_add(&sch->dev);
256 mutex_unlock(&sch->reg_mutex);
261 * css_sch_device_unregister - unregister a subchannel
262 * @sch: subchannel to be unregistered
264 void css_sch_device_unregister(struct subchannel *sch)
266 mutex_lock(&sch->reg_mutex);
267 if (device_is_registered(&sch->dev))
268 device_unregister(&sch->dev);
269 mutex_unlock(&sch->reg_mutex);
271 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
273 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
278 memset(ssd, 0, sizeof(struct chsc_ssd_info));
279 ssd->path_mask = pmcw->pim;
280 for (i = 0; i < 8; i++) {
282 if (pmcw->pim & mask) {
283 chp_id_init(&ssd->chpid[i]);
284 ssd->chpid[i].id = pmcw->chpid[i];
289 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
294 for (i = 0; i < 8; i++) {
296 if (ssd->path_mask & mask)
297 chp_new(ssd->chpid[i]);
301 void css_update_ssd_info(struct subchannel *sch)
305 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
307 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
309 ssd_register_chpids(&sch->ssd_info);
312 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
315 struct subchannel *sch = to_subchannel(dev);
317 return sprintf(buf, "%01x\n", sch->st);
320 static DEVICE_ATTR_RO(type);
322 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
325 struct subchannel *sch = to_subchannel(dev);
327 return sprintf(buf, "css:t%01X\n", sch->st);
330 static DEVICE_ATTR_RO(modalias);
332 static ssize_t driver_override_store(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf, size_t count)
336 struct subchannel *sch = to_subchannel(dev);
337 char *driver_override, *old, *cp;
339 /* We need to keep extra room for a newline */
340 if (count >= (PAGE_SIZE - 1))
343 driver_override = kstrndup(buf, count, GFP_KERNEL);
344 if (!driver_override)
347 cp = strchr(driver_override, '\n');
352 old = sch->driver_override;
353 if (strlen(driver_override)) {
354 sch->driver_override = driver_override;
356 kfree(driver_override);
357 sch->driver_override = NULL;
366 static ssize_t driver_override_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 struct subchannel *sch = to_subchannel(dev);
373 len = snprintf(buf, PAGE_SIZE, "%s\n", sch->driver_override);
377 static DEVICE_ATTR_RW(driver_override);
379 static struct attribute *subch_attrs[] = {
381 &dev_attr_modalias.attr,
382 &dev_attr_driver_override.attr,
386 static struct attribute_group subch_attr_group = {
387 .attrs = subch_attrs,
390 static const struct attribute_group *default_subch_attr_groups[] = {
395 static ssize_t chpids_show(struct device *dev,
396 struct device_attribute *attr,
399 struct subchannel *sch = to_subchannel(dev);
400 struct chsc_ssd_info *ssd = &sch->ssd_info;
405 for (chp = 0; chp < 8; chp++) {
407 if (ssd->path_mask & mask)
408 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
410 ret += sprintf(buf + ret, "00 ");
412 ret += sprintf(buf + ret, "\n");
415 static DEVICE_ATTR_RO(chpids);
417 static ssize_t pimpampom_show(struct device *dev,
418 struct device_attribute *attr,
421 struct subchannel *sch = to_subchannel(dev);
422 struct pmcw *pmcw = &sch->schib.pmcw;
424 return sprintf(buf, "%02x %02x %02x\n",
425 pmcw->pim, pmcw->pam, pmcw->pom);
427 static DEVICE_ATTR_RO(pimpampom);
429 static struct attribute *io_subchannel_type_attrs[] = {
430 &dev_attr_chpids.attr,
431 &dev_attr_pimpampom.attr,
434 ATTRIBUTE_GROUPS(io_subchannel_type);
436 static const struct device_type io_subchannel_type = {
437 .groups = io_subchannel_type_groups,
440 int css_register_subchannel(struct subchannel *sch)
444 /* Initialize the subchannel structure */
445 sch->dev.parent = &channel_subsystems[0]->device;
446 sch->dev.bus = &css_bus_type;
447 sch->dev.groups = default_subch_attr_groups;
449 if (sch->st == SUBCHANNEL_TYPE_IO)
450 sch->dev.type = &io_subchannel_type;
453 * We don't want to generate uevents for I/O subchannels that don't
454 * have a working ccw device behind them since they will be
455 * unregistered before they can be used anyway, so we delay the add
456 * uevent until after device recognition was successful.
457 * Note that we suppress the uevent for all subchannel types;
458 * the subchannel driver can decide itself when it wants to inform
459 * userspace of its existence.
461 dev_set_uevent_suppress(&sch->dev, 1);
462 css_update_ssd_info(sch);
463 /* make it known to the system */
464 ret = css_sch_device_register(sch);
466 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
467 sch->schid.ssid, sch->schid.sch_no, ret);
472 * No driver matched. Generate the uevent now so that
473 * a fitting driver module may be loaded based on the
476 dev_set_uevent_suppress(&sch->dev, 0);
477 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
482 static int css_probe_device(struct subchannel_id schid, struct schib *schib)
484 struct subchannel *sch;
487 sch = css_alloc_subchannel(schid, schib);
491 ret = css_register_subchannel(sch);
493 put_device(&sch->dev);
499 check_subchannel(struct device *dev, const void *data)
501 struct subchannel *sch;
502 struct subchannel_id *schid = (void *)data;
504 sch = to_subchannel(dev);
505 return schid_equal(&sch->schid, schid);
509 get_subchannel_by_schid(struct subchannel_id schid)
513 dev = bus_find_device(&css_bus_type, NULL,
514 &schid, check_subchannel);
516 return dev ? to_subchannel(dev) : NULL;
520 * css_sch_is_valid() - check if a subchannel is valid
521 * @schib: subchannel information block for the subchannel
523 int css_sch_is_valid(struct schib *schib)
525 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
527 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
531 EXPORT_SYMBOL_GPL(css_sch_is_valid);
533 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
539 /* Will be done on the slow path. */
543 * The first subchannel that is not-operational (ccode==3)
544 * indicates that there aren't any more devices available.
545 * If stsch gets an exception, it means the current subchannel set
548 ccode = stsch(schid, &schib);
550 return (ccode == 3) ? -ENXIO : ccode;
552 return css_probe_device(schid, &schib);
555 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
560 if (sch->driver->sch_event)
561 ret = sch->driver->sch_event(sch, slow);
564 "Got subchannel machine check but "
565 "no sch_event handler provided.\n");
567 if (ret != 0 && ret != -EAGAIN) {
568 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
569 sch->schid.ssid, sch->schid.sch_no, ret);
574 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
576 struct subchannel *sch;
579 sch = get_subchannel_by_schid(schid);
581 ret = css_evaluate_known_subchannel(sch, slow);
582 put_device(&sch->dev);
584 ret = css_evaluate_new_subchannel(schid, slow);
586 css_schedule_eval(schid);
590 * css_sched_sch_todo - schedule a subchannel operation
594 * Schedule the operation identified by @todo to be performed on the slow path
595 * workqueue. Do nothing if another operation with higher priority is already
596 * scheduled. Needs to be called with subchannel lock held.
598 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
600 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
601 sch->schid.ssid, sch->schid.sch_no, todo);
602 if (sch->todo >= todo)
604 /* Get workqueue ref. */
605 if (!get_device(&sch->dev))
608 if (!queue_work(cio_work_q, &sch->todo_work)) {
609 /* Already queued, release workqueue ref. */
610 put_device(&sch->dev);
613 EXPORT_SYMBOL_GPL(css_sched_sch_todo);
615 static void css_sch_todo(struct work_struct *work)
617 struct subchannel *sch;
621 sch = container_of(work, struct subchannel, todo_work);
623 spin_lock_irq(sch->lock);
625 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
626 sch->schid.sch_no, todo);
627 sch->todo = SCH_TODO_NOTHING;
628 spin_unlock_irq(sch->lock);
631 case SCH_TODO_NOTHING:
634 ret = css_evaluate_known_subchannel(sch, 1);
635 if (ret == -EAGAIN) {
636 spin_lock_irq(sch->lock);
637 css_sched_sch_todo(sch, todo);
638 spin_unlock_irq(sch->lock);
642 css_sch_device_unregister(sch);
645 /* Release workqueue ref. */
646 put_device(&sch->dev);
649 static struct idset *slow_subchannel_set;
650 static spinlock_t slow_subchannel_lock;
651 static wait_queue_head_t css_eval_wq;
652 static atomic_t css_eval_scheduled;
654 static int __init slow_subchannel_init(void)
656 spin_lock_init(&slow_subchannel_lock);
657 atomic_set(&css_eval_scheduled, 0);
658 init_waitqueue_head(&css_eval_wq);
659 slow_subchannel_set = idset_sch_new();
660 if (!slow_subchannel_set) {
661 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
667 static int slow_eval_known_fn(struct subchannel *sch, void *data)
672 spin_lock_irq(&slow_subchannel_lock);
673 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
674 idset_sch_del(slow_subchannel_set, sch->schid);
675 spin_unlock_irq(&slow_subchannel_lock);
677 rc = css_evaluate_known_subchannel(sch, 1);
679 css_schedule_eval(sch->schid);
684 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
689 spin_lock_irq(&slow_subchannel_lock);
690 eval = idset_sch_contains(slow_subchannel_set, schid);
691 idset_sch_del(slow_subchannel_set, schid);
692 spin_unlock_irq(&slow_subchannel_lock);
694 rc = css_evaluate_new_subchannel(schid, 1);
697 css_schedule_eval(schid);
703 /* These should abort looping */
704 spin_lock_irq(&slow_subchannel_lock);
705 idset_sch_del_subseq(slow_subchannel_set, schid);
706 spin_unlock_irq(&slow_subchannel_lock);
711 /* Allow scheduling here since the containing loop might
718 static void css_slow_path_func(struct work_struct *unused)
722 CIO_TRACE_EVENT(4, "slowpath");
723 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
725 spin_lock_irqsave(&slow_subchannel_lock, flags);
726 if (idset_is_empty(slow_subchannel_set)) {
727 atomic_set(&css_eval_scheduled, 0);
728 wake_up(&css_eval_wq);
730 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
733 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
734 struct workqueue_struct *cio_work_q;
736 void css_schedule_eval(struct subchannel_id schid)
740 spin_lock_irqsave(&slow_subchannel_lock, flags);
741 idset_sch_add(slow_subchannel_set, schid);
742 atomic_set(&css_eval_scheduled, 1);
743 queue_delayed_work(cio_work_q, &slow_path_work, 0);
744 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
747 void css_schedule_eval_all(void)
751 spin_lock_irqsave(&slow_subchannel_lock, flags);
752 idset_fill(slow_subchannel_set);
753 atomic_set(&css_eval_scheduled, 1);
754 queue_delayed_work(cio_work_q, &slow_path_work, 0);
755 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
758 static int __unset_registered(struct device *dev, void *data)
760 struct idset *set = data;
761 struct subchannel *sch = to_subchannel(dev);
763 idset_sch_del(set, sch->schid);
767 void css_schedule_eval_all_unreg(unsigned long delay)
770 struct idset *unreg_set;
772 /* Find unregistered subchannels. */
773 unreg_set = idset_sch_new();
776 css_schedule_eval_all();
779 idset_fill(unreg_set);
780 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
781 /* Apply to slow_subchannel_set. */
782 spin_lock_irqsave(&slow_subchannel_lock, flags);
783 idset_add_set(slow_subchannel_set, unreg_set);
784 atomic_set(&css_eval_scheduled, 1);
785 queue_delayed_work(cio_work_q, &slow_path_work, delay);
786 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
787 idset_free(unreg_set);
790 void css_wait_for_slow_path(void)
792 flush_workqueue(cio_work_q);
795 /* Schedule reprobing of all unregistered subchannels. */
796 void css_schedule_reprobe(void)
798 /* Schedule with a delay to allow merging of subsequent calls. */
799 css_schedule_eval_all_unreg(1 * HZ);
801 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
804 * Called from the machine check handler for subchannel report words.
806 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
808 struct subchannel_id mchk_schid;
809 struct subchannel *sch;
812 css_schedule_eval_all();
815 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
816 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
817 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
818 crw0->erc, crw0->rsid);
820 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
821 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
822 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
823 crw1->anc, crw1->erc, crw1->rsid);
824 init_subchannel_id(&mchk_schid);
825 mchk_schid.sch_no = crw0->rsid;
827 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
829 if (crw0->erc == CRW_ERC_PMOD) {
830 sch = get_subchannel_by_schid(mchk_schid);
832 css_update_ssd_info(sch);
833 put_device(&sch->dev);
837 * Since we are always presented with IPI in the CRW, we have to
838 * use stsch() to find out if the subchannel in question has come
841 css_evaluate_subchannel(mchk_schid, 0);
845 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
849 if (css_general_characteristics.mcss) {
850 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
851 css->global_pgid.pgid_high.ext_cssid.cssid =
852 (css->cssid < 0) ? 0 : css->cssid;
854 css->global_pgid.pgid_high.cpu_addr = stap();
857 css->global_pgid.cpu_id = cpu_id.ident;
858 css->global_pgid.cpu_model = cpu_id.machine;
859 css->global_pgid.tod_high = tod_high;
862 static void channel_subsystem_release(struct device *dev)
864 struct channel_subsystem *css = to_css(dev);
866 mutex_destroy(&css->mutex);
870 static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
873 struct channel_subsystem *css = to_css(dev);
878 return sprintf(buf, "%x\n", css->cssid);
880 static DEVICE_ATTR_RO(real_cssid);
882 static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
885 struct channel_subsystem *css = to_css(dev);
888 mutex_lock(&css->mutex);
889 ret = sprintf(buf, "%x\n", css->cm_enabled);
890 mutex_unlock(&css->mutex);
894 static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
895 const char *buf, size_t count)
897 struct channel_subsystem *css = to_css(dev);
901 ret = kstrtoul(buf, 16, &val);
904 mutex_lock(&css->mutex);
907 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
910 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
915 mutex_unlock(&css->mutex);
916 return ret < 0 ? ret : count;
918 static DEVICE_ATTR_RW(cm_enable);
920 static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
923 return css_chsc_characteristics.secm ? attr->mode : 0;
926 static struct attribute *cssdev_attrs[] = {
927 &dev_attr_real_cssid.attr,
931 static struct attribute_group cssdev_attr_group = {
932 .attrs = cssdev_attrs,
935 static struct attribute *cssdev_cm_attrs[] = {
936 &dev_attr_cm_enable.attr,
940 static struct attribute_group cssdev_cm_attr_group = {
941 .attrs = cssdev_cm_attrs,
942 .is_visible = cm_enable_mode,
945 static const struct attribute_group *cssdev_attr_groups[] = {
947 &cssdev_cm_attr_group,
951 static int __init setup_css(int nr)
953 struct channel_subsystem *css;
956 css = kzalloc(sizeof(*css), GFP_KERNEL);
960 channel_subsystems[nr] = css;
961 dev_set_name(&css->device, "css%x", nr);
962 css->device.groups = cssdev_attr_groups;
963 css->device.release = channel_subsystem_release;
965 * We currently allocate notifier bits with this (using
966 * css->device as the device argument with the DMA API)
967 * and are fine with 64 bit addresses.
969 css->device.coherent_dma_mask = DMA_BIT_MASK(64);
970 css->device.dma_mask = &css->device.coherent_dma_mask;
972 mutex_init(&css->mutex);
973 css->cssid = chsc_get_cssid(nr);
974 css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
976 ret = device_register(&css->device);
978 put_device(&css->device);
982 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel),
984 if (!css->pseudo_subchannel) {
985 device_unregister(&css->device);
990 css->pseudo_subchannel->dev.parent = &css->device;
991 css->pseudo_subchannel->dev.release = css_subchannel_release;
992 mutex_init(&css->pseudo_subchannel->reg_mutex);
993 ret = css_sch_create_locks(css->pseudo_subchannel);
995 kfree(css->pseudo_subchannel);
996 device_unregister(&css->device);
1000 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
1001 ret = device_register(&css->pseudo_subchannel->dev);
1003 put_device(&css->pseudo_subchannel->dev);
1004 device_unregister(&css->device);
1010 channel_subsystems[nr] = NULL;
1014 static int css_reboot_event(struct notifier_block *this,
1015 unsigned long event,
1018 struct channel_subsystem *css;
1023 mutex_lock(&css->mutex);
1024 if (css->cm_enabled)
1025 if (chsc_secm(css, 0))
1027 mutex_unlock(&css->mutex);
1033 static struct notifier_block css_reboot_notifier = {
1034 .notifier_call = css_reboot_event,
1038 * Since the css devices are neither on a bus nor have a class
1039 * nor have a special device type, we cannot stop/restart channel
1040 * path measurements via the normal suspend/resume callbacks, but have
1043 static int css_power_event(struct notifier_block *this, unsigned long event,
1046 struct channel_subsystem *css;
1050 case PM_HIBERNATION_PREPARE:
1051 case PM_SUSPEND_PREPARE:
1054 mutex_lock(&css->mutex);
1055 if (!css->cm_enabled) {
1056 mutex_unlock(&css->mutex);
1059 ret = __chsc_do_secm(css, 0);
1060 ret = notifier_from_errno(ret);
1061 mutex_unlock(&css->mutex);
1064 case PM_POST_HIBERNATION:
1065 case PM_POST_SUSPEND:
1068 mutex_lock(&css->mutex);
1069 if (!css->cm_enabled) {
1070 mutex_unlock(&css->mutex);
1073 ret = __chsc_do_secm(css, 1);
1074 ret = notifier_from_errno(ret);
1075 mutex_unlock(&css->mutex);
1077 /* search for subchannels, which appeared during hibernation */
1078 css_schedule_reprobe();
1086 static struct notifier_block css_power_notifier = {
1087 .notifier_call = css_power_event,
1090 #define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
1091 static struct gen_pool *cio_dma_pool;
1093 /* Currently cio supports only a single css */
1094 struct device *cio_get_dma_css_dev(void)
1096 return &channel_subsystems[0]->device;
1099 struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
1101 struct gen_pool *gp_dma;
1103 dma_addr_t dma_addr;
1106 gp_dma = gen_pool_create(3, -1);
1109 for (i = 0; i < nr_pages; ++i) {
1110 cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
1114 gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
1115 dma_addr, PAGE_SIZE, -1);
1120 static void __gp_dma_free_dma(struct gen_pool *pool,
1121 struct gen_pool_chunk *chunk, void *data)
1123 size_t chunk_size = chunk->end_addr - chunk->start_addr + 1;
1125 dma_free_coherent((struct device *) data, chunk_size,
1126 (void *) chunk->start_addr,
1127 (dma_addr_t) chunk->phys_addr);
1130 void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev)
1134 /* this is quite ugly but no better idea */
1135 gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev);
1136 gen_pool_destroy(gp_dma);
1139 static int cio_dma_pool_init(void)
1141 /* No need to free up the resources: compiled in */
1142 cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
1148 void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
1151 dma_addr_t dma_addr;
1157 addr = gen_pool_alloc(gp_dma, size);
1159 chunk_size = round_up(size, PAGE_SIZE);
1160 addr = (unsigned long) dma_alloc_coherent(dma_dev,
1161 chunk_size, &dma_addr, CIO_DMA_GFP);
1164 gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
1165 addr = gen_pool_alloc(gp_dma, size);
1167 return (void *) addr;
1170 void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
1174 memset(cpu_addr, 0, size);
1175 gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
1179 * Allocate dma memory from the css global pool. Intended for memory not
1180 * specific to any single device within the css. The allocated memory
1181 * is not guaranteed to be 31-bit addressable.
1183 * Caution: Not suitable for early stuff like console.
1185 void *cio_dma_zalloc(size_t size)
1187 return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);
1190 void cio_dma_free(void *cpu_addr, size_t size)
1192 cio_gp_dma_free(cio_dma_pool, cpu_addr, size);
1196 * Now that the driver core is running, we can setup our channel subsystem.
1197 * The struct subchannel's are created during probing.
1199 static int __init css_bus_init(void)
1207 chsc_determine_css_characteristics();
1208 /* Try to enable MSS. */
1209 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
1213 max_ssid = __MAX_SSID;
1215 ret = slow_subchannel_init();
1219 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
1223 if ((ret = bus_register(&css_bus_type)))
1226 /* Setup css structure. */
1227 for (i = 0; i <= MAX_CSS_IDX; i++) {
1230 goto out_unregister;
1232 ret = register_reboot_notifier(&css_reboot_notifier);
1234 goto out_unregister;
1235 ret = register_pm_notifier(&css_power_notifier);
1237 goto out_unregister_rn;
1238 ret = cio_dma_pool_init();
1240 goto out_unregister_pmn;
1244 /* Enable default isc for I/O subchannels. */
1245 isc_register(IO_SCH_ISC);
1249 unregister_pm_notifier(&css_power_notifier);
1251 unregister_reboot_notifier(&css_reboot_notifier);
1254 struct channel_subsystem *css = channel_subsystems[i];
1255 device_unregister(&css->pseudo_subchannel->dev);
1256 device_unregister(&css->device);
1258 bus_unregister(&css_bus_type);
1260 crw_unregister_handler(CRW_RSC_SCH);
1261 idset_free(slow_subchannel_set);
1262 chsc_init_cleanup();
1263 pr_alert("The CSS device driver initialization failed with "
1268 static void __init css_bus_cleanup(void)
1270 struct channel_subsystem *css;
1273 device_unregister(&css->pseudo_subchannel->dev);
1274 device_unregister(&css->device);
1276 bus_unregister(&css_bus_type);
1277 crw_unregister_handler(CRW_RSC_SCH);
1278 idset_free(slow_subchannel_set);
1279 chsc_init_cleanup();
1280 isc_unregister(IO_SCH_ISC);
1283 static int __init channel_subsystem_init(void)
1287 ret = css_bus_init();
1290 cio_work_q = create_singlethread_workqueue("cio");
1295 ret = io_subchannel_init();
1299 /* Register subchannels which are already in use. */
1300 cio_register_early_subchannels();
1301 /* Start initial subchannel evaluation. */
1302 css_schedule_eval_all();
1306 destroy_workqueue(cio_work_q);
1311 subsys_initcall(channel_subsystem_init);
1313 static int css_settle(struct device_driver *drv, void *unused)
1315 struct css_driver *cssdrv = to_cssdriver(drv);
1318 return cssdrv->settle();
1322 int css_complete_work(void)
1326 /* Wait for the evaluation of subchannels to finish. */
1327 ret = wait_event_interruptible(css_eval_wq,
1328 atomic_read(&css_eval_scheduled) == 0);
1331 flush_workqueue(cio_work_q);
1332 /* Wait for the subchannel type specific initialization to finish */
1333 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1338 * Wait for the initialization of devices to finish, to make sure we are
1339 * done with our setup if the search for the root device starts.
1341 static int __init channel_subsystem_init_sync(void)
1343 css_complete_work();
1346 subsys_initcall_sync(channel_subsystem_init_sync);
1348 void channel_subsystem_reinit(void)
1350 struct channel_path *chp;
1351 struct chp_id chpid;
1353 chsc_enable_facility(CHSC_SDA_OC_MSS);
1354 chp_id_for_each(&chpid) {
1355 chp = chpid_to_chp(chpid);
1357 chp_update_desc(chp);
1362 #ifdef CONFIG_PROC_FS
1363 static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1364 size_t count, loff_t *ppos)
1368 /* Handle pending CRW's. */
1369 crw_wait_for_channel_report();
1370 ret = css_complete_work();
1372 return ret ? ret : count;
1375 static const struct proc_ops cio_settle_proc_ops = {
1376 .proc_open = nonseekable_open,
1377 .proc_write = cio_settle_write,
1378 .proc_lseek = no_llseek,
1381 static int __init cio_settle_init(void)
1383 struct proc_dir_entry *entry;
1385 entry = proc_create("cio_settle", S_IWUSR, NULL, &cio_settle_proc_ops);
1390 device_initcall(cio_settle_init);
1391 #endif /*CONFIG_PROC_FS*/
1393 int sch_is_pseudo_sch(struct subchannel *sch)
1395 if (!sch->dev.parent)
1397 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1400 static int css_bus_match(struct device *dev, struct device_driver *drv)
1402 struct subchannel *sch = to_subchannel(dev);
1403 struct css_driver *driver = to_cssdriver(drv);
1404 struct css_device_id *id;
1406 /* When driver_override is set, only bind to the matching driver */
1407 if (sch->driver_override && strcmp(sch->driver_override, drv->name))
1410 for (id = driver->subchannel_type; id->match_flags; id++) {
1411 if (sch->st == id->type)
1418 static int css_probe(struct device *dev)
1420 struct subchannel *sch;
1423 sch = to_subchannel(dev);
1424 sch->driver = to_cssdriver(dev->driver);
1425 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1431 static int css_remove(struct device *dev)
1433 struct subchannel *sch;
1436 sch = to_subchannel(dev);
1437 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1442 static void css_shutdown(struct device *dev)
1444 struct subchannel *sch;
1446 sch = to_subchannel(dev);
1447 if (sch->driver && sch->driver->shutdown)
1448 sch->driver->shutdown(sch);
1451 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1453 struct subchannel *sch = to_subchannel(dev);
1456 ret = add_uevent_var(env, "ST=%01X", sch->st);
1459 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1463 static int css_pm_prepare(struct device *dev)
1465 struct subchannel *sch = to_subchannel(dev);
1466 struct css_driver *drv;
1468 if (mutex_is_locked(&sch->reg_mutex))
1470 if (!sch->dev.driver)
1472 drv = to_cssdriver(sch->dev.driver);
1473 /* Notify drivers that they may not register children. */
1474 return drv->prepare ? drv->prepare(sch) : 0;
1477 static void css_pm_complete(struct device *dev)
1479 struct subchannel *sch = to_subchannel(dev);
1480 struct css_driver *drv;
1482 if (!sch->dev.driver)
1484 drv = to_cssdriver(sch->dev.driver);
1489 static int css_pm_freeze(struct device *dev)
1491 struct subchannel *sch = to_subchannel(dev);
1492 struct css_driver *drv;
1494 if (!sch->dev.driver)
1496 drv = to_cssdriver(sch->dev.driver);
1497 return drv->freeze ? drv->freeze(sch) : 0;
1500 static int css_pm_thaw(struct device *dev)
1502 struct subchannel *sch = to_subchannel(dev);
1503 struct css_driver *drv;
1505 if (!sch->dev.driver)
1507 drv = to_cssdriver(sch->dev.driver);
1508 return drv->thaw ? drv->thaw(sch) : 0;
1511 static int css_pm_restore(struct device *dev)
1513 struct subchannel *sch = to_subchannel(dev);
1514 struct css_driver *drv;
1516 css_update_ssd_info(sch);
1517 if (!sch->dev.driver)
1519 drv = to_cssdriver(sch->dev.driver);
1520 return drv->restore ? drv->restore(sch) : 0;
1523 static const struct dev_pm_ops css_pm_ops = {
1524 .prepare = css_pm_prepare,
1525 .complete = css_pm_complete,
1526 .freeze = css_pm_freeze,
1527 .thaw = css_pm_thaw,
1528 .restore = css_pm_restore,
1531 static struct bus_type css_bus_type = {
1533 .match = css_bus_match,
1535 .remove = css_remove,
1536 .shutdown = css_shutdown,
1537 .uevent = css_uevent,
1542 * css_driver_register - register a css driver
1543 * @cdrv: css driver to register
1545 * This is mainly a wrapper around driver_register that sets name
1546 * and bus_type in the embedded struct device_driver correctly.
1548 int css_driver_register(struct css_driver *cdrv)
1550 cdrv->drv.bus = &css_bus_type;
1551 return driver_register(&cdrv->drv);
1553 EXPORT_SYMBOL_GPL(css_driver_register);
1556 * css_driver_unregister - unregister a css driver
1557 * @cdrv: css driver to unregister
1559 * This is a wrapper around driver_unregister.
1561 void css_driver_unregister(struct css_driver *cdrv)
1563 driver_unregister(&cdrv->drv);
1565 EXPORT_SYMBOL_GPL(css_driver_unregister);