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