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