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