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