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