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