2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/list.h>
16 #include <linux/reboot.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);
125 if (sch->st != SUBCHANNEL_TYPE_IO) {
126 /* For now we ignore all non-io subchannels. */
128 return ERR_PTR(-EINVAL);
132 * Set intparm to subchannel address.
133 * This is fine even on 64bit since the subchannel is always located
136 sch->schib.pmcw.intparm = (u32)(addr_t)sch;
137 ret = cio_modify(sch);
147 css_free_subchannel(struct subchannel *sch)
150 /* Reset intparm to zeroes. */
151 sch->schib.pmcw.intparm = 0;
159 css_subchannel_release(struct device *dev)
161 struct subchannel *sch;
163 sch = to_subchannel(dev);
164 if (!cio_is_console(sch->schid)) {
170 static int css_sch_device_register(struct subchannel *sch)
174 mutex_lock(&sch->reg_mutex);
175 ret = device_register(&sch->dev);
176 mutex_unlock(&sch->reg_mutex);
180 void css_sch_device_unregister(struct subchannel *sch)
182 mutex_lock(&sch->reg_mutex);
183 device_unregister(&sch->dev);
184 mutex_unlock(&sch->reg_mutex);
187 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
192 memset(ssd, 0, sizeof(struct chsc_ssd_info));
193 ssd->path_mask = pmcw->pim;
194 for (i = 0; i < 8; i++) {
196 if (pmcw->pim & mask) {
197 chp_id_init(&ssd->chpid[i]);
198 ssd->chpid[i].id = pmcw->chpid[i];
203 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
208 for (i = 0; i < 8; i++) {
210 if (ssd->path_mask & mask)
211 if (!chp_is_registered(ssd->chpid[i]))
212 chp_new(ssd->chpid[i]);
216 void css_update_ssd_info(struct subchannel *sch)
220 if (cio_is_console(sch->schid)) {
221 /* Console is initialized too early for functions requiring
222 * memory allocation. */
223 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
225 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
227 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
228 ssd_register_chpids(&sch->ssd_info);
232 static int css_register_subchannel(struct subchannel *sch)
236 /* Initialize the subchannel structure */
237 sch->dev.parent = &channel_subsystems[0]->device;
238 sch->dev.bus = &css_bus_type;
239 sch->dev.release = &css_subchannel_release;
240 sch->dev.groups = subch_attr_groups;
242 * We don't want to generate uevents for I/O subchannels that don't
243 * have a working ccw device behind them since they will be
244 * unregistered before they can be used anyway, so we delay the add
245 * uevent until after device recognition was successful.
247 if (!cio_is_console(sch->schid))
248 /* Console is special, no need to suppress. */
249 sch->dev.uevent_suppress = 1;
250 css_update_ssd_info(sch);
251 /* make it known to the system */
252 ret = css_sch_device_register(sch);
254 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
255 sch->schid.ssid, sch->schid.sch_no, ret);
261 static int css_probe_device(struct subchannel_id schid)
264 struct subchannel *sch;
266 sch = css_alloc_subchannel(schid);
269 ret = css_register_subchannel(sch);
271 css_free_subchannel(sch);
276 check_subchannel(struct device * dev, void * data)
278 struct subchannel *sch;
279 struct subchannel_id *schid = data;
281 sch = to_subchannel(dev);
282 return schid_equal(&sch->schid, schid);
286 get_subchannel_by_schid(struct subchannel_id schid)
290 dev = bus_find_device(&css_bus_type, NULL,
291 &schid, check_subchannel);
293 return dev ? to_subchannel(dev) : NULL;
297 * css_sch_is_valid() - check if a subchannel is valid
298 * @schib: subchannel information block for the subchannel
300 int css_sch_is_valid(struct schib *schib)
302 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
306 EXPORT_SYMBOL_GPL(css_sch_is_valid);
308 static int css_get_subchannel_status(struct subchannel *sch)
312 if (stsch(sch->schid, &schib))
314 if (!css_sch_is_valid(&schib))
316 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
317 return CIO_REVALIDATE;
323 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
325 int event, ret, disc;
327 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
329 spin_lock_irqsave(sch->lock, flags);
330 disc = device_is_disconnected(sch);
332 /* Disconnected devices are evaluated directly only.*/
333 spin_unlock_irqrestore(sch->lock, flags);
336 /* No interrupt after machine check - kill pending timers. */
337 device_kill_pending_timer(sch);
338 if (!disc && !slow) {
339 /* Non-disconnected devices are evaluated on the slow path. */
340 spin_unlock_irqrestore(sch->lock, flags);
343 event = css_get_subchannel_status(sch);
344 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
345 sch->schid.ssid, sch->schid.sch_no, event,
346 disc ? "disconnected" : "normal",
347 slow ? "slow" : "fast");
348 /* Analyze subchannel status. */
353 /* Check if paths have become available. */
359 /* Prevent unwanted effects when opening lock. */
360 cio_disable_subchannel(sch);
361 device_set_disconnected(sch);
362 /* Ask driver what to do with device. */
364 if (sch->driver && sch->driver->notify) {
365 spin_unlock_irqrestore(sch->lock, flags);
366 ret = sch->driver->notify(sch, event);
367 spin_lock_irqsave(sch->lock, flags);
373 /* Device will be removed, so no notify necessary. */
375 /* Reprobe because immediate unregister might block. */
378 action = UNREGISTER_PROBE;
382 /* Get device operational again. */
386 /* Perform action. */
390 case UNREGISTER_PROBE:
391 /* Unregister device (will use subchannel lock). */
392 spin_unlock_irqrestore(sch->lock, flags);
393 css_sch_device_unregister(sch);
394 spin_lock_irqsave(sch->lock, flags);
396 /* Reset intparm to zeroes. */
397 sch->schib.pmcw.intparm = 0;
401 device_trigger_reprobe(sch);
406 spin_unlock_irqrestore(sch->lock, flags);
407 /* Probe if necessary. */
408 if (action == UNREGISTER_PROBE)
409 ret = css_probe_device(sch->schid);
414 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
419 /* Will be done on the slow path. */
422 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
423 /* Unusable - ignore. */
426 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
427 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
429 return css_probe_device(schid);
432 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
434 struct subchannel *sch;
437 sch = get_subchannel_by_schid(schid);
439 ret = css_evaluate_known_subchannel(sch, slow);
440 put_device(&sch->dev);
442 ret = css_evaluate_new_subchannel(schid, slow);
444 css_schedule_eval(schid);
447 static struct idset *slow_subchannel_set;
448 static spinlock_t slow_subchannel_lock;
450 static int __init slow_subchannel_init(void)
452 spin_lock_init(&slow_subchannel_lock);
453 slow_subchannel_set = idset_sch_new();
454 if (!slow_subchannel_set) {
455 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
461 static int slow_eval_known_fn(struct subchannel *sch, void *data)
466 spin_lock_irq(&slow_subchannel_lock);
467 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
468 idset_sch_del(slow_subchannel_set, sch->schid);
469 spin_unlock_irq(&slow_subchannel_lock);
471 rc = css_evaluate_known_subchannel(sch, 1);
473 css_schedule_eval(sch->schid);
478 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
483 spin_lock_irq(&slow_subchannel_lock);
484 eval = idset_sch_contains(slow_subchannel_set, schid);
485 idset_sch_del(slow_subchannel_set, schid);
486 spin_unlock_irq(&slow_subchannel_lock);
488 rc = css_evaluate_new_subchannel(schid, 1);
491 css_schedule_eval(schid);
497 /* These should abort looping */
506 static void css_slow_path_func(struct work_struct *unused)
508 CIO_TRACE_EVENT(4, "slowpath");
509 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
513 static DECLARE_WORK(slow_path_work, css_slow_path_func);
514 struct workqueue_struct *slow_path_wq;
516 void css_schedule_eval(struct subchannel_id schid)
520 spin_lock_irqsave(&slow_subchannel_lock, flags);
521 idset_sch_add(slow_subchannel_set, schid);
522 queue_work(slow_path_wq, &slow_path_work);
523 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
526 void css_schedule_eval_all(void)
530 spin_lock_irqsave(&slow_subchannel_lock, flags);
531 idset_fill(slow_subchannel_set);
532 queue_work(slow_path_wq, &slow_path_work);
533 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
536 void css_wait_for_slow_path(void)
538 flush_workqueue(ccw_device_notify_work);
539 flush_workqueue(slow_path_wq);
542 /* Reprobe subchannel if unregistered. */
543 static int reprobe_subchannel(struct subchannel_id schid, void *data)
547 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
548 schid.ssid, schid.sch_no);
552 ret = css_probe_device(schid);
559 /* These should abort looping */
568 /* Work function used to reprobe all unregistered subchannels. */
569 static void reprobe_all(struct work_struct *unused)
573 CIO_MSG_EVENT(2, "reprobe start\n");
576 /* Make sure initial subchannel scan is done. */
577 wait_event(ccw_device_init_wq,
578 atomic_read(&ccw_device_init_count) == 0);
579 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
581 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
585 static DECLARE_WORK(css_reprobe_work, reprobe_all);
587 /* Schedule reprobing of all unregistered subchannels. */
588 void css_schedule_reprobe(void)
591 queue_work(slow_path_wq, &css_reprobe_work);
594 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
597 * Called from the machine check handler for subchannel report words.
599 void css_process_crw(int rsid1, int rsid2)
601 struct subchannel_id mchk_schid;
603 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
605 init_subchannel_id(&mchk_schid);
606 mchk_schid.sch_no = rsid1;
608 mchk_schid.ssid = (rsid2 >> 8) & 3;
611 * Since we are always presented with IPI in the CRW, we have to
612 * use stsch() to find out if the subchannel in question has come
615 css_evaluate_subchannel(mchk_schid, 0);
619 __init_channel_subsystem(struct subchannel_id schid, void *data)
621 struct subchannel *sch;
624 if (cio_is_console(schid))
625 sch = cio_get_console_subchannel();
627 sch = css_alloc_subchannel(schid);
636 panic("Out of memory in init_channel_subsystem\n");
637 /* -ENXIO: no more subchannels. */
640 /* -EIO: this subchannel set not supported. */
648 * We register ALL valid subchannels in ioinfo, even those
649 * that have been present before init_channel_subsystem.
650 * These subchannels can't have been registered yet (kmalloc
651 * not working) so we do it now. This is true e.g. for the
652 * console subchannel.
654 css_register_subchannel(sch);
659 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
661 if (css_characteristics_avail && css_general_characteristics.mcss) {
662 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
663 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
666 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
668 css->global_pgid.pgid_high.cpu_addr = 0;
671 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
672 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
673 css->global_pgid.tod_high = tod_high;
678 channel_subsystem_release(struct device *dev)
680 struct channel_subsystem *css;
683 mutex_destroy(&css->mutex);
688 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
691 struct channel_subsystem *css = to_css(dev);
696 mutex_lock(&css->mutex);
697 ret = sprintf(buf, "%x\n", css->cm_enabled);
698 mutex_unlock(&css->mutex);
703 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
704 const char *buf, size_t count)
706 struct channel_subsystem *css = to_css(dev);
709 mutex_lock(&css->mutex);
712 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
715 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
720 mutex_unlock(&css->mutex);
721 return ret < 0 ? ret : count;
724 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
726 static int __init setup_css(int nr)
730 struct channel_subsystem *css;
732 css = channel_subsystems[nr];
733 memset(css, 0, sizeof(struct channel_subsystem));
734 css->pseudo_subchannel =
735 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
736 if (!css->pseudo_subchannel)
738 css->pseudo_subchannel->dev.parent = &css->device;
739 css->pseudo_subchannel->dev.release = css_subchannel_release;
740 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
741 ret = cio_create_sch_lock(css->pseudo_subchannel);
743 kfree(css->pseudo_subchannel);
746 mutex_init(&css->mutex);
749 sprintf(css->device.bus_id, "css%x", nr);
750 css->device.release = channel_subsystem_release;
751 tod_high = (u32) (get_clock() >> 32);
752 css_generate_pgid(css, tod_high);
756 static int css_reboot_event(struct notifier_block *this,
763 for (i = 0; i <= __MAX_CSSID; i++) {
764 struct channel_subsystem *css;
766 css = channel_subsystems[i];
767 mutex_lock(&css->mutex);
769 if (chsc_secm(css, 0))
771 mutex_unlock(&css->mutex);
777 static struct notifier_block css_reboot_notifier = {
778 .notifier_call = css_reboot_event,
782 * Now that the driver core is running, we can setup our channel subsystem.
783 * The struct subchannel's are created during probing (except for the
784 * static console subchannel).
787 init_channel_subsystem (void)
791 ret = chsc_determine_css_characteristics();
793 goto out; /* No need to continue. */
795 css_characteristics_avail = 1;
797 ret = chsc_alloc_sei_area();
801 ret = slow_subchannel_init();
805 if ((ret = bus_register(&css_bus_type)))
808 /* Try to enable MSS. */
809 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
811 case 0: /* Success. */
812 max_ssid = __MAX_SSID;
819 /* Setup css structure. */
820 for (i = 0; i <= __MAX_CSSID; i++) {
821 struct channel_subsystem *css;
823 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
828 channel_subsystems[i] = css;
832 ret = device_register(&css->device);
835 if (css_characteristics_avail &&
836 css_chsc_characteristics.secm) {
837 ret = device_create_file(&css->device,
838 &dev_attr_cm_enable);
842 ret = device_register(&css->pseudo_subchannel->dev);
846 ret = register_reboot_notifier(&css_reboot_notifier);
853 for_each_subchannel(__init_channel_subsystem, NULL);
856 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
858 device_remove_file(&channel_subsystems[i]->device,
859 &dev_attr_cm_enable);
861 device_unregister(&channel_subsystems[i]->device);
863 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
864 kfree(channel_subsystems[i]->pseudo_subchannel);
866 kfree(channel_subsystems[i]);
869 struct channel_subsystem *css;
872 css = channel_subsystems[i];
873 device_unregister(&css->pseudo_subchannel->dev);
874 if (css_characteristics_avail && css_chsc_characteristics.secm)
875 device_remove_file(&css->device,
876 &dev_attr_cm_enable);
877 device_unregister(&css->device);
880 bus_unregister(&css_bus_type);
882 chsc_free_sei_area();
883 kfree(slow_subchannel_set);
884 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
889 int sch_is_pseudo_sch(struct subchannel *sch)
891 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
895 * find a driver for a subchannel. They identify by the subchannel
896 * type with the exception that the console subchannel driver has its own
897 * subchannel type although the device is an i/o subchannel
900 css_bus_match (struct device *dev, struct device_driver *drv)
902 struct subchannel *sch = to_subchannel(dev);
903 struct css_driver *driver = to_cssdriver(drv);
905 if (sch->st == driver->subchannel_type)
911 static int css_probe(struct device *dev)
913 struct subchannel *sch;
916 sch = to_subchannel(dev);
917 sch->driver = to_cssdriver(dev->driver);
918 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
924 static int css_remove(struct device *dev)
926 struct subchannel *sch;
929 sch = to_subchannel(dev);
930 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
935 static void css_shutdown(struct device *dev)
937 struct subchannel *sch;
939 sch = to_subchannel(dev);
940 if (sch->driver && sch->driver->shutdown)
941 sch->driver->shutdown(sch);
944 struct bus_type css_bus_type = {
946 .match = css_bus_match,
948 .remove = css_remove,
949 .shutdown = css_shutdown,
953 * css_driver_register - register a css driver
954 * @cdrv: css driver to register
956 * This is mainly a wrapper around driver_register that sets name
957 * and bus_type in the embedded struct device_driver correctly.
959 int css_driver_register(struct css_driver *cdrv)
961 cdrv->drv.name = cdrv->name;
962 cdrv->drv.bus = &css_bus_type;
963 cdrv->drv.owner = cdrv->owner;
964 return driver_register(&cdrv->drv);
966 EXPORT_SYMBOL_GPL(css_driver_register);
969 * css_driver_unregister - unregister a css driver
970 * @cdrv: css driver to unregister
972 * This is a wrapper around driver_unregister.
974 void css_driver_unregister(struct css_driver *cdrv)
976 driver_unregister(&cdrv->drv);
978 EXPORT_SYMBOL_GPL(css_driver_unregister);
980 subsys_initcall(init_channel_subsystem);
982 MODULE_LICENSE("GPL");
983 EXPORT_SYMBOL(css_bus_type);
984 EXPORT_SYMBOL_GPL(css_characteristics_avail);