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