[PATCH] s390: dasd wait for clear i/o interrupt
[linux-2.6.git] / drivers / s390 / block / dasd.c
blobabdf1ee633e7ecf3dced11ca4850237e3b2fc807
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
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>
22 #include <asm/ccwdev.h>
23 #include <asm/ebcdic.h>
24 #include <asm/idals.h>
25 #include <asm/todclk.h>
27 /* This is ugly... */
28 #define PRINTK_HEADER "dasd:"
30 #include "dasd_int.h"
32 * SECTION: Constant definitions to be used within this file
34 #define DASD_CHANQ_MAX_SIZE 4
37 * SECTION: exported variables of dasd.c
39 debug_info_t *dasd_debug_area;
40 struct dasd_discipline *dasd_diag_discipline_pointer;
42 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
43 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
44 " Copyright 2000 IBM Corporation");
45 MODULE_SUPPORTED_DEVICE("dasd");
46 MODULE_PARM(dasd, "1-" __MODULE_STRING(256) "s");
47 MODULE_LICENSE("GPL");
50 * SECTION: prototypes for static functions of dasd.c
52 static int dasd_alloc_queue(struct dasd_device * device);
53 static void dasd_setup_queue(struct dasd_device * device);
54 static void dasd_free_queue(struct dasd_device * device);
55 static void dasd_flush_request_queue(struct dasd_device *);
56 static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
57 static void dasd_flush_ccw_queue(struct dasd_device *, int);
58 static void dasd_tasklet(struct dasd_device *);
59 static void do_kick_device(void *data);
62 * SECTION: Operations on the device structure.
64 static wait_queue_head_t dasd_init_waitq;
67 * Allocate memory for a new device structure.
69 struct dasd_device *
70 dasd_alloc_device(void)
72 struct dasd_device *device;
74 device = kmalloc(sizeof (struct dasd_device), GFP_ATOMIC);
75 if (device == NULL)
76 return ERR_PTR(-ENOMEM);
77 memset(device, 0, sizeof (struct dasd_device));
78 /* open_count = 0 means device online but not in use */
79 atomic_set(&device->open_count, -1);
81 /* Get two pages for normal block device operations. */
82 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
83 if (device->ccw_mem == NULL) {
84 kfree(device);
85 return ERR_PTR(-ENOMEM);
87 /* Get one page for error recovery. */
88 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
89 if (device->erp_mem == NULL) {
90 free_pages((unsigned long) device->ccw_mem, 1);
91 kfree(device);
92 return ERR_PTR(-ENOMEM);
95 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
96 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
97 spin_lock_init(&device->mem_lock);
98 spin_lock_init(&device->request_queue_lock);
99 atomic_set (&device->tasklet_scheduled, 0);
100 tasklet_init(&device->tasklet,
101 (void (*)(unsigned long)) dasd_tasklet,
102 (unsigned long) device);
103 INIT_LIST_HEAD(&device->ccw_queue);
104 init_timer(&device->timer);
105 INIT_WORK(&device->kick_work, do_kick_device, device);
106 device->state = DASD_STATE_NEW;
107 device->target = DASD_STATE_NEW;
109 return device;
113 * Free memory of a device structure.
115 void
116 dasd_free_device(struct dasd_device *device)
118 kfree(device->private);
119 free_page((unsigned long) device->erp_mem);
120 free_pages((unsigned long) device->ccw_mem, 1);
121 kfree(device);
125 * Make a new device known to the system.
127 static inline int
128 dasd_state_new_to_known(struct dasd_device *device)
130 int rc;
133 * As long as the device is not in state DASD_STATE_NEW we want to
134 * keep the reference count > 0.
136 dasd_get_device(device);
138 rc = dasd_alloc_queue(device);
139 if (rc) {
140 dasd_put_device(device);
141 return rc;
144 device->state = DASD_STATE_KNOWN;
145 return 0;
149 * Let the system forget about a device.
151 static inline void
152 dasd_state_known_to_new(struct dasd_device * device)
154 /* Forget the discipline information. */
155 device->discipline = NULL;
156 device->state = DASD_STATE_NEW;
158 dasd_free_queue(device);
160 /* Give up reference we took in dasd_state_new_to_known. */
161 dasd_put_device(device);
165 * Request the irq line for the device.
167 static inline int
168 dasd_state_known_to_basic(struct dasd_device * device)
170 int rc;
172 /* Allocate and register gendisk structure. */
173 rc = dasd_gendisk_alloc(device);
174 if (rc)
175 return rc;
177 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
178 device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
179 8 * sizeof (long));
180 debug_register_view(device->debug_area, &debug_sprintf_view);
181 debug_set_level(device->debug_area, DBF_EMERG);
182 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
184 device->state = DASD_STATE_BASIC;
185 return 0;
189 * Release the irq line for the device. Terminate any running i/o.
191 static inline void
192 dasd_state_basic_to_known(struct dasd_device * device)
194 dasd_gendisk_free(device);
195 dasd_flush_ccw_queue(device, 1);
196 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
197 if (device->debug_area != NULL) {
198 debug_unregister(device->debug_area);
199 device->debug_area = NULL;
201 device->state = DASD_STATE_KNOWN;
205 * Do the initial analysis. The do_analysis function may return
206 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
207 * until the discipline decides to continue the startup sequence
208 * by calling the function dasd_change_state. The eckd disciplines
209 * uses this to start a ccw that detects the format. The completion
210 * interrupt for this detection ccw uses the kernel event daemon to
211 * trigger the call to dasd_change_state. All this is done in the
212 * discipline code, see dasd_eckd.c.
213 * After the analysis ccw is done (do_analysis returned 0 or error)
214 * the block device is setup. Either a fake disk is added to allow
215 * formatting or a proper device request queue is created.
217 static inline int
218 dasd_state_basic_to_ready(struct dasd_device * device)
220 int rc;
222 rc = 0;
223 if (device->discipline->do_analysis != NULL)
224 rc = device->discipline->do_analysis(device);
225 if (rc)
226 return rc;
227 dasd_setup_queue(device);
228 device->state = DASD_STATE_READY;
229 if (dasd_scan_partitions(device) != 0)
230 device->state = DASD_STATE_BASIC;
231 return 0;
235 * Remove device from block device layer. Destroy dirty buffers.
236 * Forget format information. Check if the target level is basic
237 * and if it is create fake disk for formatting.
239 static inline void
240 dasd_state_ready_to_basic(struct dasd_device * device)
242 dasd_flush_ccw_queue(device, 0);
243 dasd_destroy_partitions(device);
244 dasd_flush_request_queue(device);
245 device->blocks = 0;
246 device->bp_block = 0;
247 device->s2b_shift = 0;
248 device->state = DASD_STATE_BASIC;
252 * Make the device online and schedule the bottom half to start
253 * the requeueing of requests from the linux request queue to the
254 * ccw queue.
256 static inline int
257 dasd_state_ready_to_online(struct dasd_device * device)
259 device->state = DASD_STATE_ONLINE;
260 dasd_schedule_bh(device);
261 return 0;
265 * Stop the requeueing of requests again.
267 static inline void
268 dasd_state_online_to_ready(struct dasd_device * device)
270 device->state = DASD_STATE_READY;
274 * Device startup state changes.
276 static inline int
277 dasd_increase_state(struct dasd_device *device)
279 int rc;
281 rc = 0;
282 if (device->state == DASD_STATE_NEW &&
283 device->target >= DASD_STATE_KNOWN)
284 rc = dasd_state_new_to_known(device);
286 if (!rc &&
287 device->state == DASD_STATE_KNOWN &&
288 device->target >= DASD_STATE_BASIC)
289 rc = dasd_state_known_to_basic(device);
291 if (!rc &&
292 device->state == DASD_STATE_BASIC &&
293 device->target >= DASD_STATE_READY)
294 rc = dasd_state_basic_to_ready(device);
296 if (!rc &&
297 device->state == DASD_STATE_READY &&
298 device->target >= DASD_STATE_ONLINE)
299 rc = dasd_state_ready_to_online(device);
301 return rc;
305 * Device shutdown state changes.
307 static inline int
308 dasd_decrease_state(struct dasd_device *device)
310 if (device->state == DASD_STATE_ONLINE &&
311 device->target <= DASD_STATE_READY)
312 dasd_state_online_to_ready(device);
314 if (device->state == DASD_STATE_READY &&
315 device->target <= DASD_STATE_BASIC)
316 dasd_state_ready_to_basic(device);
318 if (device->state == DASD_STATE_BASIC &&
319 device->target <= DASD_STATE_KNOWN)
320 dasd_state_basic_to_known(device);
322 if (device->state == DASD_STATE_KNOWN &&
323 device->target <= DASD_STATE_NEW)
324 dasd_state_known_to_new(device);
326 return 0;
330 * This is the main startup/shutdown routine.
332 static void
333 dasd_change_state(struct dasd_device *device)
335 int rc;
337 if (device->state == device->target)
338 /* Already where we want to go today... */
339 return;
340 if (device->state < device->target)
341 rc = dasd_increase_state(device);
342 else
343 rc = dasd_decrease_state(device);
344 if (rc && rc != -EAGAIN)
345 device->target = device->state;
347 if (device->state == device->target)
348 wake_up(&dasd_init_waitq);
352 * Kick starter for devices that did not complete the startup/shutdown
353 * procedure or were sleeping because of a pending state.
354 * dasd_kick_device will schedule a call do do_kick_device to the kernel
355 * event daemon.
357 static void
358 do_kick_device(void *data)
360 struct dasd_device *device;
362 device = (struct dasd_device *) data;
363 dasd_change_state(device);
364 dasd_schedule_bh(device);
365 dasd_put_device(device);
368 void
369 dasd_kick_device(struct dasd_device *device)
371 dasd_get_device(device);
372 /* queue call to dasd_kick_device to the kernel event daemon. */
373 schedule_work(&device->kick_work);
377 * Set the target state for a device and starts the state change.
379 void
380 dasd_set_target_state(struct dasd_device *device, int target)
382 /* If we are in probeonly mode stop at DASD_STATE_READY. */
383 if (dasd_probeonly && target > DASD_STATE_READY)
384 target = DASD_STATE_READY;
385 if (device->target != target) {
386 if (device->state == target)
387 wake_up(&dasd_init_waitq);
388 device->target = target;
390 if (device->state != device->target)
391 dasd_change_state(device);
395 * Enable devices with device numbers in [from..to].
397 static inline int
398 _wait_for_device(struct dasd_device *device)
400 return (device->state == device->target);
403 void
404 dasd_enable_device(struct dasd_device *device)
406 dasd_set_target_state(device, DASD_STATE_ONLINE);
407 if (device->state <= DASD_STATE_KNOWN)
408 /* No discipline for device found. */
409 dasd_set_target_state(device, DASD_STATE_NEW);
410 /* Now wait for the devices to come up. */
411 wait_event(dasd_init_waitq, _wait_for_device(device));
415 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
417 #ifdef CONFIG_DASD_PROFILE
419 struct dasd_profile_info_t dasd_global_profile;
420 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
423 * Increments counter in global and local profiling structures.
425 #define dasd_profile_counter(value, counter, device) \
427 int index; \
428 for (index = 0; index < 31 && value >> (2+index); index++); \
429 dasd_global_profile.counter[index]++; \
430 device->profile.counter[index]++; \
434 * Add profiling information for cqr before execution.
436 static inline void
437 dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
438 struct request *req)
440 struct list_head *l;
441 unsigned int counter;
443 if (dasd_profile_level != DASD_PROFILE_ON)
444 return;
446 /* count the length of the chanq for statistics */
447 counter = 0;
448 list_for_each(l, &device->ccw_queue)
449 if (++counter >= 31)
450 break;
451 dasd_global_profile.dasd_io_nr_req[counter]++;
452 device->profile.dasd_io_nr_req[counter]++;
456 * Add profiling information for cqr after execution.
458 static inline void
459 dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
460 struct request *req)
462 long strtime, irqtime, endtime, tottime; /* in microseconds */
463 long tottimeps, sectors;
465 if (dasd_profile_level != DASD_PROFILE_ON)
466 return;
468 sectors = req->nr_sectors;
469 if (!cqr->buildclk || !cqr->startclk ||
470 !cqr->stopclk || !cqr->endclk ||
471 !sectors)
472 return;
474 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
475 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
476 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
477 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
478 tottimeps = tottime / sectors;
480 if (!dasd_global_profile.dasd_io_reqs)
481 memset(&dasd_global_profile, 0,
482 sizeof (struct dasd_profile_info_t));
483 dasd_global_profile.dasd_io_reqs++;
484 dasd_global_profile.dasd_io_sects += sectors;
486 if (!device->profile.dasd_io_reqs)
487 memset(&device->profile, 0,
488 sizeof (struct dasd_profile_info_t));
489 device->profile.dasd_io_reqs++;
490 device->profile.dasd_io_sects += sectors;
492 dasd_profile_counter(sectors, dasd_io_secs, device);
493 dasd_profile_counter(tottime, dasd_io_times, device);
494 dasd_profile_counter(tottimeps, dasd_io_timps, device);
495 dasd_profile_counter(strtime, dasd_io_time1, device);
496 dasd_profile_counter(irqtime, dasd_io_time2, device);
497 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
498 dasd_profile_counter(endtime, dasd_io_time3, device);
500 #else
501 #define dasd_profile_start(device, cqr, req) do {} while (0)
502 #define dasd_profile_end(device, cqr, req) do {} while (0)
503 #endif /* CONFIG_DASD_PROFILE */
506 * Allocate memory for a channel program with 'cplength' channel
507 * command words and 'datasize' additional space. There are two
508 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
509 * memory and 2) dasd_smalloc_request uses the static ccw memory
510 * that gets allocated for each device.
512 struct dasd_ccw_req *
513 dasd_kmalloc_request(char *magic, int cplength, int datasize,
514 struct dasd_device * device)
516 struct dasd_ccw_req *cqr;
518 /* Sanity checks */
519 if ( magic == NULL || datasize > PAGE_SIZE ||
520 (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
521 BUG();
523 cqr = kmalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
524 if (cqr == NULL)
525 return ERR_PTR(-ENOMEM);
526 memset(cqr, 0, sizeof(struct dasd_ccw_req));
527 cqr->cpaddr = NULL;
528 if (cplength > 0) {
529 cqr->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
530 GFP_ATOMIC | GFP_DMA);
531 if (cqr->cpaddr == NULL) {
532 kfree(cqr);
533 return ERR_PTR(-ENOMEM);
535 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
537 cqr->data = NULL;
538 if (datasize > 0) {
539 cqr->data = kmalloc(datasize, GFP_ATOMIC | GFP_DMA);
540 if (cqr->data == NULL) {
541 kfree(cqr->cpaddr);
542 kfree(cqr);
543 return ERR_PTR(-ENOMEM);
545 memset(cqr->data, 0, datasize);
547 strncpy((char *) &cqr->magic, magic, 4);
548 ASCEBC((char *) &cqr->magic, 4);
549 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
550 dasd_get_device(device);
551 return cqr;
554 struct dasd_ccw_req *
555 dasd_smalloc_request(char *magic, int cplength, int datasize,
556 struct dasd_device * device)
558 unsigned long flags;
559 struct dasd_ccw_req *cqr;
560 char *data;
561 int size;
563 /* Sanity checks */
564 if ( magic == NULL || datasize > PAGE_SIZE ||
565 (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
566 BUG();
568 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
569 if (cplength > 0)
570 size += cplength * sizeof(struct ccw1);
571 if (datasize > 0)
572 size += datasize;
573 spin_lock_irqsave(&device->mem_lock, flags);
574 cqr = (struct dasd_ccw_req *)
575 dasd_alloc_chunk(&device->ccw_chunks, size);
576 spin_unlock_irqrestore(&device->mem_lock, flags);
577 if (cqr == NULL)
578 return ERR_PTR(-ENOMEM);
579 memset(cqr, 0, sizeof(struct dasd_ccw_req));
580 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
581 cqr->cpaddr = NULL;
582 if (cplength > 0) {
583 cqr->cpaddr = (struct ccw1 *) data;
584 data += cplength*sizeof(struct ccw1);
585 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
587 cqr->data = NULL;
588 if (datasize > 0) {
589 cqr->data = data;
590 memset(cqr->data, 0, datasize);
592 strncpy((char *) &cqr->magic, magic, 4);
593 ASCEBC((char *) &cqr->magic, 4);
594 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
595 dasd_get_device(device);
596 return cqr;
600 * Free memory of a channel program. This function needs to free all the
601 * idal lists that might have been created by dasd_set_cda and the
602 * struct dasd_ccw_req itself.
604 void
605 dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
607 #ifdef CONFIG_64BIT
608 struct ccw1 *ccw;
610 /* Clear any idals used for the request. */
611 ccw = cqr->cpaddr;
612 do {
613 clear_normalized_cda(ccw);
614 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
615 #endif
616 kfree(cqr->cpaddr);
617 kfree(cqr->data);
618 kfree(cqr);
619 dasd_put_device(device);
622 void
623 dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
625 unsigned long flags;
627 spin_lock_irqsave(&device->mem_lock, flags);
628 dasd_free_chunk(&device->ccw_chunks, cqr);
629 spin_unlock_irqrestore(&device->mem_lock, flags);
630 dasd_put_device(device);
634 * Check discipline magic in cqr.
636 static inline int
637 dasd_check_cqr(struct dasd_ccw_req *cqr)
639 struct dasd_device *device;
641 if (cqr == NULL)
642 return -EINVAL;
643 device = cqr->device;
644 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
645 DEV_MESSAGE(KERN_WARNING, device,
646 " dasd_ccw_req 0x%08x magic doesn't match"
647 " discipline 0x%08x",
648 cqr->magic,
649 *(unsigned int *) device->discipline->name);
650 return -EINVAL;
652 return 0;
656 * Terminate the current i/o and set the request to clear_pending.
657 * Timer keeps device runnig.
658 * ccw_device_clear can fail if the i/o subsystem
659 * is in a bad mood.
662 dasd_term_IO(struct dasd_ccw_req * cqr)
664 struct dasd_device *device;
665 int retries, rc;
667 /* Check the cqr */
668 rc = dasd_check_cqr(cqr);
669 if (rc)
670 return rc;
671 retries = 0;
672 device = (struct dasd_device *) cqr->device;
673 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
674 rc = ccw_device_clear(device->cdev, (long) cqr);
675 switch (rc) {
676 case 0: /* termination successful */
677 cqr->retries--;
678 cqr->status = DASD_CQR_CLEAR;
679 cqr->stopclk = get_clock();
680 DBF_DEV_EVENT(DBF_DEBUG, device,
681 "terminate cqr %p successful",
682 cqr);
683 break;
684 case -ENODEV:
685 DBF_DEV_EVENT(DBF_ERR, device, "%s",
686 "device gone, retry");
687 break;
688 case -EIO:
689 DBF_DEV_EVENT(DBF_ERR, device, "%s",
690 "I/O error, retry");
691 break;
692 case -EINVAL:
693 case -EBUSY:
694 DBF_DEV_EVENT(DBF_ERR, device, "%s",
695 "device busy, retry later");
696 break;
697 default:
698 DEV_MESSAGE(KERN_ERR, device,
699 "line %d unknown RC=%d, please "
700 "report to linux390@de.ibm.com",
701 __LINE__, rc);
702 BUG();
703 break;
705 retries++;
707 dasd_schedule_bh(device);
708 return rc;
712 * Start the i/o. This start_IO can fail if the channel is really busy.
713 * In that case set up a timer to start the request later.
716 dasd_start_IO(struct dasd_ccw_req * cqr)
718 struct dasd_device *device;
719 int rc;
721 /* Check the cqr */
722 rc = dasd_check_cqr(cqr);
723 if (rc)
724 return rc;
725 device = (struct dasd_device *) cqr->device;
726 if (cqr->retries < 0) {
727 DEV_MESSAGE(KERN_DEBUG, device,
728 "start_IO: request %p (%02x/%i) - no retry left.",
729 cqr, cqr->status, cqr->retries);
730 cqr->status = DASD_CQR_FAILED;
731 return -EIO;
733 cqr->startclk = get_clock();
734 cqr->starttime = jiffies;
735 cqr->retries--;
736 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
737 cqr->lpm, 0);
738 switch (rc) {
739 case 0:
740 cqr->status = DASD_CQR_IN_IO;
741 DBF_DEV_EVENT(DBF_DEBUG, device,
742 "start_IO: request %p started successful",
743 cqr);
744 break;
745 case -EBUSY:
746 DBF_DEV_EVENT(DBF_ERR, device, "%s",
747 "start_IO: device busy, retry later");
748 break;
749 case -ETIMEDOUT:
750 DBF_DEV_EVENT(DBF_ERR, device, "%s",
751 "start_IO: request timeout, retry later");
752 break;
753 case -EACCES:
754 /* -EACCES indicates that the request used only a
755 * subset of the available pathes and all these
756 * pathes are gone.
757 * Do a retry with all available pathes.
759 cqr->lpm = LPM_ANYPATH;
760 DBF_DEV_EVENT(DBF_ERR, device, "%s",
761 "start_IO: selected pathes gone,"
762 " retry on all pathes");
763 break;
764 case -ENODEV:
765 case -EIO:
766 DBF_DEV_EVENT(DBF_ERR, device, "%s",
767 "start_IO: device gone, retry");
768 break;
769 default:
770 DEV_MESSAGE(KERN_ERR, device,
771 "line %d unknown RC=%d, please report"
772 " to linux390@de.ibm.com", __LINE__, rc);
773 BUG();
774 break;
776 return rc;
780 * Timeout function for dasd devices. This is used for different purposes
781 * 1) missing interrupt handler for normal operation
782 * 2) delayed start of request where start_IO failed with -EBUSY
783 * 3) timeout for missing state change interrupts
784 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
785 * DASD_CQR_QUEUED for 2) and 3).
787 static void
788 dasd_timeout_device(unsigned long ptr)
790 unsigned long flags;
791 struct dasd_device *device;
793 device = (struct dasd_device *) ptr;
794 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
795 /* re-activate request queue */
796 device->stopped &= ~DASD_STOPPED_PENDING;
797 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
798 dasd_schedule_bh(device);
802 * Setup timeout for a device in jiffies.
804 void
805 dasd_set_timer(struct dasd_device *device, int expires)
807 if (expires == 0) {
808 if (timer_pending(&device->timer))
809 del_timer(&device->timer);
810 return;
812 if (timer_pending(&device->timer)) {
813 if (mod_timer(&device->timer, jiffies + expires))
814 return;
816 device->timer.function = dasd_timeout_device;
817 device->timer.data = (unsigned long) device;
818 device->timer.expires = jiffies + expires;
819 add_timer(&device->timer);
823 * Clear timeout for a device.
825 void
826 dasd_clear_timer(struct dasd_device *device)
828 if (timer_pending(&device->timer))
829 del_timer(&device->timer);
832 static void
833 dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
835 struct dasd_ccw_req *cqr;
836 struct dasd_device *device;
838 cqr = (struct dasd_ccw_req *) intparm;
839 if (cqr->status != DASD_CQR_IN_IO) {
840 MESSAGE(KERN_DEBUG,
841 "invalid status in handle_killed_request: "
842 "bus_id %s, status %02x",
843 cdev->dev.bus_id, cqr->status);
844 return;
847 device = (struct dasd_device *) cqr->device;
848 if (device == NULL ||
849 device != dasd_device_from_cdev(cdev) ||
850 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
851 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
852 cdev->dev.bus_id);
853 return;
856 /* Schedule request to be retried. */
857 cqr->status = DASD_CQR_QUEUED;
859 dasd_clear_timer(device);
860 dasd_schedule_bh(device);
861 dasd_put_device(device);
864 static void
865 dasd_handle_state_change_pending(struct dasd_device *device)
867 struct dasd_ccw_req *cqr;
868 struct list_head *l, *n;
870 device->stopped &= ~DASD_STOPPED_PENDING;
872 /* restart all 'running' IO on queue */
873 list_for_each_safe(l, n, &device->ccw_queue) {
874 cqr = list_entry(l, struct dasd_ccw_req, list);
875 if (cqr->status == DASD_CQR_IN_IO) {
876 cqr->status = DASD_CQR_QUEUED;
879 dasd_clear_timer(device);
880 dasd_schedule_bh(device);
884 * Interrupt handler for "normal" ssch-io based dasd devices.
886 void
887 dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
888 struct irb *irb)
890 struct dasd_ccw_req *cqr, *next;
891 struct dasd_device *device;
892 unsigned long long now;
893 int expires;
894 dasd_era_t era;
895 char mask;
897 if (IS_ERR(irb)) {
898 switch (PTR_ERR(irb)) {
899 case -EIO:
900 dasd_handle_killed_request(cdev, intparm);
901 break;
902 case -ETIMEDOUT:
903 printk(KERN_WARNING"%s(%s): request timed out\n",
904 __FUNCTION__, cdev->dev.bus_id);
905 //FIXME - dasd uses own timeout interface...
906 break;
907 default:
908 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
909 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
911 return;
914 now = get_clock();
916 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
917 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
918 (unsigned int) intparm);
920 /* first of all check for state change pending interrupt */
921 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
922 if ((irb->scsw.dstat & mask) == mask) {
923 device = dasd_device_from_cdev(cdev);
924 if (!IS_ERR(device)) {
925 dasd_handle_state_change_pending(device);
926 dasd_put_device(device);
928 return;
931 cqr = (struct dasd_ccw_req *) intparm;
933 /* check for unsolicited interrupts */
934 if (cqr == NULL) {
935 MESSAGE(KERN_DEBUG,
936 "unsolicited interrupt received: bus_id %s",
937 cdev->dev.bus_id);
938 return;
941 device = (struct dasd_device *) cqr->device;
942 if (device == NULL ||
943 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
944 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
945 cdev->dev.bus_id);
946 return;
949 /* Check for clear pending */
950 if (cqr->status == DASD_CQR_CLEAR &&
951 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
952 cqr->status = DASD_CQR_QUEUED;
953 dasd_clear_timer(device);
954 dasd_schedule_bh(device);
955 return;
958 /* check status - the request might have been killed by dyn detach */
959 if (cqr->status != DASD_CQR_IN_IO) {
960 MESSAGE(KERN_DEBUG,
961 "invalid status: bus_id %s, status %02x",
962 cdev->dev.bus_id, cqr->status);
963 return;
965 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
966 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
968 /* Find out the appropriate era_action. */
969 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
970 era = dasd_era_fatal;
971 else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
972 irb->scsw.cstat == 0 &&
973 !irb->esw.esw0.erw.cons)
974 era = dasd_era_none;
975 else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
976 era = dasd_era_fatal; /* don't recover this request */
977 else if (irb->esw.esw0.erw.cons)
978 era = device->discipline->examine_error(cqr, irb);
979 else
980 era = dasd_era_recover;
982 DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
983 expires = 0;
984 if (era == dasd_era_none) {
985 cqr->status = DASD_CQR_DONE;
986 cqr->stopclk = now;
987 /* Start first request on queue if possible -> fast_io. */
988 if (cqr->list.next != &device->ccw_queue) {
989 next = list_entry(cqr->list.next,
990 struct dasd_ccw_req, list);
991 if ((next->status == DASD_CQR_QUEUED) &&
992 (!device->stopped)) {
993 if (device->discipline->start_IO(next) == 0)
994 expires = next->expires;
995 else
996 DEV_MESSAGE(KERN_DEBUG, device, "%s",
997 "Interrupt fastpath "
998 "failed!");
1001 } else { /* error */
1002 memcpy(&cqr->irb, irb, sizeof (struct irb));
1003 #ifdef ERP_DEBUG
1004 /* dump sense data */
1005 dasd_log_sense(cqr, irb);
1006 #endif
1007 switch (era) {
1008 case dasd_era_fatal:
1009 cqr->status = DASD_CQR_FAILED;
1010 cqr->stopclk = now;
1011 break;
1012 case dasd_era_recover:
1013 cqr->status = DASD_CQR_ERROR;
1014 break;
1015 default:
1016 BUG();
1019 if (expires != 0)
1020 dasd_set_timer(device, expires);
1021 else
1022 dasd_clear_timer(device);
1023 dasd_schedule_bh(device);
1027 * posts the buffer_cache about a finalized request
1029 static inline void
1030 dasd_end_request(struct request *req, int uptodate)
1032 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1033 BUG();
1034 add_disk_randomness(req->rq_disk);
1035 end_that_request_last(req, uptodate);
1039 * Process finished error recovery ccw.
1041 static inline void
1042 __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1044 dasd_erp_fn_t erp_fn;
1046 if (cqr->status == DASD_CQR_DONE)
1047 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1048 else
1049 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1050 erp_fn = device->discipline->erp_postaction(cqr);
1051 erp_fn(cqr);
1055 * Process ccw request queue.
1057 static inline void
1058 __dasd_process_ccw_queue(struct dasd_device * device,
1059 struct list_head *final_queue)
1061 struct list_head *l, *n;
1062 struct dasd_ccw_req *cqr;
1063 dasd_erp_fn_t erp_fn;
1065 restart:
1066 /* Process request with final status. */
1067 list_for_each_safe(l, n, &device->ccw_queue) {
1068 cqr = list_entry(l, struct dasd_ccw_req, list);
1069 /* Stop list processing at the first non-final request. */
1070 if (cqr->status != DASD_CQR_DONE &&
1071 cqr->status != DASD_CQR_FAILED &&
1072 cqr->status != DASD_CQR_ERROR)
1073 break;
1074 /* Process requests with DASD_CQR_ERROR */
1075 if (cqr->status == DASD_CQR_ERROR) {
1076 if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1077 cqr->status = DASD_CQR_FAILED;
1078 cqr->stopclk = get_clock();
1079 } else {
1080 if (cqr->irb.esw.esw0.erw.cons) {
1081 erp_fn = device->discipline->
1082 erp_action(cqr);
1083 erp_fn(cqr);
1084 } else
1085 dasd_default_erp_action(cqr);
1087 goto restart;
1089 /* Process finished ERP request. */
1090 if (cqr->refers) {
1091 __dasd_process_erp(device, cqr);
1092 goto restart;
1095 /* Rechain finished requests to final queue */
1096 cqr->endclk = get_clock();
1097 list_move_tail(&cqr->list, final_queue);
1101 static void
1102 dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1104 struct request *req;
1105 struct dasd_device *device;
1106 int status;
1108 req = (struct request *) data;
1109 device = cqr->device;
1110 dasd_profile_end(device, cqr, req);
1111 status = cqr->device->discipline->free_cp(cqr,req);
1112 spin_lock_irq(&device->request_queue_lock);
1113 dasd_end_request(req, status);
1114 spin_unlock_irq(&device->request_queue_lock);
1119 * Fetch requests from the block device queue.
1121 static inline void
1122 __dasd_process_blk_queue(struct dasd_device * device)
1124 request_queue_t *queue;
1125 struct request *req;
1126 struct dasd_ccw_req *cqr;
1127 int nr_queued;
1129 queue = device->request_queue;
1130 /* No queue ? Then there is nothing to do. */
1131 if (queue == NULL)
1132 return;
1135 * We requeue request from the block device queue to the ccw
1136 * queue only in two states. In state DASD_STATE_READY the
1137 * partition detection is done and we need to requeue requests
1138 * for that. State DASD_STATE_ONLINE is normal block device
1139 * operation.
1141 if (device->state != DASD_STATE_READY &&
1142 device->state != DASD_STATE_ONLINE)
1143 return;
1144 nr_queued = 0;
1145 /* Now we try to fetch requests from the request queue */
1146 list_for_each_entry(cqr, &device->ccw_queue, list)
1147 if (cqr->status == DASD_CQR_QUEUED)
1148 nr_queued++;
1149 while (!blk_queue_plugged(queue) &&
1150 elv_next_request(queue) &&
1151 nr_queued < DASD_CHANQ_MAX_SIZE) {
1152 req = elv_next_request(queue);
1154 if (device->features & DASD_FEATURE_READONLY &&
1155 rq_data_dir(req) == WRITE) {
1156 DBF_DEV_EVENT(DBF_ERR, device,
1157 "Rejecting write request %p",
1158 req);
1159 blkdev_dequeue_request(req);
1160 dasd_end_request(req, 0);
1161 continue;
1163 if (device->stopped & DASD_STOPPED_DC_EIO) {
1164 blkdev_dequeue_request(req);
1165 dasd_end_request(req, 0);
1166 continue;
1168 cqr = device->discipline->build_cp(device, req);
1169 if (IS_ERR(cqr)) {
1170 if (PTR_ERR(cqr) == -ENOMEM)
1171 break; /* terminate request queue loop */
1172 DBF_DEV_EVENT(DBF_ERR, device,
1173 "CCW creation failed (rc=%ld) "
1174 "on request %p",
1175 PTR_ERR(cqr), req);
1176 blkdev_dequeue_request(req);
1177 dasd_end_request(req, 0);
1178 continue;
1180 cqr->callback = dasd_end_request_cb;
1181 cqr->callback_data = (void *) req;
1182 cqr->status = DASD_CQR_QUEUED;
1183 blkdev_dequeue_request(req);
1184 list_add_tail(&cqr->list, &device->ccw_queue);
1185 dasd_profile_start(device, cqr, req);
1186 nr_queued++;
1191 * Take a look at the first request on the ccw queue and check
1192 * if it reached its expire time. If so, terminate the IO.
1194 static inline void
1195 __dasd_check_expire(struct dasd_device * device)
1197 struct dasd_ccw_req *cqr;
1199 if (list_empty(&device->ccw_queue))
1200 return;
1201 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1202 if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1203 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1204 if (device->discipline->term_IO(cqr) != 0)
1205 /* Hmpf, try again in 1/10 sec */
1206 dasd_set_timer(device, 10);
1212 * Take a look at the first request on the ccw queue and check
1213 * if it needs to be started.
1215 static inline void
1216 __dasd_start_head(struct dasd_device * device)
1218 struct dasd_ccw_req *cqr;
1219 int rc;
1221 if (list_empty(&device->ccw_queue))
1222 return;
1223 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1224 /* check FAILFAST */
1225 if (device->stopped & ~DASD_STOPPED_PENDING &&
1226 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags)) {
1227 cqr->status = DASD_CQR_FAILED;
1228 dasd_schedule_bh(device);
1230 if ((cqr->status == DASD_CQR_QUEUED) &&
1231 (!device->stopped)) {
1232 /* try to start the first I/O that can be started */
1233 rc = device->discipline->start_IO(cqr);
1234 if (rc == 0)
1235 dasd_set_timer(device, cqr->expires);
1236 else if (rc == -EACCES) {
1237 dasd_schedule_bh(device);
1238 } else
1239 /* Hmpf, try again in 1/2 sec */
1240 dasd_set_timer(device, 50);
1245 * Remove requests from the ccw queue.
1247 static void
1248 dasd_flush_ccw_queue(struct dasd_device * device, int all)
1250 struct list_head flush_queue;
1251 struct list_head *l, *n;
1252 struct dasd_ccw_req *cqr;
1254 INIT_LIST_HEAD(&flush_queue);
1255 spin_lock_irq(get_ccwdev_lock(device->cdev));
1256 list_for_each_safe(l, n, &device->ccw_queue) {
1257 cqr = list_entry(l, struct dasd_ccw_req, list);
1258 /* Flush all request or only block device requests? */
1259 if (all == 0 && cqr->callback == dasd_end_request_cb)
1260 continue;
1261 if (cqr->status == DASD_CQR_IN_IO)
1262 device->discipline->term_IO(cqr);
1263 if (cqr->status != DASD_CQR_DONE ||
1264 cqr->status != DASD_CQR_FAILED) {
1265 cqr->status = DASD_CQR_FAILED;
1266 cqr->stopclk = get_clock();
1268 /* Process finished ERP request. */
1269 if (cqr->refers) {
1270 __dasd_process_erp(device, cqr);
1271 continue;
1273 /* Rechain request on device request queue */
1274 cqr->endclk = get_clock();
1275 list_move_tail(&cqr->list, &flush_queue);
1277 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1278 /* Now call the callback function of flushed requests */
1279 list_for_each_safe(l, n, &flush_queue) {
1280 cqr = list_entry(l, struct dasd_ccw_req, list);
1281 if (cqr->callback != NULL)
1282 (cqr->callback)(cqr, cqr->callback_data);
1287 * Acquire the device lock and process queues for the device.
1289 static void
1290 dasd_tasklet(struct dasd_device * device)
1292 struct list_head final_queue;
1293 struct list_head *l, *n;
1294 struct dasd_ccw_req *cqr;
1296 atomic_set (&device->tasklet_scheduled, 0);
1297 INIT_LIST_HEAD(&final_queue);
1298 spin_lock_irq(get_ccwdev_lock(device->cdev));
1299 /* Check expire time of first request on the ccw queue. */
1300 __dasd_check_expire(device);
1301 /* Finish off requests on ccw queue */
1302 __dasd_process_ccw_queue(device, &final_queue);
1303 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1304 /* Now call the callback function of requests with final status */
1305 list_for_each_safe(l, n, &final_queue) {
1306 cqr = list_entry(l, struct dasd_ccw_req, list);
1307 list_del_init(&cqr->list);
1308 if (cqr->callback != NULL)
1309 (cqr->callback)(cqr, cqr->callback_data);
1311 spin_lock_irq(&device->request_queue_lock);
1312 spin_lock(get_ccwdev_lock(device->cdev));
1313 /* Get new request from the block device request queue */
1314 __dasd_process_blk_queue(device);
1315 /* Now check if the head of the ccw queue needs to be started. */
1316 __dasd_start_head(device);
1317 spin_unlock(get_ccwdev_lock(device->cdev));
1318 spin_unlock_irq(&device->request_queue_lock);
1319 dasd_put_device(device);
1323 * Schedules a call to dasd_tasklet over the device tasklet.
1325 void
1326 dasd_schedule_bh(struct dasd_device * device)
1328 /* Protect against rescheduling. */
1329 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1330 return;
1331 dasd_get_device(device);
1332 tasklet_hi_schedule(&device->tasklet);
1336 * Queue a request to the head of the ccw_queue. Start the I/O if
1337 * possible.
1339 void
1340 dasd_add_request_head(struct dasd_ccw_req *req)
1342 struct dasd_device *device;
1343 unsigned long flags;
1345 device = req->device;
1346 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1347 req->status = DASD_CQR_QUEUED;
1348 req->device = device;
1349 list_add(&req->list, &device->ccw_queue);
1350 /* let the bh start the request to keep them in order */
1351 dasd_schedule_bh(device);
1352 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1356 * Queue a request to the tail of the ccw_queue. Start the I/O if
1357 * possible.
1359 void
1360 dasd_add_request_tail(struct dasd_ccw_req *req)
1362 struct dasd_device *device;
1363 unsigned long flags;
1365 device = req->device;
1366 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1367 req->status = DASD_CQR_QUEUED;
1368 req->device = device;
1369 list_add_tail(&req->list, &device->ccw_queue);
1370 /* let the bh start the request to keep them in order */
1371 dasd_schedule_bh(device);
1372 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1376 * Wakeup callback.
1378 static void
1379 dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1381 wake_up((wait_queue_head_t *) data);
1384 static inline int
1385 _wait_for_wakeup(struct dasd_ccw_req *cqr)
1387 struct dasd_device *device;
1388 int rc;
1390 device = cqr->device;
1391 spin_lock_irq(get_ccwdev_lock(device->cdev));
1392 rc = ((cqr->status == DASD_CQR_DONE ||
1393 cqr->status == DASD_CQR_FAILED) &&
1394 list_empty(&cqr->list));
1395 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1396 return rc;
1400 * Attempts to start a special ccw queue and waits for its completion.
1403 dasd_sleep_on(struct dasd_ccw_req * cqr)
1405 wait_queue_head_t wait_q;
1406 struct dasd_device *device;
1407 int rc;
1409 device = cqr->device;
1410 spin_lock_irq(get_ccwdev_lock(device->cdev));
1412 init_waitqueue_head (&wait_q);
1413 cqr->callback = dasd_wakeup_cb;
1414 cqr->callback_data = (void *) &wait_q;
1415 cqr->status = DASD_CQR_QUEUED;
1416 list_add_tail(&cqr->list, &device->ccw_queue);
1418 /* let the bh start the request to keep them in order */
1419 dasd_schedule_bh(device);
1421 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1423 wait_event(wait_q, _wait_for_wakeup(cqr));
1425 /* Request status is either done or failed. */
1426 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1427 return rc;
1431 * Attempts to start a special ccw queue and wait interruptible
1432 * for its completion.
1435 dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1437 wait_queue_head_t wait_q;
1438 struct dasd_device *device;
1439 int rc, finished;
1441 device = cqr->device;
1442 spin_lock_irq(get_ccwdev_lock(device->cdev));
1444 init_waitqueue_head (&wait_q);
1445 cqr->callback = dasd_wakeup_cb;
1446 cqr->callback_data = (void *) &wait_q;
1447 cqr->status = DASD_CQR_QUEUED;
1448 list_add_tail(&cqr->list, &device->ccw_queue);
1450 /* let the bh start the request to keep them in order */
1451 dasd_schedule_bh(device);
1452 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1454 finished = 0;
1455 while (!finished) {
1456 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1457 if (rc != -ERESTARTSYS) {
1458 /* Request is final (done or failed) */
1459 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1460 break;
1462 spin_lock_irq(get_ccwdev_lock(device->cdev));
1463 switch (cqr->status) {
1464 case DASD_CQR_IN_IO:
1465 /* terminate runnig cqr */
1466 if (device->discipline->term_IO) {
1467 cqr->retries = -1;
1468 device->discipline->term_IO(cqr);
1469 /*nished =
1470 * wait (non-interruptible) for final status
1471 * because signal ist still pending
1473 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1474 wait_event(wait_q, _wait_for_wakeup(cqr));
1475 spin_lock_irq(get_ccwdev_lock(device->cdev));
1476 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1477 finished = 1;
1479 break;
1480 case DASD_CQR_QUEUED:
1481 /* request */
1482 list_del_init(&cqr->list);
1483 rc = -EIO;
1484 finished = 1;
1485 break;
1486 default:
1487 /* cqr with 'non-interruptable' status - just wait */
1488 break;
1490 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1492 return rc;
1496 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1497 * for eckd devices) the currently running request has to be terminated
1498 * and be put back to status queued, before the special request is added
1499 * to the head of the queue. Then the special request is waited on normally.
1501 static inline int
1502 _dasd_term_running_cqr(struct dasd_device *device)
1504 struct dasd_ccw_req *cqr;
1505 int rc;
1507 if (list_empty(&device->ccw_queue))
1508 return 0;
1509 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1510 rc = device->discipline->term_IO(cqr);
1511 if (rc == 0) {
1512 /* termination successful */
1513 cqr->status = DASD_CQR_QUEUED;
1514 cqr->startclk = cqr->stopclk = 0;
1515 cqr->starttime = 0;
1517 return rc;
1521 dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1523 wait_queue_head_t wait_q;
1524 struct dasd_device *device;
1525 int rc;
1527 device = cqr->device;
1528 spin_lock_irq(get_ccwdev_lock(device->cdev));
1529 rc = _dasd_term_running_cqr(device);
1530 if (rc) {
1531 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1532 return rc;
1535 init_waitqueue_head (&wait_q);
1536 cqr->callback = dasd_wakeup_cb;
1537 cqr->callback_data = (void *) &wait_q;
1538 cqr->status = DASD_CQR_QUEUED;
1539 list_add(&cqr->list, &device->ccw_queue);
1541 /* let the bh start the request to keep them in order */
1542 dasd_schedule_bh(device);
1544 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1546 wait_event(wait_q, _wait_for_wakeup(cqr));
1548 /* Request status is either done or failed. */
1549 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1550 return rc;
1554 * Cancels a request that was started with dasd_sleep_on_req.
1555 * This is useful to timeout requests. The request will be
1556 * terminated if it is currently in i/o.
1557 * Returns 1 if the request has been terminated.
1560 dasd_cancel_req(struct dasd_ccw_req *cqr)
1562 struct dasd_device *device = cqr->device;
1563 unsigned long flags;
1564 int rc;
1566 rc = 0;
1567 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1568 switch (cqr->status) {
1569 case DASD_CQR_QUEUED:
1570 /* request was not started - just set to failed */
1571 cqr->status = DASD_CQR_FAILED;
1572 break;
1573 case DASD_CQR_IN_IO:
1574 /* request in IO - terminate IO and release again */
1575 if (device->discipline->term_IO(cqr) != 0)
1576 /* what to do if unable to terminate ??????
1577 e.g. not _IN_IO */
1578 cqr->status = DASD_CQR_FAILED;
1579 cqr->stopclk = get_clock();
1580 rc = 1;
1581 break;
1582 case DASD_CQR_DONE:
1583 case DASD_CQR_FAILED:
1584 /* already finished - do nothing */
1585 break;
1586 default:
1587 DEV_MESSAGE(KERN_ALERT, device,
1588 "invalid status %02x in request",
1589 cqr->status);
1590 BUG();
1593 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1594 dasd_schedule_bh(device);
1595 return rc;
1599 * SECTION: Block device operations (request queue, partitions, open, release).
1603 * Dasd request queue function. Called from ll_rw_blk.c
1605 static void
1606 do_dasd_request(request_queue_t * queue)
1608 struct dasd_device *device;
1610 device = (struct dasd_device *) queue->queuedata;
1611 spin_lock(get_ccwdev_lock(device->cdev));
1612 /* Get new request from the block device request queue */
1613 __dasd_process_blk_queue(device);
1614 /* Now check if the head of the ccw queue needs to be started. */
1615 __dasd_start_head(device);
1616 spin_unlock(get_ccwdev_lock(device->cdev));
1620 * Allocate and initialize request queue and default I/O scheduler.
1622 static int
1623 dasd_alloc_queue(struct dasd_device * device)
1625 int rc;
1627 device->request_queue = blk_init_queue(do_dasd_request,
1628 &device->request_queue_lock);
1629 if (device->request_queue == NULL)
1630 return -ENOMEM;
1632 device->request_queue->queuedata = device;
1634 elevator_exit(device->request_queue->elevator);
1635 rc = elevator_init(device->request_queue, "deadline");
1636 if (rc) {
1637 blk_cleanup_queue(device->request_queue);
1638 return rc;
1640 return 0;
1644 * Allocate and initialize request queue.
1646 static void
1647 dasd_setup_queue(struct dasd_device * device)
1649 int max;
1651 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1652 max = device->discipline->max_blocks << device->s2b_shift;
1653 blk_queue_max_sectors(device->request_queue, max);
1654 blk_queue_max_phys_segments(device->request_queue, -1L);
1655 blk_queue_max_hw_segments(device->request_queue, -1L);
1656 blk_queue_max_segment_size(device->request_queue, -1L);
1657 blk_queue_segment_boundary(device->request_queue, -1L);
1658 blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
1662 * Deactivate and free request queue.
1664 static void
1665 dasd_free_queue(struct dasd_device * device)
1667 if (device->request_queue) {
1668 blk_cleanup_queue(device->request_queue);
1669 device->request_queue = NULL;
1674 * Flush request on the request queue.
1676 static void
1677 dasd_flush_request_queue(struct dasd_device * device)
1679 struct request *req;
1681 if (!device->request_queue)
1682 return;
1684 spin_lock_irq(&device->request_queue_lock);
1685 while (!list_empty(&device->request_queue->queue_head)) {
1686 req = elv_next_request(device->request_queue);
1687 if (req == NULL)
1688 break;
1689 dasd_end_request(req, 0);
1690 blkdev_dequeue_request(req);
1692 spin_unlock_irq(&device->request_queue_lock);
1695 static int
1696 dasd_open(struct inode *inp, struct file *filp)
1698 struct gendisk *disk = inp->i_bdev->bd_disk;
1699 struct dasd_device *device = disk->private_data;
1700 int rc;
1702 atomic_inc(&device->open_count);
1703 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1704 rc = -ENODEV;
1705 goto unlock;
1708 if (!try_module_get(device->discipline->owner)) {
1709 rc = -EINVAL;
1710 goto unlock;
1713 if (dasd_probeonly) {
1714 DEV_MESSAGE(KERN_INFO, device, "%s",
1715 "No access to device due to probeonly mode");
1716 rc = -EPERM;
1717 goto out;
1720 if (device->state < DASD_STATE_BASIC) {
1721 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1722 " Cannot open unrecognized device");
1723 rc = -ENODEV;
1724 goto out;
1727 return 0;
1729 out:
1730 module_put(device->discipline->owner);
1731 unlock:
1732 atomic_dec(&device->open_count);
1733 return rc;
1736 static int
1737 dasd_release(struct inode *inp, struct file *filp)
1739 struct gendisk *disk = inp->i_bdev->bd_disk;
1740 struct dasd_device *device = disk->private_data;
1742 atomic_dec(&device->open_count);
1743 module_put(device->discipline->owner);
1744 return 0;
1748 * Return disk geometry.
1750 static int
1751 dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1753 struct dasd_device *device;
1755 device = bdev->bd_disk->private_data;
1756 if (!device)
1757 return -ENODEV;
1759 if (!device->discipline ||
1760 !device->discipline->fill_geometry)
1761 return -EINVAL;
1763 device->discipline->fill_geometry(device, geo);
1764 geo->start = get_start_sect(bdev) >> device->s2b_shift;
1765 return 0;
1768 struct block_device_operations
1769 dasd_device_operations = {
1770 .owner = THIS_MODULE,
1771 .open = dasd_open,
1772 .release = dasd_release,
1773 .ioctl = dasd_ioctl,
1774 .compat_ioctl = dasd_compat_ioctl,
1775 .getgeo = dasd_getgeo,
1779 static void
1780 dasd_exit(void)
1782 #ifdef CONFIG_PROC_FS
1783 dasd_proc_exit();
1784 #endif
1785 dasd_ioctl_exit();
1786 if (dasd_page_cache != NULL) {
1787 kmem_cache_destroy(dasd_page_cache);
1788 dasd_page_cache = NULL;
1790 dasd_gendisk_exit();
1791 dasd_devmap_exit();
1792 devfs_remove("dasd");
1793 if (dasd_debug_area != NULL) {
1794 debug_unregister(dasd_debug_area);
1795 dasd_debug_area = NULL;
1800 * SECTION: common functions for ccw_driver use
1804 * Initial attempt at a probe function. this can be simplified once
1805 * the other detection code is gone.
1808 dasd_generic_probe (struct ccw_device *cdev,
1809 struct dasd_discipline *discipline)
1811 int ret;
1813 ret = dasd_add_sysfs_files(cdev);
1814 if (ret) {
1815 printk(KERN_WARNING
1816 "dasd_generic_probe: could not add sysfs entries "
1817 "for %s\n", cdev->dev.bus_id);
1818 } else {
1819 cdev->handler = &dasd_int_handler;
1822 return ret;
1826 * This will one day be called from a global not_oper handler.
1827 * It is also used by driver_unregister during module unload.
1829 void
1830 dasd_generic_remove (struct ccw_device *cdev)
1832 struct dasd_device *device;
1834 cdev->handler = NULL;
1836 dasd_remove_sysfs_files(cdev);
1837 device = dasd_device_from_cdev(cdev);
1838 if (IS_ERR(device))
1839 return;
1840 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1841 /* Already doing offline processing */
1842 dasd_put_device(device);
1843 return;
1846 * This device is removed unconditionally. Set offline
1847 * flag to prevent dasd_open from opening it while it is
1848 * no quite down yet.
1850 dasd_set_target_state(device, DASD_STATE_NEW);
1851 /* dasd_delete_device destroys the device reference. */
1852 dasd_delete_device(device);
1856 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1857 * the device is detected for the first time and is supposed to be used
1858 * or the user has started activation through sysfs.
1861 dasd_generic_set_online (struct ccw_device *cdev,
1862 struct dasd_discipline *discipline)
1865 struct dasd_device *device;
1866 int rc;
1868 device = dasd_create_device(cdev);
1869 if (IS_ERR(device))
1870 return PTR_ERR(device);
1872 if (device->features & DASD_FEATURE_USEDIAG) {
1873 if (!dasd_diag_discipline_pointer) {
1874 printk (KERN_WARNING
1875 "dasd_generic couldn't online device %s "
1876 "- discipline DIAG not available\n",
1877 cdev->dev.bus_id);
1878 dasd_delete_device(device);
1879 return -ENODEV;
1881 discipline = dasd_diag_discipline_pointer;
1883 device->discipline = discipline;
1885 rc = discipline->check_device(device);
1886 if (rc) {
1887 printk (KERN_WARNING
1888 "dasd_generic couldn't online device %s "
1889 "with discipline %s rc=%i\n",
1890 cdev->dev.bus_id, discipline->name, rc);
1891 dasd_delete_device(device);
1892 return rc;
1895 dasd_set_target_state(device, DASD_STATE_ONLINE);
1896 if (device->state <= DASD_STATE_KNOWN) {
1897 printk (KERN_WARNING
1898 "dasd_generic discipline not found for %s\n",
1899 cdev->dev.bus_id);
1900 rc = -ENODEV;
1901 dasd_set_target_state(device, DASD_STATE_NEW);
1902 dasd_delete_device(device);
1903 } else
1904 pr_debug("dasd_generic device %s found\n",
1905 cdev->dev.bus_id);
1907 /* FIXME: we have to wait for the root device but we don't want
1908 * to wait for each single device but for all at once. */
1909 wait_event(dasd_init_waitq, _wait_for_device(device));
1911 dasd_put_device(device);
1913 return rc;
1917 dasd_generic_set_offline (struct ccw_device *cdev)
1919 struct dasd_device *device;
1920 int max_count;
1922 device = dasd_device_from_cdev(cdev);
1923 if (IS_ERR(device))
1924 return PTR_ERR(device);
1925 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1926 /* Already doing offline processing */
1927 dasd_put_device(device);
1928 return 0;
1931 * We must make sure that this device is currently not in use.
1932 * The open_count is increased for every opener, that includes
1933 * the blkdev_get in dasd_scan_partitions. We are only interested
1934 * in the other openers.
1936 max_count = device->bdev ? 0 : -1;
1937 if (atomic_read(&device->open_count) > max_count) {
1938 printk (KERN_WARNING "Can't offline dasd device with open"
1939 " count = %i.\n",
1940 atomic_read(&device->open_count));
1941 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
1942 dasd_put_device(device);
1943 return -EBUSY;
1945 dasd_set_target_state(device, DASD_STATE_NEW);
1946 /* dasd_delete_device destroys the device reference. */
1947 dasd_delete_device(device);
1949 return 0;
1953 dasd_generic_notify(struct ccw_device *cdev, int event)
1955 struct dasd_device *device;
1956 struct dasd_ccw_req *cqr;
1957 unsigned long flags;
1958 int ret;
1960 device = dasd_device_from_cdev(cdev);
1961 if (IS_ERR(device))
1962 return 0;
1963 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1964 ret = 0;
1965 switch (event) {
1966 case CIO_GONE:
1967 case CIO_NO_PATH:
1968 if (device->state < DASD_STATE_BASIC)
1969 break;
1970 /* Device is active. We want to keep it. */
1971 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
1972 list_for_each_entry(cqr, &device->ccw_queue, list)
1973 if (cqr->status == DASD_CQR_IN_IO)
1974 cqr->status = DASD_CQR_FAILED;
1975 device->stopped |= DASD_STOPPED_DC_EIO;
1976 } else {
1977 list_for_each_entry(cqr, &device->ccw_queue, list)
1978 if (cqr->status == DASD_CQR_IN_IO) {
1979 cqr->status = DASD_CQR_QUEUED;
1980 cqr->retries++;
1982 device->stopped |= DASD_STOPPED_DC_WAIT;
1983 dasd_set_timer(device, 0);
1985 dasd_schedule_bh(device);
1986 ret = 1;
1987 break;
1988 case CIO_OPER:
1989 /* FIXME: add a sanity check. */
1990 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
1991 dasd_schedule_bh(device);
1992 ret = 1;
1993 break;
1995 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1996 dasd_put_device(device);
1997 return ret;
2001 * Automatically online either all dasd devices (dasd_autodetect) or
2002 * all devices specified with dasd= parameters.
2004 static int
2005 __dasd_auto_online(struct device *dev, void *data)
2007 struct ccw_device *cdev;
2009 cdev = to_ccwdev(dev);
2010 if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
2011 ccw_device_set_online(cdev);
2012 return 0;
2015 void
2016 dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
2018 struct device_driver *drv;
2020 drv = get_driver(&dasd_discipline_driver->driver);
2021 driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
2022 put_driver(drv);
2025 static int __init
2026 dasd_init(void)
2028 int rc;
2030 init_waitqueue_head(&dasd_init_waitq);
2032 /* register 'common' DASD debug area, used for all DBF_XXX calls */
2033 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
2034 if (dasd_debug_area == NULL) {
2035 rc = -ENOMEM;
2036 goto failed;
2038 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2039 debug_set_level(dasd_debug_area, DBF_EMERG);
2041 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2043 dasd_diag_discipline_pointer = NULL;
2045 rc = devfs_mk_dir("dasd");
2046 if (rc)
2047 goto failed;
2048 rc = dasd_devmap_init();
2049 if (rc)
2050 goto failed;
2051 rc = dasd_gendisk_init();
2052 if (rc)
2053 goto failed;
2054 rc = dasd_parse();
2055 if (rc)
2056 goto failed;
2057 rc = dasd_ioctl_init();
2058 if (rc)
2059 goto failed;
2060 #ifdef CONFIG_PROC_FS
2061 rc = dasd_proc_init();
2062 if (rc)
2063 goto failed;
2064 #endif
2066 return 0;
2067 failed:
2068 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2069 dasd_exit();
2070 return rc;
2073 module_init(dasd_init);
2074 module_exit(dasd_exit);
2076 EXPORT_SYMBOL(dasd_debug_area);
2077 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2079 EXPORT_SYMBOL(dasd_add_request_head);
2080 EXPORT_SYMBOL(dasd_add_request_tail);
2081 EXPORT_SYMBOL(dasd_cancel_req);
2082 EXPORT_SYMBOL(dasd_clear_timer);
2083 EXPORT_SYMBOL(dasd_enable_device);
2084 EXPORT_SYMBOL(dasd_int_handler);
2085 EXPORT_SYMBOL(dasd_kfree_request);
2086 EXPORT_SYMBOL(dasd_kick_device);
2087 EXPORT_SYMBOL(dasd_kmalloc_request);
2088 EXPORT_SYMBOL(dasd_schedule_bh);
2089 EXPORT_SYMBOL(dasd_set_target_state);
2090 EXPORT_SYMBOL(dasd_set_timer);
2091 EXPORT_SYMBOL(dasd_sfree_request);
2092 EXPORT_SYMBOL(dasd_sleep_on);
2093 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2094 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2095 EXPORT_SYMBOL(dasd_smalloc_request);
2096 EXPORT_SYMBOL(dasd_start_IO);
2097 EXPORT_SYMBOL(dasd_term_IO);
2099 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2100 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2101 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2102 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2103 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2104 EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
2107 * Overrides for Emacs so that we follow Linus's tabbing style.
2108 * Emacs will notice this stuff at the end of the file and automatically
2109 * adjust the settings for this buffer only. This must remain at the end
2110 * of the file.
2111 * ---------------------------------------------------------------------------
2112 * Local variables:
2113 * c-indent-level: 4
2114 * c-brace-imaginary-offset: 0
2115 * c-brace-offset: -4
2116 * c-argdecl-indent: 4
2117 * c-label-offset: -4
2118 * c-continued-statement-offset: 4
2119 * c-continued-brace-offset: 0
2120 * indent-tabs-mode: 1
2121 * tab-width: 8
2122 * End: