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