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