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;
169 if (sch && !sch->lpm)
175 css_evaluate_subchannel(int irq, int slow)
177 int event, ret, disc;
178 struct subchannel *sch;
180 sch = get_subchannel_by_schid(irq);
181 disc = sch ? device_is_disconnected(sch) : 0;
184 put_device(&sch->dev);
185 return 0; /* Already processed. */
187 if (!disc && !slow) {
189 put_device(&sch->dev);
190 return -EAGAIN; /* Will be done on the slow path. */
192 event = css_get_subchannel_status(sch, irq);
193 CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
194 irq, event, sch?(disc?"disconnected":"normal"):"unknown",
200 /* Never used this subchannel. Ignore. */
204 if (sch->driver && sch->driver->notify &&
205 sch->driver->notify(&sch->dev, event)) {
206 cio_disable_subchannel(sch);
207 device_set_disconnected(sch);
212 * Unregister subchannel.
213 * The device will be killed automatically.
215 cio_disable_subchannel(sch);
216 device_unregister(&sch->dev);
217 /* Reset intparm to zeroes. */
218 sch->schib.pmcw.intparm = 0;
220 put_device(&sch->dev);
225 * Revalidation machine check. Sick.
226 * We don't notify the driver since we have to throw the device
230 device_unregister(&sch->dev);
231 /* Reset intparm to zeroes. */
232 sch->schib.pmcw.intparm = 0;
234 put_device(&sch->dev);
235 ret = css_probe_device(irq);
238 * We can't immediately deregister the disconnected
239 * device since it might block.
241 device_trigger_reprobe(sch);
247 /* Get device operational again. */
248 device_trigger_reprobe(sch);
249 ret = sch ? 0 : css_probe_device(irq);
259 css_rescan_devices(void)
263 for (irq = 0; irq <= __MAX_SUBCHANNELS; irq++) {
264 ret = css_evaluate_subchannel(irq, 1);
265 /* No more memory. It doesn't make sense to continue. No
266 * panic because this can happen in midflight and just
267 * because we can't use a new device is no reason to crash
271 /* -ENXIO indicates that there are no more subchannels. */
277 struct slow_subchannel {
278 struct list_head slow_list;
282 static LIST_HEAD(slow_subchannels_head);
283 static spinlock_t slow_subchannel_lock = SPIN_LOCK_UNLOCKED;
286 css_trigger_slow_path(void)
288 CIO_TRACE_EVENT(4, "slowpath");
292 css_rescan_devices();
296 spin_lock_irq(&slow_subchannel_lock);
297 while (!list_empty(&slow_subchannels_head)) {
298 struct slow_subchannel *slow_sch =
299 list_entry(slow_subchannels_head.next,
300 struct slow_subchannel, slow_list);
302 list_del_init(slow_subchannels_head.next);
303 spin_unlock_irq(&slow_subchannel_lock);
304 css_evaluate_subchannel(slow_sch->schid, 1);
305 spin_lock_irq(&slow_subchannel_lock);
308 spin_unlock_irq(&slow_subchannel_lock);
311 typedef void (*workfunc)(void *);
312 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
313 struct workqueue_struct *slow_path_wq;
316 * Rescan for new devices. FIXME: This is slow.
317 * This function is called when we have lost CRWs due to overflows and we have
318 * to do subchannel housekeeping.
321 css_reiterate_subchannels(void)
323 css_clear_subchannel_slow_list();
328 * Called from the machine check handler for subchannel report words.
331 css_process_crw(int irq)
335 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
338 /* We need to iterate all subchannels anyway. */
341 * Since we are always presented with IPI in the CRW, we have to
342 * use stsch() to find out if the subchannel in question has come
345 ret = css_evaluate_subchannel(irq, 0);
346 if (ret == -EAGAIN) {
347 if (css_enqueue_subchannel_slow(irq)) {
348 css_clear_subchannel_slow_list();
356 * some of the initialization has already been done from init_IRQ(),
357 * here we do the rest now that the driver core is running.
358 * The struct subchannel's are created during probing (except for the
359 * static console subchannel).
362 init_channel_subsystem (void)
366 if ((ret = bus_register(&css_bus_type)))
368 if ((ret = device_register (&css_bus_device)))
375 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
376 struct subchannel *sch;
378 if (cio_is_console(irq))
379 sch = cio_get_console_subchannel();
381 sch = css_alloc_subchannel(irq);
387 panic("Out of memory in "
388 "init_channel_subsystem\n");
389 /* -ENXIO: no more subchannels. */
396 * We register ALL valid subchannels in ioinfo, even those
397 * that have been present before init_channel_subsystem.
398 * These subchannels can't have been registered yet (kmalloc
399 * not working) so we do it now. This is true e.g. for the
400 * console subchannel.
402 css_register_subchannel(sch);
407 bus_unregister(&css_bus_type);
413 * find a driver for a subchannel. They identify by the subchannel
414 * type with the exception that the console subchannel driver has its own
415 * subchannel type although the device is an i/o subchannel
418 css_bus_match (struct device *dev, struct device_driver *drv)
420 struct subchannel *sch = container_of (dev, struct subchannel, dev);
421 struct css_driver *driver = container_of (drv, struct css_driver, drv);
423 if (sch->st == driver->subchannel_type)
429 struct bus_type css_bus_type = {
431 .match = &css_bus_match,
434 subsys_initcall(init_channel_subsystem);
437 * Register root devices for some drivers. The release function must not be
438 * in the device drivers, so we do it here.
441 s390_root_dev_release(struct device *dev)
447 s390_root_dev_register(const char *name)
453 return ERR_PTR(-EINVAL);
454 dev = kmalloc(sizeof(struct device), GFP_KERNEL);
456 return ERR_PTR(-ENOMEM);
457 memset(dev, 0, sizeof(struct device));
458 strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE));
459 dev->release = s390_root_dev_release;
460 ret = device_register(dev);
469 s390_root_dev_unregister(struct device *dev)
472 device_unregister(dev);
476 css_enqueue_subchannel_slow(unsigned long schid)
478 struct slow_subchannel *new_slow_sch;
481 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
484 new_slow_sch->schid = schid;
485 spin_lock_irqsave(&slow_subchannel_lock, flags);
486 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
487 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
492 css_clear_subchannel_slow_list(void)
496 spin_lock_irqsave(&slow_subchannel_lock, flags);
497 while (!list_empty(&slow_subchannels_head)) {
498 struct slow_subchannel *slow_sch =
499 list_entry(slow_subchannels_head.next,
500 struct slow_subchannel, slow_list);
502 list_del_init(slow_subchannels_head.next);
505 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
511 css_slow_subchannels_exist(void)
513 return (!list_empty(&slow_subchannels_head));
516 MODULE_LICENSE("GPL");
517 EXPORT_SYMBOL(css_bus_type);
518 EXPORT_SYMBOL(s390_root_dev_register);
519 EXPORT_SYMBOL(s390_root_dev_unregister);