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