xref: /openbmc/linux/drivers/s390/block/dasd.c (revision 1f9f6a78)
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 			/*
1381 			 * device not valid so no I/O could be running
1382 			 * handle CQR as termination successful
1383 			 */
1384 			cqr->status = DASD_CQR_CLEARED;
1385 			cqr->stopclk = get_tod_clock();
1386 			cqr->starttime = 0;
1387 			/* no retries for invalid devices */
1388 			cqr->retries = -1;
1389 			DBF_DEV_EVENT(DBF_ERR, device, "%s",
1390 				      "EINVAL, handle as terminated");
1391 			/* fake rc to success */
1392 			rc = 0;
1393 			break;
1394 		case -EBUSY:
1395 			DBF_DEV_EVENT(DBF_ERR, device, "%s",
1396 				      "device busy, retry later");
1397 			break;
1398 		default:
1399 			/* internal error 10 - unknown rc*/
1400 			snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1401 			dev_err(&device->cdev->dev, "An error occurred in the "
1402 				"DASD device driver, reason=%s\n", errorstring);
1403 			BUG();
1404 			break;
1405 		}
1406 		retries++;
1407 	}
1408 	dasd_schedule_device_bh(device);
1409 	return rc;
1410 }
1411 EXPORT_SYMBOL(dasd_term_IO);
1412 
1413 /*
1414  * Start the i/o. This start_IO can fail if the channel is really busy.
1415  * In that case set up a timer to start the request later.
1416  */
1417 int dasd_start_IO(struct dasd_ccw_req *cqr)
1418 {
1419 	struct dasd_device *device;
1420 	int rc;
1421 	char errorstring[ERRORLENGTH];
1422 
1423 	/* Check the cqr */
1424 	rc = dasd_check_cqr(cqr);
1425 	if (rc) {
1426 		cqr->intrc = rc;
1427 		return rc;
1428 	}
1429 	device = (struct dasd_device *) cqr->startdev;
1430 	if (((cqr->block &&
1431 	      test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1432 	     test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1433 	    !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1434 		DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1435 			      "because of stolen lock", cqr);
1436 		cqr->status = DASD_CQR_ERROR;
1437 		cqr->intrc = -EPERM;
1438 		return -EPERM;
1439 	}
1440 	if (cqr->retries < 0) {
1441 		/* internal error 14 - start_IO run out of retries */
1442 		sprintf(errorstring, "14 %p", cqr);
1443 		dev_err(&device->cdev->dev, "An error occurred in the DASD "
1444 			"device driver, reason=%s\n", errorstring);
1445 		cqr->status = DASD_CQR_ERROR;
1446 		return -EIO;
1447 	}
1448 	cqr->startclk = get_tod_clock();
1449 	cqr->starttime = jiffies;
1450 	cqr->retries--;
1451 	if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1452 		cqr->lpm &= device->path_data.opm;
1453 		if (!cqr->lpm)
1454 			cqr->lpm = device->path_data.opm;
1455 	}
1456 	if (cqr->cpmode == 1) {
1457 		rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1458 					 (long) cqr, cqr->lpm);
1459 	} else {
1460 		rc = ccw_device_start(device->cdev, cqr->cpaddr,
1461 				      (long) cqr, cqr->lpm, 0);
1462 	}
1463 	switch (rc) {
1464 	case 0:
1465 		cqr->status = DASD_CQR_IN_IO;
1466 		break;
1467 	case -EBUSY:
1468 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1469 			      "start_IO: device busy, retry later");
1470 		break;
1471 	case -ETIMEDOUT:
1472 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1473 			      "start_IO: request timeout, retry later");
1474 		break;
1475 	case -EACCES:
1476 		/* -EACCES indicates that the request used only a subset of the
1477 		 * available paths and all these paths are gone. If the lpm of
1478 		 * this request was only a subset of the opm (e.g. the ppm) then
1479 		 * we just do a retry with all available paths.
1480 		 * If we already use the full opm, something is amiss, and we
1481 		 * need a full path verification.
1482 		 */
1483 		if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1484 			DBF_DEV_EVENT(DBF_WARNING, device,
1485 				      "start_IO: selected paths gone (%x)",
1486 				      cqr->lpm);
1487 		} else if (cqr->lpm != device->path_data.opm) {
1488 			cqr->lpm = device->path_data.opm;
1489 			DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1490 				      "start_IO: selected paths gone,"
1491 				      " retry on all paths");
1492 		} else {
1493 			DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1494 				      "start_IO: all paths in opm gone,"
1495 				      " do path verification");
1496 			dasd_generic_last_path_gone(device);
1497 			device->path_data.opm = 0;
1498 			device->path_data.ppm = 0;
1499 			device->path_data.npm = 0;
1500 			device->path_data.tbvpm =
1501 				ccw_device_get_path_mask(device->cdev);
1502 		}
1503 		break;
1504 	case -ENODEV:
1505 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1506 			      "start_IO: -ENODEV device gone, retry");
1507 		break;
1508 	case -EIO:
1509 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1510 			      "start_IO: -EIO device gone, retry");
1511 		break;
1512 	case -EINVAL:
1513 		/* most likely caused in power management context */
1514 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1515 			      "start_IO: -EINVAL device currently "
1516 			      "not accessible");
1517 		break;
1518 	default:
1519 		/* internal error 11 - unknown rc */
1520 		snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1521 		dev_err(&device->cdev->dev,
1522 			"An error occurred in the DASD device driver, "
1523 			"reason=%s\n", errorstring);
1524 		BUG();
1525 		break;
1526 	}
1527 	cqr->intrc = rc;
1528 	return rc;
1529 }
1530 EXPORT_SYMBOL(dasd_start_IO);
1531 
1532 /*
1533  * Timeout function for dasd devices. This is used for different purposes
1534  *  1) missing interrupt handler for normal operation
1535  *  2) delayed start of request where start_IO failed with -EBUSY
1536  *  3) timeout for missing state change interrupts
1537  * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1538  * DASD_CQR_QUEUED for 2) and 3).
1539  */
1540 static void dasd_device_timeout(unsigned long ptr)
1541 {
1542 	unsigned long flags;
1543 	struct dasd_device *device;
1544 
1545 	device = (struct dasd_device *) ptr;
1546 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1547 	/* re-activate request queue */
1548 	dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1549 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1550 	dasd_schedule_device_bh(device);
1551 }
1552 
1553 /*
1554  * Setup timeout for a device in jiffies.
1555  */
1556 void dasd_device_set_timer(struct dasd_device *device, int expires)
1557 {
1558 	if (expires == 0)
1559 		del_timer(&device->timer);
1560 	else
1561 		mod_timer(&device->timer, jiffies + expires);
1562 }
1563 EXPORT_SYMBOL(dasd_device_set_timer);
1564 
1565 /*
1566  * Clear timeout for a device.
1567  */
1568 void dasd_device_clear_timer(struct dasd_device *device)
1569 {
1570 	del_timer(&device->timer);
1571 }
1572 EXPORT_SYMBOL(dasd_device_clear_timer);
1573 
1574 static void dasd_handle_killed_request(struct ccw_device *cdev,
1575 				       unsigned long intparm)
1576 {
1577 	struct dasd_ccw_req *cqr;
1578 	struct dasd_device *device;
1579 
1580 	if (!intparm)
1581 		return;
1582 	cqr = (struct dasd_ccw_req *) intparm;
1583 	if (cqr->status != DASD_CQR_IN_IO) {
1584 		DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1585 				"invalid status in handle_killed_request: "
1586 				"%02x", cqr->status);
1587 		return;
1588 	}
1589 
1590 	device = dasd_device_from_cdev_locked(cdev);
1591 	if (IS_ERR(device)) {
1592 		DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1593 				"unable to get device from cdev");
1594 		return;
1595 	}
1596 
1597 	if (!cqr->startdev ||
1598 	    device != cqr->startdev ||
1599 	    strncmp(cqr->startdev->discipline->ebcname,
1600 		    (char *) &cqr->magic, 4)) {
1601 		DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1602 				"invalid device in request");
1603 		dasd_put_device(device);
1604 		return;
1605 	}
1606 
1607 	/* Schedule request to be retried. */
1608 	cqr->status = DASD_CQR_QUEUED;
1609 
1610 	dasd_device_clear_timer(device);
1611 	dasd_schedule_device_bh(device);
1612 	dasd_put_device(device);
1613 }
1614 
1615 void dasd_generic_handle_state_change(struct dasd_device *device)
1616 {
1617 	/* First of all start sense subsystem status request. */
1618 	dasd_eer_snss(device);
1619 
1620 	dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1621 	dasd_schedule_device_bh(device);
1622 	if (device->block)
1623 		dasd_schedule_block_bh(device->block);
1624 }
1625 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1626 
1627 /*
1628  * Interrupt handler for "normal" ssch-io based dasd devices.
1629  */
1630 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1631 		      struct irb *irb)
1632 {
1633 	struct dasd_ccw_req *cqr, *next;
1634 	struct dasd_device *device;
1635 	unsigned long long now;
1636 	int expires;
1637 
1638 	if (IS_ERR(irb)) {
1639 		switch (PTR_ERR(irb)) {
1640 		case -EIO:
1641 			break;
1642 		case -ETIMEDOUT:
1643 			DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1644 					"request timed out\n", __func__);
1645 			break;
1646 		default:
1647 			DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1648 					"unknown error %ld\n", __func__,
1649 					PTR_ERR(irb));
1650 		}
1651 		dasd_handle_killed_request(cdev, intparm);
1652 		return;
1653 	}
1654 
1655 	now = get_tod_clock();
1656 	cqr = (struct dasd_ccw_req *) intparm;
1657 	/* check for conditions that should be handled immediately */
1658 	if (!cqr ||
1659 	    !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1660 	      scsw_cstat(&irb->scsw) == 0)) {
1661 		if (cqr)
1662 			memcpy(&cqr->irb, irb, sizeof(*irb));
1663 		device = dasd_device_from_cdev_locked(cdev);
1664 		if (IS_ERR(device))
1665 			return;
1666 		/* ignore unsolicited interrupts for DIAG discipline */
1667 		if (device->discipline == dasd_diag_discipline_pointer) {
1668 			dasd_put_device(device);
1669 			return;
1670 		}
1671 		device->discipline->dump_sense_dbf(device, irb, "int");
1672 		if (device->features & DASD_FEATURE_ERPLOG)
1673 			device->discipline->dump_sense(device, cqr, irb);
1674 		device->discipline->check_for_device_change(device, cqr, irb);
1675 		dasd_put_device(device);
1676 	}
1677 
1678 	/* check for for attention message */
1679 	if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1680 		device = dasd_device_from_cdev_locked(cdev);
1681 		device->discipline->check_attention(device, irb->esw.esw1.lpum);
1682 		dasd_put_device(device);
1683 	}
1684 
1685 	if (!cqr)
1686 		return;
1687 
1688 	device = (struct dasd_device *) cqr->startdev;
1689 	if (!device ||
1690 	    strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1691 		DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1692 				"invalid device in request");
1693 		return;
1694 	}
1695 
1696 	/* Check for clear pending */
1697 	if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1698 	    scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1699 		cqr->status = DASD_CQR_CLEARED;
1700 		dasd_device_clear_timer(device);
1701 		wake_up(&dasd_flush_wq);
1702 		dasd_schedule_device_bh(device);
1703 		return;
1704 	}
1705 
1706 	/* check status - the request might have been killed by dyn detach */
1707 	if (cqr->status != DASD_CQR_IN_IO) {
1708 		DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1709 			      "status %02x", dev_name(&cdev->dev), cqr->status);
1710 		return;
1711 	}
1712 
1713 	next = NULL;
1714 	expires = 0;
1715 	if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1716 	    scsw_cstat(&irb->scsw) == 0) {
1717 		/* request was completed successfully */
1718 		cqr->status = DASD_CQR_SUCCESS;
1719 		cqr->stopclk = now;
1720 		/* Start first request on queue if possible -> fast_io. */
1721 		if (cqr->devlist.next != &device->ccw_queue) {
1722 			next = list_entry(cqr->devlist.next,
1723 					  struct dasd_ccw_req, devlist);
1724 		}
1725 	} else {  /* error */
1726 		/*
1727 		 * If we don't want complex ERP for this request, then just
1728 		 * reset this and retry it in the fastpath
1729 		 */
1730 		if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1731 		    cqr->retries > 0) {
1732 			if (cqr->lpm == device->path_data.opm)
1733 				DBF_DEV_EVENT(DBF_DEBUG, device,
1734 					      "default ERP in fastpath "
1735 					      "(%i retries left)",
1736 					      cqr->retries);
1737 			if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1738 				cqr->lpm = device->path_data.opm;
1739 			cqr->status = DASD_CQR_QUEUED;
1740 			next = cqr;
1741 		} else
1742 			cqr->status = DASD_CQR_ERROR;
1743 	}
1744 	if (next && (next->status == DASD_CQR_QUEUED) &&
1745 	    (!device->stopped)) {
1746 		if (device->discipline->start_IO(next) == 0)
1747 			expires = next->expires;
1748 	}
1749 	if (expires != 0)
1750 		dasd_device_set_timer(device, expires);
1751 	else
1752 		dasd_device_clear_timer(device);
1753 	dasd_schedule_device_bh(device);
1754 }
1755 EXPORT_SYMBOL(dasd_int_handler);
1756 
1757 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1758 {
1759 	struct dasd_device *device;
1760 
1761 	device = dasd_device_from_cdev_locked(cdev);
1762 
1763 	if (IS_ERR(device))
1764 		goto out;
1765 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1766 	   device->state != device->target ||
1767 	   !device->discipline->check_for_device_change){
1768 		dasd_put_device(device);
1769 		goto out;
1770 	}
1771 	if (device->discipline->dump_sense_dbf)
1772 		device->discipline->dump_sense_dbf(device, irb, "uc");
1773 	device->discipline->check_for_device_change(device, NULL, irb);
1774 	dasd_put_device(device);
1775 out:
1776 	return UC_TODO_RETRY;
1777 }
1778 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1779 
1780 /*
1781  * If we have an error on a dasd_block layer request then we cancel
1782  * and return all further requests from the same dasd_block as well.
1783  */
1784 static void __dasd_device_recovery(struct dasd_device *device,
1785 				   struct dasd_ccw_req *ref_cqr)
1786 {
1787 	struct list_head *l, *n;
1788 	struct dasd_ccw_req *cqr;
1789 
1790 	/*
1791 	 * only requeue request that came from the dasd_block layer
1792 	 */
1793 	if (!ref_cqr->block)
1794 		return;
1795 
1796 	list_for_each_safe(l, n, &device->ccw_queue) {
1797 		cqr = list_entry(l, struct dasd_ccw_req, devlist);
1798 		if (cqr->status == DASD_CQR_QUEUED &&
1799 		    ref_cqr->block == cqr->block) {
1800 			cqr->status = DASD_CQR_CLEARED;
1801 		}
1802 	}
1803 };
1804 
1805 /*
1806  * Remove those ccw requests from the queue that need to be returned
1807  * to the upper layer.
1808  */
1809 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1810 					    struct list_head *final_queue)
1811 {
1812 	struct list_head *l, *n;
1813 	struct dasd_ccw_req *cqr;
1814 
1815 	/* Process request with final status. */
1816 	list_for_each_safe(l, n, &device->ccw_queue) {
1817 		cqr = list_entry(l, struct dasd_ccw_req, devlist);
1818 
1819 		/* Skip any non-final request. */
1820 		if (cqr->status == DASD_CQR_QUEUED ||
1821 		    cqr->status == DASD_CQR_IN_IO ||
1822 		    cqr->status == DASD_CQR_CLEAR_PENDING)
1823 			continue;
1824 		if (cqr->status == DASD_CQR_ERROR) {
1825 			__dasd_device_recovery(device, cqr);
1826 		}
1827 		/* Rechain finished requests to final queue */
1828 		list_move_tail(&cqr->devlist, final_queue);
1829 	}
1830 }
1831 
1832 /*
1833  * the cqrs from the final queue are returned to the upper layer
1834  * by setting a dasd_block state and calling the callback function
1835  */
1836 static void __dasd_device_process_final_queue(struct dasd_device *device,
1837 					      struct list_head *final_queue)
1838 {
1839 	struct list_head *l, *n;
1840 	struct dasd_ccw_req *cqr;
1841 	struct dasd_block *block;
1842 	void (*callback)(struct dasd_ccw_req *, void *data);
1843 	void *callback_data;
1844 	char errorstring[ERRORLENGTH];
1845 
1846 	list_for_each_safe(l, n, final_queue) {
1847 		cqr = list_entry(l, struct dasd_ccw_req, devlist);
1848 		list_del_init(&cqr->devlist);
1849 		block = cqr->block;
1850 		callback = cqr->callback;
1851 		callback_data = cqr->callback_data;
1852 		if (block)
1853 			spin_lock_bh(&block->queue_lock);
1854 		switch (cqr->status) {
1855 		case DASD_CQR_SUCCESS:
1856 			cqr->status = DASD_CQR_DONE;
1857 			break;
1858 		case DASD_CQR_ERROR:
1859 			cqr->status = DASD_CQR_NEED_ERP;
1860 			break;
1861 		case DASD_CQR_CLEARED:
1862 			cqr->status = DASD_CQR_TERMINATED;
1863 			break;
1864 		default:
1865 			/* internal error 12 - wrong cqr status*/
1866 			snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1867 			dev_err(&device->cdev->dev,
1868 				"An error occurred in the DASD device driver, "
1869 				"reason=%s\n", errorstring);
1870 			BUG();
1871 		}
1872 		if (cqr->callback != NULL)
1873 			(callback)(cqr, callback_data);
1874 		if (block)
1875 			spin_unlock_bh(&block->queue_lock);
1876 	}
1877 }
1878 
1879 /*
1880  * Take a look at the first request on the ccw queue and check
1881  * if it reached its expire time. If so, terminate the IO.
1882  */
1883 static void __dasd_device_check_expire(struct dasd_device *device)
1884 {
1885 	struct dasd_ccw_req *cqr;
1886 
1887 	if (list_empty(&device->ccw_queue))
1888 		return;
1889 	cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1890 	if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1891 	    (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1892 		if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1893 			/*
1894 			 * IO in safe offline processing should not
1895 			 * run out of retries
1896 			 */
1897 			cqr->retries++;
1898 		}
1899 		if (device->discipline->term_IO(cqr) != 0) {
1900 			/* Hmpf, try again in 5 sec */
1901 			dev_err(&device->cdev->dev,
1902 				"cqr %p timed out (%lus) but cannot be "
1903 				"ended, retrying in 5 s\n",
1904 				cqr, (cqr->expires/HZ));
1905 			cqr->expires += 5*HZ;
1906 			dasd_device_set_timer(device, 5*HZ);
1907 		} else {
1908 			dev_err(&device->cdev->dev,
1909 				"cqr %p timed out (%lus), %i retries "
1910 				"remaining\n", cqr, (cqr->expires/HZ),
1911 				cqr->retries);
1912 		}
1913 	}
1914 }
1915 
1916 /*
1917  * Take a look at the first request on the ccw queue and check
1918  * if it needs to be started.
1919  */
1920 static void __dasd_device_start_head(struct dasd_device *device)
1921 {
1922 	struct dasd_ccw_req *cqr;
1923 	int rc;
1924 
1925 	if (list_empty(&device->ccw_queue))
1926 		return;
1927 	cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1928 	if (cqr->status != DASD_CQR_QUEUED)
1929 		return;
1930 	/* when device is stopped, return request to previous layer
1931 	 * exception: only the disconnect or unresumed bits are set and the
1932 	 * cqr is a path verification request
1933 	 */
1934 	if (device->stopped &&
1935 	    !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1936 	      && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1937 		cqr->intrc = -EAGAIN;
1938 		cqr->status = DASD_CQR_CLEARED;
1939 		dasd_schedule_device_bh(device);
1940 		return;
1941 	}
1942 
1943 	rc = device->discipline->start_IO(cqr);
1944 	if (rc == 0)
1945 		dasd_device_set_timer(device, cqr->expires);
1946 	else if (rc == -EACCES) {
1947 		dasd_schedule_device_bh(device);
1948 	} else
1949 		/* Hmpf, try again in 1/2 sec */
1950 		dasd_device_set_timer(device, 50);
1951 }
1952 
1953 static void __dasd_device_check_path_events(struct dasd_device *device)
1954 {
1955 	int rc;
1956 
1957 	if (device->path_data.tbvpm) {
1958 		if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1959 					DASD_UNRESUMED_PM))
1960 			return;
1961 		rc = device->discipline->verify_path(
1962 			device, device->path_data.tbvpm);
1963 		if (rc)
1964 			dasd_device_set_timer(device, 50);
1965 		else
1966 			device->path_data.tbvpm = 0;
1967 	}
1968 };
1969 
1970 /*
1971  * Go through all request on the dasd_device request queue,
1972  * terminate them on the cdev if necessary, and return them to the
1973  * submitting layer via callback.
1974  * Note:
1975  * Make sure that all 'submitting layers' still exist when
1976  * this function is called!. In other words, when 'device' is a base
1977  * device then all block layer requests must have been removed before
1978  * via dasd_flush_block_queue.
1979  */
1980 int dasd_flush_device_queue(struct dasd_device *device)
1981 {
1982 	struct dasd_ccw_req *cqr, *n;
1983 	int rc;
1984 	struct list_head flush_queue;
1985 
1986 	INIT_LIST_HEAD(&flush_queue);
1987 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1988 	rc = 0;
1989 	list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1990 		/* Check status and move request to flush_queue */
1991 		switch (cqr->status) {
1992 		case DASD_CQR_IN_IO:
1993 			rc = device->discipline->term_IO(cqr);
1994 			if (rc) {
1995 				/* unable to terminate requeust */
1996 				dev_err(&device->cdev->dev,
1997 					"Flushing the DASD request queue "
1998 					"failed for request %p\n", cqr);
1999 				/* stop flush processing */
2000 				goto finished;
2001 			}
2002 			break;
2003 		case DASD_CQR_QUEUED:
2004 			cqr->stopclk = get_tod_clock();
2005 			cqr->status = DASD_CQR_CLEARED;
2006 			break;
2007 		default: /* no need to modify the others */
2008 			break;
2009 		}
2010 		list_move_tail(&cqr->devlist, &flush_queue);
2011 	}
2012 finished:
2013 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
2014 	/*
2015 	 * After this point all requests must be in state CLEAR_PENDING,
2016 	 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2017 	 * one of the others.
2018 	 */
2019 	list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2020 		wait_event(dasd_flush_wq,
2021 			   (cqr->status != DASD_CQR_CLEAR_PENDING));
2022 	/*
2023 	 * Now set each request back to TERMINATED, DONE or NEED_ERP
2024 	 * and call the callback function of flushed requests
2025 	 */
2026 	__dasd_device_process_final_queue(device, &flush_queue);
2027 	return rc;
2028 }
2029 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2030 
2031 /*
2032  * Acquire the device lock and process queues for the device.
2033  */
2034 static void dasd_device_tasklet(struct dasd_device *device)
2035 {
2036 	struct list_head final_queue;
2037 
2038 	atomic_set (&device->tasklet_scheduled, 0);
2039 	INIT_LIST_HEAD(&final_queue);
2040 	spin_lock_irq(get_ccwdev_lock(device->cdev));
2041 	/* Check expire time of first request on the ccw queue. */
2042 	__dasd_device_check_expire(device);
2043 	/* find final requests on ccw queue */
2044 	__dasd_device_process_ccw_queue(device, &final_queue);
2045 	__dasd_device_check_path_events(device);
2046 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
2047 	/* Now call the callback function of requests with final status */
2048 	__dasd_device_process_final_queue(device, &final_queue);
2049 	spin_lock_irq(get_ccwdev_lock(device->cdev));
2050 	/* Now check if the head of the ccw queue needs to be started. */
2051 	__dasd_device_start_head(device);
2052 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
2053 	if (waitqueue_active(&shutdown_waitq))
2054 		wake_up(&shutdown_waitq);
2055 	dasd_put_device(device);
2056 }
2057 
2058 /*
2059  * Schedules a call to dasd_tasklet over the device tasklet.
2060  */
2061 void dasd_schedule_device_bh(struct dasd_device *device)
2062 {
2063 	/* Protect against rescheduling. */
2064 	if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2065 		return;
2066 	dasd_get_device(device);
2067 	tasklet_hi_schedule(&device->tasklet);
2068 }
2069 EXPORT_SYMBOL(dasd_schedule_device_bh);
2070 
2071 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2072 {
2073 	device->stopped |= bits;
2074 }
2075 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2076 
2077 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2078 {
2079 	device->stopped &= ~bits;
2080 	if (!device->stopped)
2081 		wake_up(&generic_waitq);
2082 }
2083 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2084 
2085 /*
2086  * Queue a request to the head of the device ccw_queue.
2087  * Start the I/O if possible.
2088  */
2089 void dasd_add_request_head(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(&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_head);
2103 
2104 /*
2105  * Queue a request to the tail of the device ccw_queue.
2106  * Start the I/O if possible.
2107  */
2108 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2109 {
2110 	struct dasd_device *device;
2111 	unsigned long flags;
2112 
2113 	device = cqr->startdev;
2114 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2115 	cqr->status = DASD_CQR_QUEUED;
2116 	list_add_tail(&cqr->devlist, &device->ccw_queue);
2117 	/* let the bh start the request to keep them in order */
2118 	dasd_schedule_device_bh(device);
2119 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2120 }
2121 EXPORT_SYMBOL(dasd_add_request_tail);
2122 
2123 /*
2124  * Wakeup helper for the 'sleep_on' functions.
2125  */
2126 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2127 {
2128 	spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2129 	cqr->callback_data = DASD_SLEEPON_END_TAG;
2130 	spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2131 	wake_up(&generic_waitq);
2132 }
2133 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2134 
2135 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2136 {
2137 	struct dasd_device *device;
2138 	int rc;
2139 
2140 	device = cqr->startdev;
2141 	spin_lock_irq(get_ccwdev_lock(device->cdev));
2142 	rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2143 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
2144 	return rc;
2145 }
2146 
2147 /*
2148  * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2149  */
2150 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2151 {
2152 	struct dasd_device *device;
2153 	dasd_erp_fn_t erp_fn;
2154 
2155 	if (cqr->status == DASD_CQR_FILLED)
2156 		return 0;
2157 	device = cqr->startdev;
2158 	if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2159 		if (cqr->status == DASD_CQR_TERMINATED) {
2160 			device->discipline->handle_terminated_request(cqr);
2161 			return 1;
2162 		}
2163 		if (cqr->status == DASD_CQR_NEED_ERP) {
2164 			erp_fn = device->discipline->erp_action(cqr);
2165 			erp_fn(cqr);
2166 			return 1;
2167 		}
2168 		if (cqr->status == DASD_CQR_FAILED)
2169 			dasd_log_sense(cqr, &cqr->irb);
2170 		if (cqr->refers) {
2171 			__dasd_process_erp(device, cqr);
2172 			return 1;
2173 		}
2174 	}
2175 	return 0;
2176 }
2177 
2178 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2179 {
2180 	if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2181 		if (cqr->refers) /* erp is not done yet */
2182 			return 1;
2183 		return ((cqr->status != DASD_CQR_DONE) &&
2184 			(cqr->status != DASD_CQR_FAILED));
2185 	} else
2186 		return (cqr->status == DASD_CQR_FILLED);
2187 }
2188 
2189 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2190 {
2191 	struct dasd_device *device;
2192 	int rc;
2193 	struct list_head ccw_queue;
2194 	struct dasd_ccw_req *cqr;
2195 
2196 	INIT_LIST_HEAD(&ccw_queue);
2197 	maincqr->status = DASD_CQR_FILLED;
2198 	device = maincqr->startdev;
2199 	list_add(&maincqr->blocklist, &ccw_queue);
2200 	for (cqr = maincqr;  __dasd_sleep_on_loop_condition(cqr);
2201 	     cqr = list_first_entry(&ccw_queue,
2202 				    struct dasd_ccw_req, blocklist)) {
2203 
2204 		if (__dasd_sleep_on_erp(cqr))
2205 			continue;
2206 		if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2207 			continue;
2208 		if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2209 		    !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2210 			cqr->status = DASD_CQR_FAILED;
2211 			cqr->intrc = -EPERM;
2212 			continue;
2213 		}
2214 		/* Non-temporary stop condition will trigger fail fast */
2215 		if (device->stopped & ~DASD_STOPPED_PENDING &&
2216 		    test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2217 		    (!dasd_eer_enabled(device))) {
2218 			cqr->status = DASD_CQR_FAILED;
2219 			cqr->intrc = -ENOLINK;
2220 			continue;
2221 		}
2222 		/* Don't try to start requests if device is stopped */
2223 		if (interruptible) {
2224 			rc = wait_event_interruptible(
2225 				generic_waitq, !(device->stopped));
2226 			if (rc == -ERESTARTSYS) {
2227 				cqr->status = DASD_CQR_FAILED;
2228 				maincqr->intrc = rc;
2229 				continue;
2230 			}
2231 		} else
2232 			wait_event(generic_waitq, !(device->stopped));
2233 
2234 		if (!cqr->callback)
2235 			cqr->callback = dasd_wakeup_cb;
2236 
2237 		cqr->callback_data = DASD_SLEEPON_START_TAG;
2238 		dasd_add_request_tail(cqr);
2239 		if (interruptible) {
2240 			rc = wait_event_interruptible(
2241 				generic_waitq, _wait_for_wakeup(cqr));
2242 			if (rc == -ERESTARTSYS) {
2243 				dasd_cancel_req(cqr);
2244 				/* wait (non-interruptible) for final status */
2245 				wait_event(generic_waitq,
2246 					   _wait_for_wakeup(cqr));
2247 				cqr->status = DASD_CQR_FAILED;
2248 				maincqr->intrc = rc;
2249 				continue;
2250 			}
2251 		} else
2252 			wait_event(generic_waitq, _wait_for_wakeup(cqr));
2253 	}
2254 
2255 	maincqr->endclk = get_tod_clock();
2256 	if ((maincqr->status != DASD_CQR_DONE) &&
2257 	    (maincqr->intrc != -ERESTARTSYS))
2258 		dasd_log_sense(maincqr, &maincqr->irb);
2259 	if (maincqr->status == DASD_CQR_DONE)
2260 		rc = 0;
2261 	else if (maincqr->intrc)
2262 		rc = maincqr->intrc;
2263 	else
2264 		rc = -EIO;
2265 	return rc;
2266 }
2267 
2268 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2269 {
2270 	struct dasd_ccw_req *cqr;
2271 
2272 	list_for_each_entry(cqr, ccw_queue, blocklist) {
2273 		if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2274 			return 0;
2275 	}
2276 
2277 	return 1;
2278 }
2279 
2280 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2281 {
2282 	struct dasd_device *device;
2283 	struct dasd_ccw_req *cqr, *n;
2284 	int rc;
2285 
2286 retry:
2287 	list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2288 		device = cqr->startdev;
2289 		if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2290 			continue;
2291 
2292 		if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2293 		    !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2294 			cqr->status = DASD_CQR_FAILED;
2295 			cqr->intrc = -EPERM;
2296 			continue;
2297 		}
2298 		/*Non-temporary stop condition will trigger fail fast*/
2299 		if (device->stopped & ~DASD_STOPPED_PENDING &&
2300 		    test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2301 		    !dasd_eer_enabled(device)) {
2302 			cqr->status = DASD_CQR_FAILED;
2303 			cqr->intrc = -EAGAIN;
2304 			continue;
2305 		}
2306 
2307 		/*Don't try to start requests if device is stopped*/
2308 		if (interruptible) {
2309 			rc = wait_event_interruptible(
2310 				generic_waitq, !device->stopped);
2311 			if (rc == -ERESTARTSYS) {
2312 				cqr->status = DASD_CQR_FAILED;
2313 				cqr->intrc = rc;
2314 				continue;
2315 			}
2316 		} else
2317 			wait_event(generic_waitq, !(device->stopped));
2318 
2319 		if (!cqr->callback)
2320 			cqr->callback = dasd_wakeup_cb;
2321 		cqr->callback_data = DASD_SLEEPON_START_TAG;
2322 		dasd_add_request_tail(cqr);
2323 	}
2324 
2325 	wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2326 
2327 	rc = 0;
2328 	list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2329 		/*
2330 		 * for alias devices simplify error recovery and
2331 		 * return to upper layer
2332 		 * do not skip ERP requests
2333 		 */
2334 		if (cqr->startdev != cqr->basedev && !cqr->refers &&
2335 		    (cqr->status == DASD_CQR_TERMINATED ||
2336 		     cqr->status == DASD_CQR_NEED_ERP))
2337 			return -EAGAIN;
2338 
2339 		/* normal recovery for basedev IO */
2340 		if (__dasd_sleep_on_erp(cqr))
2341 			/* handle erp first */
2342 			goto retry;
2343 	}
2344 
2345 	return 0;
2346 }
2347 
2348 /*
2349  * Queue a request to the tail of the device ccw_queue and wait for
2350  * it's completion.
2351  */
2352 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2353 {
2354 	return _dasd_sleep_on(cqr, 0);
2355 }
2356 EXPORT_SYMBOL(dasd_sleep_on);
2357 
2358 /*
2359  * Start requests from a ccw_queue and wait for their completion.
2360  */
2361 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2362 {
2363 	return _dasd_sleep_on_queue(ccw_queue, 0);
2364 }
2365 EXPORT_SYMBOL(dasd_sleep_on_queue);
2366 
2367 /*
2368  * Queue a request to the tail of the device ccw_queue and wait
2369  * interruptible for it's completion.
2370  */
2371 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2372 {
2373 	return _dasd_sleep_on(cqr, 1);
2374 }
2375 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2376 
2377 /*
2378  * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2379  * for eckd devices) the currently running request has to be terminated
2380  * and be put back to status queued, before the special request is added
2381  * to the head of the queue. Then the special request is waited on normally.
2382  */
2383 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2384 {
2385 	struct dasd_ccw_req *cqr;
2386 	int rc;
2387 
2388 	if (list_empty(&device->ccw_queue))
2389 		return 0;
2390 	cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2391 	rc = device->discipline->term_IO(cqr);
2392 	if (!rc)
2393 		/*
2394 		 * CQR terminated because a more important request is pending.
2395 		 * Undo decreasing of retry counter because this is
2396 		 * not an error case.
2397 		 */
2398 		cqr->retries++;
2399 	return rc;
2400 }
2401 
2402 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2403 {
2404 	struct dasd_device *device;
2405 	int rc;
2406 
2407 	device = cqr->startdev;
2408 	if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2409 	    !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2410 		cqr->status = DASD_CQR_FAILED;
2411 		cqr->intrc = -EPERM;
2412 		return -EIO;
2413 	}
2414 	spin_lock_irq(get_ccwdev_lock(device->cdev));
2415 	rc = _dasd_term_running_cqr(device);
2416 	if (rc) {
2417 		spin_unlock_irq(get_ccwdev_lock(device->cdev));
2418 		return rc;
2419 	}
2420 	cqr->callback = dasd_wakeup_cb;
2421 	cqr->callback_data = DASD_SLEEPON_START_TAG;
2422 	cqr->status = DASD_CQR_QUEUED;
2423 	/*
2424 	 * add new request as second
2425 	 * first the terminated cqr needs to be finished
2426 	 */
2427 	list_add(&cqr->devlist, device->ccw_queue.next);
2428 
2429 	/* let the bh start the request to keep them in order */
2430 	dasd_schedule_device_bh(device);
2431 
2432 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
2433 
2434 	wait_event(generic_waitq, _wait_for_wakeup(cqr));
2435 
2436 	if (cqr->status == DASD_CQR_DONE)
2437 		rc = 0;
2438 	else if (cqr->intrc)
2439 		rc = cqr->intrc;
2440 	else
2441 		rc = -EIO;
2442 
2443 	/* kick tasklets */
2444 	dasd_schedule_device_bh(device);
2445 	if (device->block)
2446 		dasd_schedule_block_bh(device->block);
2447 
2448 	return rc;
2449 }
2450 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2451 
2452 /*
2453  * Cancels a request that was started with dasd_sleep_on_req.
2454  * This is useful to timeout requests. The request will be
2455  * terminated if it is currently in i/o.
2456  * Returns 0 if request termination was successful
2457  *	   negative error code if termination failed
2458  * Cancellation of a request is an asynchronous operation! The calling
2459  * function has to wait until the request is properly returned via callback.
2460  */
2461 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2462 {
2463 	struct dasd_device *device = cqr->startdev;
2464 	unsigned long flags;
2465 	int rc;
2466 
2467 	rc = 0;
2468 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2469 	switch (cqr->status) {
2470 	case DASD_CQR_QUEUED:
2471 		/* request was not started - just set to cleared */
2472 		cqr->status = DASD_CQR_CLEARED;
2473 		if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2474 			cqr->callback_data = DASD_SLEEPON_END_TAG;
2475 		break;
2476 	case DASD_CQR_IN_IO:
2477 		/* request in IO - terminate IO and release again */
2478 		rc = device->discipline->term_IO(cqr);
2479 		if (rc) {
2480 			dev_err(&device->cdev->dev,
2481 				"Cancelling request %p failed with rc=%d\n",
2482 				cqr, rc);
2483 		} else {
2484 			cqr->stopclk = get_tod_clock();
2485 		}
2486 		break;
2487 	default: /* already finished or clear pending - do nothing */
2488 		break;
2489 	}
2490 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2491 	dasd_schedule_device_bh(device);
2492 	return rc;
2493 }
2494 EXPORT_SYMBOL(dasd_cancel_req);
2495 
2496 /*
2497  * SECTION: Operations of the dasd_block layer.
2498  */
2499 
2500 /*
2501  * Timeout function for dasd_block. This is used when the block layer
2502  * is waiting for something that may not come reliably, (e.g. a state
2503  * change interrupt)
2504  */
2505 static void dasd_block_timeout(unsigned long ptr)
2506 {
2507 	unsigned long flags;
2508 	struct dasd_block *block;
2509 
2510 	block = (struct dasd_block *) ptr;
2511 	spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2512 	/* re-activate request queue */
2513 	dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2514 	spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2515 	dasd_schedule_block_bh(block);
2516 }
2517 
2518 /*
2519  * Setup timeout for a dasd_block in jiffies.
2520  */
2521 void dasd_block_set_timer(struct dasd_block *block, int expires)
2522 {
2523 	if (expires == 0)
2524 		del_timer(&block->timer);
2525 	else
2526 		mod_timer(&block->timer, jiffies + expires);
2527 }
2528 EXPORT_SYMBOL(dasd_block_set_timer);
2529 
2530 /*
2531  * Clear timeout for a dasd_block.
2532  */
2533 void dasd_block_clear_timer(struct dasd_block *block)
2534 {
2535 	del_timer(&block->timer);
2536 }
2537 EXPORT_SYMBOL(dasd_block_clear_timer);
2538 
2539 /*
2540  * Process finished error recovery ccw.
2541  */
2542 static void __dasd_process_erp(struct dasd_device *device,
2543 			       struct dasd_ccw_req *cqr)
2544 {
2545 	dasd_erp_fn_t erp_fn;
2546 
2547 	if (cqr->status == DASD_CQR_DONE)
2548 		DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2549 	else
2550 		dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2551 	erp_fn = device->discipline->erp_postaction(cqr);
2552 	erp_fn(cqr);
2553 }
2554 
2555 /*
2556  * Fetch requests from the block device queue.
2557  */
2558 static void __dasd_process_request_queue(struct dasd_block *block)
2559 {
2560 	struct request_queue *queue;
2561 	struct request *req;
2562 	struct dasd_ccw_req *cqr;
2563 	struct dasd_device *basedev;
2564 	unsigned long flags;
2565 	queue = block->request_queue;
2566 	basedev = block->base;
2567 	/* No queue ? Then there is nothing to do. */
2568 	if (queue == NULL)
2569 		return;
2570 
2571 	/*
2572 	 * We requeue request from the block device queue to the ccw
2573 	 * queue only in two states. In state DASD_STATE_READY the
2574 	 * partition detection is done and we need to requeue requests
2575 	 * for that. State DASD_STATE_ONLINE is normal block device
2576 	 * operation.
2577 	 */
2578 	if (basedev->state < DASD_STATE_READY) {
2579 		while ((req = blk_fetch_request(block->request_queue)))
2580 			__blk_end_request_all(req, -EIO);
2581 		return;
2582 	}
2583 	/* Now we try to fetch requests from the request queue */
2584 	while ((req = blk_peek_request(queue))) {
2585 		if (basedev->features & DASD_FEATURE_READONLY &&
2586 		    rq_data_dir(req) == WRITE) {
2587 			DBF_DEV_EVENT(DBF_ERR, basedev,
2588 				      "Rejecting write request %p",
2589 				      req);
2590 			blk_start_request(req);
2591 			__blk_end_request_all(req, -EIO);
2592 			continue;
2593 		}
2594 		if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2595 		    (basedev->features & DASD_FEATURE_FAILFAST ||
2596 		     blk_noretry_request(req))) {
2597 			DBF_DEV_EVENT(DBF_ERR, basedev,
2598 				      "Rejecting failfast request %p",
2599 				      req);
2600 			blk_start_request(req);
2601 			__blk_end_request_all(req, -ETIMEDOUT);
2602 			continue;
2603 		}
2604 		cqr = basedev->discipline->build_cp(basedev, block, req);
2605 		if (IS_ERR(cqr)) {
2606 			if (PTR_ERR(cqr) == -EBUSY)
2607 				break;	/* normal end condition */
2608 			if (PTR_ERR(cqr) == -ENOMEM)
2609 				break;	/* terminate request queue loop */
2610 			if (PTR_ERR(cqr) == -EAGAIN) {
2611 				/*
2612 				 * The current request cannot be build right
2613 				 * now, we have to try later. If this request
2614 				 * is the head-of-queue we stop the device
2615 				 * for 1/2 second.
2616 				 */
2617 				if (!list_empty(&block->ccw_queue))
2618 					break;
2619 				spin_lock_irqsave(
2620 					get_ccwdev_lock(basedev->cdev), flags);
2621 				dasd_device_set_stop_bits(basedev,
2622 							  DASD_STOPPED_PENDING);
2623 				spin_unlock_irqrestore(
2624 					get_ccwdev_lock(basedev->cdev), flags);
2625 				dasd_block_set_timer(block, HZ/2);
2626 				break;
2627 			}
2628 			DBF_DEV_EVENT(DBF_ERR, basedev,
2629 				      "CCW creation failed (rc=%ld) "
2630 				      "on request %p",
2631 				      PTR_ERR(cqr), req);
2632 			blk_start_request(req);
2633 			__blk_end_request_all(req, -EIO);
2634 			continue;
2635 		}
2636 		/*
2637 		 *  Note: callback is set to dasd_return_cqr_cb in
2638 		 * __dasd_block_start_head to cover erp requests as well
2639 		 */
2640 		cqr->callback_data = (void *) req;
2641 		cqr->status = DASD_CQR_FILLED;
2642 		req->completion_data = cqr;
2643 		blk_start_request(req);
2644 		list_add_tail(&cqr->blocklist, &block->ccw_queue);
2645 		INIT_LIST_HEAD(&cqr->devlist);
2646 		dasd_profile_start(block, cqr, req);
2647 	}
2648 }
2649 
2650 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2651 {
2652 	struct request *req;
2653 	int status;
2654 	int error = 0;
2655 
2656 	req = (struct request *) cqr->callback_data;
2657 	dasd_profile_end(cqr->block, cqr, req);
2658 	status = cqr->block->base->discipline->free_cp(cqr, req);
2659 	if (status < 0)
2660 		error = status;
2661 	else if (status == 0) {
2662 		if (cqr->intrc == -EPERM)
2663 			error = -EBADE;
2664 		else if (cqr->intrc == -ENOLINK ||
2665 			 cqr->intrc == -ETIMEDOUT)
2666 			error = cqr->intrc;
2667 		else
2668 			error = -EIO;
2669 	}
2670 	__blk_end_request_all(req, error);
2671 }
2672 
2673 /*
2674  * Process ccw request queue.
2675  */
2676 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2677 					   struct list_head *final_queue)
2678 {
2679 	struct list_head *l, *n;
2680 	struct dasd_ccw_req *cqr;
2681 	dasd_erp_fn_t erp_fn;
2682 	unsigned long flags;
2683 	struct dasd_device *base = block->base;
2684 
2685 restart:
2686 	/* Process request with final status. */
2687 	list_for_each_safe(l, n, &block->ccw_queue) {
2688 		cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2689 		if (cqr->status != DASD_CQR_DONE &&
2690 		    cqr->status != DASD_CQR_FAILED &&
2691 		    cqr->status != DASD_CQR_NEED_ERP &&
2692 		    cqr->status != DASD_CQR_TERMINATED)
2693 			continue;
2694 
2695 		if (cqr->status == DASD_CQR_TERMINATED) {
2696 			base->discipline->handle_terminated_request(cqr);
2697 			goto restart;
2698 		}
2699 
2700 		/*  Process requests that may be recovered */
2701 		if (cqr->status == DASD_CQR_NEED_ERP) {
2702 			erp_fn = base->discipline->erp_action(cqr);
2703 			if (IS_ERR(erp_fn(cqr)))
2704 				continue;
2705 			goto restart;
2706 		}
2707 
2708 		/* log sense for fatal error */
2709 		if (cqr->status == DASD_CQR_FAILED) {
2710 			dasd_log_sense(cqr, &cqr->irb);
2711 		}
2712 
2713 		/* First of all call extended error reporting. */
2714 		if (dasd_eer_enabled(base) &&
2715 		    cqr->status == DASD_CQR_FAILED) {
2716 			dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2717 
2718 			/* restart request  */
2719 			cqr->status = DASD_CQR_FILLED;
2720 			cqr->retries = 255;
2721 			spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2722 			dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2723 			spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2724 					       flags);
2725 			goto restart;
2726 		}
2727 
2728 		/* Process finished ERP request. */
2729 		if (cqr->refers) {
2730 			__dasd_process_erp(base, cqr);
2731 			goto restart;
2732 		}
2733 
2734 		/* Rechain finished requests to final queue */
2735 		cqr->endclk = get_tod_clock();
2736 		list_move_tail(&cqr->blocklist, final_queue);
2737 	}
2738 }
2739 
2740 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2741 {
2742 	dasd_schedule_block_bh(cqr->block);
2743 }
2744 
2745 static void __dasd_block_start_head(struct dasd_block *block)
2746 {
2747 	struct dasd_ccw_req *cqr;
2748 
2749 	if (list_empty(&block->ccw_queue))
2750 		return;
2751 	/* We allways begin with the first requests on the queue, as some
2752 	 * of previously started requests have to be enqueued on a
2753 	 * dasd_device again for error recovery.
2754 	 */
2755 	list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2756 		if (cqr->status != DASD_CQR_FILLED)
2757 			continue;
2758 		if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2759 		    !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2760 			cqr->status = DASD_CQR_FAILED;
2761 			cqr->intrc = -EPERM;
2762 			dasd_schedule_block_bh(block);
2763 			continue;
2764 		}
2765 		/* Non-temporary stop condition will trigger fail fast */
2766 		if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2767 		    test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2768 		    (!dasd_eer_enabled(block->base))) {
2769 			cqr->status = DASD_CQR_FAILED;
2770 			cqr->intrc = -ENOLINK;
2771 			dasd_schedule_block_bh(block);
2772 			continue;
2773 		}
2774 		/* Don't try to start requests if device is stopped */
2775 		if (block->base->stopped)
2776 			return;
2777 
2778 		/* just a fail safe check, should not happen */
2779 		if (!cqr->startdev)
2780 			cqr->startdev = block->base;
2781 
2782 		/* make sure that the requests we submit find their way back */
2783 		cqr->callback = dasd_return_cqr_cb;
2784 
2785 		dasd_add_request_tail(cqr);
2786 	}
2787 }
2788 
2789 /*
2790  * Central dasd_block layer routine. Takes requests from the generic
2791  * block layer request queue, creates ccw requests, enqueues them on
2792  * a dasd_device and processes ccw requests that have been returned.
2793  */
2794 static void dasd_block_tasklet(struct dasd_block *block)
2795 {
2796 	struct list_head final_queue;
2797 	struct list_head *l, *n;
2798 	struct dasd_ccw_req *cqr;
2799 
2800 	atomic_set(&block->tasklet_scheduled, 0);
2801 	INIT_LIST_HEAD(&final_queue);
2802 	spin_lock(&block->queue_lock);
2803 	/* Finish off requests on ccw queue */
2804 	__dasd_process_block_ccw_queue(block, &final_queue);
2805 	spin_unlock(&block->queue_lock);
2806 	/* Now call the callback function of requests with final status */
2807 	spin_lock_irq(&block->request_queue_lock);
2808 	list_for_each_safe(l, n, &final_queue) {
2809 		cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2810 		list_del_init(&cqr->blocklist);
2811 		__dasd_cleanup_cqr(cqr);
2812 	}
2813 	spin_lock(&block->queue_lock);
2814 	/* Get new request from the block device request queue */
2815 	__dasd_process_request_queue(block);
2816 	/* Now check if the head of the ccw queue needs to be started. */
2817 	__dasd_block_start_head(block);
2818 	spin_unlock(&block->queue_lock);
2819 	spin_unlock_irq(&block->request_queue_lock);
2820 	if (waitqueue_active(&shutdown_waitq))
2821 		wake_up(&shutdown_waitq);
2822 	dasd_put_device(block->base);
2823 }
2824 
2825 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2826 {
2827 	wake_up(&dasd_flush_wq);
2828 }
2829 
2830 /*
2831  * Requeue a request back to the block request queue
2832  * only works for block requests
2833  */
2834 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2835 {
2836 	struct dasd_block *block = cqr->block;
2837 	struct request *req;
2838 	unsigned long flags;
2839 
2840 	if (!block)
2841 		return -EINVAL;
2842 	spin_lock_irqsave(&block->queue_lock, flags);
2843 	req = (struct request *) cqr->callback_data;
2844 	blk_requeue_request(block->request_queue, req);
2845 	spin_unlock_irqrestore(&block->queue_lock, flags);
2846 
2847 	return 0;
2848 }
2849 
2850 /*
2851  * Go through all request on the dasd_block request queue, cancel them
2852  * on the respective dasd_device, and return them to the generic
2853  * block layer.
2854  */
2855 static int dasd_flush_block_queue(struct dasd_block *block)
2856 {
2857 	struct dasd_ccw_req *cqr, *n;
2858 	int rc, i;
2859 	struct list_head flush_queue;
2860 
2861 	INIT_LIST_HEAD(&flush_queue);
2862 	spin_lock_bh(&block->queue_lock);
2863 	rc = 0;
2864 restart:
2865 	list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2866 		/* if this request currently owned by a dasd_device cancel it */
2867 		if (cqr->status >= DASD_CQR_QUEUED)
2868 			rc = dasd_cancel_req(cqr);
2869 		if (rc < 0)
2870 			break;
2871 		/* Rechain request (including erp chain) so it won't be
2872 		 * touched by the dasd_block_tasklet anymore.
2873 		 * Replace the callback so we notice when the request
2874 		 * is returned from the dasd_device layer.
2875 		 */
2876 		cqr->callback = _dasd_wake_block_flush_cb;
2877 		for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2878 			list_move_tail(&cqr->blocklist, &flush_queue);
2879 		if (i > 1)
2880 			/* moved more than one request - need to restart */
2881 			goto restart;
2882 	}
2883 	spin_unlock_bh(&block->queue_lock);
2884 	/* Now call the callback function of flushed requests */
2885 restart_cb:
2886 	list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2887 		wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2888 		/* Process finished ERP request. */
2889 		if (cqr->refers) {
2890 			spin_lock_bh(&block->queue_lock);
2891 			__dasd_process_erp(block->base, cqr);
2892 			spin_unlock_bh(&block->queue_lock);
2893 			/* restart list_for_xx loop since dasd_process_erp
2894 			 * might remove multiple elements */
2895 			goto restart_cb;
2896 		}
2897 		/* call the callback function */
2898 		spin_lock_irq(&block->request_queue_lock);
2899 		cqr->endclk = get_tod_clock();
2900 		list_del_init(&cqr->blocklist);
2901 		__dasd_cleanup_cqr(cqr);
2902 		spin_unlock_irq(&block->request_queue_lock);
2903 	}
2904 	return rc;
2905 }
2906 
2907 /*
2908  * Schedules a call to dasd_tasklet over the device tasklet.
2909  */
2910 void dasd_schedule_block_bh(struct dasd_block *block)
2911 {
2912 	/* Protect against rescheduling. */
2913 	if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2914 		return;
2915 	/* life cycle of block is bound to it's base device */
2916 	dasd_get_device(block->base);
2917 	tasklet_hi_schedule(&block->tasklet);
2918 }
2919 EXPORT_SYMBOL(dasd_schedule_block_bh);
2920 
2921 
2922 /*
2923  * SECTION: external block device operations
2924  * (request queue handling, open, release, etc.)
2925  */
2926 
2927 /*
2928  * Dasd request queue function. Called from ll_rw_blk.c
2929  */
2930 static void do_dasd_request(struct request_queue *queue)
2931 {
2932 	struct dasd_block *block;
2933 
2934 	block = queue->queuedata;
2935 	spin_lock(&block->queue_lock);
2936 	/* Get new request from the block device request queue */
2937 	__dasd_process_request_queue(block);
2938 	/* Now check if the head of the ccw queue needs to be started. */
2939 	__dasd_block_start_head(block);
2940 	spin_unlock(&block->queue_lock);
2941 }
2942 
2943 /*
2944  * Block timeout callback, called from the block layer
2945  *
2946  * request_queue lock is held on entry.
2947  *
2948  * Return values:
2949  * BLK_EH_RESET_TIMER if the request should be left running
2950  * BLK_EH_NOT_HANDLED if the request is handled or terminated
2951  *		      by the driver.
2952  */
2953 enum blk_eh_timer_return dasd_times_out(struct request *req)
2954 {
2955 	struct dasd_ccw_req *cqr = req->completion_data;
2956 	struct dasd_block *block = req->q->queuedata;
2957 	struct dasd_device *device;
2958 	int rc = 0;
2959 
2960 	if (!cqr)
2961 		return BLK_EH_NOT_HANDLED;
2962 
2963 	device = cqr->startdev ? cqr->startdev : block->base;
2964 	if (!device->blk_timeout)
2965 		return BLK_EH_RESET_TIMER;
2966 	DBF_DEV_EVENT(DBF_WARNING, device,
2967 		      " dasd_times_out cqr %p status %x",
2968 		      cqr, cqr->status);
2969 
2970 	spin_lock(&block->queue_lock);
2971 	spin_lock(get_ccwdev_lock(device->cdev));
2972 	cqr->retries = -1;
2973 	cqr->intrc = -ETIMEDOUT;
2974 	if (cqr->status >= DASD_CQR_QUEUED) {
2975 		spin_unlock(get_ccwdev_lock(device->cdev));
2976 		rc = dasd_cancel_req(cqr);
2977 	} else if (cqr->status == DASD_CQR_FILLED ||
2978 		   cqr->status == DASD_CQR_NEED_ERP) {
2979 		cqr->status = DASD_CQR_TERMINATED;
2980 		spin_unlock(get_ccwdev_lock(device->cdev));
2981 	} else if (cqr->status == DASD_CQR_IN_ERP) {
2982 		struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2983 
2984 		list_for_each_entry_safe(searchcqr, nextcqr,
2985 					 &block->ccw_queue, blocklist) {
2986 			tmpcqr = searchcqr;
2987 			while (tmpcqr->refers)
2988 				tmpcqr = tmpcqr->refers;
2989 			if (tmpcqr != cqr)
2990 				continue;
2991 			/* searchcqr is an ERP request for cqr */
2992 			searchcqr->retries = -1;
2993 			searchcqr->intrc = -ETIMEDOUT;
2994 			if (searchcqr->status >= DASD_CQR_QUEUED) {
2995 				spin_unlock(get_ccwdev_lock(device->cdev));
2996 				rc = dasd_cancel_req(searchcqr);
2997 				spin_lock(get_ccwdev_lock(device->cdev));
2998 			} else if ((searchcqr->status == DASD_CQR_FILLED) ||
2999 				   (searchcqr->status == DASD_CQR_NEED_ERP)) {
3000 				searchcqr->status = DASD_CQR_TERMINATED;
3001 				rc = 0;
3002 			} else if (searchcqr->status == DASD_CQR_IN_ERP) {
3003 				/*
3004 				 * Shouldn't happen; most recent ERP
3005 				 * request is at the front of queue
3006 				 */
3007 				continue;
3008 			}
3009 			break;
3010 		}
3011 		spin_unlock(get_ccwdev_lock(device->cdev));
3012 	}
3013 	dasd_schedule_block_bh(block);
3014 	spin_unlock(&block->queue_lock);
3015 
3016 	return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3017 }
3018 
3019 /*
3020  * Allocate and initialize request queue and default I/O scheduler.
3021  */
3022 static int dasd_alloc_queue(struct dasd_block *block)
3023 {
3024 	int rc;
3025 
3026 	block->request_queue = blk_init_queue(do_dasd_request,
3027 					       &block->request_queue_lock);
3028 	if (block->request_queue == NULL)
3029 		return -ENOMEM;
3030 
3031 	block->request_queue->queuedata = block;
3032 
3033 	elevator_exit(block->request_queue->elevator);
3034 	block->request_queue->elevator = NULL;
3035 	mutex_lock(&block->request_queue->sysfs_lock);
3036 	rc = elevator_init(block->request_queue, "deadline");
3037 	if (rc)
3038 		blk_cleanup_queue(block->request_queue);
3039 	mutex_unlock(&block->request_queue->sysfs_lock);
3040 	return rc;
3041 }
3042 
3043 /*
3044  * Allocate and initialize request queue.
3045  */
3046 static void dasd_setup_queue(struct dasd_block *block)
3047 {
3048 	int max;
3049 
3050 	if (block->base->features & DASD_FEATURE_USERAW) {
3051 		/*
3052 		 * the max_blocks value for raw_track access is 256
3053 		 * it is higher than the native ECKD value because we
3054 		 * only need one ccw per track
3055 		 * so the max_hw_sectors are
3056 		 * 2048 x 512B = 1024kB = 16 tracks
3057 		 */
3058 		max = 2048;
3059 	} else {
3060 		max = block->base->discipline->max_blocks << block->s2b_shift;
3061 	}
3062 	blk_queue_logical_block_size(block->request_queue,
3063 				     block->bp_block);
3064 	blk_queue_max_hw_sectors(block->request_queue, max);
3065 	blk_queue_max_segments(block->request_queue, -1L);
3066 	/* with page sized segments we can translate each segement into
3067 	 * one idaw/tidaw
3068 	 */
3069 	blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3070 	blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
3071 }
3072 
3073 /*
3074  * Deactivate and free request queue.
3075  */
3076 static void dasd_free_queue(struct dasd_block *block)
3077 {
3078 	if (block->request_queue) {
3079 		blk_cleanup_queue(block->request_queue);
3080 		block->request_queue = NULL;
3081 	}
3082 }
3083 
3084 /*
3085  * Flush request on the request queue.
3086  */
3087 static void dasd_flush_request_queue(struct dasd_block *block)
3088 {
3089 	struct request *req;
3090 
3091 	if (!block->request_queue)
3092 		return;
3093 
3094 	spin_lock_irq(&block->request_queue_lock);
3095 	while ((req = blk_fetch_request(block->request_queue)))
3096 		__blk_end_request_all(req, -EIO);
3097 	spin_unlock_irq(&block->request_queue_lock);
3098 }
3099 
3100 static int dasd_open(struct block_device *bdev, fmode_t mode)
3101 {
3102 	struct dasd_device *base;
3103 	int rc;
3104 
3105 	base = dasd_device_from_gendisk(bdev->bd_disk);
3106 	if (!base)
3107 		return -ENODEV;
3108 
3109 	atomic_inc(&base->block->open_count);
3110 	if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3111 		rc = -ENODEV;
3112 		goto unlock;
3113 	}
3114 
3115 	if (!try_module_get(base->discipline->owner)) {
3116 		rc = -EINVAL;
3117 		goto unlock;
3118 	}
3119 
3120 	if (dasd_probeonly) {
3121 		dev_info(&base->cdev->dev,
3122 			 "Accessing the DASD failed because it is in "
3123 			 "probeonly mode\n");
3124 		rc = -EPERM;
3125 		goto out;
3126 	}
3127 
3128 	if (base->state <= DASD_STATE_BASIC) {
3129 		DBF_DEV_EVENT(DBF_ERR, base, " %s",
3130 			      " Cannot open unrecognized device");
3131 		rc = -ENODEV;
3132 		goto out;
3133 	}
3134 
3135 	if ((mode & FMODE_WRITE) &&
3136 	    (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3137 	     (base->features & DASD_FEATURE_READONLY))) {
3138 		rc = -EROFS;
3139 		goto out;
3140 	}
3141 
3142 	dasd_put_device(base);
3143 	return 0;
3144 
3145 out:
3146 	module_put(base->discipline->owner);
3147 unlock:
3148 	atomic_dec(&base->block->open_count);
3149 	dasd_put_device(base);
3150 	return rc;
3151 }
3152 
3153 static void dasd_release(struct gendisk *disk, fmode_t mode)
3154 {
3155 	struct dasd_device *base = dasd_device_from_gendisk(disk);
3156 	if (base) {
3157 		atomic_dec(&base->block->open_count);
3158 		module_put(base->discipline->owner);
3159 		dasd_put_device(base);
3160 	}
3161 }
3162 
3163 /*
3164  * Return disk geometry.
3165  */
3166 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3167 {
3168 	struct dasd_device *base;
3169 
3170 	base = dasd_device_from_gendisk(bdev->bd_disk);
3171 	if (!base)
3172 		return -ENODEV;
3173 
3174 	if (!base->discipline ||
3175 	    !base->discipline->fill_geometry) {
3176 		dasd_put_device(base);
3177 		return -EINVAL;
3178 	}
3179 	base->discipline->fill_geometry(base->block, geo);
3180 	geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3181 	dasd_put_device(base);
3182 	return 0;
3183 }
3184 
3185 const struct block_device_operations
3186 dasd_device_operations = {
3187 	.owner		= THIS_MODULE,
3188 	.open		= dasd_open,
3189 	.release	= dasd_release,
3190 	.ioctl		= dasd_ioctl,
3191 	.compat_ioctl	= dasd_ioctl,
3192 	.getgeo		= dasd_getgeo,
3193 };
3194 
3195 /*******************************************************************************
3196  * end of block device operations
3197  */
3198 
3199 static void
3200 dasd_exit(void)
3201 {
3202 #ifdef CONFIG_PROC_FS
3203 	dasd_proc_exit();
3204 #endif
3205 	dasd_eer_exit();
3206         if (dasd_page_cache != NULL) {
3207 		kmem_cache_destroy(dasd_page_cache);
3208 		dasd_page_cache = NULL;
3209 	}
3210 	dasd_gendisk_exit();
3211 	dasd_devmap_exit();
3212 	if (dasd_debug_area != NULL) {
3213 		debug_unregister(dasd_debug_area);
3214 		dasd_debug_area = NULL;
3215 	}
3216 	dasd_statistics_removeroot();
3217 }
3218 
3219 /*
3220  * SECTION: common functions for ccw_driver use
3221  */
3222 
3223 /*
3224  * Is the device read-only?
3225  * Note that this function does not report the setting of the
3226  * readonly device attribute, but how it is configured in z/VM.
3227  */
3228 int dasd_device_is_ro(struct dasd_device *device)
3229 {
3230 	struct ccw_dev_id dev_id;
3231 	struct diag210 diag_data;
3232 	int rc;
3233 
3234 	if (!MACHINE_IS_VM)
3235 		return 0;
3236 	ccw_device_get_id(device->cdev, &dev_id);
3237 	memset(&diag_data, 0, sizeof(diag_data));
3238 	diag_data.vrdcdvno = dev_id.devno;
3239 	diag_data.vrdclen = sizeof(diag_data);
3240 	rc = diag210(&diag_data);
3241 	if (rc == 0 || rc == 2) {
3242 		return diag_data.vrdcvfla & 0x80;
3243 	} else {
3244 		DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3245 			  dev_id.devno, rc);
3246 		return 0;
3247 	}
3248 }
3249 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3250 
3251 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3252 {
3253 	struct ccw_device *cdev = data;
3254 	int ret;
3255 
3256 	ret = ccw_device_set_online(cdev);
3257 	if (ret)
3258 		pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3259 			dev_name(&cdev->dev), ret);
3260 }
3261 
3262 /*
3263  * Initial attempt at a probe function. this can be simplified once
3264  * the other detection code is gone.
3265  */
3266 int dasd_generic_probe(struct ccw_device *cdev,
3267 		       struct dasd_discipline *discipline)
3268 {
3269 	int ret;
3270 
3271 	ret = dasd_add_sysfs_files(cdev);
3272 	if (ret) {
3273 		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3274 				"dasd_generic_probe: could not add "
3275 				"sysfs entries");
3276 		return ret;
3277 	}
3278 	cdev->handler = &dasd_int_handler;
3279 
3280 	/*
3281 	 * Automatically online either all dasd devices (dasd_autodetect)
3282 	 * or all devices specified with dasd= parameters during
3283 	 * initial probe.
3284 	 */
3285 	if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3286 	    (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3287 		async_schedule(dasd_generic_auto_online, cdev);
3288 	return 0;
3289 }
3290 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3291 
3292 /*
3293  * This will one day be called from a global not_oper handler.
3294  * It is also used by driver_unregister during module unload.
3295  */
3296 void dasd_generic_remove(struct ccw_device *cdev)
3297 {
3298 	struct dasd_device *device;
3299 	struct dasd_block *block;
3300 
3301 	cdev->handler = NULL;
3302 
3303 	device = dasd_device_from_cdev(cdev);
3304 	if (IS_ERR(device)) {
3305 		dasd_remove_sysfs_files(cdev);
3306 		return;
3307 	}
3308 	if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3309 	    !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3310 		/* Already doing offline processing */
3311 		dasd_put_device(device);
3312 		dasd_remove_sysfs_files(cdev);
3313 		return;
3314 	}
3315 	/*
3316 	 * This device is removed unconditionally. Set offline
3317 	 * flag to prevent dasd_open from opening it while it is
3318 	 * no quite down yet.
3319 	 */
3320 	dasd_set_target_state(device, DASD_STATE_NEW);
3321 	/* dasd_delete_device destroys the device reference. */
3322 	block = device->block;
3323 	dasd_delete_device(device);
3324 	/*
3325 	 * life cycle of block is bound to device, so delete it after
3326 	 * device was safely removed
3327 	 */
3328 	if (block)
3329 		dasd_free_block(block);
3330 
3331 	dasd_remove_sysfs_files(cdev);
3332 }
3333 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3334 
3335 /*
3336  * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3337  * the device is detected for the first time and is supposed to be used
3338  * or the user has started activation through sysfs.
3339  */
3340 int dasd_generic_set_online(struct ccw_device *cdev,
3341 			    struct dasd_discipline *base_discipline)
3342 {
3343 	struct dasd_discipline *discipline;
3344 	struct dasd_device *device;
3345 	int rc;
3346 
3347 	/* first online clears initial online feature flag */
3348 	dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3349 	device = dasd_create_device(cdev);
3350 	if (IS_ERR(device))
3351 		return PTR_ERR(device);
3352 
3353 	discipline = base_discipline;
3354 	if (device->features & DASD_FEATURE_USEDIAG) {
3355 	  	if (!dasd_diag_discipline_pointer) {
3356 			pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3357 				dev_name(&cdev->dev));
3358 			dasd_delete_device(device);
3359 			return -ENODEV;
3360 		}
3361 		discipline = dasd_diag_discipline_pointer;
3362 	}
3363 	if (!try_module_get(base_discipline->owner)) {
3364 		dasd_delete_device(device);
3365 		return -EINVAL;
3366 	}
3367 	if (!try_module_get(discipline->owner)) {
3368 		module_put(base_discipline->owner);
3369 		dasd_delete_device(device);
3370 		return -EINVAL;
3371 	}
3372 	device->base_discipline = base_discipline;
3373 	device->discipline = discipline;
3374 
3375 	/* check_device will allocate block device if necessary */
3376 	rc = discipline->check_device(device);
3377 	if (rc) {
3378 		pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3379 			dev_name(&cdev->dev), discipline->name, rc);
3380 		module_put(discipline->owner);
3381 		module_put(base_discipline->owner);
3382 		dasd_delete_device(device);
3383 		return rc;
3384 	}
3385 
3386 	dasd_set_target_state(device, DASD_STATE_ONLINE);
3387 	if (device->state <= DASD_STATE_KNOWN) {
3388 		pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3389 			dev_name(&cdev->dev));
3390 		rc = -ENODEV;
3391 		dasd_set_target_state(device, DASD_STATE_NEW);
3392 		if (device->block)
3393 			dasd_free_block(device->block);
3394 		dasd_delete_device(device);
3395 	} else
3396 		pr_debug("dasd_generic device %s found\n",
3397 				dev_name(&cdev->dev));
3398 
3399 	wait_event(dasd_init_waitq, _wait_for_device(device));
3400 
3401 	dasd_put_device(device);
3402 	return rc;
3403 }
3404 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3405 
3406 int dasd_generic_set_offline(struct ccw_device *cdev)
3407 {
3408 	struct dasd_device *device;
3409 	struct dasd_block *block;
3410 	int max_count, open_count, rc;
3411 
3412 	rc = 0;
3413 	device = dasd_device_from_cdev(cdev);
3414 	if (IS_ERR(device))
3415 		return PTR_ERR(device);
3416 
3417 	/*
3418 	 * We must make sure that this device is currently not in use.
3419 	 * The open_count is increased for every opener, that includes
3420 	 * the blkdev_get in dasd_scan_partitions. We are only interested
3421 	 * in the other openers.
3422 	 */
3423 	if (device->block) {
3424 		max_count = device->block->bdev ? 0 : -1;
3425 		open_count = atomic_read(&device->block->open_count);
3426 		if (open_count > max_count) {
3427 			if (open_count > 0)
3428 				pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3429 					dev_name(&cdev->dev), open_count);
3430 			else
3431 				pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3432 					dev_name(&cdev->dev));
3433 			clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3434 			dasd_put_device(device);
3435 			return -EBUSY;
3436 		}
3437 	}
3438 
3439 	if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3440 		/*
3441 		 * safe offline already running
3442 		 * could only be called by normal offline so safe_offline flag
3443 		 * needs to be removed to run normal offline and kill all I/O
3444 		 */
3445 		if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3446 			/* Already doing normal offline processing */
3447 			dasd_put_device(device);
3448 			return -EBUSY;
3449 		} else
3450 			clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3451 
3452 	} else
3453 		if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3454 			/* Already doing offline processing */
3455 			dasd_put_device(device);
3456 			return -EBUSY;
3457 		}
3458 
3459 	/*
3460 	 * if safe_offline called set safe_offline_running flag and
3461 	 * clear safe_offline so that a call to normal offline
3462 	 * can overrun safe_offline processing
3463 	 */
3464 	if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3465 	    !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3466 		/*
3467 		 * If we want to set the device safe offline all IO operations
3468 		 * should be finished before continuing the offline process
3469 		 * so sync bdev first and then wait for our queues to become
3470 		 * empty
3471 		 */
3472 		/* sync blockdev and partitions */
3473 		rc = fsync_bdev(device->block->bdev);
3474 		if (rc != 0)
3475 			goto interrupted;
3476 
3477 		/* schedule device tasklet and wait for completion */
3478 		dasd_schedule_device_bh(device);
3479 		rc = wait_event_interruptible(shutdown_waitq,
3480 					      _wait_for_empty_queues(device));
3481 		if (rc != 0)
3482 			goto interrupted;
3483 	}
3484 
3485 	set_bit(DASD_FLAG_OFFLINE, &device->flags);
3486 	dasd_set_target_state(device, DASD_STATE_NEW);
3487 	/* dasd_delete_device destroys the device reference. */
3488 	block = device->block;
3489 	dasd_delete_device(device);
3490 	/*
3491 	 * life cycle of block is bound to device, so delete it after
3492 	 * device was safely removed
3493 	 */
3494 	if (block)
3495 		dasd_free_block(block);
3496 	return 0;
3497 
3498 interrupted:
3499 	/* interrupted by signal */
3500 	clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3501 	clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3502 	clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3503 	dasd_put_device(device);
3504 	return rc;
3505 }
3506 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3507 
3508 int dasd_generic_last_path_gone(struct dasd_device *device)
3509 {
3510 	struct dasd_ccw_req *cqr;
3511 
3512 	dev_warn(&device->cdev->dev, "No operational channel path is left "
3513 		 "for the device\n");
3514 	DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3515 	/* First of all call extended error reporting. */
3516 	dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3517 
3518 	if (device->state < DASD_STATE_BASIC)
3519 		return 0;
3520 	/* Device is active. We want to keep it. */
3521 	list_for_each_entry(cqr, &device->ccw_queue, devlist)
3522 		if ((cqr->status == DASD_CQR_IN_IO) ||
3523 		    (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3524 			cqr->status = DASD_CQR_QUEUED;
3525 			cqr->retries++;
3526 		}
3527 	dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3528 	dasd_device_clear_timer(device);
3529 	dasd_schedule_device_bh(device);
3530 	return 1;
3531 }
3532 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3533 
3534 int dasd_generic_path_operational(struct dasd_device *device)
3535 {
3536 	dev_info(&device->cdev->dev, "A channel path to the device has become "
3537 		 "operational\n");
3538 	DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3539 	dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3540 	if (device->stopped & DASD_UNRESUMED_PM) {
3541 		dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3542 		dasd_restore_device(device);
3543 		return 1;
3544 	}
3545 	dasd_schedule_device_bh(device);
3546 	if (device->block)
3547 		dasd_schedule_block_bh(device->block);
3548 
3549 	if (!device->stopped)
3550 		wake_up(&generic_waitq);
3551 
3552 	return 1;
3553 }
3554 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3555 
3556 int dasd_generic_notify(struct ccw_device *cdev, int event)
3557 {
3558 	struct dasd_device *device;
3559 	int ret;
3560 
3561 	device = dasd_device_from_cdev_locked(cdev);
3562 	if (IS_ERR(device))
3563 		return 0;
3564 	ret = 0;
3565 	switch (event) {
3566 	case CIO_GONE:
3567 	case CIO_BOXED:
3568 	case CIO_NO_PATH:
3569 		device->path_data.opm = 0;
3570 		device->path_data.ppm = 0;
3571 		device->path_data.npm = 0;
3572 		ret = dasd_generic_last_path_gone(device);
3573 		break;
3574 	case CIO_OPER:
3575 		ret = 1;
3576 		if (device->path_data.opm)
3577 			ret = dasd_generic_path_operational(device);
3578 		break;
3579 	}
3580 	dasd_put_device(device);
3581 	return ret;
3582 }
3583 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3584 
3585 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3586 {
3587 	int chp;
3588 	__u8 oldopm, eventlpm;
3589 	struct dasd_device *device;
3590 
3591 	device = dasd_device_from_cdev_locked(cdev);
3592 	if (IS_ERR(device))
3593 		return;
3594 	for (chp = 0; chp < 8; chp++) {
3595 		eventlpm = 0x80 >> chp;
3596 		if (path_event[chp] & PE_PATH_GONE) {
3597 			oldopm = device->path_data.opm;
3598 			device->path_data.opm &= ~eventlpm;
3599 			device->path_data.ppm &= ~eventlpm;
3600 			device->path_data.npm &= ~eventlpm;
3601 			if (oldopm && !device->path_data.opm) {
3602 				dev_warn(&device->cdev->dev,
3603 					 "No verified channel paths remain "
3604 					 "for the device\n");
3605 				DBF_DEV_EVENT(DBF_WARNING, device,
3606 					      "%s", "last verified path gone");
3607 				dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3608 				dasd_device_set_stop_bits(device,
3609 							  DASD_STOPPED_DC_WAIT);
3610 			}
3611 		}
3612 		if (path_event[chp] & PE_PATH_AVAILABLE) {
3613 			device->path_data.opm &= ~eventlpm;
3614 			device->path_data.ppm &= ~eventlpm;
3615 			device->path_data.npm &= ~eventlpm;
3616 			device->path_data.tbvpm |= eventlpm;
3617 			dasd_schedule_device_bh(device);
3618 		}
3619 		if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3620 			if (!(device->path_data.opm & eventlpm) &&
3621 			    !(device->path_data.tbvpm & eventlpm)) {
3622 				/*
3623 				 * we can not establish a pathgroup on an
3624 				 * unavailable path, so trigger a path
3625 				 * verification first
3626 				 */
3627 				device->path_data.tbvpm |= eventlpm;
3628 				dasd_schedule_device_bh(device);
3629 			}
3630 			DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3631 				      "Pathgroup re-established\n");
3632 			if (device->discipline->kick_validate)
3633 				device->discipline->kick_validate(device);
3634 		}
3635 	}
3636 	dasd_put_device(device);
3637 }
3638 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3639 
3640 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3641 {
3642 	if (!device->path_data.opm && lpm) {
3643 		device->path_data.opm = lpm;
3644 		dasd_generic_path_operational(device);
3645 	} else
3646 		device->path_data.opm |= lpm;
3647 	return 0;
3648 }
3649 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3650 
3651 
3652 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3653 {
3654 	struct dasd_device *device = dasd_device_from_cdev(cdev);
3655 	struct list_head freeze_queue;
3656 	struct dasd_ccw_req *cqr, *n;
3657 	struct dasd_ccw_req *refers;
3658 	int rc;
3659 
3660 	if (IS_ERR(device))
3661 		return PTR_ERR(device);
3662 
3663 	/* mark device as suspended */
3664 	set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3665 
3666 	if (device->discipline->freeze)
3667 		rc = device->discipline->freeze(device);
3668 
3669 	/* disallow new I/O  */
3670 	dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3671 
3672 	/* clear active requests and requeue them to block layer if possible */
3673 	INIT_LIST_HEAD(&freeze_queue);
3674 	spin_lock_irq(get_ccwdev_lock(cdev));
3675 	rc = 0;
3676 	list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3677 		/* Check status and move request to flush_queue */
3678 		if (cqr->status == DASD_CQR_IN_IO) {
3679 			rc = device->discipline->term_IO(cqr);
3680 			if (rc) {
3681 				/* unable to terminate requeust */
3682 				dev_err(&device->cdev->dev,
3683 					"Unable to terminate request %p "
3684 					"on suspend\n", cqr);
3685 				spin_unlock_irq(get_ccwdev_lock(cdev));
3686 				dasd_put_device(device);
3687 				return rc;
3688 			}
3689 		}
3690 		list_move_tail(&cqr->devlist, &freeze_queue);
3691 	}
3692 	spin_unlock_irq(get_ccwdev_lock(cdev));
3693 
3694 	list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3695 		wait_event(dasd_flush_wq,
3696 			   (cqr->status != DASD_CQR_CLEAR_PENDING));
3697 		if (cqr->status == DASD_CQR_CLEARED)
3698 			cqr->status = DASD_CQR_QUEUED;
3699 
3700 		/* requeue requests to blocklayer will only work for
3701 		   block device requests */
3702 		if (_dasd_requeue_request(cqr))
3703 			continue;
3704 
3705 		/* remove requests from device and block queue */
3706 		list_del_init(&cqr->devlist);
3707 		while (cqr->refers != NULL) {
3708 			refers = cqr->refers;
3709 			/* remove the request from the block queue */
3710 			list_del(&cqr->blocklist);
3711 			/* free the finished erp request */
3712 			dasd_free_erp_request(cqr, cqr->memdev);
3713 			cqr = refers;
3714 		}
3715 		if (cqr->block)
3716 			list_del_init(&cqr->blocklist);
3717 		cqr->block->base->discipline->free_cp(
3718 			cqr, (struct request *) cqr->callback_data);
3719 	}
3720 
3721 	/*
3722 	 * if requests remain then they are internal request
3723 	 * and go back to the device queue
3724 	 */
3725 	if (!list_empty(&freeze_queue)) {
3726 		/* move freeze_queue to start of the ccw_queue */
3727 		spin_lock_irq(get_ccwdev_lock(cdev));
3728 		list_splice_tail(&freeze_queue, &device->ccw_queue);
3729 		spin_unlock_irq(get_ccwdev_lock(cdev));
3730 	}
3731 	dasd_put_device(device);
3732 	return rc;
3733 }
3734 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3735 
3736 int dasd_generic_restore_device(struct ccw_device *cdev)
3737 {
3738 	struct dasd_device *device = dasd_device_from_cdev(cdev);
3739 	int rc = 0;
3740 
3741 	if (IS_ERR(device))
3742 		return PTR_ERR(device);
3743 
3744 	/* allow new IO again */
3745 	dasd_device_remove_stop_bits(device,
3746 				     (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3747 
3748 	dasd_schedule_device_bh(device);
3749 
3750 	/*
3751 	 * call discipline restore function
3752 	 * if device is stopped do nothing e.g. for disconnected devices
3753 	 */
3754 	if (device->discipline->restore && !(device->stopped))
3755 		rc = device->discipline->restore(device);
3756 	if (rc || device->stopped)
3757 		/*
3758 		 * if the resume failed for the DASD we put it in
3759 		 * an UNRESUMED stop state
3760 		 */
3761 		device->stopped |= DASD_UNRESUMED_PM;
3762 
3763 	if (device->block)
3764 		dasd_schedule_block_bh(device->block);
3765 
3766 	clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3767 	dasd_put_device(device);
3768 	return 0;
3769 }
3770 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3771 
3772 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3773 						   void *rdc_buffer,
3774 						   int rdc_buffer_size,
3775 						   int magic)
3776 {
3777 	struct dasd_ccw_req *cqr;
3778 	struct ccw1 *ccw;
3779 	unsigned long *idaw;
3780 
3781 	cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3782 
3783 	if (IS_ERR(cqr)) {
3784 		/* internal error 13 - Allocating the RDC request failed*/
3785 		dev_err(&device->cdev->dev,
3786 			 "An error occurred in the DASD device driver, "
3787 			 "reason=%s\n", "13");
3788 		return cqr;
3789 	}
3790 
3791 	ccw = cqr->cpaddr;
3792 	ccw->cmd_code = CCW_CMD_RDC;
3793 	if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3794 		idaw = (unsigned long *) (cqr->data);
3795 		ccw->cda = (__u32)(addr_t) idaw;
3796 		ccw->flags = CCW_FLAG_IDA;
3797 		idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3798 	} else {
3799 		ccw->cda = (__u32)(addr_t) rdc_buffer;
3800 		ccw->flags = 0;
3801 	}
3802 
3803 	ccw->count = rdc_buffer_size;
3804 	cqr->startdev = device;
3805 	cqr->memdev = device;
3806 	cqr->expires = 10*HZ;
3807 	cqr->retries = 256;
3808 	cqr->buildclk = get_tod_clock();
3809 	cqr->status = DASD_CQR_FILLED;
3810 	return cqr;
3811 }
3812 
3813 
3814 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3815 				void *rdc_buffer, int rdc_buffer_size)
3816 {
3817 	int ret;
3818 	struct dasd_ccw_req *cqr;
3819 
3820 	cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3821 				     magic);
3822 	if (IS_ERR(cqr))
3823 		return PTR_ERR(cqr);
3824 
3825 	ret = dasd_sleep_on(cqr);
3826 	dasd_sfree_request(cqr, cqr->memdev);
3827 	return ret;
3828 }
3829 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3830 
3831 /*
3832  *   In command mode and transport mode we need to look for sense
3833  *   data in different places. The sense data itself is allways
3834  *   an array of 32 bytes, so we can unify the sense data access
3835  *   for both modes.
3836  */
3837 char *dasd_get_sense(struct irb *irb)
3838 {
3839 	struct tsb *tsb = NULL;
3840 	char *sense = NULL;
3841 
3842 	if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3843 		if (irb->scsw.tm.tcw)
3844 			tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3845 					  irb->scsw.tm.tcw);
3846 		if (tsb && tsb->length == 64 && tsb->flags)
3847 			switch (tsb->flags & 0x07) {
3848 			case 1:	/* tsa_iostat */
3849 				sense = tsb->tsa.iostat.sense;
3850 				break;
3851 			case 2: /* tsa_ddpc */
3852 				sense = tsb->tsa.ddpc.sense;
3853 				break;
3854 			default:
3855 				/* currently we don't use interrogate data */
3856 				break;
3857 			}
3858 	} else if (irb->esw.esw0.erw.cons) {
3859 		sense = irb->ecw;
3860 	}
3861 	return sense;
3862 }
3863 EXPORT_SYMBOL_GPL(dasd_get_sense);
3864 
3865 void dasd_generic_shutdown(struct ccw_device *cdev)
3866 {
3867 	struct dasd_device *device;
3868 
3869 	device = dasd_device_from_cdev(cdev);
3870 	if (IS_ERR(device))
3871 		return;
3872 
3873 	if (device->block)
3874 		dasd_schedule_block_bh(device->block);
3875 
3876 	dasd_schedule_device_bh(device);
3877 
3878 	wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3879 }
3880 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3881 
3882 static int __init dasd_init(void)
3883 {
3884 	int rc;
3885 
3886 	init_waitqueue_head(&dasd_init_waitq);
3887 	init_waitqueue_head(&dasd_flush_wq);
3888 	init_waitqueue_head(&generic_waitq);
3889 	init_waitqueue_head(&shutdown_waitq);
3890 
3891 	/* register 'common' DASD debug area, used for all DBF_XXX calls */
3892 	dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3893 	if (dasd_debug_area == NULL) {
3894 		rc = -ENOMEM;
3895 		goto failed;
3896 	}
3897 	debug_register_view(dasd_debug_area, &debug_sprintf_view);
3898 	debug_set_level(dasd_debug_area, DBF_WARNING);
3899 
3900 	DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3901 
3902 	dasd_diag_discipline_pointer = NULL;
3903 
3904 	dasd_statistics_createroot();
3905 
3906 	rc = dasd_devmap_init();
3907 	if (rc)
3908 		goto failed;
3909 	rc = dasd_gendisk_init();
3910 	if (rc)
3911 		goto failed;
3912 	rc = dasd_parse();
3913 	if (rc)
3914 		goto failed;
3915 	rc = dasd_eer_init();
3916 	if (rc)
3917 		goto failed;
3918 #ifdef CONFIG_PROC_FS
3919 	rc = dasd_proc_init();
3920 	if (rc)
3921 		goto failed;
3922 #endif
3923 
3924 	return 0;
3925 failed:
3926 	pr_info("The DASD device driver could not be initialized\n");
3927 	dasd_exit();
3928 	return rc;
3929 }
3930 
3931 module_init(dasd_init);
3932 module_exit(dasd_exit);
3933