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