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