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