Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / s390 / block / dasd.c
blobacf222f91f5a97ac0564c25a5bea9386466d045b
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 * Copyright IBM Corp. 1999, 2009
9 */
11 #define KMSG_COMPONENT "dasd"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
21 #include <linux/hdreg.h>
22 #include <linux/async.h>
23 #include <linux/mutex.h>
25 #include <asm/ccwdev.h>
26 #include <asm/ebcdic.h>
27 #include <asm/idals.h>
28 #include <asm/itcw.h>
29 #include <asm/diag.h>
31 /* This is ugly... */
32 #define PRINTK_HEADER "dasd:"
34 #include "dasd_int.h"
36 * SECTION: Constant definitions to be used within this file
38 #define DASD_CHANQ_MAX_SIZE 4
41 * SECTION: exported variables of dasd.c
43 debug_info_t *dasd_debug_area;
44 struct dasd_discipline *dasd_diag_discipline_pointer;
45 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
47 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
48 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
49 " Copyright 2000 IBM Corporation");
50 MODULE_SUPPORTED_DEVICE("dasd");
51 MODULE_LICENSE("GPL");
54 * SECTION: prototypes for static functions of dasd.c
56 static int dasd_alloc_queue(struct dasd_block *);
57 static void dasd_setup_queue(struct dasd_block *);
58 static void dasd_free_queue(struct dasd_block *);
59 static void dasd_flush_request_queue(struct dasd_block *);
60 static int dasd_flush_block_queue(struct dasd_block *);
61 static void dasd_device_tasklet(struct dasd_device *);
62 static void dasd_block_tasklet(struct dasd_block *);
63 static void do_kick_device(struct work_struct *);
64 static void do_restore_device(struct work_struct *);
65 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
66 static void dasd_device_timeout(unsigned long);
67 static void dasd_block_timeout(unsigned long);
68 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
71 * SECTION: Operations on the device structure.
73 static wait_queue_head_t dasd_init_waitq;
74 static wait_queue_head_t dasd_flush_wq;
75 static wait_queue_head_t generic_waitq;
78 * Allocate memory for a new device structure.
80 struct dasd_device *dasd_alloc_device(void)
82 struct dasd_device *device;
84 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
85 if (!device)
86 return ERR_PTR(-ENOMEM);
88 /* Get two pages for normal block device operations. */
89 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
90 if (!device->ccw_mem) {
91 kfree(device);
92 return ERR_PTR(-ENOMEM);
94 /* Get one page for error recovery. */
95 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
96 if (!device->erp_mem) {
97 free_pages((unsigned long) device->ccw_mem, 1);
98 kfree(device);
99 return ERR_PTR(-ENOMEM);
102 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
103 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
104 spin_lock_init(&device->mem_lock);
105 atomic_set(&device->tasklet_scheduled, 0);
106 tasklet_init(&device->tasklet,
107 (void (*)(unsigned long)) dasd_device_tasklet,
108 (unsigned long) device);
109 INIT_LIST_HEAD(&device->ccw_queue);
110 init_timer(&device->timer);
111 device->timer.function = dasd_device_timeout;
112 device->timer.data = (unsigned long) device;
113 INIT_WORK(&device->kick_work, do_kick_device);
114 INIT_WORK(&device->restore_device, do_restore_device);
115 device->state = DASD_STATE_NEW;
116 device->target = DASD_STATE_NEW;
117 mutex_init(&device->state_mutex);
119 return device;
123 * Free memory of a device structure.
125 void dasd_free_device(struct dasd_device *device)
127 kfree(device->private);
128 free_page((unsigned long) device->erp_mem);
129 free_pages((unsigned long) device->ccw_mem, 1);
130 kfree(device);
134 * Allocate memory for a new device structure.
136 struct dasd_block *dasd_alloc_block(void)
138 struct dasd_block *block;
140 block = kzalloc(sizeof(*block), GFP_ATOMIC);
141 if (!block)
142 return ERR_PTR(-ENOMEM);
143 /* open_count = 0 means device online but not in use */
144 atomic_set(&block->open_count, -1);
146 spin_lock_init(&block->request_queue_lock);
147 atomic_set(&block->tasklet_scheduled, 0);
148 tasklet_init(&block->tasklet,
149 (void (*)(unsigned long)) dasd_block_tasklet,
150 (unsigned long) block);
151 INIT_LIST_HEAD(&block->ccw_queue);
152 spin_lock_init(&block->queue_lock);
153 init_timer(&block->timer);
154 block->timer.function = dasd_block_timeout;
155 block->timer.data = (unsigned long) block;
157 return block;
161 * Free memory of a device structure.
163 void dasd_free_block(struct dasd_block *block)
165 kfree(block);
169 * Make a new device known to the system.
171 static int dasd_state_new_to_known(struct dasd_device *device)
173 int rc;
176 * As long as the device is not in state DASD_STATE_NEW we want to
177 * keep the reference count > 0.
179 dasd_get_device(device);
181 if (device->block) {
182 rc = dasd_alloc_queue(device->block);
183 if (rc) {
184 dasd_put_device(device);
185 return rc;
188 device->state = DASD_STATE_KNOWN;
189 return 0;
193 * Let the system forget about a device.
195 static int dasd_state_known_to_new(struct dasd_device *device)
197 /* Disable extended error reporting for this device. */
198 dasd_eer_disable(device);
199 /* Forget the discipline information. */
200 if (device->discipline) {
201 if (device->discipline->uncheck_device)
202 device->discipline->uncheck_device(device);
203 module_put(device->discipline->owner);
205 device->discipline = NULL;
206 if (device->base_discipline)
207 module_put(device->base_discipline->owner);
208 device->base_discipline = NULL;
209 device->state = DASD_STATE_NEW;
211 if (device->block)
212 dasd_free_queue(device->block);
214 /* Give up reference we took in dasd_state_new_to_known. */
215 dasd_put_device(device);
216 return 0;
220 * Request the irq line for the device.
222 static int dasd_state_known_to_basic(struct dasd_device *device)
224 int rc;
226 /* Allocate and register gendisk structure. */
227 if (device->block) {
228 rc = dasd_gendisk_alloc(device->block);
229 if (rc)
230 return rc;
232 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
233 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
234 8 * sizeof(long));
235 debug_register_view(device->debug_area, &debug_sprintf_view);
236 debug_set_level(device->debug_area, DBF_WARNING);
237 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
239 device->state = DASD_STATE_BASIC;
240 return 0;
244 * Release the irq line for the device. Terminate any running i/o.
246 static int dasd_state_basic_to_known(struct dasd_device *device)
248 int rc;
249 if (device->block) {
250 dasd_gendisk_free(device->block);
251 dasd_block_clear_timer(device->block);
253 rc = dasd_flush_device_queue(device);
254 if (rc)
255 return rc;
256 dasd_device_clear_timer(device);
258 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
259 if (device->debug_area != NULL) {
260 debug_unregister(device->debug_area);
261 device->debug_area = NULL;
263 device->state = DASD_STATE_KNOWN;
264 return 0;
268 * Do the initial analysis. The do_analysis function may return
269 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
270 * until the discipline decides to continue the startup sequence
271 * by calling the function dasd_change_state. The eckd disciplines
272 * uses this to start a ccw that detects the format. The completion
273 * interrupt for this detection ccw uses the kernel event daemon to
274 * trigger the call to dasd_change_state. All this is done in the
275 * discipline code, see dasd_eckd.c.
276 * After the analysis ccw is done (do_analysis returned 0) the block
277 * device is setup.
278 * In case the analysis returns an error, the device setup is stopped
279 * (a fake disk was already added to allow formatting).
281 static int dasd_state_basic_to_ready(struct dasd_device *device)
283 int rc;
284 struct dasd_block *block;
286 rc = 0;
287 block = device->block;
288 /* make disk known with correct capacity */
289 if (block) {
290 if (block->base->discipline->do_analysis != NULL)
291 rc = block->base->discipline->do_analysis(block);
292 if (rc) {
293 if (rc != -EAGAIN)
294 device->state = DASD_STATE_UNFMT;
295 return rc;
297 dasd_setup_queue(block);
298 set_capacity(block->gdp,
299 block->blocks << block->s2b_shift);
300 device->state = DASD_STATE_READY;
301 rc = dasd_scan_partitions(block);
302 if (rc)
303 device->state = DASD_STATE_BASIC;
304 } else {
305 device->state = DASD_STATE_READY;
307 return rc;
311 * Remove device from block device layer. Destroy dirty buffers.
312 * Forget format information. Check if the target level is basic
313 * and if it is create fake disk for formatting.
315 static int dasd_state_ready_to_basic(struct dasd_device *device)
317 int rc;
319 device->state = DASD_STATE_BASIC;
320 if (device->block) {
321 struct dasd_block *block = device->block;
322 rc = dasd_flush_block_queue(block);
323 if (rc) {
324 device->state = DASD_STATE_READY;
325 return rc;
327 dasd_flush_request_queue(block);
328 dasd_destroy_partitions(block);
329 block->blocks = 0;
330 block->bp_block = 0;
331 block->s2b_shift = 0;
333 return 0;
337 * Back to basic.
339 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
341 device->state = DASD_STATE_BASIC;
342 return 0;
346 * Make the device online and schedule the bottom half to start
347 * the requeueing of requests from the linux request queue to the
348 * ccw queue.
350 static int
351 dasd_state_ready_to_online(struct dasd_device * device)
353 int rc;
354 struct gendisk *disk;
355 struct disk_part_iter piter;
356 struct hd_struct *part;
358 if (device->discipline->ready_to_online) {
359 rc = device->discipline->ready_to_online(device);
360 if (rc)
361 return rc;
363 device->state = DASD_STATE_ONLINE;
364 if (device->block) {
365 dasd_schedule_block_bh(device->block);
366 disk = device->block->bdev->bd_disk;
367 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
368 while ((part = disk_part_iter_next(&piter)))
369 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
370 disk_part_iter_exit(&piter);
372 return 0;
376 * Stop the requeueing of requests again.
378 static int dasd_state_online_to_ready(struct dasd_device *device)
380 int rc;
381 struct gendisk *disk;
382 struct disk_part_iter piter;
383 struct hd_struct *part;
385 if (device->discipline->online_to_ready) {
386 rc = device->discipline->online_to_ready(device);
387 if (rc)
388 return rc;
390 device->state = DASD_STATE_READY;
391 if (device->block) {
392 disk = device->block->bdev->bd_disk;
393 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
394 while ((part = disk_part_iter_next(&piter)))
395 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
396 disk_part_iter_exit(&piter);
398 return 0;
402 * Device startup state changes.
404 static int dasd_increase_state(struct dasd_device *device)
406 int rc;
408 rc = 0;
409 if (device->state == DASD_STATE_NEW &&
410 device->target >= DASD_STATE_KNOWN)
411 rc = dasd_state_new_to_known(device);
413 if (!rc &&
414 device->state == DASD_STATE_KNOWN &&
415 device->target >= DASD_STATE_BASIC)
416 rc = dasd_state_known_to_basic(device);
418 if (!rc &&
419 device->state == DASD_STATE_BASIC &&
420 device->target >= DASD_STATE_READY)
421 rc = dasd_state_basic_to_ready(device);
423 if (!rc &&
424 device->state == DASD_STATE_UNFMT &&
425 device->target > DASD_STATE_UNFMT)
426 rc = -EPERM;
428 if (!rc &&
429 device->state == DASD_STATE_READY &&
430 device->target >= DASD_STATE_ONLINE)
431 rc = dasd_state_ready_to_online(device);
433 return rc;
437 * Device shutdown state changes.
439 static int dasd_decrease_state(struct dasd_device *device)
441 int rc;
443 rc = 0;
444 if (device->state == DASD_STATE_ONLINE &&
445 device->target <= DASD_STATE_READY)
446 rc = dasd_state_online_to_ready(device);
448 if (!rc &&
449 device->state == DASD_STATE_READY &&
450 device->target <= DASD_STATE_BASIC)
451 rc = dasd_state_ready_to_basic(device);
453 if (!rc &&
454 device->state == DASD_STATE_UNFMT &&
455 device->target <= DASD_STATE_BASIC)
456 rc = dasd_state_unfmt_to_basic(device);
458 if (!rc &&
459 device->state == DASD_STATE_BASIC &&
460 device->target <= DASD_STATE_KNOWN)
461 rc = dasd_state_basic_to_known(device);
463 if (!rc &&
464 device->state == DASD_STATE_KNOWN &&
465 device->target <= DASD_STATE_NEW)
466 rc = dasd_state_known_to_new(device);
468 return rc;
472 * This is the main startup/shutdown routine.
474 static void dasd_change_state(struct dasd_device *device)
476 int rc;
478 if (device->state == device->target)
479 /* Already where we want to go today... */
480 return;
481 if (device->state < device->target)
482 rc = dasd_increase_state(device);
483 else
484 rc = dasd_decrease_state(device);
485 if (rc == -EAGAIN)
486 return;
487 if (rc)
488 device->target = device->state;
490 if (device->state == device->target)
491 wake_up(&dasd_init_waitq);
493 /* let user-space know that the device status changed */
494 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
498 * Kick starter for devices that did not complete the startup/shutdown
499 * procedure or were sleeping because of a pending state.
500 * dasd_kick_device will schedule a call do do_kick_device to the kernel
501 * event daemon.
503 static void do_kick_device(struct work_struct *work)
505 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
506 mutex_lock(&device->state_mutex);
507 dasd_change_state(device);
508 mutex_unlock(&device->state_mutex);
509 dasd_schedule_device_bh(device);
510 dasd_put_device(device);
513 void dasd_kick_device(struct dasd_device *device)
515 dasd_get_device(device);
516 /* queue call to dasd_kick_device to the kernel event daemon. */
517 schedule_work(&device->kick_work);
521 * dasd_restore_device will schedule a call do do_restore_device to the kernel
522 * event daemon.
524 static void do_restore_device(struct work_struct *work)
526 struct dasd_device *device = container_of(work, struct dasd_device,
527 restore_device);
528 device->cdev->drv->restore(device->cdev);
529 dasd_put_device(device);
532 void dasd_restore_device(struct dasd_device *device)
534 dasd_get_device(device);
535 /* queue call to dasd_restore_device to the kernel event daemon. */
536 schedule_work(&device->restore_device);
540 * Set the target state for a device and starts the state change.
542 void dasd_set_target_state(struct dasd_device *device, int target)
544 dasd_get_device(device);
545 mutex_lock(&device->state_mutex);
546 /* If we are in probeonly mode stop at DASD_STATE_READY. */
547 if (dasd_probeonly && target > DASD_STATE_READY)
548 target = DASD_STATE_READY;
549 if (device->target != target) {
550 if (device->state == target)
551 wake_up(&dasd_init_waitq);
552 device->target = target;
554 if (device->state != device->target)
555 dasd_change_state(device);
556 mutex_unlock(&device->state_mutex);
557 dasd_put_device(device);
561 * Enable devices with device numbers in [from..to].
563 static inline int _wait_for_device(struct dasd_device *device)
565 return (device->state == device->target);
568 void dasd_enable_device(struct dasd_device *device)
570 dasd_set_target_state(device, DASD_STATE_ONLINE);
571 if (device->state <= DASD_STATE_KNOWN)
572 /* No discipline for device found. */
573 dasd_set_target_state(device, DASD_STATE_NEW);
574 /* Now wait for the devices to come up. */
575 wait_event(dasd_init_waitq, _wait_for_device(device));
579 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
581 #ifdef CONFIG_DASD_PROFILE
583 struct dasd_profile_info_t dasd_global_profile;
584 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
587 * Increments counter in global and local profiling structures.
589 #define dasd_profile_counter(value, counter, block) \
591 int index; \
592 for (index = 0; index < 31 && value >> (2+index); index++); \
593 dasd_global_profile.counter[index]++; \
594 block->profile.counter[index]++; \
598 * Add profiling information for cqr before execution.
600 static void dasd_profile_start(struct dasd_block *block,
601 struct dasd_ccw_req *cqr,
602 struct request *req)
604 struct list_head *l;
605 unsigned int counter;
607 if (dasd_profile_level != DASD_PROFILE_ON)
608 return;
610 /* count the length of the chanq for statistics */
611 counter = 0;
612 list_for_each(l, &block->ccw_queue)
613 if (++counter >= 31)
614 break;
615 dasd_global_profile.dasd_io_nr_req[counter]++;
616 block->profile.dasd_io_nr_req[counter]++;
620 * Add profiling information for cqr after execution.
622 static void dasd_profile_end(struct dasd_block *block,
623 struct dasd_ccw_req *cqr,
624 struct request *req)
626 long strtime, irqtime, endtime, tottime; /* in microseconds */
627 long tottimeps, sectors;
629 if (dasd_profile_level != DASD_PROFILE_ON)
630 return;
632 sectors = blk_rq_sectors(req);
633 if (!cqr->buildclk || !cqr->startclk ||
634 !cqr->stopclk || !cqr->endclk ||
635 !sectors)
636 return;
638 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
639 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
640 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
641 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
642 tottimeps = tottime / sectors;
644 if (!dasd_global_profile.dasd_io_reqs)
645 memset(&dasd_global_profile, 0,
646 sizeof(struct dasd_profile_info_t));
647 dasd_global_profile.dasd_io_reqs++;
648 dasd_global_profile.dasd_io_sects += sectors;
650 if (!block->profile.dasd_io_reqs)
651 memset(&block->profile, 0,
652 sizeof(struct dasd_profile_info_t));
653 block->profile.dasd_io_reqs++;
654 block->profile.dasd_io_sects += sectors;
656 dasd_profile_counter(sectors, dasd_io_secs, block);
657 dasd_profile_counter(tottime, dasd_io_times, block);
658 dasd_profile_counter(tottimeps, dasd_io_timps, block);
659 dasd_profile_counter(strtime, dasd_io_time1, block);
660 dasd_profile_counter(irqtime, dasd_io_time2, block);
661 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
662 dasd_profile_counter(endtime, dasd_io_time3, block);
664 #else
665 #define dasd_profile_start(block, cqr, req) do {} while (0)
666 #define dasd_profile_end(block, cqr, req) do {} while (0)
667 #endif /* CONFIG_DASD_PROFILE */
670 * Allocate memory for a channel program with 'cplength' channel
671 * command words and 'datasize' additional space. There are two
672 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
673 * memory and 2) dasd_smalloc_request uses the static ccw memory
674 * that gets allocated for each device.
676 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
677 int datasize,
678 struct dasd_device *device)
680 struct dasd_ccw_req *cqr;
682 /* Sanity checks */
683 BUG_ON(datasize > PAGE_SIZE ||
684 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
686 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
687 if (cqr == NULL)
688 return ERR_PTR(-ENOMEM);
689 cqr->cpaddr = NULL;
690 if (cplength > 0) {
691 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
692 GFP_ATOMIC | GFP_DMA);
693 if (cqr->cpaddr == NULL) {
694 kfree(cqr);
695 return ERR_PTR(-ENOMEM);
698 cqr->data = NULL;
699 if (datasize > 0) {
700 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
701 if (cqr->data == NULL) {
702 kfree(cqr->cpaddr);
703 kfree(cqr);
704 return ERR_PTR(-ENOMEM);
707 cqr->magic = magic;
708 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
709 dasd_get_device(device);
710 return cqr;
713 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
714 int datasize,
715 struct dasd_device *device)
717 unsigned long flags;
718 struct dasd_ccw_req *cqr;
719 char *data;
720 int size;
722 /* Sanity checks */
723 BUG_ON(datasize > PAGE_SIZE ||
724 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
726 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
727 if (cplength > 0)
728 size += cplength * sizeof(struct ccw1);
729 if (datasize > 0)
730 size += datasize;
731 spin_lock_irqsave(&device->mem_lock, flags);
732 cqr = (struct dasd_ccw_req *)
733 dasd_alloc_chunk(&device->ccw_chunks, size);
734 spin_unlock_irqrestore(&device->mem_lock, flags);
735 if (cqr == NULL)
736 return ERR_PTR(-ENOMEM);
737 memset(cqr, 0, sizeof(struct dasd_ccw_req));
738 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
739 cqr->cpaddr = NULL;
740 if (cplength > 0) {
741 cqr->cpaddr = (struct ccw1 *) data;
742 data += cplength*sizeof(struct ccw1);
743 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
745 cqr->data = NULL;
746 if (datasize > 0) {
747 cqr->data = data;
748 memset(cqr->data, 0, datasize);
750 cqr->magic = magic;
751 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
752 dasd_get_device(device);
753 return cqr;
757 * Free memory of a channel program. This function needs to free all the
758 * idal lists that might have been created by dasd_set_cda and the
759 * struct dasd_ccw_req itself.
761 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
763 #ifdef CONFIG_64BIT
764 struct ccw1 *ccw;
766 /* Clear any idals used for the request. */
767 ccw = cqr->cpaddr;
768 do {
769 clear_normalized_cda(ccw);
770 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
771 #endif
772 kfree(cqr->cpaddr);
773 kfree(cqr->data);
774 kfree(cqr);
775 dasd_put_device(device);
778 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
780 unsigned long flags;
782 spin_lock_irqsave(&device->mem_lock, flags);
783 dasd_free_chunk(&device->ccw_chunks, cqr);
784 spin_unlock_irqrestore(&device->mem_lock, flags);
785 dasd_put_device(device);
789 * Check discipline magic in cqr.
791 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
793 struct dasd_device *device;
795 if (cqr == NULL)
796 return -EINVAL;
797 device = cqr->startdev;
798 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
799 DBF_DEV_EVENT(DBF_WARNING, device,
800 " dasd_ccw_req 0x%08x magic doesn't match"
801 " discipline 0x%08x",
802 cqr->magic,
803 *(unsigned int *) device->discipline->name);
804 return -EINVAL;
806 return 0;
810 * Terminate the current i/o and set the request to clear_pending.
811 * Timer keeps device runnig.
812 * ccw_device_clear can fail if the i/o subsystem
813 * is in a bad mood.
815 int dasd_term_IO(struct dasd_ccw_req *cqr)
817 struct dasd_device *device;
818 int retries, rc;
819 char errorstring[ERRORLENGTH];
821 /* Check the cqr */
822 rc = dasd_check_cqr(cqr);
823 if (rc)
824 return rc;
825 retries = 0;
826 device = (struct dasd_device *) cqr->startdev;
827 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
828 rc = ccw_device_clear(device->cdev, (long) cqr);
829 switch (rc) {
830 case 0: /* termination successful */
831 cqr->retries--;
832 cqr->status = DASD_CQR_CLEAR_PENDING;
833 cqr->stopclk = get_clock();
834 cqr->starttime = 0;
835 DBF_DEV_EVENT(DBF_DEBUG, device,
836 "terminate cqr %p successful",
837 cqr);
838 break;
839 case -ENODEV:
840 DBF_DEV_EVENT(DBF_ERR, device, "%s",
841 "device gone, retry");
842 break;
843 case -EIO:
844 DBF_DEV_EVENT(DBF_ERR, device, "%s",
845 "I/O error, retry");
846 break;
847 case -EINVAL:
848 case -EBUSY:
849 DBF_DEV_EVENT(DBF_ERR, device, "%s",
850 "device busy, retry later");
851 break;
852 default:
853 /* internal error 10 - unknown rc*/
854 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
855 dev_err(&device->cdev->dev, "An error occurred in the "
856 "DASD device driver, reason=%s\n", errorstring);
857 BUG();
858 break;
860 retries++;
862 dasd_schedule_device_bh(device);
863 return rc;
867 * Start the i/o. This start_IO can fail if the channel is really busy.
868 * In that case set up a timer to start the request later.
870 int dasd_start_IO(struct dasd_ccw_req *cqr)
872 struct dasd_device *device;
873 int rc;
874 char errorstring[ERRORLENGTH];
876 /* Check the cqr */
877 rc = dasd_check_cqr(cqr);
878 if (rc) {
879 cqr->intrc = rc;
880 return rc;
882 device = (struct dasd_device *) cqr->startdev;
883 if (cqr->retries < 0) {
884 /* internal error 14 - start_IO run out of retries */
885 sprintf(errorstring, "14 %p", cqr);
886 dev_err(&device->cdev->dev, "An error occurred in the DASD "
887 "device driver, reason=%s\n", errorstring);
888 cqr->status = DASD_CQR_ERROR;
889 return -EIO;
891 cqr->startclk = get_clock();
892 cqr->starttime = jiffies;
893 cqr->retries--;
894 if (cqr->cpmode == 1) {
895 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
896 (long) cqr, cqr->lpm);
897 } else {
898 rc = ccw_device_start(device->cdev, cqr->cpaddr,
899 (long) cqr, cqr->lpm, 0);
901 switch (rc) {
902 case 0:
903 cqr->status = DASD_CQR_IN_IO;
904 break;
905 case -EBUSY:
906 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
907 "start_IO: device busy, retry later");
908 break;
909 case -ETIMEDOUT:
910 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
911 "start_IO: request timeout, retry later");
912 break;
913 case -EACCES:
914 /* -EACCES indicates that the request used only a
915 * subset of the available pathes and all these
916 * pathes are gone.
917 * Do a retry with all available pathes.
919 cqr->lpm = LPM_ANYPATH;
920 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
921 "start_IO: selected pathes gone,"
922 " retry on all pathes");
923 break;
924 case -ENODEV:
925 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
926 "start_IO: -ENODEV device gone, retry");
927 break;
928 case -EIO:
929 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
930 "start_IO: -EIO device gone, retry");
931 break;
932 case -EINVAL:
933 /* most likely caused in power management context */
934 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
935 "start_IO: -EINVAL device currently "
936 "not accessible");
937 break;
938 default:
939 /* internal error 11 - unknown rc */
940 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
941 dev_err(&device->cdev->dev,
942 "An error occurred in the DASD device driver, "
943 "reason=%s\n", errorstring);
944 BUG();
945 break;
947 cqr->intrc = rc;
948 return rc;
952 * Timeout function for dasd devices. This is used for different purposes
953 * 1) missing interrupt handler for normal operation
954 * 2) delayed start of request where start_IO failed with -EBUSY
955 * 3) timeout for missing state change interrupts
956 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
957 * DASD_CQR_QUEUED for 2) and 3).
959 static void dasd_device_timeout(unsigned long ptr)
961 unsigned long flags;
962 struct dasd_device *device;
964 device = (struct dasd_device *) ptr;
965 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
966 /* re-activate request queue */
967 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
968 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
969 dasd_schedule_device_bh(device);
973 * Setup timeout for a device in jiffies.
975 void dasd_device_set_timer(struct dasd_device *device, int expires)
977 if (expires == 0)
978 del_timer(&device->timer);
979 else
980 mod_timer(&device->timer, jiffies + expires);
984 * Clear timeout for a device.
986 void dasd_device_clear_timer(struct dasd_device *device)
988 del_timer(&device->timer);
991 static void dasd_handle_killed_request(struct ccw_device *cdev,
992 unsigned long intparm)
994 struct dasd_ccw_req *cqr;
995 struct dasd_device *device;
997 if (!intparm)
998 return;
999 cqr = (struct dasd_ccw_req *) intparm;
1000 if (cqr->status != DASD_CQR_IN_IO) {
1001 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1002 "invalid status in handle_killed_request: "
1003 "%02x", cqr->status);
1004 return;
1007 device = dasd_device_from_cdev_locked(cdev);
1008 if (IS_ERR(device)) {
1009 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1010 "unable to get device from cdev");
1011 return;
1014 if (!cqr->startdev ||
1015 device != cqr->startdev ||
1016 strncmp(cqr->startdev->discipline->ebcname,
1017 (char *) &cqr->magic, 4)) {
1018 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1019 "invalid device in request");
1020 dasd_put_device(device);
1021 return;
1024 /* Schedule request to be retried. */
1025 cqr->status = DASD_CQR_QUEUED;
1027 dasd_device_clear_timer(device);
1028 dasd_schedule_device_bh(device);
1029 dasd_put_device(device);
1032 void dasd_generic_handle_state_change(struct dasd_device *device)
1034 /* First of all start sense subsystem status request. */
1035 dasd_eer_snss(device);
1037 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1038 dasd_schedule_device_bh(device);
1039 if (device->block)
1040 dasd_schedule_block_bh(device->block);
1044 * Interrupt handler for "normal" ssch-io based dasd devices.
1046 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1047 struct irb *irb)
1049 struct dasd_ccw_req *cqr, *next;
1050 struct dasd_device *device;
1051 unsigned long long now;
1052 int expires;
1054 if (IS_ERR(irb)) {
1055 switch (PTR_ERR(irb)) {
1056 case -EIO:
1057 break;
1058 case -ETIMEDOUT:
1059 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1060 "request timed out\n", __func__);
1061 break;
1062 default:
1063 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1064 "unknown error %ld\n", __func__,
1065 PTR_ERR(irb));
1067 dasd_handle_killed_request(cdev, intparm);
1068 return;
1071 now = get_clock();
1073 /* check for unsolicited interrupts */
1074 cqr = (struct dasd_ccw_req *) intparm;
1075 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1076 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1077 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
1078 if (cqr && cqr->status == DASD_CQR_IN_IO)
1079 cqr->status = DASD_CQR_QUEUED;
1080 device = dasd_device_from_cdev_locked(cdev);
1081 if (!IS_ERR(device)) {
1082 dasd_device_clear_timer(device);
1083 device->discipline->handle_unsolicited_interrupt(device,
1084 irb);
1085 dasd_put_device(device);
1087 return;
1090 device = (struct dasd_device *) cqr->startdev;
1091 if (!device ||
1092 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1093 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1094 "invalid device in request");
1095 return;
1098 /* Check for clear pending */
1099 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1100 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1101 cqr->status = DASD_CQR_CLEARED;
1102 dasd_device_clear_timer(device);
1103 wake_up(&dasd_flush_wq);
1104 dasd_schedule_device_bh(device);
1105 return;
1108 /* check status - the request might have been killed by dyn detach */
1109 if (cqr->status != DASD_CQR_IN_IO) {
1110 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1111 "status %02x", dev_name(&cdev->dev), cqr->status);
1112 return;
1115 next = NULL;
1116 expires = 0;
1117 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1118 scsw_cstat(&irb->scsw) == 0) {
1119 /* request was completed successfully */
1120 cqr->status = DASD_CQR_SUCCESS;
1121 cqr->stopclk = now;
1122 /* Start first request on queue if possible -> fast_io. */
1123 if (cqr->devlist.next != &device->ccw_queue) {
1124 next = list_entry(cqr->devlist.next,
1125 struct dasd_ccw_req, devlist);
1127 } else { /* error */
1128 memcpy(&cqr->irb, irb, sizeof(struct irb));
1129 /* log sense for every failed I/O to s390 debugfeature */
1130 dasd_log_sense_dbf(cqr, irb);
1131 if (device->features & DASD_FEATURE_ERPLOG) {
1132 dasd_log_sense(cqr, irb);
1136 * If we don't want complex ERP for this request, then just
1137 * reset this and retry it in the fastpath
1139 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1140 cqr->retries > 0) {
1141 if (cqr->lpm == LPM_ANYPATH)
1142 DBF_DEV_EVENT(DBF_DEBUG, device,
1143 "default ERP in fastpath "
1144 "(%i retries left)",
1145 cqr->retries);
1146 cqr->lpm = LPM_ANYPATH;
1147 cqr->status = DASD_CQR_QUEUED;
1148 next = cqr;
1149 } else
1150 cqr->status = DASD_CQR_ERROR;
1152 if (next && (next->status == DASD_CQR_QUEUED) &&
1153 (!device->stopped)) {
1154 if (device->discipline->start_IO(next) == 0)
1155 expires = next->expires;
1157 if (expires != 0)
1158 dasd_device_set_timer(device, expires);
1159 else
1160 dasd_device_clear_timer(device);
1161 dasd_schedule_device_bh(device);
1165 * If we have an error on a dasd_block layer request then we cancel
1166 * and return all further requests from the same dasd_block as well.
1168 static void __dasd_device_recovery(struct dasd_device *device,
1169 struct dasd_ccw_req *ref_cqr)
1171 struct list_head *l, *n;
1172 struct dasd_ccw_req *cqr;
1175 * only requeue request that came from the dasd_block layer
1177 if (!ref_cqr->block)
1178 return;
1180 list_for_each_safe(l, n, &device->ccw_queue) {
1181 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1182 if (cqr->status == DASD_CQR_QUEUED &&
1183 ref_cqr->block == cqr->block) {
1184 cqr->status = DASD_CQR_CLEARED;
1190 * Remove those ccw requests from the queue that need to be returned
1191 * to the upper layer.
1193 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1194 struct list_head *final_queue)
1196 struct list_head *l, *n;
1197 struct dasd_ccw_req *cqr;
1199 /* Process request with final status. */
1200 list_for_each_safe(l, n, &device->ccw_queue) {
1201 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1203 /* Stop list processing at the first non-final request. */
1204 if (cqr->status == DASD_CQR_QUEUED ||
1205 cqr->status == DASD_CQR_IN_IO ||
1206 cqr->status == DASD_CQR_CLEAR_PENDING)
1207 break;
1208 if (cqr->status == DASD_CQR_ERROR) {
1209 __dasd_device_recovery(device, cqr);
1211 /* Rechain finished requests to final queue */
1212 list_move_tail(&cqr->devlist, final_queue);
1217 * the cqrs from the final queue are returned to the upper layer
1218 * by setting a dasd_block state and calling the callback function
1220 static void __dasd_device_process_final_queue(struct dasd_device *device,
1221 struct list_head *final_queue)
1223 struct list_head *l, *n;
1224 struct dasd_ccw_req *cqr;
1225 struct dasd_block *block;
1226 void (*callback)(struct dasd_ccw_req *, void *data);
1227 void *callback_data;
1228 char errorstring[ERRORLENGTH];
1230 list_for_each_safe(l, n, final_queue) {
1231 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1232 list_del_init(&cqr->devlist);
1233 block = cqr->block;
1234 callback = cqr->callback;
1235 callback_data = cqr->callback_data;
1236 if (block)
1237 spin_lock_bh(&block->queue_lock);
1238 switch (cqr->status) {
1239 case DASD_CQR_SUCCESS:
1240 cqr->status = DASD_CQR_DONE;
1241 break;
1242 case DASD_CQR_ERROR:
1243 cqr->status = DASD_CQR_NEED_ERP;
1244 break;
1245 case DASD_CQR_CLEARED:
1246 cqr->status = DASD_CQR_TERMINATED;
1247 break;
1248 default:
1249 /* internal error 12 - wrong cqr status*/
1250 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1251 dev_err(&device->cdev->dev,
1252 "An error occurred in the DASD device driver, "
1253 "reason=%s\n", errorstring);
1254 BUG();
1256 if (cqr->callback != NULL)
1257 (callback)(cqr, callback_data);
1258 if (block)
1259 spin_unlock_bh(&block->queue_lock);
1264 * Take a look at the first request on the ccw queue and check
1265 * if it reached its expire time. If so, terminate the IO.
1267 static void __dasd_device_check_expire(struct dasd_device *device)
1269 struct dasd_ccw_req *cqr;
1271 if (list_empty(&device->ccw_queue))
1272 return;
1273 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1274 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1275 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1276 if (device->discipline->term_IO(cqr) != 0) {
1277 /* Hmpf, try again in 5 sec */
1278 dev_err(&device->cdev->dev,
1279 "cqr %p timed out (%is) but cannot be "
1280 "ended, retrying in 5 s\n",
1281 cqr, (cqr->expires/HZ));
1282 cqr->expires += 5*HZ;
1283 dasd_device_set_timer(device, 5*HZ);
1284 } else {
1285 dev_err(&device->cdev->dev,
1286 "cqr %p timed out (%is), %i retries "
1287 "remaining\n", cqr, (cqr->expires/HZ),
1288 cqr->retries);
1294 * Take a look at the first request on the ccw queue and check
1295 * if it needs to be started.
1297 static void __dasd_device_start_head(struct dasd_device *device)
1299 struct dasd_ccw_req *cqr;
1300 int rc;
1302 if (list_empty(&device->ccw_queue))
1303 return;
1304 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1305 if (cqr->status != DASD_CQR_QUEUED)
1306 return;
1307 /* when device is stopped, return request to previous layer */
1308 if (device->stopped) {
1309 cqr->status = DASD_CQR_CLEARED;
1310 dasd_schedule_device_bh(device);
1311 return;
1314 rc = device->discipline->start_IO(cqr);
1315 if (rc == 0)
1316 dasd_device_set_timer(device, cqr->expires);
1317 else if (rc == -EACCES) {
1318 dasd_schedule_device_bh(device);
1319 } else
1320 /* Hmpf, try again in 1/2 sec */
1321 dasd_device_set_timer(device, 50);
1325 * Go through all request on the dasd_device request queue,
1326 * terminate them on the cdev if necessary, and return them to the
1327 * submitting layer via callback.
1328 * Note:
1329 * Make sure that all 'submitting layers' still exist when
1330 * this function is called!. In other words, when 'device' is a base
1331 * device then all block layer requests must have been removed before
1332 * via dasd_flush_block_queue.
1334 int dasd_flush_device_queue(struct dasd_device *device)
1336 struct dasd_ccw_req *cqr, *n;
1337 int rc;
1338 struct list_head flush_queue;
1340 INIT_LIST_HEAD(&flush_queue);
1341 spin_lock_irq(get_ccwdev_lock(device->cdev));
1342 rc = 0;
1343 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1344 /* Check status and move request to flush_queue */
1345 switch (cqr->status) {
1346 case DASD_CQR_IN_IO:
1347 rc = device->discipline->term_IO(cqr);
1348 if (rc) {
1349 /* unable to terminate requeust */
1350 dev_err(&device->cdev->dev,
1351 "Flushing the DASD request queue "
1352 "failed for request %p\n", cqr);
1353 /* stop flush processing */
1354 goto finished;
1356 break;
1357 case DASD_CQR_QUEUED:
1358 cqr->stopclk = get_clock();
1359 cqr->status = DASD_CQR_CLEARED;
1360 break;
1361 default: /* no need to modify the others */
1362 break;
1364 list_move_tail(&cqr->devlist, &flush_queue);
1366 finished:
1367 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1369 * After this point all requests must be in state CLEAR_PENDING,
1370 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1371 * one of the others.
1373 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1374 wait_event(dasd_flush_wq,
1375 (cqr->status != DASD_CQR_CLEAR_PENDING));
1377 * Now set each request back to TERMINATED, DONE or NEED_ERP
1378 * and call the callback function of flushed requests
1380 __dasd_device_process_final_queue(device, &flush_queue);
1381 return rc;
1385 * Acquire the device lock and process queues for the device.
1387 static void dasd_device_tasklet(struct dasd_device *device)
1389 struct list_head final_queue;
1391 atomic_set (&device->tasklet_scheduled, 0);
1392 INIT_LIST_HEAD(&final_queue);
1393 spin_lock_irq(get_ccwdev_lock(device->cdev));
1394 /* Check expire time of first request on the ccw queue. */
1395 __dasd_device_check_expire(device);
1396 /* find final requests on ccw queue */
1397 __dasd_device_process_ccw_queue(device, &final_queue);
1398 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1399 /* Now call the callback function of requests with final status */
1400 __dasd_device_process_final_queue(device, &final_queue);
1401 spin_lock_irq(get_ccwdev_lock(device->cdev));
1402 /* Now check if the head of the ccw queue needs to be started. */
1403 __dasd_device_start_head(device);
1404 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1405 dasd_put_device(device);
1409 * Schedules a call to dasd_tasklet over the device tasklet.
1411 void dasd_schedule_device_bh(struct dasd_device *device)
1413 /* Protect against rescheduling. */
1414 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1415 return;
1416 dasd_get_device(device);
1417 tasklet_hi_schedule(&device->tasklet);
1420 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1422 device->stopped |= bits;
1424 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1426 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1428 device->stopped &= ~bits;
1429 if (!device->stopped)
1430 wake_up(&generic_waitq);
1432 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1435 * Queue a request to the head of the device ccw_queue.
1436 * Start the I/O if possible.
1438 void dasd_add_request_head(struct dasd_ccw_req *cqr)
1440 struct dasd_device *device;
1441 unsigned long flags;
1443 device = cqr->startdev;
1444 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1445 cqr->status = DASD_CQR_QUEUED;
1446 list_add(&cqr->devlist, &device->ccw_queue);
1447 /* let the bh start the request to keep them in order */
1448 dasd_schedule_device_bh(device);
1449 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1453 * Queue a request to the tail of the device ccw_queue.
1454 * Start the I/O if possible.
1456 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1458 struct dasd_device *device;
1459 unsigned long flags;
1461 device = cqr->startdev;
1462 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1463 cqr->status = DASD_CQR_QUEUED;
1464 list_add_tail(&cqr->devlist, &device->ccw_queue);
1465 /* let the bh start the request to keep them in order */
1466 dasd_schedule_device_bh(device);
1467 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1471 * Wakeup helper for the 'sleep_on' functions.
1473 static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1475 wake_up((wait_queue_head_t *) data);
1478 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1480 struct dasd_device *device;
1481 int rc;
1483 device = cqr->startdev;
1484 spin_lock_irq(get_ccwdev_lock(device->cdev));
1485 rc = ((cqr->status == DASD_CQR_DONE ||
1486 cqr->status == DASD_CQR_NEED_ERP ||
1487 cqr->status == DASD_CQR_TERMINATED) &&
1488 list_empty(&cqr->devlist));
1489 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1490 return rc;
1494 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1496 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1498 struct dasd_device *device;
1499 dasd_erp_fn_t erp_fn;
1501 if (cqr->status == DASD_CQR_FILLED)
1502 return 0;
1503 device = cqr->startdev;
1504 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1505 if (cqr->status == DASD_CQR_TERMINATED) {
1506 device->discipline->handle_terminated_request(cqr);
1507 return 1;
1509 if (cqr->status == DASD_CQR_NEED_ERP) {
1510 erp_fn = device->discipline->erp_action(cqr);
1511 erp_fn(cqr);
1512 return 1;
1514 if (cqr->status == DASD_CQR_FAILED)
1515 dasd_log_sense(cqr, &cqr->irb);
1516 if (cqr->refers) {
1517 __dasd_process_erp(device, cqr);
1518 return 1;
1521 return 0;
1524 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1526 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1527 if (cqr->refers) /* erp is not done yet */
1528 return 1;
1529 return ((cqr->status != DASD_CQR_DONE) &&
1530 (cqr->status != DASD_CQR_FAILED));
1531 } else
1532 return (cqr->status == DASD_CQR_FILLED);
1535 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1537 struct dasd_device *device;
1538 int rc;
1539 struct list_head ccw_queue;
1540 struct dasd_ccw_req *cqr;
1542 INIT_LIST_HEAD(&ccw_queue);
1543 maincqr->status = DASD_CQR_FILLED;
1544 device = maincqr->startdev;
1545 list_add(&maincqr->blocklist, &ccw_queue);
1546 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1547 cqr = list_first_entry(&ccw_queue,
1548 struct dasd_ccw_req, blocklist)) {
1550 if (__dasd_sleep_on_erp(cqr))
1551 continue;
1552 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1553 continue;
1555 /* Non-temporary stop condition will trigger fail fast */
1556 if (device->stopped & ~DASD_STOPPED_PENDING &&
1557 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1558 (!dasd_eer_enabled(device))) {
1559 cqr->status = DASD_CQR_FAILED;
1560 continue;
1563 /* Don't try to start requests if device is stopped */
1564 if (interruptible) {
1565 rc = wait_event_interruptible(
1566 generic_waitq, !(device->stopped));
1567 if (rc == -ERESTARTSYS) {
1568 cqr->status = DASD_CQR_FAILED;
1569 maincqr->intrc = rc;
1570 continue;
1572 } else
1573 wait_event(generic_waitq, !(device->stopped));
1575 cqr->callback = dasd_wakeup_cb;
1576 cqr->callback_data = (void *) &generic_waitq;
1577 dasd_add_request_tail(cqr);
1578 if (interruptible) {
1579 rc = wait_event_interruptible(
1580 generic_waitq, _wait_for_wakeup(cqr));
1581 if (rc == -ERESTARTSYS) {
1582 dasd_cancel_req(cqr);
1583 /* wait (non-interruptible) for final status */
1584 wait_event(generic_waitq,
1585 _wait_for_wakeup(cqr));
1586 cqr->status = DASD_CQR_FAILED;
1587 maincqr->intrc = rc;
1588 continue;
1590 } else
1591 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1594 maincqr->endclk = get_clock();
1595 if ((maincqr->status != DASD_CQR_DONE) &&
1596 (maincqr->intrc != -ERESTARTSYS))
1597 dasd_log_sense(maincqr, &maincqr->irb);
1598 if (maincqr->status == DASD_CQR_DONE)
1599 rc = 0;
1600 else if (maincqr->intrc)
1601 rc = maincqr->intrc;
1602 else
1603 rc = -EIO;
1604 return rc;
1608 * Queue a request to the tail of the device ccw_queue and wait for
1609 * it's completion.
1611 int dasd_sleep_on(struct dasd_ccw_req *cqr)
1613 return _dasd_sleep_on(cqr, 0);
1617 * Queue a request to the tail of the device ccw_queue and wait
1618 * interruptible for it's completion.
1620 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1622 return _dasd_sleep_on(cqr, 1);
1626 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1627 * for eckd devices) the currently running request has to be terminated
1628 * and be put back to status queued, before the special request is added
1629 * to the head of the queue. Then the special request is waited on normally.
1631 static inline int _dasd_term_running_cqr(struct dasd_device *device)
1633 struct dasd_ccw_req *cqr;
1635 if (list_empty(&device->ccw_queue))
1636 return 0;
1637 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1638 return device->discipline->term_IO(cqr);
1641 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1643 struct dasd_device *device;
1644 int rc;
1646 device = cqr->startdev;
1647 spin_lock_irq(get_ccwdev_lock(device->cdev));
1648 rc = _dasd_term_running_cqr(device);
1649 if (rc) {
1650 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1651 return rc;
1654 cqr->callback = dasd_wakeup_cb;
1655 cqr->callback_data = (void *) &generic_waitq;
1656 cqr->status = DASD_CQR_QUEUED;
1657 list_add(&cqr->devlist, &device->ccw_queue);
1659 /* let the bh start the request to keep them in order */
1660 dasd_schedule_device_bh(device);
1662 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1664 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1666 if (cqr->status == DASD_CQR_DONE)
1667 rc = 0;
1668 else if (cqr->intrc)
1669 rc = cqr->intrc;
1670 else
1671 rc = -EIO;
1672 return rc;
1676 * Cancels a request that was started with dasd_sleep_on_req.
1677 * This is useful to timeout requests. The request will be
1678 * terminated if it is currently in i/o.
1679 * Returns 1 if the request has been terminated.
1680 * 0 if there was no need to terminate the request (not started yet)
1681 * negative error code if termination failed
1682 * Cancellation of a request is an asynchronous operation! The calling
1683 * function has to wait until the request is properly returned via callback.
1685 int dasd_cancel_req(struct dasd_ccw_req *cqr)
1687 struct dasd_device *device = cqr->startdev;
1688 unsigned long flags;
1689 int rc;
1691 rc = 0;
1692 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1693 switch (cqr->status) {
1694 case DASD_CQR_QUEUED:
1695 /* request was not started - just set to cleared */
1696 cqr->status = DASD_CQR_CLEARED;
1697 break;
1698 case DASD_CQR_IN_IO:
1699 /* request in IO - terminate IO and release again */
1700 rc = device->discipline->term_IO(cqr);
1701 if (rc) {
1702 dev_err(&device->cdev->dev,
1703 "Cancelling request %p failed with rc=%d\n",
1704 cqr, rc);
1705 } else {
1706 cqr->stopclk = get_clock();
1708 break;
1709 default: /* already finished or clear pending - do nothing */
1710 break;
1712 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1713 dasd_schedule_device_bh(device);
1714 return rc;
1719 * SECTION: Operations of the dasd_block layer.
1723 * Timeout function for dasd_block. This is used when the block layer
1724 * is waiting for something that may not come reliably, (e.g. a state
1725 * change interrupt)
1727 static void dasd_block_timeout(unsigned long ptr)
1729 unsigned long flags;
1730 struct dasd_block *block;
1732 block = (struct dasd_block *) ptr;
1733 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1734 /* re-activate request queue */
1735 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
1736 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1737 dasd_schedule_block_bh(block);
1741 * Setup timeout for a dasd_block in jiffies.
1743 void dasd_block_set_timer(struct dasd_block *block, int expires)
1745 if (expires == 0)
1746 del_timer(&block->timer);
1747 else
1748 mod_timer(&block->timer, jiffies + expires);
1752 * Clear timeout for a dasd_block.
1754 void dasd_block_clear_timer(struct dasd_block *block)
1756 del_timer(&block->timer);
1760 * Process finished error recovery ccw.
1762 static void __dasd_process_erp(struct dasd_device *device,
1763 struct dasd_ccw_req *cqr)
1765 dasd_erp_fn_t erp_fn;
1767 if (cqr->status == DASD_CQR_DONE)
1768 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1769 else
1770 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
1771 erp_fn = device->discipline->erp_postaction(cqr);
1772 erp_fn(cqr);
1776 * Fetch requests from the block device queue.
1778 static void __dasd_process_request_queue(struct dasd_block *block)
1780 struct request_queue *queue;
1781 struct request *req;
1782 struct dasd_ccw_req *cqr;
1783 struct dasd_device *basedev;
1784 unsigned long flags;
1785 queue = block->request_queue;
1786 basedev = block->base;
1787 /* No queue ? Then there is nothing to do. */
1788 if (queue == NULL)
1789 return;
1792 * We requeue request from the block device queue to the ccw
1793 * queue only in two states. In state DASD_STATE_READY the
1794 * partition detection is done and we need to requeue requests
1795 * for that. State DASD_STATE_ONLINE is normal block device
1796 * operation.
1798 if (basedev->state < DASD_STATE_READY) {
1799 while ((req = blk_fetch_request(block->request_queue)))
1800 __blk_end_request_all(req, -EIO);
1801 return;
1803 /* Now we try to fetch requests from the request queue */
1804 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
1805 if (basedev->features & DASD_FEATURE_READONLY &&
1806 rq_data_dir(req) == WRITE) {
1807 DBF_DEV_EVENT(DBF_ERR, basedev,
1808 "Rejecting write request %p",
1809 req);
1810 blk_start_request(req);
1811 __blk_end_request_all(req, -EIO);
1812 continue;
1814 cqr = basedev->discipline->build_cp(basedev, block, req);
1815 if (IS_ERR(cqr)) {
1816 if (PTR_ERR(cqr) == -EBUSY)
1817 break; /* normal end condition */
1818 if (PTR_ERR(cqr) == -ENOMEM)
1819 break; /* terminate request queue loop */
1820 if (PTR_ERR(cqr) == -EAGAIN) {
1822 * The current request cannot be build right
1823 * now, we have to try later. If this request
1824 * is the head-of-queue we stop the device
1825 * for 1/2 second.
1827 if (!list_empty(&block->ccw_queue))
1828 break;
1829 spin_lock_irqsave(
1830 get_ccwdev_lock(basedev->cdev), flags);
1831 dasd_device_set_stop_bits(basedev,
1832 DASD_STOPPED_PENDING);
1833 spin_unlock_irqrestore(
1834 get_ccwdev_lock(basedev->cdev), flags);
1835 dasd_block_set_timer(block, HZ/2);
1836 break;
1838 DBF_DEV_EVENT(DBF_ERR, basedev,
1839 "CCW creation failed (rc=%ld) "
1840 "on request %p",
1841 PTR_ERR(cqr), req);
1842 blk_start_request(req);
1843 __blk_end_request_all(req, -EIO);
1844 continue;
1847 * Note: callback is set to dasd_return_cqr_cb in
1848 * __dasd_block_start_head to cover erp requests as well
1850 cqr->callback_data = (void *) req;
1851 cqr->status = DASD_CQR_FILLED;
1852 blk_start_request(req);
1853 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1854 dasd_profile_start(block, cqr, req);
1858 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1860 struct request *req;
1861 int status;
1862 int error = 0;
1864 req = (struct request *) cqr->callback_data;
1865 dasd_profile_end(cqr->block, cqr, req);
1866 status = cqr->block->base->discipline->free_cp(cqr, req);
1867 if (status <= 0)
1868 error = status ? status : -EIO;
1869 __blk_end_request_all(req, error);
1873 * Process ccw request queue.
1875 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1876 struct list_head *final_queue)
1878 struct list_head *l, *n;
1879 struct dasd_ccw_req *cqr;
1880 dasd_erp_fn_t erp_fn;
1881 unsigned long flags;
1882 struct dasd_device *base = block->base;
1884 restart:
1885 /* Process request with final status. */
1886 list_for_each_safe(l, n, &block->ccw_queue) {
1887 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1888 if (cqr->status != DASD_CQR_DONE &&
1889 cqr->status != DASD_CQR_FAILED &&
1890 cqr->status != DASD_CQR_NEED_ERP &&
1891 cqr->status != DASD_CQR_TERMINATED)
1892 continue;
1894 if (cqr->status == DASD_CQR_TERMINATED) {
1895 base->discipline->handle_terminated_request(cqr);
1896 goto restart;
1899 /* Process requests that may be recovered */
1900 if (cqr->status == DASD_CQR_NEED_ERP) {
1901 erp_fn = base->discipline->erp_action(cqr);
1902 if (IS_ERR(erp_fn(cqr)))
1903 continue;
1904 goto restart;
1907 /* log sense for fatal error */
1908 if (cqr->status == DASD_CQR_FAILED) {
1909 dasd_log_sense(cqr, &cqr->irb);
1912 /* First of all call extended error reporting. */
1913 if (dasd_eer_enabled(base) &&
1914 cqr->status == DASD_CQR_FAILED) {
1915 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1917 /* restart request */
1918 cqr->status = DASD_CQR_FILLED;
1919 cqr->retries = 255;
1920 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1921 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
1922 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1923 flags);
1924 goto restart;
1927 /* Process finished ERP request. */
1928 if (cqr->refers) {
1929 __dasd_process_erp(base, cqr);
1930 goto restart;
1933 /* Rechain finished requests to final queue */
1934 cqr->endclk = get_clock();
1935 list_move_tail(&cqr->blocklist, final_queue);
1939 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1941 dasd_schedule_block_bh(cqr->block);
1944 static void __dasd_block_start_head(struct dasd_block *block)
1946 struct dasd_ccw_req *cqr;
1948 if (list_empty(&block->ccw_queue))
1949 return;
1950 /* We allways begin with the first requests on the queue, as some
1951 * of previously started requests have to be enqueued on a
1952 * dasd_device again for error recovery.
1954 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1955 if (cqr->status != DASD_CQR_FILLED)
1956 continue;
1957 /* Non-temporary stop condition will trigger fail fast */
1958 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1959 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1960 (!dasd_eer_enabled(block->base))) {
1961 cqr->status = DASD_CQR_FAILED;
1962 dasd_schedule_block_bh(block);
1963 continue;
1965 /* Don't try to start requests if device is stopped */
1966 if (block->base->stopped)
1967 return;
1969 /* just a fail safe check, should not happen */
1970 if (!cqr->startdev)
1971 cqr->startdev = block->base;
1973 /* make sure that the requests we submit find their way back */
1974 cqr->callback = dasd_return_cqr_cb;
1976 dasd_add_request_tail(cqr);
1981 * Central dasd_block layer routine. Takes requests from the generic
1982 * block layer request queue, creates ccw requests, enqueues them on
1983 * a dasd_device and processes ccw requests that have been returned.
1985 static void dasd_block_tasklet(struct dasd_block *block)
1987 struct list_head final_queue;
1988 struct list_head *l, *n;
1989 struct dasd_ccw_req *cqr;
1991 atomic_set(&block->tasklet_scheduled, 0);
1992 INIT_LIST_HEAD(&final_queue);
1993 spin_lock(&block->queue_lock);
1994 /* Finish off requests on ccw queue */
1995 __dasd_process_block_ccw_queue(block, &final_queue);
1996 spin_unlock(&block->queue_lock);
1997 /* Now call the callback function of requests with final status */
1998 spin_lock_irq(&block->request_queue_lock);
1999 list_for_each_safe(l, n, &final_queue) {
2000 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2001 list_del_init(&cqr->blocklist);
2002 __dasd_cleanup_cqr(cqr);
2004 spin_lock(&block->queue_lock);
2005 /* Get new request from the block device request queue */
2006 __dasd_process_request_queue(block);
2007 /* Now check if the head of the ccw queue needs to be started. */
2008 __dasd_block_start_head(block);
2009 spin_unlock(&block->queue_lock);
2010 spin_unlock_irq(&block->request_queue_lock);
2011 dasd_put_device(block->base);
2014 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2016 wake_up(&dasd_flush_wq);
2020 * Go through all request on the dasd_block request queue, cancel them
2021 * on the respective dasd_device, and return them to the generic
2022 * block layer.
2024 static int dasd_flush_block_queue(struct dasd_block *block)
2026 struct dasd_ccw_req *cqr, *n;
2027 int rc, i;
2028 struct list_head flush_queue;
2030 INIT_LIST_HEAD(&flush_queue);
2031 spin_lock_bh(&block->queue_lock);
2032 rc = 0;
2033 restart:
2034 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2035 /* if this request currently owned by a dasd_device cancel it */
2036 if (cqr->status >= DASD_CQR_QUEUED)
2037 rc = dasd_cancel_req(cqr);
2038 if (rc < 0)
2039 break;
2040 /* Rechain request (including erp chain) so it won't be
2041 * touched by the dasd_block_tasklet anymore.
2042 * Replace the callback so we notice when the request
2043 * is returned from the dasd_device layer.
2045 cqr->callback = _dasd_wake_block_flush_cb;
2046 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2047 list_move_tail(&cqr->blocklist, &flush_queue);
2048 if (i > 1)
2049 /* moved more than one request - need to restart */
2050 goto restart;
2052 spin_unlock_bh(&block->queue_lock);
2053 /* Now call the callback function of flushed requests */
2054 restart_cb:
2055 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2056 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2057 /* Process finished ERP request. */
2058 if (cqr->refers) {
2059 spin_lock_bh(&block->queue_lock);
2060 __dasd_process_erp(block->base, cqr);
2061 spin_unlock_bh(&block->queue_lock);
2062 /* restart list_for_xx loop since dasd_process_erp
2063 * might remove multiple elements */
2064 goto restart_cb;
2066 /* call the callback function */
2067 spin_lock_irq(&block->request_queue_lock);
2068 cqr->endclk = get_clock();
2069 list_del_init(&cqr->blocklist);
2070 __dasd_cleanup_cqr(cqr);
2071 spin_unlock_irq(&block->request_queue_lock);
2073 return rc;
2077 * Schedules a call to dasd_tasklet over the device tasklet.
2079 void dasd_schedule_block_bh(struct dasd_block *block)
2081 /* Protect against rescheduling. */
2082 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2083 return;
2084 /* life cycle of block is bound to it's base device */
2085 dasd_get_device(block->base);
2086 tasklet_hi_schedule(&block->tasklet);
2091 * SECTION: external block device operations
2092 * (request queue handling, open, release, etc.)
2096 * Dasd request queue function. Called from ll_rw_blk.c
2098 static void do_dasd_request(struct request_queue *queue)
2100 struct dasd_block *block;
2102 block = queue->queuedata;
2103 spin_lock(&block->queue_lock);
2104 /* Get new request from the block device request queue */
2105 __dasd_process_request_queue(block);
2106 /* Now check if the head of the ccw queue needs to be started. */
2107 __dasd_block_start_head(block);
2108 spin_unlock(&block->queue_lock);
2112 * Allocate and initialize request queue and default I/O scheduler.
2114 static int dasd_alloc_queue(struct dasd_block *block)
2116 int rc;
2118 block->request_queue = blk_init_queue(do_dasd_request,
2119 &block->request_queue_lock);
2120 if (block->request_queue == NULL)
2121 return -ENOMEM;
2123 block->request_queue->queuedata = block;
2125 elevator_exit(block->request_queue->elevator);
2126 block->request_queue->elevator = NULL;
2127 rc = elevator_init(block->request_queue, "deadline");
2128 if (rc) {
2129 blk_cleanup_queue(block->request_queue);
2130 return rc;
2132 return 0;
2136 * Allocate and initialize request queue.
2138 static void dasd_setup_queue(struct dasd_block *block)
2140 int max;
2142 blk_queue_logical_block_size(block->request_queue, block->bp_block);
2143 max = block->base->discipline->max_blocks << block->s2b_shift;
2144 blk_queue_max_hw_sectors(block->request_queue, max);
2145 blk_queue_max_segments(block->request_queue, -1L);
2146 /* with page sized segments we can translate each segement into
2147 * one idaw/tidaw
2149 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2150 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2151 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
2155 * Deactivate and free request queue.
2157 static void dasd_free_queue(struct dasd_block *block)
2159 if (block->request_queue) {
2160 blk_cleanup_queue(block->request_queue);
2161 block->request_queue = NULL;
2166 * Flush request on the request queue.
2168 static void dasd_flush_request_queue(struct dasd_block *block)
2170 struct request *req;
2172 if (!block->request_queue)
2173 return;
2175 spin_lock_irq(&block->request_queue_lock);
2176 while ((req = blk_fetch_request(block->request_queue)))
2177 __blk_end_request_all(req, -EIO);
2178 spin_unlock_irq(&block->request_queue_lock);
2181 static int dasd_open(struct block_device *bdev, fmode_t mode)
2183 struct dasd_block *block = bdev->bd_disk->private_data;
2184 struct dasd_device *base;
2185 int rc;
2187 if (!block)
2188 return -ENODEV;
2190 base = block->base;
2191 atomic_inc(&block->open_count);
2192 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2193 rc = -ENODEV;
2194 goto unlock;
2197 if (!try_module_get(base->discipline->owner)) {
2198 rc = -EINVAL;
2199 goto unlock;
2202 if (dasd_probeonly) {
2203 dev_info(&base->cdev->dev,
2204 "Accessing the DASD failed because it is in "
2205 "probeonly mode\n");
2206 rc = -EPERM;
2207 goto out;
2210 if (base->state <= DASD_STATE_BASIC) {
2211 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2212 " Cannot open unrecognized device");
2213 rc = -ENODEV;
2214 goto out;
2217 if ((mode & FMODE_WRITE) &&
2218 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2219 (base->features & DASD_FEATURE_READONLY))) {
2220 rc = -EROFS;
2221 goto out;
2224 return 0;
2226 out:
2227 module_put(base->discipline->owner);
2228 unlock:
2229 atomic_dec(&block->open_count);
2230 return rc;
2233 static int dasd_release(struct gendisk *disk, fmode_t mode)
2235 struct dasd_block *block = disk->private_data;
2237 atomic_dec(&block->open_count);
2238 module_put(block->base->discipline->owner);
2239 return 0;
2243 * Return disk geometry.
2245 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2247 struct dasd_block *block;
2248 struct dasd_device *base;
2250 block = bdev->bd_disk->private_data;
2251 if (!block)
2252 return -ENODEV;
2253 base = block->base;
2255 if (!base->discipline ||
2256 !base->discipline->fill_geometry)
2257 return -EINVAL;
2259 base->discipline->fill_geometry(block, geo);
2260 geo->start = get_start_sect(bdev) >> block->s2b_shift;
2261 return 0;
2264 const struct block_device_operations
2265 dasd_device_operations = {
2266 .owner = THIS_MODULE,
2267 .open = dasd_open,
2268 .release = dasd_release,
2269 .ioctl = dasd_ioctl,
2270 .compat_ioctl = dasd_ioctl,
2271 .getgeo = dasd_getgeo,
2274 /*******************************************************************************
2275 * end of block device operations
2278 static void
2279 dasd_exit(void)
2281 #ifdef CONFIG_PROC_FS
2282 dasd_proc_exit();
2283 #endif
2284 dasd_eer_exit();
2285 if (dasd_page_cache != NULL) {
2286 kmem_cache_destroy(dasd_page_cache);
2287 dasd_page_cache = NULL;
2289 dasd_gendisk_exit();
2290 dasd_devmap_exit();
2291 if (dasd_debug_area != NULL) {
2292 debug_unregister(dasd_debug_area);
2293 dasd_debug_area = NULL;
2298 * SECTION: common functions for ccw_driver use
2302 * Is the device read-only?
2303 * Note that this function does not report the setting of the
2304 * readonly device attribute, but how it is configured in z/VM.
2306 int dasd_device_is_ro(struct dasd_device *device)
2308 struct ccw_dev_id dev_id;
2309 struct diag210 diag_data;
2310 int rc;
2312 if (!MACHINE_IS_VM)
2313 return 0;
2314 ccw_device_get_id(device->cdev, &dev_id);
2315 memset(&diag_data, 0, sizeof(diag_data));
2316 diag_data.vrdcdvno = dev_id.devno;
2317 diag_data.vrdclen = sizeof(diag_data);
2318 rc = diag210(&diag_data);
2319 if (rc == 0 || rc == 2) {
2320 return diag_data.vrdcvfla & 0x80;
2321 } else {
2322 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2323 dev_id.devno, rc);
2324 return 0;
2327 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2329 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2331 struct ccw_device *cdev = data;
2332 int ret;
2334 ret = ccw_device_set_online(cdev);
2335 if (ret)
2336 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2337 dev_name(&cdev->dev), ret);
2341 * Initial attempt at a probe function. this can be simplified once
2342 * the other detection code is gone.
2344 int dasd_generic_probe(struct ccw_device *cdev,
2345 struct dasd_discipline *discipline)
2347 int ret;
2349 ret = dasd_add_sysfs_files(cdev);
2350 if (ret) {
2351 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2352 "dasd_generic_probe: could not add "
2353 "sysfs entries");
2354 return ret;
2356 cdev->handler = &dasd_int_handler;
2359 * Automatically online either all dasd devices (dasd_autodetect)
2360 * or all devices specified with dasd= parameters during
2361 * initial probe.
2363 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2364 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
2365 async_schedule(dasd_generic_auto_online, cdev);
2366 return 0;
2370 * This will one day be called from a global not_oper handler.
2371 * It is also used by driver_unregister during module unload.
2373 void dasd_generic_remove(struct ccw_device *cdev)
2375 struct dasd_device *device;
2376 struct dasd_block *block;
2378 cdev->handler = NULL;
2380 dasd_remove_sysfs_files(cdev);
2381 device = dasd_device_from_cdev(cdev);
2382 if (IS_ERR(device))
2383 return;
2384 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2385 /* Already doing offline processing */
2386 dasd_put_device(device);
2387 return;
2390 * This device is removed unconditionally. Set offline
2391 * flag to prevent dasd_open from opening it while it is
2392 * no quite down yet.
2394 dasd_set_target_state(device, DASD_STATE_NEW);
2395 /* dasd_delete_device destroys the device reference. */
2396 block = device->block;
2397 device->block = NULL;
2398 dasd_delete_device(device);
2400 * life cycle of block is bound to device, so delete it after
2401 * device was safely removed
2403 if (block)
2404 dasd_free_block(block);
2408 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
2409 * the device is detected for the first time and is supposed to be used
2410 * or the user has started activation through sysfs.
2412 int dasd_generic_set_online(struct ccw_device *cdev,
2413 struct dasd_discipline *base_discipline)
2415 struct dasd_discipline *discipline;
2416 struct dasd_device *device;
2417 int rc;
2419 /* first online clears initial online feature flag */
2420 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
2421 device = dasd_create_device(cdev);
2422 if (IS_ERR(device))
2423 return PTR_ERR(device);
2425 discipline = base_discipline;
2426 if (device->features & DASD_FEATURE_USEDIAG) {
2427 if (!dasd_diag_discipline_pointer) {
2428 pr_warning("%s Setting the DASD online failed because "
2429 "of missing DIAG discipline\n",
2430 dev_name(&cdev->dev));
2431 dasd_delete_device(device);
2432 return -ENODEV;
2434 discipline = dasd_diag_discipline_pointer;
2436 if (!try_module_get(base_discipline->owner)) {
2437 dasd_delete_device(device);
2438 return -EINVAL;
2440 if (!try_module_get(discipline->owner)) {
2441 module_put(base_discipline->owner);
2442 dasd_delete_device(device);
2443 return -EINVAL;
2445 device->base_discipline = base_discipline;
2446 device->discipline = discipline;
2448 /* check_device will allocate block device if necessary */
2449 rc = discipline->check_device(device);
2450 if (rc) {
2451 pr_warning("%s Setting the DASD online with discipline %s "
2452 "failed with rc=%i\n",
2453 dev_name(&cdev->dev), discipline->name, rc);
2454 module_put(discipline->owner);
2455 module_put(base_discipline->owner);
2456 dasd_delete_device(device);
2457 return rc;
2460 dasd_set_target_state(device, DASD_STATE_ONLINE);
2461 if (device->state <= DASD_STATE_KNOWN) {
2462 pr_warning("%s Setting the DASD online failed because of a "
2463 "missing discipline\n", dev_name(&cdev->dev));
2464 rc = -ENODEV;
2465 dasd_set_target_state(device, DASD_STATE_NEW);
2466 if (device->block)
2467 dasd_free_block(device->block);
2468 dasd_delete_device(device);
2469 } else
2470 pr_debug("dasd_generic device %s found\n",
2471 dev_name(&cdev->dev));
2473 wait_event(dasd_init_waitq, _wait_for_device(device));
2475 dasd_put_device(device);
2476 return rc;
2479 int dasd_generic_set_offline(struct ccw_device *cdev)
2481 struct dasd_device *device;
2482 struct dasd_block *block;
2483 int max_count, open_count;
2485 device = dasd_device_from_cdev(cdev);
2486 if (IS_ERR(device))
2487 return PTR_ERR(device);
2488 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2489 /* Already doing offline processing */
2490 dasd_put_device(device);
2491 return 0;
2494 * We must make sure that this device is currently not in use.
2495 * The open_count is increased for every opener, that includes
2496 * the blkdev_get in dasd_scan_partitions. We are only interested
2497 * in the other openers.
2499 if (device->block) {
2500 max_count = device->block->bdev ? 0 : -1;
2501 open_count = atomic_read(&device->block->open_count);
2502 if (open_count > max_count) {
2503 if (open_count > 0)
2504 pr_warning("%s: The DASD cannot be set offline "
2505 "with open count %i\n",
2506 dev_name(&cdev->dev), open_count);
2507 else
2508 pr_warning("%s: The DASD cannot be set offline "
2509 "while it is in use\n",
2510 dev_name(&cdev->dev));
2511 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2512 dasd_put_device(device);
2513 return -EBUSY;
2516 dasd_set_target_state(device, DASD_STATE_NEW);
2517 /* dasd_delete_device destroys the device reference. */
2518 block = device->block;
2519 device->block = NULL;
2520 dasd_delete_device(device);
2522 * life cycle of block is bound to device, so delete it after
2523 * device was safely removed
2525 if (block)
2526 dasd_free_block(block);
2527 return 0;
2530 int dasd_generic_notify(struct ccw_device *cdev, int event)
2532 struct dasd_device *device;
2533 struct dasd_ccw_req *cqr;
2534 int ret;
2536 device = dasd_device_from_cdev_locked(cdev);
2537 if (IS_ERR(device))
2538 return 0;
2539 ret = 0;
2540 switch (event) {
2541 case CIO_GONE:
2542 case CIO_BOXED:
2543 case CIO_NO_PATH:
2544 /* First of all call extended error reporting. */
2545 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2547 if (device->state < DASD_STATE_BASIC)
2548 break;
2549 /* Device is active. We want to keep it. */
2550 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2551 if (cqr->status == DASD_CQR_IN_IO) {
2552 cqr->status = DASD_CQR_QUEUED;
2553 cqr->retries++;
2555 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
2556 dasd_device_clear_timer(device);
2557 dasd_schedule_device_bh(device);
2558 ret = 1;
2559 break;
2560 case CIO_OPER:
2561 /* FIXME: add a sanity check. */
2562 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
2563 if (device->stopped & DASD_UNRESUMED_PM) {
2564 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
2565 dasd_restore_device(device);
2566 ret = 1;
2567 break;
2569 dasd_schedule_device_bh(device);
2570 if (device->block)
2571 dasd_schedule_block_bh(device->block);
2572 ret = 1;
2573 break;
2575 dasd_put_device(device);
2576 return ret;
2579 int dasd_generic_pm_freeze(struct ccw_device *cdev)
2581 struct dasd_ccw_req *cqr, *n;
2582 int rc;
2583 struct list_head freeze_queue;
2584 struct dasd_device *device = dasd_device_from_cdev(cdev);
2586 if (IS_ERR(device))
2587 return PTR_ERR(device);
2588 /* disallow new I/O */
2589 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
2590 /* clear active requests */
2591 INIT_LIST_HEAD(&freeze_queue);
2592 spin_lock_irq(get_ccwdev_lock(cdev));
2593 rc = 0;
2594 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2595 /* Check status and move request to flush_queue */
2596 if (cqr->status == DASD_CQR_IN_IO) {
2597 rc = device->discipline->term_IO(cqr);
2598 if (rc) {
2599 /* unable to terminate requeust */
2600 dev_err(&device->cdev->dev,
2601 "Unable to terminate request %p "
2602 "on suspend\n", cqr);
2603 spin_unlock_irq(get_ccwdev_lock(cdev));
2604 dasd_put_device(device);
2605 return rc;
2608 list_move_tail(&cqr->devlist, &freeze_queue);
2611 spin_unlock_irq(get_ccwdev_lock(cdev));
2613 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2614 wait_event(dasd_flush_wq,
2615 (cqr->status != DASD_CQR_CLEAR_PENDING));
2616 if (cqr->status == DASD_CQR_CLEARED)
2617 cqr->status = DASD_CQR_QUEUED;
2619 /* move freeze_queue to start of the ccw_queue */
2620 spin_lock_irq(get_ccwdev_lock(cdev));
2621 list_splice_tail(&freeze_queue, &device->ccw_queue);
2622 spin_unlock_irq(get_ccwdev_lock(cdev));
2624 if (device->discipline->freeze)
2625 rc = device->discipline->freeze(device);
2627 dasd_put_device(device);
2628 return rc;
2630 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2632 int dasd_generic_restore_device(struct ccw_device *cdev)
2634 struct dasd_device *device = dasd_device_from_cdev(cdev);
2635 int rc = 0;
2637 if (IS_ERR(device))
2638 return PTR_ERR(device);
2640 /* allow new IO again */
2641 dasd_device_remove_stop_bits(device,
2642 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
2644 dasd_schedule_device_bh(device);
2647 * call discipline restore function
2648 * if device is stopped do nothing e.g. for disconnected devices
2650 if (device->discipline->restore && !(device->stopped))
2651 rc = device->discipline->restore(device);
2652 if (rc || device->stopped)
2654 * if the resume failed for the DASD we put it in
2655 * an UNRESUMED stop state
2657 device->stopped |= DASD_UNRESUMED_PM;
2659 if (device->block)
2660 dasd_schedule_block_bh(device->block);
2662 dasd_put_device(device);
2663 return 0;
2665 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2667 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2668 void *rdc_buffer,
2669 int rdc_buffer_size,
2670 int magic)
2672 struct dasd_ccw_req *cqr;
2673 struct ccw1 *ccw;
2674 unsigned long *idaw;
2676 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2678 if (IS_ERR(cqr)) {
2679 /* internal error 13 - Allocating the RDC request failed*/
2680 dev_err(&device->cdev->dev,
2681 "An error occurred in the DASD device driver, "
2682 "reason=%s\n", "13");
2683 return cqr;
2686 ccw = cqr->cpaddr;
2687 ccw->cmd_code = CCW_CMD_RDC;
2688 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2689 idaw = (unsigned long *) (cqr->data);
2690 ccw->cda = (__u32)(addr_t) idaw;
2691 ccw->flags = CCW_FLAG_IDA;
2692 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2693 } else {
2694 ccw->cda = (__u32)(addr_t) rdc_buffer;
2695 ccw->flags = 0;
2698 ccw->count = rdc_buffer_size;
2699 cqr->startdev = device;
2700 cqr->memdev = device;
2701 cqr->expires = 10*HZ;
2702 cqr->retries = 256;
2703 cqr->buildclk = get_clock();
2704 cqr->status = DASD_CQR_FILLED;
2705 return cqr;
2709 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
2710 void *rdc_buffer, int rdc_buffer_size)
2712 int ret;
2713 struct dasd_ccw_req *cqr;
2715 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
2716 magic);
2717 if (IS_ERR(cqr))
2718 return PTR_ERR(cqr);
2720 ret = dasd_sleep_on(cqr);
2721 dasd_sfree_request(cqr, cqr->memdev);
2722 return ret;
2724 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
2727 * In command mode and transport mode we need to look for sense
2728 * data in different places. The sense data itself is allways
2729 * an array of 32 bytes, so we can unify the sense data access
2730 * for both modes.
2732 char *dasd_get_sense(struct irb *irb)
2734 struct tsb *tsb = NULL;
2735 char *sense = NULL;
2737 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2738 if (irb->scsw.tm.tcw)
2739 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2740 irb->scsw.tm.tcw);
2741 if (tsb && tsb->length == 64 && tsb->flags)
2742 switch (tsb->flags & 0x07) {
2743 case 1: /* tsa_iostat */
2744 sense = tsb->tsa.iostat.sense;
2745 break;
2746 case 2: /* tsa_ddpc */
2747 sense = tsb->tsa.ddpc.sense;
2748 break;
2749 default:
2750 /* currently we don't use interrogate data */
2751 break;
2753 } else if (irb->esw.esw0.erw.cons) {
2754 sense = irb->ecw;
2756 return sense;
2758 EXPORT_SYMBOL_GPL(dasd_get_sense);
2760 static int __init dasd_init(void)
2762 int rc;
2764 init_waitqueue_head(&dasd_init_waitq);
2765 init_waitqueue_head(&dasd_flush_wq);
2766 init_waitqueue_head(&generic_waitq);
2768 /* register 'common' DASD debug area, used for all DBF_XXX calls */
2769 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
2770 if (dasd_debug_area == NULL) {
2771 rc = -ENOMEM;
2772 goto failed;
2774 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2775 debug_set_level(dasd_debug_area, DBF_WARNING);
2777 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2779 dasd_diag_discipline_pointer = NULL;
2781 rc = dasd_devmap_init();
2782 if (rc)
2783 goto failed;
2784 rc = dasd_gendisk_init();
2785 if (rc)
2786 goto failed;
2787 rc = dasd_parse();
2788 if (rc)
2789 goto failed;
2790 rc = dasd_eer_init();
2791 if (rc)
2792 goto failed;
2793 #ifdef CONFIG_PROC_FS
2794 rc = dasd_proc_init();
2795 if (rc)
2796 goto failed;
2797 #endif
2799 return 0;
2800 failed:
2801 pr_info("The DASD device driver could not be initialized\n");
2802 dasd_exit();
2803 return rc;
2806 module_init(dasd_init);
2807 module_exit(dasd_exit);
2809 EXPORT_SYMBOL(dasd_debug_area);
2810 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2812 EXPORT_SYMBOL(dasd_add_request_head);
2813 EXPORT_SYMBOL(dasd_add_request_tail);
2814 EXPORT_SYMBOL(dasd_cancel_req);
2815 EXPORT_SYMBOL(dasd_device_clear_timer);
2816 EXPORT_SYMBOL(dasd_block_clear_timer);
2817 EXPORT_SYMBOL(dasd_enable_device);
2818 EXPORT_SYMBOL(dasd_int_handler);
2819 EXPORT_SYMBOL(dasd_kfree_request);
2820 EXPORT_SYMBOL(dasd_kick_device);
2821 EXPORT_SYMBOL(dasd_kmalloc_request);
2822 EXPORT_SYMBOL(dasd_schedule_device_bh);
2823 EXPORT_SYMBOL(dasd_schedule_block_bh);
2824 EXPORT_SYMBOL(dasd_set_target_state);
2825 EXPORT_SYMBOL(dasd_device_set_timer);
2826 EXPORT_SYMBOL(dasd_block_set_timer);
2827 EXPORT_SYMBOL(dasd_sfree_request);
2828 EXPORT_SYMBOL(dasd_sleep_on);
2829 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2830 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2831 EXPORT_SYMBOL(dasd_smalloc_request);
2832 EXPORT_SYMBOL(dasd_start_IO);
2833 EXPORT_SYMBOL(dasd_term_IO);
2835 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2836 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2837 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2838 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2839 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2840 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2841 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2842 EXPORT_SYMBOL_GPL(dasd_alloc_block);
2843 EXPORT_SYMBOL_GPL(dasd_free_block);