[PATCH] s390: kzalloc() conversion in drivers/s390
[firefly-linux-kernel-4.4.55.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 = 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         if ( magic == NULL || datasize > PAGE_SIZE ||
546              (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
547                 BUG();
548
549         cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
550         if (cqr == NULL)
551                 return ERR_PTR(-ENOMEM);
552         cqr->cpaddr = NULL;
553         if (cplength > 0) {
554                 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
555                                       GFP_ATOMIC | GFP_DMA);
556                 if (cqr->cpaddr == NULL) {
557                         kfree(cqr);
558                         return ERR_PTR(-ENOMEM);
559                 }
560         }
561         cqr->data = NULL;
562         if (datasize > 0) {
563                 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
564                 if (cqr->data == NULL) {
565                         kfree(cqr->cpaddr);
566                         kfree(cqr);
567                         return ERR_PTR(-ENOMEM);
568                 }
569         }
570         strncpy((char *) &cqr->magic, magic, 4);
571         ASCEBC((char *) &cqr->magic, 4);
572         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
573         dasd_get_device(device);
574         return cqr;
575 }
576
577 struct dasd_ccw_req *
578 dasd_smalloc_request(char *magic, int cplength, int datasize,
579                    struct dasd_device * device)
580 {
581         unsigned long flags;
582         struct dasd_ccw_req *cqr;
583         char *data;
584         int size;
585
586         /* Sanity checks */
587         if ( magic == NULL || datasize > PAGE_SIZE ||
588              (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
589                 BUG();
590
591         size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
592         if (cplength > 0)
593                 size += cplength * sizeof(struct ccw1);
594         if (datasize > 0)
595                 size += datasize;
596         spin_lock_irqsave(&device->mem_lock, flags);
597         cqr = (struct dasd_ccw_req *)
598                 dasd_alloc_chunk(&device->ccw_chunks, size);
599         spin_unlock_irqrestore(&device->mem_lock, flags);
600         if (cqr == NULL)
601                 return ERR_PTR(-ENOMEM);
602         memset(cqr, 0, sizeof(struct dasd_ccw_req));
603         data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
604         cqr->cpaddr = NULL;
605         if (cplength > 0) {
606                 cqr->cpaddr = (struct ccw1 *) data;
607                 data += cplength*sizeof(struct ccw1);
608                 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
609         }
610         cqr->data = NULL;
611         if (datasize > 0) {
612                 cqr->data = data;
613                 memset(cqr->data, 0, datasize);
614         }
615         strncpy((char *) &cqr->magic, magic, 4);
616         ASCEBC((char *) &cqr->magic, 4);
617         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
618         dasd_get_device(device);
619         return cqr;
620 }
621
622 /*
623  * Free memory of a channel program. This function needs to free all the
624  * idal lists that might have been created by dasd_set_cda and the
625  * struct dasd_ccw_req itself.
626  */
627 void
628 dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
629 {
630 #ifdef CONFIG_64BIT
631         struct ccw1 *ccw;
632
633         /* Clear any idals used for the request. */
634         ccw = cqr->cpaddr;
635         do {
636                 clear_normalized_cda(ccw);
637         } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
638 #endif
639         kfree(cqr->cpaddr);
640         kfree(cqr->data);
641         kfree(cqr);
642         dasd_put_device(device);
643 }
644
645 void
646 dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
647 {
648         unsigned long flags;
649
650         spin_lock_irqsave(&device->mem_lock, flags);
651         dasd_free_chunk(&device->ccw_chunks, cqr);
652         spin_unlock_irqrestore(&device->mem_lock, flags);
653         dasd_put_device(device);
654 }
655
656 /*
657  * Check discipline magic in cqr.
658  */
659 static inline int
660 dasd_check_cqr(struct dasd_ccw_req *cqr)
661 {
662         struct dasd_device *device;
663
664         if (cqr == NULL)
665                 return -EINVAL;
666         device = cqr->device;
667         if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
668                 DEV_MESSAGE(KERN_WARNING, device,
669                             " dasd_ccw_req 0x%08x magic doesn't match"
670                             " discipline 0x%08x",
671                             cqr->magic,
672                             *(unsigned int *) device->discipline->name);
673                 return -EINVAL;
674         }
675         return 0;
676 }
677
678 /*
679  * Terminate the current i/o and set the request to clear_pending.
680  * Timer keeps device runnig.
681  * ccw_device_clear can fail if the i/o subsystem
682  * is in a bad mood.
683  */
684 int
685 dasd_term_IO(struct dasd_ccw_req * cqr)
686 {
687         struct dasd_device *device;
688         int retries, rc;
689
690         /* Check the cqr */
691         rc = dasd_check_cqr(cqr);
692         if (rc)
693                 return rc;
694         retries = 0;
695         device = (struct dasd_device *) cqr->device;
696         while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
697                 rc = ccw_device_clear(device->cdev, (long) cqr);
698                 switch (rc) {
699                 case 0: /* termination successful */
700                         cqr->retries--;
701                         cqr->status = DASD_CQR_CLEAR;
702                         cqr->stopclk = get_clock();
703                         DBF_DEV_EVENT(DBF_DEBUG, device,
704                                       "terminate cqr %p successful",
705                                       cqr);
706                         break;
707                 case -ENODEV:
708                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
709                                       "device gone, retry");
710                         break;
711                 case -EIO:
712                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
713                                       "I/O error, retry");
714                         break;
715                 case -EINVAL:
716                 case -EBUSY:
717                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
718                                       "device busy, retry later");
719                         break;
720                 default:
721                         DEV_MESSAGE(KERN_ERR, device,
722                                     "line %d unknown RC=%d, please "
723                                     "report to linux390@de.ibm.com",
724                                     __LINE__, rc);
725                         BUG();
726                         break;
727                 }
728                 retries++;
729         }
730         dasd_schedule_bh(device);
731         return rc;
732 }
733
734 /*
735  * Start the i/o. This start_IO can fail if the channel is really busy.
736  * In that case set up a timer to start the request later.
737  */
738 int
739 dasd_start_IO(struct dasd_ccw_req * cqr)
740 {
741         struct dasd_device *device;
742         int rc;
743
744         /* Check the cqr */
745         rc = dasd_check_cqr(cqr);
746         if (rc)
747                 return rc;
748         device = (struct dasd_device *) cqr->device;
749         if (cqr->retries < 0) {
750                 DEV_MESSAGE(KERN_DEBUG, device,
751                             "start_IO: request %p (%02x/%i) - no retry left.",
752                             cqr, cqr->status, cqr->retries);
753                 cqr->status = DASD_CQR_FAILED;
754                 return -EIO;
755         }
756         cqr->startclk = get_clock();
757         cqr->starttime = jiffies;
758         cqr->retries--;
759         rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
760                               cqr->lpm, 0);
761         switch (rc) {
762         case 0:
763                 cqr->status = DASD_CQR_IN_IO;
764                 DBF_DEV_EVENT(DBF_DEBUG, device,
765                               "start_IO: request %p started successful",
766                               cqr);
767                 break;
768         case -EBUSY:
769                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
770                               "start_IO: device busy, retry later");
771                 break;
772         case -ETIMEDOUT:
773                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
774                               "start_IO: request timeout, retry later");
775                 break;
776         case -EACCES:
777                 /* -EACCES indicates that the request used only a
778                  * subset of the available pathes and all these
779                  * pathes are gone.
780                  * Do a retry with all available pathes.
781                  */
782                 cqr->lpm = LPM_ANYPATH;
783                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
784                               "start_IO: selected pathes gone,"
785                               " retry on all pathes");
786                 break;
787         case -ENODEV:
788         case -EIO:
789                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
790                               "start_IO: device gone, retry");
791                 break;
792         default:
793                 DEV_MESSAGE(KERN_ERR, device,
794                             "line %d unknown RC=%d, please report"
795                             " to linux390@de.ibm.com", __LINE__, rc);
796                 BUG();
797                 break;
798         }
799         return rc;
800 }
801
802 /*
803  * Timeout function for dasd devices. This is used for different purposes
804  *  1) missing interrupt handler for normal operation
805  *  2) delayed start of request where start_IO failed with -EBUSY
806  *  3) timeout for missing state change interrupts
807  * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
808  * DASD_CQR_QUEUED for 2) and 3).
809  */
810 static void
811 dasd_timeout_device(unsigned long ptr)
812 {
813         unsigned long flags;
814         struct dasd_device *device;
815
816         device = (struct dasd_device *) ptr;
817         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
818         /* re-activate request queue */
819         device->stopped &= ~DASD_STOPPED_PENDING;
820         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
821         dasd_schedule_bh(device);
822 }
823
824 /*
825  * Setup timeout for a device in jiffies.
826  */
827 void
828 dasd_set_timer(struct dasd_device *device, int expires)
829 {
830         if (expires == 0) {
831                 if (timer_pending(&device->timer))
832                         del_timer(&device->timer);
833                 return;
834         }
835         if (timer_pending(&device->timer)) {
836                 if (mod_timer(&device->timer, jiffies + expires))
837                         return;
838         }
839         device->timer.function = dasd_timeout_device;
840         device->timer.data = (unsigned long) device;
841         device->timer.expires = jiffies + expires;
842         add_timer(&device->timer);
843 }
844
845 /*
846  * Clear timeout for a device.
847  */
848 void
849 dasd_clear_timer(struct dasd_device *device)
850 {
851         if (timer_pending(&device->timer))
852                 del_timer(&device->timer);
853 }
854
855 static void
856 dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
857 {
858         struct dasd_ccw_req *cqr;
859         struct dasd_device *device;
860
861         cqr = (struct dasd_ccw_req *) intparm;
862         if (cqr->status != DASD_CQR_IN_IO) {
863                 MESSAGE(KERN_DEBUG,
864                         "invalid status in handle_killed_request: "
865                         "bus_id %s, status %02x",
866                         cdev->dev.bus_id, cqr->status);
867                 return;
868         }
869
870         device = (struct dasd_device *) cqr->device;
871         if (device == NULL ||
872             device != dasd_device_from_cdev(cdev) ||
873             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
874                 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
875                         cdev->dev.bus_id);
876                 return;
877         }
878
879         /* Schedule request to be retried. */
880         cqr->status = DASD_CQR_QUEUED;
881
882         dasd_clear_timer(device);
883         dasd_schedule_bh(device);
884         dasd_put_device(device);
885 }
886
887 static void
888 dasd_handle_state_change_pending(struct dasd_device *device)
889 {
890         struct dasd_ccw_req *cqr;
891         struct list_head *l, *n;
892
893         /* First of all start sense subsystem status request. */
894         dasd_eer_snss(device);
895
896         device->stopped &= ~DASD_STOPPED_PENDING;
897
898         /* restart all 'running' IO on queue */
899         list_for_each_safe(l, n, &device->ccw_queue) {
900                 cqr = list_entry(l, struct dasd_ccw_req, list);
901                 if (cqr->status == DASD_CQR_IN_IO) {
902                         cqr->status = DASD_CQR_QUEUED;
903                 }
904         }
905         dasd_clear_timer(device);
906         dasd_schedule_bh(device);
907 }
908
909 /*
910  * Interrupt handler for "normal" ssch-io based dasd devices.
911  */
912 void
913 dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
914                  struct irb *irb)
915 {
916         struct dasd_ccw_req *cqr, *next;
917         struct dasd_device *device;
918         unsigned long long now;
919         int expires;
920         dasd_era_t era;
921         char mask;
922
923         if (IS_ERR(irb)) {
924                 switch (PTR_ERR(irb)) {
925                 case -EIO:
926                         dasd_handle_killed_request(cdev, intparm);
927                         break;
928                 case -ETIMEDOUT:
929                         printk(KERN_WARNING"%s(%s): request timed out\n",
930                                __FUNCTION__, cdev->dev.bus_id);
931                         //FIXME - dasd uses own timeout interface...
932                         break;
933                 default:
934                         printk(KERN_WARNING"%s(%s): unknown error %ld\n",
935                                __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
936                 }
937                 return;
938         }
939
940         now = get_clock();
941
942         DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
943                   cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
944                   (unsigned int) intparm);
945
946         /* first of all check for state change pending interrupt */
947         mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
948         if ((irb->scsw.dstat & mask) == mask) {
949                 device = dasd_device_from_cdev(cdev);
950                 if (!IS_ERR(device)) {
951                         dasd_handle_state_change_pending(device);
952                         dasd_put_device(device);
953                 }
954                 return;
955         }
956
957         cqr = (struct dasd_ccw_req *) intparm;
958
959         /* check for unsolicited interrupts */
960         if (cqr == NULL) {
961                 MESSAGE(KERN_DEBUG,
962                         "unsolicited interrupt received: bus_id %s",
963                         cdev->dev.bus_id);
964                 return;
965         }
966
967         device = (struct dasd_device *) cqr->device;
968         if (device == NULL ||
969             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
970                 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
971                         cdev->dev.bus_id);
972                 return;
973         }
974
975         /* Check for clear pending */
976         if (cqr->status == DASD_CQR_CLEAR &&
977             irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
978                 cqr->status = DASD_CQR_QUEUED;
979                 dasd_clear_timer(device);
980                 dasd_schedule_bh(device);
981                 return;
982         }
983
984         /* check status - the request might have been killed by dyn detach */
985         if (cqr->status != DASD_CQR_IN_IO) {
986                 MESSAGE(KERN_DEBUG,
987                         "invalid status: bus_id %s, status %02x",
988                         cdev->dev.bus_id, cqr->status);
989                 return;
990         }
991         DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
992                       ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
993
994         /* Find out the appropriate era_action. */
995         if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) 
996                 era = dasd_era_fatal;
997         else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
998                  irb->scsw.cstat == 0 &&
999                  !irb->esw.esw0.erw.cons)
1000                 era = dasd_era_none;
1001         else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
1002                 era = dasd_era_fatal; /* don't recover this request */
1003         else if (irb->esw.esw0.erw.cons)
1004                 era = device->discipline->examine_error(cqr, irb);
1005         else 
1006                 era = dasd_era_recover;
1007
1008         DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
1009         expires = 0;
1010         if (era == dasd_era_none) {
1011                 cqr->status = DASD_CQR_DONE;
1012                 cqr->stopclk = now;
1013                 /* Start first request on queue if possible -> fast_io. */
1014                 if (cqr->list.next != &device->ccw_queue) {
1015                         next = list_entry(cqr->list.next,
1016                                           struct dasd_ccw_req, list);
1017                         if ((next->status == DASD_CQR_QUEUED) &&
1018                             (!device->stopped)) {
1019                                 if (device->discipline->start_IO(next) == 0)
1020                                         expires = next->expires;
1021                                 else
1022                                         DEV_MESSAGE(KERN_DEBUG, device, "%s",
1023                                                     "Interrupt fastpath "
1024                                                     "failed!");
1025                         }
1026                 }
1027         } else {                /* error */
1028                 memcpy(&cqr->irb, irb, sizeof (struct irb));
1029 #ifdef ERP_DEBUG
1030                 /* dump sense data */
1031                 dasd_log_sense(cqr, irb);
1032 #endif
1033                 switch (era) {
1034                 case dasd_era_fatal:
1035                         cqr->status = DASD_CQR_FAILED;
1036                         cqr->stopclk = now;
1037                         break;
1038                 case dasd_era_recover:
1039                         cqr->status = DASD_CQR_ERROR;
1040                         break;
1041                 default:
1042                         BUG();
1043                 }
1044         }
1045         if (expires != 0)
1046                 dasd_set_timer(device, expires);
1047         else
1048                 dasd_clear_timer(device);
1049         dasd_schedule_bh(device);
1050 }
1051
1052 /*
1053  * posts the buffer_cache about a finalized request
1054  */
1055 static inline void
1056 dasd_end_request(struct request *req, int uptodate)
1057 {
1058         if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1059                 BUG();
1060         add_disk_randomness(req->rq_disk);
1061         end_that_request_last(req, uptodate);
1062 }
1063
1064 /*
1065  * Process finished error recovery ccw.
1066  */
1067 static inline void
1068 __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1069 {
1070         dasd_erp_fn_t erp_fn;
1071
1072         if (cqr->status == DASD_CQR_DONE)
1073                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1074         else
1075                 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1076         erp_fn = device->discipline->erp_postaction(cqr);
1077         erp_fn(cqr);
1078 }
1079
1080 /*
1081  * Process ccw request queue.
1082  */
1083 static inline void
1084 __dasd_process_ccw_queue(struct dasd_device * device,
1085                          struct list_head *final_queue)
1086 {
1087         struct list_head *l, *n;
1088         struct dasd_ccw_req *cqr;
1089         dasd_erp_fn_t erp_fn;
1090
1091 restart:
1092         /* Process request with final status. */
1093         list_for_each_safe(l, n, &device->ccw_queue) {
1094                 cqr = list_entry(l, struct dasd_ccw_req, list);
1095                 /* Stop list processing at the first non-final request. */
1096                 if (cqr->status != DASD_CQR_DONE &&
1097                     cqr->status != DASD_CQR_FAILED &&
1098                     cqr->status != DASD_CQR_ERROR)
1099                         break;
1100                 /*  Process requests with DASD_CQR_ERROR */
1101                 if (cqr->status == DASD_CQR_ERROR) {
1102                         if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1103                                 cqr->status = DASD_CQR_FAILED;
1104                                 cqr->stopclk = get_clock();
1105                         } else {
1106                                 if (cqr->irb.esw.esw0.erw.cons) {
1107                                         erp_fn = device->discipline->
1108                                                 erp_action(cqr);
1109                                         erp_fn(cqr);
1110                                 } else
1111                                         dasd_default_erp_action(cqr);
1112                         }
1113                         goto restart;
1114                 }
1115
1116                 /* First of all call extended error reporting. */
1117                 if (dasd_eer_enabled(device) &&
1118                     cqr->status == DASD_CQR_FAILED) {
1119                         dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
1120
1121                         /* restart request  */
1122                         cqr->status = DASD_CQR_QUEUED;
1123                         cqr->retries = 255;
1124                         device->stopped |= DASD_STOPPED_QUIESCE;
1125                         goto restart;
1126                 }
1127
1128                 /* Process finished ERP request. */
1129                 if (cqr->refers) {
1130                         __dasd_process_erp(device, cqr);
1131                         goto restart;
1132                 }
1133
1134                 /* Rechain finished requests to final queue */
1135                 cqr->endclk = get_clock();
1136                 list_move_tail(&cqr->list, final_queue);
1137         }
1138 }
1139
1140 static void
1141 dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1142 {
1143         struct request *req;
1144         struct dasd_device *device;
1145         int status;
1146
1147         req = (struct request *) data;
1148         device = cqr->device;
1149         dasd_profile_end(device, cqr, req);
1150         status = cqr->device->discipline->free_cp(cqr,req);
1151         spin_lock_irq(&device->request_queue_lock);
1152         dasd_end_request(req, status);
1153         spin_unlock_irq(&device->request_queue_lock);
1154 }
1155
1156
1157 /*
1158  * Fetch requests from the block device queue.
1159  */
1160 static inline void
1161 __dasd_process_blk_queue(struct dasd_device * device)
1162 {
1163         request_queue_t *queue;
1164         struct request *req;
1165         struct dasd_ccw_req *cqr;
1166         int nr_queued;
1167
1168         queue = device->request_queue;
1169         /* No queue ? Then there is nothing to do. */
1170         if (queue == NULL)
1171                 return;
1172
1173         /*
1174          * We requeue request from the block device queue to the ccw
1175          * queue only in two states. In state DASD_STATE_READY the
1176          * partition detection is done and we need to requeue requests
1177          * for that. State DASD_STATE_ONLINE is normal block device
1178          * operation.
1179          */
1180         if (device->state != DASD_STATE_READY &&
1181             device->state != DASD_STATE_ONLINE)
1182                 return;
1183         nr_queued = 0;
1184         /* Now we try to fetch requests from the request queue */
1185         list_for_each_entry(cqr, &device->ccw_queue, list)
1186                 if (cqr->status == DASD_CQR_QUEUED)
1187                         nr_queued++;
1188         while (!blk_queue_plugged(queue) &&
1189                elv_next_request(queue) &&
1190                 nr_queued < DASD_CHANQ_MAX_SIZE) {
1191                 req = elv_next_request(queue);
1192
1193                 if (device->features & DASD_FEATURE_READONLY &&
1194                     rq_data_dir(req) == WRITE) {
1195                         DBF_DEV_EVENT(DBF_ERR, device,
1196                                       "Rejecting write request %p",
1197                                       req);
1198                         blkdev_dequeue_request(req);
1199                         dasd_end_request(req, 0);
1200                         continue;
1201                 }
1202                 if (device->stopped & DASD_STOPPED_DC_EIO) {
1203                         blkdev_dequeue_request(req);
1204                         dasd_end_request(req, 0);
1205                         continue;
1206                 }
1207                 cqr = device->discipline->build_cp(device, req);
1208                 if (IS_ERR(cqr)) {
1209                         if (PTR_ERR(cqr) == -ENOMEM)
1210                                 break;  /* terminate request queue loop */
1211                         DBF_DEV_EVENT(DBF_ERR, device,
1212                                       "CCW creation failed (rc=%ld) "
1213                                       "on request %p",
1214                                       PTR_ERR(cqr), req);
1215                         blkdev_dequeue_request(req);
1216                         dasd_end_request(req, 0);
1217                         continue;
1218                 }
1219                 cqr->callback = dasd_end_request_cb;
1220                 cqr->callback_data = (void *) req;
1221                 cqr->status = DASD_CQR_QUEUED;
1222                 blkdev_dequeue_request(req);
1223                 list_add_tail(&cqr->list, &device->ccw_queue);
1224                 dasd_profile_start(device, cqr, req);
1225                 nr_queued++;
1226         }
1227 }
1228
1229 /*
1230  * Take a look at the first request on the ccw queue and check
1231  * if it reached its expire time. If so, terminate the IO.
1232  */
1233 static inline void
1234 __dasd_check_expire(struct dasd_device * device)
1235 {
1236         struct dasd_ccw_req *cqr;
1237
1238         if (list_empty(&device->ccw_queue))
1239                 return;
1240         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1241         if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1242                 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1243                         if (device->discipline->term_IO(cqr) != 0)
1244                                 /* Hmpf, try again in 1/10 sec */
1245                                 dasd_set_timer(device, 10);
1246                 }
1247         }
1248 }
1249
1250 /*
1251  * Take a look at the first request on the ccw queue and check
1252  * if it needs to be started.
1253  */
1254 static inline void
1255 __dasd_start_head(struct dasd_device * device)
1256 {
1257         struct dasd_ccw_req *cqr;
1258         int rc;
1259
1260         if (list_empty(&device->ccw_queue))
1261                 return;
1262         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1263         /* check FAILFAST */
1264         if (device->stopped & ~DASD_STOPPED_PENDING &&
1265             test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1266             (!dasd_eer_enabled(device))) {
1267                 cqr->status = DASD_CQR_FAILED;
1268                 dasd_schedule_bh(device);
1269         }
1270         if ((cqr->status == DASD_CQR_QUEUED) &&
1271             (!device->stopped)) {
1272                 /* try to start the first I/O that can be started */
1273                 rc = device->discipline->start_IO(cqr);
1274                 if (rc == 0)
1275                         dasd_set_timer(device, cqr->expires);
1276                 else if (rc == -EACCES) {
1277                         dasd_schedule_bh(device);
1278                 } else
1279                         /* Hmpf, try again in 1/2 sec */
1280                         dasd_set_timer(device, 50);
1281         }
1282 }
1283
1284 /*
1285  * Remove requests from the ccw queue. 
1286  */
1287 static void
1288 dasd_flush_ccw_queue(struct dasd_device * device, int all)
1289 {
1290         struct list_head flush_queue;
1291         struct list_head *l, *n;
1292         struct dasd_ccw_req *cqr;
1293
1294         INIT_LIST_HEAD(&flush_queue);
1295         spin_lock_irq(get_ccwdev_lock(device->cdev));
1296         list_for_each_safe(l, n, &device->ccw_queue) {
1297                 cqr = list_entry(l, struct dasd_ccw_req, list);
1298                 /* Flush all request or only block device requests? */
1299                 if (all == 0 && cqr->callback == dasd_end_request_cb)
1300                         continue;
1301                 if (cqr->status == DASD_CQR_IN_IO)
1302                         device->discipline->term_IO(cqr);
1303                 if (cqr->status != DASD_CQR_DONE ||
1304                     cqr->status != DASD_CQR_FAILED) {
1305                         cqr->status = DASD_CQR_FAILED;
1306                         cqr->stopclk = get_clock();
1307                 }
1308                 /* Process finished ERP request. */
1309                 if (cqr->refers) {
1310                         __dasd_process_erp(device, cqr);
1311                         continue;
1312                 }
1313                 /* Rechain request on device request queue */
1314                 cqr->endclk = get_clock();
1315                 list_move_tail(&cqr->list, &flush_queue);
1316         }
1317         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1318         /* Now call the callback function of flushed requests */
1319         list_for_each_safe(l, n, &flush_queue) {
1320                 cqr = list_entry(l, struct dasd_ccw_req, list);
1321                 if (cqr->callback != NULL)
1322                         (cqr->callback)(cqr, cqr->callback_data);
1323         }
1324 }
1325
1326 /*
1327  * Acquire the device lock and process queues for the device.
1328  */
1329 static void
1330 dasd_tasklet(struct dasd_device * device)
1331 {
1332         struct list_head final_queue;
1333         struct list_head *l, *n;
1334         struct dasd_ccw_req *cqr;
1335
1336         atomic_set (&device->tasklet_scheduled, 0);
1337         INIT_LIST_HEAD(&final_queue);
1338         spin_lock_irq(get_ccwdev_lock(device->cdev));
1339         /* Check expire time of first request on the ccw queue. */
1340         __dasd_check_expire(device);
1341         /* Finish off requests on ccw queue */
1342         __dasd_process_ccw_queue(device, &final_queue);
1343         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1344         /* Now call the callback function of requests with final status */
1345         list_for_each_safe(l, n, &final_queue) {
1346                 cqr = list_entry(l, struct dasd_ccw_req, list);
1347                 list_del_init(&cqr->list);
1348                 if (cqr->callback != NULL)
1349                         (cqr->callback)(cqr, cqr->callback_data);
1350         }
1351         spin_lock_irq(&device->request_queue_lock);
1352         spin_lock(get_ccwdev_lock(device->cdev));
1353         /* Get new request from the block device request queue */
1354         __dasd_process_blk_queue(device);
1355         /* Now check if the head of the ccw queue needs to be started. */
1356         __dasd_start_head(device);
1357         spin_unlock(get_ccwdev_lock(device->cdev));
1358         spin_unlock_irq(&device->request_queue_lock);
1359         dasd_put_device(device);
1360 }
1361
1362 /*
1363  * Schedules a call to dasd_tasklet over the device tasklet.
1364  */
1365 void
1366 dasd_schedule_bh(struct dasd_device * device)
1367 {
1368         /* Protect against rescheduling. */
1369         if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1370                 return;
1371         dasd_get_device(device);
1372         tasklet_hi_schedule(&device->tasklet);
1373 }
1374
1375 /*
1376  * Queue a request to the head of the ccw_queue. Start the I/O if
1377  * possible.
1378  */
1379 void
1380 dasd_add_request_head(struct dasd_ccw_req *req)
1381 {
1382         struct dasd_device *device;
1383         unsigned long flags;
1384
1385         device = req->device;
1386         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1387         req->status = DASD_CQR_QUEUED;
1388         req->device = device;
1389         list_add(&req->list, &device->ccw_queue);
1390         /* let the bh start the request to keep them in order */
1391         dasd_schedule_bh(device);
1392         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1393 }
1394
1395 /*
1396  * Queue a request to the tail of the ccw_queue. Start the I/O if
1397  * possible.
1398  */
1399 void
1400 dasd_add_request_tail(struct dasd_ccw_req *req)
1401 {
1402         struct dasd_device *device;
1403         unsigned long flags;
1404
1405         device = req->device;
1406         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1407         req->status = DASD_CQR_QUEUED;
1408         req->device = device;
1409         list_add_tail(&req->list, &device->ccw_queue);
1410         /* let the bh start the request to keep them in order */
1411         dasd_schedule_bh(device);
1412         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1413 }
1414
1415 /*
1416  * Wakeup callback.
1417  */
1418 static void
1419 dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1420 {
1421         wake_up((wait_queue_head_t *) data);
1422 }
1423
1424 static inline int
1425 _wait_for_wakeup(struct dasd_ccw_req *cqr)
1426 {
1427         struct dasd_device *device;
1428         int rc;
1429
1430         device = cqr->device;
1431         spin_lock_irq(get_ccwdev_lock(device->cdev));
1432         rc = ((cqr->status == DASD_CQR_DONE ||
1433                cqr->status == DASD_CQR_FAILED) &&
1434               list_empty(&cqr->list));
1435         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1436         return rc;
1437 }
1438
1439 /*
1440  * Attempts to start a special ccw queue and waits for its completion.
1441  */
1442 int
1443 dasd_sleep_on(struct dasd_ccw_req * cqr)
1444 {
1445         wait_queue_head_t wait_q;
1446         struct dasd_device *device;
1447         int rc;
1448         
1449         device = cqr->device;
1450         spin_lock_irq(get_ccwdev_lock(device->cdev));
1451         
1452         init_waitqueue_head (&wait_q);
1453         cqr->callback = dasd_wakeup_cb;
1454         cqr->callback_data = (void *) &wait_q;
1455         cqr->status = DASD_CQR_QUEUED;
1456         list_add_tail(&cqr->list, &device->ccw_queue);
1457         
1458         /* let the bh start the request to keep them in order */
1459         dasd_schedule_bh(device);
1460         
1461         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1462
1463         wait_event(wait_q, _wait_for_wakeup(cqr));
1464         
1465         /* Request status is either done or failed. */
1466         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1467         return rc;
1468 }
1469
1470 /*
1471  * Attempts to start a special ccw queue and wait interruptible
1472  * for its completion.
1473  */
1474 int
1475 dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1476 {
1477         wait_queue_head_t wait_q;
1478         struct dasd_device *device;
1479         int rc, finished;
1480
1481         device = cqr->device;
1482         spin_lock_irq(get_ccwdev_lock(device->cdev));
1483
1484         init_waitqueue_head (&wait_q);
1485         cqr->callback = dasd_wakeup_cb;
1486         cqr->callback_data = (void *) &wait_q;
1487         cqr->status = DASD_CQR_QUEUED;
1488         list_add_tail(&cqr->list, &device->ccw_queue);
1489
1490         /* let the bh start the request to keep them in order */
1491         dasd_schedule_bh(device);
1492         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1493
1494         finished = 0;
1495         while (!finished) {
1496                 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1497                 if (rc != -ERESTARTSYS) {
1498                         /* Request is final (done or failed) */
1499                         rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1500                         break;
1501                 }
1502                 spin_lock_irq(get_ccwdev_lock(device->cdev));
1503                 switch (cqr->status) {
1504                 case DASD_CQR_IN_IO:
1505                         /* terminate runnig cqr */
1506                         if (device->discipline->term_IO) {
1507                                 cqr->retries = -1;
1508                                 device->discipline->term_IO(cqr);
1509                                 /*nished =
1510                                  * wait (non-interruptible) for final status
1511                                  * because signal ist still pending
1512                                  */
1513                                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1514                                 wait_event(wait_q, _wait_for_wakeup(cqr));
1515                                 spin_lock_irq(get_ccwdev_lock(device->cdev));
1516                                 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1517                                 finished = 1;
1518                         }
1519                         break;
1520                 case DASD_CQR_QUEUED:
1521                         /* request  */
1522                         list_del_init(&cqr->list);
1523                         rc = -EIO;
1524                         finished = 1;
1525                         break;
1526                 default:
1527                         /* cqr with 'non-interruptable' status - just wait */
1528                         break;
1529                 }
1530                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1531         }
1532         return rc;
1533 }
1534
1535 /*
1536  * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1537  * for eckd devices) the currently running request has to be terminated
1538  * and be put back to status queued, before the special request is added
1539  * to the head of the queue. Then the special request is waited on normally.
1540  */
1541 static inline int
1542 _dasd_term_running_cqr(struct dasd_device *device)
1543 {
1544         struct dasd_ccw_req *cqr;
1545         int rc;
1546
1547         if (list_empty(&device->ccw_queue))
1548                 return 0;
1549         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1550         rc = device->discipline->term_IO(cqr);
1551         if (rc == 0) {
1552                 /* termination successful */
1553                 cqr->status = DASD_CQR_QUEUED;
1554                 cqr->startclk = cqr->stopclk = 0;
1555                 cqr->starttime = 0;
1556         }
1557         return rc;
1558 }
1559
1560 int
1561 dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1562 {
1563         wait_queue_head_t wait_q;
1564         struct dasd_device *device;
1565         int rc;
1566         
1567         device = cqr->device;
1568         spin_lock_irq(get_ccwdev_lock(device->cdev));
1569         rc = _dasd_term_running_cqr(device);
1570         if (rc) {
1571                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1572                 return rc;
1573         }
1574         
1575         init_waitqueue_head (&wait_q);
1576         cqr->callback = dasd_wakeup_cb;
1577         cqr->callback_data = (void *) &wait_q;
1578         cqr->status = DASD_CQR_QUEUED;
1579         list_add(&cqr->list, &device->ccw_queue);
1580         
1581         /* let the bh start the request to keep them in order */
1582         dasd_schedule_bh(device);
1583         
1584         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1585
1586         wait_event(wait_q, _wait_for_wakeup(cqr));
1587         
1588         /* Request status is either done or failed. */
1589         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1590         return rc;
1591 }
1592
1593 /*
1594  * Cancels a request that was started with dasd_sleep_on_req.
1595  * This is useful to timeout requests. The request will be
1596  * terminated if it is currently in i/o.
1597  * Returns 1 if the request has been terminated.
1598  */
1599 int
1600 dasd_cancel_req(struct dasd_ccw_req *cqr)
1601 {
1602         struct dasd_device *device = cqr->device;
1603         unsigned long flags;
1604         int rc;
1605
1606         rc = 0;
1607         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1608         switch (cqr->status) {
1609         case DASD_CQR_QUEUED:
1610                 /* request was not started - just set to failed */
1611                 cqr->status = DASD_CQR_FAILED;
1612                 break;
1613         case DASD_CQR_IN_IO:
1614                 /* request in IO - terminate IO and release again */
1615                 if (device->discipline->term_IO(cqr) != 0)
1616                         /* what to do if unable to terminate ??????
1617                            e.g. not _IN_IO */
1618                         cqr->status = DASD_CQR_FAILED;
1619                 cqr->stopclk = get_clock();
1620                 rc = 1;
1621                 break;
1622         case DASD_CQR_DONE:
1623         case DASD_CQR_FAILED:
1624                 /* already finished - do nothing */
1625                 break;
1626         default:
1627                 DEV_MESSAGE(KERN_ALERT, device,
1628                             "invalid status %02x in request",
1629                             cqr->status);
1630                 BUG();
1631
1632         }
1633         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1634         dasd_schedule_bh(device);
1635         return rc;
1636 }
1637
1638 /*
1639  * SECTION: Block device operations (request queue, partitions, open, release).
1640  */
1641
1642 /*
1643  * Dasd request queue function. Called from ll_rw_blk.c
1644  */
1645 static void
1646 do_dasd_request(request_queue_t * queue)
1647 {
1648         struct dasd_device *device;
1649
1650         device = (struct dasd_device *) queue->queuedata;
1651         spin_lock(get_ccwdev_lock(device->cdev));
1652         /* Get new request from the block device request queue */
1653         __dasd_process_blk_queue(device);
1654         /* Now check if the head of the ccw queue needs to be started. */
1655         __dasd_start_head(device);
1656         spin_unlock(get_ccwdev_lock(device->cdev));
1657 }
1658
1659 /*
1660  * Allocate and initialize request queue and default I/O scheduler.
1661  */
1662 static int
1663 dasd_alloc_queue(struct dasd_device * device)
1664 {
1665         int rc;
1666
1667         device->request_queue = blk_init_queue(do_dasd_request,
1668                                                &device->request_queue_lock);
1669         if (device->request_queue == NULL)
1670                 return -ENOMEM;
1671
1672         device->request_queue->queuedata = device;
1673
1674         elevator_exit(device->request_queue->elevator);
1675         rc = elevator_init(device->request_queue, "deadline");
1676         if (rc) {
1677                 blk_cleanup_queue(device->request_queue);
1678                 return rc;
1679         }
1680         return 0;
1681 }
1682
1683 /*
1684  * Allocate and initialize request queue.
1685  */
1686 static void
1687 dasd_setup_queue(struct dasd_device * device)
1688 {
1689         int max;
1690
1691         blk_queue_hardsect_size(device->request_queue, device->bp_block);
1692         max = device->discipline->max_blocks << device->s2b_shift;
1693         blk_queue_max_sectors(device->request_queue, max);
1694         blk_queue_max_phys_segments(device->request_queue, -1L);
1695         blk_queue_max_hw_segments(device->request_queue, -1L);
1696         blk_queue_max_segment_size(device->request_queue, -1L);
1697         blk_queue_segment_boundary(device->request_queue, -1L);
1698         blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
1699 }
1700
1701 /*
1702  * Deactivate and free request queue.
1703  */
1704 static void
1705 dasd_free_queue(struct dasd_device * device)
1706 {
1707         if (device->request_queue) {
1708                 blk_cleanup_queue(device->request_queue);
1709                 device->request_queue = NULL;
1710         }
1711 }
1712
1713 /*
1714  * Flush request on the request queue.
1715  */
1716 static void
1717 dasd_flush_request_queue(struct dasd_device * device)
1718 {
1719         struct request *req;
1720
1721         if (!device->request_queue)
1722                 return;
1723         
1724         spin_lock_irq(&device->request_queue_lock);
1725         while (!list_empty(&device->request_queue->queue_head)) {
1726                 req = elv_next_request(device->request_queue);
1727                 if (req == NULL)
1728                         break;
1729                 dasd_end_request(req, 0);
1730                 blkdev_dequeue_request(req);
1731         }
1732         spin_unlock_irq(&device->request_queue_lock);
1733 }
1734
1735 static int
1736 dasd_open(struct inode *inp, struct file *filp)
1737 {
1738         struct gendisk *disk = inp->i_bdev->bd_disk;
1739         struct dasd_device *device = disk->private_data;
1740         int rc;
1741
1742         atomic_inc(&device->open_count);
1743         if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1744                 rc = -ENODEV;
1745                 goto unlock;
1746         }
1747
1748         if (!try_module_get(device->discipline->owner)) {
1749                 rc = -EINVAL;
1750                 goto unlock;
1751         }
1752
1753         if (dasd_probeonly) {
1754                 DEV_MESSAGE(KERN_INFO, device, "%s",
1755                             "No access to device due to probeonly mode");
1756                 rc = -EPERM;
1757                 goto out;
1758         }
1759
1760         if (device->state <= DASD_STATE_BASIC) {
1761                 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1762                               " Cannot open unrecognized device");
1763                 rc = -ENODEV;
1764                 goto out;
1765         }
1766
1767         return 0;
1768
1769 out:
1770         module_put(device->discipline->owner);
1771 unlock:
1772         atomic_dec(&device->open_count);
1773         return rc;
1774 }
1775
1776 static int
1777 dasd_release(struct inode *inp, struct file *filp)
1778 {
1779         struct gendisk *disk = inp->i_bdev->bd_disk;
1780         struct dasd_device *device = disk->private_data;
1781
1782         atomic_dec(&device->open_count);
1783         module_put(device->discipline->owner);
1784         return 0;
1785 }
1786
1787 /*
1788  * Return disk geometry.
1789  */
1790 static int
1791 dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1792 {
1793         struct dasd_device *device;
1794
1795         device = bdev->bd_disk->private_data;
1796         if (!device)
1797                 return -ENODEV;
1798
1799         if (!device->discipline ||
1800             !device->discipline->fill_geometry)
1801                 return -EINVAL;
1802
1803         device->discipline->fill_geometry(device, geo);
1804         geo->start = get_start_sect(bdev) >> device->s2b_shift;
1805         return 0;
1806 }
1807
1808 struct block_device_operations
1809 dasd_device_operations = {
1810         .owner          = THIS_MODULE,
1811         .open           = dasd_open,
1812         .release        = dasd_release,
1813         .ioctl          = dasd_ioctl,
1814         .compat_ioctl   = dasd_compat_ioctl,
1815         .getgeo         = dasd_getgeo,
1816 };
1817
1818
1819 static void
1820 dasd_exit(void)
1821 {
1822 #ifdef CONFIG_PROC_FS
1823         dasd_proc_exit();
1824 #endif
1825         dasd_eer_exit();
1826         if (dasd_page_cache != NULL) {
1827                 kmem_cache_destroy(dasd_page_cache);
1828                 dasd_page_cache = NULL;
1829         }
1830         dasd_gendisk_exit();
1831         dasd_devmap_exit();
1832         devfs_remove("dasd");
1833         if (dasd_debug_area != NULL) {
1834                 debug_unregister(dasd_debug_area);
1835                 dasd_debug_area = NULL;
1836         }
1837 }
1838
1839 /*
1840  * SECTION: common functions for ccw_driver use
1841  */
1842
1843 /*
1844  * Initial attempt at a probe function. this can be simplified once
1845  * the other detection code is gone.
1846  */
1847 int
1848 dasd_generic_probe (struct ccw_device *cdev,
1849                     struct dasd_discipline *discipline)
1850 {
1851         int ret;
1852
1853         ret = dasd_add_sysfs_files(cdev);
1854         if (ret) {
1855                 printk(KERN_WARNING
1856                        "dasd_generic_probe: could not add sysfs entries "
1857                        "for %s\n", cdev->dev.bus_id);
1858         } else {
1859                 cdev->handler = &dasd_int_handler;
1860         }
1861
1862         return ret;
1863 }
1864
1865 /*
1866  * This will one day be called from a global not_oper handler.
1867  * It is also used by driver_unregister during module unload.
1868  */
1869 void
1870 dasd_generic_remove (struct ccw_device *cdev)
1871 {
1872         struct dasd_device *device;
1873
1874         cdev->handler = NULL;
1875
1876         dasd_remove_sysfs_files(cdev);
1877         device = dasd_device_from_cdev(cdev);
1878         if (IS_ERR(device))
1879                 return;
1880         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1881                 /* Already doing offline processing */
1882                 dasd_put_device(device);
1883                 return;
1884         }
1885         /*
1886          * This device is removed unconditionally. Set offline
1887          * flag to prevent dasd_open from opening it while it is
1888          * no quite down yet.
1889          */
1890         dasd_set_target_state(device, DASD_STATE_NEW);
1891         /* dasd_delete_device destroys the device reference. */
1892         dasd_delete_device(device);
1893 }
1894
1895 /*
1896  * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1897  * the device is detected for the first time and is supposed to be used
1898  * or the user has started activation through sysfs.
1899  */
1900 int
1901 dasd_generic_set_online (struct ccw_device *cdev,
1902                          struct dasd_discipline *base_discipline)
1903
1904 {
1905         struct dasd_discipline *discipline;
1906         struct dasd_device *device;
1907         int rc;
1908
1909         device = dasd_create_device(cdev);
1910         if (IS_ERR(device))
1911                 return PTR_ERR(device);
1912
1913         discipline = base_discipline;
1914         if (device->features & DASD_FEATURE_USEDIAG) {
1915                 if (!dasd_diag_discipline_pointer) {
1916                         printk (KERN_WARNING
1917                                 "dasd_generic couldn't online device %s "
1918                                 "- discipline DIAG not available\n",
1919                                 cdev->dev.bus_id);
1920                         dasd_delete_device(device);
1921                         return -ENODEV;
1922                 }
1923                 discipline = dasd_diag_discipline_pointer;
1924         }
1925         if (!try_module_get(base_discipline->owner)) {
1926                 dasd_delete_device(device);
1927                 return -EINVAL;
1928         }
1929         if (!try_module_get(discipline->owner)) {
1930                 module_put(base_discipline->owner);
1931                 dasd_delete_device(device);
1932                 return -EINVAL;
1933         }
1934         device->base_discipline = base_discipline;
1935         device->discipline = discipline;
1936
1937         rc = discipline->check_device(device);
1938         if (rc) {
1939                 printk (KERN_WARNING
1940                         "dasd_generic couldn't online device %s "
1941                         "with discipline %s rc=%i\n",
1942                         cdev->dev.bus_id, discipline->name, rc);
1943                 module_put(discipline->owner);
1944                 module_put(base_discipline->owner);
1945                 dasd_delete_device(device);
1946                 return rc;
1947         }
1948
1949         dasd_set_target_state(device, DASD_STATE_ONLINE);
1950         if (device->state <= DASD_STATE_KNOWN) {
1951                 printk (KERN_WARNING
1952                         "dasd_generic discipline not found for %s\n",
1953                         cdev->dev.bus_id);
1954                 rc = -ENODEV;
1955                 dasd_set_target_state(device, DASD_STATE_NEW);
1956                 dasd_delete_device(device);
1957         } else
1958                 pr_debug("dasd_generic device %s found\n",
1959                                 cdev->dev.bus_id);
1960
1961         /* FIXME: we have to wait for the root device but we don't want
1962          * to wait for each single device but for all at once. */
1963         wait_event(dasd_init_waitq, _wait_for_device(device));
1964
1965         dasd_put_device(device);
1966
1967         return rc;
1968 }
1969
1970 int
1971 dasd_generic_set_offline (struct ccw_device *cdev)
1972 {
1973         struct dasd_device *device;
1974         int max_count;
1975
1976         device = dasd_device_from_cdev(cdev);
1977         if (IS_ERR(device))
1978                 return PTR_ERR(device);
1979         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1980                 /* Already doing offline processing */
1981                 dasd_put_device(device);
1982                 return 0;
1983         }
1984         /*
1985          * We must make sure that this device is currently not in use.
1986          * The open_count is increased for every opener, that includes
1987          * the blkdev_get in dasd_scan_partitions. We are only interested
1988          * in the other openers.
1989          */
1990         max_count = device->bdev ? 0 : -1;
1991         if (atomic_read(&device->open_count) > max_count) {
1992                 printk (KERN_WARNING "Can't offline dasd device with open"
1993                         " count = %i.\n",
1994                         atomic_read(&device->open_count));
1995                 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
1996                 dasd_put_device(device);
1997                 return -EBUSY;
1998         }
1999         dasd_set_target_state(device, DASD_STATE_NEW);
2000         /* dasd_delete_device destroys the device reference. */
2001         dasd_delete_device(device);
2002
2003         return 0;
2004 }
2005
2006 int
2007 dasd_generic_notify(struct ccw_device *cdev, int event)
2008 {
2009         struct dasd_device *device;
2010         struct dasd_ccw_req *cqr;
2011         unsigned long flags;
2012         int ret;
2013
2014         device = dasd_device_from_cdev(cdev);
2015         if (IS_ERR(device))
2016                 return 0;
2017         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2018         ret = 0;
2019         switch (event) {
2020         case CIO_GONE:
2021         case CIO_NO_PATH:
2022                 /* First of all call extended error reporting. */
2023                 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2024
2025                 if (device->state < DASD_STATE_BASIC)
2026                         break;
2027                 /* Device is active. We want to keep it. */
2028                 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2029                         list_for_each_entry(cqr, &device->ccw_queue, list)
2030                                 if (cqr->status == DASD_CQR_IN_IO)
2031                                         cqr->status = DASD_CQR_FAILED;
2032                         device->stopped |= DASD_STOPPED_DC_EIO;
2033                 } else {
2034                         list_for_each_entry(cqr, &device->ccw_queue, list)
2035                                 if (cqr->status == DASD_CQR_IN_IO) {
2036                                         cqr->status = DASD_CQR_QUEUED;
2037                                         cqr->retries++;
2038                                 }
2039                         device->stopped |= DASD_STOPPED_DC_WAIT;
2040                         dasd_set_timer(device, 0);
2041                 }
2042                 dasd_schedule_bh(device);
2043                 ret = 1;
2044                 break;
2045         case CIO_OPER:
2046                 /* FIXME: add a sanity check. */
2047                 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2048                 dasd_schedule_bh(device);
2049                 ret = 1;
2050                 break;
2051         }
2052         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2053         dasd_put_device(device);
2054         return ret;
2055 }
2056
2057 /*
2058  * Automatically online either all dasd devices (dasd_autodetect) or
2059  * all devices specified with dasd= parameters.
2060  */
2061 static int
2062 __dasd_auto_online(struct device *dev, void *data)
2063 {
2064         struct ccw_device *cdev;
2065
2066         cdev = to_ccwdev(dev);
2067         if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
2068                 ccw_device_set_online(cdev);
2069         return 0;
2070 }
2071
2072 void
2073 dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
2074 {
2075         struct device_driver *drv;
2076
2077         drv = get_driver(&dasd_discipline_driver->driver);
2078         driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
2079         put_driver(drv);
2080 }
2081
2082
2083 static int __init
2084 dasd_init(void)
2085 {
2086         int rc;
2087
2088         init_waitqueue_head(&dasd_init_waitq);
2089
2090         /* register 'common' DASD debug area, used for all DBF_XXX calls */
2091         dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
2092         if (dasd_debug_area == NULL) {
2093                 rc = -ENOMEM;
2094                 goto failed;
2095         }
2096         debug_register_view(dasd_debug_area, &debug_sprintf_view);
2097         debug_set_level(dasd_debug_area, DBF_EMERG);
2098
2099         DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2100
2101         dasd_diag_discipline_pointer = NULL;
2102
2103         rc = devfs_mk_dir("dasd");
2104         if (rc)
2105                 goto failed;
2106         rc = dasd_devmap_init();
2107         if (rc)
2108                 goto failed;
2109         rc = dasd_gendisk_init();
2110         if (rc)
2111                 goto failed;
2112         rc = dasd_parse();
2113         if (rc)
2114                 goto failed;
2115         rc = dasd_eer_init();
2116         if (rc)
2117                 goto failed;
2118 #ifdef CONFIG_PROC_FS
2119         rc = dasd_proc_init();
2120         if (rc)
2121                 goto failed;
2122 #endif
2123
2124         return 0;
2125 failed:
2126         MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2127         dasd_exit();
2128         return rc;
2129 }
2130
2131 module_init(dasd_init);
2132 module_exit(dasd_exit);
2133
2134 EXPORT_SYMBOL(dasd_debug_area);
2135 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2136
2137 EXPORT_SYMBOL(dasd_add_request_head);
2138 EXPORT_SYMBOL(dasd_add_request_tail);
2139 EXPORT_SYMBOL(dasd_cancel_req);
2140 EXPORT_SYMBOL(dasd_clear_timer);
2141 EXPORT_SYMBOL(dasd_enable_device);
2142 EXPORT_SYMBOL(dasd_int_handler);
2143 EXPORT_SYMBOL(dasd_kfree_request);
2144 EXPORT_SYMBOL(dasd_kick_device);
2145 EXPORT_SYMBOL(dasd_kmalloc_request);
2146 EXPORT_SYMBOL(dasd_schedule_bh);
2147 EXPORT_SYMBOL(dasd_set_target_state);
2148 EXPORT_SYMBOL(dasd_set_timer);
2149 EXPORT_SYMBOL(dasd_sfree_request);
2150 EXPORT_SYMBOL(dasd_sleep_on);
2151 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2152 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2153 EXPORT_SYMBOL(dasd_smalloc_request);
2154 EXPORT_SYMBOL(dasd_start_IO);
2155 EXPORT_SYMBOL(dasd_term_IO);
2156
2157 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2158 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2159 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2160 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2161 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2162 EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
2163
2164 /*
2165  * Overrides for Emacs so that we follow Linus's tabbing style.
2166  * Emacs will notice this stuff at the end of the file and automatically
2167  * adjust the settings for this buffer only.  This must remain at the end
2168  * of the file.
2169  * ---------------------------------------------------------------------------
2170  * Local variables:
2171  * c-indent-level: 4
2172  * c-brace-imaginary-offset: 0
2173  * c-brace-offset: -4
2174  * c-argdecl-indent: 4
2175  * c-label-offset: -4
2176  * c-continued-statement-offset: 4
2177  * c-continued-brace-offset: 0
2178  * indent-tabs-mode: 1
2179  * tab-width: 8
2180  * End:
2181  */