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