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