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>
19 #include "cio_debug.h"
25 int css_init_done = 0;
26 static int need_reprobe = 0;
27 static int max_ssid = 0;
29 struct channel_subsystem *css[__MAX_CSSID + 1];
31 int css_characteristics_avail = 0;
34 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
36 struct subchannel_id schid;
39 init_subchannel_id(&schid);
43 ret = fn(schid, data);
46 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
48 } while (schid.ssid++ < max_ssid);
52 static struct subchannel *
53 css_alloc_subchannel(struct subchannel_id schid)
55 struct subchannel *sch;
58 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
60 return ERR_PTR(-ENOMEM);
61 ret = cio_validate_subchannel (sch, schid);
67 if (sch->st != SUBCHANNEL_TYPE_IO) {
68 /* For now we ignore all non-io subchannels. */
70 return ERR_PTR(-EINVAL);
74 * Set intparm to subchannel address.
75 * This is fine even on 64bit since the subchannel is always located
78 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
79 ret = cio_modify(sch);
88 css_free_subchannel(struct subchannel *sch)
91 /* Reset intparm to zeroes. */
92 sch->schib.pmcw.intparm = 0;
100 css_subchannel_release(struct device *dev)
102 struct subchannel *sch;
104 sch = to_subchannel(dev);
105 if (!cio_is_console(sch->schid)) {
111 int css_sch_device_register(struct subchannel *sch)
115 mutex_lock(&sch->reg_mutex);
116 ret = device_register(&sch->dev);
117 mutex_unlock(&sch->reg_mutex);
121 void css_sch_device_unregister(struct subchannel *sch)
123 mutex_lock(&sch->reg_mutex);
124 device_unregister(&sch->dev);
125 mutex_unlock(&sch->reg_mutex);
129 css_register_subchannel(struct subchannel *sch)
133 /* Initialize the subchannel structure */
134 sch->dev.parent = &css[0]->device;
135 sch->dev.bus = &css_bus_type;
136 sch->dev.release = &css_subchannel_release;
137 sch->dev.groups = subch_attr_groups;
139 css_get_ssd_info(sch);
141 /* make it known to the system */
142 ret = css_sch_device_register(sch);
144 printk (KERN_WARNING "%s: could not register %s\n",
145 __func__, sch->dev.bus_id);
152 css_probe_device(struct subchannel_id schid)
155 struct subchannel *sch;
157 sch = css_alloc_subchannel(schid);
160 ret = css_register_subchannel(sch);
162 css_free_subchannel(sch);
167 check_subchannel(struct device * dev, void * data)
169 struct subchannel *sch;
170 struct subchannel_id *schid = data;
172 sch = to_subchannel(dev);
173 return schid_equal(&sch->schid, schid);
177 get_subchannel_by_schid(struct subchannel_id schid)
181 dev = bus_find_device(&css_bus_type, NULL,
182 &schid, check_subchannel);
184 return dev ? to_subchannel(dev) : NULL;
187 static int css_get_subchannel_status(struct subchannel *sch)
191 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
193 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
194 return CIO_REVALIDATE;
200 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
202 int event, ret, disc;
204 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
206 spin_lock_irqsave(sch->lock, flags);
207 disc = device_is_disconnected(sch);
209 /* Disconnected devices are evaluated directly only.*/
210 spin_unlock_irqrestore(sch->lock, flags);
213 /* No interrupt after machine check - kill pending timers. */
214 device_kill_pending_timer(sch);
215 if (!disc && !slow) {
216 /* Non-disconnected devices are evaluated on the slow path. */
217 spin_unlock_irqrestore(sch->lock, flags);
220 event = css_get_subchannel_status(sch);
221 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
222 sch->schid.ssid, sch->schid.sch_no, event,
223 disc ? "disconnected" : "normal",
224 slow ? "slow" : "fast");
225 /* Analyze subchannel status. */
230 /* Check if paths have become available. */
236 /* Prevent unwanted effects when opening lock. */
237 cio_disable_subchannel(sch);
238 device_set_disconnected(sch);
239 /* Ask driver what to do with device. */
241 if (sch->driver && sch->driver->notify) {
242 spin_unlock_irqrestore(sch->lock, flags);
243 ret = sch->driver->notify(&sch->dev, event);
244 spin_lock_irqsave(sch->lock, flags);
250 /* Device will be removed, so no notify necessary. */
252 /* Reprobe because immediate unregister might block. */
255 action = UNREGISTER_PROBE;
259 /* Get device operational again. */
263 /* Perform action. */
267 case UNREGISTER_PROBE:
268 /* Unregister device (will use subchannel lock). */
269 spin_unlock_irqrestore(sch->lock, flags);
270 css_sch_device_unregister(sch);
271 spin_lock_irqsave(sch->lock, flags);
273 /* Reset intparm to zeroes. */
274 sch->schib.pmcw.intparm = 0;
278 device_trigger_reprobe(sch);
283 spin_unlock_irqrestore(sch->lock, flags);
284 /* Probe if necessary. */
285 if (action == UNREGISTER_PROBE)
286 ret = css_probe_device(sch->schid);
291 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
296 /* Will be done on the slow path. */
299 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
300 /* Unusable - ignore. */
303 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
304 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
306 return css_probe_device(schid);
309 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
311 struct subchannel *sch;
314 sch = get_subchannel_by_schid(schid);
316 ret = css_evaluate_known_subchannel(sch, slow);
317 put_device(&sch->dev);
319 ret = css_evaluate_new_subchannel(schid, slow);
321 css_schedule_eval(schid);
324 static struct idset *slow_subchannel_set;
325 static spinlock_t slow_subchannel_lock;
327 static int __init slow_subchannel_init(void)
329 spin_lock_init(&slow_subchannel_lock);
330 slow_subchannel_set = idset_sch_new();
331 if (!slow_subchannel_set) {
332 printk(KERN_WARNING "cio: could not allocate slow subchannel "
339 subsys_initcall(slow_subchannel_init);
341 static void css_slow_path_func(struct work_struct *unused)
343 struct subchannel_id schid;
345 CIO_TRACE_EVENT(4, "slowpath");
346 spin_lock_irq(&slow_subchannel_lock);
347 init_subchannel_id(&schid);
348 while (idset_sch_get_first(slow_subchannel_set, &schid)) {
349 idset_sch_del(slow_subchannel_set, schid);
350 spin_unlock_irq(&slow_subchannel_lock);
351 css_evaluate_subchannel(schid, 1);
352 spin_lock_irq(&slow_subchannel_lock);
354 spin_unlock_irq(&slow_subchannel_lock);
357 static DECLARE_WORK(slow_path_work, css_slow_path_func);
358 struct workqueue_struct *slow_path_wq;
360 void css_schedule_eval(struct subchannel_id schid)
364 spin_lock_irqsave(&slow_subchannel_lock, flags);
365 idset_sch_add(slow_subchannel_set, schid);
366 queue_work(slow_path_wq, &slow_path_work);
367 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
370 void css_schedule_eval_all(void)
374 spin_lock_irqsave(&slow_subchannel_lock, flags);
375 idset_fill(slow_subchannel_set);
376 queue_work(slow_path_wq, &slow_path_work);
377 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
380 /* Reprobe subchannel if unregistered. */
381 static int reprobe_subchannel(struct subchannel_id schid, void *data)
383 struct subchannel *sch;
386 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
387 schid.ssid, schid.sch_no);
391 sch = get_subchannel_by_schid(schid);
394 put_device(&sch->dev);
398 ret = css_probe_device(schid);
404 /* These should abort looping */
413 /* Work function used to reprobe all unregistered subchannels. */
414 static void reprobe_all(struct work_struct *unused)
418 CIO_MSG_EVENT(2, "reprobe start\n");
421 /* Make sure initial subchannel scan is done. */
422 wait_event(ccw_device_init_wq,
423 atomic_read(&ccw_device_init_count) == 0);
424 ret = for_each_subchannel(reprobe_subchannel, NULL);
426 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
430 static DECLARE_WORK(css_reprobe_work, reprobe_all);
432 /* Schedule reprobing of all unregistered subchannels. */
433 void css_schedule_reprobe(void)
436 queue_work(ccw_device_work, &css_reprobe_work);
439 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
442 * Called from the machine check handler for subchannel report words.
444 void css_process_crw(int rsid1, int rsid2)
446 struct subchannel_id mchk_schid;
448 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
450 init_subchannel_id(&mchk_schid);
451 mchk_schid.sch_no = rsid1;
453 mchk_schid.ssid = (rsid2 >> 8) & 3;
456 * Since we are always presented with IPI in the CRW, we have to
457 * use stsch() to find out if the subchannel in question has come
460 css_evaluate_subchannel(mchk_schid, 0);
464 __init_channel_subsystem(struct subchannel_id schid, void *data)
466 struct subchannel *sch;
469 if (cio_is_console(schid))
470 sch = cio_get_console_subchannel();
472 sch = css_alloc_subchannel(schid);
481 panic("Out of memory in init_channel_subsystem\n");
482 /* -ENXIO: no more subchannels. */
485 /* -EIO: this subchannel set not supported. */
493 * We register ALL valid subchannels in ioinfo, even those
494 * that have been present before init_channel_subsystem.
495 * These subchannels can't have been registered yet (kmalloc
496 * not working) so we do it now. This is true e.g. for the
497 * console subchannel.
499 css_register_subchannel(sch);
504 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
506 if (css_characteristics_avail && css_general_characteristics.mcss) {
507 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
508 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
511 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
513 css->global_pgid.pgid_high.cpu_addr = 0;
516 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
517 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
518 css->global_pgid.tod_high = tod_high;
523 channel_subsystem_release(struct device *dev)
525 struct channel_subsystem *css;
528 mutex_destroy(&css->mutex);
533 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
536 struct channel_subsystem *css = to_css(dev);
540 return sprintf(buf, "%x\n", css->cm_enabled);
544 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
545 const char *buf, size_t count)
547 struct channel_subsystem *css = to_css(dev);
552 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
555 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
560 return ret < 0 ? ret : count;
563 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
565 static int __init setup_css(int nr)
570 memset(css[nr], 0, sizeof(struct channel_subsystem));
571 css[nr]->pseudo_subchannel =
572 kzalloc(sizeof(*css[nr]->pseudo_subchannel), GFP_KERNEL);
573 if (!css[nr]->pseudo_subchannel)
575 css[nr]->pseudo_subchannel->dev.parent = &css[nr]->device;
576 css[nr]->pseudo_subchannel->dev.release = css_subchannel_release;
577 sprintf(css[nr]->pseudo_subchannel->dev.bus_id, "defunct");
578 ret = cio_create_sch_lock(css[nr]->pseudo_subchannel);
580 kfree(css[nr]->pseudo_subchannel);
583 mutex_init(&css[nr]->mutex);
586 sprintf(css[nr]->device.bus_id, "css%x", nr);
587 css[nr]->device.release = channel_subsystem_release;
588 tod_high = (u32) (get_clock() >> 32);
589 css_generate_pgid(css[nr], tod_high);
594 * Now that the driver core is running, we can setup our channel subsystem.
595 * The struct subchannel's are created during probing (except for the
596 * static console subchannel).
599 init_channel_subsystem (void)
603 if (chsc_determine_css_characteristics() == 0)
604 css_characteristics_avail = 1;
606 if ((ret = bus_register(&css_bus_type)))
609 /* Try to enable MSS. */
610 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
612 case 0: /* Success. */
613 max_ssid = __MAX_SSID;
620 /* Setup css structure. */
621 for (i = 0; i <= __MAX_CSSID; i++) {
622 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
630 ret = device_register(&css[i]->device);
633 if (css_characteristics_avail &&
634 css_chsc_characteristics.secm) {
635 ret = device_create_file(&css[i]->device,
636 &dev_attr_cm_enable);
640 ret = device_register(&css[i]->pseudo_subchannel->dev);
648 for_each_subchannel(__init_channel_subsystem, NULL);
651 device_remove_file(&css[i]->device, &dev_attr_cm_enable);
653 device_unregister(&css[i]->device);
655 kfree(css[i]->pseudo_subchannel->lock);
656 kfree(css[i]->pseudo_subchannel);
662 device_unregister(&css[i]->pseudo_subchannel->dev);
663 if (css_characteristics_avail && css_chsc_characteristics.secm)
664 device_remove_file(&css[i]->device,
665 &dev_attr_cm_enable);
666 device_unregister(&css[i]->device);
669 bus_unregister(&css_bus_type);
674 int sch_is_pseudo_sch(struct subchannel *sch)
676 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
680 * find a driver for a subchannel. They identify by the subchannel
681 * type with the exception that the console subchannel driver has its own
682 * subchannel type although the device is an i/o subchannel
685 css_bus_match (struct device *dev, struct device_driver *drv)
687 struct subchannel *sch = container_of (dev, struct subchannel, dev);
688 struct css_driver *driver = container_of (drv, struct css_driver, drv);
690 if (sch->st == driver->subchannel_type)
697 css_probe (struct device *dev)
699 struct subchannel *sch;
701 sch = to_subchannel(dev);
702 sch->driver = container_of (dev->driver, struct css_driver, drv);
703 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
707 css_remove (struct device *dev)
709 struct subchannel *sch;
711 sch = to_subchannel(dev);
712 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
716 css_shutdown (struct device *dev)
718 struct subchannel *sch;
720 sch = to_subchannel(dev);
721 if (sch->driver->shutdown)
722 sch->driver->shutdown(sch);
725 struct bus_type css_bus_type = {
727 .match = css_bus_match,
729 .remove = css_remove,
730 .shutdown = css_shutdown,
733 subsys_initcall(init_channel_subsystem);
735 MODULE_LICENSE("GPL");
736 EXPORT_SYMBOL(css_bus_type);
737 EXPORT_SYMBOL_GPL(css_characteristics_avail);