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