[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / s390 / block / dasd.c
blob8fc891a9d47f569b823c02ed7dc85855f464fd0c
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
10 * $Revision: 1.167 $
13 #include <linux/config.h>
14 #include <linux/kmod.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/ctype.h>
18 #include <linux/major.h>
19 #include <linux/slab.h>
20 #include <linux/buffer_head.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 if (device->private)
119 kfree(device->private);
120 free_page((unsigned long) device->erp_mem);
121 free_pages((unsigned long) device->ccw_mem, 1);
122 kfree(device);
126 * Make a new device known to the system.
128 static inline int
129 dasd_state_new_to_known(struct dasd_device *device)
131 int rc;
134 * As long as the device is not in state DASD_STATE_NEW we want to
135 * keep the reference count > 0.
137 dasd_get_device(device);
139 rc = dasd_alloc_queue(device);
140 if (rc) {
141 dasd_put_device(device);
142 return rc;
145 device->state = DASD_STATE_KNOWN;
146 return 0;
150 * Let the system forget about a device.
152 static inline void
153 dasd_state_known_to_new(struct dasd_device * device)
155 /* Forget the discipline information. */
156 device->discipline = NULL;
157 device->state = DASD_STATE_NEW;
159 dasd_free_queue(device);
161 /* Give up reference we took in dasd_state_new_to_known. */
162 dasd_put_device(device);
166 * Request the irq line for the device.
168 static inline int
169 dasd_state_known_to_basic(struct dasd_device * device)
171 int rc;
173 /* Allocate and register gendisk structure. */
174 rc = dasd_gendisk_alloc(device);
175 if (rc)
176 return rc;
178 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
179 device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
180 8 * sizeof (long));
181 debug_register_view(device->debug_area, &debug_sprintf_view);
182 debug_set_level(device->debug_area, DBF_EMERG);
183 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
185 device->state = DASD_STATE_BASIC;
186 return 0;
190 * Release the irq line for the device. Terminate any running i/o.
192 static inline void
193 dasd_state_basic_to_known(struct dasd_device * device)
195 dasd_gendisk_free(device);
196 dasd_flush_ccw_queue(device, 1);
197 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
198 if (device->debug_area != NULL) {
199 debug_unregister(device->debug_area);
200 device->debug_area = NULL;
202 device->state = DASD_STATE_KNOWN;
206 * Do the initial analysis. The do_analysis function may return
207 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
208 * until the discipline decides to continue the startup sequence
209 * by calling the function dasd_change_state. The eckd disciplines
210 * uses this to start a ccw that detects the format. The completion
211 * interrupt for this detection ccw uses the kernel event daemon to
212 * trigger the call to dasd_change_state. All this is done in the
213 * discipline code, see dasd_eckd.c.
214 * After the analysis ccw is done (do_analysis returned 0 or error)
215 * the block device is setup. Either a fake disk is added to allow
216 * formatting or a proper device request queue is created.
218 static inline int
219 dasd_state_basic_to_ready(struct dasd_device * device)
221 int rc;
223 rc = 0;
224 if (device->discipline->do_analysis != NULL)
225 rc = device->discipline->do_analysis(device);
226 if (rc)
227 return rc;
228 dasd_setup_queue(device);
229 device->state = DASD_STATE_READY;
230 if (dasd_scan_partitions(device) != 0)
231 device->state = DASD_STATE_BASIC;
232 return 0;
236 * Remove device from block device layer. Destroy dirty buffers.
237 * Forget format information. Check if the target level is basic
238 * and if it is create fake disk for formatting.
240 static inline void
241 dasd_state_ready_to_basic(struct dasd_device * device)
243 dasd_flush_ccw_queue(device, 0);
244 dasd_destroy_partitions(device);
245 dasd_flush_request_queue(device);
246 device->blocks = 0;
247 device->bp_block = 0;
248 device->s2b_shift = 0;
249 device->state = DASD_STATE_BASIC;
253 * Make the device online and schedule the bottom half to start
254 * the requeueing of requests from the linux request queue to the
255 * ccw queue.
257 static inline int
258 dasd_state_ready_to_online(struct dasd_device * device)
260 device->state = DASD_STATE_ONLINE;
261 dasd_schedule_bh(device);
262 return 0;
266 * Stop the requeueing of requests again.
268 static inline void
269 dasd_state_online_to_ready(struct dasd_device * device)
271 device->state = DASD_STATE_READY;
275 * Device startup state changes.
277 static inline int
278 dasd_increase_state(struct dasd_device *device)
280 int rc;
282 rc = 0;
283 if (device->state == DASD_STATE_NEW &&
284 device->target >= DASD_STATE_KNOWN)
285 rc = dasd_state_new_to_known(device);
287 if (!rc &&
288 device->state == DASD_STATE_KNOWN &&
289 device->target >= DASD_STATE_BASIC)
290 rc = dasd_state_known_to_basic(device);
292 if (!rc &&
293 device->state == DASD_STATE_BASIC &&
294 device->target >= DASD_STATE_READY)
295 rc = dasd_state_basic_to_ready(device);
297 if (!rc &&
298 device->state == DASD_STATE_READY &&
299 device->target >= DASD_STATE_ONLINE)
300 rc = dasd_state_ready_to_online(device);
302 return rc;
306 * Device shutdown state changes.
308 static inline int
309 dasd_decrease_state(struct dasd_device *device)
311 if (device->state == DASD_STATE_ONLINE &&
312 device->target <= DASD_STATE_READY)
313 dasd_state_online_to_ready(device);
315 if (device->state == DASD_STATE_READY &&
316 device->target <= DASD_STATE_BASIC)
317 dasd_state_ready_to_basic(device);
319 if (device->state == DASD_STATE_BASIC &&
320 device->target <= DASD_STATE_KNOWN)
321 dasd_state_basic_to_known(device);
323 if (device->state == DASD_STATE_KNOWN &&
324 device->target <= DASD_STATE_NEW)
325 dasd_state_known_to_new(device);
327 return 0;
331 * This is the main startup/shutdown routine.
333 static void
334 dasd_change_state(struct dasd_device *device)
336 int rc;
338 if (device->state == device->target)
339 /* Already where we want to go today... */
340 return;
341 if (device->state < device->target)
342 rc = dasd_increase_state(device);
343 else
344 rc = dasd_decrease_state(device);
345 if (rc && rc != -EAGAIN)
346 device->target = device->state;
348 if (device->state == device->target)
349 wake_up(&dasd_init_waitq);
353 * Kick starter for devices that did not complete the startup/shutdown
354 * procedure or were sleeping because of a pending state.
355 * dasd_kick_device will schedule a call do do_kick_device to the kernel
356 * event daemon.
358 static void
359 do_kick_device(void *data)
361 struct dasd_device *device;
363 device = (struct dasd_device *) data;
364 dasd_change_state(device);
365 dasd_schedule_bh(device);
366 dasd_put_device(device);
369 void
370 dasd_kick_device(struct dasd_device *device)
372 dasd_get_device(device);
373 /* queue call to dasd_kick_device to the kernel event daemon. */
374 schedule_work(&device->kick_work);
378 * Set the target state for a device and starts the state change.
380 void
381 dasd_set_target_state(struct dasd_device *device, int target)
383 /* If we are in probeonly mode stop at DASD_STATE_READY. */
384 if (dasd_probeonly && target > DASD_STATE_READY)
385 target = DASD_STATE_READY;
386 if (device->target != target) {
387 if (device->state == target)
388 wake_up(&dasd_init_waitq);
389 device->target = target;
391 if (device->state != device->target)
392 dasd_change_state(device);
396 * Enable devices with device numbers in [from..to].
398 static inline int
399 _wait_for_device(struct dasd_device *device)
401 return (device->state == device->target);
404 void
405 dasd_enable_device(struct dasd_device *device)
407 dasd_set_target_state(device, DASD_STATE_ONLINE);
408 if (device->state <= DASD_STATE_KNOWN)
409 /* No discipline for device found. */
410 dasd_set_target_state(device, DASD_STATE_NEW);
411 /* Now wait for the devices to come up. */
412 wait_event(dasd_init_waitq, _wait_for_device(device));
416 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
418 #ifdef CONFIG_DASD_PROFILE
420 struct dasd_profile_info_t dasd_global_profile;
421 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
424 * Increments counter in global and local profiling structures.
426 #define dasd_profile_counter(value, counter, device) \
428 int index; \
429 for (index = 0; index < 31 && value >> (2+index); index++); \
430 dasd_global_profile.counter[index]++; \
431 device->profile.counter[index]++; \
435 * Add profiling information for cqr before execution.
437 static inline void
438 dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
439 struct request *req)
441 struct list_head *l;
442 unsigned int counter;
444 if (dasd_profile_level != DASD_PROFILE_ON)
445 return;
447 /* count the length of the chanq for statistics */
448 counter = 0;
449 list_for_each(l, &device->ccw_queue)
450 if (++counter >= 31)
451 break;
452 dasd_global_profile.dasd_io_nr_req[counter]++;
453 device->profile.dasd_io_nr_req[counter]++;
457 * Add profiling information for cqr after execution.
459 static inline void
460 dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
461 struct request *req)
463 long strtime, irqtime, endtime, tottime; /* in microseconds */
464 long tottimeps, sectors;
466 if (dasd_profile_level != DASD_PROFILE_ON)
467 return;
469 sectors = req->nr_sectors;
470 if (!cqr->buildclk || !cqr->startclk ||
471 !cqr->stopclk || !cqr->endclk ||
472 !sectors)
473 return;
475 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
476 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
477 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
478 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
479 tottimeps = tottime / sectors;
481 if (!dasd_global_profile.dasd_io_reqs)
482 memset(&dasd_global_profile, 0,
483 sizeof (struct dasd_profile_info_t));
484 dasd_global_profile.dasd_io_reqs++;
485 dasd_global_profile.dasd_io_sects += sectors;
487 if (!device->profile.dasd_io_reqs)
488 memset(&device->profile, 0,
489 sizeof (struct dasd_profile_info_t));
490 device->profile.dasd_io_reqs++;
491 device->profile.dasd_io_sects += sectors;
493 dasd_profile_counter(sectors, dasd_io_secs, device);
494 dasd_profile_counter(tottime, dasd_io_times, device);
495 dasd_profile_counter(tottimeps, dasd_io_timps, device);
496 dasd_profile_counter(strtime, dasd_io_time1, device);
497 dasd_profile_counter(irqtime, dasd_io_time2, device);
498 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
499 dasd_profile_counter(endtime, dasd_io_time3, device);
501 #else
502 #define dasd_profile_start(device, cqr, req) do {} while (0)
503 #define dasd_profile_end(device, cqr, req) do {} while (0)
504 #endif /* CONFIG_DASD_PROFILE */
507 * Allocate memory for a channel program with 'cplength' channel
508 * command words and 'datasize' additional space. There are two
509 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
510 * memory and 2) dasd_smalloc_request uses the static ccw memory
511 * that gets allocated for each device.
513 struct dasd_ccw_req *
514 dasd_kmalloc_request(char *magic, int cplength, int datasize,
515 struct dasd_device * device)
517 struct dasd_ccw_req *cqr;
519 /* Sanity checks */
520 if ( magic == NULL || datasize > PAGE_SIZE ||
521 (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
522 BUG();
524 cqr = kmalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
525 if (cqr == NULL)
526 return ERR_PTR(-ENOMEM);
527 memset(cqr, 0, sizeof(struct dasd_ccw_req));
528 cqr->cpaddr = NULL;
529 if (cplength > 0) {
530 cqr->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
531 GFP_ATOMIC | GFP_DMA);
532 if (cqr->cpaddr == NULL) {
533 kfree(cqr);
534 return ERR_PTR(-ENOMEM);
536 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
538 cqr->data = NULL;
539 if (datasize > 0) {
540 cqr->data = kmalloc(datasize, GFP_ATOMIC | GFP_DMA);
541 if (cqr->data == NULL) {
542 if (cqr->cpaddr != NULL)
543 kfree(cqr->cpaddr);
544 kfree(cqr);
545 return ERR_PTR(-ENOMEM);
547 memset(cqr->data, 0, datasize);
549 strncpy((char *) &cqr->magic, magic, 4);
550 ASCEBC((char *) &cqr->magic, 4);
551 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
552 dasd_get_device(device);
553 return cqr;
556 struct dasd_ccw_req *
557 dasd_smalloc_request(char *magic, int cplength, int datasize,
558 struct dasd_device * device)
560 unsigned long flags;
561 struct dasd_ccw_req *cqr;
562 char *data;
563 int size;
565 /* Sanity checks */
566 if ( magic == NULL || datasize > PAGE_SIZE ||
567 (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
568 BUG();
570 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
571 if (cplength > 0)
572 size += cplength * sizeof(struct ccw1);
573 if (datasize > 0)
574 size += datasize;
575 spin_lock_irqsave(&device->mem_lock, flags);
576 cqr = (struct dasd_ccw_req *)
577 dasd_alloc_chunk(&device->ccw_chunks, size);
578 spin_unlock_irqrestore(&device->mem_lock, flags);
579 if (cqr == NULL)
580 return ERR_PTR(-ENOMEM);
581 memset(cqr, 0, sizeof(struct dasd_ccw_req));
582 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
583 cqr->cpaddr = NULL;
584 if (cplength > 0) {
585 cqr->cpaddr = (struct ccw1 *) data;
586 data += cplength*sizeof(struct ccw1);
587 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
589 cqr->data = NULL;
590 if (datasize > 0) {
591 cqr->data = data;
592 memset(cqr->data, 0, datasize);
594 strncpy((char *) &cqr->magic, magic, 4);
595 ASCEBC((char *) &cqr->magic, 4);
596 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
597 dasd_get_device(device);
598 return cqr;
602 * Free memory of a channel program. This function needs to free all the
603 * idal lists that might have been created by dasd_set_cda and the
604 * struct dasd_ccw_req itself.
606 void
607 dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
609 #ifdef CONFIG_ARCH_S390X
610 struct ccw1 *ccw;
612 /* Clear any idals used for the request. */
613 ccw = cqr->cpaddr;
614 do {
615 clear_normalized_cda(ccw);
616 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
617 #endif
618 if (cqr->cpaddr != NULL)
619 kfree(cqr->cpaddr);
620 if (cqr->data != NULL)
621 kfree(cqr->data);
622 kfree(cqr);
623 dasd_put_device(device);
626 void
627 dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
629 unsigned long flags;
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);
638 * Check discipline magic in cqr.
640 static inline int
641 dasd_check_cqr(struct dasd_ccw_req *cqr)
643 struct dasd_device *device;
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;
656 return 0;
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.
666 dasd_term_IO(struct dasd_ccw_req * cqr)
668 struct dasd_device *device;
669 int retries, rc;
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 if (cqr->retries > 0) {
682 cqr->retries--;
683 cqr->status = DASD_CQR_CLEAR;
684 } else
685 cqr->status = DASD_CQR_FAILED;
686 cqr->stopclk = get_clock();
687 DBF_DEV_EVENT(DBF_DEBUG, device,
688 "terminate cqr %p successful",
689 cqr);
690 break;
691 case -ENODEV:
692 DBF_DEV_EVENT(DBF_ERR, device, "%s",
693 "device gone, retry");
694 break;
695 case -EIO:
696 DBF_DEV_EVENT(DBF_ERR, device, "%s",
697 "I/O error, retry");
698 break;
699 case -EINVAL:
700 case -EBUSY:
701 DBF_DEV_EVENT(DBF_ERR, device, "%s",
702 "device busy, retry later");
703 break;
704 default:
705 DEV_MESSAGE(KERN_ERR, device,
706 "line %d unknown RC=%d, please "
707 "report to linux390@de.ibm.com",
708 __LINE__, rc);
709 BUG();
710 break;
712 retries++;
714 dasd_schedule_bh(device);
715 return rc;
719 * Start the i/o. This start_IO can fail if the channel is really busy.
720 * In that case set up a timer to start the request later.
723 dasd_start_IO(struct dasd_ccw_req * cqr)
725 struct dasd_device *device;
726 int rc;
728 /* Check the cqr */
729 rc = dasd_check_cqr(cqr);
730 if (rc)
731 return rc;
732 device = (struct dasd_device *) cqr->device;
733 if (cqr->retries < 0) {
734 DEV_MESSAGE(KERN_DEBUG, device,
735 "start_IO: request %p (%02x/%i) - no retry left.",
736 cqr, cqr->status, cqr->retries);
737 cqr->status = DASD_CQR_FAILED;
738 return -EIO;
740 cqr->startclk = get_clock();
741 cqr->starttime = jiffies;
742 cqr->retries--;
743 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
744 cqr->lpm, 0);
745 switch (rc) {
746 case 0:
747 cqr->status = DASD_CQR_IN_IO;
748 DBF_DEV_EVENT(DBF_DEBUG, device,
749 "start_IO: request %p started successful",
750 cqr);
751 break;
752 case -EBUSY:
753 DBF_DEV_EVENT(DBF_ERR, device, "%s",
754 "start_IO: device busy, retry later");
755 break;
756 case -ETIMEDOUT:
757 DBF_DEV_EVENT(DBF_ERR, device, "%s",
758 "start_IO: request timeout, retry later");
759 break;
760 case -EACCES:
761 /* -EACCES indicates that the request used only a
762 * subset of the available pathes and all these
763 * pathes are gone.
764 * Do a retry with all available pathes.
766 cqr->lpm = LPM_ANYPATH;
767 DBF_DEV_EVENT(DBF_ERR, device, "%s",
768 "start_IO: selected pathes gone,"
769 " retry on all pathes");
770 break;
771 case -ENODEV:
772 case -EIO:
773 DBF_DEV_EVENT(DBF_ERR, device, "%s",
774 "start_IO: device gone, retry");
775 break;
776 default:
777 DEV_MESSAGE(KERN_ERR, device,
778 "line %d unknown RC=%d, please report"
779 " to linux390@de.ibm.com", __LINE__, rc);
780 BUG();
781 break;
783 return rc;
787 * Timeout function for dasd devices. This is used for different purposes
788 * 1) missing interrupt handler for normal operation
789 * 2) delayed start of request where start_IO failed with -EBUSY
790 * 3) timeout for missing state change interrupts
791 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
792 * DASD_CQR_QUEUED for 2) and 3).
794 static void
795 dasd_timeout_device(unsigned long ptr)
797 unsigned long flags;
798 struct dasd_device *device;
800 device = (struct dasd_device *) ptr;
801 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
802 /* re-activate request queue */
803 device->stopped &= ~DASD_STOPPED_PENDING;
804 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
805 dasd_schedule_bh(device);
809 * Setup timeout for a device in jiffies.
811 void
812 dasd_set_timer(struct dasd_device *device, int expires)
814 if (expires == 0) {
815 if (timer_pending(&device->timer))
816 del_timer(&device->timer);
817 return;
819 if (timer_pending(&device->timer)) {
820 if (mod_timer(&device->timer, jiffies + expires))
821 return;
823 device->timer.function = dasd_timeout_device;
824 device->timer.data = (unsigned long) device;
825 device->timer.expires = jiffies + expires;
826 add_timer(&device->timer);
830 * Clear timeout for a device.
832 void
833 dasd_clear_timer(struct dasd_device *device)
835 if (timer_pending(&device->timer))
836 del_timer(&device->timer);
839 static void
840 dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
842 struct dasd_ccw_req *cqr;
843 struct dasd_device *device;
845 cqr = (struct dasd_ccw_req *) intparm;
846 if (cqr->status != DASD_CQR_IN_IO) {
847 MESSAGE(KERN_DEBUG,
848 "invalid status in handle_killed_request: "
849 "bus_id %s, status %02x",
850 cdev->dev.bus_id, cqr->status);
851 return;
854 device = (struct dasd_device *) cqr->device;
855 if (device == NULL ||
856 device != dasd_device_from_cdev(cdev) ||
857 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
858 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
859 cdev->dev.bus_id);
860 return;
863 /* Schedule request to be retried. */
864 cqr->status = DASD_CQR_QUEUED;
866 dasd_clear_timer(device);
867 dasd_schedule_bh(device);
868 dasd_put_device(device);
871 static void
872 dasd_handle_state_change_pending(struct dasd_device *device)
874 struct dasd_ccw_req *cqr;
875 struct list_head *l, *n;
877 device->stopped &= ~DASD_STOPPED_PENDING;
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;
886 dasd_clear_timer(device);
887 dasd_schedule_bh(device);
891 * Interrupt handler for "normal" ssch-io based dasd devices.
893 void
894 dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
895 struct irb *irb)
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;
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));
918 return;
921 now = get_clock();
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);
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);
935 return;
938 cqr = (struct dasd_ccw_req *) intparm;
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;
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;
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;
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;
972 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
973 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
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;
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!");
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();
1026 if (expires != 0)
1027 dasd_set_timer(device, expires);
1028 else
1029 dasd_clear_timer(device);
1030 dasd_schedule_bh(device);
1034 * posts the buffer_cache about a finalized request
1036 static inline void
1037 dasd_end_request(struct request *req, int uptodate)
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);
1046 * Process finished error recovery ccw.
1048 static inline void
1049 __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1051 dasd_erp_fn_t erp_fn;
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);
1062 * Process ccw request queue.
1064 static inline void
1065 __dasd_process_ccw_queue(struct dasd_device * device,
1066 struct list_head *final_queue)
1068 struct list_head *l, *n;
1069 struct dasd_ccw_req *cqr;
1070 dasd_erp_fn_t erp_fn;
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);
1094 goto restart;
1096 /* Process finished ERP request. */
1097 if (cqr->refers) {
1098 __dasd_process_erp(device, cqr);
1099 goto restart;
1102 /* Rechain finished requests to final queue */
1103 cqr->endclk = get_clock();
1104 list_move_tail(&cqr->list, final_queue);
1108 static void
1109 dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1111 struct request *req;
1112 struct dasd_device *device;
1113 int status;
1115 req = (struct request *) data;
1116 device = cqr->device;
1117 dasd_profile_end(device, cqr, req);
1118 status = cqr->device->discipline->free_cp(cqr,req);
1119 spin_lock_irq(&device->request_queue_lock);
1120 dasd_end_request(req, status);
1121 spin_unlock_irq(&device->request_queue_lock);
1126 * Fetch requests from the block device queue.
1128 static inline void
1129 __dasd_process_blk_queue(struct dasd_device * device)
1131 request_queue_t *queue;
1132 struct request *req;
1133 struct dasd_ccw_req *cqr;
1134 int nr_queued;
1136 queue = device->request_queue;
1137 /* No queue ? Then there is nothing to do. */
1138 if (queue == NULL)
1139 return;
1142 * We requeue request from the block device queue to the ccw
1143 * queue only in two states. In state DASD_STATE_READY the
1144 * partition detection is done and we need to requeue requests
1145 * for that. State DASD_STATE_ONLINE is normal block device
1146 * operation.
1148 if (device->state != DASD_STATE_READY &&
1149 device->state != DASD_STATE_ONLINE)
1150 return;
1151 nr_queued = 0;
1152 /* Now we try to fetch requests from the request queue */
1153 list_for_each_entry(cqr, &device->ccw_queue, list)
1154 if (cqr->status == DASD_CQR_QUEUED)
1155 nr_queued++;
1156 while (!blk_queue_plugged(queue) &&
1157 elv_next_request(queue) &&
1158 nr_queued < DASD_CHANQ_MAX_SIZE) {
1159 req = elv_next_request(queue);
1161 if (device->features & DASD_FEATURE_READONLY &&
1162 rq_data_dir(req) == WRITE) {
1163 DBF_DEV_EVENT(DBF_ERR, device,
1164 "Rejecting write request %p",
1165 req);
1166 blkdev_dequeue_request(req);
1167 dasd_end_request(req, 0);
1168 continue;
1170 if (device->stopped & DASD_STOPPED_DC_EIO) {
1171 blkdev_dequeue_request(req);
1172 dasd_end_request(req, 0);
1173 continue;
1175 cqr = device->discipline->build_cp(device, req);
1176 if (IS_ERR(cqr)) {
1177 if (PTR_ERR(cqr) == -ENOMEM)
1178 break; /* terminate request queue loop */
1179 DBF_DEV_EVENT(DBF_ERR, device,
1180 "CCW creation failed (rc=%ld) "
1181 "on request %p",
1182 PTR_ERR(cqr), req);
1183 blkdev_dequeue_request(req);
1184 dasd_end_request(req, 0);
1185 continue;
1187 cqr->callback = dasd_end_request_cb;
1188 cqr->callback_data = (void *) req;
1189 cqr->status = DASD_CQR_QUEUED;
1190 blkdev_dequeue_request(req);
1191 list_add_tail(&cqr->list, &device->ccw_queue);
1192 dasd_profile_start(device, cqr, req);
1193 nr_queued++;
1198 * Take a look at the first request on the ccw queue and check
1199 * if it reached its expire time. If so, terminate the IO.
1201 static inline void
1202 __dasd_check_expire(struct dasd_device * device)
1204 struct dasd_ccw_req *cqr;
1206 if (list_empty(&device->ccw_queue))
1207 return;
1208 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1209 if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1210 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1211 if (device->discipline->term_IO(cqr) != 0)
1212 /* Hmpf, try again in 1/10 sec */
1213 dasd_set_timer(device, 10);
1219 * Take a look at the first request on the ccw queue and check
1220 * if it needs to be started.
1222 static inline void
1223 __dasd_start_head(struct dasd_device * device)
1225 struct dasd_ccw_req *cqr;
1226 int rc;
1228 if (list_empty(&device->ccw_queue))
1229 return;
1230 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1231 if ((cqr->status == DASD_CQR_QUEUED) &&
1232 (!device->stopped)) {
1233 /* try to start the first I/O that can be started */
1234 rc = device->discipline->start_IO(cqr);
1235 if (rc == 0)
1236 dasd_set_timer(device, cqr->expires);
1237 else if (rc == -EACCES) {
1238 dasd_schedule_bh(device);
1239 } else
1240 /* Hmpf, try again in 1/2 sec */
1241 dasd_set_timer(device, 50);
1246 * Remove requests from the ccw queue.
1248 static void
1249 dasd_flush_ccw_queue(struct dasd_device * device, int all)
1251 struct list_head flush_queue;
1252 struct list_head *l, *n;
1253 struct dasd_ccw_req *cqr;
1255 INIT_LIST_HEAD(&flush_queue);
1256 spin_lock_irq(get_ccwdev_lock(device->cdev));
1257 list_for_each_safe(l, n, &device->ccw_queue) {
1258 cqr = list_entry(l, struct dasd_ccw_req, list);
1259 /* Flush all request or only block device requests? */
1260 if (all == 0 && cqr->callback == dasd_end_request_cb)
1261 continue;
1262 if (cqr->status == DASD_CQR_IN_IO)
1263 device->discipline->term_IO(cqr);
1264 if (cqr->status != DASD_CQR_DONE ||
1265 cqr->status != DASD_CQR_FAILED) {
1266 cqr->status = DASD_CQR_FAILED;
1267 cqr->stopclk = get_clock();
1269 /* Process finished ERP request. */
1270 if (cqr->refers) {
1271 __dasd_process_erp(device, cqr);
1272 continue;
1274 /* Rechain request on device request queue */
1275 cqr->endclk = get_clock();
1276 list_move_tail(&cqr->list, &flush_queue);
1278 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1279 /* Now call the callback function of flushed requests */
1280 list_for_each_safe(l, n, &flush_queue) {
1281 cqr = list_entry(l, struct dasd_ccw_req, list);
1282 if (cqr->callback != NULL)
1283 (cqr->callback)(cqr, cqr->callback_data);
1288 * Acquire the device lock and process queues for the device.
1290 static void
1291 dasd_tasklet(struct dasd_device * device)
1293 struct list_head final_queue;
1294 struct list_head *l, *n;
1295 struct dasd_ccw_req *cqr;
1297 atomic_set (&device->tasklet_scheduled, 0);
1298 INIT_LIST_HEAD(&final_queue);
1299 spin_lock_irq(get_ccwdev_lock(device->cdev));
1300 /* Check expire time of first request on the ccw queue. */
1301 __dasd_check_expire(device);
1302 /* Finish off requests on ccw queue */
1303 __dasd_process_ccw_queue(device, &final_queue);
1304 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1305 /* Now call the callback function of requests with final status */
1306 list_for_each_safe(l, n, &final_queue) {
1307 cqr = list_entry(l, struct dasd_ccw_req, list);
1308 list_del(&cqr->list);
1309 if (cqr->callback != NULL)
1310 (cqr->callback)(cqr, cqr->callback_data);
1312 spin_lock_irq(&device->request_queue_lock);
1313 spin_lock(get_ccwdev_lock(device->cdev));
1314 /* Get new request from the block device request queue */
1315 __dasd_process_blk_queue(device);
1316 /* Now check if the head of the ccw queue needs to be started. */
1317 __dasd_start_head(device);
1318 spin_unlock(get_ccwdev_lock(device->cdev));
1319 spin_unlock_irq(&device->request_queue_lock);
1320 dasd_put_device(device);
1324 * Schedules a call to dasd_tasklet over the device tasklet.
1326 void
1327 dasd_schedule_bh(struct dasd_device * device)
1329 /* Protect against rescheduling. */
1330 if (atomic_compare_and_swap (0, 1, &device->tasklet_scheduled))
1331 return;
1332 dasd_get_device(device);
1333 tasklet_hi_schedule(&device->tasklet);
1337 * Queue a request to the head of the ccw_queue. Start the I/O if
1338 * possible.
1340 void
1341 dasd_add_request_head(struct dasd_ccw_req *req)
1343 struct dasd_device *device;
1344 unsigned long flags;
1346 device = req->device;
1347 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1348 req->status = DASD_CQR_QUEUED;
1349 req->device = device;
1350 list_add(&req->list, &device->ccw_queue);
1351 /* let the bh start the request to keep them in order */
1352 dasd_schedule_bh(device);
1353 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1357 * Queue a request to the tail of the ccw_queue. Start the I/O if
1358 * possible.
1360 void
1361 dasd_add_request_tail(struct dasd_ccw_req *req)
1363 struct dasd_device *device;
1364 unsigned long flags;
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_tail(&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);
1377 * Wakeup callback.
1379 static void
1380 dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1382 wake_up((wait_queue_head_t *) data);
1385 static inline int
1386 _wait_for_wakeup(struct dasd_ccw_req *cqr)
1388 struct dasd_device *device;
1389 int rc;
1391 device = cqr->device;
1392 spin_lock_irq(get_ccwdev_lock(device->cdev));
1393 rc = cqr->status == DASD_CQR_DONE || cqr->status == DASD_CQR_FAILED;
1394 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1395 return rc;
1399 * Attempts to start a special ccw queue and waits for its completion.
1402 dasd_sleep_on(struct dasd_ccw_req * cqr)
1404 wait_queue_head_t wait_q;
1405 struct dasd_device *device;
1406 int rc;
1408 device = cqr->device;
1409 spin_lock_irq(get_ccwdev_lock(device->cdev));
1411 init_waitqueue_head (&wait_q);
1412 cqr->callback = dasd_wakeup_cb;
1413 cqr->callback_data = (void *) &wait_q;
1414 cqr->status = DASD_CQR_QUEUED;
1415 list_add_tail(&cqr->list, &device->ccw_queue);
1417 /* let the bh start the request to keep them in order */
1418 dasd_schedule_bh(device);
1420 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1422 wait_event(wait_q, _wait_for_wakeup(cqr));
1424 /* Request status is either done or failed. */
1425 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1426 return rc;
1430 * Attempts to start a special ccw queue and wait interruptible
1431 * for its completion.
1434 dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1436 wait_queue_head_t wait_q;
1437 struct dasd_device *device;
1438 int rc, finished;
1440 device = cqr->device;
1441 spin_lock_irq(get_ccwdev_lock(device->cdev));
1443 init_waitqueue_head (&wait_q);
1444 cqr->callback = dasd_wakeup_cb;
1445 cqr->callback_data = (void *) &wait_q;
1446 cqr->status = DASD_CQR_QUEUED;
1447 list_add_tail(&cqr->list, &device->ccw_queue);
1449 /* let the bh start the request to keep them in order */
1450 dasd_schedule_bh(device);
1451 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1453 finished = 0;
1454 while (!finished) {
1455 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1456 if (rc != -ERESTARTSYS) {
1457 /* Request status is either done or failed. */
1458 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1459 break;
1461 spin_lock_irq(get_ccwdev_lock(device->cdev));
1462 if (cqr->status == DASD_CQR_IN_IO &&
1463 device->discipline->term_IO(cqr) == 0) {
1464 list_del(&cqr->list);
1465 finished = 1;
1467 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1469 return rc;
1473 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1474 * for eckd devices) the currently running request has to be terminated
1475 * and be put back to status queued, before the special request is added
1476 * to the head of the queue. Then the special request is waited on normally.
1478 static inline int
1479 _dasd_term_running_cqr(struct dasd_device *device)
1481 struct dasd_ccw_req *cqr;
1482 int rc;
1484 if (list_empty(&device->ccw_queue))
1485 return 0;
1486 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1487 rc = device->discipline->term_IO(cqr);
1488 if (rc == 0) {
1489 /* termination successful */
1490 cqr->status = DASD_CQR_QUEUED;
1491 cqr->startclk = cqr->stopclk = 0;
1492 cqr->starttime = 0;
1494 return rc;
1498 dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1500 wait_queue_head_t wait_q;
1501 struct dasd_device *device;
1502 int rc;
1504 device = cqr->device;
1505 spin_lock_irq(get_ccwdev_lock(device->cdev));
1506 rc = _dasd_term_running_cqr(device);
1507 if (rc) {
1508 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1509 return rc;
1512 init_waitqueue_head (&wait_q);
1513 cqr->callback = dasd_wakeup_cb;
1514 cqr->callback_data = (void *) &wait_q;
1515 cqr->status = DASD_CQR_QUEUED;
1516 list_add(&cqr->list, &device->ccw_queue);
1518 /* let the bh start the request to keep them in order */
1519 dasd_schedule_bh(device);
1521 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1523 wait_event(wait_q, _wait_for_wakeup(cqr));
1525 /* Request status is either done or failed. */
1526 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1527 return rc;
1531 * Cancels a request that was started with dasd_sleep_on_req.
1532 * This is useful to timeout requests. The request will be
1533 * terminated if it is currently in i/o.
1534 * Returns 1 if the request has been terminated.
1537 dasd_cancel_req(struct dasd_ccw_req *cqr)
1539 struct dasd_device *device = cqr->device;
1540 unsigned long flags;
1541 int rc;
1543 rc = 0;
1544 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1545 switch (cqr->status) {
1546 case DASD_CQR_QUEUED:
1547 /* request was not started - just set to failed */
1548 cqr->status = DASD_CQR_FAILED;
1549 break;
1550 case DASD_CQR_IN_IO:
1551 /* request in IO - terminate IO and release again */
1552 if (device->discipline->term_IO(cqr) != 0)
1553 /* what to do if unable to terminate ??????
1554 e.g. not _IN_IO */
1555 cqr->status = DASD_CQR_FAILED;
1556 cqr->stopclk = get_clock();
1557 rc = 1;
1558 break;
1559 case DASD_CQR_DONE:
1560 case DASD_CQR_FAILED:
1561 /* already finished - do nothing */
1562 break;
1563 default:
1564 DEV_MESSAGE(KERN_ALERT, device,
1565 "invalid status %02x in request",
1566 cqr->status);
1567 BUG();
1570 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1571 dasd_schedule_bh(device);
1572 return rc;
1576 * SECTION: Block device operations (request queue, partitions, open, release).
1580 * Dasd request queue function. Called from ll_rw_blk.c
1582 static void
1583 do_dasd_request(request_queue_t * queue)
1585 struct dasd_device *device;
1587 device = (struct dasd_device *) queue->queuedata;
1588 spin_lock(get_ccwdev_lock(device->cdev));
1589 /* Get new request from the block device request queue */
1590 __dasd_process_blk_queue(device);
1591 /* Now check if the head of the ccw queue needs to be started. */
1592 __dasd_start_head(device);
1593 spin_unlock(get_ccwdev_lock(device->cdev));
1597 * Allocate and initialize request queue and default I/O scheduler.
1599 static int
1600 dasd_alloc_queue(struct dasd_device * device)
1602 int rc;
1604 device->request_queue = blk_init_queue(do_dasd_request,
1605 &device->request_queue_lock);
1606 if (device->request_queue == NULL)
1607 return -ENOMEM;
1609 device->request_queue->queuedata = device;
1611 elevator_exit(device->request_queue->elevator);
1612 rc = elevator_init(device->request_queue, "deadline");
1613 if (rc) {
1614 blk_cleanup_queue(device->request_queue);
1615 return rc;
1617 return 0;
1621 * Allocate and initialize request queue.
1623 static void
1624 dasd_setup_queue(struct dasd_device * device)
1626 int max;
1628 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1629 max = device->discipline->max_blocks << device->s2b_shift;
1630 blk_queue_max_sectors(device->request_queue, max);
1631 blk_queue_max_phys_segments(device->request_queue, -1L);
1632 blk_queue_max_hw_segments(device->request_queue, -1L);
1633 blk_queue_max_segment_size(device->request_queue, -1L);
1634 blk_queue_segment_boundary(device->request_queue, -1L);
1635 blk_queue_ordered(device->request_queue, 1);
1639 * Deactivate and free request queue.
1641 static void
1642 dasd_free_queue(struct dasd_device * device)
1644 if (device->request_queue) {
1645 blk_cleanup_queue(device->request_queue);
1646 device->request_queue = NULL;
1651 * Flush request on the request queue.
1653 static void
1654 dasd_flush_request_queue(struct dasd_device * device)
1656 struct request *req;
1658 if (!device->request_queue)
1659 return;
1661 spin_lock_irq(&device->request_queue_lock);
1662 while (!list_empty(&device->request_queue->queue_head)) {
1663 req = elv_next_request(device->request_queue);
1664 if (req == NULL)
1665 break;
1666 dasd_end_request(req, 0);
1667 blkdev_dequeue_request(req);
1669 spin_unlock_irq(&device->request_queue_lock);
1672 static int
1673 dasd_open(struct inode *inp, struct file *filp)
1675 struct gendisk *disk = inp->i_bdev->bd_disk;
1676 struct dasd_device *device = disk->private_data;
1677 int rc;
1679 atomic_inc(&device->open_count);
1680 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1681 rc = -ENODEV;
1682 goto unlock;
1685 if (!try_module_get(device->discipline->owner)) {
1686 rc = -EINVAL;
1687 goto unlock;
1690 if (dasd_probeonly) {
1691 DEV_MESSAGE(KERN_INFO, device, "%s",
1692 "No access to device due to probeonly mode");
1693 rc = -EPERM;
1694 goto out;
1697 if (device->state < DASD_STATE_BASIC) {
1698 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1699 " Cannot open unrecognized device");
1700 rc = -ENODEV;
1701 goto out;
1704 return 0;
1706 out:
1707 module_put(device->discipline->owner);
1708 unlock:
1709 atomic_dec(&device->open_count);
1710 return rc;
1713 static int
1714 dasd_release(struct inode *inp, struct file *filp)
1716 struct gendisk *disk = inp->i_bdev->bd_disk;
1717 struct dasd_device *device = disk->private_data;
1719 atomic_dec(&device->open_count);
1720 module_put(device->discipline->owner);
1721 return 0;
1724 struct block_device_operations
1725 dasd_device_operations = {
1726 .owner = THIS_MODULE,
1727 .open = dasd_open,
1728 .release = dasd_release,
1729 .ioctl = dasd_ioctl,
1733 static void
1734 dasd_exit(void)
1736 #ifdef CONFIG_PROC_FS
1737 dasd_proc_exit();
1738 #endif
1739 dasd_ioctl_exit();
1740 if (dasd_page_cache != NULL) {
1741 kmem_cache_destroy(dasd_page_cache);
1742 dasd_page_cache = NULL;
1744 dasd_gendisk_exit();
1745 dasd_devmap_exit();
1746 devfs_remove("dasd");
1747 if (dasd_debug_area != NULL) {
1748 debug_unregister(dasd_debug_area);
1749 dasd_debug_area = NULL;
1754 * SECTION: common functions for ccw_driver use
1757 /* initial attempt at a probe function. this can be simplified once
1758 * the other detection code is gone */
1760 dasd_generic_probe (struct ccw_device *cdev,
1761 struct dasd_discipline *discipline)
1763 int ret;
1765 ret = dasd_add_sysfs_files(cdev);
1766 if (ret) {
1767 printk(KERN_WARNING
1768 "dasd_generic_probe: could not add sysfs entries "
1769 "for %s\n", cdev->dev.bus_id);
1770 } else {
1771 cdev->handler = &dasd_int_handler;
1774 return ret;
1777 /* this will one day be called from a global not_oper handler.
1778 * It is also used by driver_unregister during module unload */
1779 void
1780 dasd_generic_remove (struct ccw_device *cdev)
1782 struct dasd_device *device;
1784 cdev->handler = NULL;
1786 dasd_remove_sysfs_files(cdev);
1787 device = dasd_device_from_cdev(cdev);
1788 if (IS_ERR(device))
1789 return;
1790 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1791 /* Already doing offline processing */
1792 dasd_put_device(device);
1793 return;
1796 * This device is removed unconditionally. Set offline
1797 * flag to prevent dasd_open from opening it while it is
1798 * no quite down yet.
1800 dasd_set_target_state(device, DASD_STATE_NEW);
1801 /* dasd_delete_device destroys the device reference. */
1802 dasd_delete_device(device);
1805 /* activate a device. This is called from dasd_{eckd,fba}_probe() when either
1806 * the device is detected for the first time and is supposed to be used
1807 * or the user has started activation through sysfs */
1809 dasd_generic_set_online (struct ccw_device *cdev,
1810 struct dasd_discipline *discipline)
1813 struct dasd_device *device;
1814 int rc;
1816 device = dasd_create_device(cdev);
1817 if (IS_ERR(device))
1818 return PTR_ERR(device);
1820 if (device->features & DASD_FEATURE_USEDIAG) {
1821 if (!dasd_diag_discipline_pointer) {
1822 printk (KERN_WARNING
1823 "dasd_generic couldn't online device %s "
1824 "- discipline DIAG not available\n",
1825 cdev->dev.bus_id);
1826 dasd_delete_device(device);
1827 return -ENODEV;
1829 discipline = dasd_diag_discipline_pointer;
1831 device->discipline = discipline;
1833 rc = discipline->check_device(device);
1834 if (rc) {
1835 printk (KERN_WARNING
1836 "dasd_generic couldn't online device %s "
1837 "with discipline %s rc=%i\n",
1838 cdev->dev.bus_id, discipline->name, rc);
1839 dasd_delete_device(device);
1840 return rc;
1843 dasd_set_target_state(device, DASD_STATE_ONLINE);
1844 if (device->state <= DASD_STATE_KNOWN) {
1845 printk (KERN_WARNING
1846 "dasd_generic discipline not found for %s\n",
1847 cdev->dev.bus_id);
1848 rc = -ENODEV;
1849 dasd_set_target_state(device, DASD_STATE_NEW);
1850 dasd_delete_device(device);
1851 } else
1852 pr_debug("dasd_generic device %s found\n",
1853 cdev->dev.bus_id);
1855 /* FIXME: we have to wait for the root device but we don't want
1856 * to wait for each single device but for all at once. */
1857 wait_event(dasd_init_waitq, _wait_for_device(device));
1859 dasd_put_device(device);
1861 return rc;
1865 dasd_generic_set_offline (struct ccw_device *cdev)
1867 struct dasd_device *device;
1868 int max_count;
1870 device = dasd_device_from_cdev(cdev);
1871 if (IS_ERR(device))
1872 return PTR_ERR(device);
1873 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1874 /* Already doing offline processing */
1875 dasd_put_device(device);
1876 return 0;
1879 * We must make sure that this device is currently not in use.
1880 * The open_count is increased for every opener, that includes
1881 * the blkdev_get in dasd_scan_partitions. We are only interested
1882 * in the other openers.
1884 max_count = device->bdev ? 0 : -1;
1885 if (atomic_read(&device->open_count) > max_count) {
1886 printk (KERN_WARNING "Can't offline dasd device with open"
1887 " count = %i.\n",
1888 atomic_read(&device->open_count));
1889 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
1890 dasd_put_device(device);
1891 return -EBUSY;
1893 dasd_set_target_state(device, DASD_STATE_NEW);
1894 /* dasd_delete_device destroys the device reference. */
1895 dasd_delete_device(device);
1897 return 0;
1901 dasd_generic_notify(struct ccw_device *cdev, int event)
1903 struct dasd_device *device;
1904 struct dasd_ccw_req *cqr;
1905 unsigned long flags;
1906 int ret;
1908 device = dasd_device_from_cdev(cdev);
1909 if (IS_ERR(device))
1910 return 0;
1911 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1912 ret = 0;
1913 switch (event) {
1914 case CIO_GONE:
1915 case CIO_NO_PATH:
1916 if (device->state < DASD_STATE_BASIC)
1917 break;
1918 /* Device is active. We want to keep it. */
1919 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
1920 list_for_each_entry(cqr, &device->ccw_queue, list)
1921 if (cqr->status == DASD_CQR_IN_IO)
1922 cqr->status = DASD_CQR_FAILED;
1923 device->stopped |= DASD_STOPPED_DC_EIO;
1924 dasd_schedule_bh(device);
1925 } else {
1926 list_for_each_entry(cqr, &device->ccw_queue, list)
1927 if (cqr->status == DASD_CQR_IN_IO) {
1928 cqr->status = DASD_CQR_QUEUED;
1929 cqr->retries++;
1931 device->stopped |= DASD_STOPPED_DC_WAIT;
1932 dasd_set_timer(device, 0);
1934 ret = 1;
1935 break;
1936 case CIO_OPER:
1937 /* FIXME: add a sanity check. */
1938 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
1939 dasd_schedule_bh(device);
1940 ret = 1;
1941 break;
1943 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1944 dasd_put_device(device);
1945 return ret;
1949 * Automatically online either all dasd devices (dasd_autodetect) or
1950 * all devices specified with dasd= parameters.
1952 static int
1953 __dasd_auto_online(struct device *dev, void *data)
1955 struct ccw_device *cdev;
1957 cdev = to_ccwdev(dev);
1958 if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
1959 ccw_device_set_online(cdev);
1960 return 0;
1963 void
1964 dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
1966 struct device_driver *drv;
1968 drv = get_driver(&dasd_discipline_driver->driver);
1969 driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
1970 put_driver(drv);
1973 static int __init
1974 dasd_init(void)
1976 int rc;
1978 init_waitqueue_head(&dasd_init_waitq);
1980 /* register 'common' DASD debug area, used for all DBF_XXX calls */
1981 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
1982 if (dasd_debug_area == NULL) {
1983 rc = -ENOMEM;
1984 goto failed;
1986 debug_register_view(dasd_debug_area, &debug_sprintf_view);
1987 debug_set_level(dasd_debug_area, DBF_EMERG);
1989 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
1991 dasd_diag_discipline_pointer = NULL;
1993 rc = devfs_mk_dir("dasd");
1994 if (rc)
1995 goto failed;
1996 rc = dasd_devmap_init();
1997 if (rc)
1998 goto failed;
1999 rc = dasd_gendisk_init();
2000 if (rc)
2001 goto failed;
2002 rc = dasd_parse();
2003 if (rc)
2004 goto failed;
2005 rc = dasd_ioctl_init();
2006 if (rc)
2007 goto failed;
2008 #ifdef CONFIG_PROC_FS
2009 rc = dasd_proc_init();
2010 if (rc)
2011 goto failed;
2012 #endif
2014 return 0;
2015 failed:
2016 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2017 dasd_exit();
2018 return rc;
2021 module_init(dasd_init);
2022 module_exit(dasd_exit);
2024 EXPORT_SYMBOL(dasd_debug_area);
2025 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2027 EXPORT_SYMBOL(dasd_add_request_head);
2028 EXPORT_SYMBOL(dasd_add_request_tail);
2029 EXPORT_SYMBOL(dasd_cancel_req);
2030 EXPORT_SYMBOL(dasd_clear_timer);
2031 EXPORT_SYMBOL(dasd_enable_device);
2032 EXPORT_SYMBOL(dasd_int_handler);
2033 EXPORT_SYMBOL(dasd_kfree_request);
2034 EXPORT_SYMBOL(dasd_kick_device);
2035 EXPORT_SYMBOL(dasd_kmalloc_request);
2036 EXPORT_SYMBOL(dasd_schedule_bh);
2037 EXPORT_SYMBOL(dasd_set_target_state);
2038 EXPORT_SYMBOL(dasd_set_timer);
2039 EXPORT_SYMBOL(dasd_sfree_request);
2040 EXPORT_SYMBOL(dasd_sleep_on);
2041 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2042 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2043 EXPORT_SYMBOL(dasd_smalloc_request);
2044 EXPORT_SYMBOL(dasd_start_IO);
2045 EXPORT_SYMBOL(dasd_term_IO);
2047 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2048 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2049 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2050 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2051 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2052 EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
2055 * Overrides for Emacs so that we follow Linus's tabbing style.
2056 * Emacs will notice this stuff at the end of the file and automatically
2057 * adjust the settings for this buffer only. This must remain at the end
2058 * of the file.
2059 * ---------------------------------------------------------------------------
2060 * Local variables:
2061 * c-indent-level: 4
2062 * c-brace-imaginary-offset: 0
2063 * c-brace-offset: -4
2064 * c-argdecl-indent: 4
2065 * c-label-offset: -4
2066 * c-continued-statement-offset: 4
2067 * c-continued-brace-offset: 0
2068 * indent-tabs-mode: 1
2069 * tab-width: 8
2070 * End: