xref: /openbmc/linux/drivers/s390/block/dasd.c (revision 12c3a548)
1 /*
2  * File...........: linux/drivers/s390/block/dasd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *		    Horst Hummel <Horst.Hummel@de.ibm.com>
5  *		    Carsten Otte <Cotte@de.ibm.com>
6  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9  *
10  */
11 
12 #include <linux/config.h>
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/buffer_head.h>
20 #include <linux/hdreg.h>
21 #include <linux/notifier.h>
22 
23 #include <asm/ccwdev.h>
24 #include <asm/ebcdic.h>
25 #include <asm/idals.h>
26 #include <asm/todclk.h>
27 
28 /* This is ugly... */
29 #define PRINTK_HEADER "dasd:"
30 
31 #include "dasd_int.h"
32 /*
33  * SECTION: Constant definitions to be used within this file
34  */
35 #define DASD_CHANQ_MAX_SIZE 4
36 
37 /*
38  * SECTION: exported variables of dasd.c
39  */
40 debug_info_t *dasd_debug_area;
41 struct dasd_discipline *dasd_diag_discipline_pointer;
42 
43 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
44 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
45 		   " Copyright 2000 IBM Corporation");
46 MODULE_SUPPORTED_DEVICE("dasd");
47 MODULE_PARM(dasd, "1-" __MODULE_STRING(256) "s");
48 MODULE_LICENSE("GPL");
49 
50 /*
51  * SECTION: prototypes for static functions of dasd.c
52  */
53 static int  dasd_alloc_queue(struct dasd_device * device);
54 static void dasd_setup_queue(struct dasd_device * device);
55 static void dasd_free_queue(struct dasd_device * device);
56 static void dasd_flush_request_queue(struct dasd_device *);
57 static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
58 static void dasd_flush_ccw_queue(struct dasd_device *, int);
59 static void dasd_tasklet(struct dasd_device *);
60 static void do_kick_device(void *data);
61 static void dasd_disable_eer(struct dasd_device *device);
62 
63 /*
64  * SECTION: Operations on the device structure.
65  */
66 static wait_queue_head_t dasd_init_waitq;
67 
68 /*
69  * Allocate memory for a new device structure.
70  */
71 struct dasd_device *
72 dasd_alloc_device(void)
73 {
74 	struct dasd_device *device;
75 
76 	device = kmalloc(sizeof (struct dasd_device), GFP_ATOMIC);
77 	if (device == NULL)
78 		return ERR_PTR(-ENOMEM);
79 	memset(device, 0, sizeof (struct dasd_device));
80 	/* open_count = 0 means device online but not in use */
81 	atomic_set(&device->open_count, -1);
82 
83 	/* Get two pages for normal block device operations. */
84 	device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
85 	if (device->ccw_mem == NULL) {
86 		kfree(device);
87 		return ERR_PTR(-ENOMEM);
88 	}
89 	/* Get one page for error recovery. */
90 	device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
91 	if (device->erp_mem == NULL) {
92 		free_pages((unsigned long) device->ccw_mem, 1);
93 		kfree(device);
94 		return ERR_PTR(-ENOMEM);
95 	}
96 
97 	dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
98 	dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
99 	spin_lock_init(&device->mem_lock);
100 	spin_lock_init(&device->request_queue_lock);
101 	atomic_set (&device->tasklet_scheduled, 0);
102 	tasklet_init(&device->tasklet,
103 		     (void (*)(unsigned long)) dasd_tasklet,
104 		     (unsigned long) device);
105 	INIT_LIST_HEAD(&device->ccw_queue);
106 	init_timer(&device->timer);
107 	INIT_WORK(&device->kick_work, do_kick_device, device);
108 	device->state = DASD_STATE_NEW;
109 	device->target = DASD_STATE_NEW;
110 
111 	return device;
112 }
113 
114 /*
115  * Free memory of a device structure.
116  */
117 void
118 dasd_free_device(struct dasd_device *device)
119 {
120 	kfree(device->private);
121 	free_page((unsigned long) device->erp_mem);
122 	free_pages((unsigned long) device->ccw_mem, 1);
123 	kfree(device);
124 }
125 
126 /*
127  * Make a new device known to the system.
128  */
129 static inline int
130 dasd_state_new_to_known(struct dasd_device *device)
131 {
132 	int rc;
133 
134 	/*
135 	 * As long as the device is not in state DASD_STATE_NEW we want to
136 	 * keep the reference count > 0.
137 	 */
138 	dasd_get_device(device);
139 
140 	rc = dasd_alloc_queue(device);
141 	if (rc) {
142 		dasd_put_device(device);
143 		return rc;
144 	}
145 
146 	device->state = DASD_STATE_KNOWN;
147 	return 0;
148 }
149 
150 /*
151  * Let the system forget about a device.
152  */
153 static inline void
154 dasd_state_known_to_new(struct dasd_device * device)
155 {
156 	/* disable extended error reporting for this device */
157 	dasd_disable_eer(device);
158 	/* Forget the discipline information. */
159 	device->discipline = NULL;
160 	device->state = DASD_STATE_NEW;
161 
162 	dasd_free_queue(device);
163 
164 	/* Give up reference we took in dasd_state_new_to_known. */
165 	dasd_put_device(device);
166 }
167 
168 /*
169  * Request the irq line for the device.
170  */
171 static inline int
172 dasd_state_known_to_basic(struct dasd_device * device)
173 {
174 	int rc;
175 
176 	/* Allocate and register gendisk structure. */
177 	rc = dasd_gendisk_alloc(device);
178 	if (rc)
179 		return rc;
180 
181 	/* register 'device' debug area, used for all DBF_DEV_XXX calls */
182 	device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
183 					    8 * sizeof (long));
184 	debug_register_view(device->debug_area, &debug_sprintf_view);
185 	debug_set_level(device->debug_area, DBF_EMERG);
186 	DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
187 
188 	device->state = DASD_STATE_BASIC;
189 	return 0;
190 }
191 
192 /*
193  * Release the irq line for the device. Terminate any running i/o.
194  */
195 static inline void
196 dasd_state_basic_to_known(struct dasd_device * device)
197 {
198 	dasd_gendisk_free(device);
199 	dasd_flush_ccw_queue(device, 1);
200 	DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
201 	if (device->debug_area != NULL) {
202 		debug_unregister(device->debug_area);
203 		device->debug_area = NULL;
204 	}
205 	device->state = DASD_STATE_KNOWN;
206 }
207 
208 /*
209  * Do the initial analysis. The do_analysis function may return
210  * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
211  * until the discipline decides to continue the startup sequence
212  * by calling the function dasd_change_state. The eckd disciplines
213  * uses this to start a ccw that detects the format. The completion
214  * interrupt for this detection ccw uses the kernel event daemon to
215  * trigger the call to dasd_change_state. All this is done in the
216  * discipline code, see dasd_eckd.c.
217  * After the analysis ccw is done (do_analysis returned 0 or error)
218  * the block device is setup. Either a fake disk is added to allow
219  * formatting or a proper device request queue is created.
220  */
221 static inline int
222 dasd_state_basic_to_ready(struct dasd_device * device)
223 {
224 	int rc;
225 
226 	rc = 0;
227 	if (device->discipline->do_analysis != NULL)
228 		rc = device->discipline->do_analysis(device);
229 	if (rc)
230 		return rc;
231 	dasd_setup_queue(device);
232 	device->state = DASD_STATE_READY;
233 	if (dasd_scan_partitions(device) != 0)
234 		device->state = DASD_STATE_BASIC;
235 	return 0;
236 }
237 
238 /*
239  * Remove device from block device layer. Destroy dirty buffers.
240  * Forget format information. Check if the target level is basic
241  * and if it is create fake disk for formatting.
242  */
243 static inline void
244 dasd_state_ready_to_basic(struct dasd_device * device)
245 {
246 	dasd_flush_ccw_queue(device, 0);
247 	dasd_destroy_partitions(device);
248 	dasd_flush_request_queue(device);
249 	device->blocks = 0;
250 	device->bp_block = 0;
251 	device->s2b_shift = 0;
252 	device->state = DASD_STATE_BASIC;
253 }
254 
255 /*
256  * Make the device online and schedule the bottom half to start
257  * the requeueing of requests from the linux request queue to the
258  * ccw queue.
259  */
260 static inline int
261 dasd_state_ready_to_online(struct dasd_device * device)
262 {
263 	device->state = DASD_STATE_ONLINE;
264 	dasd_schedule_bh(device);
265 	return 0;
266 }
267 
268 /*
269  * Stop the requeueing of requests again.
270  */
271 static inline void
272 dasd_state_online_to_ready(struct dasd_device * device)
273 {
274 	device->state = DASD_STATE_READY;
275 }
276 
277 /*
278  * Device startup state changes.
279  */
280 static inline int
281 dasd_increase_state(struct dasd_device *device)
282 {
283 	int rc;
284 
285 	rc = 0;
286 	if (device->state == DASD_STATE_NEW &&
287 	    device->target >= DASD_STATE_KNOWN)
288 		rc = dasd_state_new_to_known(device);
289 
290 	if (!rc &&
291 	    device->state == DASD_STATE_KNOWN &&
292 	    device->target >= DASD_STATE_BASIC)
293 		rc = dasd_state_known_to_basic(device);
294 
295 	if (!rc &&
296 	    device->state == DASD_STATE_BASIC &&
297 	    device->target >= DASD_STATE_READY)
298 		rc = dasd_state_basic_to_ready(device);
299 
300 	if (!rc &&
301 	    device->state == DASD_STATE_READY &&
302 	    device->target >= DASD_STATE_ONLINE)
303 		rc = dasd_state_ready_to_online(device);
304 
305 	return rc;
306 }
307 
308 /*
309  * Device shutdown state changes.
310  */
311 static inline int
312 dasd_decrease_state(struct dasd_device *device)
313 {
314 	if (device->state == DASD_STATE_ONLINE &&
315 	    device->target <= DASD_STATE_READY)
316 		dasd_state_online_to_ready(device);
317 
318 	if (device->state == DASD_STATE_READY &&
319 	    device->target <= DASD_STATE_BASIC)
320 		dasd_state_ready_to_basic(device);
321 
322 	if (device->state == DASD_STATE_BASIC &&
323 	    device->target <= DASD_STATE_KNOWN)
324 		dasd_state_basic_to_known(device);
325 
326 	if (device->state == DASD_STATE_KNOWN &&
327 	    device->target <= DASD_STATE_NEW)
328 		dasd_state_known_to_new(device);
329 
330 	return 0;
331 }
332 
333 /*
334  * This is the main startup/shutdown routine.
335  */
336 static void
337 dasd_change_state(struct dasd_device *device)
338 {
339         int rc;
340 
341 	if (device->state == device->target)
342 		/* Already where we want to go today... */
343 		return;
344 	if (device->state < device->target)
345 		rc = dasd_increase_state(device);
346 	else
347 		rc = dasd_decrease_state(device);
348         if (rc && rc != -EAGAIN)
349                 device->target = device->state;
350 
351 	if (device->state == device->target)
352 		wake_up(&dasd_init_waitq);
353 }
354 
355 /*
356  * Kick starter for devices that did not complete the startup/shutdown
357  * procedure or were sleeping because of a pending state.
358  * dasd_kick_device will schedule a call do do_kick_device to the kernel
359  * event daemon.
360  */
361 static void
362 do_kick_device(void *data)
363 {
364 	struct dasd_device *device;
365 
366 	device = (struct dasd_device *) data;
367 	dasd_change_state(device);
368 	dasd_schedule_bh(device);
369 	dasd_put_device(device);
370 }
371 
372 void
373 dasd_kick_device(struct dasd_device *device)
374 {
375 	dasd_get_device(device);
376 	/* queue call to dasd_kick_device to the kernel event daemon. */
377 	schedule_work(&device->kick_work);
378 }
379 
380 /*
381  * Set the target state for a device and starts the state change.
382  */
383 void
384 dasd_set_target_state(struct dasd_device *device, int target)
385 {
386 	/* If we are in probeonly mode stop at DASD_STATE_READY. */
387 	if (dasd_probeonly && target > DASD_STATE_READY)
388 		target = DASD_STATE_READY;
389 	if (device->target != target) {
390                 if (device->state == target)
391 			wake_up(&dasd_init_waitq);
392 		device->target = target;
393 	}
394 	if (device->state != device->target)
395 		dasd_change_state(device);
396 }
397 
398 /*
399  * Enable devices with device numbers in [from..to].
400  */
401 static inline int
402 _wait_for_device(struct dasd_device *device)
403 {
404 	return (device->state == device->target);
405 }
406 
407 void
408 dasd_enable_device(struct dasd_device *device)
409 {
410 	dasd_set_target_state(device, DASD_STATE_ONLINE);
411 	if (device->state <= DASD_STATE_KNOWN)
412 		/* No discipline for device found. */
413 		dasd_set_target_state(device, DASD_STATE_NEW);
414 	/* Now wait for the devices to come up. */
415 	wait_event(dasd_init_waitq, _wait_for_device(device));
416 }
417 
418 /*
419  * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
420  */
421 #ifdef CONFIG_DASD_PROFILE
422 
423 struct dasd_profile_info_t dasd_global_profile;
424 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
425 
426 /*
427  * Increments counter in global and local profiling structures.
428  */
429 #define dasd_profile_counter(value, counter, device) \
430 { \
431 	int index; \
432 	for (index = 0; index < 31 && value >> (2+index); index++); \
433 	dasd_global_profile.counter[index]++; \
434 	device->profile.counter[index]++; \
435 }
436 
437 /*
438  * Add profiling information for cqr before execution.
439  */
440 static inline void
441 dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
442 		   struct request *req)
443 {
444 	struct list_head *l;
445 	unsigned int counter;
446 
447 	if (dasd_profile_level != DASD_PROFILE_ON)
448 		return;
449 
450 	/* count the length of the chanq for statistics */
451 	counter = 0;
452 	list_for_each(l, &device->ccw_queue)
453 		if (++counter >= 31)
454 			break;
455 	dasd_global_profile.dasd_io_nr_req[counter]++;
456 	device->profile.dasd_io_nr_req[counter]++;
457 }
458 
459 /*
460  * Add profiling information for cqr after execution.
461  */
462 static inline void
463 dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
464 		 struct request *req)
465 {
466 	long strtime, irqtime, endtime, tottime;	/* in microseconds */
467 	long tottimeps, sectors;
468 
469 	if (dasd_profile_level != DASD_PROFILE_ON)
470 		return;
471 
472 	sectors = req->nr_sectors;
473 	if (!cqr->buildclk || !cqr->startclk ||
474 	    !cqr->stopclk || !cqr->endclk ||
475 	    !sectors)
476 		return;
477 
478 	strtime = ((cqr->startclk - cqr->buildclk) >> 12);
479 	irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
480 	endtime = ((cqr->endclk - cqr->stopclk) >> 12);
481 	tottime = ((cqr->endclk - cqr->buildclk) >> 12);
482 	tottimeps = tottime / sectors;
483 
484 	if (!dasd_global_profile.dasd_io_reqs)
485 		memset(&dasd_global_profile, 0,
486 		       sizeof (struct dasd_profile_info_t));
487 	dasd_global_profile.dasd_io_reqs++;
488 	dasd_global_profile.dasd_io_sects += sectors;
489 
490 	if (!device->profile.dasd_io_reqs)
491 		memset(&device->profile, 0,
492 		       sizeof (struct dasd_profile_info_t));
493 	device->profile.dasd_io_reqs++;
494 	device->profile.dasd_io_sects += sectors;
495 
496 	dasd_profile_counter(sectors, dasd_io_secs, device);
497 	dasd_profile_counter(tottime, dasd_io_times, device);
498 	dasd_profile_counter(tottimeps, dasd_io_timps, device);
499 	dasd_profile_counter(strtime, dasd_io_time1, device);
500 	dasd_profile_counter(irqtime, dasd_io_time2, device);
501 	dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
502 	dasd_profile_counter(endtime, dasd_io_time3, device);
503 }
504 #else
505 #define dasd_profile_start(device, cqr, req) do {} while (0)
506 #define dasd_profile_end(device, cqr, req) do {} while (0)
507 #endif				/* CONFIG_DASD_PROFILE */
508 
509 /*
510  * Allocate memory for a channel program with 'cplength' channel
511  * command words and 'datasize' additional space. There are two
512  * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
513  * memory and 2) dasd_smalloc_request uses the static ccw memory
514  * that gets allocated for each device.
515  */
516 struct dasd_ccw_req *
517 dasd_kmalloc_request(char *magic, int cplength, int datasize,
518 		   struct dasd_device * device)
519 {
520 	struct dasd_ccw_req *cqr;
521 
522 	/* Sanity checks */
523 	if ( magic == NULL || datasize > PAGE_SIZE ||
524 	     (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
525 		BUG();
526 
527 	cqr = kmalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
528 	if (cqr == NULL)
529 		return ERR_PTR(-ENOMEM);
530 	memset(cqr, 0, sizeof(struct dasd_ccw_req));
531 	cqr->cpaddr = NULL;
532 	if (cplength > 0) {
533 		cqr->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
534 				      GFP_ATOMIC | GFP_DMA);
535 		if (cqr->cpaddr == NULL) {
536 			kfree(cqr);
537 			return ERR_PTR(-ENOMEM);
538 		}
539 		memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
540 	}
541 	cqr->data = NULL;
542 	if (datasize > 0) {
543 		cqr->data = kmalloc(datasize, GFP_ATOMIC | GFP_DMA);
544 		if (cqr->data == NULL) {
545 			kfree(cqr->cpaddr);
546 			kfree(cqr);
547 			return ERR_PTR(-ENOMEM);
548 		}
549 		memset(cqr->data, 0, datasize);
550 	}
551 	strncpy((char *) &cqr->magic, magic, 4);
552 	ASCEBC((char *) &cqr->magic, 4);
553 	set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
554 	dasd_get_device(device);
555 	return cqr;
556 }
557 
558 struct dasd_ccw_req *
559 dasd_smalloc_request(char *magic, int cplength, int datasize,
560 		   struct dasd_device * device)
561 {
562 	unsigned long flags;
563 	struct dasd_ccw_req *cqr;
564 	char *data;
565 	int size;
566 
567 	/* Sanity checks */
568 	if ( magic == NULL || datasize > PAGE_SIZE ||
569 	     (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
570 		BUG();
571 
572 	size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
573 	if (cplength > 0)
574 		size += cplength * sizeof(struct ccw1);
575 	if (datasize > 0)
576 		size += datasize;
577 	spin_lock_irqsave(&device->mem_lock, flags);
578 	cqr = (struct dasd_ccw_req *)
579 		dasd_alloc_chunk(&device->ccw_chunks, size);
580 	spin_unlock_irqrestore(&device->mem_lock, flags);
581 	if (cqr == NULL)
582 		return ERR_PTR(-ENOMEM);
583 	memset(cqr, 0, sizeof(struct dasd_ccw_req));
584 	data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
585 	cqr->cpaddr = NULL;
586 	if (cplength > 0) {
587 		cqr->cpaddr = (struct ccw1 *) data;
588 		data += cplength*sizeof(struct ccw1);
589 		memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
590 	}
591 	cqr->data = NULL;
592 	if (datasize > 0) {
593 		cqr->data = data;
594  		memset(cqr->data, 0, datasize);
595 	}
596 	strncpy((char *) &cqr->magic, magic, 4);
597 	ASCEBC((char *) &cqr->magic, 4);
598 	set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
599 	dasd_get_device(device);
600 	return cqr;
601 }
602 
603 /*
604  * Free memory of a channel program. This function needs to free all the
605  * idal lists that might have been created by dasd_set_cda and the
606  * struct dasd_ccw_req itself.
607  */
608 void
609 dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
610 {
611 #ifdef CONFIG_64BIT
612 	struct ccw1 *ccw;
613 
614 	/* Clear any idals used for the request. */
615 	ccw = cqr->cpaddr;
616 	do {
617 		clear_normalized_cda(ccw);
618 	} while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
619 #endif
620 	kfree(cqr->cpaddr);
621 	kfree(cqr->data);
622 	kfree(cqr);
623 	dasd_put_device(device);
624 }
625 
626 void
627 dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
628 {
629 	unsigned long flags;
630 
631 	spin_lock_irqsave(&device->mem_lock, flags);
632 	dasd_free_chunk(&device->ccw_chunks, cqr);
633 	spin_unlock_irqrestore(&device->mem_lock, flags);
634 	dasd_put_device(device);
635 }
636 
637 /*
638  * Check discipline magic in cqr.
639  */
640 static inline int
641 dasd_check_cqr(struct dasd_ccw_req *cqr)
642 {
643 	struct dasd_device *device;
644 
645 	if (cqr == NULL)
646 		return -EINVAL;
647 	device = cqr->device;
648 	if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
649 		DEV_MESSAGE(KERN_WARNING, device,
650 			    " dasd_ccw_req 0x%08x magic doesn't match"
651 			    " discipline 0x%08x",
652 			    cqr->magic,
653 			    *(unsigned int *) device->discipline->name);
654 		return -EINVAL;
655 	}
656 	return 0;
657 }
658 
659 /*
660  * Terminate the current i/o and set the request to clear_pending.
661  * Timer keeps device runnig.
662  * ccw_device_clear can fail if the i/o subsystem
663  * is in a bad mood.
664  */
665 int
666 dasd_term_IO(struct dasd_ccw_req * cqr)
667 {
668 	struct dasd_device *device;
669 	int retries, rc;
670 
671 	/* Check the cqr */
672 	rc = dasd_check_cqr(cqr);
673 	if (rc)
674 		return rc;
675 	retries = 0;
676 	device = (struct dasd_device *) cqr->device;
677 	while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
678 		rc = ccw_device_clear(device->cdev, (long) cqr);
679 		switch (rc) {
680 		case 0:	/* termination successful */
681 			cqr->retries--;
682 			cqr->status = DASD_CQR_CLEAR;
683 			cqr->stopclk = get_clock();
684 			DBF_DEV_EVENT(DBF_DEBUG, device,
685 				      "terminate cqr %p successful",
686 				      cqr);
687 			break;
688 		case -ENODEV:
689 			DBF_DEV_EVENT(DBF_ERR, device, "%s",
690 				      "device gone, retry");
691 			break;
692 		case -EIO:
693 			DBF_DEV_EVENT(DBF_ERR, device, "%s",
694 				      "I/O error, retry");
695 			break;
696 		case -EINVAL:
697 		case -EBUSY:
698 			DBF_DEV_EVENT(DBF_ERR, device, "%s",
699 				      "device busy, retry later");
700 			break;
701 		default:
702 			DEV_MESSAGE(KERN_ERR, device,
703 				    "line %d unknown RC=%d, please "
704 				    "report to linux390@de.ibm.com",
705 				    __LINE__, rc);
706 			BUG();
707 			break;
708 		}
709 		retries++;
710 	}
711 	dasd_schedule_bh(device);
712 	return rc;
713 }
714 
715 /*
716  * Start the i/o. This start_IO can fail if the channel is really busy.
717  * In that case set up a timer to start the request later.
718  */
719 int
720 dasd_start_IO(struct dasd_ccw_req * cqr)
721 {
722 	struct dasd_device *device;
723 	int rc;
724 
725 	/* Check the cqr */
726 	rc = dasd_check_cqr(cqr);
727 	if (rc)
728 		return rc;
729 	device = (struct dasd_device *) cqr->device;
730 	if (cqr->retries < 0) {
731 		DEV_MESSAGE(KERN_DEBUG, device,
732 			    "start_IO: request %p (%02x/%i) - no retry left.",
733 			    cqr, cqr->status, cqr->retries);
734 		cqr->status = DASD_CQR_FAILED;
735 		return -EIO;
736 	}
737 	cqr->startclk = get_clock();
738 	cqr->starttime = jiffies;
739 	cqr->retries--;
740 	rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
741 			      cqr->lpm, 0);
742 	switch (rc) {
743 	case 0:
744 		cqr->status = DASD_CQR_IN_IO;
745 		DBF_DEV_EVENT(DBF_DEBUG, device,
746 			      "start_IO: request %p started successful",
747 			      cqr);
748 		break;
749 	case -EBUSY:
750 		DBF_DEV_EVENT(DBF_ERR, device, "%s",
751 			      "start_IO: device busy, retry later");
752 		break;
753 	case -ETIMEDOUT:
754 		DBF_DEV_EVENT(DBF_ERR, device, "%s",
755 			      "start_IO: request timeout, retry later");
756 		break;
757 	case -EACCES:
758 		/* -EACCES indicates that the request used only a
759 		 * subset of the available pathes and all these
760 		 * pathes are gone.
761 		 * Do a retry with all available pathes.
762 		 */
763 		cqr->lpm = LPM_ANYPATH;
764 		DBF_DEV_EVENT(DBF_ERR, device, "%s",
765 			      "start_IO: selected pathes gone,"
766 			      " retry on all pathes");
767 		break;
768 	case -ENODEV:
769 	case -EIO:
770 		DBF_DEV_EVENT(DBF_ERR, device, "%s",
771 			      "start_IO: device gone, retry");
772 		break;
773 	default:
774 		DEV_MESSAGE(KERN_ERR, device,
775 			    "line %d unknown RC=%d, please report"
776 			    " to linux390@de.ibm.com", __LINE__, rc);
777 		BUG();
778 		break;
779 	}
780 	return rc;
781 }
782 
783 /*
784  * Timeout function for dasd devices. This is used for different purposes
785  *  1) missing interrupt handler for normal operation
786  *  2) delayed start of request where start_IO failed with -EBUSY
787  *  3) timeout for missing state change interrupts
788  * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
789  * DASD_CQR_QUEUED for 2) and 3).
790  */
791 static void
792 dasd_timeout_device(unsigned long ptr)
793 {
794 	unsigned long flags;
795 	struct dasd_device *device;
796 
797 	device = (struct dasd_device *) ptr;
798 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
799 	/* re-activate request queue */
800         device->stopped &= ~DASD_STOPPED_PENDING;
801 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
802 	dasd_schedule_bh(device);
803 }
804 
805 /*
806  * Setup timeout for a device in jiffies.
807  */
808 void
809 dasd_set_timer(struct dasd_device *device, int expires)
810 {
811 	if (expires == 0) {
812 		if (timer_pending(&device->timer))
813 			del_timer(&device->timer);
814 		return;
815 	}
816 	if (timer_pending(&device->timer)) {
817 		if (mod_timer(&device->timer, jiffies + expires))
818 			return;
819 	}
820 	device->timer.function = dasd_timeout_device;
821 	device->timer.data = (unsigned long) device;
822 	device->timer.expires = jiffies + expires;
823 	add_timer(&device->timer);
824 }
825 
826 /*
827  * Clear timeout for a device.
828  */
829 void
830 dasd_clear_timer(struct dasd_device *device)
831 {
832 	if (timer_pending(&device->timer))
833 		del_timer(&device->timer);
834 }
835 
836 static void
837 dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
838 {
839 	struct dasd_ccw_req *cqr;
840 	struct dasd_device *device;
841 
842 	cqr = (struct dasd_ccw_req *) intparm;
843 	if (cqr->status != DASD_CQR_IN_IO) {
844 		MESSAGE(KERN_DEBUG,
845 			"invalid status in handle_killed_request: "
846 			"bus_id %s, status %02x",
847 			cdev->dev.bus_id, cqr->status);
848 		return;
849 	}
850 
851 	device = (struct dasd_device *) cqr->device;
852 	if (device == NULL ||
853 	    device != dasd_device_from_cdev(cdev) ||
854 	    strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
855 		MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
856 			cdev->dev.bus_id);
857 		return;
858 	}
859 
860 	/* Schedule request to be retried. */
861 	cqr->status = DASD_CQR_QUEUED;
862 
863 	dasd_clear_timer(device);
864 	dasd_schedule_bh(device);
865 	dasd_put_device(device);
866 }
867 
868 static void
869 dasd_handle_state_change_pending(struct dasd_device *device)
870 {
871 	struct dasd_ccw_req *cqr;
872 	struct list_head *l, *n;
873 
874 	/* first of all call extended error reporting */
875 	dasd_write_eer_trigger(DASD_EER_STATECHANGE, device, NULL);
876 
877 	device->stopped &= ~DASD_STOPPED_PENDING;
878 
879         /* restart all 'running' IO on queue */
880 	list_for_each_safe(l, n, &device->ccw_queue) {
881 		cqr = list_entry(l, struct dasd_ccw_req, list);
882                 if (cqr->status == DASD_CQR_IN_IO) {
883                         cqr->status = DASD_CQR_QUEUED;
884 		}
885         }
886 	dasd_clear_timer(device);
887 	dasd_schedule_bh(device);
888 }
889 
890 /*
891  * Interrupt handler for "normal" ssch-io based dasd devices.
892  */
893 void
894 dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
895 		 struct irb *irb)
896 {
897 	struct dasd_ccw_req *cqr, *next;
898 	struct dasd_device *device;
899 	unsigned long long now;
900 	int expires;
901 	dasd_era_t era;
902 	char mask;
903 
904 	if (IS_ERR(irb)) {
905 		switch (PTR_ERR(irb)) {
906 		case -EIO:
907 			dasd_handle_killed_request(cdev, intparm);
908 			break;
909 		case -ETIMEDOUT:
910 			printk(KERN_WARNING"%s(%s): request timed out\n",
911 			       __FUNCTION__, cdev->dev.bus_id);
912 			//FIXME - dasd uses own timeout interface...
913 			break;
914 		default:
915 			printk(KERN_WARNING"%s(%s): unknown error %ld\n",
916 			       __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
917 		}
918 		return;
919 	}
920 
921 	now = get_clock();
922 
923 	DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
924 		  cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
925 		  (unsigned int) intparm);
926 
927 	/* first of all check for state change pending interrupt */
928 	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
929 	if ((irb->scsw.dstat & mask) == mask) {
930 		device = dasd_device_from_cdev(cdev);
931 		if (!IS_ERR(device)) {
932 			dasd_handle_state_change_pending(device);
933 			dasd_put_device(device);
934 		}
935 		return;
936 	}
937 
938 	cqr = (struct dasd_ccw_req *) intparm;
939 
940 	/* check for unsolicited interrupts */
941 	if (cqr == NULL) {
942 		MESSAGE(KERN_DEBUG,
943 			"unsolicited interrupt received: bus_id %s",
944 			cdev->dev.bus_id);
945 		return;
946 	}
947 
948 	device = (struct dasd_device *) cqr->device;
949 	if (device == NULL ||
950 	    strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
951 		MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
952 			cdev->dev.bus_id);
953 		return;
954 	}
955 
956 	/* Check for clear pending */
957 	if (cqr->status == DASD_CQR_CLEAR &&
958 	    irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
959 		cqr->status = DASD_CQR_QUEUED;
960 		dasd_clear_timer(device);
961 		dasd_schedule_bh(device);
962 		return;
963 	}
964 
965  	/* check status - the request might have been killed by dyn detach */
966 	if (cqr->status != DASD_CQR_IN_IO) {
967 		MESSAGE(KERN_DEBUG,
968 			"invalid status: bus_id %s, status %02x",
969 			cdev->dev.bus_id, cqr->status);
970 		return;
971 	}
972 	DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
973 		      ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
974 
975  	/* Find out the appropriate era_action. */
976 	if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
977 		era = dasd_era_fatal;
978 	else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
979 		 irb->scsw.cstat == 0 &&
980 		 !irb->esw.esw0.erw.cons)
981 		era = dasd_era_none;
982 	else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
983  	        era = dasd_era_fatal; /* don't recover this request */
984 	else if (irb->esw.esw0.erw.cons)
985 		era = device->discipline->examine_error(cqr, irb);
986 	else
987 		era = dasd_era_recover;
988 
989 	DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
990 	expires = 0;
991 	if (era == dasd_era_none) {
992 		cqr->status = DASD_CQR_DONE;
993 		cqr->stopclk = now;
994 		/* Start first request on queue if possible -> fast_io. */
995 		if (cqr->list.next != &device->ccw_queue) {
996 			next = list_entry(cqr->list.next,
997 					  struct dasd_ccw_req, list);
998 			if ((next->status == DASD_CQR_QUEUED) &&
999 			    (!device->stopped)) {
1000 				if (device->discipline->start_IO(next) == 0)
1001 					expires = next->expires;
1002 				else
1003 					DEV_MESSAGE(KERN_DEBUG, device, "%s",
1004 						    "Interrupt fastpath "
1005 						    "failed!");
1006 			}
1007 		}
1008 	} else {		/* error */
1009 		memcpy(&cqr->irb, irb, sizeof (struct irb));
1010 #ifdef ERP_DEBUG
1011 		/* dump sense data */
1012 		dasd_log_sense(cqr, irb);
1013 #endif
1014 		switch (era) {
1015 		case dasd_era_fatal:
1016 			cqr->status = DASD_CQR_FAILED;
1017 			cqr->stopclk = now;
1018 			break;
1019 		case dasd_era_recover:
1020 			cqr->status = DASD_CQR_ERROR;
1021 			break;
1022 		default:
1023 			BUG();
1024 		}
1025 	}
1026 	if (expires != 0)
1027 		dasd_set_timer(device, expires);
1028 	else
1029 		dasd_clear_timer(device);
1030 	dasd_schedule_bh(device);
1031 }
1032 
1033 /*
1034  * posts the buffer_cache about a finalized request
1035  */
1036 static inline void
1037 dasd_end_request(struct request *req, int uptodate)
1038 {
1039 	if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1040 		BUG();
1041 	add_disk_randomness(req->rq_disk);
1042 	end_that_request_last(req, uptodate);
1043 }
1044 
1045 /*
1046  * Process finished error recovery ccw.
1047  */
1048 static inline void
1049 __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1050 {
1051 	dasd_erp_fn_t erp_fn;
1052 
1053 	if (cqr->status == DASD_CQR_DONE)
1054 		DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1055 	else
1056 		DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1057 	erp_fn = device->discipline->erp_postaction(cqr);
1058 	erp_fn(cqr);
1059 }
1060 
1061 /*
1062  * Process ccw request queue.
1063  */
1064 static inline void
1065 __dasd_process_ccw_queue(struct dasd_device * device,
1066 			 struct list_head *final_queue)
1067 {
1068 	struct list_head *l, *n;
1069 	struct dasd_ccw_req *cqr;
1070 	dasd_erp_fn_t erp_fn;
1071 
1072 restart:
1073 	/* Process request with final status. */
1074 	list_for_each_safe(l, n, &device->ccw_queue) {
1075 		cqr = list_entry(l, struct dasd_ccw_req, list);
1076 		/* Stop list processing at the first non-final request. */
1077 		if (cqr->status != DASD_CQR_DONE &&
1078 		    cqr->status != DASD_CQR_FAILED &&
1079 		    cqr->status != DASD_CQR_ERROR)
1080 			break;
1081 		/*  Process requests with DASD_CQR_ERROR */
1082 		if (cqr->status == DASD_CQR_ERROR) {
1083 			if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1084 				cqr->status = DASD_CQR_FAILED;
1085 				cqr->stopclk = get_clock();
1086 			} else {
1087 				if (cqr->irb.esw.esw0.erw.cons) {
1088 					erp_fn = device->discipline->
1089 						erp_action(cqr);
1090 					erp_fn(cqr);
1091 				} else
1092 					dasd_default_erp_action(cqr);
1093 			}
1094 			goto restart;
1095 		}
1096 
1097 		/* first of all call extended error reporting */
1098 		if (device->eer && cqr->status == DASD_CQR_FAILED) {
1099 			dasd_write_eer_trigger(DASD_EER_FATALERROR,
1100 					       device, cqr);
1101 
1102 			/* restart request  */
1103 			cqr->status = DASD_CQR_QUEUED;
1104 			cqr->retries = 255;
1105 			device->stopped |= DASD_STOPPED_QUIESCE;
1106 			goto restart;
1107 		}
1108 
1109 		/* Process finished ERP request. */
1110 		if (cqr->refers) {
1111 			__dasd_process_erp(device, cqr);
1112 			goto restart;
1113 		}
1114 
1115 		/* Rechain finished requests to final queue */
1116 		cqr->endclk = get_clock();
1117 		list_move_tail(&cqr->list, final_queue);
1118 	}
1119 }
1120 
1121 static void
1122 dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1123 {
1124 	struct request *req;
1125 	struct dasd_device *device;
1126 	int status;
1127 
1128 	req = (struct request *) data;
1129 	device = cqr->device;
1130 	dasd_profile_end(device, cqr, req);
1131 	status = cqr->device->discipline->free_cp(cqr,req);
1132 	spin_lock_irq(&device->request_queue_lock);
1133 	dasd_end_request(req, status);
1134 	spin_unlock_irq(&device->request_queue_lock);
1135 }
1136 
1137 
1138 /*
1139  * Fetch requests from the block device queue.
1140  */
1141 static inline void
1142 __dasd_process_blk_queue(struct dasd_device * device)
1143 {
1144 	request_queue_t *queue;
1145 	struct request *req;
1146 	struct dasd_ccw_req *cqr;
1147 	int nr_queued;
1148 
1149 	queue = device->request_queue;
1150 	/* No queue ? Then there is nothing to do. */
1151 	if (queue == NULL)
1152 		return;
1153 
1154 	/*
1155 	 * We requeue request from the block device queue to the ccw
1156 	 * queue only in two states. In state DASD_STATE_READY the
1157 	 * partition detection is done and we need to requeue requests
1158 	 * for that. State DASD_STATE_ONLINE is normal block device
1159 	 * operation.
1160 	 */
1161 	if (device->state != DASD_STATE_READY &&
1162 	    device->state != DASD_STATE_ONLINE)
1163 		return;
1164 	nr_queued = 0;
1165 	/* Now we try to fetch requests from the request queue */
1166 	list_for_each_entry(cqr, &device->ccw_queue, list)
1167 		if (cqr->status == DASD_CQR_QUEUED)
1168 			nr_queued++;
1169 	while (!blk_queue_plugged(queue) &&
1170 	       elv_next_request(queue) &&
1171 		nr_queued < DASD_CHANQ_MAX_SIZE) {
1172 		req = elv_next_request(queue);
1173 
1174 		if (device->features & DASD_FEATURE_READONLY &&
1175 		    rq_data_dir(req) == WRITE) {
1176 			DBF_DEV_EVENT(DBF_ERR, device,
1177 				      "Rejecting write request %p",
1178 				      req);
1179 			blkdev_dequeue_request(req);
1180 			dasd_end_request(req, 0);
1181 			continue;
1182 		}
1183 		if (device->stopped & DASD_STOPPED_DC_EIO) {
1184 			blkdev_dequeue_request(req);
1185 			dasd_end_request(req, 0);
1186 			continue;
1187 		}
1188 		cqr = device->discipline->build_cp(device, req);
1189 		if (IS_ERR(cqr)) {
1190 			if (PTR_ERR(cqr) == -ENOMEM)
1191 				break;	/* terminate request queue loop */
1192 			DBF_DEV_EVENT(DBF_ERR, device,
1193 				      "CCW creation failed (rc=%ld) "
1194 				      "on request %p",
1195 				      PTR_ERR(cqr), req);
1196 			blkdev_dequeue_request(req);
1197 			dasd_end_request(req, 0);
1198 			continue;
1199 		}
1200 		cqr->callback = dasd_end_request_cb;
1201 		cqr->callback_data = (void *) req;
1202 		cqr->status = DASD_CQR_QUEUED;
1203 		blkdev_dequeue_request(req);
1204 		list_add_tail(&cqr->list, &device->ccw_queue);
1205 		dasd_profile_start(device, cqr, req);
1206 		nr_queued++;
1207 	}
1208 }
1209 
1210 /*
1211  * Take a look at the first request on the ccw queue and check
1212  * if it reached its expire time. If so, terminate the IO.
1213  */
1214 static inline void
1215 __dasd_check_expire(struct dasd_device * device)
1216 {
1217 	struct dasd_ccw_req *cqr;
1218 
1219 	if (list_empty(&device->ccw_queue))
1220 		return;
1221 	cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1222 	if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1223 		if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1224 			if (device->discipline->term_IO(cqr) != 0)
1225 				/* Hmpf, try again in 1/10 sec */
1226 				dasd_set_timer(device, 10);
1227 		}
1228 	}
1229 }
1230 
1231 /*
1232  * Take a look at the first request on the ccw queue and check
1233  * if it needs to be started.
1234  */
1235 static inline void
1236 __dasd_start_head(struct dasd_device * device)
1237 {
1238 	struct dasd_ccw_req *cqr;
1239 	int rc;
1240 
1241 	if (list_empty(&device->ccw_queue))
1242 		return;
1243 	cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1244         /* check FAILFAST */
1245 	if (device->stopped & ~DASD_STOPPED_PENDING &&
1246 	    test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1247 	    (!device->eer)) {
1248 		cqr->status = DASD_CQR_FAILED;
1249 		dasd_schedule_bh(device);
1250 	}
1251 	if ((cqr->status == DASD_CQR_QUEUED) &&
1252 	    (!device->stopped)) {
1253 		/* try to start the first I/O that can be started */
1254 		rc = device->discipline->start_IO(cqr);
1255 		if (rc == 0)
1256 			dasd_set_timer(device, cqr->expires);
1257 		else if (rc == -EACCES) {
1258 			dasd_schedule_bh(device);
1259 		} else
1260 			/* Hmpf, try again in 1/2 sec */
1261 			dasd_set_timer(device, 50);
1262 	}
1263 }
1264 
1265 /*
1266  * Remove requests from the ccw queue.
1267  */
1268 static void
1269 dasd_flush_ccw_queue(struct dasd_device * device, int all)
1270 {
1271 	struct list_head flush_queue;
1272 	struct list_head *l, *n;
1273 	struct dasd_ccw_req *cqr;
1274 
1275 	INIT_LIST_HEAD(&flush_queue);
1276 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1277 	list_for_each_safe(l, n, &device->ccw_queue) {
1278 		cqr = list_entry(l, struct dasd_ccw_req, list);
1279 		/* Flush all request or only block device requests? */
1280 		if (all == 0 && cqr->callback == dasd_end_request_cb)
1281 			continue;
1282 		if (cqr->status == DASD_CQR_IN_IO)
1283 			device->discipline->term_IO(cqr);
1284 		if (cqr->status != DASD_CQR_DONE ||
1285 		    cqr->status != DASD_CQR_FAILED) {
1286 			cqr->status = DASD_CQR_FAILED;
1287 			cqr->stopclk = get_clock();
1288 		}
1289 		/* Process finished ERP request. */
1290 		if (cqr->refers) {
1291 			__dasd_process_erp(device, cqr);
1292 			continue;
1293 		}
1294 		/* Rechain request on device request queue */
1295 		cqr->endclk = get_clock();
1296 		list_move_tail(&cqr->list, &flush_queue);
1297 	}
1298 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
1299 	/* Now call the callback function of flushed requests */
1300 	list_for_each_safe(l, n, &flush_queue) {
1301 		cqr = list_entry(l, struct dasd_ccw_req, list);
1302 		if (cqr->callback != NULL)
1303 			(cqr->callback)(cqr, cqr->callback_data);
1304 	}
1305 }
1306 
1307 /*
1308  * Acquire the device lock and process queues for the device.
1309  */
1310 static void
1311 dasd_tasklet(struct dasd_device * device)
1312 {
1313 	struct list_head final_queue;
1314 	struct list_head *l, *n;
1315 	struct dasd_ccw_req *cqr;
1316 
1317 	atomic_set (&device->tasklet_scheduled, 0);
1318 	INIT_LIST_HEAD(&final_queue);
1319 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1320 	/* Check expire time of first request on the ccw queue. */
1321 	__dasd_check_expire(device);
1322 	/* Finish off requests on ccw queue */
1323 	__dasd_process_ccw_queue(device, &final_queue);
1324 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
1325 	/* Now call the callback function of requests with final status */
1326 	list_for_each_safe(l, n, &final_queue) {
1327 		cqr = list_entry(l, struct dasd_ccw_req, list);
1328 		list_del_init(&cqr->list);
1329 		if (cqr->callback != NULL)
1330 			(cqr->callback)(cqr, cqr->callback_data);
1331 	}
1332 	spin_lock_irq(&device->request_queue_lock);
1333 	spin_lock(get_ccwdev_lock(device->cdev));
1334 	/* Get new request from the block device request queue */
1335 	__dasd_process_blk_queue(device);
1336 	/* Now check if the head of the ccw queue needs to be started. */
1337 	__dasd_start_head(device);
1338 	spin_unlock(get_ccwdev_lock(device->cdev));
1339 	spin_unlock_irq(&device->request_queue_lock);
1340 	dasd_put_device(device);
1341 }
1342 
1343 /*
1344  * Schedules a call to dasd_tasklet over the device tasklet.
1345  */
1346 void
1347 dasd_schedule_bh(struct dasd_device * device)
1348 {
1349 	/* Protect against rescheduling. */
1350 	if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1351 		return;
1352 	dasd_get_device(device);
1353 	tasklet_hi_schedule(&device->tasklet);
1354 }
1355 
1356 /*
1357  * Queue a request to the head of the ccw_queue. Start the I/O if
1358  * possible.
1359  */
1360 void
1361 dasd_add_request_head(struct dasd_ccw_req *req)
1362 {
1363 	struct dasd_device *device;
1364 	unsigned long flags;
1365 
1366 	device = req->device;
1367 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1368 	req->status = DASD_CQR_QUEUED;
1369 	req->device = device;
1370 	list_add(&req->list, &device->ccw_queue);
1371 	/* let the bh start the request to keep them in order */
1372 	dasd_schedule_bh(device);
1373 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1374 }
1375 
1376 /*
1377  * Queue a request to the tail of the ccw_queue. Start the I/O if
1378  * possible.
1379  */
1380 void
1381 dasd_add_request_tail(struct dasd_ccw_req *req)
1382 {
1383 	struct dasd_device *device;
1384 	unsigned long flags;
1385 
1386 	device = req->device;
1387 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1388 	req->status = DASD_CQR_QUEUED;
1389 	req->device = device;
1390 	list_add_tail(&req->list, &device->ccw_queue);
1391 	/* let the bh start the request to keep them in order */
1392 	dasd_schedule_bh(device);
1393 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1394 }
1395 
1396 /*
1397  * Wakeup callback.
1398  */
1399 static void
1400 dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1401 {
1402 	wake_up((wait_queue_head_t *) data);
1403 }
1404 
1405 static inline int
1406 _wait_for_wakeup(struct dasd_ccw_req *cqr)
1407 {
1408 	struct dasd_device *device;
1409 	int rc;
1410 
1411 	device = cqr->device;
1412 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1413 	rc = ((cqr->status == DASD_CQR_DONE ||
1414 	       cqr->status == DASD_CQR_FAILED) &&
1415 	      list_empty(&cqr->list));
1416 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
1417 	return rc;
1418 }
1419 
1420 /*
1421  * Attempts to start a special ccw queue and waits for its completion.
1422  */
1423 int
1424 dasd_sleep_on(struct dasd_ccw_req * cqr)
1425 {
1426 	wait_queue_head_t wait_q;
1427 	struct dasd_device *device;
1428 	int rc;
1429 
1430 	device = cqr->device;
1431 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1432 
1433 	init_waitqueue_head (&wait_q);
1434 	cqr->callback = dasd_wakeup_cb;
1435 	cqr->callback_data = (void *) &wait_q;
1436 	cqr->status = DASD_CQR_QUEUED;
1437 	list_add_tail(&cqr->list, &device->ccw_queue);
1438 
1439 	/* let the bh start the request to keep them in order */
1440 	dasd_schedule_bh(device);
1441 
1442 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
1443 
1444 	wait_event(wait_q, _wait_for_wakeup(cqr));
1445 
1446 	/* Request status is either done or failed. */
1447 	rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1448 	return rc;
1449 }
1450 
1451 /*
1452  * Attempts to start a special ccw queue and wait interruptible
1453  * for its completion.
1454  */
1455 int
1456 dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1457 {
1458 	wait_queue_head_t wait_q;
1459 	struct dasd_device *device;
1460 	int rc, finished;
1461 
1462 	device = cqr->device;
1463 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1464 
1465 	init_waitqueue_head (&wait_q);
1466 	cqr->callback = dasd_wakeup_cb;
1467 	cqr->callback_data = (void *) &wait_q;
1468 	cqr->status = DASD_CQR_QUEUED;
1469 	list_add_tail(&cqr->list, &device->ccw_queue);
1470 
1471 	/* let the bh start the request to keep them in order */
1472 	dasd_schedule_bh(device);
1473 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
1474 
1475 	finished = 0;
1476 	while (!finished) {
1477 		rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1478 		if (rc != -ERESTARTSYS) {
1479 			/* Request is final (done or failed) */
1480 			rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1481 			break;
1482 		}
1483 		spin_lock_irq(get_ccwdev_lock(device->cdev));
1484 		switch (cqr->status) {
1485 		case DASD_CQR_IN_IO:
1486                         /* terminate runnig cqr */
1487 			if (device->discipline->term_IO) {
1488 				cqr->retries = -1;
1489 				device->discipline->term_IO(cqr);
1490 				/*nished =
1491 				 * wait (non-interruptible) for final status
1492 				 * because signal ist still pending
1493 				 */
1494 				spin_unlock_irq(get_ccwdev_lock(device->cdev));
1495 				wait_event(wait_q, _wait_for_wakeup(cqr));
1496 				spin_lock_irq(get_ccwdev_lock(device->cdev));
1497 				rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1498 				finished = 1;
1499 			}
1500 			break;
1501 		case DASD_CQR_QUEUED:
1502 			/* request  */
1503 			list_del_init(&cqr->list);
1504 			rc = -EIO;
1505 			finished = 1;
1506 			break;
1507 		default:
1508 			/* cqr with 'non-interruptable' status - just wait */
1509 			break;
1510 		}
1511 		spin_unlock_irq(get_ccwdev_lock(device->cdev));
1512 	}
1513 	return rc;
1514 }
1515 
1516 /*
1517  * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1518  * for eckd devices) the currently running request has to be terminated
1519  * and be put back to status queued, before the special request is added
1520  * to the head of the queue. Then the special request is waited on normally.
1521  */
1522 static inline int
1523 _dasd_term_running_cqr(struct dasd_device *device)
1524 {
1525 	struct dasd_ccw_req *cqr;
1526 	int rc;
1527 
1528 	if (list_empty(&device->ccw_queue))
1529 		return 0;
1530 	cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1531 	rc = device->discipline->term_IO(cqr);
1532 	if (rc == 0) {
1533 		/* termination successful */
1534 		cqr->status = DASD_CQR_QUEUED;
1535 		cqr->startclk = cqr->stopclk = 0;
1536 		cqr->starttime = 0;
1537 	}
1538 	return rc;
1539 }
1540 
1541 int
1542 dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1543 {
1544 	wait_queue_head_t wait_q;
1545 	struct dasd_device *device;
1546 	int rc;
1547 
1548 	device = cqr->device;
1549 	spin_lock_irq(get_ccwdev_lock(device->cdev));
1550 	rc = _dasd_term_running_cqr(device);
1551 	if (rc) {
1552 		spin_unlock_irq(get_ccwdev_lock(device->cdev));
1553 		return rc;
1554 	}
1555 
1556 	init_waitqueue_head (&wait_q);
1557 	cqr->callback = dasd_wakeup_cb;
1558 	cqr->callback_data = (void *) &wait_q;
1559 	cqr->status = DASD_CQR_QUEUED;
1560 	list_add(&cqr->list, &device->ccw_queue);
1561 
1562 	/* let the bh start the request to keep them in order */
1563 	dasd_schedule_bh(device);
1564 
1565 	spin_unlock_irq(get_ccwdev_lock(device->cdev));
1566 
1567 	wait_event(wait_q, _wait_for_wakeup(cqr));
1568 
1569 	/* Request status is either done or failed. */
1570 	rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1571 	return rc;
1572 }
1573 
1574 /*
1575  * Cancels a request that was started with dasd_sleep_on_req.
1576  * This is useful to timeout requests. The request will be
1577  * terminated if it is currently in i/o.
1578  * Returns 1 if the request has been terminated.
1579  */
1580 int
1581 dasd_cancel_req(struct dasd_ccw_req *cqr)
1582 {
1583 	struct dasd_device *device = cqr->device;
1584 	unsigned long flags;
1585 	int rc;
1586 
1587 	rc = 0;
1588 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1589 	switch (cqr->status) {
1590 	case DASD_CQR_QUEUED:
1591 		/* request was not started - just set to failed */
1592 		cqr->status = DASD_CQR_FAILED;
1593 		break;
1594 	case DASD_CQR_IN_IO:
1595 		/* request in IO - terminate IO and release again */
1596 		if (device->discipline->term_IO(cqr) != 0)
1597 			/* what to do if unable to terminate ??????
1598 			   e.g. not _IN_IO */
1599 			cqr->status = DASD_CQR_FAILED;
1600 		cqr->stopclk = get_clock();
1601 		rc = 1;
1602 		break;
1603 	case DASD_CQR_DONE:
1604 	case DASD_CQR_FAILED:
1605 		/* already finished - do nothing */
1606 		break;
1607 	default:
1608 		DEV_MESSAGE(KERN_ALERT, device,
1609 			    "invalid status %02x in request",
1610 			    cqr->status);
1611 		BUG();
1612 
1613 	}
1614 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1615 	dasd_schedule_bh(device);
1616 	return rc;
1617 }
1618 
1619 /*
1620  * SECTION: Block device operations (request queue, partitions, open, release).
1621  */
1622 
1623 /*
1624  * Dasd request queue function. Called from ll_rw_blk.c
1625  */
1626 static void
1627 do_dasd_request(request_queue_t * queue)
1628 {
1629 	struct dasd_device *device;
1630 
1631 	device = (struct dasd_device *) queue->queuedata;
1632 	spin_lock(get_ccwdev_lock(device->cdev));
1633 	/* Get new request from the block device request queue */
1634 	__dasd_process_blk_queue(device);
1635 	/* Now check if the head of the ccw queue needs to be started. */
1636 	__dasd_start_head(device);
1637 	spin_unlock(get_ccwdev_lock(device->cdev));
1638 }
1639 
1640 /*
1641  * Allocate and initialize request queue and default I/O scheduler.
1642  */
1643 static int
1644 dasd_alloc_queue(struct dasd_device * device)
1645 {
1646 	int rc;
1647 
1648 	device->request_queue = blk_init_queue(do_dasd_request,
1649 					       &device->request_queue_lock);
1650 	if (device->request_queue == NULL)
1651 		return -ENOMEM;
1652 
1653 	device->request_queue->queuedata = device;
1654 
1655 	elevator_exit(device->request_queue->elevator);
1656 	rc = elevator_init(device->request_queue, "deadline");
1657 	if (rc) {
1658 		blk_cleanup_queue(device->request_queue);
1659 		return rc;
1660 	}
1661 	return 0;
1662 }
1663 
1664 /*
1665  * Allocate and initialize request queue.
1666  */
1667 static void
1668 dasd_setup_queue(struct dasd_device * device)
1669 {
1670 	int max;
1671 
1672 	blk_queue_hardsect_size(device->request_queue, device->bp_block);
1673 	max = device->discipline->max_blocks << device->s2b_shift;
1674 	blk_queue_max_sectors(device->request_queue, max);
1675 	blk_queue_max_phys_segments(device->request_queue, -1L);
1676 	blk_queue_max_hw_segments(device->request_queue, -1L);
1677 	blk_queue_max_segment_size(device->request_queue, -1L);
1678 	blk_queue_segment_boundary(device->request_queue, -1L);
1679 	blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
1680 }
1681 
1682 /*
1683  * Deactivate and free request queue.
1684  */
1685 static void
1686 dasd_free_queue(struct dasd_device * device)
1687 {
1688 	if (device->request_queue) {
1689 		blk_cleanup_queue(device->request_queue);
1690 		device->request_queue = NULL;
1691 	}
1692 }
1693 
1694 /*
1695  * Flush request on the request queue.
1696  */
1697 static void
1698 dasd_flush_request_queue(struct dasd_device * device)
1699 {
1700 	struct request *req;
1701 
1702 	if (!device->request_queue)
1703 		return;
1704 
1705 	spin_lock_irq(&device->request_queue_lock);
1706 	while (!list_empty(&device->request_queue->queue_head)) {
1707 		req = elv_next_request(device->request_queue);
1708 		if (req == NULL)
1709 			break;
1710 		dasd_end_request(req, 0);
1711 		blkdev_dequeue_request(req);
1712 	}
1713 	spin_unlock_irq(&device->request_queue_lock);
1714 }
1715 
1716 static int
1717 dasd_open(struct inode *inp, struct file *filp)
1718 {
1719 	struct gendisk *disk = inp->i_bdev->bd_disk;
1720 	struct dasd_device *device = disk->private_data;
1721 	int rc;
1722 
1723         atomic_inc(&device->open_count);
1724 	if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1725 		rc = -ENODEV;
1726 		goto unlock;
1727 	}
1728 
1729 	if (!try_module_get(device->discipline->owner)) {
1730 		rc = -EINVAL;
1731 		goto unlock;
1732 	}
1733 
1734 	if (dasd_probeonly) {
1735 		DEV_MESSAGE(KERN_INFO, device, "%s",
1736 			    "No access to device due to probeonly mode");
1737 		rc = -EPERM;
1738 		goto out;
1739 	}
1740 
1741 	if (device->state < DASD_STATE_BASIC) {
1742 		DBF_DEV_EVENT(DBF_ERR, device, " %s",
1743 			      " Cannot open unrecognized device");
1744 		rc = -ENODEV;
1745 		goto out;
1746 	}
1747 
1748 	return 0;
1749 
1750 out:
1751 	module_put(device->discipline->owner);
1752 unlock:
1753 	atomic_dec(&device->open_count);
1754 	return rc;
1755 }
1756 
1757 static int
1758 dasd_release(struct inode *inp, struct file *filp)
1759 {
1760 	struct gendisk *disk = inp->i_bdev->bd_disk;
1761 	struct dasd_device *device = disk->private_data;
1762 
1763 	atomic_dec(&device->open_count);
1764 	module_put(device->discipline->owner);
1765 	return 0;
1766 }
1767 
1768 /*
1769  * Return disk geometry.
1770  */
1771 static int
1772 dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1773 {
1774 	struct dasd_device *device;
1775 
1776 	device = bdev->bd_disk->private_data;
1777 	if (!device)
1778 		return -ENODEV;
1779 
1780 	if (!device->discipline ||
1781 	    !device->discipline->fill_geometry)
1782 		return -EINVAL;
1783 
1784 	device->discipline->fill_geometry(device, geo);
1785 	geo->start = get_start_sect(bdev) >> device->s2b_shift;
1786 	return 0;
1787 }
1788 
1789 struct block_device_operations
1790 dasd_device_operations = {
1791 	.owner		= THIS_MODULE,
1792 	.open		= dasd_open,
1793 	.release	= dasd_release,
1794 	.ioctl		= dasd_ioctl,
1795 	.compat_ioctl	= dasd_compat_ioctl,
1796 	.getgeo		= dasd_getgeo,
1797 };
1798 
1799 
1800 static void
1801 dasd_exit(void)
1802 {
1803 #ifdef CONFIG_PROC_FS
1804 	dasd_proc_exit();
1805 #endif
1806 	dasd_ioctl_exit();
1807         if (dasd_page_cache != NULL) {
1808 		kmem_cache_destroy(dasd_page_cache);
1809 		dasd_page_cache = NULL;
1810 	}
1811 	dasd_gendisk_exit();
1812 	dasd_devmap_exit();
1813 	devfs_remove("dasd");
1814 	if (dasd_debug_area != NULL) {
1815 		debug_unregister(dasd_debug_area);
1816 		dasd_debug_area = NULL;
1817 	}
1818 }
1819 
1820 /*
1821  * SECTION: common functions for ccw_driver use
1822  */
1823 
1824 /*
1825  * Initial attempt at a probe function. this can be simplified once
1826  * the other detection code is gone.
1827  */
1828 int
1829 dasd_generic_probe (struct ccw_device *cdev,
1830 		    struct dasd_discipline *discipline)
1831 {
1832 	int ret;
1833 
1834 	ret = dasd_add_sysfs_files(cdev);
1835 	if (ret) {
1836 		printk(KERN_WARNING
1837 		       "dasd_generic_probe: could not add sysfs entries "
1838 		       "for %s\n", cdev->dev.bus_id);
1839 	} else {
1840 		cdev->handler = &dasd_int_handler;
1841 	}
1842 
1843 	return ret;
1844 }
1845 
1846 /*
1847  * This will one day be called from a global not_oper handler.
1848  * It is also used by driver_unregister during module unload.
1849  */
1850 void
1851 dasd_generic_remove (struct ccw_device *cdev)
1852 {
1853 	struct dasd_device *device;
1854 
1855 	cdev->handler = NULL;
1856 
1857 	dasd_remove_sysfs_files(cdev);
1858 	device = dasd_device_from_cdev(cdev);
1859 	if (IS_ERR(device))
1860 		return;
1861 	if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1862 		/* Already doing offline processing */
1863 		dasd_put_device(device);
1864 		return;
1865 	}
1866 	/*
1867 	 * This device is removed unconditionally. Set offline
1868 	 * flag to prevent dasd_open from opening it while it is
1869 	 * no quite down yet.
1870 	 */
1871 	dasd_set_target_state(device, DASD_STATE_NEW);
1872 	/* dasd_delete_device destroys the device reference. */
1873 	dasd_delete_device(device);
1874 }
1875 
1876 /*
1877  * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1878  * the device is detected for the first time and is supposed to be used
1879  * or the user has started activation through sysfs.
1880  */
1881 int
1882 dasd_generic_set_online (struct ccw_device *cdev,
1883 			 struct dasd_discipline *discipline)
1884 
1885 {
1886 	struct dasd_device *device;
1887 	int rc;
1888 
1889 	device = dasd_create_device(cdev);
1890 	if (IS_ERR(device))
1891 		return PTR_ERR(device);
1892 
1893 	if (device->features & DASD_FEATURE_USEDIAG) {
1894 	  	if (!dasd_diag_discipline_pointer) {
1895 		        printk (KERN_WARNING
1896 				"dasd_generic couldn't online device %s "
1897 				"- discipline DIAG not available\n",
1898 				cdev->dev.bus_id);
1899 			dasd_delete_device(device);
1900 			return -ENODEV;
1901 		}
1902 		discipline = dasd_diag_discipline_pointer;
1903 	}
1904 	device->discipline = discipline;
1905 
1906 	rc = discipline->check_device(device);
1907 	if (rc) {
1908 		printk (KERN_WARNING
1909 			"dasd_generic couldn't online device %s "
1910 			"with discipline %s rc=%i\n",
1911 			cdev->dev.bus_id, discipline->name, rc);
1912 		dasd_delete_device(device);
1913 		return rc;
1914 	}
1915 
1916 	dasd_set_target_state(device, DASD_STATE_ONLINE);
1917 	if (device->state <= DASD_STATE_KNOWN) {
1918 		printk (KERN_WARNING
1919 			"dasd_generic discipline not found for %s\n",
1920 			cdev->dev.bus_id);
1921 		rc = -ENODEV;
1922 		dasd_set_target_state(device, DASD_STATE_NEW);
1923 		dasd_delete_device(device);
1924 	} else
1925 		pr_debug("dasd_generic device %s found\n",
1926 				cdev->dev.bus_id);
1927 
1928 	/* FIXME: we have to wait for the root device but we don't want
1929 	 * to wait for each single device but for all at once. */
1930 	wait_event(dasd_init_waitq, _wait_for_device(device));
1931 
1932 	dasd_put_device(device);
1933 
1934 	return rc;
1935 }
1936 
1937 int
1938 dasd_generic_set_offline (struct ccw_device *cdev)
1939 {
1940 	struct dasd_device *device;
1941 	int max_count;
1942 
1943 	device = dasd_device_from_cdev(cdev);
1944 	if (IS_ERR(device))
1945 		return PTR_ERR(device);
1946 	if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1947 		/* Already doing offline processing */
1948 		dasd_put_device(device);
1949 		return 0;
1950 	}
1951 	/*
1952 	 * We must make sure that this device is currently not in use.
1953 	 * The open_count is increased for every opener, that includes
1954 	 * the blkdev_get in dasd_scan_partitions. We are only interested
1955 	 * in the other openers.
1956 	 */
1957 	max_count = device->bdev ? 0 : -1;
1958 	if (atomic_read(&device->open_count) > max_count) {
1959 		printk (KERN_WARNING "Can't offline dasd device with open"
1960 			" count = %i.\n",
1961 			atomic_read(&device->open_count));
1962 		clear_bit(DASD_FLAG_OFFLINE, &device->flags);
1963 		dasd_put_device(device);
1964 		return -EBUSY;
1965 	}
1966 	dasd_set_target_state(device, DASD_STATE_NEW);
1967 	/* dasd_delete_device destroys the device reference. */
1968 	dasd_delete_device(device);
1969 
1970 	return 0;
1971 }
1972 
1973 int
1974 dasd_generic_notify(struct ccw_device *cdev, int event)
1975 {
1976 	struct dasd_device *device;
1977 	struct dasd_ccw_req *cqr;
1978 	unsigned long flags;
1979 	int ret;
1980 
1981 	device = dasd_device_from_cdev(cdev);
1982 	if (IS_ERR(device))
1983 		return 0;
1984 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1985 	ret = 0;
1986 	switch (event) {
1987 	case CIO_GONE:
1988 	case CIO_NO_PATH:
1989 		/* first of all call extended error reporting */
1990 		dasd_write_eer_trigger(DASD_EER_NOPATH, device, NULL);
1991 
1992 		if (device->state < DASD_STATE_BASIC)
1993 			break;
1994 		/* Device is active. We want to keep it. */
1995 		if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
1996 			list_for_each_entry(cqr, &device->ccw_queue, list)
1997 				if (cqr->status == DASD_CQR_IN_IO)
1998 					cqr->status = DASD_CQR_FAILED;
1999 			device->stopped |= DASD_STOPPED_DC_EIO;
2000 		} else {
2001 			list_for_each_entry(cqr, &device->ccw_queue, list)
2002 				if (cqr->status == DASD_CQR_IN_IO) {
2003 					cqr->status = DASD_CQR_QUEUED;
2004 					cqr->retries++;
2005 				}
2006 			device->stopped |= DASD_STOPPED_DC_WAIT;
2007 			dasd_set_timer(device, 0);
2008 		}
2009 		dasd_schedule_bh(device);
2010 		ret = 1;
2011 		break;
2012 	case CIO_OPER:
2013 		/* FIXME: add a sanity check. */
2014 		device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2015 		dasd_schedule_bh(device);
2016 		ret = 1;
2017 		break;
2018 	}
2019 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2020 	dasd_put_device(device);
2021 	return ret;
2022 }
2023 
2024 /*
2025  * Automatically online either all dasd devices (dasd_autodetect) or
2026  * all devices specified with dasd= parameters.
2027  */
2028 static int
2029 __dasd_auto_online(struct device *dev, void *data)
2030 {
2031 	struct ccw_device *cdev;
2032 
2033 	cdev = to_ccwdev(dev);
2034 	if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
2035 		ccw_device_set_online(cdev);
2036 	return 0;
2037 }
2038 
2039 void
2040 dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
2041 {
2042 	struct device_driver *drv;
2043 
2044 	drv = get_driver(&dasd_discipline_driver->driver);
2045 	driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
2046 	put_driver(drv);
2047 }
2048 
2049 /*
2050  * notifications for extended error reports
2051  */
2052 static struct notifier_block *dasd_eer_chain;
2053 
2054 int
2055 dasd_register_eer_notifier(struct notifier_block *nb)
2056 {
2057 	return notifier_chain_register(&dasd_eer_chain, nb);
2058 }
2059 
2060 int
2061 dasd_unregister_eer_notifier(struct notifier_block *nb)
2062 {
2063 	return notifier_chain_unregister(&dasd_eer_chain, nb);
2064 }
2065 
2066 /*
2067  * Notify the registered error reporting module of a problem
2068  */
2069 void
2070 dasd_write_eer_trigger(unsigned int id, struct dasd_device *device,
2071 		       struct dasd_ccw_req *cqr)
2072 {
2073 	if (device->eer) {
2074 		struct dasd_eer_trigger temp;
2075 		temp.id = id;
2076 		temp.device = device;
2077 		temp.cqr = cqr;
2078 		notifier_call_chain(&dasd_eer_chain, DASD_EER_TRIGGER,
2079 				    (void *)&temp);
2080 	}
2081 }
2082 
2083 /*
2084  * Tell the registered error reporting module to disable error reporting for
2085  * a given device and to cleanup any private data structures on that device.
2086  */
2087 static void
2088 dasd_disable_eer(struct dasd_device *device)
2089 {
2090 	notifier_call_chain(&dasd_eer_chain, DASD_EER_DISABLE, (void *)device);
2091 }
2092 
2093 
2094 static int __init
2095 dasd_init(void)
2096 {
2097 	int rc;
2098 
2099 	init_waitqueue_head(&dasd_init_waitq);
2100 
2101 	/* register 'common' DASD debug area, used for all DBF_XXX calls */
2102 	dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
2103 	if (dasd_debug_area == NULL) {
2104 		rc = -ENOMEM;
2105 		goto failed;
2106 	}
2107 	debug_register_view(dasd_debug_area, &debug_sprintf_view);
2108 	debug_set_level(dasd_debug_area, DBF_EMERG);
2109 
2110 	DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2111 
2112 	dasd_diag_discipline_pointer = NULL;
2113 
2114 	rc = devfs_mk_dir("dasd");
2115 	if (rc)
2116 		goto failed;
2117 	rc = dasd_devmap_init();
2118 	if (rc)
2119 		goto failed;
2120 	rc = dasd_gendisk_init();
2121 	if (rc)
2122 		goto failed;
2123 	rc = dasd_parse();
2124 	if (rc)
2125 		goto failed;
2126 	rc = dasd_ioctl_init();
2127 	if (rc)
2128 		goto failed;
2129 #ifdef CONFIG_PROC_FS
2130 	rc = dasd_proc_init();
2131 	if (rc)
2132 		goto failed;
2133 #endif
2134 
2135 	return 0;
2136 failed:
2137 	MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2138 	dasd_exit();
2139 	return rc;
2140 }
2141 
2142 module_init(dasd_init);
2143 module_exit(dasd_exit);
2144 
2145 EXPORT_SYMBOL(dasd_debug_area);
2146 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2147 
2148 EXPORT_SYMBOL(dasd_add_request_head);
2149 EXPORT_SYMBOL(dasd_add_request_tail);
2150 EXPORT_SYMBOL(dasd_cancel_req);
2151 EXPORT_SYMBOL(dasd_clear_timer);
2152 EXPORT_SYMBOL(dasd_enable_device);
2153 EXPORT_SYMBOL(dasd_int_handler);
2154 EXPORT_SYMBOL(dasd_kfree_request);
2155 EXPORT_SYMBOL(dasd_kick_device);
2156 EXPORT_SYMBOL(dasd_kmalloc_request);
2157 EXPORT_SYMBOL(dasd_schedule_bh);
2158 EXPORT_SYMBOL(dasd_set_target_state);
2159 EXPORT_SYMBOL(dasd_set_timer);
2160 EXPORT_SYMBOL(dasd_sfree_request);
2161 EXPORT_SYMBOL(dasd_sleep_on);
2162 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2163 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2164 EXPORT_SYMBOL(dasd_smalloc_request);
2165 EXPORT_SYMBOL(dasd_start_IO);
2166 EXPORT_SYMBOL(dasd_term_IO);
2167 
2168 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2169 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2170 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2171 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2172 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2173 EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
2174 
2175 EXPORT_SYMBOL(dasd_register_eer_notifier);
2176 EXPORT_SYMBOL(dasd_unregister_eer_notifier);
2177 EXPORT_SYMBOL(dasd_write_eer_trigger);
2178 
2179 
2180 /*
2181  * Overrides for Emacs so that we follow Linus's tabbing style.
2182  * Emacs will notice this stuff at the end of the file and automatically
2183  * adjust the settings for this buffer only.  This must remain at the end
2184  * of the file.
2185  * ---------------------------------------------------------------------------
2186  * Local variables:
2187  * c-indent-level: 4
2188  * c-brace-imaginary-offset: 0
2189  * c-brace-offset: -4
2190  * c-argdecl-indent: 4
2191  * c-label-offset: -4
2192  * c-continued-statement-offset: 4
2193  * c-continued-brace-offset: 0
2194  * indent-tabs-mode: 1
2195  * tab-width: 8
2196  * End:
2197  */
2198