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