blob: dba632a5f71fb02bd90f255d011411d2f324c854 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
4 * $Revision: 1.85 $
5 *
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)
10 */
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>
17
18#include "css.h"
19#include "cio.h"
20#include "cio_debug.h"
21#include "ioasm.h"
22#include "chsc.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024int need_rescan = 0;
25int css_init_done = 0;
26
27struct pgid global_pgid;
28int css_characteristics_avail = 0;
29
30struct device css_bus_device = {
31 .bus_id = "css0",
32};
33
Cornelia Huckf97a56f2006-01-06 00:19:22 -080034inline int
35for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
36{
37 struct subchannel_id schid;
38 int ret;
39
40 init_subchannel_id(&schid);
41 ret = -ENODEV;
42 do {
43 ret = fn(schid, data);
44 if (ret)
45 break;
46 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
47 return ret;
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -080051css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct subchannel *sch;
54 int ret;
55
56 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
57 if (sch == NULL)
58 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -080059 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (ret < 0) {
61 kfree(sch);
62 return ERR_PTR(ret);
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (sch->st != SUBCHANNEL_TYPE_IO) {
66 /* For now we ignore all non-io subchannels. */
67 kfree(sch);
68 return ERR_PTR(-EINVAL);
69 }
70
71 /*
72 * Set intparm to subchannel address.
73 * This is fine even on 64bit since the subchannel is always located
74 * under 2G.
75 */
76 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
77 ret = cio_modify(sch);
78 if (ret) {
79 kfree(sch);
80 return ERR_PTR(ret);
81 }
82 return sch;
83}
84
85static void
86css_free_subchannel(struct subchannel *sch)
87{
88 if (sch) {
89 /* Reset intparm to zeroes. */
90 sch->schib.pmcw.intparm = 0;
91 cio_modify(sch);
92 kfree(sch);
93 }
94
95}
96
97static void
98css_subchannel_release(struct device *dev)
99{
100 struct subchannel *sch;
101
102 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800103 if (!cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kfree(sch);
105}
106
107extern int css_get_ssd_info(struct subchannel *sch);
108
109static int
110css_register_subchannel(struct subchannel *sch)
111{
112 int ret;
113
114 /* Initialize the subchannel structure */
115 sch->dev.parent = &css_bus_device;
116 sch->dev.bus = &css_bus_type;
117 sch->dev.release = &css_subchannel_release;
118
119 /* make it known to the system */
120 ret = device_register(&sch->dev);
121 if (ret)
122 printk (KERN_WARNING "%s: could not register %s\n",
123 __func__, sch->dev.bus_id);
124 else
125 css_get_ssd_info(sch);
126 return ret;
127}
128
129int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800130css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 int ret;
133 struct subchannel *sch;
134
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800135 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (IS_ERR(sch))
137 return PTR_ERR(sch);
138 ret = css_register_subchannel(sch);
139 if (ret)
140 css_free_subchannel(sch);
141 return ret;
142}
143
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700144static int
145check_subchannel(struct device * dev, void * data)
146{
147 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800148 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700149
150 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800151 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800155get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct device *dev;
158
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700159 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800160 (void *)&schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700162 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static inline int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800167css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct schib schib;
170 int cc;
171
172 cc = stsch(schid, &schib);
173 if (cc)
174 return CIO_GONE;
175 if (!schib.pmcw.dnv)
176 return CIO_GONE;
177 if (sch && sch->schib.pmcw.dnv &&
178 (schib.pmcw.dev != sch->schib.pmcw.dev))
179 return CIO_REVALIDATE;
180 if (sch && !sch->lpm)
181 return CIO_NO_PATH;
182 return CIO_OPER;
183}
184
185static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800186css_evaluate_subchannel(struct subchannel_id schid, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int event, ret, disc;
189 struct subchannel *sch;
190 unsigned long flags;
191
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800192 sch = get_subchannel_by_schid(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 disc = sch ? device_is_disconnected(sch) : 0;
194 if (disc && slow) {
195 if (sch)
196 put_device(&sch->dev);
197 return 0; /* Already processed. */
198 }
199 /*
200 * We've got a machine check, so running I/O won't get an interrupt.
201 * Kill any pending timers.
202 */
203 if (sch)
204 device_kill_pending_timer(sch);
205 if (!disc && !slow) {
206 if (sch)
207 put_device(&sch->dev);
208 return -EAGAIN; /* Will be done on the slow path. */
209 }
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800210 event = css_get_subchannel_status(sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800212 schid.sch_no, event,
213 sch?(disc?"disconnected":"normal"):"unknown",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 slow?"slow":"fast");
215 switch (event) {
216 case CIO_NO_PATH:
217 case CIO_GONE:
218 if (!sch) {
219 /* Never used this subchannel. Ignore. */
220 ret = 0;
221 break;
222 }
223 if (disc && (event == CIO_NO_PATH)) {
224 /*
225 * Uargh, hack again. Because we don't get a machine
226 * check on configure on, our path bookkeeping can
227 * be out of date here (it's fine while we only do
228 * logical varying or get chsc machine checks). We
229 * need to force reprobing or we might miss devices
230 * coming operational again. It won't do harm in real
231 * no path situations.
232 */
233 spin_lock_irqsave(&sch->lock, flags);
234 device_trigger_reprobe(sch);
235 spin_unlock_irqrestore(&sch->lock, flags);
236 ret = 0;
237 break;
238 }
239 if (sch->driver && sch->driver->notify &&
240 sch->driver->notify(&sch->dev, event)) {
241 cio_disable_subchannel(sch);
242 device_set_disconnected(sch);
243 ret = 0;
244 break;
245 }
246 /*
247 * Unregister subchannel.
248 * The device will be killed automatically.
249 */
250 cio_disable_subchannel(sch);
251 device_unregister(&sch->dev);
252 /* Reset intparm to zeroes. */
253 sch->schib.pmcw.intparm = 0;
254 cio_modify(sch);
255 put_device(&sch->dev);
256 ret = 0;
257 break;
258 case CIO_REVALIDATE:
259 /*
260 * Revalidation machine check. Sick.
261 * We don't notify the driver since we have to throw the device
262 * away in any case.
263 */
264 if (!disc) {
265 device_unregister(&sch->dev);
266 /* Reset intparm to zeroes. */
267 sch->schib.pmcw.intparm = 0;
268 cio_modify(sch);
269 put_device(&sch->dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800270 ret = css_probe_device(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else {
272 /*
273 * We can't immediately deregister the disconnected
274 * device since it might block.
275 */
276 spin_lock_irqsave(&sch->lock, flags);
277 device_trigger_reprobe(sch);
278 spin_unlock_irqrestore(&sch->lock, flags);
279 ret = 0;
280 }
281 break;
282 case CIO_OPER:
283 if (disc) {
284 spin_lock_irqsave(&sch->lock, flags);
285 /* Get device operational again. */
286 device_trigger_reprobe(sch);
287 spin_unlock_irqrestore(&sch->lock, flags);
288 }
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800289 ret = sch ? 0 : css_probe_device(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 default:
292 BUG();
293 ret = 0;
294 }
295 return ret;
296}
297
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800298static int
299css_rescan_devices(struct subchannel_id schid, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800301 return css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304struct slow_subchannel {
305 struct list_head slow_list;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800306 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
309static LIST_HEAD(slow_subchannels_head);
310static DEFINE_SPINLOCK(slow_subchannel_lock);
311
312static void
313css_trigger_slow_path(void)
314{
315 CIO_TRACE_EVENT(4, "slowpath");
316
317 if (need_rescan) {
318 need_rescan = 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800319 for_each_subchannel(css_rescan_devices, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return;
321 }
322
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);
328
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);
333 kfree(slow_sch);
334 }
335 spin_unlock_irq(&slow_subchannel_lock);
336}
337
338typedef void (*workfunc)(void *);
339DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
340struct workqueue_struct *slow_path_wq;
341
342/*
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.
346 */
347void
348css_reiterate_subchannels(void)
349{
350 css_clear_subchannel_slow_list();
351 need_rescan = 1;
352}
353
354/*
355 * Called from the machine check handler for subchannel report words.
356 */
357int
358css_process_crw(int irq)
359{
360 int ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800361 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
364
365 if (need_rescan)
366 /* We need to iterate all subchannels anyway. */
367 return -EAGAIN;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800368
369 init_subchannel_id(&mchk_schid);
370 mchk_schid.sch_no = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
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
374 * or gone.
375 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800376 ret = css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (ret == -EAGAIN) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800378 if (css_enqueue_subchannel_slow(mchk_schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 css_clear_subchannel_slow_list();
380 need_rescan = 1;
381 }
382 }
383 return ret;
384}
385
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800386static int __init
387__init_channel_subsystem(struct subchannel_id schid, void *data)
388{
389 struct subchannel *sch;
390 int ret;
391
392 if (cio_is_console(schid))
393 sch = cio_get_console_subchannel();
394 else {
395 sch = css_alloc_subchannel(schid);
396 if (IS_ERR(sch))
397 ret = PTR_ERR(sch);
398 else
399 ret = 0;
400 switch (ret) {
401 case 0:
402 break;
403 case -ENOMEM:
404 panic("Out of memory in init_channel_subsystem\n");
405 /* -ENXIO: no more subchannels. */
406 case -ENXIO:
407 return ret;
408 default:
409 return 0;
410 }
411 }
412 /*
413 * We register ALL valid subchannels in ioinfo, even those
414 * that have been present before init_channel_subsystem.
415 * These subchannels can't have been registered yet (kmalloc
416 * not working) so we do it now. This is true e.g. for the
417 * console subchannel.
418 */
419 css_register_subchannel(sch);
420 return 0;
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static void __init
424css_generate_pgid(void)
425{
426 /* Let's build our path group ID here. */
427 if (css_characteristics_avail && css_general_characteristics.mcss)
428 global_pgid.cpu_addr = 0x8000;
429 else {
430#ifdef CONFIG_SMP
431 global_pgid.cpu_addr = hard_smp_processor_id();
432#else
433 global_pgid.cpu_addr = 0;
434#endif
435 }
436 global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
437 global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
438 global_pgid.tod_high = (__u32) (get_clock() >> 32);
439}
440
441/*
442 * Now that the driver core is running, we can setup our channel subsystem.
443 * The struct subchannel's are created during probing (except for the
444 * static console subchannel).
445 */
446static int __init
447init_channel_subsystem (void)
448{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800449 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (chsc_determine_css_characteristics() == 0)
452 css_characteristics_avail = 1;
453
454 css_generate_pgid();
455
456 if ((ret = bus_register(&css_bus_type)))
457 goto out;
458 if ((ret = device_register (&css_bus_device)))
459 goto out_bus;
460
461 css_init_done = 1;
462
463 ctl_set_bit(6, 28);
464
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800465 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467out_bus:
468 bus_unregister(&css_bus_type);
469out:
470 return ret;
471}
472
473/*
474 * find a driver for a subchannel. They identify by the subchannel
475 * type with the exception that the console subchannel driver has its own
476 * subchannel type although the device is an i/o subchannel
477 */
478static int
479css_bus_match (struct device *dev, struct device_driver *drv)
480{
481 struct subchannel *sch = container_of (dev, struct subchannel, dev);
482 struct css_driver *driver = container_of (drv, struct css_driver, drv);
483
484 if (sch->st == driver->subchannel_type)
485 return 1;
486
487 return 0;
488}
489
490struct bus_type css_bus_type = {
491 .name = "css",
492 .match = &css_bus_match,
493};
494
495subsys_initcall(init_channel_subsystem);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800498css_enqueue_subchannel_slow(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct slow_subchannel *new_slow_sch;
501 unsigned long flags;
502
503 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
504 if (!new_slow_sch)
505 return -ENOMEM;
506 memset(new_slow_sch, 0, sizeof(struct slow_subchannel));
507 new_slow_sch->schid = schid;
508 spin_lock_irqsave(&slow_subchannel_lock, flags);
509 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
510 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
511 return 0;
512}
513
514void
515css_clear_subchannel_slow_list(void)
516{
517 unsigned long flags;
518
519 spin_lock_irqsave(&slow_subchannel_lock, flags);
520 while (!list_empty(&slow_subchannels_head)) {
521 struct slow_subchannel *slow_sch =
522 list_entry(slow_subchannels_head.next,
523 struct slow_subchannel, slow_list);
524
525 list_del_init(slow_subchannels_head.next);
526 kfree(slow_sch);
527 }
528 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
529}
530
531
532
533int
534css_slow_subchannels_exist(void)
535{
536 return (!list_empty(&slow_subchannels_head));
537}
538
539MODULE_LICENSE("GPL");
540EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541EXPORT_SYMBOL_GPL(css_characteristics_avail);