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