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