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