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