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