2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
6 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
9 * Cornelia Huck (cohuck@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/list.h>
20 #include "cio_debug.h"
23 unsigned int highest_subchannel;
25 int css_init_done = 0;
27 struct device css_bus_device = {
31 static struct subchannel *
32 css_alloc_subchannel(int irq)
34 struct subchannel *sch;
37 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
39 return ERR_PTR(-ENOMEM);
40 ret = cio_validate_subchannel (sch, irq);
45 if (irq > highest_subchannel)
46 highest_subchannel = irq;
48 if (sch->st != SUBCHANNEL_TYPE_IO) {
49 /* For now we ignore all non-io subchannels. */
51 return ERR_PTR(-EINVAL);
55 * Set intparm to subchannel address.
56 * This is fine even on 64bit since the subchannel is always located
59 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
60 ret = cio_modify(sch);
69 css_free_subchannel(struct subchannel *sch)
72 /* Reset intparm to zeroes. */
73 sch->schib.pmcw.intparm = 0;
81 css_subchannel_release(struct device *dev)
83 struct subchannel *sch;
85 sch = to_subchannel(dev);
86 if (!cio_is_console(sch->irq))
90 extern int css_get_ssd_info(struct subchannel *sch);
93 css_register_subchannel(struct subchannel *sch)
97 /* Initialize the subchannel structure */
98 sch->dev.parent = &css_bus_device;
99 sch->dev.bus = &css_bus_type;
100 sch->dev.release = &css_subchannel_release;
102 /* make it known to the system */
103 ret = device_register(&sch->dev);
105 printk (KERN_WARNING "%s: could not register %s\n",
106 __func__, sch->dev.bus_id);
108 css_get_ssd_info(sch);
113 css_probe_device(int irq)
116 struct subchannel *sch;
118 sch = css_alloc_subchannel(irq);
121 ret = css_register_subchannel(sch);
123 css_free_subchannel(sch);
128 get_subchannel_by_schid(int irq)
130 struct subchannel *sch;
131 struct list_head *entry;
134 if (!get_bus(&css_bus_type))
136 down_read(&css_bus_type.subsys.rwsem);
138 list_for_each(entry, &css_bus_type.devices.list) {
139 dev = get_device(container_of(entry,
140 struct device, bus_list));
143 sch = to_subchannel(dev);
149 up_read(&css_bus_type.subsys.rwsem);
150 put_bus(&css_bus_type);
156 css_get_subchannel_status(struct subchannel *sch, int schid)
161 cc = stsch(schid, &schib);
166 if (sch && sch->schib.pmcw.dnv &&
167 (schib.pmcw.dev != sch->schib.pmcw.dev))
168 return CIO_REVALIDATE;
173 css_evaluate_subchannel(int irq, int slow)
175 int event, ret, disc;
176 struct subchannel *sch;
178 sch = get_subchannel_by_schid(irq);
179 disc = sch ? device_is_disconnected(sch) : 0;
182 put_device(&sch->dev);
183 return 0; /* Already processed. */
185 if (!disc && !slow) {
187 put_device(&sch->dev);
188 return -EAGAIN; /* Will be done on the slow path. */
190 event = css_get_subchannel_status(sch, irq);
194 /* Never used this subchannel. Ignore. */
198 if (sch->driver && sch->driver->notify &&
199 sch->driver->notify(&sch->dev, CIO_GONE)) {
200 device_set_disconnected(sch);
205 * Unregister subchannel.
206 * The device will be killed automatically.
208 device_unregister(&sch->dev);
209 /* Reset intparm to zeroes. */
210 sch->schib.pmcw.intparm = 0;
212 put_device(&sch->dev);
217 * Revalidation machine check. Sick.
218 * We don't notify the driver since we have to throw the device
221 device_unregister(&sch->dev);
222 /* Reset intparm to zeroes. */
223 sch->schib.pmcw.intparm = 0;
225 put_device(&sch->dev);
226 ret = css_probe_device(irq);
230 /* Get device operational again. */
231 device_trigger_reprobe(sch);
232 ret = sch ? 0 : css_probe_device(irq);
242 css_rescan_devices(void)
246 for (irq = 0; irq <= __MAX_SUBCHANNELS; irq++) {
247 ret = css_evaluate_subchannel(irq, 1);
248 /* No more memory. It doesn't make sense to continue. No
249 * panic because this can happen in midflight and just
250 * because we can't use a new device is no reason to crash
254 /* -ENXIO indicates that there are no more subchannels. */
261 css_evaluate_slow_subchannel(unsigned long schid)
263 css_evaluate_subchannel(schid, 1);
267 css_trigger_slow_path(void)
271 css_rescan_devices();
274 css_walk_subchannel_slow_list(css_evaluate_slow_subchannel);
278 * Rescan for new devices. FIXME: This is slow.
279 * This function is called when we have lost CRWs due to overflows and we have
280 * to do subchannel housekeeping.
283 css_reiterate_subchannels(void)
285 css_clear_subchannel_slow_list();
290 * Called from the machine check handler for subchannel report words.
293 css_process_crw(int irq)
297 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
300 /* We need to iterate all subchannels anyway. */
303 * Since we are always presented with IPI in the CRW, we have to
304 * use stsch() to find out if the subchannel in question has come
307 ret = css_evaluate_subchannel(irq, 0);
308 if (ret == -EAGAIN) {
309 if (css_enqueue_subchannel_slow(irq)) {
310 css_clear_subchannel_slow_list();
318 * some of the initialization has already been done from init_IRQ(),
319 * here we do the rest now that the driver core is running.
320 * The struct subchannel's are created during probing (except for the
321 * static console subchannel).
324 init_channel_subsystem (void)
328 if ((ret = bus_register(&css_bus_type)))
330 if ((ret = device_register (&css_bus_device)))
337 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
338 struct subchannel *sch;
340 if (cio_is_console(irq))
341 sch = cio_get_console_subchannel();
343 sch = css_alloc_subchannel(irq);
349 panic("Out of memory in "
350 "init_channel_subsystem\n");
351 /* -ENXIO: no more subchannels. */
358 * We register ALL valid subchannels in ioinfo, even those
359 * that have been present before init_channel_subsystem.
360 * These subchannels can't have been registered yet (kmalloc
361 * not working) so we do it now. This is true e.g. for the
362 * console subchannel.
364 css_register_subchannel(sch);
369 bus_unregister(&css_bus_type);
375 * find a driver for a subchannel. They identify by the subchannel
376 * type with the exception that the console subchannel driver has its own
377 * subchannel type although the device is an i/o subchannel
380 css_bus_match (struct device *dev, struct device_driver *drv)
382 struct subchannel *sch = container_of (dev, struct subchannel, dev);
383 struct css_driver *driver = container_of (drv, struct css_driver, drv);
385 if (sch->st == driver->subchannel_type)
391 struct bus_type css_bus_type = {
393 .match = &css_bus_match,
396 subsys_initcall(init_channel_subsystem);
399 * Register root devices for some drivers. The release function must not be
400 * in the device drivers, so we do it here.
403 s390_root_dev_release(struct device *dev)
409 s390_root_dev_register(const char *name)
415 return ERR_PTR(-EINVAL);
416 dev = kmalloc(sizeof(struct device), GFP_KERNEL);
418 return ERR_PTR(-ENOMEM);
419 memset(dev, 0, sizeof(struct device));
420 strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE));
421 dev->release = s390_root_dev_release;
422 ret = device_register(dev);
431 s390_root_dev_unregister(struct device *dev)
434 device_unregister(dev);
437 struct slow_subchannel {
438 struct list_head slow_list;
442 static LIST_HEAD(slow_subchannels_head);
443 static spinlock_t slow_subchannel_lock = SPIN_LOCK_UNLOCKED;
446 css_enqueue_subchannel_slow(unsigned long schid)
448 struct slow_subchannel *new_slow_sch;
451 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
454 new_slow_sch->schid = schid;
455 spin_lock_irqsave(&slow_subchannel_lock, flags);
456 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
457 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
462 css_clear_subchannel_slow_list(void)
466 spin_lock_irqsave(&slow_subchannel_lock, flags);
467 while (!list_empty(&slow_subchannels_head)) {
468 struct slow_subchannel *slow_sch =
469 list_entry(slow_subchannels_head.next,
470 struct slow_subchannel, slow_list);
472 list_del_init(slow_subchannels_head.next);
475 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
479 css_walk_subchannel_slow_list(void (*fn)(unsigned long))
483 spin_lock_irqsave(&slow_subchannel_lock, flags);
484 while (!list_empty(&slow_subchannels_head)) {
485 struct slow_subchannel *slow_sch =
486 list_entry(slow_subchannels_head.next,
487 struct slow_subchannel, slow_list);
489 list_del_init(slow_subchannels_head.next);
490 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
492 spin_lock_irqsave(&slow_subchannel_lock, flags);
495 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
499 css_slow_subchannels_exist(void)
501 return (!list_empty(&slow_subchannels_head));
504 MODULE_LICENSE("GPL");
505 EXPORT_SYMBOL(css_bus_type);
506 EXPORT_SYMBOL(s390_root_dev_register);
507 EXPORT_SYMBOL(s390_root_dev_unregister);