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
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>
24 #include <asm/ccwdev.h>
25 #include <asm/ebcdic.h>
26 #include <asm/idals.h>
30 #define PRINTK_HEADER "dasd:"
34 * SECTION: Constant definitions to be used within this file
36 #define DASD_CHANQ_MAX_SIZE 4
39 * SECTION: exported variables of dasd.c
41 debug_info_t *dasd_debug_area;
42 struct dasd_discipline *dasd_diag_discipline_pointer;
43 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
45 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
46 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
47 " Copyright 2000 IBM Corporation");
48 MODULE_SUPPORTED_DEVICE("dasd");
49 MODULE_LICENSE("GPL");
52 * SECTION: prototypes for static functions of dasd.c
54 static int dasd_alloc_queue(struct dasd_block *);
55 static void dasd_setup_queue(struct dasd_block *);
56 static void dasd_free_queue(struct dasd_block *);
57 static void dasd_flush_request_queue(struct dasd_block *);
58 static int dasd_flush_block_queue(struct dasd_block *);
59 static void dasd_device_tasklet(struct dasd_device *);
60 static void dasd_block_tasklet(struct dasd_block *);
61 static void do_kick_device(struct work_struct *);
62 static void do_restore_device(struct work_struct *);
63 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
64 static void dasd_device_timeout(unsigned long);
65 static void dasd_block_timeout(unsigned long);
66 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
69 * SECTION: Operations on the device structure.
71 static wait_queue_head_t dasd_init_waitq;
72 static wait_queue_head_t dasd_flush_wq;
73 static wait_queue_head_t generic_waitq;
76 * Allocate memory for a new device structure.
78 struct dasd_device *dasd_alloc_device(void)
80 struct dasd_device *device;
82 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
84 return ERR_PTR(-ENOMEM);
86 /* Get two pages for normal block device operations. */
87 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
88 if (!device->ccw_mem) {
90 return ERR_PTR(-ENOMEM);
92 /* Get one page for error recovery. */
93 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
94 if (!device->erp_mem) {
95 free_pages((unsigned long) device->ccw_mem, 1);
97 return ERR_PTR(-ENOMEM);
100 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
101 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
102 spin_lock_init(&device->mem_lock);
103 atomic_set(&device->tasklet_scheduled, 0);
104 tasklet_init(&device->tasklet,
105 (void (*)(unsigned long)) dasd_device_tasklet,
106 (unsigned long) device);
107 INIT_LIST_HEAD(&device->ccw_queue);
108 init_timer(&device->timer);
109 device->timer.function = dasd_device_timeout;
110 device->timer.data = (unsigned long) device;
111 INIT_WORK(&device->kick_work, do_kick_device);
112 INIT_WORK(&device->restore_device, do_restore_device);
113 device->state = DASD_STATE_NEW;
114 device->target = DASD_STATE_NEW;
120 * Free memory of a device structure.
122 void dasd_free_device(struct dasd_device *device)
124 kfree(device->private);
125 free_page((unsigned long) device->erp_mem);
126 free_pages((unsigned long) device->ccw_mem, 1);
131 * Allocate memory for a new device structure.
133 struct dasd_block *dasd_alloc_block(void)
135 struct dasd_block *block;
137 block = kzalloc(sizeof(*block), GFP_ATOMIC);
139 return ERR_PTR(-ENOMEM);
140 /* open_count = 0 means device online but not in use */
141 atomic_set(&block->open_count, -1);
143 spin_lock_init(&block->request_queue_lock);
144 atomic_set(&block->tasklet_scheduled, 0);
145 tasklet_init(&block->tasklet,
146 (void (*)(unsigned long)) dasd_block_tasklet,
147 (unsigned long) block);
148 INIT_LIST_HEAD(&block->ccw_queue);
149 spin_lock_init(&block->queue_lock);
150 init_timer(&block->timer);
151 block->timer.function = dasd_block_timeout;
152 block->timer.data = (unsigned long) block;
158 * Free memory of a device structure.
160 void dasd_free_block(struct dasd_block *block)
166 * Make a new device known to the system.
168 static int dasd_state_new_to_known(struct dasd_device *device)
173 * As long as the device is not in state DASD_STATE_NEW we want to
174 * keep the reference count > 0.
176 dasd_get_device(device);
179 rc = dasd_alloc_queue(device->block);
181 dasd_put_device(device);
185 device->state = DASD_STATE_KNOWN;
190 * Let the system forget about a device.
192 static int dasd_state_known_to_new(struct dasd_device *device)
194 /* Disable extended error reporting for this device. */
195 dasd_eer_disable(device);
196 /* Forget the discipline information. */
197 if (device->discipline) {
198 if (device->discipline->uncheck_device)
199 device->discipline->uncheck_device(device);
200 module_put(device->discipline->owner);
202 device->discipline = NULL;
203 if (device->base_discipline)
204 module_put(device->base_discipline->owner);
205 device->base_discipline = NULL;
206 device->state = DASD_STATE_NEW;
209 dasd_free_queue(device->block);
211 /* Give up reference we took in dasd_state_new_to_known. */
212 dasd_put_device(device);
217 * Request the irq line for the device.
219 static int dasd_state_known_to_basic(struct dasd_device *device)
223 /* Allocate and register gendisk structure. */
225 rc = dasd_gendisk_alloc(device->block);
229 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
230 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
232 debug_register_view(device->debug_area, &debug_sprintf_view);
233 debug_set_level(device->debug_area, DBF_WARNING);
234 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
236 device->state = DASD_STATE_BASIC;
241 * Release the irq line for the device. Terminate any running i/o.
243 static int dasd_state_basic_to_known(struct dasd_device *device)
247 dasd_gendisk_free(device->block);
248 dasd_block_clear_timer(device->block);
250 rc = dasd_flush_device_queue(device);
253 dasd_device_clear_timer(device);
255 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
256 if (device->debug_area != NULL) {
257 debug_unregister(device->debug_area);
258 device->debug_area = NULL;
260 device->state = DASD_STATE_KNOWN;
265 * Do the initial analysis. The do_analysis function may return
266 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
267 * until the discipline decides to continue the startup sequence
268 * by calling the function dasd_change_state. The eckd disciplines
269 * uses this to start a ccw that detects the format. The completion
270 * interrupt for this detection ccw uses the kernel event daemon to
271 * trigger the call to dasd_change_state. All this is done in the
272 * discipline code, see dasd_eckd.c.
273 * After the analysis ccw is done (do_analysis returned 0) the block
275 * In case the analysis returns an error, the device setup is stopped
276 * (a fake disk was already added to allow formatting).
278 static int dasd_state_basic_to_ready(struct dasd_device *device)
281 struct dasd_block *block;
284 block = device->block;
285 /* make disk known with correct capacity */
287 if (block->base->discipline->do_analysis != NULL)
288 rc = block->base->discipline->do_analysis(block);
291 device->state = DASD_STATE_UNFMT;
294 dasd_setup_queue(block);
295 set_capacity(block->gdp,
296 block->blocks << block->s2b_shift);
297 device->state = DASD_STATE_READY;
298 rc = dasd_scan_partitions(block);
300 device->state = DASD_STATE_BASIC;
302 device->state = DASD_STATE_READY;
308 * Remove device from block device layer. Destroy dirty buffers.
309 * Forget format information. Check if the target level is basic
310 * and if it is create fake disk for formatting.
312 static int dasd_state_ready_to_basic(struct dasd_device *device)
316 device->state = DASD_STATE_BASIC;
318 struct dasd_block *block = device->block;
319 rc = dasd_flush_block_queue(block);
321 device->state = DASD_STATE_READY;
324 dasd_destroy_partitions(block);
325 dasd_flush_request_queue(block);
328 block->s2b_shift = 0;
336 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
338 device->state = DASD_STATE_BASIC;
343 * Make the device online and schedule the bottom half to start
344 * the requeueing of requests from the linux request queue to the
348 dasd_state_ready_to_online(struct dasd_device * device)
351 struct gendisk *disk;
352 struct disk_part_iter piter;
353 struct hd_struct *part;
355 if (device->discipline->ready_to_online) {
356 rc = device->discipline->ready_to_online(device);
360 device->state = DASD_STATE_ONLINE;
362 dasd_schedule_block_bh(device->block);
363 disk = device->block->bdev->bd_disk;
364 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
365 while ((part = disk_part_iter_next(&piter)))
366 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
367 disk_part_iter_exit(&piter);
373 * Stop the requeueing of requests again.
375 static int dasd_state_online_to_ready(struct dasd_device *device)
378 struct gendisk *disk;
379 struct disk_part_iter piter;
380 struct hd_struct *part;
382 if (device->discipline->online_to_ready) {
383 rc = device->discipline->online_to_ready(device);
387 device->state = DASD_STATE_READY;
389 disk = device->block->bdev->bd_disk;
390 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
391 while ((part = disk_part_iter_next(&piter)))
392 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
393 disk_part_iter_exit(&piter);
399 * Device startup state changes.
401 static int dasd_increase_state(struct dasd_device *device)
406 if (device->state == DASD_STATE_NEW &&
407 device->target >= DASD_STATE_KNOWN)
408 rc = dasd_state_new_to_known(device);
411 device->state == DASD_STATE_KNOWN &&
412 device->target >= DASD_STATE_BASIC)
413 rc = dasd_state_known_to_basic(device);
416 device->state == DASD_STATE_BASIC &&
417 device->target >= DASD_STATE_READY)
418 rc = dasd_state_basic_to_ready(device);
421 device->state == DASD_STATE_UNFMT &&
422 device->target > DASD_STATE_UNFMT)
426 device->state == DASD_STATE_READY &&
427 device->target >= DASD_STATE_ONLINE)
428 rc = dasd_state_ready_to_online(device);
434 * Device shutdown state changes.
436 static int dasd_decrease_state(struct dasd_device *device)
441 if (device->state == DASD_STATE_ONLINE &&
442 device->target <= DASD_STATE_READY)
443 rc = dasd_state_online_to_ready(device);
446 device->state == DASD_STATE_READY &&
447 device->target <= DASD_STATE_BASIC)
448 rc = dasd_state_ready_to_basic(device);
451 device->state == DASD_STATE_UNFMT &&
452 device->target <= DASD_STATE_BASIC)
453 rc = dasd_state_unfmt_to_basic(device);
456 device->state == DASD_STATE_BASIC &&
457 device->target <= DASD_STATE_KNOWN)
458 rc = dasd_state_basic_to_known(device);
461 device->state == DASD_STATE_KNOWN &&
462 device->target <= DASD_STATE_NEW)
463 rc = dasd_state_known_to_new(device);
469 * This is the main startup/shutdown routine.
471 static void dasd_change_state(struct dasd_device *device)
475 if (device->state == device->target)
476 /* Already where we want to go today... */
478 if (device->state < device->target)
479 rc = dasd_increase_state(device);
481 rc = dasd_decrease_state(device);
485 device->target = device->state;
487 if (device->state == device->target) {
488 wake_up(&dasd_init_waitq);
489 dasd_put_device(device);
492 /* let user-space know that the device status changed */
493 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
497 * Kick starter for devices that did not complete the startup/shutdown
498 * procedure or were sleeping because of a pending state.
499 * dasd_kick_device will schedule a call do do_kick_device to the kernel
502 static void do_kick_device(struct work_struct *work)
504 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
505 dasd_change_state(device);
506 dasd_schedule_device_bh(device);
507 dasd_put_device(device);
510 void dasd_kick_device(struct dasd_device *device)
512 dasd_get_device(device);
513 /* queue call to dasd_kick_device to the kernel event daemon. */
514 schedule_work(&device->kick_work);
518 * dasd_restore_device will schedule a call do do_restore_device to the kernel
521 static void do_restore_device(struct work_struct *work)
523 struct dasd_device *device = container_of(work, struct dasd_device,
525 device->cdev->drv->restore(device->cdev);
526 dasd_put_device(device);
529 void dasd_restore_device(struct dasd_device *device)
531 dasd_get_device(device);
532 /* queue call to dasd_restore_device to the kernel event daemon. */
533 schedule_work(&device->restore_device);
537 * Set the target state for a device and starts the state change.
539 void dasd_set_target_state(struct dasd_device *device, int target)
541 dasd_get_device(device);
542 /* If we are in probeonly mode stop at DASD_STATE_READY. */
543 if (dasd_probeonly && target > DASD_STATE_READY)
544 target = DASD_STATE_READY;
545 if (device->target != target) {
546 if (device->state == target) {
547 wake_up(&dasd_init_waitq);
548 dasd_put_device(device);
550 device->target = target;
552 if (device->state != device->target)
553 dasd_change_state(device);
557 * Enable devices with device numbers in [from..to].
559 static inline int _wait_for_device(struct dasd_device *device)
561 return (device->state == device->target);
564 void dasd_enable_device(struct dasd_device *device)
566 dasd_set_target_state(device, DASD_STATE_ONLINE);
567 if (device->state <= DASD_STATE_KNOWN)
568 /* No discipline for device found. */
569 dasd_set_target_state(device, DASD_STATE_NEW);
570 /* Now wait for the devices to come up. */
571 wait_event(dasd_init_waitq, _wait_for_device(device));
575 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
577 #ifdef CONFIG_DASD_PROFILE
579 struct dasd_profile_info_t dasd_global_profile;
580 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
583 * Increments counter in global and local profiling structures.
585 #define dasd_profile_counter(value, counter, block) \
588 for (index = 0; index < 31 && value >> (2+index); index++); \
589 dasd_global_profile.counter[index]++; \
590 block->profile.counter[index]++; \
594 * Add profiling information for cqr before execution.
596 static void dasd_profile_start(struct dasd_block *block,
597 struct dasd_ccw_req *cqr,
601 unsigned int counter;
603 if (dasd_profile_level != DASD_PROFILE_ON)
606 /* count the length of the chanq for statistics */
608 list_for_each(l, &block->ccw_queue)
611 dasd_global_profile.dasd_io_nr_req[counter]++;
612 block->profile.dasd_io_nr_req[counter]++;
616 * Add profiling information for cqr after execution.
618 static void dasd_profile_end(struct dasd_block *block,
619 struct dasd_ccw_req *cqr,
622 long strtime, irqtime, endtime, tottime; /* in microseconds */
623 long tottimeps, sectors;
625 if (dasd_profile_level != DASD_PROFILE_ON)
628 sectors = blk_rq_sectors(req);
629 if (!cqr->buildclk || !cqr->startclk ||
630 !cqr->stopclk || !cqr->endclk ||
634 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
635 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
636 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
637 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
638 tottimeps = tottime / sectors;
640 if (!dasd_global_profile.dasd_io_reqs)
641 memset(&dasd_global_profile, 0,
642 sizeof(struct dasd_profile_info_t));
643 dasd_global_profile.dasd_io_reqs++;
644 dasd_global_profile.dasd_io_sects += sectors;
646 if (!block->profile.dasd_io_reqs)
647 memset(&block->profile, 0,
648 sizeof(struct dasd_profile_info_t));
649 block->profile.dasd_io_reqs++;
650 block->profile.dasd_io_sects += sectors;
652 dasd_profile_counter(sectors, dasd_io_secs, block);
653 dasd_profile_counter(tottime, dasd_io_times, block);
654 dasd_profile_counter(tottimeps, dasd_io_timps, block);
655 dasd_profile_counter(strtime, dasd_io_time1, block);
656 dasd_profile_counter(irqtime, dasd_io_time2, block);
657 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
658 dasd_profile_counter(endtime, dasd_io_time3, block);
661 #define dasd_profile_start(block, cqr, req) do {} while (0)
662 #define dasd_profile_end(block, cqr, req) do {} while (0)
663 #endif /* CONFIG_DASD_PROFILE */
666 * Allocate memory for a channel program with 'cplength' channel
667 * command words and 'datasize' additional space. There are two
668 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
669 * memory and 2) dasd_smalloc_request uses the static ccw memory
670 * that gets allocated for each device.
672 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
674 struct dasd_device *device)
676 struct dasd_ccw_req *cqr;
679 BUG_ON(datasize > PAGE_SIZE ||
680 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
682 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
684 return ERR_PTR(-ENOMEM);
687 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
688 GFP_ATOMIC | GFP_DMA);
689 if (cqr->cpaddr == NULL) {
691 return ERR_PTR(-ENOMEM);
696 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
697 if (cqr->data == NULL) {
700 return ERR_PTR(-ENOMEM);
704 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
705 dasd_get_device(device);
709 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
711 struct dasd_device *device)
714 struct dasd_ccw_req *cqr;
719 BUG_ON(datasize > PAGE_SIZE ||
720 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
722 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
724 size += cplength * sizeof(struct ccw1);
727 spin_lock_irqsave(&device->mem_lock, flags);
728 cqr = (struct dasd_ccw_req *)
729 dasd_alloc_chunk(&device->ccw_chunks, size);
730 spin_unlock_irqrestore(&device->mem_lock, flags);
732 return ERR_PTR(-ENOMEM);
733 memset(cqr, 0, sizeof(struct dasd_ccw_req));
734 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
737 cqr->cpaddr = (struct ccw1 *) data;
738 data += cplength*sizeof(struct ccw1);
739 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
744 memset(cqr->data, 0, datasize);
747 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
748 dasd_get_device(device);
753 * Free memory of a channel program. This function needs to free all the
754 * idal lists that might have been created by dasd_set_cda and the
755 * struct dasd_ccw_req itself.
757 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
762 /* Clear any idals used for the request. */
765 clear_normalized_cda(ccw);
766 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
771 dasd_put_device(device);
774 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
778 spin_lock_irqsave(&device->mem_lock, flags);
779 dasd_free_chunk(&device->ccw_chunks, cqr);
780 spin_unlock_irqrestore(&device->mem_lock, flags);
781 dasd_put_device(device);
785 * Check discipline magic in cqr.
787 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
789 struct dasd_device *device;
793 device = cqr->startdev;
794 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
795 DBF_DEV_EVENT(DBF_WARNING, device,
796 " dasd_ccw_req 0x%08x magic doesn't match"
797 " discipline 0x%08x",
799 *(unsigned int *) device->discipline->name);
806 * Terminate the current i/o and set the request to clear_pending.
807 * Timer keeps device runnig.
808 * ccw_device_clear can fail if the i/o subsystem
811 int dasd_term_IO(struct dasd_ccw_req *cqr)
813 struct dasd_device *device;
815 char errorstring[ERRORLENGTH];
818 rc = dasd_check_cqr(cqr);
822 device = (struct dasd_device *) cqr->startdev;
823 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
824 rc = ccw_device_clear(device->cdev, (long) cqr);
826 case 0: /* termination successful */
828 cqr->status = DASD_CQR_CLEAR_PENDING;
829 cqr->stopclk = get_clock();
831 DBF_DEV_EVENT(DBF_DEBUG, device,
832 "terminate cqr %p successful",
836 DBF_DEV_EVENT(DBF_ERR, device, "%s",
837 "device gone, retry");
840 DBF_DEV_EVENT(DBF_ERR, device, "%s",
845 DBF_DEV_EVENT(DBF_ERR, device, "%s",
846 "device busy, retry later");
849 /* internal error 10 - unknown rc*/
850 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
851 dev_err(&device->cdev->dev, "An error occurred in the "
852 "DASD device driver, reason=%s\n", errorstring);
858 dasd_schedule_device_bh(device);
863 * Start the i/o. This start_IO can fail if the channel is really busy.
864 * In that case set up a timer to start the request later.
866 int dasd_start_IO(struct dasd_ccw_req *cqr)
868 struct dasd_device *device;
870 char errorstring[ERRORLENGTH];
873 rc = dasd_check_cqr(cqr);
878 device = (struct dasd_device *) cqr->startdev;
879 if (cqr->retries < 0) {
880 /* internal error 14 - start_IO run out of retries */
881 sprintf(errorstring, "14 %p", cqr);
882 dev_err(&device->cdev->dev, "An error occurred in the DASD "
883 "device driver, reason=%s\n", errorstring);
884 cqr->status = DASD_CQR_ERROR;
887 cqr->startclk = get_clock();
888 cqr->starttime = jiffies;
890 if (cqr->cpmode == 1) {
891 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
892 (long) cqr, cqr->lpm);
894 rc = ccw_device_start(device->cdev, cqr->cpaddr,
895 (long) cqr, cqr->lpm, 0);
899 cqr->status = DASD_CQR_IN_IO;
902 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
903 "start_IO: device busy, retry later");
906 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
907 "start_IO: request timeout, retry later");
910 /* -EACCES indicates that the request used only a
911 * subset of the available pathes and all these
913 * Do a retry with all available pathes.
915 cqr->lpm = LPM_ANYPATH;
916 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
917 "start_IO: selected pathes gone,"
918 " retry on all pathes");
921 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
922 "start_IO: -ENODEV device gone, retry");
925 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
926 "start_IO: -EIO device gone, retry");
929 /* most likely caused in power management context */
930 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
931 "start_IO: -EINVAL device currently "
935 /* internal error 11 - unknown rc */
936 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
937 dev_err(&device->cdev->dev,
938 "An error occurred in the DASD device driver, "
939 "reason=%s\n", errorstring);
948 * Timeout function for dasd devices. This is used for different purposes
949 * 1) missing interrupt handler for normal operation
950 * 2) delayed start of request where start_IO failed with -EBUSY
951 * 3) timeout for missing state change interrupts
952 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
953 * DASD_CQR_QUEUED for 2) and 3).
955 static void dasd_device_timeout(unsigned long ptr)
958 struct dasd_device *device;
960 device = (struct dasd_device *) ptr;
961 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
962 /* re-activate request queue */
963 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
964 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
965 dasd_schedule_device_bh(device);
969 * Setup timeout for a device in jiffies.
971 void dasd_device_set_timer(struct dasd_device *device, int expires)
974 del_timer(&device->timer);
976 mod_timer(&device->timer, jiffies + expires);
980 * Clear timeout for a device.
982 void dasd_device_clear_timer(struct dasd_device *device)
984 del_timer(&device->timer);
987 static void dasd_handle_killed_request(struct ccw_device *cdev,
988 unsigned long intparm)
990 struct dasd_ccw_req *cqr;
991 struct dasd_device *device;
995 cqr = (struct dasd_ccw_req *) intparm;
996 if (cqr->status != DASD_CQR_IN_IO) {
997 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
998 "invalid status in handle_killed_request: "
999 "%02x", cqr->status);
1003 device = (struct dasd_device *) cqr->startdev;
1004 if (device == NULL ||
1005 device != dasd_device_from_cdev_locked(cdev) ||
1006 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1007 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1008 "invalid device in request");
1012 /* Schedule request to be retried. */
1013 cqr->status = DASD_CQR_QUEUED;
1015 dasd_device_clear_timer(device);
1016 dasd_schedule_device_bh(device);
1017 dasd_put_device(device);
1020 void dasd_generic_handle_state_change(struct dasd_device *device)
1022 /* First of all start sense subsystem status request. */
1023 dasd_eer_snss(device);
1025 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1026 dasd_schedule_device_bh(device);
1028 dasd_schedule_block_bh(device->block);
1032 * Interrupt handler for "normal" ssch-io based dasd devices.
1034 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1037 struct dasd_ccw_req *cqr, *next;
1038 struct dasd_device *device;
1039 unsigned long long now;
1043 switch (PTR_ERR(irb)) {
1047 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1048 "request timed out\n", __func__);
1051 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1052 "unknown error %ld\n", __func__,
1055 dasd_handle_killed_request(cdev, intparm);
1061 /* check for unsolicited interrupts */
1062 cqr = (struct dasd_ccw_req *) intparm;
1063 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1064 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1065 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
1066 if (cqr && cqr->status == DASD_CQR_IN_IO)
1067 cqr->status = DASD_CQR_QUEUED;
1068 device = dasd_device_from_cdev_locked(cdev);
1069 if (!IS_ERR(device)) {
1070 dasd_device_clear_timer(device);
1071 device->discipline->handle_unsolicited_interrupt(device,
1073 dasd_put_device(device);
1078 device = (struct dasd_device *) cqr->startdev;
1080 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1081 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1082 "invalid device in request");
1086 /* Check for clear pending */
1087 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1088 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1089 cqr->status = DASD_CQR_CLEARED;
1090 dasd_device_clear_timer(device);
1091 wake_up(&dasd_flush_wq);
1092 dasd_schedule_device_bh(device);
1096 /* check status - the request might have been killed by dyn detach */
1097 if (cqr->status != DASD_CQR_IN_IO) {
1098 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1099 "status %02x", dev_name(&cdev->dev), cqr->status);
1105 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1106 scsw_cstat(&irb->scsw) == 0) {
1107 /* request was completed successfully */
1108 cqr->status = DASD_CQR_SUCCESS;
1110 /* Start first request on queue if possible -> fast_io. */
1111 if (cqr->devlist.next != &device->ccw_queue) {
1112 next = list_entry(cqr->devlist.next,
1113 struct dasd_ccw_req, devlist);
1115 } else { /* error */
1116 memcpy(&cqr->irb, irb, sizeof(struct irb));
1117 /* log sense for every failed I/O to s390 debugfeature */
1118 dasd_log_sense_dbf(cqr, irb);
1119 if (device->features & DASD_FEATURE_ERPLOG) {
1120 dasd_log_sense(cqr, irb);
1124 * If we don't want complex ERP for this request, then just
1125 * reset this and retry it in the fastpath
1127 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1129 if (cqr->lpm == LPM_ANYPATH)
1130 DBF_DEV_EVENT(DBF_DEBUG, device,
1131 "default ERP in fastpath "
1132 "(%i retries left)",
1134 cqr->lpm = LPM_ANYPATH;
1135 cqr->status = DASD_CQR_QUEUED;
1138 cqr->status = DASD_CQR_ERROR;
1140 if (next && (next->status == DASD_CQR_QUEUED) &&
1141 (!device->stopped)) {
1142 if (device->discipline->start_IO(next) == 0)
1143 expires = next->expires;
1146 dasd_device_set_timer(device, expires);
1148 dasd_device_clear_timer(device);
1149 dasd_schedule_device_bh(device);
1153 * If we have an error on a dasd_block layer request then we cancel
1154 * and return all further requests from the same dasd_block as well.
1156 static void __dasd_device_recovery(struct dasd_device *device,
1157 struct dasd_ccw_req *ref_cqr)
1159 struct list_head *l, *n;
1160 struct dasd_ccw_req *cqr;
1163 * only requeue request that came from the dasd_block layer
1165 if (!ref_cqr->block)
1168 list_for_each_safe(l, n, &device->ccw_queue) {
1169 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1170 if (cqr->status == DASD_CQR_QUEUED &&
1171 ref_cqr->block == cqr->block) {
1172 cqr->status = DASD_CQR_CLEARED;
1178 * Remove those ccw requests from the queue that need to be returned
1179 * to the upper layer.
1181 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1182 struct list_head *final_queue)
1184 struct list_head *l, *n;
1185 struct dasd_ccw_req *cqr;
1187 /* Process request with final status. */
1188 list_for_each_safe(l, n, &device->ccw_queue) {
1189 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1191 /* Stop list processing at the first non-final request. */
1192 if (cqr->status == DASD_CQR_QUEUED ||
1193 cqr->status == DASD_CQR_IN_IO ||
1194 cqr->status == DASD_CQR_CLEAR_PENDING)
1196 if (cqr->status == DASD_CQR_ERROR) {
1197 __dasd_device_recovery(device, cqr);
1199 /* Rechain finished requests to final queue */
1200 list_move_tail(&cqr->devlist, final_queue);
1205 * the cqrs from the final queue are returned to the upper layer
1206 * by setting a dasd_block state and calling the callback function
1208 static void __dasd_device_process_final_queue(struct dasd_device *device,
1209 struct list_head *final_queue)
1211 struct list_head *l, *n;
1212 struct dasd_ccw_req *cqr;
1213 struct dasd_block *block;
1214 void (*callback)(struct dasd_ccw_req *, void *data);
1215 void *callback_data;
1216 char errorstring[ERRORLENGTH];
1218 list_for_each_safe(l, n, final_queue) {
1219 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1220 list_del_init(&cqr->devlist);
1222 callback = cqr->callback;
1223 callback_data = cqr->callback_data;
1225 spin_lock_bh(&block->queue_lock);
1226 switch (cqr->status) {
1227 case DASD_CQR_SUCCESS:
1228 cqr->status = DASD_CQR_DONE;
1230 case DASD_CQR_ERROR:
1231 cqr->status = DASD_CQR_NEED_ERP;
1233 case DASD_CQR_CLEARED:
1234 cqr->status = DASD_CQR_TERMINATED;
1237 /* internal error 12 - wrong cqr status*/
1238 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1239 dev_err(&device->cdev->dev,
1240 "An error occurred in the DASD device driver, "
1241 "reason=%s\n", errorstring);
1244 if (cqr->callback != NULL)
1245 (callback)(cqr, callback_data);
1247 spin_unlock_bh(&block->queue_lock);
1252 * Take a look at the first request on the ccw queue and check
1253 * if it reached its expire time. If so, terminate the IO.
1255 static void __dasd_device_check_expire(struct dasd_device *device)
1257 struct dasd_ccw_req *cqr;
1259 if (list_empty(&device->ccw_queue))
1261 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1262 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1263 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1264 if (device->discipline->term_IO(cqr) != 0) {
1265 /* Hmpf, try again in 5 sec */
1266 dev_err(&device->cdev->dev,
1267 "cqr %p timed out (%is) but cannot be "
1268 "ended, retrying in 5 s\n",
1269 cqr, (cqr->expires/HZ));
1270 cqr->expires += 5*HZ;
1271 dasd_device_set_timer(device, 5*HZ);
1273 dev_err(&device->cdev->dev,
1274 "cqr %p timed out (%is), %i retries "
1275 "remaining\n", cqr, (cqr->expires/HZ),
1282 * Take a look at the first request on the ccw queue and check
1283 * if it needs to be started.
1285 static void __dasd_device_start_head(struct dasd_device *device)
1287 struct dasd_ccw_req *cqr;
1290 if (list_empty(&device->ccw_queue))
1292 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1293 if (cqr->status != DASD_CQR_QUEUED)
1295 /* when device is stopped, return request to previous layer */
1296 if (device->stopped) {
1297 cqr->status = DASD_CQR_CLEARED;
1298 dasd_schedule_device_bh(device);
1302 rc = device->discipline->start_IO(cqr);
1304 dasd_device_set_timer(device, cqr->expires);
1305 else if (rc == -EACCES) {
1306 dasd_schedule_device_bh(device);
1308 /* Hmpf, try again in 1/2 sec */
1309 dasd_device_set_timer(device, 50);
1313 * Go through all request on the dasd_device request queue,
1314 * terminate them on the cdev if necessary, and return them to the
1315 * submitting layer via callback.
1317 * Make sure that all 'submitting layers' still exist when
1318 * this function is called!. In other words, when 'device' is a base
1319 * device then all block layer requests must have been removed before
1320 * via dasd_flush_block_queue.
1322 int dasd_flush_device_queue(struct dasd_device *device)
1324 struct dasd_ccw_req *cqr, *n;
1326 struct list_head flush_queue;
1328 INIT_LIST_HEAD(&flush_queue);
1329 spin_lock_irq(get_ccwdev_lock(device->cdev));
1331 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1332 /* Check status and move request to flush_queue */
1333 switch (cqr->status) {
1334 case DASD_CQR_IN_IO:
1335 rc = device->discipline->term_IO(cqr);
1337 /* unable to terminate requeust */
1338 dev_err(&device->cdev->dev,
1339 "Flushing the DASD request queue "
1340 "failed for request %p\n", cqr);
1341 /* stop flush processing */
1345 case DASD_CQR_QUEUED:
1346 cqr->stopclk = get_clock();
1347 cqr->status = DASD_CQR_CLEARED;
1349 default: /* no need to modify the others */
1352 list_move_tail(&cqr->devlist, &flush_queue);
1355 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1357 * After this point all requests must be in state CLEAR_PENDING,
1358 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1359 * one of the others.
1361 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1362 wait_event(dasd_flush_wq,
1363 (cqr->status != DASD_CQR_CLEAR_PENDING));
1365 * Now set each request back to TERMINATED, DONE or NEED_ERP
1366 * and call the callback function of flushed requests
1368 __dasd_device_process_final_queue(device, &flush_queue);
1373 * Acquire the device lock and process queues for the device.
1375 static void dasd_device_tasklet(struct dasd_device *device)
1377 struct list_head final_queue;
1379 atomic_set (&device->tasklet_scheduled, 0);
1380 INIT_LIST_HEAD(&final_queue);
1381 spin_lock_irq(get_ccwdev_lock(device->cdev));
1382 /* Check expire time of first request on the ccw queue. */
1383 __dasd_device_check_expire(device);
1384 /* find final requests on ccw queue */
1385 __dasd_device_process_ccw_queue(device, &final_queue);
1386 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1387 /* Now call the callback function of requests with final status */
1388 __dasd_device_process_final_queue(device, &final_queue);
1389 spin_lock_irq(get_ccwdev_lock(device->cdev));
1390 /* Now check if the head of the ccw queue needs to be started. */
1391 __dasd_device_start_head(device);
1392 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1393 dasd_put_device(device);
1397 * Schedules a call to dasd_tasklet over the device tasklet.
1399 void dasd_schedule_device_bh(struct dasd_device *device)
1401 /* Protect against rescheduling. */
1402 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1404 dasd_get_device(device);
1405 tasklet_hi_schedule(&device->tasklet);
1408 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1410 device->stopped |= bits;
1412 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1414 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1416 device->stopped &= ~bits;
1417 if (!device->stopped)
1418 wake_up(&generic_waitq);
1420 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1423 * Queue a request to the head of the device ccw_queue.
1424 * Start the I/O if possible.
1426 void dasd_add_request_head(struct dasd_ccw_req *cqr)
1428 struct dasd_device *device;
1429 unsigned long flags;
1431 device = cqr->startdev;
1432 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1433 cqr->status = DASD_CQR_QUEUED;
1434 list_add(&cqr->devlist, &device->ccw_queue);
1435 /* let the bh start the request to keep them in order */
1436 dasd_schedule_device_bh(device);
1437 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1441 * Queue a request to the tail of the device ccw_queue.
1442 * Start the I/O if possible.
1444 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1446 struct dasd_device *device;
1447 unsigned long flags;
1449 device = cqr->startdev;
1450 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1451 cqr->status = DASD_CQR_QUEUED;
1452 list_add_tail(&cqr->devlist, &device->ccw_queue);
1453 /* let the bh start the request to keep them in order */
1454 dasd_schedule_device_bh(device);
1455 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1459 * Wakeup helper for the 'sleep_on' functions.
1461 static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1463 wake_up((wait_queue_head_t *) data);
1466 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1468 struct dasd_device *device;
1471 device = cqr->startdev;
1472 spin_lock_irq(get_ccwdev_lock(device->cdev));
1473 rc = ((cqr->status == DASD_CQR_DONE ||
1474 cqr->status == DASD_CQR_NEED_ERP ||
1475 cqr->status == DASD_CQR_TERMINATED) &&
1476 list_empty(&cqr->devlist));
1477 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1482 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1484 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1486 struct dasd_device *device;
1487 dasd_erp_fn_t erp_fn;
1489 if (cqr->status == DASD_CQR_FILLED)
1491 device = cqr->startdev;
1492 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1493 if (cqr->status == DASD_CQR_TERMINATED) {
1494 device->discipline->handle_terminated_request(cqr);
1497 if (cqr->status == DASD_CQR_NEED_ERP) {
1498 erp_fn = device->discipline->erp_action(cqr);
1502 if (cqr->status == DASD_CQR_FAILED)
1503 dasd_log_sense(cqr, &cqr->irb);
1505 __dasd_process_erp(device, cqr);
1512 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1514 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1515 if (cqr->refers) /* erp is not done yet */
1517 return ((cqr->status != DASD_CQR_DONE) &&
1518 (cqr->status != DASD_CQR_FAILED));
1520 return (cqr->status == DASD_CQR_FILLED);
1523 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1525 struct dasd_device *device;
1527 struct list_head ccw_queue;
1528 struct dasd_ccw_req *cqr;
1530 INIT_LIST_HEAD(&ccw_queue);
1531 maincqr->status = DASD_CQR_FILLED;
1532 device = maincqr->startdev;
1533 list_add(&maincqr->blocklist, &ccw_queue);
1534 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1535 cqr = list_first_entry(&ccw_queue,
1536 struct dasd_ccw_req, blocklist)) {
1538 if (__dasd_sleep_on_erp(cqr))
1540 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1543 /* Non-temporary stop condition will trigger fail fast */
1544 if (device->stopped & ~DASD_STOPPED_PENDING &&
1545 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1546 (!dasd_eer_enabled(device))) {
1547 cqr->status = DASD_CQR_FAILED;
1551 /* Don't try to start requests if device is stopped */
1552 if (interruptible) {
1553 rc = wait_event_interruptible(
1554 generic_waitq, !(device->stopped));
1555 if (rc == -ERESTARTSYS) {
1556 cqr->status = DASD_CQR_FAILED;
1557 maincqr->intrc = rc;
1561 wait_event(generic_waitq, !(device->stopped));
1563 cqr->callback = dasd_wakeup_cb;
1564 cqr->callback_data = (void *) &generic_waitq;
1565 dasd_add_request_tail(cqr);
1566 if (interruptible) {
1567 rc = wait_event_interruptible(
1568 generic_waitq, _wait_for_wakeup(cqr));
1569 if (rc == -ERESTARTSYS) {
1570 dasd_cancel_req(cqr);
1571 /* wait (non-interruptible) for final status */
1572 wait_event(generic_waitq,
1573 _wait_for_wakeup(cqr));
1574 cqr->status = DASD_CQR_FAILED;
1575 maincqr->intrc = rc;
1579 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1582 maincqr->endclk = get_clock();
1583 if ((maincqr->status != DASD_CQR_DONE) &&
1584 (maincqr->intrc != -ERESTARTSYS))
1585 dasd_log_sense(maincqr, &maincqr->irb);
1586 if (maincqr->status == DASD_CQR_DONE)
1588 else if (maincqr->intrc)
1589 rc = maincqr->intrc;
1596 * Queue a request to the tail of the device ccw_queue and wait for
1599 int dasd_sleep_on(struct dasd_ccw_req *cqr)
1601 return _dasd_sleep_on(cqr, 0);
1605 * Queue a request to the tail of the device ccw_queue and wait
1606 * interruptible for it's completion.
1608 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1610 return _dasd_sleep_on(cqr, 1);
1614 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1615 * for eckd devices) the currently running request has to be terminated
1616 * and be put back to status queued, before the special request is added
1617 * to the head of the queue. Then the special request is waited on normally.
1619 static inline int _dasd_term_running_cqr(struct dasd_device *device)
1621 struct dasd_ccw_req *cqr;
1623 if (list_empty(&device->ccw_queue))
1625 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1626 return device->discipline->term_IO(cqr);
1629 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1631 struct dasd_device *device;
1634 device = cqr->startdev;
1635 spin_lock_irq(get_ccwdev_lock(device->cdev));
1636 rc = _dasd_term_running_cqr(device);
1638 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1642 cqr->callback = dasd_wakeup_cb;
1643 cqr->callback_data = (void *) &generic_waitq;
1644 cqr->status = DASD_CQR_QUEUED;
1645 list_add(&cqr->devlist, &device->ccw_queue);
1647 /* let the bh start the request to keep them in order */
1648 dasd_schedule_device_bh(device);
1650 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1652 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1654 if (cqr->status == DASD_CQR_DONE)
1656 else if (cqr->intrc)
1664 * Cancels a request that was started with dasd_sleep_on_req.
1665 * This is useful to timeout requests. The request will be
1666 * terminated if it is currently in i/o.
1667 * Returns 1 if the request has been terminated.
1668 * 0 if there was no need to terminate the request (not started yet)
1669 * negative error code if termination failed
1670 * Cancellation of a request is an asynchronous operation! The calling
1671 * function has to wait until the request is properly returned via callback.
1673 int dasd_cancel_req(struct dasd_ccw_req *cqr)
1675 struct dasd_device *device = cqr->startdev;
1676 unsigned long flags;
1680 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1681 switch (cqr->status) {
1682 case DASD_CQR_QUEUED:
1683 /* request was not started - just set to cleared */
1684 cqr->status = DASD_CQR_CLEARED;
1686 case DASD_CQR_IN_IO:
1687 /* request in IO - terminate IO and release again */
1688 rc = device->discipline->term_IO(cqr);
1690 dev_err(&device->cdev->dev,
1691 "Cancelling request %p failed with rc=%d\n",
1694 cqr->stopclk = get_clock();
1698 default: /* already finished or clear pending - do nothing */
1701 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1702 dasd_schedule_device_bh(device);
1708 * SECTION: Operations of the dasd_block layer.
1712 * Timeout function for dasd_block. This is used when the block layer
1713 * is waiting for something that may not come reliably, (e.g. a state
1716 static void dasd_block_timeout(unsigned long ptr)
1718 unsigned long flags;
1719 struct dasd_block *block;
1721 block = (struct dasd_block *) ptr;
1722 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1723 /* re-activate request queue */
1724 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
1725 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1726 dasd_schedule_block_bh(block);
1730 * Setup timeout for a dasd_block in jiffies.
1732 void dasd_block_set_timer(struct dasd_block *block, int expires)
1735 del_timer(&block->timer);
1737 mod_timer(&block->timer, jiffies + expires);
1741 * Clear timeout for a dasd_block.
1743 void dasd_block_clear_timer(struct dasd_block *block)
1745 del_timer(&block->timer);
1749 * Process finished error recovery ccw.
1751 static void __dasd_process_erp(struct dasd_device *device,
1752 struct dasd_ccw_req *cqr)
1754 dasd_erp_fn_t erp_fn;
1756 if (cqr->status == DASD_CQR_DONE)
1757 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1759 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
1760 erp_fn = device->discipline->erp_postaction(cqr);
1765 * Fetch requests from the block device queue.
1767 static void __dasd_process_request_queue(struct dasd_block *block)
1769 struct request_queue *queue;
1770 struct request *req;
1771 struct dasd_ccw_req *cqr;
1772 struct dasd_device *basedev;
1773 unsigned long flags;
1774 queue = block->request_queue;
1775 basedev = block->base;
1776 /* No queue ? Then there is nothing to do. */
1781 * We requeue request from the block device queue to the ccw
1782 * queue only in two states. In state DASD_STATE_READY the
1783 * partition detection is done and we need to requeue requests
1784 * for that. State DASD_STATE_ONLINE is normal block device
1787 if (basedev->state < DASD_STATE_READY) {
1788 while ((req = blk_fetch_request(block->request_queue)))
1789 __blk_end_request_all(req, -EIO);
1792 /* Now we try to fetch requests from the request queue */
1793 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
1794 if (basedev->features & DASD_FEATURE_READONLY &&
1795 rq_data_dir(req) == WRITE) {
1796 DBF_DEV_EVENT(DBF_ERR, basedev,
1797 "Rejecting write request %p",
1799 blk_start_request(req);
1800 __blk_end_request_all(req, -EIO);
1803 cqr = basedev->discipline->build_cp(basedev, block, req);
1805 if (PTR_ERR(cqr) == -EBUSY)
1806 break; /* normal end condition */
1807 if (PTR_ERR(cqr) == -ENOMEM)
1808 break; /* terminate request queue loop */
1809 if (PTR_ERR(cqr) == -EAGAIN) {
1811 * The current request cannot be build right
1812 * now, we have to try later. If this request
1813 * is the head-of-queue we stop the device
1816 if (!list_empty(&block->ccw_queue))
1819 get_ccwdev_lock(basedev->cdev), flags);
1820 dasd_device_set_stop_bits(basedev,
1821 DASD_STOPPED_PENDING);
1822 spin_unlock_irqrestore(
1823 get_ccwdev_lock(basedev->cdev), flags);
1824 dasd_block_set_timer(block, HZ/2);
1827 DBF_DEV_EVENT(DBF_ERR, basedev,
1828 "CCW creation failed (rc=%ld) "
1831 blk_start_request(req);
1832 __blk_end_request_all(req, -EIO);
1836 * Note: callback is set to dasd_return_cqr_cb in
1837 * __dasd_block_start_head to cover erp requests as well
1839 cqr->callback_data = (void *) req;
1840 cqr->status = DASD_CQR_FILLED;
1841 blk_start_request(req);
1842 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1843 dasd_profile_start(block, cqr, req);
1847 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1849 struct request *req;
1853 req = (struct request *) cqr->callback_data;
1854 dasd_profile_end(cqr->block, cqr, req);
1855 status = cqr->block->base->discipline->free_cp(cqr, req);
1857 error = status ? status : -EIO;
1858 __blk_end_request_all(req, error);
1862 * Process ccw request queue.
1864 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1865 struct list_head *final_queue)
1867 struct list_head *l, *n;
1868 struct dasd_ccw_req *cqr;
1869 dasd_erp_fn_t erp_fn;
1870 unsigned long flags;
1871 struct dasd_device *base = block->base;
1874 /* Process request with final status. */
1875 list_for_each_safe(l, n, &block->ccw_queue) {
1876 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1877 if (cqr->status != DASD_CQR_DONE &&
1878 cqr->status != DASD_CQR_FAILED &&
1879 cqr->status != DASD_CQR_NEED_ERP &&
1880 cqr->status != DASD_CQR_TERMINATED)
1883 if (cqr->status == DASD_CQR_TERMINATED) {
1884 base->discipline->handle_terminated_request(cqr);
1888 /* Process requests that may be recovered */
1889 if (cqr->status == DASD_CQR_NEED_ERP) {
1890 erp_fn = base->discipline->erp_action(cqr);
1895 /* log sense for fatal error */
1896 if (cqr->status == DASD_CQR_FAILED) {
1897 dasd_log_sense(cqr, &cqr->irb);
1900 /* First of all call extended error reporting. */
1901 if (dasd_eer_enabled(base) &&
1902 cqr->status == DASD_CQR_FAILED) {
1903 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1905 /* restart request */
1906 cqr->status = DASD_CQR_FILLED;
1908 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1909 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
1910 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1915 /* Process finished ERP request. */
1917 __dasd_process_erp(base, cqr);
1921 /* Rechain finished requests to final queue */
1922 cqr->endclk = get_clock();
1923 list_move_tail(&cqr->blocklist, final_queue);
1927 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1929 dasd_schedule_block_bh(cqr->block);
1932 static void __dasd_block_start_head(struct dasd_block *block)
1934 struct dasd_ccw_req *cqr;
1936 if (list_empty(&block->ccw_queue))
1938 /* We allways begin with the first requests on the queue, as some
1939 * of previously started requests have to be enqueued on a
1940 * dasd_device again for error recovery.
1942 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1943 if (cqr->status != DASD_CQR_FILLED)
1945 /* Non-temporary stop condition will trigger fail fast */
1946 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1947 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1948 (!dasd_eer_enabled(block->base))) {
1949 cqr->status = DASD_CQR_FAILED;
1950 dasd_schedule_block_bh(block);
1953 /* Don't try to start requests if device is stopped */
1954 if (block->base->stopped)
1957 /* just a fail safe check, should not happen */
1959 cqr->startdev = block->base;
1961 /* make sure that the requests we submit find their way back */
1962 cqr->callback = dasd_return_cqr_cb;
1964 dasd_add_request_tail(cqr);
1969 * Central dasd_block layer routine. Takes requests from the generic
1970 * block layer request queue, creates ccw requests, enqueues them on
1971 * a dasd_device and processes ccw requests that have been returned.
1973 static void dasd_block_tasklet(struct dasd_block *block)
1975 struct list_head final_queue;
1976 struct list_head *l, *n;
1977 struct dasd_ccw_req *cqr;
1979 atomic_set(&block->tasklet_scheduled, 0);
1980 INIT_LIST_HEAD(&final_queue);
1981 spin_lock(&block->queue_lock);
1982 /* Finish off requests on ccw queue */
1983 __dasd_process_block_ccw_queue(block, &final_queue);
1984 spin_unlock(&block->queue_lock);
1985 /* Now call the callback function of requests with final status */
1986 spin_lock_irq(&block->request_queue_lock);
1987 list_for_each_safe(l, n, &final_queue) {
1988 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1989 list_del_init(&cqr->blocklist);
1990 __dasd_cleanup_cqr(cqr);
1992 spin_lock(&block->queue_lock);
1993 /* Get new request from the block device request queue */
1994 __dasd_process_request_queue(block);
1995 /* Now check if the head of the ccw queue needs to be started. */
1996 __dasd_block_start_head(block);
1997 spin_unlock(&block->queue_lock);
1998 spin_unlock_irq(&block->request_queue_lock);
1999 dasd_put_device(block->base);
2002 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2004 wake_up(&dasd_flush_wq);
2008 * Go through all request on the dasd_block request queue, cancel them
2009 * on the respective dasd_device, and return them to the generic
2012 static int dasd_flush_block_queue(struct dasd_block *block)
2014 struct dasd_ccw_req *cqr, *n;
2016 struct list_head flush_queue;
2018 INIT_LIST_HEAD(&flush_queue);
2019 spin_lock_bh(&block->queue_lock);
2022 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2023 /* if this request currently owned by a dasd_device cancel it */
2024 if (cqr->status >= DASD_CQR_QUEUED)
2025 rc = dasd_cancel_req(cqr);
2028 /* Rechain request (including erp chain) so it won't be
2029 * touched by the dasd_block_tasklet anymore.
2030 * Replace the callback so we notice when the request
2031 * is returned from the dasd_device layer.
2033 cqr->callback = _dasd_wake_block_flush_cb;
2034 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2035 list_move_tail(&cqr->blocklist, &flush_queue);
2037 /* moved more than one request - need to restart */
2040 spin_unlock_bh(&block->queue_lock);
2041 /* Now call the callback function of flushed requests */
2043 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2044 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2045 /* Process finished ERP request. */
2047 spin_lock_bh(&block->queue_lock);
2048 __dasd_process_erp(block->base, cqr);
2049 spin_unlock_bh(&block->queue_lock);
2050 /* restart list_for_xx loop since dasd_process_erp
2051 * might remove multiple elements */
2054 /* call the callback function */
2055 spin_lock_irq(&block->request_queue_lock);
2056 cqr->endclk = get_clock();
2057 list_del_init(&cqr->blocklist);
2058 __dasd_cleanup_cqr(cqr);
2059 spin_unlock_irq(&block->request_queue_lock);
2065 * Schedules a call to dasd_tasklet over the device tasklet.
2067 void dasd_schedule_block_bh(struct dasd_block *block)
2069 /* Protect against rescheduling. */
2070 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2072 /* life cycle of block is bound to it's base device */
2073 dasd_get_device(block->base);
2074 tasklet_hi_schedule(&block->tasklet);
2079 * SECTION: external block device operations
2080 * (request queue handling, open, release, etc.)
2084 * Dasd request queue function. Called from ll_rw_blk.c
2086 static void do_dasd_request(struct request_queue *queue)
2088 struct dasd_block *block;
2090 block = queue->queuedata;
2091 spin_lock(&block->queue_lock);
2092 /* Get new request from the block device request queue */
2093 __dasd_process_request_queue(block);
2094 /* Now check if the head of the ccw queue needs to be started. */
2095 __dasd_block_start_head(block);
2096 spin_unlock(&block->queue_lock);
2100 * Allocate and initialize request queue and default I/O scheduler.
2102 static int dasd_alloc_queue(struct dasd_block *block)
2106 block->request_queue = blk_init_queue(do_dasd_request,
2107 &block->request_queue_lock);
2108 if (block->request_queue == NULL)
2111 block->request_queue->queuedata = block;
2113 elevator_exit(block->request_queue->elevator);
2114 block->request_queue->elevator = NULL;
2115 rc = elevator_init(block->request_queue, "deadline");
2117 blk_cleanup_queue(block->request_queue);
2124 * Allocate and initialize request queue.
2126 static void dasd_setup_queue(struct dasd_block *block)
2130 blk_queue_logical_block_size(block->request_queue, block->bp_block);
2131 max = block->base->discipline->max_blocks << block->s2b_shift;
2132 blk_queue_max_sectors(block->request_queue, max);
2133 blk_queue_max_phys_segments(block->request_queue, -1L);
2134 blk_queue_max_hw_segments(block->request_queue, -1L);
2135 /* with page sized segments we can translate each segement into
2138 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2139 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2140 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
2144 * Deactivate and free request queue.
2146 static void dasd_free_queue(struct dasd_block *block)
2148 if (block->request_queue) {
2149 blk_cleanup_queue(block->request_queue);
2150 block->request_queue = NULL;
2155 * Flush request on the request queue.
2157 static void dasd_flush_request_queue(struct dasd_block *block)
2159 struct request *req;
2161 if (!block->request_queue)
2164 spin_lock_irq(&block->request_queue_lock);
2165 while ((req = blk_fetch_request(block->request_queue)))
2166 __blk_end_request_all(req, -EIO);
2167 spin_unlock_irq(&block->request_queue_lock);
2170 static int dasd_open(struct block_device *bdev, fmode_t mode)
2172 struct dasd_block *block = bdev->bd_disk->private_data;
2173 struct dasd_device *base = block->base;
2176 atomic_inc(&block->open_count);
2177 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2182 if (!try_module_get(base->discipline->owner)) {
2187 if (dasd_probeonly) {
2188 dev_info(&base->cdev->dev,
2189 "Accessing the DASD failed because it is in "
2190 "probeonly mode\n");
2195 if (base->state <= DASD_STATE_BASIC) {
2196 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2197 " Cannot open unrecognized device");
2205 module_put(base->discipline->owner);
2207 atomic_dec(&block->open_count);
2211 static int dasd_release(struct gendisk *disk, fmode_t mode)
2213 struct dasd_block *block = disk->private_data;
2215 atomic_dec(&block->open_count);
2216 module_put(block->base->discipline->owner);
2221 * Return disk geometry.
2223 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2225 struct dasd_block *block;
2226 struct dasd_device *base;
2228 block = bdev->bd_disk->private_data;
2233 if (!base->discipline ||
2234 !base->discipline->fill_geometry)
2237 base->discipline->fill_geometry(block, geo);
2238 geo->start = get_start_sect(bdev) >> block->s2b_shift;
2242 const struct block_device_operations
2243 dasd_device_operations = {
2244 .owner = THIS_MODULE,
2246 .release = dasd_release,
2247 .ioctl = dasd_ioctl,
2248 .compat_ioctl = dasd_ioctl,
2249 .getgeo = dasd_getgeo,
2252 /*******************************************************************************
2253 * end of block device operations
2259 #ifdef CONFIG_PROC_FS
2263 if (dasd_page_cache != NULL) {
2264 kmem_cache_destroy(dasd_page_cache);
2265 dasd_page_cache = NULL;
2267 dasd_gendisk_exit();
2269 if (dasd_debug_area != NULL) {
2270 debug_unregister(dasd_debug_area);
2271 dasd_debug_area = NULL;
2276 * SECTION: common functions for ccw_driver use
2279 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2281 struct ccw_device *cdev = data;
2284 ret = ccw_device_set_online(cdev);
2286 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2287 dev_name(&cdev->dev), ret);
2289 struct dasd_device *device = dasd_device_from_cdev(cdev);
2290 wait_event(dasd_init_waitq, _wait_for_device(device));
2291 dasd_put_device(device);
2296 * Initial attempt at a probe function. this can be simplified once
2297 * the other detection code is gone.
2299 int dasd_generic_probe(struct ccw_device *cdev,
2300 struct dasd_discipline *discipline)
2304 ret = dasd_add_sysfs_files(cdev);
2306 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2307 "dasd_generic_probe: could not add "
2311 cdev->handler = &dasd_int_handler;
2314 * Automatically online either all dasd devices (dasd_autodetect)
2315 * or all devices specified with dasd= parameters during
2318 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2319 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
2320 async_schedule(dasd_generic_auto_online, cdev);
2325 * This will one day be called from a global not_oper handler.
2326 * It is also used by driver_unregister during module unload.
2328 void dasd_generic_remove(struct ccw_device *cdev)
2330 struct dasd_device *device;
2331 struct dasd_block *block;
2333 cdev->handler = NULL;
2335 dasd_remove_sysfs_files(cdev);
2336 device = dasd_device_from_cdev(cdev);
2339 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2340 /* Already doing offline processing */
2341 dasd_put_device(device);
2345 * This device is removed unconditionally. Set offline
2346 * flag to prevent dasd_open from opening it while it is
2347 * no quite down yet.
2349 dasd_set_target_state(device, DASD_STATE_NEW);
2350 /* dasd_delete_device destroys the device reference. */
2351 block = device->block;
2352 device->block = NULL;
2353 dasd_delete_device(device);
2355 * life cycle of block is bound to device, so delete it after
2356 * device was safely removed
2359 dasd_free_block(block);
2363 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
2364 * the device is detected for the first time and is supposed to be used
2365 * or the user has started activation through sysfs.
2367 int dasd_generic_set_online(struct ccw_device *cdev,
2368 struct dasd_discipline *base_discipline)
2370 struct dasd_discipline *discipline;
2371 struct dasd_device *device;
2374 /* first online clears initial online feature flag */
2375 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
2376 device = dasd_create_device(cdev);
2378 return PTR_ERR(device);
2380 discipline = base_discipline;
2381 if (device->features & DASD_FEATURE_USEDIAG) {
2382 if (!dasd_diag_discipline_pointer) {
2383 pr_warning("%s Setting the DASD online failed because "
2384 "of missing DIAG discipline\n",
2385 dev_name(&cdev->dev));
2386 dasd_delete_device(device);
2389 discipline = dasd_diag_discipline_pointer;
2391 if (!try_module_get(base_discipline->owner)) {
2392 dasd_delete_device(device);
2395 if (!try_module_get(discipline->owner)) {
2396 module_put(base_discipline->owner);
2397 dasd_delete_device(device);
2400 device->base_discipline = base_discipline;
2401 device->discipline = discipline;
2403 /* check_device will allocate block device if necessary */
2404 rc = discipline->check_device(device);
2406 pr_warning("%s Setting the DASD online with discipline %s "
2407 "failed with rc=%i\n",
2408 dev_name(&cdev->dev), discipline->name, rc);
2409 module_put(discipline->owner);
2410 module_put(base_discipline->owner);
2411 dasd_delete_device(device);
2415 dasd_set_target_state(device, DASD_STATE_ONLINE);
2416 if (device->state <= DASD_STATE_KNOWN) {
2417 pr_warning("%s Setting the DASD online failed because of a "
2418 "missing discipline\n", dev_name(&cdev->dev));
2420 dasd_set_target_state(device, DASD_STATE_NEW);
2422 dasd_free_block(device->block);
2423 dasd_delete_device(device);
2425 pr_debug("dasd_generic device %s found\n",
2426 dev_name(&cdev->dev));
2427 dasd_put_device(device);
2431 int dasd_generic_set_offline(struct ccw_device *cdev)
2433 struct dasd_device *device;
2434 struct dasd_block *block;
2435 int max_count, open_count;
2437 device = dasd_device_from_cdev(cdev);
2439 return PTR_ERR(device);
2440 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2441 /* Already doing offline processing */
2442 dasd_put_device(device);
2446 * We must make sure that this device is currently not in use.
2447 * The open_count is increased for every opener, that includes
2448 * the blkdev_get in dasd_scan_partitions. We are only interested
2449 * in the other openers.
2451 if (device->block) {
2452 max_count = device->block->bdev ? 0 : -1;
2453 open_count = atomic_read(&device->block->open_count);
2454 if (open_count > max_count) {
2456 pr_warning("%s: The DASD cannot be set offline "
2457 "with open count %i\n",
2458 dev_name(&cdev->dev), open_count);
2460 pr_warning("%s: The DASD cannot be set offline "
2461 "while it is in use\n",
2462 dev_name(&cdev->dev));
2463 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2464 dasd_put_device(device);
2468 dasd_set_target_state(device, DASD_STATE_NEW);
2469 /* dasd_delete_device destroys the device reference. */
2470 block = device->block;
2471 device->block = NULL;
2472 dasd_delete_device(device);
2474 * life cycle of block is bound to device, so delete it after
2475 * device was safely removed
2478 dasd_free_block(block);
2482 int dasd_generic_notify(struct ccw_device *cdev, int event)
2484 struct dasd_device *device;
2485 struct dasd_ccw_req *cqr;
2488 device = dasd_device_from_cdev_locked(cdev);
2496 /* First of all call extended error reporting. */
2497 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2499 if (device->state < DASD_STATE_BASIC)
2501 /* Device is active. We want to keep it. */
2502 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2503 if (cqr->status == DASD_CQR_IN_IO) {
2504 cqr->status = DASD_CQR_QUEUED;
2507 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
2508 dasd_device_clear_timer(device);
2509 dasd_schedule_device_bh(device);
2513 /* FIXME: add a sanity check. */
2514 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
2515 if (device->stopped & DASD_UNRESUMED_PM) {
2516 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
2517 dasd_restore_device(device);
2521 dasd_schedule_device_bh(device);
2523 dasd_schedule_block_bh(device->block);
2527 dasd_put_device(device);
2531 int dasd_generic_pm_freeze(struct ccw_device *cdev)
2533 struct dasd_ccw_req *cqr, *n;
2535 struct list_head freeze_queue;
2536 struct dasd_device *device = dasd_device_from_cdev(cdev);
2539 return PTR_ERR(device);
2540 /* disallow new I/O */
2541 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
2542 /* clear active requests */
2543 INIT_LIST_HEAD(&freeze_queue);
2544 spin_lock_irq(get_ccwdev_lock(cdev));
2546 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2547 /* Check status and move request to flush_queue */
2548 if (cqr->status == DASD_CQR_IN_IO) {
2549 rc = device->discipline->term_IO(cqr);
2551 /* unable to terminate requeust */
2552 dev_err(&device->cdev->dev,
2553 "Unable to terminate request %p "
2554 "on suspend\n", cqr);
2555 spin_unlock_irq(get_ccwdev_lock(cdev));
2556 dasd_put_device(device);
2560 list_move_tail(&cqr->devlist, &freeze_queue);
2563 spin_unlock_irq(get_ccwdev_lock(cdev));
2565 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2566 wait_event(dasd_flush_wq,
2567 (cqr->status != DASD_CQR_CLEAR_PENDING));
2568 if (cqr->status == DASD_CQR_CLEARED)
2569 cqr->status = DASD_CQR_QUEUED;
2571 /* move freeze_queue to start of the ccw_queue */
2572 spin_lock_irq(get_ccwdev_lock(cdev));
2573 list_splice_tail(&freeze_queue, &device->ccw_queue);
2574 spin_unlock_irq(get_ccwdev_lock(cdev));
2576 if (device->discipline->freeze)
2577 rc = device->discipline->freeze(device);
2579 dasd_put_device(device);
2582 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2584 int dasd_generic_restore_device(struct ccw_device *cdev)
2586 struct dasd_device *device = dasd_device_from_cdev(cdev);
2590 return PTR_ERR(device);
2592 /* allow new IO again */
2593 dasd_device_remove_stop_bits(device,
2594 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
2596 dasd_schedule_device_bh(device);
2599 * call discipline restore function
2600 * if device is stopped do nothing e.g. for disconnected devices
2602 if (device->discipline->restore && !(device->stopped))
2603 rc = device->discipline->restore(device);
2604 if (rc || device->stopped)
2606 * if the resume failed for the DASD we put it in
2607 * an UNRESUMED stop state
2609 device->stopped |= DASD_UNRESUMED_PM;
2612 dasd_schedule_block_bh(device->block);
2614 dasd_put_device(device);
2617 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2619 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2621 int rdc_buffer_size,
2624 struct dasd_ccw_req *cqr;
2626 unsigned long *idaw;
2628 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2631 /* internal error 13 - Allocating the RDC request failed*/
2632 dev_err(&device->cdev->dev,
2633 "An error occurred in the DASD device driver, "
2634 "reason=%s\n", "13");
2639 ccw->cmd_code = CCW_CMD_RDC;
2640 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2641 idaw = (unsigned long *) (cqr->data);
2642 ccw->cda = (__u32)(addr_t) idaw;
2643 ccw->flags = CCW_FLAG_IDA;
2644 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2646 ccw->cda = (__u32)(addr_t) rdc_buffer;
2650 ccw->count = rdc_buffer_size;
2651 cqr->startdev = device;
2652 cqr->memdev = device;
2653 cqr->expires = 10*HZ;
2655 cqr->buildclk = get_clock();
2656 cqr->status = DASD_CQR_FILLED;
2661 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
2662 void *rdc_buffer, int rdc_buffer_size)
2665 struct dasd_ccw_req *cqr;
2667 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
2670 return PTR_ERR(cqr);
2672 ret = dasd_sleep_on(cqr);
2673 dasd_sfree_request(cqr, cqr->memdev);
2676 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
2679 * In command mode and transport mode we need to look for sense
2680 * data in different places. The sense data itself is allways
2681 * an array of 32 bytes, so we can unify the sense data access
2684 char *dasd_get_sense(struct irb *irb)
2686 struct tsb *tsb = NULL;
2689 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2690 if (irb->scsw.tm.tcw)
2691 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2693 if (tsb && tsb->length == 64 && tsb->flags)
2694 switch (tsb->flags & 0x07) {
2695 case 1: /* tsa_iostat */
2696 sense = tsb->tsa.iostat.sense;
2698 case 2: /* tsa_ddpc */
2699 sense = tsb->tsa.ddpc.sense;
2702 /* currently we don't use interrogate data */
2705 } else if (irb->esw.esw0.erw.cons) {
2710 EXPORT_SYMBOL_GPL(dasd_get_sense);
2712 static int __init dasd_init(void)
2716 init_waitqueue_head(&dasd_init_waitq);
2717 init_waitqueue_head(&dasd_flush_wq);
2718 init_waitqueue_head(&generic_waitq);
2720 /* register 'common' DASD debug area, used for all DBF_XXX calls */
2721 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
2722 if (dasd_debug_area == NULL) {
2726 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2727 debug_set_level(dasd_debug_area, DBF_WARNING);
2729 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2731 dasd_diag_discipline_pointer = NULL;
2733 rc = dasd_devmap_init();
2736 rc = dasd_gendisk_init();
2742 rc = dasd_eer_init();
2745 #ifdef CONFIG_PROC_FS
2746 rc = dasd_proc_init();
2753 pr_info("The DASD device driver could not be initialized\n");
2758 module_init(dasd_init);
2759 module_exit(dasd_exit);
2761 EXPORT_SYMBOL(dasd_debug_area);
2762 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2764 EXPORT_SYMBOL(dasd_add_request_head);
2765 EXPORT_SYMBOL(dasd_add_request_tail);
2766 EXPORT_SYMBOL(dasd_cancel_req);
2767 EXPORT_SYMBOL(dasd_device_clear_timer);
2768 EXPORT_SYMBOL(dasd_block_clear_timer);
2769 EXPORT_SYMBOL(dasd_enable_device);
2770 EXPORT_SYMBOL(dasd_int_handler);
2771 EXPORT_SYMBOL(dasd_kfree_request);
2772 EXPORT_SYMBOL(dasd_kick_device);
2773 EXPORT_SYMBOL(dasd_kmalloc_request);
2774 EXPORT_SYMBOL(dasd_schedule_device_bh);
2775 EXPORT_SYMBOL(dasd_schedule_block_bh);
2776 EXPORT_SYMBOL(dasd_set_target_state);
2777 EXPORT_SYMBOL(dasd_device_set_timer);
2778 EXPORT_SYMBOL(dasd_block_set_timer);
2779 EXPORT_SYMBOL(dasd_sfree_request);
2780 EXPORT_SYMBOL(dasd_sleep_on);
2781 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2782 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2783 EXPORT_SYMBOL(dasd_smalloc_request);
2784 EXPORT_SYMBOL(dasd_start_IO);
2785 EXPORT_SYMBOL(dasd_term_IO);
2787 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2788 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2789 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2790 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2791 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2792 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2793 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2794 EXPORT_SYMBOL_GPL(dasd_alloc_block);
2795 EXPORT_SYMBOL_GPL(dasd_free_block);