This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / s390 / cio / css.c
1 /*
2  *  drivers/s390/cio/css.c
3  *  driver for channel subsystem
4  *   $Revision: 1.74 $
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
23 unsigned int highest_subchannel;
24 int need_rescan = 0;
25 int css_init_done = 0;
26
27 struct device css_bus_device = {
28         .bus_id = "css0",
29 };
30
31 static struct subchannel *
32 css_alloc_subchannel(int irq)
33 {
34         struct subchannel *sch;
35         int ret;
36
37         sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
38         if (sch == NULL)
39                 return ERR_PTR(-ENOMEM);
40         ret = cio_validate_subchannel (sch, irq);
41         if (ret < 0) {
42                 kfree(sch);
43                 return ERR_PTR(ret);
44         }
45         if (irq > highest_subchannel)
46                 highest_subchannel = irq;
47
48         if (sch->st != SUBCHANNEL_TYPE_IO) {
49                 /* For now we ignore all non-io subchannels. */
50                 kfree(sch);
51                 return ERR_PTR(-EINVAL);
52         }
53
54         /* 
55          * Set intparm to subchannel address.
56          * This is fine even on 64bit since the subchannel is always located
57          * under 2G.
58          */
59         sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
60         ret = cio_modify(sch);
61         if (ret) {
62                 kfree(sch);
63                 return ERR_PTR(ret);
64         }
65         return sch;
66 }
67
68 static void
69 css_free_subchannel(struct subchannel *sch)
70 {
71         if (sch) {
72                 /* Reset intparm to zeroes. */
73                 sch->schib.pmcw.intparm = 0;
74                 cio_modify(sch);
75                 kfree(sch);
76         }
77         
78 }
79
80 static void
81 css_subchannel_release(struct device *dev)
82 {
83         struct subchannel *sch;
84
85         sch = to_subchannel(dev);
86         if (!cio_is_console(sch->irq))
87                 kfree(sch);
88 }
89
90 extern int css_get_ssd_info(struct subchannel *sch);
91
92 static int
93 css_register_subchannel(struct subchannel *sch)
94 {
95         int ret;
96
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;
101         
102         /* make it known to the system */
103         ret = device_register(&sch->dev);
104         if (ret)
105                 printk (KERN_WARNING "%s: could not register %s\n",
106                         __func__, sch->dev.bus_id);
107         else
108                 css_get_ssd_info(sch);
109         return ret;
110 }
111
112 int
113 css_probe_device(int irq)
114 {
115         int ret;
116         struct subchannel *sch;
117
118         sch = css_alloc_subchannel(irq);
119         if (IS_ERR(sch))
120                 return PTR_ERR(sch);
121         ret = css_register_subchannel(sch);
122         if (ret)
123                 css_free_subchannel(sch);
124         return ret;
125 }
126
127 struct subchannel *
128 get_subchannel_by_schid(int irq)
129 {
130         struct subchannel *sch;
131         struct list_head *entry;
132         struct device *dev;
133
134         if (!get_bus(&css_bus_type))
135                 return NULL;
136         down_read(&css_bus_type.subsys.rwsem);
137         sch = NULL;
138         list_for_each(entry, &css_bus_type.devices.list) {
139                 dev = get_device(container_of(entry,
140                                               struct device, bus_list));
141                 if (!dev)
142                         continue;
143                 sch = to_subchannel(dev);
144                 if (sch->irq == irq)
145                         break;
146                 put_device(dev);
147                 sch = NULL;
148         }
149         up_read(&css_bus_type.subsys.rwsem);
150         put_bus(&css_bus_type);
151
152         return sch;
153 }
154
155 static inline int
156 css_get_subchannel_status(struct subchannel *sch, int schid)
157 {
158         struct schib schib;
159         int cc;
160
161         cc = stsch(schid, &schib);
162         if (cc)
163                 return CIO_GONE;
164         if (!schib.pmcw.dnv)
165                 return CIO_GONE;
166         if (sch && sch->schib.pmcw.dnv &&
167             (schib.pmcw.dev != sch->schib.pmcw.dev))
168                 return CIO_REVALIDATE;
169         return CIO_OPER;
170 }
171         
172 static inline int
173 css_evaluate_subchannel(int irq, int slow)
174 {
175         int event, ret, disc;
176         struct subchannel *sch;
177
178         sch = get_subchannel_by_schid(irq);
179         disc = sch ? device_is_disconnected(sch) : 0;
180         if (disc && slow) {
181                 if (sch)
182                         put_device(&sch->dev);
183                 return 0; /* Already processed. */
184         }
185         if (!disc && !slow) {
186                 if (sch)
187                         put_device(&sch->dev);
188                 return -EAGAIN; /* Will be done on the slow path. */
189         }
190         event = css_get_subchannel_status(sch, irq);
191         switch (event) {
192         case CIO_GONE:
193                 if (!sch) {
194                         /* Never used this subchannel. Ignore. */
195                         ret = 0;
196                         break;
197                 }
198                 if (sch->driver && sch->driver->notify &&
199                     sch->driver->notify(&sch->dev, CIO_GONE)) {
200                         device_set_disconnected(sch);
201                         ret = 0;
202                         break;
203                 }
204                 /*
205                  * Unregister subchannel.
206                  * The device will be killed automatically.
207                  */
208                 device_unregister(&sch->dev);
209                 /* Reset intparm to zeroes. */
210                 sch->schib.pmcw.intparm = 0;
211                 cio_modify(sch);
212                 put_device(&sch->dev);
213                 ret = 0;
214                 break;
215         case CIO_REVALIDATE:
216                 /* 
217                  * Revalidation machine check. Sick.
218                  * We don't notify the driver since we have to throw the device
219                  * away in any case.
220                  */
221                 if (!disc) {
222                         device_unregister(&sch->dev);
223                         /* Reset intparm to zeroes. */
224                         sch->schib.pmcw.intparm = 0;
225                         cio_modify(sch);
226                         put_device(&sch->dev);
227                         ret = css_probe_device(irq);
228                 } else {
229                         /*
230                          * We can't immediately deregister the disconnected
231                          * device since it might block.
232                          */
233                         device_trigger_reprobe(sch);
234                         ret = 0;
235                 }
236                 break;
237         case CIO_OPER:
238                 if (disc)
239                         /* Get device operational again. */
240                         device_trigger_reprobe(sch);
241                 ret = sch ? 0 : css_probe_device(irq);
242                 break;
243         default:
244                 BUG();
245                 ret = 0;
246         }
247         return ret;
248 }
249
250 static void
251 css_rescan_devices(void)
252 {
253         int irq, ret;
254
255         for (irq = 0; irq <= __MAX_SUBCHANNELS; irq++) {
256                 ret = css_evaluate_subchannel(irq, 1);
257                 /* No more memory. It doesn't make sense to continue. No
258                  * panic because this can happen in midflight and just
259                  * because we can't use a new device is no reason to crash
260                  * the system. */
261                 if (ret == -ENOMEM)
262                         break;
263                 /* -ENXIO indicates that there are no more subchannels. */
264                 if (ret == -ENXIO)
265                         break;
266         }
267 }
268
269 static void
270 css_evaluate_slow_subchannel(unsigned long schid)
271 {
272         css_evaluate_subchannel(schid, 1);
273 }
274
275 void
276 css_trigger_slow_path(void)
277 {
278         if (need_rescan) {
279                 need_rescan = 0;
280                 css_rescan_devices();
281                 return;
282         }
283         css_walk_subchannel_slow_list(css_evaluate_slow_subchannel);
284 }
285
286 /*
287  * Rescan for new devices. FIXME: This is slow.
288  * This function is called when we have lost CRWs due to overflows and we have
289  * to do subchannel housekeeping.
290  */
291 void
292 css_reiterate_subchannels(void)
293 {
294         css_clear_subchannel_slow_list();
295         need_rescan = 1;
296 }
297
298 /*
299  * Called from the machine check handler for subchannel report words.
300  */
301 int
302 css_process_crw(int irq)
303 {
304         int ret;
305
306         CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
307
308         if (need_rescan)
309                 /* We need to iterate all subchannels anyway. */
310                 return -EAGAIN;
311         /* 
312          * Since we are always presented with IPI in the CRW, we have to
313          * use stsch() to find out if the subchannel in question has come
314          * or gone.
315          */
316         ret = css_evaluate_subchannel(irq, 0);
317         if (ret == -EAGAIN) {
318                 if (css_enqueue_subchannel_slow(irq)) {
319                         css_clear_subchannel_slow_list();
320                         need_rescan = 1;
321                 }
322         }
323         return ret;
324 }
325
326 /*
327  * some of the initialization has already been done from init_IRQ(),
328  * here we do the rest now that the driver core is running.
329  * The struct subchannel's are created during probing (except for the
330  * static console subchannel).
331  */
332 static int __init
333 init_channel_subsystem (void)
334 {
335         int ret, irq;
336
337         if ((ret = bus_register(&css_bus_type)))
338                 goto out;
339         if ((ret = device_register (&css_bus_device)))
340                 goto out_bus;
341
342         css_init_done = 1;
343
344         ctl_set_bit(6, 28);
345
346         for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
347                 struct subchannel *sch;
348
349                 if (cio_is_console(irq))
350                         sch = cio_get_console_subchannel();
351                 else {
352                         sch = css_alloc_subchannel(irq);
353                         if (IS_ERR(sch))
354                                 ret = PTR_ERR(sch);
355                         else
356                                 ret = 0;
357                         if (ret == -ENOMEM)
358                                 panic("Out of memory in "
359                                       "init_channel_subsystem\n");
360                         /* -ENXIO: no more subchannels. */
361                         if (ret == -ENXIO)
362                                 break;
363                         if (ret)
364                                 continue;
365                 }
366                 /*
367                  * We register ALL valid subchannels in ioinfo, even those
368                  * that have been present before init_channel_subsystem.
369                  * These subchannels can't have been registered yet (kmalloc
370                  * not working) so we do it now. This is true e.g. for the
371                  * console subchannel.
372                  */
373                 css_register_subchannel(sch);
374         }
375         return 0;
376
377 out_bus:
378         bus_unregister(&css_bus_type);
379 out:
380         return ret;
381 }
382
383 /*
384  * find a driver for a subchannel. They identify by the subchannel
385  * type with the exception that the console subchannel driver has its own
386  * subchannel type although the device is an i/o subchannel
387  */
388 static int
389 css_bus_match (struct device *dev, struct device_driver *drv)
390 {
391         struct subchannel *sch = container_of (dev, struct subchannel, dev);
392         struct css_driver *driver = container_of (drv, struct css_driver, drv);
393
394         if (sch->st == driver->subchannel_type)
395                 return 1;
396
397         return 0;
398 }
399
400 struct bus_type css_bus_type = {
401         .name  = "css",
402         .match = &css_bus_match,
403 };
404
405 subsys_initcall(init_channel_subsystem);
406
407 /*
408  * Register root devices for some drivers. The release function must not be
409  * in the device drivers, so we do it here.
410  */
411 static void
412 s390_root_dev_release(struct device *dev)
413 {
414         kfree(dev);
415 }
416
417 struct device *
418 s390_root_dev_register(const char *name)
419 {
420         struct device *dev;
421         int ret;
422
423         if (!strlen(name))
424                 return ERR_PTR(-EINVAL);
425         dev = kmalloc(sizeof(struct device), GFP_KERNEL);
426         if (!dev)
427                 return ERR_PTR(-ENOMEM);
428         memset(dev, 0, sizeof(struct device));
429         strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE));
430         dev->release = s390_root_dev_release;
431         ret = device_register(dev);
432         if (ret) {
433                 kfree(dev);
434                 return ERR_PTR(ret);
435         }
436         return dev;
437 }
438
439 void
440 s390_root_dev_unregister(struct device *dev)
441 {
442         if (dev)
443                 device_unregister(dev);
444 }
445
446 struct slow_subchannel {
447         struct list_head slow_list;
448         unsigned long schid;
449 };
450
451 static LIST_HEAD(slow_subchannels_head);
452 static spinlock_t slow_subchannel_lock = SPIN_LOCK_UNLOCKED;
453
454 int
455 css_enqueue_subchannel_slow(unsigned long schid)
456 {
457         struct slow_subchannel *new_slow_sch;
458         unsigned long flags;
459
460         new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
461         if (!new_slow_sch)
462                 return -ENOMEM;
463         new_slow_sch->schid = schid;
464         spin_lock_irqsave(&slow_subchannel_lock, flags);
465         list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
466         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
467         return 0;
468 }
469
470 void
471 css_clear_subchannel_slow_list(void)
472 {
473         unsigned long flags;
474
475         spin_lock_irqsave(&slow_subchannel_lock, flags);
476         while (!list_empty(&slow_subchannels_head)) {
477                 struct slow_subchannel *slow_sch =
478                         list_entry(slow_subchannels_head.next,
479                                    struct slow_subchannel, slow_list);
480
481                 list_del_init(slow_subchannels_head.next);
482                 kfree(slow_sch);
483         }
484         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
485 }
486
487 void
488 css_walk_subchannel_slow_list(void (*fn)(unsigned long))
489 {
490         unsigned long flags;
491
492         spin_lock_irqsave(&slow_subchannel_lock, flags);
493         while (!list_empty(&slow_subchannels_head)) {
494                 struct slow_subchannel *slow_sch =
495                         list_entry(slow_subchannels_head.next,
496                                    struct slow_subchannel, slow_list);
497
498                 list_del_init(slow_subchannels_head.next);
499                 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
500                 fn(slow_sch->schid);
501                 spin_lock_irqsave(&slow_subchannel_lock, flags);
502                 kfree(slow_sch);
503         }
504         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
505 }
506
507 int
508 css_slow_subchannels_exist(void)
509 {
510         return (!list_empty(&slow_subchannels_head));
511 }
512
513 MODULE_LICENSE("GPL");
514 EXPORT_SYMBOL(css_bus_type);
515 EXPORT_SYMBOL(s390_root_dev_register);
516 EXPORT_SYMBOL(s390_root_dev_unregister);