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