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