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