Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / s390 / block / dasd.c
blob0a807143509f2c4b84a78fda504a0e5a0ff83d57
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/kmod.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/ctype.h>
16 #include <linux/major.h>
17 #include <linux/slab.h>
18 #include <linux/buffer_head.h>
19 #include <linux/hdreg.h>
21 #include <asm/ccwdev.h>
22 #include <asm/ebcdic.h>
23 #include <asm/idals.h>
24 #include <asm/todclk.h>
26 /* This is ugly... */
27 #define PRINTK_HEADER "dasd:"
29 #include "dasd_int.h"
31 * SECTION: Constant definitions to be used within this file
33 #define DASD_CHANQ_MAX_SIZE 4
36 * SECTION: exported variables of dasd.c
38 debug_info_t *dasd_debug_area;
39 struct dasd_discipline *dasd_diag_discipline_pointer;
40 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
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_LICENSE("GPL");
49 * SECTION: prototypes for static functions of dasd.c
51 static int dasd_alloc_queue(struct dasd_block *);
52 static void dasd_setup_queue(struct dasd_block *);
53 static void dasd_free_queue(struct dasd_block *);
54 static void dasd_flush_request_queue(struct dasd_block *);
55 static int dasd_flush_block_queue(struct dasd_block *);
56 static void dasd_device_tasklet(struct dasd_device *);
57 static void dasd_block_tasklet(struct dasd_block *);
58 static void do_kick_device(struct work_struct *);
59 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
62 * SECTION: Operations on the device structure.
64 static wait_queue_head_t dasd_init_waitq;
65 static wait_queue_head_t dasd_flush_wq;
68 * Allocate memory for a new device structure.
70 struct dasd_device *dasd_alloc_device(void)
72 struct dasd_device *device;
74 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
75 if (!device)
76 return ERR_PTR(-ENOMEM);
78 /* Get two pages for normal block device operations. */
79 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
80 if (!device->ccw_mem) {
81 kfree(device);
82 return ERR_PTR(-ENOMEM);
84 /* Get one page for error recovery. */
85 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
86 if (!device->erp_mem) {
87 free_pages((unsigned long) device->ccw_mem, 1);
88 kfree(device);
89 return ERR_PTR(-ENOMEM);
92 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
93 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
94 spin_lock_init(&device->mem_lock);
95 atomic_set(&device->tasklet_scheduled, 0);
96 tasklet_init(&device->tasklet,
97 (void (*)(unsigned long)) dasd_device_tasklet,
98 (unsigned long) device);
99 INIT_LIST_HEAD(&device->ccw_queue);
100 init_timer(&device->timer);
101 INIT_WORK(&device->kick_work, do_kick_device);
102 device->state = DASD_STATE_NEW;
103 device->target = DASD_STATE_NEW;
105 return device;
109 * Free memory of a device structure.
111 void dasd_free_device(struct dasd_device *device)
113 kfree(device->private);
114 free_page((unsigned long) device->erp_mem);
115 free_pages((unsigned long) device->ccw_mem, 1);
116 kfree(device);
120 * Allocate memory for a new device structure.
122 struct dasd_block *dasd_alloc_block(void)
124 struct dasd_block *block;
126 block = kzalloc(sizeof(*block), GFP_ATOMIC);
127 if (!block)
128 return ERR_PTR(-ENOMEM);
129 /* open_count = 0 means device online but not in use */
130 atomic_set(&block->open_count, -1);
132 spin_lock_init(&block->request_queue_lock);
133 atomic_set(&block->tasklet_scheduled, 0);
134 tasklet_init(&block->tasklet,
135 (void (*)(unsigned long)) dasd_block_tasklet,
136 (unsigned long) block);
137 INIT_LIST_HEAD(&block->ccw_queue);
138 spin_lock_init(&block->queue_lock);
139 init_timer(&block->timer);
141 return block;
145 * Free memory of a device structure.
147 void dasd_free_block(struct dasd_block *block)
149 kfree(block);
153 * Make a new device known to the system.
155 static int dasd_state_new_to_known(struct dasd_device *device)
157 int rc;
160 * As long as the device is not in state DASD_STATE_NEW we want to
161 * keep the reference count > 0.
163 dasd_get_device(device);
165 if (device->block) {
166 rc = dasd_alloc_queue(device->block);
167 if (rc) {
168 dasd_put_device(device);
169 return rc;
172 device->state = DASD_STATE_KNOWN;
173 return 0;
177 * Let the system forget about a device.
179 static int dasd_state_known_to_new(struct dasd_device *device)
181 /* Disable extended error reporting for this device. */
182 dasd_eer_disable(device);
183 /* Forget the discipline information. */
184 if (device->discipline) {
185 if (device->discipline->uncheck_device)
186 device->discipline->uncheck_device(device);
187 module_put(device->discipline->owner);
189 device->discipline = NULL;
190 if (device->base_discipline)
191 module_put(device->base_discipline->owner);
192 device->base_discipline = NULL;
193 device->state = DASD_STATE_NEW;
195 if (device->block)
196 dasd_free_queue(device->block);
198 /* Give up reference we took in dasd_state_new_to_known. */
199 dasd_put_device(device);
200 return 0;
204 * Request the irq line for the device.
206 static int dasd_state_known_to_basic(struct dasd_device *device)
208 int rc;
210 /* Allocate and register gendisk structure. */
211 if (device->block) {
212 rc = dasd_gendisk_alloc(device->block);
213 if (rc)
214 return rc;
216 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
217 device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 1,
218 8 * sizeof(long));
219 debug_register_view(device->debug_area, &debug_sprintf_view);
220 debug_set_level(device->debug_area, DBF_WARNING);
221 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
223 device->state = DASD_STATE_BASIC;
224 return 0;
228 * Release the irq line for the device. Terminate any running i/o.
230 static int dasd_state_basic_to_known(struct dasd_device *device)
232 int rc;
233 if (device->block) {
234 dasd_gendisk_free(device->block);
235 dasd_block_clear_timer(device->block);
237 rc = dasd_flush_device_queue(device);
238 if (rc)
239 return rc;
240 dasd_device_clear_timer(device);
242 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
243 if (device->debug_area != NULL) {
244 debug_unregister(device->debug_area);
245 device->debug_area = NULL;
247 device->state = DASD_STATE_KNOWN;
248 return 0;
252 * Do the initial analysis. The do_analysis function may return
253 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
254 * until the discipline decides to continue the startup sequence
255 * by calling the function dasd_change_state. The eckd disciplines
256 * uses this to start a ccw that detects the format. The completion
257 * interrupt for this detection ccw uses the kernel event daemon to
258 * trigger the call to dasd_change_state. All this is done in the
259 * discipline code, see dasd_eckd.c.
260 * After the analysis ccw is done (do_analysis returned 0) the block
261 * device is setup.
262 * In case the analysis returns an error, the device setup is stopped
263 * (a fake disk was already added to allow formatting).
265 static int dasd_state_basic_to_ready(struct dasd_device *device)
267 int rc;
268 struct dasd_block *block;
270 rc = 0;
271 block = device->block;
272 /* make disk known with correct capacity */
273 if (block) {
274 if (block->base->discipline->do_analysis != NULL)
275 rc = block->base->discipline->do_analysis(block);
276 if (rc) {
277 if (rc != -EAGAIN)
278 device->state = DASD_STATE_UNFMT;
279 return rc;
281 dasd_setup_queue(block);
282 set_capacity(block->gdp,
283 block->blocks << block->s2b_shift);
284 device->state = DASD_STATE_READY;
285 rc = dasd_scan_partitions(block);
286 if (rc)
287 device->state = DASD_STATE_BASIC;
288 } else {
289 device->state = DASD_STATE_READY;
291 return rc;
295 * Remove device from block device layer. Destroy dirty buffers.
296 * Forget format information. Check if the target level is basic
297 * and if it is create fake disk for formatting.
299 static int dasd_state_ready_to_basic(struct dasd_device *device)
301 int rc;
303 device->state = DASD_STATE_BASIC;
304 if (device->block) {
305 struct dasd_block *block = device->block;
306 rc = dasd_flush_block_queue(block);
307 if (rc) {
308 device->state = DASD_STATE_READY;
309 return rc;
311 dasd_destroy_partitions(block);
312 dasd_flush_request_queue(block);
313 block->blocks = 0;
314 block->bp_block = 0;
315 block->s2b_shift = 0;
317 return 0;
321 * Back to basic.
323 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
325 device->state = DASD_STATE_BASIC;
326 return 0;
330 * Make the device online and schedule the bottom half to start
331 * the requeueing of requests from the linux request queue to the
332 * ccw queue.
334 static int
335 dasd_state_ready_to_online(struct dasd_device * device)
337 int rc;
339 if (device->discipline->ready_to_online) {
340 rc = device->discipline->ready_to_online(device);
341 if (rc)
342 return rc;
344 device->state = DASD_STATE_ONLINE;
345 if (device->block)
346 dasd_schedule_block_bh(device->block);
347 return 0;
351 * Stop the requeueing of requests again.
353 static int dasd_state_online_to_ready(struct dasd_device *device)
355 int rc;
357 if (device->discipline->online_to_ready) {
358 rc = device->discipline->online_to_ready(device);
359 if (rc)
360 return rc;
362 device->state = DASD_STATE_READY;
363 return 0;
367 * Device startup state changes.
369 static int dasd_increase_state(struct dasd_device *device)
371 int rc;
373 rc = 0;
374 if (device->state == DASD_STATE_NEW &&
375 device->target >= DASD_STATE_KNOWN)
376 rc = dasd_state_new_to_known(device);
378 if (!rc &&
379 device->state == DASD_STATE_KNOWN &&
380 device->target >= DASD_STATE_BASIC)
381 rc = dasd_state_known_to_basic(device);
383 if (!rc &&
384 device->state == DASD_STATE_BASIC &&
385 device->target >= DASD_STATE_READY)
386 rc = dasd_state_basic_to_ready(device);
388 if (!rc &&
389 device->state == DASD_STATE_UNFMT &&
390 device->target > DASD_STATE_UNFMT)
391 rc = -EPERM;
393 if (!rc &&
394 device->state == DASD_STATE_READY &&
395 device->target >= DASD_STATE_ONLINE)
396 rc = dasd_state_ready_to_online(device);
398 return rc;
402 * Device shutdown state changes.
404 static int dasd_decrease_state(struct dasd_device *device)
406 int rc;
408 rc = 0;
409 if (device->state == DASD_STATE_ONLINE &&
410 device->target <= DASD_STATE_READY)
411 rc = dasd_state_online_to_ready(device);
413 if (!rc &&
414 device->state == DASD_STATE_READY &&
415 device->target <= DASD_STATE_BASIC)
416 rc = dasd_state_ready_to_basic(device);
418 if (!rc &&
419 device->state == DASD_STATE_UNFMT &&
420 device->target <= DASD_STATE_BASIC)
421 rc = dasd_state_unfmt_to_basic(device);
423 if (!rc &&
424 device->state == DASD_STATE_BASIC &&
425 device->target <= DASD_STATE_KNOWN)
426 rc = dasd_state_basic_to_known(device);
428 if (!rc &&
429 device->state == DASD_STATE_KNOWN &&
430 device->target <= DASD_STATE_NEW)
431 rc = dasd_state_known_to_new(device);
433 return rc;
437 * This is the main startup/shutdown routine.
439 static void dasd_change_state(struct dasd_device *device)
441 int rc;
443 if (device->state == device->target)
444 /* Already where we want to go today... */
445 return;
446 if (device->state < device->target)
447 rc = dasd_increase_state(device);
448 else
449 rc = dasd_decrease_state(device);
450 if (rc && rc != -EAGAIN)
451 device->target = device->state;
453 if (device->state == device->target)
454 wake_up(&dasd_init_waitq);
456 /* let user-space know that the device status changed */
457 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
461 * Kick starter for devices that did not complete the startup/shutdown
462 * procedure or were sleeping because of a pending state.
463 * dasd_kick_device will schedule a call do do_kick_device to the kernel
464 * event daemon.
466 static void do_kick_device(struct work_struct *work)
468 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
469 dasd_change_state(device);
470 dasd_schedule_device_bh(device);
471 dasd_put_device(device);
474 void dasd_kick_device(struct dasd_device *device)
476 dasd_get_device(device);
477 /* queue call to dasd_kick_device to the kernel event daemon. */
478 schedule_work(&device->kick_work);
482 * Set the target state for a device and starts the state change.
484 void dasd_set_target_state(struct dasd_device *device, int target)
486 /* If we are in probeonly mode stop at DASD_STATE_READY. */
487 if (dasd_probeonly && target > DASD_STATE_READY)
488 target = DASD_STATE_READY;
489 if (device->target != target) {
490 if (device->state == target)
491 wake_up(&dasd_init_waitq);
492 device->target = target;
494 if (device->state != device->target)
495 dasd_change_state(device);
499 * Enable devices with device numbers in [from..to].
501 static inline int _wait_for_device(struct dasd_device *device)
503 return (device->state == device->target);
506 void dasd_enable_device(struct dasd_device *device)
508 dasd_set_target_state(device, DASD_STATE_ONLINE);
509 if (device->state <= DASD_STATE_KNOWN)
510 /* No discipline for device found. */
511 dasd_set_target_state(device, DASD_STATE_NEW);
512 /* Now wait for the devices to come up. */
513 wait_event(dasd_init_waitq, _wait_for_device(device));
517 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
519 #ifdef CONFIG_DASD_PROFILE
521 struct dasd_profile_info_t dasd_global_profile;
522 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
525 * Increments counter in global and local profiling structures.
527 #define dasd_profile_counter(value, counter, block) \
529 int index; \
530 for (index = 0; index < 31 && value >> (2+index); index++); \
531 dasd_global_profile.counter[index]++; \
532 block->profile.counter[index]++; \
536 * Add profiling information for cqr before execution.
538 static void dasd_profile_start(struct dasd_block *block,
539 struct dasd_ccw_req *cqr,
540 struct request *req)
542 struct list_head *l;
543 unsigned int counter;
545 if (dasd_profile_level != DASD_PROFILE_ON)
546 return;
548 /* count the length of the chanq for statistics */
549 counter = 0;
550 list_for_each(l, &block->ccw_queue)
551 if (++counter >= 31)
552 break;
553 dasd_global_profile.dasd_io_nr_req[counter]++;
554 block->profile.dasd_io_nr_req[counter]++;
558 * Add profiling information for cqr after execution.
560 static void dasd_profile_end(struct dasd_block *block,
561 struct dasd_ccw_req *cqr,
562 struct request *req)
564 long strtime, irqtime, endtime, tottime; /* in microseconds */
565 long tottimeps, sectors;
567 if (dasd_profile_level != DASD_PROFILE_ON)
568 return;
570 sectors = req->nr_sectors;
571 if (!cqr->buildclk || !cqr->startclk ||
572 !cqr->stopclk || !cqr->endclk ||
573 !sectors)
574 return;
576 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
577 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
578 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
579 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
580 tottimeps = tottime / sectors;
582 if (!dasd_global_profile.dasd_io_reqs)
583 memset(&dasd_global_profile, 0,
584 sizeof(struct dasd_profile_info_t));
585 dasd_global_profile.dasd_io_reqs++;
586 dasd_global_profile.dasd_io_sects += sectors;
588 if (!block->profile.dasd_io_reqs)
589 memset(&block->profile, 0,
590 sizeof(struct dasd_profile_info_t));
591 block->profile.dasd_io_reqs++;
592 block->profile.dasd_io_sects += sectors;
594 dasd_profile_counter(sectors, dasd_io_secs, block);
595 dasd_profile_counter(tottime, dasd_io_times, block);
596 dasd_profile_counter(tottimeps, dasd_io_timps, block);
597 dasd_profile_counter(strtime, dasd_io_time1, block);
598 dasd_profile_counter(irqtime, dasd_io_time2, block);
599 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
600 dasd_profile_counter(endtime, dasd_io_time3, block);
602 #else
603 #define dasd_profile_start(block, cqr, req) do {} while (0)
604 #define dasd_profile_end(block, cqr, req) do {} while (0)
605 #endif /* CONFIG_DASD_PROFILE */
608 * Allocate memory for a channel program with 'cplength' channel
609 * command words and 'datasize' additional space. There are two
610 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
611 * memory and 2) dasd_smalloc_request uses the static ccw memory
612 * that gets allocated for each device.
614 struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
615 int datasize,
616 struct dasd_device *device)
618 struct dasd_ccw_req *cqr;
620 /* Sanity checks */
621 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
622 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
624 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
625 if (cqr == NULL)
626 return ERR_PTR(-ENOMEM);
627 cqr->cpaddr = NULL;
628 if (cplength > 0) {
629 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
630 GFP_ATOMIC | GFP_DMA);
631 if (cqr->cpaddr == NULL) {
632 kfree(cqr);
633 return ERR_PTR(-ENOMEM);
636 cqr->data = NULL;
637 if (datasize > 0) {
638 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
639 if (cqr->data == NULL) {
640 kfree(cqr->cpaddr);
641 kfree(cqr);
642 return ERR_PTR(-ENOMEM);
645 strncpy((char *) &cqr->magic, magic, 4);
646 ASCEBC((char *) &cqr->magic, 4);
647 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
648 dasd_get_device(device);
649 return cqr;
652 struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
653 int datasize,
654 struct dasd_device *device)
656 unsigned long flags;
657 struct dasd_ccw_req *cqr;
658 char *data;
659 int size;
661 /* Sanity checks */
662 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
663 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
665 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
666 if (cplength > 0)
667 size += cplength * sizeof(struct ccw1);
668 if (datasize > 0)
669 size += datasize;
670 spin_lock_irqsave(&device->mem_lock, flags);
671 cqr = (struct dasd_ccw_req *)
672 dasd_alloc_chunk(&device->ccw_chunks, size);
673 spin_unlock_irqrestore(&device->mem_lock, flags);
674 if (cqr == NULL)
675 return ERR_PTR(-ENOMEM);
676 memset(cqr, 0, sizeof(struct dasd_ccw_req));
677 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
678 cqr->cpaddr = NULL;
679 if (cplength > 0) {
680 cqr->cpaddr = (struct ccw1 *) data;
681 data += cplength*sizeof(struct ccw1);
682 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
684 cqr->data = NULL;
685 if (datasize > 0) {
686 cqr->data = data;
687 memset(cqr->data, 0, datasize);
689 strncpy((char *) &cqr->magic, magic, 4);
690 ASCEBC((char *) &cqr->magic, 4);
691 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
692 dasd_get_device(device);
693 return cqr;
697 * Free memory of a channel program. This function needs to free all the
698 * idal lists that might have been created by dasd_set_cda and the
699 * struct dasd_ccw_req itself.
701 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
703 #ifdef CONFIG_64BIT
704 struct ccw1 *ccw;
706 /* Clear any idals used for the request. */
707 ccw = cqr->cpaddr;
708 do {
709 clear_normalized_cda(ccw);
710 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
711 #endif
712 kfree(cqr->cpaddr);
713 kfree(cqr->data);
714 kfree(cqr);
715 dasd_put_device(device);
718 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
720 unsigned long flags;
722 spin_lock_irqsave(&device->mem_lock, flags);
723 dasd_free_chunk(&device->ccw_chunks, cqr);
724 spin_unlock_irqrestore(&device->mem_lock, flags);
725 dasd_put_device(device);
729 * Check discipline magic in cqr.
731 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
733 struct dasd_device *device;
735 if (cqr == NULL)
736 return -EINVAL;
737 device = cqr->startdev;
738 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
739 DEV_MESSAGE(KERN_WARNING, device,
740 " dasd_ccw_req 0x%08x magic doesn't match"
741 " discipline 0x%08x",
742 cqr->magic,
743 *(unsigned int *) device->discipline->name);
744 return -EINVAL;
746 return 0;
750 * Terminate the current i/o and set the request to clear_pending.
751 * Timer keeps device runnig.
752 * ccw_device_clear can fail if the i/o subsystem
753 * is in a bad mood.
755 int dasd_term_IO(struct dasd_ccw_req *cqr)
757 struct dasd_device *device;
758 int retries, rc;
760 /* Check the cqr */
761 rc = dasd_check_cqr(cqr);
762 if (rc)
763 return rc;
764 retries = 0;
765 device = (struct dasd_device *) cqr->startdev;
766 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
767 rc = ccw_device_clear(device->cdev, (long) cqr);
768 switch (rc) {
769 case 0: /* termination successful */
770 cqr->retries--;
771 cqr->status = DASD_CQR_CLEAR_PENDING;
772 cqr->stopclk = get_clock();
773 cqr->starttime = 0;
774 DBF_DEV_EVENT(DBF_DEBUG, device,
775 "terminate cqr %p successful",
776 cqr);
777 break;
778 case -ENODEV:
779 DBF_DEV_EVENT(DBF_ERR, device, "%s",
780 "device gone, retry");
781 break;
782 case -EIO:
783 DBF_DEV_EVENT(DBF_ERR, device, "%s",
784 "I/O error, retry");
785 break;
786 case -EINVAL:
787 case -EBUSY:
788 DBF_DEV_EVENT(DBF_ERR, device, "%s",
789 "device busy, retry later");
790 break;
791 default:
792 DEV_MESSAGE(KERN_ERR, device,
793 "line %d unknown RC=%d, please "
794 "report to linux390@de.ibm.com",
795 __LINE__, rc);
796 BUG();
797 break;
799 retries++;
801 dasd_schedule_device_bh(device);
802 return rc;
806 * Start the i/o. This start_IO can fail if the channel is really busy.
807 * In that case set up a timer to start the request later.
809 int dasd_start_IO(struct dasd_ccw_req *cqr)
811 struct dasd_device *device;
812 int rc;
814 /* Check the cqr */
815 rc = dasd_check_cqr(cqr);
816 if (rc)
817 return rc;
818 device = (struct dasd_device *) cqr->startdev;
819 if (cqr->retries < 0) {
820 DEV_MESSAGE(KERN_DEBUG, device,
821 "start_IO: request %p (%02x/%i) - no retry left.",
822 cqr, cqr->status, cqr->retries);
823 cqr->status = DASD_CQR_ERROR;
824 return -EIO;
826 cqr->startclk = get_clock();
827 cqr->starttime = jiffies;
828 cqr->retries--;
829 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
830 cqr->lpm, 0);
831 switch (rc) {
832 case 0:
833 cqr->status = DASD_CQR_IN_IO;
834 DBF_DEV_EVENT(DBF_DEBUG, device,
835 "start_IO: request %p started successful",
836 cqr);
837 break;
838 case -EBUSY:
839 DBF_DEV_EVENT(DBF_ERR, device, "%s",
840 "start_IO: device busy, retry later");
841 break;
842 case -ETIMEDOUT:
843 DBF_DEV_EVENT(DBF_ERR, device, "%s",
844 "start_IO: request timeout, retry later");
845 break;
846 case -EACCES:
847 /* -EACCES indicates that the request used only a
848 * subset of the available pathes and all these
849 * pathes are gone.
850 * Do a retry with all available pathes.
852 cqr->lpm = LPM_ANYPATH;
853 DBF_DEV_EVENT(DBF_ERR, device, "%s",
854 "start_IO: selected pathes gone,"
855 " retry on all pathes");
856 break;
857 case -ENODEV:
858 case -EIO:
859 DBF_DEV_EVENT(DBF_ERR, device, "%s",
860 "start_IO: device gone, retry");
861 break;
862 default:
863 DEV_MESSAGE(KERN_ERR, device,
864 "line %d unknown RC=%d, please report"
865 " to linux390@de.ibm.com", __LINE__, rc);
866 BUG();
867 break;
869 return rc;
873 * Timeout function for dasd devices. This is used for different purposes
874 * 1) missing interrupt handler for normal operation
875 * 2) delayed start of request where start_IO failed with -EBUSY
876 * 3) timeout for missing state change interrupts
877 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
878 * DASD_CQR_QUEUED for 2) and 3).
880 static void dasd_device_timeout(unsigned long ptr)
882 unsigned long flags;
883 struct dasd_device *device;
885 device = (struct dasd_device *) ptr;
886 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
887 /* re-activate request queue */
888 device->stopped &= ~DASD_STOPPED_PENDING;
889 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
890 dasd_schedule_device_bh(device);
894 * Setup timeout for a device in jiffies.
896 void dasd_device_set_timer(struct dasd_device *device, int expires)
898 if (expires == 0) {
899 if (timer_pending(&device->timer))
900 del_timer(&device->timer);
901 return;
903 if (timer_pending(&device->timer)) {
904 if (mod_timer(&device->timer, jiffies + expires))
905 return;
907 device->timer.function = dasd_device_timeout;
908 device->timer.data = (unsigned long) device;
909 device->timer.expires = jiffies + expires;
910 add_timer(&device->timer);
914 * Clear timeout for a device.
916 void dasd_device_clear_timer(struct dasd_device *device)
918 if (timer_pending(&device->timer))
919 del_timer(&device->timer);
922 static void dasd_handle_killed_request(struct ccw_device *cdev,
923 unsigned long intparm)
925 struct dasd_ccw_req *cqr;
926 struct dasd_device *device;
928 cqr = (struct dasd_ccw_req *) intparm;
929 if (cqr->status != DASD_CQR_IN_IO) {
930 MESSAGE(KERN_DEBUG,
931 "invalid status in handle_killed_request: "
932 "bus_id %s, status %02x",
933 cdev->dev.bus_id, cqr->status);
934 return;
937 device = (struct dasd_device *) cqr->startdev;
938 if (device == NULL ||
939 device != dasd_device_from_cdev_locked(cdev) ||
940 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
941 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
942 cdev->dev.bus_id);
943 return;
946 /* Schedule request to be retried. */
947 cqr->status = DASD_CQR_QUEUED;
949 dasd_device_clear_timer(device);
950 dasd_schedule_device_bh(device);
951 dasd_put_device(device);
954 void dasd_generic_handle_state_change(struct dasd_device *device)
956 /* First of all start sense subsystem status request. */
957 dasd_eer_snss(device);
959 device->stopped &= ~DASD_STOPPED_PENDING;
960 dasd_schedule_device_bh(device);
961 if (device->block)
962 dasd_schedule_block_bh(device->block);
966 * Interrupt handler for "normal" ssch-io based dasd devices.
968 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
969 struct irb *irb)
971 struct dasd_ccw_req *cqr, *next;
972 struct dasd_device *device;
973 unsigned long long now;
974 int expires;
976 if (IS_ERR(irb)) {
977 switch (PTR_ERR(irb)) {
978 case -EIO:
979 dasd_handle_killed_request(cdev, intparm);
980 break;
981 case -ETIMEDOUT:
982 printk(KERN_WARNING"%s(%s): request timed out\n",
983 __FUNCTION__, cdev->dev.bus_id);
984 //FIXME - dasd uses own timeout interface...
985 break;
986 default:
987 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
988 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
990 return;
993 now = get_clock();
995 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
996 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
997 (unsigned int) intparm);
999 /* check for unsolicited interrupts */
1000 cqr = (struct dasd_ccw_req *) intparm;
1001 if (!cqr || ((irb->scsw.cc == 1) &&
1002 (irb->scsw.fctl & SCSW_FCTL_START_FUNC) &&
1003 (irb->scsw.stctl & SCSW_STCTL_STATUS_PEND)) ) {
1004 if (cqr && cqr->status == DASD_CQR_IN_IO)
1005 cqr->status = DASD_CQR_QUEUED;
1006 device = dasd_device_from_cdev_locked(cdev);
1007 if (!IS_ERR(device)) {
1008 dasd_device_clear_timer(device);
1009 device->discipline->handle_unsolicited_interrupt(device,
1010 irb);
1011 dasd_put_device(device);
1013 return;
1016 device = (struct dasd_device *) cqr->startdev;
1017 if (!device ||
1018 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1019 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
1020 cdev->dev.bus_id);
1021 return;
1024 /* Check for clear pending */
1025 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1026 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1027 cqr->status = DASD_CQR_CLEARED;
1028 dasd_device_clear_timer(device);
1029 wake_up(&dasd_flush_wq);
1030 dasd_schedule_device_bh(device);
1031 return;
1034 /* check status - the request might have been killed by dyn detach */
1035 if (cqr->status != DASD_CQR_IN_IO) {
1036 MESSAGE(KERN_DEBUG,
1037 "invalid status: bus_id %s, status %02x",
1038 cdev->dev.bus_id, cqr->status);
1039 return;
1041 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
1042 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
1043 next = NULL;
1044 expires = 0;
1045 if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1046 irb->scsw.cstat == 0 && !irb->esw.esw0.erw.cons) {
1047 /* request was completed successfully */
1048 cqr->status = DASD_CQR_SUCCESS;
1049 cqr->stopclk = now;
1050 /* Start first request on queue if possible -> fast_io. */
1051 if (cqr->devlist.next != &device->ccw_queue) {
1052 next = list_entry(cqr->devlist.next,
1053 struct dasd_ccw_req, devlist);
1055 } else { /* error */
1056 memcpy(&cqr->irb, irb, sizeof(struct irb));
1057 if (device->features & DASD_FEATURE_ERPLOG) {
1058 dasd_log_sense(cqr, irb);
1061 * If we don't want complex ERP for this request, then just
1062 * reset this and retry it in the fastpath
1064 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1065 cqr->retries > 0) {
1066 DEV_MESSAGE(KERN_DEBUG, device,
1067 "default ERP in fastpath (%i retries left)",
1068 cqr->retries);
1069 cqr->lpm = LPM_ANYPATH;
1070 cqr->status = DASD_CQR_QUEUED;
1071 next = cqr;
1072 } else
1073 cqr->status = DASD_CQR_ERROR;
1075 if (next && (next->status == DASD_CQR_QUEUED) &&
1076 (!device->stopped)) {
1077 if (device->discipline->start_IO(next) == 0)
1078 expires = next->expires;
1079 else
1080 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1081 "Interrupt fastpath "
1082 "failed!");
1084 if (expires != 0)
1085 dasd_device_set_timer(device, expires);
1086 else
1087 dasd_device_clear_timer(device);
1088 dasd_schedule_device_bh(device);
1092 * If we have an error on a dasd_block layer request then we cancel
1093 * and return all further requests from the same dasd_block as well.
1095 static void __dasd_device_recovery(struct dasd_device *device,
1096 struct dasd_ccw_req *ref_cqr)
1098 struct list_head *l, *n;
1099 struct dasd_ccw_req *cqr;
1102 * only requeue request that came from the dasd_block layer
1104 if (!ref_cqr->block)
1105 return;
1107 list_for_each_safe(l, n, &device->ccw_queue) {
1108 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1109 if (cqr->status == DASD_CQR_QUEUED &&
1110 ref_cqr->block == cqr->block) {
1111 cqr->status = DASD_CQR_CLEARED;
1117 * Remove those ccw requests from the queue that need to be returned
1118 * to the upper layer.
1120 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1121 struct list_head *final_queue)
1123 struct list_head *l, *n;
1124 struct dasd_ccw_req *cqr;
1126 /* Process request with final status. */
1127 list_for_each_safe(l, n, &device->ccw_queue) {
1128 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1130 /* Stop list processing at the first non-final request. */
1131 if (cqr->status == DASD_CQR_QUEUED ||
1132 cqr->status == DASD_CQR_IN_IO ||
1133 cqr->status == DASD_CQR_CLEAR_PENDING)
1134 break;
1135 if (cqr->status == DASD_CQR_ERROR) {
1136 __dasd_device_recovery(device, cqr);
1138 /* Rechain finished requests to final queue */
1139 list_move_tail(&cqr->devlist, final_queue);
1144 * the cqrs from the final queue are returned to the upper layer
1145 * by setting a dasd_block state and calling the callback function
1147 static void __dasd_device_process_final_queue(struct dasd_device *device,
1148 struct list_head *final_queue)
1150 struct list_head *l, *n;
1151 struct dasd_ccw_req *cqr;
1152 <<<<<<< HEAD:drivers/s390/block/dasd.c
1153 =======
1154 struct dasd_block *block;
1155 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/s390/block/dasd.c
1157 list_for_each_safe(l, n, final_queue) {
1158 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1159 list_del_init(&cqr->devlist);
1160 <<<<<<< HEAD:drivers/s390/block/dasd.c
1161 if (cqr->block)
1162 spin_lock_bh(&cqr->block->queue_lock);
1163 =======
1164 block = cqr->block;
1165 if (block)
1166 spin_lock_bh(&block->queue_lock);
1167 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/s390/block/dasd.c
1168 switch (cqr->status) {
1169 case DASD_CQR_SUCCESS:
1170 cqr->status = DASD_CQR_DONE;
1171 break;
1172 case DASD_CQR_ERROR:
1173 cqr->status = DASD_CQR_NEED_ERP;
1174 break;
1175 case DASD_CQR_CLEARED:
1176 cqr->status = DASD_CQR_TERMINATED;
1177 break;
1178 default:
1179 DEV_MESSAGE(KERN_ERR, device,
1180 "wrong cqr status in __dasd_process_final_queue "
1181 "for cqr %p, status %x",
1182 cqr, cqr->status);
1183 BUG();
1185 <<<<<<< HEAD:drivers/s390/block/dasd.c
1186 if (cqr->block)
1187 spin_unlock_bh(&cqr->block->queue_lock);
1188 =======
1189 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/s390/block/dasd.c
1190 if (cqr->callback != NULL)
1191 (cqr->callback)(cqr, cqr->callback_data);
1192 <<<<<<< HEAD:drivers/s390/block/dasd.c
1193 =======
1194 if (block)
1195 spin_unlock_bh(&block->queue_lock);
1196 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/s390/block/dasd.c
1200 <<<<<<< HEAD:drivers/s390/block/dasd.c
1203 =======
1204 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/s390/block/dasd.c
1206 * Take a look at the first request on the ccw queue and check
1207 * if it reached its expire time. If so, terminate the IO.
1209 static void __dasd_device_check_expire(struct dasd_device *device)
1211 struct dasd_ccw_req *cqr;
1213 if (list_empty(&device->ccw_queue))
1214 return;
1215 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1216 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1217 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1218 if (device->discipline->term_IO(cqr) != 0) {
1219 /* Hmpf, try again in 5 sec */
1220 DEV_MESSAGE(KERN_ERR, device,
1221 "internal error - timeout (%is) expired "
1222 "for cqr %p, termination failed, "
1223 "retrying in 5s",
1224 (cqr->expires/HZ), cqr);
1225 cqr->expires += 5*HZ;
1226 dasd_device_set_timer(device, 5*HZ);
1227 } else {
1228 DEV_MESSAGE(KERN_ERR, device,
1229 "internal error - timeout (%is) expired "
1230 "for cqr %p (%i retries left)",
1231 (cqr->expires/HZ), cqr, cqr->retries);
1237 * Take a look at the first request on the ccw queue and check
1238 * if it needs to be started.
1240 static void __dasd_device_start_head(struct dasd_device *device)
1242 struct dasd_ccw_req *cqr;
1243 int rc;
1245 if (list_empty(&device->ccw_queue))
1246 return;
1247 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1248 if (cqr->status != DASD_CQR_QUEUED)
1249 return;
1250 /* when device is stopped, return request to previous layer */
1251 if (device->stopped) {
1252 cqr->status = DASD_CQR_CLEARED;
1253 dasd_schedule_device_bh(device);
1254 return;
1257 rc = device->discipline->start_IO(cqr);
1258 if (rc == 0)
1259 dasd_device_set_timer(device, cqr->expires);
1260 else if (rc == -EACCES) {
1261 dasd_schedule_device_bh(device);
1262 } else
1263 /* Hmpf, try again in 1/2 sec */
1264 dasd_device_set_timer(device, 50);
1268 * Go through all request on the dasd_device request queue,
1269 * terminate them on the cdev if necessary, and return them to the
1270 * submitting layer via callback.
1271 * Note:
1272 * Make sure that all 'submitting layers' still exist when
1273 * this function is called!. In other words, when 'device' is a base
1274 * device then all block layer requests must have been removed before
1275 * via dasd_flush_block_queue.
1277 int dasd_flush_device_queue(struct dasd_device *device)
1279 struct dasd_ccw_req *cqr, *n;
1280 int rc;
1281 struct list_head flush_queue;
1283 INIT_LIST_HEAD(&flush_queue);
1284 spin_lock_irq(get_ccwdev_lock(device->cdev));
1285 rc = 0;
1286 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1287 /* Check status and move request to flush_queue */
1288 switch (cqr->status) {
1289 case DASD_CQR_IN_IO:
1290 rc = device->discipline->term_IO(cqr);
1291 if (rc) {
1292 /* unable to terminate requeust */
1293 DEV_MESSAGE(KERN_ERR, device,
1294 "dasd flush ccw_queue is unable "
1295 " to terminate request %p",
1296 cqr);
1297 /* stop flush processing */
1298 goto finished;
1300 break;
1301 case DASD_CQR_QUEUED:
1302 cqr->stopclk = get_clock();
1303 cqr->status = DASD_CQR_CLEARED;
1304 break;
1305 default: /* no need to modify the others */
1306 break;
1308 list_move_tail(&cqr->devlist, &flush_queue);
1310 finished:
1311 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1313 * After this point all requests must be in state CLEAR_PENDING,
1314 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1315 * one of the others.
1317 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1318 wait_event(dasd_flush_wq,
1319 (cqr->status != DASD_CQR_CLEAR_PENDING));
1321 * Now set each request back to TERMINATED, DONE or NEED_ERP
1322 * and call the callback function of flushed requests
1324 __dasd_device_process_final_queue(device, &flush_queue);
1325 return rc;
1329 * Acquire the device lock and process queues for the device.
1331 static void dasd_device_tasklet(struct dasd_device *device)
1333 struct list_head final_queue;
1335 atomic_set (&device->tasklet_scheduled, 0);
1336 INIT_LIST_HEAD(&final_queue);
1337 spin_lock_irq(get_ccwdev_lock(device->cdev));
1338 /* Check expire time of first request on the ccw queue. */
1339 __dasd_device_check_expire(device);
1340 /* find final requests on ccw queue */
1341 __dasd_device_process_ccw_queue(device, &final_queue);
1342 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1343 /* Now call the callback function of requests with final status */
1344 __dasd_device_process_final_queue(device, &final_queue);
1345 spin_lock_irq(get_ccwdev_lock(device->cdev));
1346 /* Now check if the head of the ccw queue needs to be started. */
1347 __dasd_device_start_head(device);
1348 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1349 dasd_put_device(device);
1353 * Schedules a call to dasd_tasklet over the device tasklet.
1355 void dasd_schedule_device_bh(struct dasd_device *device)
1357 /* Protect against rescheduling. */
1358 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1359 return;
1360 dasd_get_device(device);
1361 tasklet_hi_schedule(&device->tasklet);
1365 * Queue a request to the head of the device ccw_queue.
1366 * Start the I/O if possible.
1368 void dasd_add_request_head(struct dasd_ccw_req *cqr)
1370 struct dasd_device *device;
1371 unsigned long flags;
1373 device = cqr->startdev;
1374 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1375 cqr->status = DASD_CQR_QUEUED;
1376 list_add(&cqr->devlist, &device->ccw_queue);
1377 /* let the bh start the request to keep them in order */
1378 dasd_schedule_device_bh(device);
1379 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1383 * Queue a request to the tail of the device ccw_queue.
1384 * Start the I/O if possible.
1386 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1388 struct dasd_device *device;
1389 unsigned long flags;
1391 device = cqr->startdev;
1392 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1393 cqr->status = DASD_CQR_QUEUED;
1394 list_add_tail(&cqr->devlist, &device->ccw_queue);
1395 /* let the bh start the request to keep them in order */
1396 dasd_schedule_device_bh(device);
1397 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1401 * Wakeup helper for the 'sleep_on' functions.
1403 static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1405 wake_up((wait_queue_head_t *) data);
1408 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1410 struct dasd_device *device;
1411 int rc;
1413 device = cqr->startdev;
1414 spin_lock_irq(get_ccwdev_lock(device->cdev));
1415 rc = ((cqr->status == DASD_CQR_DONE ||
1416 cqr->status == DASD_CQR_NEED_ERP ||
1417 cqr->status == DASD_CQR_TERMINATED) &&
1418 list_empty(&cqr->devlist));
1419 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1420 return rc;
1424 * Queue a request to the tail of the device ccw_queue and wait for
1425 * it's completion.
1427 int dasd_sleep_on(struct dasd_ccw_req *cqr)
1429 wait_queue_head_t wait_q;
1430 struct dasd_device *device;
1431 int rc;
1433 device = cqr->startdev;
1435 init_waitqueue_head (&wait_q);
1436 cqr->callback = dasd_wakeup_cb;
1437 cqr->callback_data = (void *) &wait_q;
1438 dasd_add_request_tail(cqr);
1439 wait_event(wait_q, _wait_for_wakeup(cqr));
1441 /* Request status is either done or failed. */
1442 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1443 return rc;
1447 * Queue a request to the tail of the device ccw_queue and wait
1448 * interruptible for it's completion.
1450 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1452 wait_queue_head_t wait_q;
1453 struct dasd_device *device;
1454 int rc;
1456 device = cqr->startdev;
1457 init_waitqueue_head (&wait_q);
1458 cqr->callback = dasd_wakeup_cb;
1459 cqr->callback_data = (void *) &wait_q;
1460 dasd_add_request_tail(cqr);
1461 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1462 if (rc == -ERESTARTSYS) {
1463 dasd_cancel_req(cqr);
1464 /* wait (non-interruptible) for final status */
1465 wait_event(wait_q, _wait_for_wakeup(cqr));
1467 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1468 return rc;
1472 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1473 * for eckd devices) the currently running request has to be terminated
1474 * and be put back to status queued, before the special request is added
1475 * to the head of the queue. Then the special request is waited on normally.
1477 static inline int _dasd_term_running_cqr(struct dasd_device *device)
1479 struct dasd_ccw_req *cqr;
1481 if (list_empty(&device->ccw_queue))
1482 return 0;
1483 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1484 return device->discipline->term_IO(cqr);
1487 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1489 wait_queue_head_t wait_q;
1490 struct dasd_device *device;
1491 int rc;
1493 device = cqr->startdev;
1494 spin_lock_irq(get_ccwdev_lock(device->cdev));
1495 rc = _dasd_term_running_cqr(device);
1496 if (rc) {
1497 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1498 return rc;
1501 init_waitqueue_head (&wait_q);
1502 cqr->callback = dasd_wakeup_cb;
1503 cqr->callback_data = (void *) &wait_q;
1504 cqr->status = DASD_CQR_QUEUED;
1505 list_add(&cqr->devlist, &device->ccw_queue);
1507 /* let the bh start the request to keep them in order */
1508 dasd_schedule_device_bh(device);
1510 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1512 wait_event(wait_q, _wait_for_wakeup(cqr));
1514 /* Request status is either done or failed. */
1515 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1516 return rc;
1520 * Cancels a request that was started with dasd_sleep_on_req.
1521 * This is useful to timeout requests. The request will be
1522 * terminated if it is currently in i/o.
1523 * Returns 1 if the request has been terminated.
1524 * 0 if there was no need to terminate the request (not started yet)
1525 * negative error code if termination failed
1526 * Cancellation of a request is an asynchronous operation! The calling
1527 * function has to wait until the request is properly returned via callback.
1529 int dasd_cancel_req(struct dasd_ccw_req *cqr)
1531 struct dasd_device *device = cqr->startdev;
1532 unsigned long flags;
1533 int rc;
1535 rc = 0;
1536 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1537 switch (cqr->status) {
1538 case DASD_CQR_QUEUED:
1539 /* request was not started - just set to cleared */
1540 cqr->status = DASD_CQR_CLEARED;
1541 break;
1542 case DASD_CQR_IN_IO:
1543 /* request in IO - terminate IO and release again */
1544 rc = device->discipline->term_IO(cqr);
1545 if (rc) {
1546 DEV_MESSAGE(KERN_ERR, device,
1547 "dasd_cancel_req is unable "
1548 " to terminate request %p, rc = %d",
1549 cqr, rc);
1550 } else {
1551 cqr->stopclk = get_clock();
1552 rc = 1;
1554 break;
1555 default: /* already finished or clear pending - do nothing */
1556 break;
1558 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1559 dasd_schedule_device_bh(device);
1560 return rc;
1565 * SECTION: Operations of the dasd_block layer.
1569 * Timeout function for dasd_block. This is used when the block layer
1570 * is waiting for something that may not come reliably, (e.g. a state
1571 * change interrupt)
1573 static void dasd_block_timeout(unsigned long ptr)
1575 unsigned long flags;
1576 struct dasd_block *block;
1578 block = (struct dasd_block *) ptr;
1579 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1580 /* re-activate request queue */
1581 block->base->stopped &= ~DASD_STOPPED_PENDING;
1582 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1583 dasd_schedule_block_bh(block);
1587 * Setup timeout for a dasd_block in jiffies.
1589 void dasd_block_set_timer(struct dasd_block *block, int expires)
1591 if (expires == 0) {
1592 if (timer_pending(&block->timer))
1593 del_timer(&block->timer);
1594 return;
1596 if (timer_pending(&block->timer)) {
1597 if (mod_timer(&block->timer, jiffies + expires))
1598 return;
1600 block->timer.function = dasd_block_timeout;
1601 block->timer.data = (unsigned long) block;
1602 block->timer.expires = jiffies + expires;
1603 add_timer(&block->timer);
1607 * Clear timeout for a dasd_block.
1609 void dasd_block_clear_timer(struct dasd_block *block)
1611 if (timer_pending(&block->timer))
1612 del_timer(&block->timer);
1616 * posts the buffer_cache about a finalized request
1618 static inline void dasd_end_request(struct request *req, int error)
1620 if (__blk_end_request(req, error, blk_rq_bytes(req)))
1621 BUG();
1625 * Process finished error recovery ccw.
1627 static inline void __dasd_block_process_erp(struct dasd_block *block,
1628 struct dasd_ccw_req *cqr)
1630 dasd_erp_fn_t erp_fn;
1631 struct dasd_device *device = block->base;
1633 if (cqr->status == DASD_CQR_DONE)
1634 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1635 else
1636 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1637 erp_fn = device->discipline->erp_postaction(cqr);
1638 erp_fn(cqr);
1642 * Fetch requests from the block device queue.
1644 static void __dasd_process_request_queue(struct dasd_block *block)
1646 struct request_queue *queue;
1647 struct request *req;
1648 struct dasd_ccw_req *cqr;
1649 struct dasd_device *basedev;
1650 unsigned long flags;
1651 queue = block->request_queue;
1652 basedev = block->base;
1653 /* No queue ? Then there is nothing to do. */
1654 if (queue == NULL)
1655 return;
1658 * We requeue request from the block device queue to the ccw
1659 * queue only in two states. In state DASD_STATE_READY the
1660 * partition detection is done and we need to requeue requests
1661 * for that. State DASD_STATE_ONLINE is normal block device
1662 * operation.
1664 if (basedev->state < DASD_STATE_READY)
1665 return;
1666 /* Now we try to fetch requests from the request queue */
1667 while (!blk_queue_plugged(queue) &&
1668 elv_next_request(queue)) {
1670 req = elv_next_request(queue);
1672 if (basedev->features & DASD_FEATURE_READONLY &&
1673 rq_data_dir(req) == WRITE) {
1674 DBF_DEV_EVENT(DBF_ERR, basedev,
1675 "Rejecting write request %p",
1676 req);
1677 blkdev_dequeue_request(req);
1678 dasd_end_request(req, -EIO);
1679 continue;
1681 cqr = basedev->discipline->build_cp(basedev, block, req);
1682 if (IS_ERR(cqr)) {
1683 if (PTR_ERR(cqr) == -EBUSY)
1684 break; /* normal end condition */
1685 if (PTR_ERR(cqr) == -ENOMEM)
1686 break; /* terminate request queue loop */
1687 if (PTR_ERR(cqr) == -EAGAIN) {
1689 * The current request cannot be build right
1690 * now, we have to try later. If this request
1691 * is the head-of-queue we stop the device
1692 * for 1/2 second.
1694 if (!list_empty(&block->ccw_queue))
1695 break;
1696 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1697 basedev->stopped |= DASD_STOPPED_PENDING;
1698 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1699 dasd_block_set_timer(block, HZ/2);
1700 break;
1702 DBF_DEV_EVENT(DBF_ERR, basedev,
1703 "CCW creation failed (rc=%ld) "
1704 "on request %p",
1705 PTR_ERR(cqr), req);
1706 blkdev_dequeue_request(req);
1707 dasd_end_request(req, -EIO);
1708 continue;
1711 * Note: callback is set to dasd_return_cqr_cb in
1712 * __dasd_block_start_head to cover erp requests as well
1714 cqr->callback_data = (void *) req;
1715 cqr->status = DASD_CQR_FILLED;
1716 blkdev_dequeue_request(req);
1717 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1718 dasd_profile_start(block, cqr, req);
1722 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1724 struct request *req;
1725 int status;
1726 int error = 0;
1728 req = (struct request *) cqr->callback_data;
1729 dasd_profile_end(cqr->block, cqr, req);
1730 status = cqr->block->base->discipline->free_cp(cqr, req);
1731 if (status <= 0)
1732 error = status ? status : -EIO;
1733 dasd_end_request(req, error);
1737 * Process ccw request queue.
1739 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1740 struct list_head *final_queue)
1742 struct list_head *l, *n;
1743 struct dasd_ccw_req *cqr;
1744 dasd_erp_fn_t erp_fn;
1745 unsigned long flags;
1746 struct dasd_device *base = block->base;
1748 restart:
1749 /* Process request with final status. */
1750 list_for_each_safe(l, n, &block->ccw_queue) {
1751 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1752 if (cqr->status != DASD_CQR_DONE &&
1753 cqr->status != DASD_CQR_FAILED &&
1754 cqr->status != DASD_CQR_NEED_ERP &&
1755 cqr->status != DASD_CQR_TERMINATED)
1756 continue;
1758 if (cqr->status == DASD_CQR_TERMINATED) {
1759 base->discipline->handle_terminated_request(cqr);
1760 goto restart;
1763 /* Process requests that may be recovered */
1764 if (cqr->status == DASD_CQR_NEED_ERP) {
1765 erp_fn = base->discipline->erp_action(cqr);
1766 erp_fn(cqr);
1767 goto restart;
1770 /* First of all call extended error reporting. */
1771 if (dasd_eer_enabled(base) &&
1772 cqr->status == DASD_CQR_FAILED) {
1773 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1775 /* restart request */
1776 cqr->status = DASD_CQR_FILLED;
1777 cqr->retries = 255;
1778 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1779 base->stopped |= DASD_STOPPED_QUIESCE;
1780 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1781 flags);
1782 goto restart;
1785 /* Process finished ERP request. */
1786 if (cqr->refers) {
1787 __dasd_block_process_erp(block, cqr);
1788 goto restart;
1791 /* Rechain finished requests to final queue */
1792 cqr->endclk = get_clock();
1793 list_move_tail(&cqr->blocklist, final_queue);
1797 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1799 dasd_schedule_block_bh(cqr->block);
1802 static void __dasd_block_start_head(struct dasd_block *block)
1804 struct dasd_ccw_req *cqr;
1806 if (list_empty(&block->ccw_queue))
1807 return;
1808 /* We allways begin with the first requests on the queue, as some
1809 * of previously started requests have to be enqueued on a
1810 * dasd_device again for error recovery.
1812 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1813 if (cqr->status != DASD_CQR_FILLED)
1814 continue;
1815 /* Non-temporary stop condition will trigger fail fast */
1816 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1817 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1818 (!dasd_eer_enabled(block->base))) {
1819 cqr->status = DASD_CQR_FAILED;
1820 dasd_schedule_block_bh(block);
1821 continue;
1823 /* Don't try to start requests if device is stopped */
1824 if (block->base->stopped)
1825 return;
1827 /* just a fail safe check, should not happen */
1828 if (!cqr->startdev)
1829 cqr->startdev = block->base;
1831 /* make sure that the requests we submit find their way back */
1832 cqr->callback = dasd_return_cqr_cb;
1834 dasd_add_request_tail(cqr);
1839 * Central dasd_block layer routine. Takes requests from the generic
1840 * block layer request queue, creates ccw requests, enqueues them on
1841 * a dasd_device and processes ccw requests that have been returned.
1843 static void dasd_block_tasklet(struct dasd_block *block)
1845 struct list_head final_queue;
1846 struct list_head *l, *n;
1847 struct dasd_ccw_req *cqr;
1849 atomic_set(&block->tasklet_scheduled, 0);
1850 INIT_LIST_HEAD(&final_queue);
1851 spin_lock(&block->queue_lock);
1852 /* Finish off requests on ccw queue */
1853 __dasd_process_block_ccw_queue(block, &final_queue);
1854 spin_unlock(&block->queue_lock);
1855 /* Now call the callback function of requests with final status */
1856 spin_lock_irq(&block->request_queue_lock);
1857 list_for_each_safe(l, n, &final_queue) {
1858 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1859 list_del_init(&cqr->blocklist);
1860 __dasd_cleanup_cqr(cqr);
1862 spin_lock(&block->queue_lock);
1863 /* Get new request from the block device request queue */
1864 __dasd_process_request_queue(block);
1865 /* Now check if the head of the ccw queue needs to be started. */
1866 __dasd_block_start_head(block);
1867 spin_unlock(&block->queue_lock);
1868 spin_unlock_irq(&block->request_queue_lock);
1869 dasd_put_device(block->base);
1872 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1874 wake_up(&dasd_flush_wq);
1878 * Go through all request on the dasd_block request queue, cancel them
1879 * on the respective dasd_device, and return them to the generic
1880 * block layer.
1882 static int dasd_flush_block_queue(struct dasd_block *block)
1884 struct dasd_ccw_req *cqr, *n;
1885 int rc, i;
1886 struct list_head flush_queue;
1888 INIT_LIST_HEAD(&flush_queue);
1889 spin_lock_bh(&block->queue_lock);
1890 rc = 0;
1891 restart:
1892 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1893 /* if this request currently owned by a dasd_device cancel it */
1894 if (cqr->status >= DASD_CQR_QUEUED)
1895 rc = dasd_cancel_req(cqr);
1896 if (rc < 0)
1897 break;
1898 /* Rechain request (including erp chain) so it won't be
1899 * touched by the dasd_block_tasklet anymore.
1900 * Replace the callback so we notice when the request
1901 * is returned from the dasd_device layer.
1903 cqr->callback = _dasd_wake_block_flush_cb;
1904 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1905 list_move_tail(&cqr->blocklist, &flush_queue);
1906 if (i > 1)
1907 /* moved more than one request - need to restart */
1908 goto restart;
1910 spin_unlock_bh(&block->queue_lock);
1911 /* Now call the callback function of flushed requests */
1912 restart_cb:
1913 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1914 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1915 /* Process finished ERP request. */
1916 if (cqr->refers) {
1917 __dasd_block_process_erp(block, cqr);
1918 /* restart list_for_xx loop since dasd_process_erp
1919 * might remove multiple elements */
1920 goto restart_cb;
1922 /* call the callback function */
1923 cqr->endclk = get_clock();
1924 list_del_init(&cqr->blocklist);
1925 __dasd_cleanup_cqr(cqr);
1927 return rc;
1931 * Schedules a call to dasd_tasklet over the device tasklet.
1933 void dasd_schedule_block_bh(struct dasd_block *block)
1935 /* Protect against rescheduling. */
1936 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1937 return;
1938 /* life cycle of block is bound to it's base device */
1939 dasd_get_device(block->base);
1940 tasklet_hi_schedule(&block->tasklet);
1945 * SECTION: external block device operations
1946 * (request queue handling, open, release, etc.)
1950 * Dasd request queue function. Called from ll_rw_blk.c
1952 static void do_dasd_request(struct request_queue *queue)
1954 struct dasd_block *block;
1956 block = queue->queuedata;
1957 spin_lock(&block->queue_lock);
1958 /* Get new request from the block device request queue */
1959 __dasd_process_request_queue(block);
1960 /* Now check if the head of the ccw queue needs to be started. */
1961 __dasd_block_start_head(block);
1962 spin_unlock(&block->queue_lock);
1966 * Allocate and initialize request queue and default I/O scheduler.
1968 static int dasd_alloc_queue(struct dasd_block *block)
1970 int rc;
1972 block->request_queue = blk_init_queue(do_dasd_request,
1973 &block->request_queue_lock);
1974 if (block->request_queue == NULL)
1975 return -ENOMEM;
1977 block->request_queue->queuedata = block;
1979 elevator_exit(block->request_queue->elevator);
1980 rc = elevator_init(block->request_queue, "deadline");
1981 if (rc) {
1982 blk_cleanup_queue(block->request_queue);
1983 return rc;
1985 return 0;
1989 * Allocate and initialize request queue.
1991 static void dasd_setup_queue(struct dasd_block *block)
1993 int max;
1995 blk_queue_hardsect_size(block->request_queue, block->bp_block);
1996 max = block->base->discipline->max_blocks << block->s2b_shift;
1997 blk_queue_max_sectors(block->request_queue, max);
1998 blk_queue_max_phys_segments(block->request_queue, -1L);
1999 blk_queue_max_hw_segments(block->request_queue, -1L);
2000 blk_queue_max_segment_size(block->request_queue, -1L);
2001 blk_queue_segment_boundary(block->request_queue, -1L);
2002 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
2006 * Deactivate and free request queue.
2008 static void dasd_free_queue(struct dasd_block *block)
2010 if (block->request_queue) {
2011 blk_cleanup_queue(block->request_queue);
2012 block->request_queue = NULL;
2017 * Flush request on the request queue.
2019 static void dasd_flush_request_queue(struct dasd_block *block)
2021 struct request *req;
2023 if (!block->request_queue)
2024 return;
2026 spin_lock_irq(&block->request_queue_lock);
2027 while ((req = elv_next_request(block->request_queue))) {
2028 blkdev_dequeue_request(req);
2029 dasd_end_request(req, -EIO);
2031 spin_unlock_irq(&block->request_queue_lock);
2034 static int dasd_open(struct inode *inp, struct file *filp)
2036 struct gendisk *disk = inp->i_bdev->bd_disk;
2037 struct dasd_block *block = disk->private_data;
2038 struct dasd_device *base = block->base;
2039 int rc;
2041 atomic_inc(&block->open_count);
2042 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2043 rc = -ENODEV;
2044 goto unlock;
2047 if (!try_module_get(base->discipline->owner)) {
2048 rc = -EINVAL;
2049 goto unlock;
2052 if (dasd_probeonly) {
2053 DEV_MESSAGE(KERN_INFO, base, "%s",
2054 "No access to device due to probeonly mode");
2055 rc = -EPERM;
2056 goto out;
2059 if (base->state <= DASD_STATE_BASIC) {
2060 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2061 " Cannot open unrecognized device");
2062 rc = -ENODEV;
2063 goto out;
2066 return 0;
2068 out:
2069 module_put(base->discipline->owner);
2070 unlock:
2071 atomic_dec(&block->open_count);
2072 return rc;
2075 static int dasd_release(struct inode *inp, struct file *filp)
2077 struct gendisk *disk = inp->i_bdev->bd_disk;
2078 struct dasd_block *block = disk->private_data;
2080 atomic_dec(&block->open_count);
2081 module_put(block->base->discipline->owner);
2082 return 0;
2086 * Return disk geometry.
2088 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2090 struct dasd_block *block;
2091 struct dasd_device *base;
2093 block = bdev->bd_disk->private_data;
2094 base = block->base;
2095 if (!block)
2096 return -ENODEV;
2098 if (!base->discipline ||
2099 !base->discipline->fill_geometry)
2100 return -EINVAL;
2102 base->discipline->fill_geometry(block, geo);
2103 geo->start = get_start_sect(bdev) >> block->s2b_shift;
2104 return 0;
2107 struct block_device_operations
2108 dasd_device_operations = {
2109 .owner = THIS_MODULE,
2110 .open = dasd_open,
2111 .release = dasd_release,
2112 .ioctl = dasd_ioctl,
2113 .compat_ioctl = dasd_compat_ioctl,
2114 .getgeo = dasd_getgeo,
2117 /*******************************************************************************
2118 * end of block device operations
2121 static void
2122 dasd_exit(void)
2124 #ifdef CONFIG_PROC_FS
2125 dasd_proc_exit();
2126 #endif
2127 dasd_eer_exit();
2128 if (dasd_page_cache != NULL) {
2129 kmem_cache_destroy(dasd_page_cache);
2130 dasd_page_cache = NULL;
2132 dasd_gendisk_exit();
2133 dasd_devmap_exit();
2134 if (dasd_debug_area != NULL) {
2135 debug_unregister(dasd_debug_area);
2136 dasd_debug_area = NULL;
2141 * SECTION: common functions for ccw_driver use
2145 * Initial attempt at a probe function. this can be simplified once
2146 * the other detection code is gone.
2148 int dasd_generic_probe(struct ccw_device *cdev,
2149 struct dasd_discipline *discipline)
2151 int ret;
2153 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2154 if (ret) {
2155 printk(KERN_WARNING
2156 "dasd_generic_probe: could not set ccw-device options "
2157 "for %s\n", cdev->dev.bus_id);
2158 return ret;
2160 ret = dasd_add_sysfs_files(cdev);
2161 if (ret) {
2162 printk(KERN_WARNING
2163 "dasd_generic_probe: could not add sysfs entries "
2164 "for %s\n", cdev->dev.bus_id);
2165 return ret;
2167 cdev->handler = &dasd_int_handler;
2170 * Automatically online either all dasd devices (dasd_autodetect)
2171 * or all devices specified with dasd= parameters during
2172 * initial probe.
2174 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2175 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
2176 ret = ccw_device_set_online(cdev);
2177 if (ret)
2178 printk(KERN_WARNING
2179 "dasd_generic_probe: could not initially "
2180 "online ccw-device %s; return code: %d\n",
2181 cdev->dev.bus_id, ret);
2182 return 0;
2186 * This will one day be called from a global not_oper handler.
2187 * It is also used by driver_unregister during module unload.
2189 void dasd_generic_remove(struct ccw_device *cdev)
2191 struct dasd_device *device;
2192 struct dasd_block *block;
2194 cdev->handler = NULL;
2196 dasd_remove_sysfs_files(cdev);
2197 device = dasd_device_from_cdev(cdev);
2198 if (IS_ERR(device))
2199 return;
2200 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2201 /* Already doing offline processing */
2202 dasd_put_device(device);
2203 return;
2206 * This device is removed unconditionally. Set offline
2207 * flag to prevent dasd_open from opening it while it is
2208 * no quite down yet.
2210 dasd_set_target_state(device, DASD_STATE_NEW);
2211 /* dasd_delete_device destroys the device reference. */
2212 block = device->block;
2213 device->block = NULL;
2214 dasd_delete_device(device);
2216 * life cycle of block is bound to device, so delete it after
2217 * device was safely removed
2219 if (block)
2220 dasd_free_block(block);
2224 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
2225 * the device is detected for the first time and is supposed to be used
2226 * or the user has started activation through sysfs.
2228 int dasd_generic_set_online(struct ccw_device *cdev,
2229 struct dasd_discipline *base_discipline)
2231 struct dasd_discipline *discipline;
2232 struct dasd_device *device;
2233 int rc;
2235 /* first online clears initial online feature flag */
2236 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
2237 device = dasd_create_device(cdev);
2238 if (IS_ERR(device))
2239 return PTR_ERR(device);
2241 discipline = base_discipline;
2242 if (device->features & DASD_FEATURE_USEDIAG) {
2243 if (!dasd_diag_discipline_pointer) {
2244 printk (KERN_WARNING
2245 "dasd_generic couldn't online device %s "
2246 "- discipline DIAG not available\n",
2247 cdev->dev.bus_id);
2248 dasd_delete_device(device);
2249 return -ENODEV;
2251 discipline = dasd_diag_discipline_pointer;
2253 if (!try_module_get(base_discipline->owner)) {
2254 dasd_delete_device(device);
2255 return -EINVAL;
2257 if (!try_module_get(discipline->owner)) {
2258 module_put(base_discipline->owner);
2259 dasd_delete_device(device);
2260 return -EINVAL;
2262 device->base_discipline = base_discipline;
2263 device->discipline = discipline;
2265 /* check_device will allocate block device if necessary */
2266 rc = discipline->check_device(device);
2267 if (rc) {
2268 printk (KERN_WARNING
2269 "dasd_generic couldn't online device %s "
2270 "with discipline %s rc=%i\n",
2271 cdev->dev.bus_id, discipline->name, rc);
2272 module_put(discipline->owner);
2273 module_put(base_discipline->owner);
2274 dasd_delete_device(device);
2275 return rc;
2278 dasd_set_target_state(device, DASD_STATE_ONLINE);
2279 if (device->state <= DASD_STATE_KNOWN) {
2280 printk (KERN_WARNING
2281 "dasd_generic discipline not found for %s\n",
2282 cdev->dev.bus_id);
2283 rc = -ENODEV;
2284 dasd_set_target_state(device, DASD_STATE_NEW);
2285 if (device->block)
2286 dasd_free_block(device->block);
2287 dasd_delete_device(device);
2288 } else
2289 pr_debug("dasd_generic device %s found\n",
2290 cdev->dev.bus_id);
2292 /* FIXME: we have to wait for the root device but we don't want
2293 * to wait for each single device but for all at once. */
2294 wait_event(dasd_init_waitq, _wait_for_device(device));
2296 dasd_put_device(device);
2298 return rc;
2301 int dasd_generic_set_offline(struct ccw_device *cdev)
2303 struct dasd_device *device;
2304 struct dasd_block *block;
2305 int max_count, open_count;
2307 device = dasd_device_from_cdev(cdev);
2308 if (IS_ERR(device))
2309 return PTR_ERR(device);
2310 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2311 /* Already doing offline processing */
2312 dasd_put_device(device);
2313 return 0;
2316 * We must make sure that this device is currently not in use.
2317 * The open_count is increased for every opener, that includes
2318 * the blkdev_get in dasd_scan_partitions. We are only interested
2319 * in the other openers.
2321 if (device->block) {
2322 struct dasd_block *block = device->block;
2323 max_count = block->bdev ? 0 : -1;
2324 open_count = (int) atomic_read(&block->open_count);
2325 if (open_count > max_count) {
2326 if (open_count > 0)
2327 printk(KERN_WARNING "Can't offline dasd "
2328 "device with open count = %i.\n",
2329 open_count);
2330 else
2331 printk(KERN_WARNING "%s",
2332 "Can't offline dasd device due "
2333 "to internal use\n");
2334 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2335 dasd_put_device(device);
2336 return -EBUSY;
2339 dasd_set_target_state(device, DASD_STATE_NEW);
2340 /* dasd_delete_device destroys the device reference. */
2341 block = device->block;
2342 device->block = NULL;
2343 dasd_delete_device(device);
2345 * life cycle of block is bound to device, so delete it after
2346 * device was safely removed
2348 if (block)
2349 dasd_free_block(block);
2350 return 0;
2353 int dasd_generic_notify(struct ccw_device *cdev, int event)
2355 struct dasd_device *device;
2356 struct dasd_ccw_req *cqr;
2357 unsigned long flags;
2358 int ret;
2360 device = dasd_device_from_cdev(cdev);
2361 if (IS_ERR(device))
2362 return 0;
2363 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2364 ret = 0;
2365 switch (event) {
2366 case CIO_GONE:
2367 case CIO_NO_PATH:
2368 /* First of all call extended error reporting. */
2369 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2371 if (device->state < DASD_STATE_BASIC)
2372 break;
2373 /* Device is active. We want to keep it. */
2374 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2375 if (cqr->status == DASD_CQR_IN_IO) {
2376 cqr->status = DASD_CQR_QUEUED;
2377 cqr->retries++;
2379 device->stopped |= DASD_STOPPED_DC_WAIT;
2380 dasd_device_clear_timer(device);
2381 dasd_schedule_device_bh(device);
2382 ret = 1;
2383 break;
2384 case CIO_OPER:
2385 /* FIXME: add a sanity check. */
2386 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2387 dasd_schedule_device_bh(device);
2388 if (device->block)
2389 dasd_schedule_block_bh(device->block);
2390 ret = 1;
2391 break;
2393 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2394 dasd_put_device(device);
2395 return ret;
2398 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2399 void *rdc_buffer,
2400 int rdc_buffer_size,
2401 char *magic)
2403 struct dasd_ccw_req *cqr;
2404 struct ccw1 *ccw;
2406 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2408 if (IS_ERR(cqr)) {
2409 DEV_MESSAGE(KERN_WARNING, device, "%s",
2410 "Could not allocate RDC request");
2411 return cqr;
2414 ccw = cqr->cpaddr;
2415 ccw->cmd_code = CCW_CMD_RDC;
2416 ccw->cda = (__u32)(addr_t)rdc_buffer;
2417 ccw->count = rdc_buffer_size;
2419 cqr->startdev = device;
2420 cqr->memdev = device;
2421 cqr->expires = 10*HZ;
2422 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2423 cqr->retries = 2;
2424 cqr->buildclk = get_clock();
2425 cqr->status = DASD_CQR_FILLED;
2426 return cqr;
2430 int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2431 void **rdc_buffer, int rdc_buffer_size)
2433 int ret;
2434 struct dasd_ccw_req *cqr;
2436 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2437 magic);
2438 if (IS_ERR(cqr))
2439 return PTR_ERR(cqr);
2441 ret = dasd_sleep_on(cqr);
2442 dasd_sfree_request(cqr, cqr->memdev);
2443 return ret;
2445 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
2447 static int __init dasd_init(void)
2449 int rc;
2451 init_waitqueue_head(&dasd_init_waitq);
2452 init_waitqueue_head(&dasd_flush_wq);
2454 /* register 'common' DASD debug area, used for all DBF_XXX calls */
2455 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
2456 if (dasd_debug_area == NULL) {
2457 rc = -ENOMEM;
2458 goto failed;
2460 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2461 debug_set_level(dasd_debug_area, DBF_WARNING);
2463 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2465 dasd_diag_discipline_pointer = NULL;
2467 rc = dasd_devmap_init();
2468 if (rc)
2469 goto failed;
2470 rc = dasd_gendisk_init();
2471 if (rc)
2472 goto failed;
2473 rc = dasd_parse();
2474 if (rc)
2475 goto failed;
2476 rc = dasd_eer_init();
2477 if (rc)
2478 goto failed;
2479 #ifdef CONFIG_PROC_FS
2480 rc = dasd_proc_init();
2481 if (rc)
2482 goto failed;
2483 #endif
2485 return 0;
2486 failed:
2487 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2488 dasd_exit();
2489 return rc;
2492 module_init(dasd_init);
2493 module_exit(dasd_exit);
2495 EXPORT_SYMBOL(dasd_debug_area);
2496 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2498 EXPORT_SYMBOL(dasd_add_request_head);
2499 EXPORT_SYMBOL(dasd_add_request_tail);
2500 EXPORT_SYMBOL(dasd_cancel_req);
2501 EXPORT_SYMBOL(dasd_device_clear_timer);
2502 EXPORT_SYMBOL(dasd_block_clear_timer);
2503 EXPORT_SYMBOL(dasd_enable_device);
2504 EXPORT_SYMBOL(dasd_int_handler);
2505 EXPORT_SYMBOL(dasd_kfree_request);
2506 EXPORT_SYMBOL(dasd_kick_device);
2507 EXPORT_SYMBOL(dasd_kmalloc_request);
2508 EXPORT_SYMBOL(dasd_schedule_device_bh);
2509 EXPORT_SYMBOL(dasd_schedule_block_bh);
2510 EXPORT_SYMBOL(dasd_set_target_state);
2511 EXPORT_SYMBOL(dasd_device_set_timer);
2512 EXPORT_SYMBOL(dasd_block_set_timer);
2513 EXPORT_SYMBOL(dasd_sfree_request);
2514 EXPORT_SYMBOL(dasd_sleep_on);
2515 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2516 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2517 EXPORT_SYMBOL(dasd_smalloc_request);
2518 EXPORT_SYMBOL(dasd_start_IO);
2519 EXPORT_SYMBOL(dasd_term_IO);
2521 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2522 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2523 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2524 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2525 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2526 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2527 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2528 EXPORT_SYMBOL_GPL(dasd_alloc_block);
2529 EXPORT_SYMBOL_GPL(dasd_free_block);