vserver 1.9.5.x5
[linux-2.6.git] / drivers / s390 / cio / css.c
1 /*
2  *  drivers/s390/cio/css.c
3  *  driver for channel subsystem
4  *   $Revision: 1.84 $
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
24 unsigned int highest_subchannel;
25 int need_rescan = 0;
26 int css_init_done = 0;
27
28 struct pgid global_pgid;
29 int css_characteristics_avail = 0;
30
31 struct device css_bus_device = {
32         .bus_id = "css0",
33 };
34
35 static struct subchannel *
36 css_alloc_subchannel(int irq)
37 {
38         struct subchannel *sch;
39         int ret;
40
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);
48         }
49         if (irq > highest_subchannel)
50                 highest_subchannel = irq;
51
52         if (sch->st != SUBCHANNEL_TYPE_IO) {
53                 /* For now we ignore all non-io subchannels. */
54                 kfree(sch);
55                 return ERR_PTR(-EINVAL);
56         }
57
58         /* 
59          * Set intparm to subchannel address.
60          * This is fine even on 64bit since the subchannel is always located
61          * under 2G.
62          */
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);
68         }
69         return sch;
70 }
71
72 static void
73 css_free_subchannel(struct subchannel *sch)
74 {
75         if (sch) {
76                 /* Reset intparm to zeroes. */
77                 sch->schib.pmcw.intparm = 0;
78                 cio_modify(sch);
79                 kfree(sch);
80         }
81         
82 }
83
84 static void
85 css_subchannel_release(struct device *dev)
86 {
87         struct subchannel *sch;
88
89         sch = to_subchannel(dev);
90         if (!cio_is_console(sch->irq))
91                 kfree(sch);
92 }
93
94 extern int css_get_ssd_info(struct subchannel *sch);
95
96 static int
97 css_register_subchannel(struct subchannel *sch)
98 {
99         int ret;
100
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;
105         
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;
114 }
115
116 int
117 css_probe_device(int irq)
118 {
119         int ret;
120         struct subchannel *sch;
121
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;
129 }
130
131 struct subchannel *
132 get_subchannel_by_schid(int irq)
133 {
134         struct subchannel *sch;
135         struct list_head *entry;
136         struct device *dev;
137
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;
152         }
153         up_read(&css_bus_type.subsys.rwsem);
154         put_bus(&css_bus_type);
155
156         return sch;
157 }
158
159 static inline int
160 css_get_subchannel_status(struct subchannel *sch, int schid)
161 {
162         struct schib schib;
163         int cc;
164
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;
176 }
177         
178 static int
179 css_evaluate_subchannel(int irq, int slow)
180 {
181         int event, ret, disc;
182         struct subchannel *sch;
183
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. */
190         }
191         /*
192          * We've got a machine check, so running I/O won't get an interrupt.
193          * Kill any pending timers.
194          */
195         if (sch)
196                 device_kill_pending_timer(sch);
197         if (!disc && !slow) {
198                 if (sch)
199                         put_device(&sch->dev);
200                 return -EAGAIN; /* Will be done on the slow path. */
201         }
202         event = css_get_subchannel_status(sch, irq);
203         CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
204                       irq, event, sch?(disc?"disconnected":"normal"):"unknown",
205                       slow?"slow":"fast");
206         switch (event) {
207         case CIO_NO_PATH:
208         case CIO_GONE:
209                 if (!sch) {
210                         /* Never used this subchannel. Ignore. */
211                         ret = 0;
212                         break;
213                 }
214                 if (disc && (event == CIO_NO_PATH)) {
215                         /*
216                          * Uargh, hack again. Because we don't get a machine
217                          * check on configure on, our path bookkeeping can
218                          * be out of date here (it's fine while we only do
219                          * logical varying or get chsc machine checks). We
220                          * need to force reprobing or we might miss devices
221                          * coming operational again. It won't do harm in real
222                          * no path situations.
223                          */
224                         device_trigger_reprobe(sch);
225                         ret = 0;
226                         break;
227                 }
228                 if (sch->driver && sch->driver->notify &&
229                     sch->driver->notify(&sch->dev, event)) {
230                         cio_disable_subchannel(sch);
231                         device_set_disconnected(sch);
232                         ret = 0;
233                         break;
234                 }
235                 /*
236                  * Unregister subchannel.
237                  * The device will be killed automatically.
238                  */
239                 cio_disable_subchannel(sch);
240                 device_unregister(&sch->dev);
241                 /* Reset intparm to zeroes. */
242                 sch->schib.pmcw.intparm = 0;
243                 cio_modify(sch);
244                 put_device(&sch->dev);
245                 ret = 0;
246                 break;
247         case CIO_REVALIDATE:
248                 /* 
249                  * Revalidation machine check. Sick.
250                  * We don't notify the driver since we have to throw the device
251                  * away in any case.
252                  */
253                 if (!disc) {
254                         device_unregister(&sch->dev);
255                         /* Reset intparm to zeroes. */
256                         sch->schib.pmcw.intparm = 0;
257                         cio_modify(sch);
258                         put_device(&sch->dev);
259                         ret = css_probe_device(irq);
260                 } else {
261                         /*
262                          * We can't immediately deregister the disconnected
263                          * device since it might block.
264                          */
265                         device_trigger_reprobe(sch);
266                         ret = 0;
267                 }
268                 break;
269         case CIO_OPER:
270                 if (disc)
271                         /* Get device operational again. */
272                         device_trigger_reprobe(sch);
273                 ret = sch ? 0 : css_probe_device(irq);
274                 break;
275         default:
276                 BUG();
277                 ret = 0;
278         }
279         return ret;
280 }
281
282 static void
283 css_rescan_devices(void)
284 {
285         int irq, ret;
286
287         for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
288                 ret = css_evaluate_subchannel(irq, 1);
289                 /* No more memory. It doesn't make sense to continue. No
290                  * panic because this can happen in midflight and just
291                  * because we can't use a new device is no reason to crash
292                  * the system. */
293                 if (ret == -ENOMEM)
294                         break;
295                 /* -ENXIO indicates that there are no more subchannels. */
296                 if (ret == -ENXIO)
297                         break;
298         }
299 }
300
301 struct slow_subchannel {
302         struct list_head slow_list;
303         unsigned long schid;
304 };
305
306 static LIST_HEAD(slow_subchannels_head);
307 static DEFINE_SPINLOCK(slow_subchannel_lock);
308
309 static void
310 css_trigger_slow_path(void)
311 {
312         CIO_TRACE_EVENT(4, "slowpath");
313
314         if (need_rescan) {
315                 need_rescan = 0;
316                 css_rescan_devices();
317                 return;
318         }
319
320         spin_lock_irq(&slow_subchannel_lock);
321         while (!list_empty(&slow_subchannels_head)) {
322                 struct slow_subchannel *slow_sch =
323                         list_entry(slow_subchannels_head.next,
324                                    struct slow_subchannel, slow_list);
325
326                 list_del_init(slow_subchannels_head.next);
327                 spin_unlock_irq(&slow_subchannel_lock);
328                 css_evaluate_subchannel(slow_sch->schid, 1);
329                 spin_lock_irq(&slow_subchannel_lock);
330                 kfree(slow_sch);
331         }
332         spin_unlock_irq(&slow_subchannel_lock);
333 }
334
335 typedef void (*workfunc)(void *);
336 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
337 struct workqueue_struct *slow_path_wq;
338
339 /*
340  * Rescan for new devices. FIXME: This is slow.
341  * This function is called when we have lost CRWs due to overflows and we have
342  * to do subchannel housekeeping.
343  */
344 void
345 css_reiterate_subchannels(void)
346 {
347         css_clear_subchannel_slow_list();
348         need_rescan = 1;
349 }
350
351 /*
352  * Called from the machine check handler for subchannel report words.
353  */
354 int
355 css_process_crw(int irq)
356 {
357         int ret;
358
359         CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
360
361         if (need_rescan)
362                 /* We need to iterate all subchannels anyway. */
363                 return -EAGAIN;
364         /* 
365          * Since we are always presented with IPI in the CRW, we have to
366          * use stsch() to find out if the subchannel in question has come
367          * or gone.
368          */
369         ret = css_evaluate_subchannel(irq, 0);
370         if (ret == -EAGAIN) {
371                 if (css_enqueue_subchannel_slow(irq)) {
372                         css_clear_subchannel_slow_list();
373                         need_rescan = 1;
374                 }
375         }
376         return ret;
377 }
378
379 static void __init
380 css_generate_pgid(void)
381 {
382         /* Let's build our path group ID here. */
383         if (css_characteristics_avail && css_general_characteristics.mcss)
384                 global_pgid.cpu_addr = 0x8000;
385         else {
386 #ifdef CONFIG_SMP
387                 global_pgid.cpu_addr = hard_smp_processor_id();
388 #else
389                 global_pgid.cpu_addr = 0;
390 #endif
391         }
392         global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
393         global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
394         global_pgid.tod_high = (__u32) (get_clock() >> 32);
395 }
396
397 /*
398  * Now that the driver core is running, we can setup our channel subsystem.
399  * The struct subchannel's are created during probing (except for the
400  * static console subchannel).
401  */
402 static int __init
403 init_channel_subsystem (void)
404 {
405         int ret, irq;
406
407         if (chsc_determine_css_characteristics() == 0)
408                 css_characteristics_avail = 1;
409
410         css_generate_pgid();
411
412         if ((ret = bus_register(&css_bus_type)))
413                 goto out;
414         if ((ret = device_register (&css_bus_device)))
415                 goto out_bus;
416
417         css_init_done = 1;
418
419         ctl_set_bit(6, 28);
420
421         for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
422                 struct subchannel *sch;
423
424                 if (cio_is_console(irq))
425                         sch = cio_get_console_subchannel();
426                 else {
427                         sch = css_alloc_subchannel(irq);
428                         if (IS_ERR(sch))
429                                 ret = PTR_ERR(sch);
430                         else
431                                 ret = 0;
432                         if (ret == -ENOMEM)
433                                 panic("Out of memory in "
434                                       "init_channel_subsystem\n");
435                         /* -ENXIO: no more subchannels. */
436                         if (ret == -ENXIO)
437                                 break;
438                         if (ret)
439                                 continue;
440                 }
441                 /*
442                  * We register ALL valid subchannels in ioinfo, even those
443                  * that have been present before init_channel_subsystem.
444                  * These subchannels can't have been registered yet (kmalloc
445                  * not working) so we do it now. This is true e.g. for the
446                  * console subchannel.
447                  */
448                 css_register_subchannel(sch);
449         }
450         return 0;
451
452 out_bus:
453         bus_unregister(&css_bus_type);
454 out:
455         return ret;
456 }
457
458 /*
459  * find a driver for a subchannel. They identify by the subchannel
460  * type with the exception that the console subchannel driver has its own
461  * subchannel type although the device is an i/o subchannel
462  */
463 static int
464 css_bus_match (struct device *dev, struct device_driver *drv)
465 {
466         struct subchannel *sch = container_of (dev, struct subchannel, dev);
467         struct css_driver *driver = container_of (drv, struct css_driver, drv);
468
469         if (sch->st == driver->subchannel_type)
470                 return 1;
471
472         return 0;
473 }
474
475 struct bus_type css_bus_type = {
476         .name  = "css",
477         .match = &css_bus_match,
478 };
479
480 subsys_initcall(init_channel_subsystem);
481
482 /*
483  * Register root devices for some drivers. The release function must not be
484  * in the device drivers, so we do it here.
485  */
486 static void
487 s390_root_dev_release(struct device *dev)
488 {
489         kfree(dev);
490 }
491
492 struct device *
493 s390_root_dev_register(const char *name)
494 {
495         struct device *dev;
496         int ret;
497
498         if (!strlen(name))
499                 return ERR_PTR(-EINVAL);
500         dev = kmalloc(sizeof(struct device), GFP_KERNEL);
501         if (!dev)
502                 return ERR_PTR(-ENOMEM);
503         memset(dev, 0, sizeof(struct device));
504         strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE));
505         dev->release = s390_root_dev_release;
506         ret = device_register(dev);
507         if (ret) {
508                 kfree(dev);
509                 return ERR_PTR(ret);
510         }
511         return dev;
512 }
513
514 void
515 s390_root_dev_unregister(struct device *dev)
516 {
517         if (dev)
518                 device_unregister(dev);
519 }
520
521 int
522 css_enqueue_subchannel_slow(unsigned long schid)
523 {
524         struct slow_subchannel *new_slow_sch;
525         unsigned long flags;
526
527         new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
528         if (!new_slow_sch)
529                 return -ENOMEM;
530         memset(new_slow_sch, 0, sizeof(struct slow_subchannel));
531         new_slow_sch->schid = schid;
532         spin_lock_irqsave(&slow_subchannel_lock, flags);
533         list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
534         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
535         return 0;
536 }
537
538 void
539 css_clear_subchannel_slow_list(void)
540 {
541         unsigned long flags;
542
543         spin_lock_irqsave(&slow_subchannel_lock, flags);
544         while (!list_empty(&slow_subchannels_head)) {
545                 struct slow_subchannel *slow_sch =
546                         list_entry(slow_subchannels_head.next,
547                                    struct slow_subchannel, slow_list);
548
549                 list_del_init(slow_subchannels_head.next);
550                 kfree(slow_sch);
551         }
552         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
553 }
554
555
556
557 int
558 css_slow_subchannels_exist(void)
559 {
560         return (!list_empty(&slow_subchannels_head));
561 }
562
563 MODULE_LICENSE("GPL");
564 EXPORT_SYMBOL(css_bus_type);
565 EXPORT_SYMBOL(s390_root_dev_register);
566 EXPORT_SYMBOL(s390_root_dev_unregister);
567 EXPORT_SYMBOL_GPL(css_characteristics_avail);