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"
24 unsigned int highest_subchannel;
26 int css_init_done = 0;
28 struct pgid global_pgid;
29 int css_characteristics_avail = 0;
31 struct device css_bus_device = {
35 static struct subchannel *
36 css_alloc_subchannel(struct subchannel_id schid)
38 struct subchannel *sch;
41 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
43 return ERR_PTR(-ENOMEM);
44 ret = cio_validate_subchannel (sch, schid);
50 if (sch->st != SUBCHANNEL_TYPE_IO) {
51 /* For now we ignore all non-io subchannels. */
53 return ERR_PTR(-EINVAL);
57 * Set intparm to subchannel address.
58 * This is fine even on 64bit since the subchannel is always located
61 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
62 ret = cio_modify(sch);
71 css_free_subchannel(struct subchannel *sch)
74 /* Reset intparm to zeroes. */
75 sch->schib.pmcw.intparm = 0;
83 css_subchannel_release(struct device *dev)
85 struct subchannel *sch;
87 sch = to_subchannel(dev);
88 if (!cio_is_console(sch->schid))
92 extern int css_get_ssd_info(struct subchannel *sch);
95 css_register_subchannel(struct subchannel *sch)
99 /* Initialize the subchannel structure */
100 sch->dev.parent = &css_bus_device;
101 sch->dev.bus = &css_bus_type;
102 sch->dev.release = &css_subchannel_release;
104 /* make it known to the system */
105 ret = device_register(&sch->dev);
107 printk (KERN_WARNING "%s: could not register %s\n",
108 __func__, sch->dev.bus_id);
110 css_get_ssd_info(sch);
115 css_probe_device(struct subchannel_id schid)
118 struct subchannel *sch;
120 sch = css_alloc_subchannel(schid);
123 ret = css_register_subchannel(sch);
125 css_free_subchannel(sch);
130 check_subchannel(struct device * dev, void * data)
132 struct subchannel *sch;
133 struct subchannel_id *schid = data;
135 sch = to_subchannel(dev);
136 return schid_equal(&sch->schid, schid);
140 get_subchannel_by_schid(struct subchannel_id schid)
144 dev = bus_find_device(&css_bus_type, NULL,
145 (void *)&schid, check_subchannel);
147 return dev ? to_subchannel(dev) : NULL;
152 css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
157 cc = stsch(schid, &schib);
162 if (sch && sch->schib.pmcw.dnv &&
163 (schib.pmcw.dev != sch->schib.pmcw.dev))
164 return CIO_REVALIDATE;
165 if (sch && !sch->lpm)
171 css_evaluate_subchannel(struct subchannel_id schid, int slow)
173 int event, ret, disc;
174 struct subchannel *sch;
177 sch = get_subchannel_by_schid(schid);
178 disc = sch ? device_is_disconnected(sch) : 0;
181 put_device(&sch->dev);
182 return 0; /* Already processed. */
185 * We've got a machine check, so running I/O won't get an interrupt.
186 * Kill any pending timers.
189 device_kill_pending_timer(sch);
190 if (!disc && !slow) {
192 put_device(&sch->dev);
193 return -EAGAIN; /* Will be done on the slow path. */
195 event = css_get_subchannel_status(sch, schid);
196 CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
198 sch?(disc?"disconnected":"normal"):"unknown",
204 /* Never used this subchannel. Ignore. */
208 if (disc && (event == CIO_NO_PATH)) {
210 * Uargh, hack again. Because we don't get a machine
211 * check on configure on, our path bookkeeping can
212 * be out of date here (it's fine while we only do
213 * logical varying or get chsc machine checks). We
214 * need to force reprobing or we might miss devices
215 * coming operational again. It won't do harm in real
216 * no path situations.
218 spin_lock_irqsave(&sch->lock, flags);
219 device_trigger_reprobe(sch);
220 spin_unlock_irqrestore(&sch->lock, flags);
224 if (sch->driver && sch->driver->notify &&
225 sch->driver->notify(&sch->dev, event)) {
226 cio_disable_subchannel(sch);
227 device_set_disconnected(sch);
232 * Unregister subchannel.
233 * The device will be killed automatically.
235 cio_disable_subchannel(sch);
236 device_unregister(&sch->dev);
237 /* Reset intparm to zeroes. */
238 sch->schib.pmcw.intparm = 0;
240 put_device(&sch->dev);
245 * Revalidation machine check. Sick.
246 * We don't notify the driver since we have to throw the device
250 device_unregister(&sch->dev);
251 /* Reset intparm to zeroes. */
252 sch->schib.pmcw.intparm = 0;
254 put_device(&sch->dev);
255 ret = css_probe_device(schid);
258 * We can't immediately deregister the disconnected
259 * device since it might block.
261 spin_lock_irqsave(&sch->lock, flags);
262 device_trigger_reprobe(sch);
263 spin_unlock_irqrestore(&sch->lock, flags);
269 spin_lock_irqsave(&sch->lock, flags);
270 /* Get device operational again. */
271 device_trigger_reprobe(sch);
272 spin_unlock_irqrestore(&sch->lock, flags);
274 ret = sch ? 0 : css_probe_device(schid);
284 css_rescan_devices(void)
287 struct subchannel_id schid;
289 init_subchannel_id(&schid);
291 ret = css_evaluate_subchannel(schid, 1);
292 /* No more memory. It doesn't make sense to continue. No
293 * panic because this can happen in midflight and just
294 * because we can't use a new device is no reason to crash
298 /* -ENXIO indicates that there are no more subchannels. */
301 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
304 struct slow_subchannel {
305 struct list_head slow_list;
306 struct subchannel_id schid;
309 static LIST_HEAD(slow_subchannels_head);
310 static DEFINE_SPINLOCK(slow_subchannel_lock);
313 css_trigger_slow_path(void)
315 CIO_TRACE_EVENT(4, "slowpath");
319 css_rescan_devices();
323 spin_lock_irq(&slow_subchannel_lock);
324 while (!list_empty(&slow_subchannels_head)) {
325 struct slow_subchannel *slow_sch =
326 list_entry(slow_subchannels_head.next,
327 struct slow_subchannel, slow_list);
329 list_del_init(slow_subchannels_head.next);
330 spin_unlock_irq(&slow_subchannel_lock);
331 css_evaluate_subchannel(slow_sch->schid, 1);
332 spin_lock_irq(&slow_subchannel_lock);
335 spin_unlock_irq(&slow_subchannel_lock);
338 typedef void (*workfunc)(void *);
339 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
340 struct workqueue_struct *slow_path_wq;
343 * Rescan for new devices. FIXME: This is slow.
344 * This function is called when we have lost CRWs due to overflows and we have
345 * to do subchannel housekeeping.
348 css_reiterate_subchannels(void)
350 css_clear_subchannel_slow_list();
355 * Called from the machine check handler for subchannel report words.
358 css_process_crw(int irq)
361 struct subchannel_id mchk_schid;
363 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
366 /* We need to iterate all subchannels anyway. */
369 init_subchannel_id(&mchk_schid);
370 mchk_schid.sch_no = irq;
372 * Since we are always presented with IPI in the CRW, we have to
373 * use stsch() to find out if the subchannel in question has come
376 ret = css_evaluate_subchannel(mchk_schid, 0);
377 if (ret == -EAGAIN) {
378 if (css_enqueue_subchannel_slow(mchk_schid)) {
379 css_clear_subchannel_slow_list();
387 css_generate_pgid(void)
389 /* Let's build our path group ID here. */
390 if (css_characteristics_avail && css_general_characteristics.mcss)
391 global_pgid.cpu_addr = 0x8000;
394 global_pgid.cpu_addr = hard_smp_processor_id();
396 global_pgid.cpu_addr = 0;
399 global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
400 global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
401 global_pgid.tod_high = (__u32) (get_clock() >> 32);
405 * Now that the driver core is running, we can setup our channel subsystem.
406 * The struct subchannel's are created during probing (except for the
407 * static console subchannel).
410 init_channel_subsystem (void)
413 struct subchannel_id schid;
415 if (chsc_determine_css_characteristics() == 0)
416 css_characteristics_avail = 1;
420 if ((ret = bus_register(&css_bus_type)))
422 if ((ret = device_register (&css_bus_device)))
429 init_subchannel_id(&schid);
431 struct subchannel *sch;
433 if (cio_is_console(schid))
434 sch = cio_get_console_subchannel();
436 sch = css_alloc_subchannel(schid);
442 panic("Out of memory in "
443 "init_channel_subsystem\n");
444 /* -ENXIO: no more subchannels. */
451 * We register ALL valid subchannels in ioinfo, even those
452 * that have been present before init_channel_subsystem.
453 * These subchannels can't have been registered yet (kmalloc
454 * not working) so we do it now. This is true e.g. for the
455 * console subchannel.
457 css_register_subchannel(sch);
458 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
462 bus_unregister(&css_bus_type);
468 * find a driver for a subchannel. They identify by the subchannel
469 * type with the exception that the console subchannel driver has its own
470 * subchannel type although the device is an i/o subchannel
473 css_bus_match (struct device *dev, struct device_driver *drv)
475 struct subchannel *sch = container_of (dev, struct subchannel, dev);
476 struct css_driver *driver = container_of (drv, struct css_driver, drv);
478 if (sch->st == driver->subchannel_type)
484 struct bus_type css_bus_type = {
486 .match = &css_bus_match,
489 subsys_initcall(init_channel_subsystem);
492 css_enqueue_subchannel_slow(struct subchannel_id schid)
494 struct slow_subchannel *new_slow_sch;
497 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
500 memset(new_slow_sch, 0, sizeof(struct slow_subchannel));
501 new_slow_sch->schid = schid;
502 spin_lock_irqsave(&slow_subchannel_lock, flags);
503 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
504 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
509 css_clear_subchannel_slow_list(void)
513 spin_lock_irqsave(&slow_subchannel_lock, flags);
514 while (!list_empty(&slow_subchannels_head)) {
515 struct slow_subchannel *slow_sch =
516 list_entry(slow_subchannels_head.next,
517 struct slow_subchannel, slow_list);
519 list_del_init(slow_subchannels_head.next);
522 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
528 css_slow_subchannels_exist(void)
530 return (!list_empty(&slow_subchannels_head));
533 MODULE_LICENSE("GPL");
534 EXPORT_SYMBOL(css_bus_type);
535 EXPORT_SYMBOL_GPL(css_characteristics_avail);