initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / s390 / cio / css.c
blobdd12416e82df0da81b67d6e9807b1f1087268b1e
1 /*
2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
4 * $Revision: 1.82 $
6 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * IBM Corporation
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>
18 #include "css.h"
19 #include "cio.h"
20 #include "cio_debug.h"
21 #include "ioasm.h"
22 #include "chsc.h"
24 unsigned int highest_subchannel;
25 int need_rescan = 0;
26 int css_init_done = 0;
28 struct pgid global_pgid;
29 int css_characteristics_avail = 0;
31 struct device css_bus_device = {
32 .bus_id = "css0",
35 static struct subchannel *
36 css_alloc_subchannel(int irq)
38 struct subchannel *sch;
39 int ret;
41 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
42 if (sch == NULL)
43 return ERR_PTR(-ENOMEM);
44 ret = cio_validate_subchannel (sch, irq);
45 if (ret < 0) {
46 kfree(sch);
47 return ERR_PTR(ret);
49 if (irq > highest_subchannel)
50 highest_subchannel = irq;
52 if (sch->st != SUBCHANNEL_TYPE_IO) {
53 /* For now we ignore all non-io subchannels. */
54 kfree(sch);
55 return ERR_PTR(-EINVAL);
58 /*
59 * Set intparm to subchannel address.
60 * This is fine even on 64bit since the subchannel is always located
61 * under 2G.
63 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
64 ret = cio_modify(sch);
65 if (ret) {
66 kfree(sch);
67 return ERR_PTR(ret);
69 return sch;
72 static void
73 css_free_subchannel(struct subchannel *sch)
75 if (sch) {
76 /* Reset intparm to zeroes. */
77 sch->schib.pmcw.intparm = 0;
78 cio_modify(sch);
79 kfree(sch);
84 static void
85 css_subchannel_release(struct device *dev)
87 struct subchannel *sch;
89 sch = to_subchannel(dev);
90 if (!cio_is_console(sch->irq))
91 kfree(sch);
94 extern int css_get_ssd_info(struct subchannel *sch);
96 static int
97 css_register_subchannel(struct subchannel *sch)
99 int ret;
101 /* Initialize the subchannel structure */
102 sch->dev.parent = &css_bus_device;
103 sch->dev.bus = &css_bus_type;
104 sch->dev.release = &css_subchannel_release;
106 /* make it known to the system */
107 ret = device_register(&sch->dev);
108 if (ret)
109 printk (KERN_WARNING "%s: could not register %s\n",
110 __func__, sch->dev.bus_id);
111 else
112 css_get_ssd_info(sch);
113 return ret;
117 css_probe_device(int irq)
119 int ret;
120 struct subchannel *sch;
122 sch = css_alloc_subchannel(irq);
123 if (IS_ERR(sch))
124 return PTR_ERR(sch);
125 ret = css_register_subchannel(sch);
126 if (ret)
127 css_free_subchannel(sch);
128 return ret;
131 struct subchannel *
132 get_subchannel_by_schid(int irq)
134 struct subchannel *sch;
135 struct list_head *entry;
136 struct device *dev;
138 if (!get_bus(&css_bus_type))
139 return NULL;
140 down_read(&css_bus_type.subsys.rwsem);
141 sch = NULL;
142 list_for_each(entry, &css_bus_type.devices.list) {
143 dev = get_device(container_of(entry,
144 struct device, bus_list));
145 if (!dev)
146 continue;
147 sch = to_subchannel(dev);
148 if (sch->irq == irq)
149 break;
150 put_device(dev);
151 sch = NULL;
153 up_read(&css_bus_type.subsys.rwsem);
154 put_bus(&css_bus_type);
156 return sch;
159 static inline int
160 css_get_subchannel_status(struct subchannel *sch, int schid)
162 struct schib schib;
163 int cc;
165 cc = stsch(schid, &schib);
166 if (cc)
167 return CIO_GONE;
168 if (!schib.pmcw.dnv)
169 return CIO_GONE;
170 if (sch && sch->schib.pmcw.dnv &&
171 (schib.pmcw.dev != sch->schib.pmcw.dev))
172 return CIO_REVALIDATE;
173 if (sch && !sch->lpm)
174 return CIO_NO_PATH;
175 return CIO_OPER;
178 static int
179 css_evaluate_subchannel(int irq, int slow)
181 int event, ret, disc;
182 struct subchannel *sch;
184 sch = get_subchannel_by_schid(irq);
185 disc = sch ? device_is_disconnected(sch) : 0;
186 if (disc && slow) {
187 if (sch)
188 put_device(&sch->dev);
189 return 0; /* Already processed. */
191 if (!disc && !slow) {
192 if (sch)
193 put_device(&sch->dev);
194 return -EAGAIN; /* Will be done on the slow path. */
196 event = css_get_subchannel_status(sch, irq);
197 CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
198 irq, event, sch?(disc?"disconnected":"normal"):"unknown",
199 slow?"slow":"fast");
200 switch (event) {
201 case CIO_NO_PATH:
202 case CIO_GONE:
203 if (!sch) {
204 /* Never used this subchannel. Ignore. */
205 ret = 0;
206 break;
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 device_trigger_reprobe(sch);
219 ret = 0;
220 break;
222 if (sch->driver && sch->driver->notify &&
223 sch->driver->notify(&sch->dev, event)) {
224 cio_disable_subchannel(sch);
225 device_set_disconnected(sch);
226 ret = 0;
227 break;
230 * Unregister subchannel.
231 * The device will be killed automatically.
233 cio_disable_subchannel(sch);
234 device_unregister(&sch->dev);
235 /* Reset intparm to zeroes. */
236 sch->schib.pmcw.intparm = 0;
237 cio_modify(sch);
238 put_device(&sch->dev);
239 ret = 0;
240 break;
241 case CIO_REVALIDATE:
243 * Revalidation machine check. Sick.
244 * We don't notify the driver since we have to throw the device
245 * away in any case.
247 if (!disc) {
248 device_unregister(&sch->dev);
249 /* Reset intparm to zeroes. */
250 sch->schib.pmcw.intparm = 0;
251 cio_modify(sch);
252 put_device(&sch->dev);
253 ret = css_probe_device(irq);
254 } else {
256 * We can't immediately deregister the disconnected
257 * device since it might block.
259 device_trigger_reprobe(sch);
260 ret = 0;
262 break;
263 case CIO_OPER:
264 if (disc)
265 /* Get device operational again. */
266 device_trigger_reprobe(sch);
267 ret = sch ? 0 : css_probe_device(irq);
268 break;
269 default:
270 BUG();
271 ret = 0;
273 return ret;
276 static void
277 css_rescan_devices(void)
279 int irq, ret;
281 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
282 ret = css_evaluate_subchannel(irq, 1);
283 /* No more memory. It doesn't make sense to continue. No
284 * panic because this can happen in midflight and just
285 * because we can't use a new device is no reason to crash
286 * the system. */
287 if (ret == -ENOMEM)
288 break;
289 /* -ENXIO indicates that there are no more subchannels. */
290 if (ret == -ENXIO)
291 break;
295 struct slow_subchannel {
296 struct list_head slow_list;
297 unsigned long schid;
300 static LIST_HEAD(slow_subchannels_head);
301 static spinlock_t slow_subchannel_lock = SPIN_LOCK_UNLOCKED;
303 static void
304 css_trigger_slow_path(void)
306 CIO_TRACE_EVENT(4, "slowpath");
308 if (need_rescan) {
309 need_rescan = 0;
310 css_rescan_devices();
311 return;
314 spin_lock_irq(&slow_subchannel_lock);
315 while (!list_empty(&slow_subchannels_head)) {
316 struct slow_subchannel *slow_sch =
317 list_entry(slow_subchannels_head.next,
318 struct slow_subchannel, slow_list);
320 list_del_init(slow_subchannels_head.next);
321 spin_unlock_irq(&slow_subchannel_lock);
322 css_evaluate_subchannel(slow_sch->schid, 1);
323 spin_lock_irq(&slow_subchannel_lock);
324 kfree(slow_sch);
326 spin_unlock_irq(&slow_subchannel_lock);
329 typedef void (*workfunc)(void *);
330 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
331 struct workqueue_struct *slow_path_wq;
334 * Rescan for new devices. FIXME: This is slow.
335 * This function is called when we have lost CRWs due to overflows and we have
336 * to do subchannel housekeeping.
338 void
339 css_reiterate_subchannels(void)
341 css_clear_subchannel_slow_list();
342 need_rescan = 1;
346 * Called from the machine check handler for subchannel report words.
349 css_process_crw(int irq)
351 int ret;
353 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
355 if (need_rescan)
356 /* We need to iterate all subchannels anyway. */
357 return -EAGAIN;
359 * Since we are always presented with IPI in the CRW, we have to
360 * use stsch() to find out if the subchannel in question has come
361 * or gone.
363 ret = css_evaluate_subchannel(irq, 0);
364 if (ret == -EAGAIN) {
365 if (css_enqueue_subchannel_slow(irq)) {
366 css_clear_subchannel_slow_list();
367 need_rescan = 1;
370 return ret;
373 static void __init
374 css_generate_pgid(void)
376 /* Let's build our path group ID here. */
377 if (css_characteristics_avail && css_general_characteristics.mcss)
378 global_pgid.cpu_addr = 0x8000;
379 else {
380 #ifdef CONFIG_SMP
381 global_pgid.cpu_addr = hard_smp_processor_id();
382 #else
383 global_pgid.cpu_addr = 0;
384 #endif
386 global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
387 global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
388 global_pgid.tod_high = (__u32) (get_clock() >> 32);
392 * Now that the driver core is running, we can setup our channel subsystem.
393 * The struct subchannel's are created during probing (except for the
394 * static console subchannel).
396 static int __init
397 init_channel_subsystem (void)
399 int ret, irq;
401 if (chsc_determine_css_characteristics() == 0)
402 css_characteristics_avail = 1;
404 css_generate_pgid();
406 if ((ret = bus_register(&css_bus_type)))
407 goto out;
408 if ((ret = device_register (&css_bus_device)))
409 goto out_bus;
411 css_init_done = 1;
413 ctl_set_bit(6, 28);
415 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
416 struct subchannel *sch;
418 if (cio_is_console(irq))
419 sch = cio_get_console_subchannel();
420 else {
421 sch = css_alloc_subchannel(irq);
422 if (IS_ERR(sch))
423 ret = PTR_ERR(sch);
424 else
425 ret = 0;
426 if (ret == -ENOMEM)
427 panic("Out of memory in "
428 "init_channel_subsystem\n");
429 /* -ENXIO: no more subchannels. */
430 if (ret == -ENXIO)
431 break;
432 if (ret)
433 continue;
436 * We register ALL valid subchannels in ioinfo, even those
437 * that have been present before init_channel_subsystem.
438 * These subchannels can't have been registered yet (kmalloc
439 * not working) so we do it now. This is true e.g. for the
440 * console subchannel.
442 css_register_subchannel(sch);
444 return 0;
446 out_bus:
447 bus_unregister(&css_bus_type);
448 out:
449 return ret;
453 * find a driver for a subchannel. They identify by the subchannel
454 * type with the exception that the console subchannel driver has its own
455 * subchannel type although the device is an i/o subchannel
457 static int
458 css_bus_match (struct device *dev, struct device_driver *drv)
460 struct subchannel *sch = container_of (dev, struct subchannel, dev);
461 struct css_driver *driver = container_of (drv, struct css_driver, drv);
463 if (sch->st == driver->subchannel_type)
464 return 1;
466 return 0;
469 struct bus_type css_bus_type = {
470 .name = "css",
471 .match = &css_bus_match,
474 subsys_initcall(init_channel_subsystem);
477 * Register root devices for some drivers. The release function must not be
478 * in the device drivers, so we do it here.
480 static void
481 s390_root_dev_release(struct device *dev)
483 kfree(dev);
486 struct device *
487 s390_root_dev_register(const char *name)
489 struct device *dev;
490 int ret;
492 if (!strlen(name))
493 return ERR_PTR(-EINVAL);
494 dev = kmalloc(sizeof(struct device), GFP_KERNEL);
495 if (!dev)
496 return ERR_PTR(-ENOMEM);
497 memset(dev, 0, sizeof(struct device));
498 strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE));
499 dev->release = s390_root_dev_release;
500 ret = device_register(dev);
501 if (ret) {
502 kfree(dev);
503 return ERR_PTR(ret);
505 return dev;
508 void
509 s390_root_dev_unregister(struct device *dev)
511 if (dev)
512 device_unregister(dev);
516 css_enqueue_subchannel_slow(unsigned long schid)
518 struct slow_subchannel *new_slow_sch;
519 unsigned long flags;
521 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
522 if (!new_slow_sch)
523 return -ENOMEM;
524 memset(new_slow_sch, sizeof(struct slow_subchannel), 0);
525 new_slow_sch->schid = schid;
526 spin_lock_irqsave(&slow_subchannel_lock, flags);
527 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
528 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
529 return 0;
532 void
533 css_clear_subchannel_slow_list(void)
535 unsigned long flags;
537 spin_lock_irqsave(&slow_subchannel_lock, flags);
538 while (!list_empty(&slow_subchannels_head)) {
539 struct slow_subchannel *slow_sch =
540 list_entry(slow_subchannels_head.next,
541 struct slow_subchannel, slow_list);
543 list_del_init(slow_subchannels_head.next);
544 kfree(slow_sch);
546 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
552 css_slow_subchannels_exist(void)
554 return (!list_empty(&slow_subchannels_head));
557 MODULE_LICENSE("GPL");
558 EXPORT_SYMBOL(css_bus_type);
559 EXPORT_SYMBOL(s390_root_dev_register);
560 EXPORT_SYMBOL(s390_root_dev_unregister);
561 EXPORT_SYMBOL_GPL(css_characteristics_avail);