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