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