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