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