BUG_ON() Conversion in drivers/s390/block/dasd.c
[linux-2.6] / drivers / s390 / block / dasd.c
1 /*
2  * File...........: linux/drivers/s390/block/dasd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com>
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/kmod.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/ctype.h>
17 #include <linux/major.h>
18 #include <linux/slab.h>
19 #include <linux/buffer_head.h>
20 #include <linux/hdreg.h>
21
22 #include <asm/ccwdev.h>
23 #include <asm/ebcdic.h>
24 #include <asm/idals.h>
25 #include <asm/todclk.h>
26
27 /* This is ugly... */
28 #define PRINTK_HEADER "dasd:"
29
30 #include "dasd_int.h"
31 /*
32  * SECTION: Constant definitions to be used within this file
33  */
34 #define DASD_CHANQ_MAX_SIZE 4
35
36 /*
37  * SECTION: exported variables of dasd.c
38  */
39 debug_info_t *dasd_debug_area;
40 struct dasd_discipline *dasd_diag_discipline_pointer;
41
42 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
43 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
44                    " Copyright 2000 IBM Corporation");
45 MODULE_SUPPORTED_DEVICE("dasd");
46 MODULE_PARM(dasd, "1-" __MODULE_STRING(256) "s");
47 MODULE_LICENSE("GPL");
48
49 /*
50  * SECTION: prototypes for static functions of dasd.c
51  */
52 static int  dasd_alloc_queue(struct dasd_device * device);
53 static void dasd_setup_queue(struct dasd_device * device);
54 static void dasd_free_queue(struct dasd_device * device);
55 static void dasd_flush_request_queue(struct dasd_device *);
56 static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
57 static void dasd_flush_ccw_queue(struct dasd_device *, int);
58 static void dasd_tasklet(struct dasd_device *);
59 static void do_kick_device(void *data);
60
61 /*
62  * SECTION: Operations on the device structure.
63  */
64 static wait_queue_head_t dasd_init_waitq;
65
66 /*
67  * Allocate memory for a new device structure.
68  */
69 struct dasd_device *
70 dasd_alloc_device(void)
71 {
72         struct dasd_device *device;
73
74         device = kzalloc(sizeof (struct dasd_device), GFP_ATOMIC);
75         if (device == NULL)
76                 return ERR_PTR(-ENOMEM);
77         /* open_count = 0 means device online but not in use */
78         atomic_set(&device->open_count, -1);
79
80         /* Get two pages for normal block device operations. */
81         device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
82         if (device->ccw_mem == NULL) {
83                 kfree(device);
84                 return ERR_PTR(-ENOMEM);
85         }
86         /* Get one page for error recovery. */
87         device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
88         if (device->erp_mem == NULL) {
89                 free_pages((unsigned long) device->ccw_mem, 1);
90                 kfree(device);
91                 return ERR_PTR(-ENOMEM);
92         }
93
94         dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
95         dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
96         spin_lock_init(&device->mem_lock);
97         spin_lock_init(&device->request_queue_lock);
98         atomic_set (&device->tasklet_scheduled, 0);
99         tasklet_init(&device->tasklet, 
100                      (void (*)(unsigned long)) dasd_tasklet,
101                      (unsigned long) device);
102         INIT_LIST_HEAD(&device->ccw_queue);
103         init_timer(&device->timer);
104         INIT_WORK(&device->kick_work, do_kick_device, device);
105         device->state = DASD_STATE_NEW;
106         device->target = DASD_STATE_NEW;
107
108         return device;
109 }
110
111 /*
112  * Free memory of a device structure.
113  */
114 void
115 dasd_free_device(struct dasd_device *device)
116 {
117         kfree(device->private);
118         free_page((unsigned long) device->erp_mem);
119         free_pages((unsigned long) device->ccw_mem, 1);
120         kfree(device);
121 }
122
123 /*
124  * Make a new device known to the system.
125  */
126 static inline int
127 dasd_state_new_to_known(struct dasd_device *device)
128 {
129         int rc;
130
131         /*
132          * As long as the device is not in state DASD_STATE_NEW we want to 
133          * keep the reference count > 0.
134          */
135         dasd_get_device(device);
136
137         rc = dasd_alloc_queue(device);
138         if (rc) {
139                 dasd_put_device(device);
140                 return rc;
141         }
142
143         device->state = DASD_STATE_KNOWN;
144         return 0;
145 }
146
147 /*
148  * Let the system forget about a device.
149  */
150 static inline void
151 dasd_state_known_to_new(struct dasd_device * device)
152 {
153         /* Disable extended error reporting for this device. */
154         dasd_eer_disable(device);
155         /* Forget the discipline information. */
156         if (device->discipline)
157                 module_put(device->discipline->owner);
158         device->discipline = NULL;
159         if (device->base_discipline)
160                 module_put(device->base_discipline->owner);
161         device->base_discipline = NULL;
162         device->state = DASD_STATE_NEW;
163
164         dasd_free_queue(device);
165
166         /* Give up reference we took in dasd_state_new_to_known. */
167         dasd_put_device(device);
168 }
169
170 /*
171  * Request the irq line for the device.
172  */
173 static inline int
174 dasd_state_known_to_basic(struct dasd_device * device)
175 {
176         int rc;
177
178         /* Allocate and register gendisk structure. */
179         rc = dasd_gendisk_alloc(device);
180         if (rc)
181                 return rc;
182
183         /* register 'device' debug area, used for all DBF_DEV_XXX calls */
184         device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
185                                             8 * sizeof (long));
186         debug_register_view(device->debug_area, &debug_sprintf_view);
187         debug_set_level(device->debug_area, DBF_EMERG);
188         DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
189
190         device->state = DASD_STATE_BASIC;
191         return 0;
192 }
193
194 /*
195  * Release the irq line for the device. Terminate any running i/o.
196  */
197 static inline void
198 dasd_state_basic_to_known(struct dasd_device * device)
199 {
200         dasd_gendisk_free(device);
201         dasd_flush_ccw_queue(device, 1);
202         DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
203         if (device->debug_area != NULL) {
204                 debug_unregister(device->debug_area);
205                 device->debug_area = NULL;
206         }
207         device->state = DASD_STATE_KNOWN;
208 }
209
210 /*
211  * Do the initial analysis. The do_analysis function may return
212  * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
213  * until the discipline decides to continue the startup sequence
214  * by calling the function dasd_change_state. The eckd disciplines
215  * uses this to start a ccw that detects the format. The completion
216  * interrupt for this detection ccw uses the kernel event daemon to
217  * trigger the call to dasd_change_state. All this is done in the
218  * discipline code, see dasd_eckd.c.
219  * After the analysis ccw is done (do_analysis returned 0) the block
220  * device is setup.
221  * In case the analysis returns an error, the device setup is stopped
222  * (a fake disk was already added to allow formatting).
223  */
224 static inline int
225 dasd_state_basic_to_ready(struct dasd_device * device)
226 {
227         int rc;
228
229         rc = 0;
230         if (device->discipline->do_analysis != NULL)
231                 rc = device->discipline->do_analysis(device);
232         if (rc) {
233                 if (rc != -EAGAIN)
234                         device->state = DASD_STATE_UNFMT;
235                 return rc;
236         }
237         /* make disk known with correct capacity */
238         dasd_setup_queue(device);
239         set_capacity(device->gdp, device->blocks << device->s2b_shift);
240         device->state = DASD_STATE_READY;
241         rc = dasd_scan_partitions(device);
242         if (rc)
243                 device->state = DASD_STATE_BASIC;
244         return rc;
245 }
246
247 /*
248  * Remove device from block device layer. Destroy dirty buffers.
249  * Forget format information. Check if the target level is basic
250  * and if it is create fake disk for formatting.
251  */
252 static inline void
253 dasd_state_ready_to_basic(struct dasd_device * device)
254 {
255         dasd_flush_ccw_queue(device, 0);
256         dasd_destroy_partitions(device);
257         dasd_flush_request_queue(device);
258         device->blocks = 0;
259         device->bp_block = 0;
260         device->s2b_shift = 0;
261         device->state = DASD_STATE_BASIC;
262 }
263
264 /*
265  * Back to basic.
266  */
267 static inline void
268 dasd_state_unfmt_to_basic(struct dasd_device * device)
269 {
270         device->state = DASD_STATE_BASIC;
271 }
272
273 /*
274  * Make the device online and schedule the bottom half to start
275  * the requeueing of requests from the linux request queue to the
276  * ccw queue.
277  */
278 static inline int
279 dasd_state_ready_to_online(struct dasd_device * device)
280 {
281         device->state = DASD_STATE_ONLINE;
282         dasd_schedule_bh(device);
283         return 0;
284 }
285
286 /*
287  * Stop the requeueing of requests again.
288  */
289 static inline void
290 dasd_state_online_to_ready(struct dasd_device * device)
291 {
292         device->state = DASD_STATE_READY;
293 }
294
295 /*
296  * Device startup state changes.
297  */
298 static inline int
299 dasd_increase_state(struct dasd_device *device)
300 {
301         int rc;
302
303         rc = 0;
304         if (device->state == DASD_STATE_NEW &&
305             device->target >= DASD_STATE_KNOWN)
306                 rc = dasd_state_new_to_known(device);
307
308         if (!rc &&
309             device->state == DASD_STATE_KNOWN &&
310             device->target >= DASD_STATE_BASIC)
311                 rc = dasd_state_known_to_basic(device);
312
313         if (!rc &&
314             device->state == DASD_STATE_BASIC &&
315             device->target >= DASD_STATE_READY)
316                 rc = dasd_state_basic_to_ready(device);
317
318         if (!rc &&
319             device->state == DASD_STATE_READY &&
320             device->target >= DASD_STATE_ONLINE)
321                 rc = dasd_state_ready_to_online(device);
322
323         return rc;
324 }
325
326 /*
327  * Device shutdown state changes.
328  */
329 static inline int
330 dasd_decrease_state(struct dasd_device *device)
331 {
332         if (device->state == DASD_STATE_ONLINE &&
333             device->target <= DASD_STATE_READY)
334                 dasd_state_online_to_ready(device);
335         
336         if (device->state == DASD_STATE_READY &&
337             device->target <= DASD_STATE_BASIC)
338                 dasd_state_ready_to_basic(device);
339
340         if (device->state == DASD_STATE_UNFMT &&
341             device->target <= DASD_STATE_BASIC)
342                 dasd_state_unfmt_to_basic(device);
343
344         if (device->state == DASD_STATE_BASIC &&
345             device->target <= DASD_STATE_KNOWN)
346                 dasd_state_basic_to_known(device);
347         
348         if (device->state == DASD_STATE_KNOWN &&
349             device->target <= DASD_STATE_NEW)
350                 dasd_state_known_to_new(device);
351
352         return 0;
353 }
354
355 /*
356  * This is the main startup/shutdown routine.
357  */
358 static void
359 dasd_change_state(struct dasd_device *device)
360 {
361         int rc;
362
363         if (device->state == device->target)
364                 /* Already where we want to go today... */
365                 return;
366         if (device->state < device->target)
367                 rc = dasd_increase_state(device);
368         else
369                 rc = dasd_decrease_state(device);
370         if (rc && rc != -EAGAIN)
371                 device->target = device->state;
372
373         if (device->state == device->target)
374                 wake_up(&dasd_init_waitq);
375 }
376
377 /*
378  * Kick starter for devices that did not complete the startup/shutdown
379  * procedure or were sleeping because of a pending state.
380  * dasd_kick_device will schedule a call do do_kick_device to the kernel
381  * event daemon.
382  */
383 static void
384 do_kick_device(void *data)
385 {
386         struct dasd_device *device;
387
388         device = (struct dasd_device *) data;
389         dasd_change_state(device);
390         dasd_schedule_bh(device);
391         dasd_put_device(device);
392 }
393
394 void
395 dasd_kick_device(struct dasd_device *device)
396 {
397         dasd_get_device(device);
398         /* queue call to dasd_kick_device to the kernel event daemon. */
399         schedule_work(&device->kick_work);
400 }
401
402 /*
403  * Set the target state for a device and starts the state change.
404  */
405 void
406 dasd_set_target_state(struct dasd_device *device, int target)
407 {
408         /* If we are in probeonly mode stop at DASD_STATE_READY. */
409         if (dasd_probeonly && target > DASD_STATE_READY)
410                 target = DASD_STATE_READY;
411         if (device->target != target) {
412                 if (device->state == target)
413                         wake_up(&dasd_init_waitq);
414                 device->target = target;
415         }
416         if (device->state != device->target)
417                 dasd_change_state(device);
418 }
419
420 /*
421  * Enable devices with device numbers in [from..to].
422  */
423 static inline int
424 _wait_for_device(struct dasd_device *device)
425 {
426         return (device->state == device->target);
427 }
428
429 void
430 dasd_enable_device(struct dasd_device *device)
431 {
432         dasd_set_target_state(device, DASD_STATE_ONLINE);
433         if (device->state <= DASD_STATE_KNOWN)
434                 /* No discipline for device found. */
435                 dasd_set_target_state(device, DASD_STATE_NEW);
436         /* Now wait for the devices to come up. */
437         wait_event(dasd_init_waitq, _wait_for_device(device));
438 }
439
440 /*
441  * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
442  */
443 #ifdef CONFIG_DASD_PROFILE
444
445 struct dasd_profile_info_t dasd_global_profile;
446 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
447
448 /*
449  * Increments counter in global and local profiling structures.
450  */
451 #define dasd_profile_counter(value, counter, device) \
452 { \
453         int index; \
454         for (index = 0; index < 31 && value >> (2+index); index++); \
455         dasd_global_profile.counter[index]++; \
456         device->profile.counter[index]++; \
457 }
458
459 /*
460  * Add profiling information for cqr before execution.
461  */
462 static inline void
463 dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
464                    struct request *req)
465 {
466         struct list_head *l;
467         unsigned int counter;
468
469         if (dasd_profile_level != DASD_PROFILE_ON)
470                 return;
471
472         /* count the length of the chanq for statistics */
473         counter = 0;
474         list_for_each(l, &device->ccw_queue)
475                 if (++counter >= 31)
476                         break;
477         dasd_global_profile.dasd_io_nr_req[counter]++;
478         device->profile.dasd_io_nr_req[counter]++;
479 }
480
481 /*
482  * Add profiling information for cqr after execution.
483  */
484 static inline void
485 dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
486                  struct request *req)
487 {
488         long strtime, irqtime, endtime, tottime;        /* in microseconds */
489         long tottimeps, sectors;
490
491         if (dasd_profile_level != DASD_PROFILE_ON)
492                 return;
493
494         sectors = req->nr_sectors;
495         if (!cqr->buildclk || !cqr->startclk ||
496             !cqr->stopclk || !cqr->endclk ||
497             !sectors)
498                 return;
499
500         strtime = ((cqr->startclk - cqr->buildclk) >> 12);
501         irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
502         endtime = ((cqr->endclk - cqr->stopclk) >> 12);
503         tottime = ((cqr->endclk - cqr->buildclk) >> 12);
504         tottimeps = tottime / sectors;
505
506         if (!dasd_global_profile.dasd_io_reqs)
507                 memset(&dasd_global_profile, 0,
508                        sizeof (struct dasd_profile_info_t));
509         dasd_global_profile.dasd_io_reqs++;
510         dasd_global_profile.dasd_io_sects += sectors;
511
512         if (!device->profile.dasd_io_reqs)
513                 memset(&device->profile, 0,
514                        sizeof (struct dasd_profile_info_t));
515         device->profile.dasd_io_reqs++;
516         device->profile.dasd_io_sects += sectors;
517
518         dasd_profile_counter(sectors, dasd_io_secs, device);
519         dasd_profile_counter(tottime, dasd_io_times, device);
520         dasd_profile_counter(tottimeps, dasd_io_timps, device);
521         dasd_profile_counter(strtime, dasd_io_time1, device);
522         dasd_profile_counter(irqtime, dasd_io_time2, device);
523         dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
524         dasd_profile_counter(endtime, dasd_io_time3, device);
525 }
526 #else
527 #define dasd_profile_start(device, cqr, req) do {} while (0)
528 #define dasd_profile_end(device, cqr, req) do {} while (0)
529 #endif                          /* CONFIG_DASD_PROFILE */
530
531 /*
532  * Allocate memory for a channel program with 'cplength' channel
533  * command words and 'datasize' additional space. There are two
534  * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
535  * memory and 2) dasd_smalloc_request uses the static ccw memory
536  * that gets allocated for each device.
537  */
538 struct dasd_ccw_req *
539 dasd_kmalloc_request(char *magic, int cplength, int datasize,
540                    struct dasd_device * device)
541 {
542         struct dasd_ccw_req *cqr;
543
544         /* Sanity checks */
545         BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
546              (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
547
548         cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
549         if (cqr == NULL)
550                 return ERR_PTR(-ENOMEM);
551         cqr->cpaddr = NULL;
552         if (cplength > 0) {
553                 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
554                                       GFP_ATOMIC | GFP_DMA);
555                 if (cqr->cpaddr == NULL) {
556                         kfree(cqr);
557                         return ERR_PTR(-ENOMEM);
558                 }
559         }
560         cqr->data = NULL;
561         if (datasize > 0) {
562                 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
563                 if (cqr->data == NULL) {
564                         kfree(cqr->cpaddr);
565                         kfree(cqr);
566                         return ERR_PTR(-ENOMEM);
567                 }
568         }
569         strncpy((char *) &cqr->magic, magic, 4);
570         ASCEBC((char *) &cqr->magic, 4);
571         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
572         dasd_get_device(device);
573         return cqr;
574 }
575
576 struct dasd_ccw_req *
577 dasd_smalloc_request(char *magic, int cplength, int datasize,
578                    struct dasd_device * device)
579 {
580         unsigned long flags;
581         struct dasd_ccw_req *cqr;
582         char *data;
583         int size;
584
585         /* Sanity checks */
586         BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
587              (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
588
589         size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
590         if (cplength > 0)
591                 size += cplength * sizeof(struct ccw1);
592         if (datasize > 0)
593                 size += datasize;
594         spin_lock_irqsave(&device->mem_lock, flags);
595         cqr = (struct dasd_ccw_req *)
596                 dasd_alloc_chunk(&device->ccw_chunks, size);
597         spin_unlock_irqrestore(&device->mem_lock, flags);
598         if (cqr == NULL)
599                 return ERR_PTR(-ENOMEM);
600         memset(cqr, 0, sizeof(struct dasd_ccw_req));
601         data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
602         cqr->cpaddr = NULL;
603         if (cplength > 0) {
604                 cqr->cpaddr = (struct ccw1 *) data;
605                 data += cplength*sizeof(struct ccw1);
606                 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
607         }
608         cqr->data = NULL;
609         if (datasize > 0) {
610                 cqr->data = data;
611                 memset(cqr->data, 0, datasize);
612         }
613         strncpy((char *) &cqr->magic, magic, 4);
614         ASCEBC((char *) &cqr->magic, 4);
615         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
616         dasd_get_device(device);
617         return cqr;
618 }
619
620 /*
621  * Free memory of a channel program. This function needs to free all the
622  * idal lists that might have been created by dasd_set_cda and the
623  * struct dasd_ccw_req itself.
624  */
625 void
626 dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
627 {
628 #ifdef CONFIG_64BIT
629         struct ccw1 *ccw;
630
631         /* Clear any idals used for the request. */
632         ccw = cqr->cpaddr;
633         do {
634                 clear_normalized_cda(ccw);
635         } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
636 #endif
637         kfree(cqr->cpaddr);
638         kfree(cqr->data);
639         kfree(cqr);
640         dasd_put_device(device);
641 }
642
643 void
644 dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
645 {
646         unsigned long flags;
647
648         spin_lock_irqsave(&device->mem_lock, flags);
649         dasd_free_chunk(&device->ccw_chunks, cqr);
650         spin_unlock_irqrestore(&device->mem_lock, flags);
651         dasd_put_device(device);
652 }
653
654 /*
655  * Check discipline magic in cqr.
656  */
657 static inline int
658 dasd_check_cqr(struct dasd_ccw_req *cqr)
659 {
660         struct dasd_device *device;
661
662         if (cqr == NULL)
663                 return -EINVAL;
664         device = cqr->device;
665         if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
666                 DEV_MESSAGE(KERN_WARNING, device,
667                             " dasd_ccw_req 0x%08x magic doesn't match"
668                             " discipline 0x%08x",
669                             cqr->magic,
670                             *(unsigned int *) device->discipline->name);
671                 return -EINVAL;
672         }
673         return 0;
674 }
675
676 /*
677  * Terminate the current i/o and set the request to clear_pending.
678  * Timer keeps device runnig.
679  * ccw_device_clear can fail if the i/o subsystem
680  * is in a bad mood.
681  */
682 int
683 dasd_term_IO(struct dasd_ccw_req * cqr)
684 {
685         struct dasd_device *device;
686         int retries, rc;
687
688         /* Check the cqr */
689         rc = dasd_check_cqr(cqr);
690         if (rc)
691                 return rc;
692         retries = 0;
693         device = (struct dasd_device *) cqr->device;
694         while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
695                 rc = ccw_device_clear(device->cdev, (long) cqr);
696                 switch (rc) {
697                 case 0: /* termination successful */
698                         cqr->retries--;
699                         cqr->status = DASD_CQR_CLEAR;
700                         cqr->stopclk = get_clock();
701                         DBF_DEV_EVENT(DBF_DEBUG, device,
702                                       "terminate cqr %p successful",
703                                       cqr);
704                         break;
705                 case -ENODEV:
706                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
707                                       "device gone, retry");
708                         break;
709                 case -EIO:
710                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
711                                       "I/O error, retry");
712                         break;
713                 case -EINVAL:
714                 case -EBUSY:
715                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
716                                       "device busy, retry later");
717                         break;
718                 default:
719                         DEV_MESSAGE(KERN_ERR, device,
720                                     "line %d unknown RC=%d, please "
721                                     "report to linux390@de.ibm.com",
722                                     __LINE__, rc);
723                         BUG();
724                         break;
725                 }
726                 retries++;
727         }
728         dasd_schedule_bh(device);
729         return rc;
730 }
731
732 /*
733  * Start the i/o. This start_IO can fail if the channel is really busy.
734  * In that case set up a timer to start the request later.
735  */
736 int
737 dasd_start_IO(struct dasd_ccw_req * cqr)
738 {
739         struct dasd_device *device;
740         int rc;
741
742         /* Check the cqr */
743         rc = dasd_check_cqr(cqr);
744         if (rc)
745                 return rc;
746         device = (struct dasd_device *) cqr->device;
747         if (cqr->retries < 0) {
748                 DEV_MESSAGE(KERN_DEBUG, device,
749                             "start_IO: request %p (%02x/%i) - no retry left.",
750                             cqr, cqr->status, cqr->retries);
751                 cqr->status = DASD_CQR_FAILED;
752                 return -EIO;
753         }
754         cqr->startclk = get_clock();
755         cqr->starttime = jiffies;
756         cqr->retries--;
757         rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
758                               cqr->lpm, 0);
759         switch (rc) {
760         case 0:
761                 cqr->status = DASD_CQR_IN_IO;
762                 DBF_DEV_EVENT(DBF_DEBUG, device,
763                               "start_IO: request %p started successful",
764                               cqr);
765                 break;
766         case -EBUSY:
767                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
768                               "start_IO: device busy, retry later");
769                 break;
770         case -ETIMEDOUT:
771                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
772                               "start_IO: request timeout, retry later");
773                 break;
774         case -EACCES:
775                 /* -EACCES indicates that the request used only a
776                  * subset of the available pathes and all these
777                  * pathes are gone.
778                  * Do a retry with all available pathes.
779                  */
780                 cqr->lpm = LPM_ANYPATH;
781                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
782                               "start_IO: selected pathes gone,"
783                               " retry on all pathes");
784                 break;
785         case -ENODEV:
786         case -EIO:
787                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
788                               "start_IO: device gone, retry");
789                 break;
790         default:
791                 DEV_MESSAGE(KERN_ERR, device,
792                             "line %d unknown RC=%d, please report"
793                             " to linux390@de.ibm.com", __LINE__, rc);
794                 BUG();
795                 break;
796         }
797         return rc;
798 }
799
800 /*
801  * Timeout function for dasd devices. This is used for different purposes
802  *  1) missing interrupt handler for normal operation
803  *  2) delayed start of request where start_IO failed with -EBUSY
804  *  3) timeout for missing state change interrupts
805  * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
806  * DASD_CQR_QUEUED for 2) and 3).
807  */
808 static void
809 dasd_timeout_device(unsigned long ptr)
810 {
811         unsigned long flags;
812         struct dasd_device *device;
813
814         device = (struct dasd_device *) ptr;
815         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
816         /* re-activate request queue */
817         device->stopped &= ~DASD_STOPPED_PENDING;
818         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
819         dasd_schedule_bh(device);
820 }
821
822 /*
823  * Setup timeout for a device in jiffies.
824  */
825 void
826 dasd_set_timer(struct dasd_device *device, int expires)
827 {
828         if (expires == 0) {
829                 if (timer_pending(&device->timer))
830                         del_timer(&device->timer);
831                 return;
832         }
833         if (timer_pending(&device->timer)) {
834                 if (mod_timer(&device->timer, jiffies + expires))
835                         return;
836         }
837         device->timer.function = dasd_timeout_device;
838         device->timer.data = (unsigned long) device;
839         device->timer.expires = jiffies + expires;
840         add_timer(&device->timer);
841 }
842
843 /*
844  * Clear timeout for a device.
845  */
846 void
847 dasd_clear_timer(struct dasd_device *device)
848 {
849         if (timer_pending(&device->timer))
850                 del_timer(&device->timer);
851 }
852
853 static void
854 dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
855 {
856         struct dasd_ccw_req *cqr;
857         struct dasd_device *device;
858
859         cqr = (struct dasd_ccw_req *) intparm;
860         if (cqr->status != DASD_CQR_IN_IO) {
861                 MESSAGE(KERN_DEBUG,
862                         "invalid status in handle_killed_request: "
863                         "bus_id %s, status %02x",
864                         cdev->dev.bus_id, cqr->status);
865                 return;
866         }
867
868         device = (struct dasd_device *) cqr->device;
869         if (device == NULL ||
870             device != dasd_device_from_cdev(cdev) ||
871             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
872                 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
873                         cdev->dev.bus_id);
874                 return;
875         }
876
877         /* Schedule request to be retried. */
878         cqr->status = DASD_CQR_QUEUED;
879
880         dasd_clear_timer(device);
881         dasd_schedule_bh(device);
882         dasd_put_device(device);
883 }
884
885 static void
886 dasd_handle_state_change_pending(struct dasd_device *device)
887 {
888         struct dasd_ccw_req *cqr;
889         struct list_head *l, *n;
890
891         /* First of all start sense subsystem status request. */
892         dasd_eer_snss(device);
893
894         device->stopped &= ~DASD_STOPPED_PENDING;
895
896         /* restart all 'running' IO on queue */
897         list_for_each_safe(l, n, &device->ccw_queue) {
898                 cqr = list_entry(l, struct dasd_ccw_req, list);
899                 if (cqr->status == DASD_CQR_IN_IO) {
900                         cqr->status = DASD_CQR_QUEUED;
901                 }
902         }
903         dasd_clear_timer(device);
904         dasd_schedule_bh(device);
905 }
906
907 /*
908  * Interrupt handler for "normal" ssch-io based dasd devices.
909  */
910 void
911 dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
912                  struct irb *irb)
913 {
914         struct dasd_ccw_req *cqr, *next;
915         struct dasd_device *device;
916         unsigned long long now;
917         int expires;
918         dasd_era_t era;
919         char mask;
920
921         if (IS_ERR(irb)) {
922                 switch (PTR_ERR(irb)) {
923                 case -EIO:
924                         dasd_handle_killed_request(cdev, intparm);
925                         break;
926                 case -ETIMEDOUT:
927                         printk(KERN_WARNING"%s(%s): request timed out\n",
928                                __FUNCTION__, cdev->dev.bus_id);
929                         //FIXME - dasd uses own timeout interface...
930                         break;
931                 default:
932                         printk(KERN_WARNING"%s(%s): unknown error %ld\n",
933                                __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
934                 }
935                 return;
936         }
937
938         now = get_clock();
939
940         DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
941                   cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
942                   (unsigned int) intparm);
943
944         /* first of all check for state change pending interrupt */
945         mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
946         if ((irb->scsw.dstat & mask) == mask) {
947                 device = dasd_device_from_cdev(cdev);
948                 if (!IS_ERR(device)) {
949                         dasd_handle_state_change_pending(device);
950                         dasd_put_device(device);
951                 }
952                 return;
953         }
954
955         cqr = (struct dasd_ccw_req *) intparm;
956
957         /* check for unsolicited interrupts */
958         if (cqr == NULL) {
959                 MESSAGE(KERN_DEBUG,
960                         "unsolicited interrupt received: bus_id %s",
961                         cdev->dev.bus_id);
962                 return;
963         }
964
965         device = (struct dasd_device *) cqr->device;
966         if (device == NULL ||
967             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
968                 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
969                         cdev->dev.bus_id);
970                 return;
971         }
972
973         /* Check for clear pending */
974         if (cqr->status == DASD_CQR_CLEAR &&
975             irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
976                 cqr->status = DASD_CQR_QUEUED;
977                 dasd_clear_timer(device);
978                 dasd_schedule_bh(device);
979                 return;
980         }
981
982         /* check status - the request might have been killed by dyn detach */
983         if (cqr->status != DASD_CQR_IN_IO) {
984                 MESSAGE(KERN_DEBUG,
985                         "invalid status: bus_id %s, status %02x",
986                         cdev->dev.bus_id, cqr->status);
987                 return;
988         }
989         DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
990                       ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
991
992         /* Find out the appropriate era_action. */
993         if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) 
994                 era = dasd_era_fatal;
995         else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
996                  irb->scsw.cstat == 0 &&
997                  !irb->esw.esw0.erw.cons)
998                 era = dasd_era_none;
999         else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
1000                 era = dasd_era_fatal; /* don't recover this request */
1001         else if (irb->esw.esw0.erw.cons)
1002                 era = device->discipline->examine_error(cqr, irb);
1003         else 
1004                 era = dasd_era_recover;
1005
1006         DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
1007         expires = 0;
1008         if (era == dasd_era_none) {
1009                 cqr->status = DASD_CQR_DONE;
1010                 cqr->stopclk = now;
1011                 /* Start first request on queue if possible -> fast_io. */
1012                 if (cqr->list.next != &device->ccw_queue) {
1013                         next = list_entry(cqr->list.next,
1014                                           struct dasd_ccw_req, list);
1015                         if ((next->status == DASD_CQR_QUEUED) &&
1016                             (!device->stopped)) {
1017                                 if (device->discipline->start_IO(next) == 0)
1018                                         expires = next->expires;
1019                                 else
1020                                         DEV_MESSAGE(KERN_DEBUG, device, "%s",
1021                                                     "Interrupt fastpath "
1022                                                     "failed!");
1023                         }
1024                 }
1025         } else {                /* error */
1026                 memcpy(&cqr->irb, irb, sizeof (struct irb));
1027 #ifdef ERP_DEBUG
1028                 /* dump sense data */
1029                 dasd_log_sense(cqr, irb);
1030 #endif
1031                 switch (era) {
1032                 case dasd_era_fatal:
1033                         cqr->status = DASD_CQR_FAILED;
1034                         cqr->stopclk = now;
1035                         break;
1036                 case dasd_era_recover:
1037                         cqr->status = DASD_CQR_ERROR;
1038                         break;
1039                 default:
1040                         BUG();
1041                 }
1042         }
1043         if (expires != 0)
1044                 dasd_set_timer(device, expires);
1045         else
1046                 dasd_clear_timer(device);
1047         dasd_schedule_bh(device);
1048 }
1049
1050 /*
1051  * posts the buffer_cache about a finalized request
1052  */
1053 static inline void
1054 dasd_end_request(struct request *req, int uptodate)
1055 {
1056         if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1057                 BUG();
1058         add_disk_randomness(req->rq_disk);
1059         end_that_request_last(req, uptodate);
1060 }
1061
1062 /*
1063  * Process finished error recovery ccw.
1064  */
1065 static inline void
1066 __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1067 {
1068         dasd_erp_fn_t erp_fn;
1069
1070         if (cqr->status == DASD_CQR_DONE)
1071                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1072         else
1073                 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1074         erp_fn = device->discipline->erp_postaction(cqr);
1075         erp_fn(cqr);
1076 }
1077
1078 /*
1079  * Process ccw request queue.
1080  */
1081 static inline void
1082 __dasd_process_ccw_queue(struct dasd_device * device,
1083                          struct list_head *final_queue)
1084 {
1085         struct list_head *l, *n;
1086         struct dasd_ccw_req *cqr;
1087         dasd_erp_fn_t erp_fn;
1088
1089 restart:
1090         /* Process request with final status. */
1091         list_for_each_safe(l, n, &device->ccw_queue) {
1092                 cqr = list_entry(l, struct dasd_ccw_req, list);
1093                 /* Stop list processing at the first non-final request. */
1094                 if (cqr->status != DASD_CQR_DONE &&
1095                     cqr->status != DASD_CQR_FAILED &&
1096                     cqr->status != DASD_CQR_ERROR)
1097                         break;
1098                 /*  Process requests with DASD_CQR_ERROR */
1099                 if (cqr->status == DASD_CQR_ERROR) {
1100                         if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1101                                 cqr->status = DASD_CQR_FAILED;
1102                                 cqr->stopclk = get_clock();
1103                         } else {
1104                                 if (cqr->irb.esw.esw0.erw.cons) {
1105                                         erp_fn = device->discipline->
1106                                                 erp_action(cqr);
1107                                         erp_fn(cqr);
1108                                 } else
1109                                         dasd_default_erp_action(cqr);
1110                         }
1111                         goto restart;
1112                 }
1113
1114                 /* First of all call extended error reporting. */
1115                 if (dasd_eer_enabled(device) &&
1116                     cqr->status == DASD_CQR_FAILED) {
1117                         dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
1118
1119                         /* restart request  */
1120                         cqr->status = DASD_CQR_QUEUED;
1121                         cqr->retries = 255;
1122                         device->stopped |= DASD_STOPPED_QUIESCE;
1123                         goto restart;
1124                 }
1125
1126                 /* Process finished ERP request. */
1127                 if (cqr->refers) {
1128                         __dasd_process_erp(device, cqr);
1129                         goto restart;
1130                 }
1131
1132                 /* Rechain finished requests to final queue */
1133                 cqr->endclk = get_clock();
1134                 list_move_tail(&cqr->list, final_queue);
1135         }
1136 }
1137
1138 static void
1139 dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1140 {
1141         struct request *req;
1142         struct dasd_device *device;
1143         int status;
1144
1145         req = (struct request *) data;
1146         device = cqr->device;
1147         dasd_profile_end(device, cqr, req);
1148         status = cqr->device->discipline->free_cp(cqr,req);
1149         spin_lock_irq(&device->request_queue_lock);
1150         dasd_end_request(req, status);
1151         spin_unlock_irq(&device->request_queue_lock);
1152 }
1153
1154
1155 /*
1156  * Fetch requests from the block device queue.
1157  */
1158 static inline void
1159 __dasd_process_blk_queue(struct dasd_device * device)
1160 {
1161         request_queue_t *queue;
1162         struct request *req;
1163         struct dasd_ccw_req *cqr;
1164         int nr_queued;
1165
1166         queue = device->request_queue;
1167         /* No queue ? Then there is nothing to do. */
1168         if (queue == NULL)
1169                 return;
1170
1171         /*
1172          * We requeue request from the block device queue to the ccw
1173          * queue only in two states. In state DASD_STATE_READY the
1174          * partition detection is done and we need to requeue requests
1175          * for that. State DASD_STATE_ONLINE is normal block device
1176          * operation.
1177          */
1178         if (device->state != DASD_STATE_READY &&
1179             device->state != DASD_STATE_ONLINE)
1180                 return;
1181         nr_queued = 0;
1182         /* Now we try to fetch requests from the request queue */
1183         list_for_each_entry(cqr, &device->ccw_queue, list)
1184                 if (cqr->status == DASD_CQR_QUEUED)
1185                         nr_queued++;
1186         while (!blk_queue_plugged(queue) &&
1187                elv_next_request(queue) &&
1188                 nr_queued < DASD_CHANQ_MAX_SIZE) {
1189                 req = elv_next_request(queue);
1190
1191                 if (device->features & DASD_FEATURE_READONLY &&
1192                     rq_data_dir(req) == WRITE) {
1193                         DBF_DEV_EVENT(DBF_ERR, device,
1194                                       "Rejecting write request %p",
1195                                       req);
1196                         blkdev_dequeue_request(req);
1197                         dasd_end_request(req, 0);
1198                         continue;
1199                 }
1200                 if (device->stopped & DASD_STOPPED_DC_EIO) {
1201                         blkdev_dequeue_request(req);
1202                         dasd_end_request(req, 0);
1203                         continue;
1204                 }
1205                 cqr = device->discipline->build_cp(device, req);
1206                 if (IS_ERR(cqr)) {
1207                         if (PTR_ERR(cqr) == -ENOMEM)
1208                                 break;  /* terminate request queue loop */
1209                         DBF_DEV_EVENT(DBF_ERR, device,
1210                                       "CCW creation failed (rc=%ld) "
1211                                       "on request %p",
1212                                       PTR_ERR(cqr), req);
1213                         blkdev_dequeue_request(req);
1214                         dasd_end_request(req, 0);
1215                         continue;
1216                 }
1217                 cqr->callback = dasd_end_request_cb;
1218                 cqr->callback_data = (void *) req;
1219                 cqr->status = DASD_CQR_QUEUED;
1220                 blkdev_dequeue_request(req);
1221                 list_add_tail(&cqr->list, &device->ccw_queue);
1222                 dasd_profile_start(device, cqr, req);
1223                 nr_queued++;
1224         }
1225 }
1226
1227 /*
1228  * Take a look at the first request on the ccw queue and check
1229  * if it reached its expire time. If so, terminate the IO.
1230  */
1231 static inline void
1232 __dasd_check_expire(struct dasd_device * device)
1233 {
1234         struct dasd_ccw_req *cqr;
1235
1236         if (list_empty(&device->ccw_queue))
1237                 return;
1238         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1239         if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1240                 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1241                         if (device->discipline->term_IO(cqr) != 0)
1242                                 /* Hmpf, try again in 1/10 sec */
1243                                 dasd_set_timer(device, 10);
1244                 }
1245         }
1246 }
1247
1248 /*
1249  * Take a look at the first request on the ccw queue and check
1250  * if it needs to be started.
1251  */
1252 static inline void
1253 __dasd_start_head(struct dasd_device * device)
1254 {
1255         struct dasd_ccw_req *cqr;
1256         int rc;
1257
1258         if (list_empty(&device->ccw_queue))
1259                 return;
1260         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1261         /* check FAILFAST */
1262         if (device->stopped & ~DASD_STOPPED_PENDING &&
1263             test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1264             (!dasd_eer_enabled(device))) {
1265                 cqr->status = DASD_CQR_FAILED;
1266                 dasd_schedule_bh(device);
1267         }
1268         if ((cqr->status == DASD_CQR_QUEUED) &&
1269             (!device->stopped)) {
1270                 /* try to start the first I/O that can be started */
1271                 rc = device->discipline->start_IO(cqr);
1272                 if (rc == 0)
1273                         dasd_set_timer(device, cqr->expires);
1274                 else if (rc == -EACCES) {
1275                         dasd_schedule_bh(device);
1276                 } else
1277                         /* Hmpf, try again in 1/2 sec */
1278                         dasd_set_timer(device, 50);
1279         }
1280 }
1281
1282 /*
1283  * Remove requests from the ccw queue. 
1284  */
1285 static void
1286 dasd_flush_ccw_queue(struct dasd_device * device, int all)
1287 {
1288         struct list_head flush_queue;
1289         struct list_head *l, *n;
1290         struct dasd_ccw_req *cqr;
1291
1292         INIT_LIST_HEAD(&flush_queue);
1293         spin_lock_irq(get_ccwdev_lock(device->cdev));
1294         list_for_each_safe(l, n, &device->ccw_queue) {
1295                 cqr = list_entry(l, struct dasd_ccw_req, list);
1296                 /* Flush all request or only block device requests? */
1297                 if (all == 0 && cqr->callback == dasd_end_request_cb)
1298                         continue;
1299                 if (cqr->status == DASD_CQR_IN_IO)
1300                         device->discipline->term_IO(cqr);
1301                 if (cqr->status != DASD_CQR_DONE ||
1302                     cqr->status != DASD_CQR_FAILED) {
1303                         cqr->status = DASD_CQR_FAILED;
1304                         cqr->stopclk = get_clock();
1305                 }
1306                 /* Process finished ERP request. */
1307                 if (cqr->refers) {
1308                         __dasd_process_erp(device, cqr);
1309                         continue;
1310                 }
1311                 /* Rechain request on device request queue */
1312                 cqr->endclk = get_clock();
1313                 list_move_tail(&cqr->list, &flush_queue);
1314         }
1315         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1316         /* Now call the callback function of flushed requests */
1317         list_for_each_safe(l, n, &flush_queue) {
1318                 cqr = list_entry(l, struct dasd_ccw_req, list);
1319                 if (cqr->callback != NULL)
1320                         (cqr->callback)(cqr, cqr->callback_data);
1321         }
1322 }
1323
1324 /*
1325  * Acquire the device lock and process queues for the device.
1326  */
1327 static void
1328 dasd_tasklet(struct dasd_device * device)
1329 {
1330         struct list_head final_queue;
1331         struct list_head *l, *n;
1332         struct dasd_ccw_req *cqr;
1333
1334         atomic_set (&device->tasklet_scheduled, 0);
1335         INIT_LIST_HEAD(&final_queue);
1336         spin_lock_irq(get_ccwdev_lock(device->cdev));
1337         /* Check expire time of first request on the ccw queue. */
1338         __dasd_check_expire(device);
1339         /* Finish off requests on ccw queue */
1340         __dasd_process_ccw_queue(device, &final_queue);
1341         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1342         /* Now call the callback function of requests with final status */
1343         list_for_each_safe(l, n, &final_queue) {
1344                 cqr = list_entry(l, struct dasd_ccw_req, list);
1345                 list_del_init(&cqr->list);
1346                 if (cqr->callback != NULL)
1347                         (cqr->callback)(cqr, cqr->callback_data);
1348         }
1349         spin_lock_irq(&device->request_queue_lock);
1350         spin_lock(get_ccwdev_lock(device->cdev));
1351         /* Get new request from the block device request queue */
1352         __dasd_process_blk_queue(device);
1353         /* Now check if the head of the ccw queue needs to be started. */
1354         __dasd_start_head(device);
1355         spin_unlock(get_ccwdev_lock(device->cdev));
1356         spin_unlock_irq(&device->request_queue_lock);
1357         dasd_put_device(device);
1358 }
1359
1360 /*
1361  * Schedules a call to dasd_tasklet over the device tasklet.
1362  */
1363 void
1364 dasd_schedule_bh(struct dasd_device * device)
1365 {
1366         /* Protect against rescheduling. */
1367         if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1368                 return;
1369         dasd_get_device(device);
1370         tasklet_hi_schedule(&device->tasklet);
1371 }
1372
1373 /*
1374  * Queue a request to the head of the ccw_queue. Start the I/O if
1375  * possible.
1376  */
1377 void
1378 dasd_add_request_head(struct dasd_ccw_req *req)
1379 {
1380         struct dasd_device *device;
1381         unsigned long flags;
1382
1383         device = req->device;
1384         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1385         req->status = DASD_CQR_QUEUED;
1386         req->device = device;
1387         list_add(&req->list, &device->ccw_queue);
1388         /* let the bh start the request to keep them in order */
1389         dasd_schedule_bh(device);
1390         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1391 }
1392
1393 /*
1394  * Queue a request to the tail of the ccw_queue. Start the I/O if
1395  * possible.
1396  */
1397 void
1398 dasd_add_request_tail(struct dasd_ccw_req *req)
1399 {
1400         struct dasd_device *device;
1401         unsigned long flags;
1402
1403         device = req->device;
1404         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1405         req->status = DASD_CQR_QUEUED;
1406         req->device = device;
1407         list_add_tail(&req->list, &device->ccw_queue);
1408         /* let the bh start the request to keep them in order */
1409         dasd_schedule_bh(device);
1410         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1411 }
1412
1413 /*
1414  * Wakeup callback.
1415  */
1416 static void
1417 dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1418 {
1419         wake_up((wait_queue_head_t *) data);
1420 }
1421
1422 static inline int
1423 _wait_for_wakeup(struct dasd_ccw_req *cqr)
1424 {
1425         struct dasd_device *device;
1426         int rc;
1427
1428         device = cqr->device;
1429         spin_lock_irq(get_ccwdev_lock(device->cdev));
1430         rc = ((cqr->status == DASD_CQR_DONE ||
1431                cqr->status == DASD_CQR_FAILED) &&
1432               list_empty(&cqr->list));
1433         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1434         return rc;
1435 }
1436
1437 /*
1438  * Attempts to start a special ccw queue and waits for its completion.
1439  */
1440 int
1441 dasd_sleep_on(struct dasd_ccw_req * cqr)
1442 {
1443         wait_queue_head_t wait_q;
1444         struct dasd_device *device;
1445         int rc;
1446         
1447         device = cqr->device;
1448         spin_lock_irq(get_ccwdev_lock(device->cdev));
1449         
1450         init_waitqueue_head (&wait_q);
1451         cqr->callback = dasd_wakeup_cb;
1452         cqr->callback_data = (void *) &wait_q;
1453         cqr->status = DASD_CQR_QUEUED;
1454         list_add_tail(&cqr->list, &device->ccw_queue);
1455         
1456         /* let the bh start the request to keep them in order */
1457         dasd_schedule_bh(device);
1458         
1459         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1460
1461         wait_event(wait_q, _wait_for_wakeup(cqr));
1462         
1463         /* Request status is either done or failed. */
1464         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1465         return rc;
1466 }
1467
1468 /*
1469  * Attempts to start a special ccw queue and wait interruptible
1470  * for its completion.
1471  */
1472 int
1473 dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1474 {
1475         wait_queue_head_t wait_q;
1476         struct dasd_device *device;
1477         int rc, finished;
1478
1479         device = cqr->device;
1480         spin_lock_irq(get_ccwdev_lock(device->cdev));
1481
1482         init_waitqueue_head (&wait_q);
1483         cqr->callback = dasd_wakeup_cb;
1484         cqr->callback_data = (void *) &wait_q;
1485         cqr->status = DASD_CQR_QUEUED;
1486         list_add_tail(&cqr->list, &device->ccw_queue);
1487
1488         /* let the bh start the request to keep them in order */
1489         dasd_schedule_bh(device);
1490         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1491
1492         finished = 0;
1493         while (!finished) {
1494                 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1495                 if (rc != -ERESTARTSYS) {
1496                         /* Request is final (done or failed) */
1497                         rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1498                         break;
1499                 }
1500                 spin_lock_irq(get_ccwdev_lock(device->cdev));
1501                 switch (cqr->status) {
1502                 case DASD_CQR_IN_IO:
1503                         /* terminate runnig cqr */
1504                         if (device->discipline->term_IO) {
1505                                 cqr->retries = -1;
1506                                 device->discipline->term_IO(cqr);
1507                                 /*nished =
1508                                  * wait (non-interruptible) for final status
1509                                  * because signal ist still pending
1510                                  */
1511                                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1512                                 wait_event(wait_q, _wait_for_wakeup(cqr));
1513                                 spin_lock_irq(get_ccwdev_lock(device->cdev));
1514                                 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1515                                 finished = 1;
1516                         }
1517                         break;
1518                 case DASD_CQR_QUEUED:
1519                         /* request  */
1520                         list_del_init(&cqr->list);
1521                         rc = -EIO;
1522                         finished = 1;
1523                         break;
1524                 default:
1525                         /* cqr with 'non-interruptable' status - just wait */
1526                         break;
1527                 }
1528                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1529         }
1530         return rc;
1531 }
1532
1533 /*
1534  * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1535  * for eckd devices) the currently running request has to be terminated
1536  * and be put back to status queued, before the special request is added
1537  * to the head of the queue. Then the special request is waited on normally.
1538  */
1539 static inline int
1540 _dasd_term_running_cqr(struct dasd_device *device)
1541 {
1542         struct dasd_ccw_req *cqr;
1543         int rc;
1544
1545         if (list_empty(&device->ccw_queue))
1546                 return 0;
1547         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1548         rc = device->discipline->term_IO(cqr);
1549         if (rc == 0) {
1550                 /* termination successful */
1551                 cqr->status = DASD_CQR_QUEUED;
1552                 cqr->startclk = cqr->stopclk = 0;
1553                 cqr->starttime = 0;
1554         }
1555         return rc;
1556 }
1557
1558 int
1559 dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1560 {
1561         wait_queue_head_t wait_q;
1562         struct dasd_device *device;
1563         int rc;
1564         
1565         device = cqr->device;
1566         spin_lock_irq(get_ccwdev_lock(device->cdev));
1567         rc = _dasd_term_running_cqr(device);
1568         if (rc) {
1569                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1570                 return rc;
1571         }
1572         
1573         init_waitqueue_head (&wait_q);
1574         cqr->callback = dasd_wakeup_cb;
1575         cqr->callback_data = (void *) &wait_q;
1576         cqr->status = DASD_CQR_QUEUED;
1577         list_add(&cqr->list, &device->ccw_queue);
1578         
1579         /* let the bh start the request to keep them in order */
1580         dasd_schedule_bh(device);
1581         
1582         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1583
1584         wait_event(wait_q, _wait_for_wakeup(cqr));
1585         
1586         /* Request status is either done or failed. */
1587         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1588         return rc;
1589 }
1590
1591 /*
1592  * Cancels a request that was started with dasd_sleep_on_req.
1593  * This is useful to timeout requests. The request will be
1594  * terminated if it is currently in i/o.
1595  * Returns 1 if the request has been terminated.
1596  */
1597 int
1598 dasd_cancel_req(struct dasd_ccw_req *cqr)
1599 {
1600         struct dasd_device *device = cqr->device;
1601         unsigned long flags;
1602         int rc;
1603
1604         rc = 0;
1605         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1606         switch (cqr->status) {
1607         case DASD_CQR_QUEUED:
1608                 /* request was not started - just set to failed */
1609                 cqr->status = DASD_CQR_FAILED;
1610                 break;
1611         case DASD_CQR_IN_IO:
1612                 /* request in IO - terminate IO and release again */
1613                 if (device->discipline->term_IO(cqr) != 0)
1614                         /* what to do if unable to terminate ??????
1615                            e.g. not _IN_IO */
1616                         cqr->status = DASD_CQR_FAILED;
1617                 cqr->stopclk = get_clock();
1618                 rc = 1;
1619                 break;
1620         case DASD_CQR_DONE:
1621         case DASD_CQR_FAILED:
1622                 /* already finished - do nothing */
1623                 break;
1624         default:
1625                 DEV_MESSAGE(KERN_ALERT, device,
1626                             "invalid status %02x in request",
1627                             cqr->status);
1628                 BUG();
1629
1630         }
1631         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1632         dasd_schedule_bh(device);
1633         return rc;
1634 }
1635
1636 /*
1637  * SECTION: Block device operations (request queue, partitions, open, release).
1638  */
1639
1640 /*
1641  * Dasd request queue function. Called from ll_rw_blk.c
1642  */
1643 static void
1644 do_dasd_request(request_queue_t * queue)
1645 {
1646         struct dasd_device *device;
1647
1648         device = (struct dasd_device *) queue->queuedata;
1649         spin_lock(get_ccwdev_lock(device->cdev));
1650         /* Get new request from the block device request queue */
1651         __dasd_process_blk_queue(device);
1652         /* Now check if the head of the ccw queue needs to be started. */
1653         __dasd_start_head(device);
1654         spin_unlock(get_ccwdev_lock(device->cdev));
1655 }
1656
1657 /*
1658  * Allocate and initialize request queue and default I/O scheduler.
1659  */
1660 static int
1661 dasd_alloc_queue(struct dasd_device * device)
1662 {
1663         int rc;
1664
1665         device->request_queue = blk_init_queue(do_dasd_request,
1666                                                &device->request_queue_lock);
1667         if (device->request_queue == NULL)
1668                 return -ENOMEM;
1669
1670         device->request_queue->queuedata = device;
1671
1672         elevator_exit(device->request_queue->elevator);
1673         rc = elevator_init(device->request_queue, "deadline");
1674         if (rc) {
1675                 blk_cleanup_queue(device->request_queue);
1676                 return rc;
1677         }
1678         return 0;
1679 }
1680
1681 /*
1682  * Allocate and initialize request queue.
1683  */
1684 static void
1685 dasd_setup_queue(struct dasd_device * device)
1686 {
1687         int max;
1688
1689         blk_queue_hardsect_size(device->request_queue, device->bp_block);
1690         max = device->discipline->max_blocks << device->s2b_shift;
1691         blk_queue_max_sectors(device->request_queue, max);
1692         blk_queue_max_phys_segments(device->request_queue, -1L);
1693         blk_queue_max_hw_segments(device->request_queue, -1L);
1694         blk_queue_max_segment_size(device->request_queue, -1L);
1695         blk_queue_segment_boundary(device->request_queue, -1L);
1696         blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
1697 }
1698
1699 /*
1700  * Deactivate and free request queue.
1701  */
1702 static void
1703 dasd_free_queue(struct dasd_device * device)
1704 {
1705         if (device->request_queue) {
1706                 blk_cleanup_queue(device->request_queue);
1707                 device->request_queue = NULL;
1708         }
1709 }
1710
1711 /*
1712  * Flush request on the request queue.
1713  */
1714 static void
1715 dasd_flush_request_queue(struct dasd_device * device)
1716 {
1717         struct request *req;
1718
1719         if (!device->request_queue)
1720                 return;
1721         
1722         spin_lock_irq(&device->request_queue_lock);
1723         while (!list_empty(&device->request_queue->queue_head)) {
1724                 req = elv_next_request(device->request_queue);
1725                 if (req == NULL)
1726                         break;
1727                 dasd_end_request(req, 0);
1728                 blkdev_dequeue_request(req);
1729         }
1730         spin_unlock_irq(&device->request_queue_lock);
1731 }
1732
1733 static int
1734 dasd_open(struct inode *inp, struct file *filp)
1735 {
1736         struct gendisk *disk = inp->i_bdev->bd_disk;
1737         struct dasd_device *device = disk->private_data;
1738         int rc;
1739
1740         atomic_inc(&device->open_count);
1741         if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1742                 rc = -ENODEV;
1743                 goto unlock;
1744         }
1745
1746         if (!try_module_get(device->discipline->owner)) {
1747                 rc = -EINVAL;
1748                 goto unlock;
1749         }
1750
1751         if (dasd_probeonly) {
1752                 DEV_MESSAGE(KERN_INFO, device, "%s",
1753                             "No access to device due to probeonly mode");
1754                 rc = -EPERM;
1755                 goto out;
1756         }
1757
1758         if (device->state <= DASD_STATE_BASIC) {
1759                 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1760                               " Cannot open unrecognized device");
1761                 rc = -ENODEV;
1762                 goto out;
1763         }
1764
1765         return 0;
1766
1767 out:
1768         module_put(device->discipline->owner);
1769 unlock:
1770         atomic_dec(&device->open_count);
1771         return rc;
1772 }
1773
1774 static int
1775 dasd_release(struct inode *inp, struct file *filp)
1776 {
1777         struct gendisk *disk = inp->i_bdev->bd_disk;
1778         struct dasd_device *device = disk->private_data;
1779
1780         atomic_dec(&device->open_count);
1781         module_put(device->discipline->owner);
1782         return 0;
1783 }
1784
1785 /*
1786  * Return disk geometry.
1787  */
1788 static int
1789 dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1790 {
1791         struct dasd_device *device;
1792
1793         device = bdev->bd_disk->private_data;
1794         if (!device)
1795                 return -ENODEV;
1796
1797         if (!device->discipline ||
1798             !device->discipline->fill_geometry)
1799                 return -EINVAL;
1800
1801         device->discipline->fill_geometry(device, geo);
1802         geo->start = get_start_sect(bdev) >> device->s2b_shift;
1803         return 0;
1804 }
1805
1806 struct block_device_operations
1807 dasd_device_operations = {
1808         .owner          = THIS_MODULE,
1809         .open           = dasd_open,
1810         .release        = dasd_release,
1811         .ioctl          = dasd_ioctl,
1812         .compat_ioctl   = dasd_compat_ioctl,
1813         .getgeo         = dasd_getgeo,
1814 };
1815
1816
1817 static void
1818 dasd_exit(void)
1819 {
1820 #ifdef CONFIG_PROC_FS
1821         dasd_proc_exit();
1822 #endif
1823         dasd_eer_exit();
1824         if (dasd_page_cache != NULL) {
1825                 kmem_cache_destroy(dasd_page_cache);
1826                 dasd_page_cache = NULL;
1827         }
1828         dasd_gendisk_exit();
1829         dasd_devmap_exit();
1830         devfs_remove("dasd");
1831         if (dasd_debug_area != NULL) {
1832                 debug_unregister(dasd_debug_area);
1833                 dasd_debug_area = NULL;
1834         }
1835 }
1836
1837 /*
1838  * SECTION: common functions for ccw_driver use
1839  */
1840
1841 /*
1842  * Initial attempt at a probe function. this can be simplified once
1843  * the other detection code is gone.
1844  */
1845 int
1846 dasd_generic_probe (struct ccw_device *cdev,
1847                     struct dasd_discipline *discipline)
1848 {
1849         int ret;
1850
1851         ret = dasd_add_sysfs_files(cdev);
1852         if (ret) {
1853                 printk(KERN_WARNING
1854                        "dasd_generic_probe: could not add sysfs entries "
1855                        "for %s\n", cdev->dev.bus_id);
1856         } else {
1857                 cdev->handler = &dasd_int_handler;
1858         }
1859
1860         return ret;
1861 }
1862
1863 /*
1864  * This will one day be called from a global not_oper handler.
1865  * It is also used by driver_unregister during module unload.
1866  */
1867 void
1868 dasd_generic_remove (struct ccw_device *cdev)
1869 {
1870         struct dasd_device *device;
1871
1872         cdev->handler = NULL;
1873
1874         dasd_remove_sysfs_files(cdev);
1875         device = dasd_device_from_cdev(cdev);
1876         if (IS_ERR(device))
1877                 return;
1878         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1879                 /* Already doing offline processing */
1880                 dasd_put_device(device);
1881                 return;
1882         }
1883         /*
1884          * This device is removed unconditionally. Set offline
1885          * flag to prevent dasd_open from opening it while it is
1886          * no quite down yet.
1887          */
1888         dasd_set_target_state(device, DASD_STATE_NEW);
1889         /* dasd_delete_device destroys the device reference. */
1890         dasd_delete_device(device);
1891 }
1892
1893 /*
1894  * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1895  * the device is detected for the first time and is supposed to be used
1896  * or the user has started activation through sysfs.
1897  */
1898 int
1899 dasd_generic_set_online (struct ccw_device *cdev,
1900                          struct dasd_discipline *base_discipline)
1901
1902 {
1903         struct dasd_discipline *discipline;
1904         struct dasd_device *device;
1905         int rc;
1906
1907         device = dasd_create_device(cdev);
1908         if (IS_ERR(device))
1909                 return PTR_ERR(device);
1910
1911         discipline = base_discipline;
1912         if (device->features & DASD_FEATURE_USEDIAG) {
1913                 if (!dasd_diag_discipline_pointer) {
1914                         printk (KERN_WARNING
1915                                 "dasd_generic couldn't online device %s "
1916                                 "- discipline DIAG not available\n",
1917                                 cdev->dev.bus_id);
1918                         dasd_delete_device(device);
1919                         return -ENODEV;
1920                 }
1921                 discipline = dasd_diag_discipline_pointer;
1922         }
1923         if (!try_module_get(base_discipline->owner)) {
1924                 dasd_delete_device(device);
1925                 return -EINVAL;
1926         }
1927         if (!try_module_get(discipline->owner)) {
1928                 module_put(base_discipline->owner);
1929                 dasd_delete_device(device);
1930                 return -EINVAL;
1931         }
1932         device->base_discipline = base_discipline;
1933         device->discipline = discipline;
1934
1935         rc = discipline->check_device(device);
1936         if (rc) {
1937                 printk (KERN_WARNING
1938                         "dasd_generic couldn't online device %s "
1939                         "with discipline %s rc=%i\n",
1940                         cdev->dev.bus_id, discipline->name, rc);
1941                 module_put(discipline->owner);
1942                 module_put(base_discipline->owner);
1943                 dasd_delete_device(device);
1944                 return rc;
1945         }
1946
1947         dasd_set_target_state(device, DASD_STATE_ONLINE);
1948         if (device->state <= DASD_STATE_KNOWN) {
1949                 printk (KERN_WARNING
1950                         "dasd_generic discipline not found for %s\n",
1951                         cdev->dev.bus_id);
1952                 rc = -ENODEV;
1953                 dasd_set_target_state(device, DASD_STATE_NEW);
1954                 dasd_delete_device(device);
1955         } else
1956                 pr_debug("dasd_generic device %s found\n",
1957                                 cdev->dev.bus_id);
1958
1959         /* FIXME: we have to wait for the root device but we don't want
1960          * to wait for each single device but for all at once. */
1961         wait_event(dasd_init_waitq, _wait_for_device(device));
1962
1963         dasd_put_device(device);
1964
1965         return rc;
1966 }
1967
1968 int
1969 dasd_generic_set_offline (struct ccw_device *cdev)
1970 {
1971         struct dasd_device *device;
1972         int max_count;
1973
1974         device = dasd_device_from_cdev(cdev);
1975         if (IS_ERR(device))
1976                 return PTR_ERR(device);
1977         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1978                 /* Already doing offline processing */
1979                 dasd_put_device(device);
1980                 return 0;
1981         }
1982         /*
1983          * We must make sure that this device is currently not in use.
1984          * The open_count is increased for every opener, that includes
1985          * the blkdev_get in dasd_scan_partitions. We are only interested
1986          * in the other openers.
1987          */
1988         max_count = device->bdev ? 0 : -1;
1989         if (atomic_read(&device->open_count) > max_count) {
1990                 printk (KERN_WARNING "Can't offline dasd device with open"
1991                         " count = %i.\n",
1992                         atomic_read(&device->open_count));
1993                 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
1994                 dasd_put_device(device);
1995                 return -EBUSY;
1996         }
1997         dasd_set_target_state(device, DASD_STATE_NEW);
1998         /* dasd_delete_device destroys the device reference. */
1999         dasd_delete_device(device);
2000
2001         return 0;
2002 }
2003
2004 int
2005 dasd_generic_notify(struct ccw_device *cdev, int event)
2006 {
2007         struct dasd_device *device;
2008         struct dasd_ccw_req *cqr;
2009         unsigned long flags;
2010         int ret;
2011
2012         device = dasd_device_from_cdev(cdev);
2013         if (IS_ERR(device))
2014                 return 0;
2015         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2016         ret = 0;
2017         switch (event) {
2018         case CIO_GONE:
2019         case CIO_NO_PATH:
2020                 /* First of all call extended error reporting. */
2021                 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2022
2023                 if (device->state < DASD_STATE_BASIC)
2024                         break;
2025                 /* Device is active. We want to keep it. */
2026                 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2027                         list_for_each_entry(cqr, &device->ccw_queue, list)
2028                                 if (cqr->status == DASD_CQR_IN_IO)
2029                                         cqr->status = DASD_CQR_FAILED;
2030                         device->stopped |= DASD_STOPPED_DC_EIO;
2031                 } else {
2032                         list_for_each_entry(cqr, &device->ccw_queue, list)
2033                                 if (cqr->status == DASD_CQR_IN_IO) {
2034                                         cqr->status = DASD_CQR_QUEUED;
2035                                         cqr->retries++;
2036                                 }
2037                         device->stopped |= DASD_STOPPED_DC_WAIT;
2038                         dasd_set_timer(device, 0);
2039                 }
2040                 dasd_schedule_bh(device);
2041                 ret = 1;
2042                 break;
2043         case CIO_OPER:
2044                 /* FIXME: add a sanity check. */
2045                 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2046                 dasd_schedule_bh(device);
2047                 ret = 1;
2048                 break;
2049         }
2050         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2051         dasd_put_device(device);
2052         return ret;
2053 }
2054
2055 /*
2056  * Automatically online either all dasd devices (dasd_autodetect) or
2057  * all devices specified with dasd= parameters.
2058  */
2059 static int
2060 __dasd_auto_online(struct device *dev, void *data)
2061 {
2062         struct ccw_device *cdev;
2063
2064         cdev = to_ccwdev(dev);
2065         if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
2066                 ccw_device_set_online(cdev);
2067         return 0;
2068 }
2069
2070 void
2071 dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
2072 {
2073         struct device_driver *drv;
2074
2075         drv = get_driver(&dasd_discipline_driver->driver);
2076         driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
2077         put_driver(drv);
2078 }
2079
2080
2081 static int __init
2082 dasd_init(void)
2083 {
2084         int rc;
2085
2086         init_waitqueue_head(&dasd_init_waitq);
2087
2088         /* register 'common' DASD debug area, used for all DBF_XXX calls */
2089         dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
2090         if (dasd_debug_area == NULL) {
2091                 rc = -ENOMEM;
2092                 goto failed;
2093         }
2094         debug_register_view(dasd_debug_area, &debug_sprintf_view);
2095         debug_set_level(dasd_debug_area, DBF_EMERG);
2096
2097         DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2098
2099         dasd_diag_discipline_pointer = NULL;
2100
2101         rc = devfs_mk_dir("dasd");
2102         if (rc)
2103                 goto failed;
2104         rc = dasd_devmap_init();
2105         if (rc)
2106                 goto failed;
2107         rc = dasd_gendisk_init();
2108         if (rc)
2109                 goto failed;
2110         rc = dasd_parse();
2111         if (rc)
2112                 goto failed;
2113         rc = dasd_eer_init();
2114         if (rc)
2115                 goto failed;
2116 #ifdef CONFIG_PROC_FS
2117         rc = dasd_proc_init();
2118         if (rc)
2119                 goto failed;
2120 #endif
2121
2122         return 0;
2123 failed:
2124         MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2125         dasd_exit();
2126         return rc;
2127 }
2128
2129 module_init(dasd_init);
2130 module_exit(dasd_exit);
2131
2132 EXPORT_SYMBOL(dasd_debug_area);
2133 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2134
2135 EXPORT_SYMBOL(dasd_add_request_head);
2136 EXPORT_SYMBOL(dasd_add_request_tail);
2137 EXPORT_SYMBOL(dasd_cancel_req);
2138 EXPORT_SYMBOL(dasd_clear_timer);
2139 EXPORT_SYMBOL(dasd_enable_device);
2140 EXPORT_SYMBOL(dasd_int_handler);
2141 EXPORT_SYMBOL(dasd_kfree_request);
2142 EXPORT_SYMBOL(dasd_kick_device);
2143 EXPORT_SYMBOL(dasd_kmalloc_request);
2144 EXPORT_SYMBOL(dasd_schedule_bh);
2145 EXPORT_SYMBOL(dasd_set_target_state);
2146 EXPORT_SYMBOL(dasd_set_timer);
2147 EXPORT_SYMBOL(dasd_sfree_request);
2148 EXPORT_SYMBOL(dasd_sleep_on);
2149 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2150 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2151 EXPORT_SYMBOL(dasd_smalloc_request);
2152 EXPORT_SYMBOL(dasd_start_IO);
2153 EXPORT_SYMBOL(dasd_term_IO);
2154
2155 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2156 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2157 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2158 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2159 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2160 EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
2161
2162 /*
2163  * Overrides for Emacs so that we follow Linus's tabbing style.
2164  * Emacs will notice this stuff at the end of the file and automatically
2165  * adjust the settings for this buffer only.  This must remain at the end
2166  * of the file.
2167  * ---------------------------------------------------------------------------
2168  * Local variables:
2169  * c-indent-level: 4
2170  * c-brace-imaginary-offset: 0
2171  * c-brace-offset: -4
2172  * c-argdecl-indent: 4
2173  * c-label-offset: -4
2174  * c-continued-statement-offset: 4
2175  * c-continued-brace-offset: 0
2176  * indent-tabs-mode: 1
2177  * tab-width: 8
2178  * End:
2179  */