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