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