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