2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
7 * Copyright IBM Corp. 1999, 2009
10 #define KMSG_COMPONENT "dasd"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kmod.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/ctype.h>
17 #include <linux/major.h>
18 #include <linux/slab.h>
19 #include <linux/hdreg.h>
20 #include <linux/async.h>
21 #include <linux/mutex.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/vmalloc.h>
26 #include <asm/ccwdev.h>
27 #include <asm/ebcdic.h>
28 #include <asm/idals.h>
33 #define PRINTK_HEADER "dasd:"
37 * SECTION: Constant definitions to be used within this file
39 #define DASD_CHANQ_MAX_SIZE 4
41 #define DASD_DIAG_MOD "dasd_diag_mod"
44 * SECTION: exported variables of dasd.c
46 debug_info_t *dasd_debug_area;
47 EXPORT_SYMBOL(dasd_debug_area);
48 static struct dentry *dasd_debugfs_root_entry;
49 struct dasd_discipline *dasd_diag_discipline_pointer;
50 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
51 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
53 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
54 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
55 " Copyright IBM Corp. 2000");
56 MODULE_SUPPORTED_DEVICE("dasd");
57 MODULE_LICENSE("GPL");
60 * SECTION: prototypes for static functions of dasd.c
62 static int dasd_alloc_queue(struct dasd_block *);
63 static void dasd_setup_queue(struct dasd_block *);
64 static void dasd_free_queue(struct dasd_block *);
65 static void dasd_flush_request_queue(struct dasd_block *);
66 static int dasd_flush_block_queue(struct dasd_block *);
67 static void dasd_device_tasklet(struct dasd_device *);
68 static void dasd_block_tasklet(struct dasd_block *);
69 static void do_kick_device(struct work_struct *);
70 static void do_restore_device(struct work_struct *);
71 static void do_reload_device(struct work_struct *);
72 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
73 static void dasd_device_timeout(unsigned long);
74 static void dasd_block_timeout(unsigned long);
75 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
76 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
77 static void dasd_profile_exit(struct dasd_profile *);
80 * SECTION: Operations on the device structure.
82 static wait_queue_head_t dasd_init_waitq;
83 static wait_queue_head_t dasd_flush_wq;
84 static wait_queue_head_t generic_waitq;
85 static wait_queue_head_t shutdown_waitq;
88 * Allocate memory for a new device structure.
90 struct dasd_device *dasd_alloc_device(void)
92 struct dasd_device *device;
94 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
96 return ERR_PTR(-ENOMEM);
98 /* Get two pages for normal block device operations. */
99 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
100 if (!device->ccw_mem) {
102 return ERR_PTR(-ENOMEM);
104 /* Get one page for error recovery. */
105 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
106 if (!device->erp_mem) {
107 free_pages((unsigned long) device->ccw_mem, 1);
109 return ERR_PTR(-ENOMEM);
112 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
113 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
114 spin_lock_init(&device->mem_lock);
115 atomic_set(&device->tasklet_scheduled, 0);
116 tasklet_init(&device->tasklet,
117 (void (*)(unsigned long)) dasd_device_tasklet,
118 (unsigned long) device);
119 INIT_LIST_HEAD(&device->ccw_queue);
120 init_timer(&device->timer);
121 device->timer.function = dasd_device_timeout;
122 device->timer.data = (unsigned long) device;
123 INIT_WORK(&device->kick_work, do_kick_device);
124 INIT_WORK(&device->restore_device, do_restore_device);
125 INIT_WORK(&device->reload_device, do_reload_device);
126 device->state = DASD_STATE_NEW;
127 device->target = DASD_STATE_NEW;
128 mutex_init(&device->state_mutex);
129 spin_lock_init(&device->profile.lock);
134 * Free memory of a device structure.
136 void dasd_free_device(struct dasd_device *device)
138 kfree(device->private);
139 free_page((unsigned long) device->erp_mem);
140 free_pages((unsigned long) device->ccw_mem, 1);
145 * Allocate memory for a new device structure.
147 struct dasd_block *dasd_alloc_block(void)
149 struct dasd_block *block;
151 block = kzalloc(sizeof(*block), GFP_ATOMIC);
153 return ERR_PTR(-ENOMEM);
154 /* open_count = 0 means device online but not in use */
155 atomic_set(&block->open_count, -1);
157 spin_lock_init(&block->request_queue_lock);
158 atomic_set(&block->tasklet_scheduled, 0);
159 tasklet_init(&block->tasklet,
160 (void (*)(unsigned long)) dasd_block_tasklet,
161 (unsigned long) block);
162 INIT_LIST_HEAD(&block->ccw_queue);
163 spin_lock_init(&block->queue_lock);
164 init_timer(&block->timer);
165 block->timer.function = dasd_block_timeout;
166 block->timer.data = (unsigned long) block;
167 spin_lock_init(&block->profile.lock);
171 EXPORT_SYMBOL_GPL(dasd_alloc_block);
174 * Free memory of a device structure.
176 void dasd_free_block(struct dasd_block *block)
180 EXPORT_SYMBOL_GPL(dasd_free_block);
183 * Make a new device known to the system.
185 static int dasd_state_new_to_known(struct dasd_device *device)
190 * As long as the device is not in state DASD_STATE_NEW we want to
191 * keep the reference count > 0.
193 dasd_get_device(device);
196 rc = dasd_alloc_queue(device->block);
198 dasd_put_device(device);
202 device->state = DASD_STATE_KNOWN;
207 * Let the system forget about a device.
209 static int dasd_state_known_to_new(struct dasd_device *device)
211 /* Disable extended error reporting for this device. */
212 dasd_eer_disable(device);
213 /* Forget the discipline information. */
214 if (device->discipline) {
215 if (device->discipline->uncheck_device)
216 device->discipline->uncheck_device(device);
217 module_put(device->discipline->owner);
219 device->discipline = NULL;
220 if (device->base_discipline)
221 module_put(device->base_discipline->owner);
222 device->base_discipline = NULL;
223 device->state = DASD_STATE_NEW;
226 dasd_free_queue(device->block);
228 /* Give up reference we took in dasd_state_new_to_known. */
229 dasd_put_device(device);
233 static struct dentry *dasd_debugfs_setup(const char *name,
234 struct dentry *base_dentry)
240 pde = debugfs_create_dir(name, base_dentry);
241 if (!pde || IS_ERR(pde))
247 * Request the irq line for the device.
249 static int dasd_state_known_to_basic(struct dasd_device *device)
251 struct dasd_block *block = device->block;
254 /* Allocate and register gendisk structure. */
256 rc = dasd_gendisk_alloc(block);
259 block->debugfs_dentry =
260 dasd_debugfs_setup(block->gdp->disk_name,
261 dasd_debugfs_root_entry);
262 dasd_profile_init(&block->profile, block->debugfs_dentry);
263 if (dasd_global_profile_level == DASD_PROFILE_ON)
264 dasd_profile_on(&device->block->profile);
266 device->debugfs_dentry =
267 dasd_debugfs_setup(dev_name(&device->cdev->dev),
268 dasd_debugfs_root_entry);
269 dasd_profile_init(&device->profile, device->debugfs_dentry);
271 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
272 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
274 debug_register_view(device->debug_area, &debug_sprintf_view);
275 debug_set_level(device->debug_area, DBF_WARNING);
276 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
278 device->state = DASD_STATE_BASIC;
284 * Release the irq line for the device. Terminate any running i/o.
286 static int dasd_state_basic_to_known(struct dasd_device *device)
290 if (device->discipline->basic_to_known) {
291 rc = device->discipline->basic_to_known(device);
297 dasd_profile_exit(&device->block->profile);
298 debugfs_remove(device->block->debugfs_dentry);
299 dasd_gendisk_free(device->block);
300 dasd_block_clear_timer(device->block);
302 rc = dasd_flush_device_queue(device);
305 dasd_device_clear_timer(device);
306 dasd_profile_exit(&device->profile);
307 debugfs_remove(device->debugfs_dentry);
308 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
309 if (device->debug_area != NULL) {
310 debug_unregister(device->debug_area);
311 device->debug_area = NULL;
313 device->state = DASD_STATE_KNOWN;
318 * Do the initial analysis. The do_analysis function may return
319 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
320 * until the discipline decides to continue the startup sequence
321 * by calling the function dasd_change_state. The eckd disciplines
322 * uses this to start a ccw that detects the format. The completion
323 * interrupt for this detection ccw uses the kernel event daemon to
324 * trigger the call to dasd_change_state. All this is done in the
325 * discipline code, see dasd_eckd.c.
326 * After the analysis ccw is done (do_analysis returned 0) the block
328 * In case the analysis returns an error, the device setup is stopped
329 * (a fake disk was already added to allow formatting).
331 static int dasd_state_basic_to_ready(struct dasd_device *device)
334 struct dasd_block *block;
337 block = device->block;
338 /* make disk known with correct capacity */
340 if (block->base->discipline->do_analysis != NULL)
341 rc = block->base->discipline->do_analysis(block);
344 device->state = DASD_STATE_UNFMT;
349 dasd_setup_queue(block);
350 set_capacity(block->gdp,
351 block->blocks << block->s2b_shift);
352 device->state = DASD_STATE_READY;
353 rc = dasd_scan_partitions(block);
355 device->state = DASD_STATE_BASIC;
359 device->state = DASD_STATE_READY;
362 if (device->discipline->basic_to_ready)
363 rc = device->discipline->basic_to_ready(device);
368 int _wait_for_empty_queues(struct dasd_device *device)
371 return list_empty(&device->ccw_queue) &&
372 list_empty(&device->block->ccw_queue);
374 return list_empty(&device->ccw_queue);
378 * Remove device from block device layer. Destroy dirty buffers.
379 * Forget format information. Check if the target level is basic
380 * and if it is create fake disk for formatting.
382 static int dasd_state_ready_to_basic(struct dasd_device *device)
386 device->state = DASD_STATE_BASIC;
388 struct dasd_block *block = device->block;
389 rc = dasd_flush_block_queue(block);
391 device->state = DASD_STATE_READY;
394 dasd_flush_request_queue(block);
395 dasd_destroy_partitions(block);
398 block->s2b_shift = 0;
406 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
408 device->state = DASD_STATE_BASIC;
413 * Make the device online and schedule the bottom half to start
414 * the requeueing of requests from the linux request queue to the
418 dasd_state_ready_to_online(struct dasd_device * device)
420 struct gendisk *disk;
421 struct disk_part_iter piter;
422 struct hd_struct *part;
424 device->state = DASD_STATE_ONLINE;
426 dasd_schedule_block_bh(device->block);
427 if ((device->features & DASD_FEATURE_USERAW)) {
428 disk = device->block->gdp;
429 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
432 disk = device->block->bdev->bd_disk;
433 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
434 while ((part = disk_part_iter_next(&piter)))
435 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
436 disk_part_iter_exit(&piter);
442 * Stop the requeueing of requests again.
444 static int dasd_state_online_to_ready(struct dasd_device *device)
447 struct gendisk *disk;
448 struct disk_part_iter piter;
449 struct hd_struct *part;
451 if (device->discipline->online_to_ready) {
452 rc = device->discipline->online_to_ready(device);
457 device->state = DASD_STATE_READY;
458 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
459 disk = device->block->bdev->bd_disk;
460 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
461 while ((part = disk_part_iter_next(&piter)))
462 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
463 disk_part_iter_exit(&piter);
469 * Device startup state changes.
471 static int dasd_increase_state(struct dasd_device *device)
476 if (device->state == DASD_STATE_NEW &&
477 device->target >= DASD_STATE_KNOWN)
478 rc = dasd_state_new_to_known(device);
481 device->state == DASD_STATE_KNOWN &&
482 device->target >= DASD_STATE_BASIC)
483 rc = dasd_state_known_to_basic(device);
486 device->state == DASD_STATE_BASIC &&
487 device->target >= DASD_STATE_READY)
488 rc = dasd_state_basic_to_ready(device);
491 device->state == DASD_STATE_UNFMT &&
492 device->target > DASD_STATE_UNFMT)
496 device->state == DASD_STATE_READY &&
497 device->target >= DASD_STATE_ONLINE)
498 rc = dasd_state_ready_to_online(device);
504 * Device shutdown state changes.
506 static int dasd_decrease_state(struct dasd_device *device)
511 if (device->state == DASD_STATE_ONLINE &&
512 device->target <= DASD_STATE_READY)
513 rc = dasd_state_online_to_ready(device);
516 device->state == DASD_STATE_READY &&
517 device->target <= DASD_STATE_BASIC)
518 rc = dasd_state_ready_to_basic(device);
521 device->state == DASD_STATE_UNFMT &&
522 device->target <= DASD_STATE_BASIC)
523 rc = dasd_state_unfmt_to_basic(device);
526 device->state == DASD_STATE_BASIC &&
527 device->target <= DASD_STATE_KNOWN)
528 rc = dasd_state_basic_to_known(device);
531 device->state == DASD_STATE_KNOWN &&
532 device->target <= DASD_STATE_NEW)
533 rc = dasd_state_known_to_new(device);
539 * This is the main startup/shutdown routine.
541 static void dasd_change_state(struct dasd_device *device)
545 if (device->state == device->target)
546 /* Already where we want to go today... */
548 if (device->state < device->target)
549 rc = dasd_increase_state(device);
551 rc = dasd_decrease_state(device);
555 device->target = device->state;
557 /* let user-space know that the device status changed */
558 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
560 if (device->state == device->target)
561 wake_up(&dasd_init_waitq);
565 * Kick starter for devices that did not complete the startup/shutdown
566 * procedure or were sleeping because of a pending state.
567 * dasd_kick_device will schedule a call do do_kick_device to the kernel
570 static void do_kick_device(struct work_struct *work)
572 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
573 mutex_lock(&device->state_mutex);
574 dasd_change_state(device);
575 mutex_unlock(&device->state_mutex);
576 dasd_schedule_device_bh(device);
577 dasd_put_device(device);
580 void dasd_kick_device(struct dasd_device *device)
582 dasd_get_device(device);
583 /* queue call to dasd_kick_device to the kernel event daemon. */
584 if (!schedule_work(&device->kick_work))
585 dasd_put_device(device);
587 EXPORT_SYMBOL(dasd_kick_device);
590 * dasd_reload_device will schedule a call do do_reload_device to the kernel
593 static void do_reload_device(struct work_struct *work)
595 struct dasd_device *device = container_of(work, struct dasd_device,
597 device->discipline->reload(device);
598 dasd_put_device(device);
601 void dasd_reload_device(struct dasd_device *device)
603 dasd_get_device(device);
604 /* queue call to dasd_reload_device to the kernel event daemon. */
605 if (!schedule_work(&device->reload_device))
606 dasd_put_device(device);
608 EXPORT_SYMBOL(dasd_reload_device);
611 * dasd_restore_device will schedule a call do do_restore_device to the kernel
614 static void do_restore_device(struct work_struct *work)
616 struct dasd_device *device = container_of(work, struct dasd_device,
618 device->cdev->drv->restore(device->cdev);
619 dasd_put_device(device);
622 void dasd_restore_device(struct dasd_device *device)
624 dasd_get_device(device);
625 /* queue call to dasd_restore_device to the kernel event daemon. */
626 if (!schedule_work(&device->restore_device))
627 dasd_put_device(device);
631 * Set the target state for a device and starts the state change.
633 void dasd_set_target_state(struct dasd_device *device, int target)
635 dasd_get_device(device);
636 mutex_lock(&device->state_mutex);
637 /* If we are in probeonly mode stop at DASD_STATE_READY. */
638 if (dasd_probeonly && target > DASD_STATE_READY)
639 target = DASD_STATE_READY;
640 if (device->target != target) {
641 if (device->state == target)
642 wake_up(&dasd_init_waitq);
643 device->target = target;
645 if (device->state != device->target)
646 dasd_change_state(device);
647 mutex_unlock(&device->state_mutex);
648 dasd_put_device(device);
650 EXPORT_SYMBOL(dasd_set_target_state);
653 * Enable devices with device numbers in [from..to].
655 static inline int _wait_for_device(struct dasd_device *device)
657 return (device->state == device->target);
660 void dasd_enable_device(struct dasd_device *device)
662 dasd_set_target_state(device, DASD_STATE_ONLINE);
663 if (device->state <= DASD_STATE_KNOWN)
664 /* No discipline for device found. */
665 dasd_set_target_state(device, DASD_STATE_NEW);
666 /* Now wait for the devices to come up. */
667 wait_event(dasd_init_waitq, _wait_for_device(device));
669 dasd_reload_device(device);
670 if (device->discipline->kick_validate)
671 device->discipline->kick_validate(device);
673 EXPORT_SYMBOL(dasd_enable_device);
676 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
679 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
681 #ifdef CONFIG_DASD_PROFILE
682 struct dasd_profile dasd_global_profile = {
683 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
685 static struct dentry *dasd_debugfs_global_entry;
688 * Add profiling information for cqr before execution.
690 static void dasd_profile_start(struct dasd_block *block,
691 struct dasd_ccw_req *cqr,
695 unsigned int counter;
696 struct dasd_device *device;
698 /* count the length of the chanq for statistics */
700 if (dasd_global_profile_level || block->profile.data)
701 list_for_each(l, &block->ccw_queue)
705 spin_lock(&dasd_global_profile.lock);
706 if (dasd_global_profile.data) {
707 dasd_global_profile.data->dasd_io_nr_req[counter]++;
708 if (rq_data_dir(req) == READ)
709 dasd_global_profile.data->dasd_read_nr_req[counter]++;
711 spin_unlock(&dasd_global_profile.lock);
713 spin_lock(&block->profile.lock);
714 if (block->profile.data) {
715 block->profile.data->dasd_io_nr_req[counter]++;
716 if (rq_data_dir(req) == READ)
717 block->profile.data->dasd_read_nr_req[counter]++;
719 spin_unlock(&block->profile.lock);
722 * We count the request for the start device, even though it may run on
723 * some other device due to error recovery. This way we make sure that
724 * we count each request only once.
726 device = cqr->startdev;
727 if (device->profile.data) {
728 counter = 1; /* request is not yet queued on the start device */
729 list_for_each(l, &device->ccw_queue)
733 spin_lock(&device->profile.lock);
734 if (device->profile.data) {
735 device->profile.data->dasd_io_nr_req[counter]++;
736 if (rq_data_dir(req) == READ)
737 device->profile.data->dasd_read_nr_req[counter]++;
739 spin_unlock(&device->profile.lock);
743 * Add profiling information for cqr after execution.
746 #define dasd_profile_counter(value, index) \
748 for (index = 0; index < 31 && value >> (2+index); index++) \
752 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
765 /* in case of an overflow, reset the whole profile */
766 if (data->dasd_io_reqs == UINT_MAX) {
767 memset(data, 0, sizeof(*data));
768 getnstimeofday(&data->starttod);
770 data->dasd_io_reqs++;
771 data->dasd_io_sects += sectors;
773 data->dasd_io_alias++;
777 data->dasd_io_secs[sectors_ind]++;
778 data->dasd_io_times[tottime_ind]++;
779 data->dasd_io_timps[tottimeps_ind]++;
780 data->dasd_io_time1[strtime_ind]++;
781 data->dasd_io_time2[irqtime_ind]++;
782 data->dasd_io_time2ps[irqtimeps_ind]++;
783 data->dasd_io_time3[endtime_ind]++;
786 data->dasd_read_reqs++;
787 data->dasd_read_sects += sectors;
789 data->dasd_read_alias++;
791 data->dasd_read_tpm++;
792 data->dasd_read_secs[sectors_ind]++;
793 data->dasd_read_times[tottime_ind]++;
794 data->dasd_read_time1[strtime_ind]++;
795 data->dasd_read_time2[irqtime_ind]++;
796 data->dasd_read_time3[endtime_ind]++;
800 static void dasd_profile_end(struct dasd_block *block,
801 struct dasd_ccw_req *cqr,
804 long strtime, irqtime, endtime, tottime; /* in microseconds */
805 long tottimeps, sectors;
806 struct dasd_device *device;
807 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
808 int irqtime_ind, irqtimeps_ind, endtime_ind;
810 device = cqr->startdev;
811 if (!(dasd_global_profile_level ||
812 block->profile.data ||
813 device->profile.data))
816 sectors = blk_rq_sectors(req);
817 if (!cqr->buildclk || !cqr->startclk ||
818 !cqr->stopclk || !cqr->endclk ||
822 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
823 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
824 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
825 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
826 tottimeps = tottime / sectors;
828 dasd_profile_counter(sectors, sectors_ind);
829 dasd_profile_counter(tottime, tottime_ind);
830 dasd_profile_counter(tottimeps, tottimeps_ind);
831 dasd_profile_counter(strtime, strtime_ind);
832 dasd_profile_counter(irqtime, irqtime_ind);
833 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
834 dasd_profile_counter(endtime, endtime_ind);
836 spin_lock(&dasd_global_profile.lock);
837 if (dasd_global_profile.data) {
838 dasd_profile_end_add_data(dasd_global_profile.data,
839 cqr->startdev != block->base,
841 rq_data_dir(req) == READ,
842 sectors, sectors_ind, tottime_ind,
843 tottimeps_ind, strtime_ind,
844 irqtime_ind, irqtimeps_ind,
847 spin_unlock(&dasd_global_profile.lock);
849 spin_lock(&block->profile.lock);
850 if (block->profile.data)
851 dasd_profile_end_add_data(block->profile.data,
852 cqr->startdev != block->base,
854 rq_data_dir(req) == READ,
855 sectors, sectors_ind, tottime_ind,
856 tottimeps_ind, strtime_ind,
857 irqtime_ind, irqtimeps_ind,
859 spin_unlock(&block->profile.lock);
861 spin_lock(&device->profile.lock);
862 if (device->profile.data)
863 dasd_profile_end_add_data(device->profile.data,
864 cqr->startdev != block->base,
866 rq_data_dir(req) == READ,
867 sectors, sectors_ind, tottime_ind,
868 tottimeps_ind, strtime_ind,
869 irqtime_ind, irqtimeps_ind,
871 spin_unlock(&device->profile.lock);
874 void dasd_profile_reset(struct dasd_profile *profile)
876 struct dasd_profile_info *data;
878 spin_lock_bh(&profile->lock);
879 data = profile->data;
881 spin_unlock_bh(&profile->lock);
884 memset(data, 0, sizeof(*data));
885 getnstimeofday(&data->starttod);
886 spin_unlock_bh(&profile->lock);
889 int dasd_profile_on(struct dasd_profile *profile)
891 struct dasd_profile_info *data;
893 data = kzalloc(sizeof(*data), GFP_KERNEL);
896 spin_lock_bh(&profile->lock);
898 spin_unlock_bh(&profile->lock);
902 getnstimeofday(&data->starttod);
903 profile->data = data;
904 spin_unlock_bh(&profile->lock);
908 void dasd_profile_off(struct dasd_profile *profile)
910 spin_lock_bh(&profile->lock);
911 kfree(profile->data);
912 profile->data = NULL;
913 spin_unlock_bh(&profile->lock);
916 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
920 buffer = vmalloc(user_len + 1);
922 return ERR_PTR(-ENOMEM);
923 if (copy_from_user(buffer, user_buf, user_len) != 0) {
925 return ERR_PTR(-EFAULT);
927 /* got the string, now strip linefeed. */
928 if (buffer[user_len - 1] == '\n')
929 buffer[user_len - 1] = 0;
931 buffer[user_len] = 0;
935 static ssize_t dasd_stats_write(struct file *file,
936 const char __user *user_buf,
937 size_t user_len, loff_t *pos)
941 struct seq_file *m = (struct seq_file *)file->private_data;
942 struct dasd_profile *prof = m->private;
944 if (user_len > 65536)
946 buffer = dasd_get_user_string(user_buf, user_len);
948 return PTR_ERR(buffer);
950 str = skip_spaces(buffer);
952 if (strncmp(str, "reset", 5) == 0) {
953 dasd_profile_reset(prof);
954 } else if (strncmp(str, "on", 2) == 0) {
955 rc = dasd_profile_on(prof);
959 if (prof == &dasd_global_profile) {
960 dasd_profile_reset(prof);
961 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
963 } else if (strncmp(str, "off", 3) == 0) {
964 if (prof == &dasd_global_profile)
965 dasd_global_profile_level = DASD_PROFILE_OFF;
966 dasd_profile_off(prof);
974 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
978 for (i = 0; i < 32; i++)
979 seq_printf(m, "%u ", array[i]);
983 static void dasd_stats_seq_print(struct seq_file *m,
984 struct dasd_profile_info *data)
986 seq_printf(m, "start_time %ld.%09ld\n",
987 data->starttod.tv_sec, data->starttod.tv_nsec);
988 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
989 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
990 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
991 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
992 seq_puts(m, "histogram_sectors ");
993 dasd_stats_array(m, data->dasd_io_secs);
994 seq_puts(m, "histogram_io_times ");
995 dasd_stats_array(m, data->dasd_io_times);
996 seq_puts(m, "histogram_io_times_weighted ");
997 dasd_stats_array(m, data->dasd_io_timps);
998 seq_puts(m, "histogram_time_build_to_ssch ");
999 dasd_stats_array(m, data->dasd_io_time1);
1000 seq_puts(m, "histogram_time_ssch_to_irq ");
1001 dasd_stats_array(m, data->dasd_io_time2);
1002 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
1003 dasd_stats_array(m, data->dasd_io_time2ps);
1004 seq_puts(m, "histogram_time_irq_to_end ");
1005 dasd_stats_array(m, data->dasd_io_time3);
1006 seq_puts(m, "histogram_ccw_queue_length ");
1007 dasd_stats_array(m, data->dasd_io_nr_req);
1008 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1009 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1010 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1011 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
1012 seq_puts(m, "histogram_read_sectors ");
1013 dasd_stats_array(m, data->dasd_read_secs);
1014 seq_puts(m, "histogram_read_times ");
1015 dasd_stats_array(m, data->dasd_read_times);
1016 seq_puts(m, "histogram_read_time_build_to_ssch ");
1017 dasd_stats_array(m, data->dasd_read_time1);
1018 seq_puts(m, "histogram_read_time_ssch_to_irq ");
1019 dasd_stats_array(m, data->dasd_read_time2);
1020 seq_puts(m, "histogram_read_time_irq_to_end ");
1021 dasd_stats_array(m, data->dasd_read_time3);
1022 seq_puts(m, "histogram_read_ccw_queue_length ");
1023 dasd_stats_array(m, data->dasd_read_nr_req);
1026 static int dasd_stats_show(struct seq_file *m, void *v)
1028 struct dasd_profile *profile;
1029 struct dasd_profile_info *data;
1031 profile = m->private;
1032 spin_lock_bh(&profile->lock);
1033 data = profile->data;
1035 spin_unlock_bh(&profile->lock);
1036 seq_puts(m, "disabled\n");
1039 dasd_stats_seq_print(m, data);
1040 spin_unlock_bh(&profile->lock);
1044 static int dasd_stats_open(struct inode *inode, struct file *file)
1046 struct dasd_profile *profile = inode->i_private;
1047 return single_open(file, dasd_stats_show, profile);
1050 static const struct file_operations dasd_stats_raw_fops = {
1051 .owner = THIS_MODULE,
1052 .open = dasd_stats_open,
1054 .llseek = seq_lseek,
1055 .release = single_release,
1056 .write = dasd_stats_write,
1059 static void dasd_profile_init(struct dasd_profile *profile,
1060 struct dentry *base_dentry)
1067 profile->dentry = NULL;
1068 profile->data = NULL;
1069 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1070 pde = debugfs_create_file("statistics", mode, base_dentry,
1071 profile, &dasd_stats_raw_fops);
1072 if (pde && !IS_ERR(pde))
1073 profile->dentry = pde;
1077 static void dasd_profile_exit(struct dasd_profile *profile)
1079 dasd_profile_off(profile);
1080 debugfs_remove(profile->dentry);
1081 profile->dentry = NULL;
1084 static void dasd_statistics_removeroot(void)
1086 dasd_global_profile_level = DASD_PROFILE_OFF;
1087 dasd_profile_exit(&dasd_global_profile);
1088 debugfs_remove(dasd_debugfs_global_entry);
1089 debugfs_remove(dasd_debugfs_root_entry);
1092 static void dasd_statistics_createroot(void)
1096 dasd_debugfs_root_entry = NULL;
1097 pde = debugfs_create_dir("dasd", NULL);
1098 if (!pde || IS_ERR(pde))
1100 dasd_debugfs_root_entry = pde;
1101 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1102 if (!pde || IS_ERR(pde))
1104 dasd_debugfs_global_entry = pde;
1105 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
1109 DBF_EVENT(DBF_ERR, "%s",
1110 "Creation of the dasd debugfs interface failed");
1111 dasd_statistics_removeroot();
1116 #define dasd_profile_start(block, cqr, req) do {} while (0)
1117 #define dasd_profile_end(block, cqr, req) do {} while (0)
1119 static void dasd_statistics_createroot(void)
1124 static void dasd_statistics_removeroot(void)
1129 int dasd_stats_generic_show(struct seq_file *m, void *v)
1131 seq_puts(m, "Statistics are not activated in this kernel\n");
1135 static void dasd_profile_init(struct dasd_profile *profile,
1136 struct dentry *base_dentry)
1141 static void dasd_profile_exit(struct dasd_profile *profile)
1146 int dasd_profile_on(struct dasd_profile *profile)
1151 #endif /* CONFIG_DASD_PROFILE */
1154 * Allocate memory for a channel program with 'cplength' channel
1155 * command words and 'datasize' additional space. There are two
1156 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1157 * memory and 2) dasd_smalloc_request uses the static ccw memory
1158 * that gets allocated for each device.
1160 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
1162 struct dasd_device *device)
1164 struct dasd_ccw_req *cqr;
1167 BUG_ON(datasize > PAGE_SIZE ||
1168 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1170 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1172 return ERR_PTR(-ENOMEM);
1175 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1176 GFP_ATOMIC | GFP_DMA);
1177 if (cqr->cpaddr == NULL) {
1179 return ERR_PTR(-ENOMEM);
1184 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1185 if (cqr->data == NULL) {
1188 return ERR_PTR(-ENOMEM);
1192 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1193 dasd_get_device(device);
1196 EXPORT_SYMBOL(dasd_kmalloc_request);
1198 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
1200 struct dasd_device *device)
1202 unsigned long flags;
1203 struct dasd_ccw_req *cqr;
1207 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1209 size += cplength * sizeof(struct ccw1);
1212 spin_lock_irqsave(&device->mem_lock, flags);
1213 cqr = (struct dasd_ccw_req *)
1214 dasd_alloc_chunk(&device->ccw_chunks, size);
1215 spin_unlock_irqrestore(&device->mem_lock, flags);
1217 return ERR_PTR(-ENOMEM);
1218 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1219 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1222 cqr->cpaddr = (struct ccw1 *) data;
1223 data += cplength*sizeof(struct ccw1);
1224 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1229 memset(cqr->data, 0, datasize);
1232 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1233 dasd_get_device(device);
1236 EXPORT_SYMBOL(dasd_smalloc_request);
1239 * Free memory of a channel program. This function needs to free all the
1240 * idal lists that might have been created by dasd_set_cda and the
1241 * struct dasd_ccw_req itself.
1243 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1247 /* Clear any idals used for the request. */
1250 clear_normalized_cda(ccw);
1251 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1255 dasd_put_device(device);
1257 EXPORT_SYMBOL(dasd_kfree_request);
1259 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1261 unsigned long flags;
1263 spin_lock_irqsave(&device->mem_lock, flags);
1264 dasd_free_chunk(&device->ccw_chunks, cqr);
1265 spin_unlock_irqrestore(&device->mem_lock, flags);
1266 dasd_put_device(device);
1268 EXPORT_SYMBOL(dasd_sfree_request);
1271 * Check discipline magic in cqr.
1273 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1275 struct dasd_device *device;
1279 device = cqr->startdev;
1280 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1281 DBF_DEV_EVENT(DBF_WARNING, device,
1282 " dasd_ccw_req 0x%08x magic doesn't match"
1283 " discipline 0x%08x",
1285 *(unsigned int *) device->discipline->name);
1292 * Terminate the current i/o and set the request to clear_pending.
1293 * Timer keeps device runnig.
1294 * ccw_device_clear can fail if the i/o subsystem
1297 int dasd_term_IO(struct dasd_ccw_req *cqr)
1299 struct dasd_device *device;
1301 char errorstring[ERRORLENGTH];
1304 rc = dasd_check_cqr(cqr);
1308 device = (struct dasd_device *) cqr->startdev;
1309 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1310 rc = ccw_device_clear(device->cdev, (long) cqr);
1312 case 0: /* termination successful */
1313 cqr->status = DASD_CQR_CLEAR_PENDING;
1314 cqr->stopclk = get_tod_clock();
1316 DBF_DEV_EVENT(DBF_DEBUG, device,
1317 "terminate cqr %p successful",
1321 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1322 "device gone, retry");
1325 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1326 "I/O error, retry");
1330 * device not valid so no I/O could be running
1331 * handle CQR as termination successful
1333 cqr->status = DASD_CQR_CLEARED;
1334 cqr->stopclk = get_tod_clock();
1336 /* no retries for invalid devices */
1338 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1339 "EINVAL, handle as terminated");
1340 /* fake rc to success */
1344 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1345 "device busy, retry later");
1348 /* internal error 10 - unknown rc*/
1349 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1350 dev_err(&device->cdev->dev, "An error occurred in the "
1351 "DASD device driver, reason=%s\n", errorstring);
1357 dasd_schedule_device_bh(device);
1360 EXPORT_SYMBOL(dasd_term_IO);
1363 * Start the i/o. This start_IO can fail if the channel is really busy.
1364 * In that case set up a timer to start the request later.
1366 int dasd_start_IO(struct dasd_ccw_req *cqr)
1368 struct dasd_device *device;
1370 char errorstring[ERRORLENGTH];
1373 rc = dasd_check_cqr(cqr);
1378 device = (struct dasd_device *) cqr->startdev;
1380 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1381 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1382 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1383 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1384 "because of stolen lock", cqr);
1385 cqr->status = DASD_CQR_ERROR;
1386 cqr->intrc = -EPERM;
1389 if (cqr->retries < 0) {
1390 /* internal error 14 - start_IO run out of retries */
1391 sprintf(errorstring, "14 %p", cqr);
1392 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1393 "device driver, reason=%s\n", errorstring);
1394 cqr->status = DASD_CQR_ERROR;
1397 cqr->startclk = get_tod_clock();
1398 cqr->starttime = jiffies;
1400 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1401 cqr->lpm &= device->path_data.opm;
1403 cqr->lpm = device->path_data.opm;
1405 if (cqr->cpmode == 1) {
1406 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1407 (long) cqr, cqr->lpm);
1409 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1410 (long) cqr, cqr->lpm, 0);
1414 cqr->status = DASD_CQR_IN_IO;
1417 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1418 "start_IO: device busy, retry later");
1421 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1422 "start_IO: request timeout, retry later");
1425 /* -EACCES indicates that the request used only a subset of the
1426 * available paths and all these paths are gone. If the lpm of
1427 * this request was only a subset of the opm (e.g. the ppm) then
1428 * we just do a retry with all available paths.
1429 * If we already use the full opm, something is amiss, and we
1430 * need a full path verification.
1432 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1433 DBF_DEV_EVENT(DBF_WARNING, device,
1434 "start_IO: selected paths gone (%x)",
1436 } else if (cqr->lpm != device->path_data.opm) {
1437 cqr->lpm = device->path_data.opm;
1438 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1439 "start_IO: selected paths gone,"
1440 " retry on all paths");
1442 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1443 "start_IO: all paths in opm gone,"
1444 " do path verification");
1445 dasd_generic_last_path_gone(device);
1446 device->path_data.opm = 0;
1447 device->path_data.ppm = 0;
1448 device->path_data.npm = 0;
1449 device->path_data.tbvpm =
1450 ccw_device_get_path_mask(device->cdev);
1454 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1455 "start_IO: -ENODEV device gone, retry");
1458 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1459 "start_IO: -EIO device gone, retry");
1462 /* most likely caused in power management context */
1463 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1464 "start_IO: -EINVAL device currently "
1468 /* internal error 11 - unknown rc */
1469 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1470 dev_err(&device->cdev->dev,
1471 "An error occurred in the DASD device driver, "
1472 "reason=%s\n", errorstring);
1479 EXPORT_SYMBOL(dasd_start_IO);
1482 * Timeout function for dasd devices. This is used for different purposes
1483 * 1) missing interrupt handler for normal operation
1484 * 2) delayed start of request where start_IO failed with -EBUSY
1485 * 3) timeout for missing state change interrupts
1486 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1487 * DASD_CQR_QUEUED for 2) and 3).
1489 static void dasd_device_timeout(unsigned long ptr)
1491 unsigned long flags;
1492 struct dasd_device *device;
1494 device = (struct dasd_device *) ptr;
1495 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1496 /* re-activate request queue */
1497 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1498 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1499 dasd_schedule_device_bh(device);
1503 * Setup timeout for a device in jiffies.
1505 void dasd_device_set_timer(struct dasd_device *device, int expires)
1508 del_timer(&device->timer);
1510 mod_timer(&device->timer, jiffies + expires);
1512 EXPORT_SYMBOL(dasd_device_set_timer);
1515 * Clear timeout for a device.
1517 void dasd_device_clear_timer(struct dasd_device *device)
1519 del_timer(&device->timer);
1521 EXPORT_SYMBOL(dasd_device_clear_timer);
1523 static void dasd_handle_killed_request(struct ccw_device *cdev,
1524 unsigned long intparm)
1526 struct dasd_ccw_req *cqr;
1527 struct dasd_device *device;
1531 cqr = (struct dasd_ccw_req *) intparm;
1532 if (cqr->status != DASD_CQR_IN_IO) {
1533 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1534 "invalid status in handle_killed_request: "
1535 "%02x", cqr->status);
1539 device = dasd_device_from_cdev_locked(cdev);
1540 if (IS_ERR(device)) {
1541 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1542 "unable to get device from cdev");
1546 if (!cqr->startdev ||
1547 device != cqr->startdev ||
1548 strncmp(cqr->startdev->discipline->ebcname,
1549 (char *) &cqr->magic, 4)) {
1550 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1551 "invalid device in request");
1552 dasd_put_device(device);
1556 /* Schedule request to be retried. */
1557 cqr->status = DASD_CQR_QUEUED;
1559 dasd_device_clear_timer(device);
1560 dasd_schedule_device_bh(device);
1561 dasd_put_device(device);
1564 void dasd_generic_handle_state_change(struct dasd_device *device)
1566 /* First of all start sense subsystem status request. */
1567 dasd_eer_snss(device);
1569 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1570 dasd_schedule_device_bh(device);
1572 dasd_schedule_block_bh(device->block);
1574 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1577 * Interrupt handler for "normal" ssch-io based dasd devices.
1579 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1582 struct dasd_ccw_req *cqr, *next;
1583 struct dasd_device *device;
1584 unsigned long long now;
1587 cqr = (struct dasd_ccw_req *) intparm;
1589 switch (PTR_ERR(irb)) {
1591 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1592 device = (struct dasd_device *) cqr->startdev;
1593 cqr->status = DASD_CQR_CLEARED;
1594 dasd_device_clear_timer(device);
1595 wake_up(&dasd_flush_wq);
1596 dasd_schedule_device_bh(device);
1601 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1602 "request timed out\n", __func__);
1605 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1606 "unknown error %ld\n", __func__,
1609 dasd_handle_killed_request(cdev, intparm);
1613 now = get_tod_clock();
1614 /* check for conditions that should be handled immediately */
1616 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1617 scsw_cstat(&irb->scsw) == 0)) {
1619 memcpy(&cqr->irb, irb, sizeof(*irb));
1620 device = dasd_device_from_cdev_locked(cdev);
1623 /* ignore unsolicited interrupts for DIAG discipline */
1624 if (device->discipline == dasd_diag_discipline_pointer) {
1625 dasd_put_device(device);
1628 device->discipline->dump_sense_dbf(device, irb, "int");
1629 if (device->features & DASD_FEATURE_ERPLOG)
1630 device->discipline->dump_sense(device, cqr, irb);
1631 device->discipline->check_for_device_change(device, cqr, irb);
1632 dasd_put_device(device);
1635 /* check for for attention message */
1636 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1637 device = dasd_device_from_cdev_locked(cdev);
1638 device->discipline->check_attention(device, irb->esw.esw1.lpum);
1639 dasd_put_device(device);
1645 device = (struct dasd_device *) cqr->startdev;
1647 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1648 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1649 "invalid device in request");
1653 /* Check for clear pending */
1654 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1655 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1656 cqr->status = DASD_CQR_CLEARED;
1657 dasd_device_clear_timer(device);
1658 wake_up(&dasd_flush_wq);
1659 dasd_schedule_device_bh(device);
1663 /* check status - the request might have been killed by dyn detach */
1664 if (cqr->status != DASD_CQR_IN_IO) {
1665 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1666 "status %02x", dev_name(&cdev->dev), cqr->status);
1672 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1673 scsw_cstat(&irb->scsw) == 0) {
1674 /* request was completed successfully */
1675 cqr->status = DASD_CQR_SUCCESS;
1677 /* Start first request on queue if possible -> fast_io. */
1678 if (cqr->devlist.next != &device->ccw_queue) {
1679 next = list_entry(cqr->devlist.next,
1680 struct dasd_ccw_req, devlist);
1682 } else { /* error */
1684 * If we don't want complex ERP for this request, then just
1685 * reset this and retry it in the fastpath
1687 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1689 if (cqr->lpm == device->path_data.opm)
1690 DBF_DEV_EVENT(DBF_DEBUG, device,
1691 "default ERP in fastpath "
1692 "(%i retries left)",
1694 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1695 cqr->lpm = device->path_data.opm;
1696 cqr->status = DASD_CQR_QUEUED;
1699 cqr->status = DASD_CQR_ERROR;
1701 if (next && (next->status == DASD_CQR_QUEUED) &&
1702 (!device->stopped)) {
1703 if (device->discipline->start_IO(next) == 0)
1704 expires = next->expires;
1707 dasd_device_set_timer(device, expires);
1709 dasd_device_clear_timer(device);
1710 dasd_schedule_device_bh(device);
1712 EXPORT_SYMBOL(dasd_int_handler);
1714 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1716 struct dasd_device *device;
1718 device = dasd_device_from_cdev_locked(cdev);
1722 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1723 device->state != device->target ||
1724 !device->discipline->check_for_device_change){
1725 dasd_put_device(device);
1728 if (device->discipline->dump_sense_dbf)
1729 device->discipline->dump_sense_dbf(device, irb, "uc");
1730 device->discipline->check_for_device_change(device, NULL, irb);
1731 dasd_put_device(device);
1733 return UC_TODO_RETRY;
1735 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1738 * If we have an error on a dasd_block layer request then we cancel
1739 * and return all further requests from the same dasd_block as well.
1741 static void __dasd_device_recovery(struct dasd_device *device,
1742 struct dasd_ccw_req *ref_cqr)
1744 struct list_head *l, *n;
1745 struct dasd_ccw_req *cqr;
1748 * only requeue request that came from the dasd_block layer
1750 if (!ref_cqr->block)
1753 list_for_each_safe(l, n, &device->ccw_queue) {
1754 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1755 if (cqr->status == DASD_CQR_QUEUED &&
1756 ref_cqr->block == cqr->block) {
1757 cqr->status = DASD_CQR_CLEARED;
1763 * Remove those ccw requests from the queue that need to be returned
1764 * to the upper layer.
1766 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1767 struct list_head *final_queue)
1769 struct list_head *l, *n;
1770 struct dasd_ccw_req *cqr;
1772 /* Process request with final status. */
1773 list_for_each_safe(l, n, &device->ccw_queue) {
1774 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1776 /* Skip any non-final request. */
1777 if (cqr->status == DASD_CQR_QUEUED ||
1778 cqr->status == DASD_CQR_IN_IO ||
1779 cqr->status == DASD_CQR_CLEAR_PENDING)
1781 if (cqr->status == DASD_CQR_ERROR) {
1782 __dasd_device_recovery(device, cqr);
1784 /* Rechain finished requests to final queue */
1785 list_move_tail(&cqr->devlist, final_queue);
1790 * the cqrs from the final queue are returned to the upper layer
1791 * by setting a dasd_block state and calling the callback function
1793 static void __dasd_device_process_final_queue(struct dasd_device *device,
1794 struct list_head *final_queue)
1796 struct list_head *l, *n;
1797 struct dasd_ccw_req *cqr;
1798 struct dasd_block *block;
1799 void (*callback)(struct dasd_ccw_req *, void *data);
1800 void *callback_data;
1801 char errorstring[ERRORLENGTH];
1803 list_for_each_safe(l, n, final_queue) {
1804 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1805 list_del_init(&cqr->devlist);
1807 callback = cqr->callback;
1808 callback_data = cqr->callback_data;
1810 spin_lock_bh(&block->queue_lock);
1811 switch (cqr->status) {
1812 case DASD_CQR_SUCCESS:
1813 cqr->status = DASD_CQR_DONE;
1815 case DASD_CQR_ERROR:
1816 cqr->status = DASD_CQR_NEED_ERP;
1818 case DASD_CQR_CLEARED:
1819 cqr->status = DASD_CQR_TERMINATED;
1822 /* internal error 12 - wrong cqr status*/
1823 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1824 dev_err(&device->cdev->dev,
1825 "An error occurred in the DASD device driver, "
1826 "reason=%s\n", errorstring);
1829 if (cqr->callback != NULL)
1830 (callback)(cqr, callback_data);
1832 spin_unlock_bh(&block->queue_lock);
1837 * Take a look at the first request on the ccw queue and check
1838 * if it reached its expire time. If so, terminate the IO.
1840 static void __dasd_device_check_expire(struct dasd_device *device)
1842 struct dasd_ccw_req *cqr;
1844 if (list_empty(&device->ccw_queue))
1846 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1847 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1848 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1849 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1851 * IO in safe offline processing should not
1852 * run out of retries
1856 if (device->discipline->term_IO(cqr) != 0) {
1857 /* Hmpf, try again in 5 sec */
1858 dev_err(&device->cdev->dev,
1859 "cqr %p timed out (%lus) but cannot be "
1860 "ended, retrying in 5 s\n",
1861 cqr, (cqr->expires/HZ));
1862 cqr->expires += 5*HZ;
1863 dasd_device_set_timer(device, 5*HZ);
1865 dev_err(&device->cdev->dev,
1866 "cqr %p timed out (%lus), %i retries "
1867 "remaining\n", cqr, (cqr->expires/HZ),
1874 * return 1 when device is not eligible for IO
1876 static int __dasd_device_is_unusable(struct dasd_device *device,
1877 struct dasd_ccw_req *cqr)
1879 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1881 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1882 /* dasd is being set offline. */
1885 if (device->stopped) {
1886 if (device->stopped & mask) {
1887 /* stopped and CQR will not change that. */
1890 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1891 /* CQR is not able to change device to
1895 /* CQR required to get device operational. */
1901 * Take a look at the first request on the ccw queue and check
1902 * if it needs to be started.
1904 static void __dasd_device_start_head(struct dasd_device *device)
1906 struct dasd_ccw_req *cqr;
1909 if (list_empty(&device->ccw_queue))
1911 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1912 if (cqr->status != DASD_CQR_QUEUED)
1914 /* if device is not usable return request to upper layer */
1915 if (__dasd_device_is_unusable(device, cqr)) {
1916 cqr->intrc = -EAGAIN;
1917 cqr->status = DASD_CQR_CLEARED;
1918 dasd_schedule_device_bh(device);
1922 rc = device->discipline->start_IO(cqr);
1924 dasd_device_set_timer(device, cqr->expires);
1925 else if (rc == -EACCES) {
1926 dasd_schedule_device_bh(device);
1928 /* Hmpf, try again in 1/2 sec */
1929 dasd_device_set_timer(device, 50);
1932 static void __dasd_device_check_path_events(struct dasd_device *device)
1936 if (device->path_data.tbvpm) {
1937 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1940 rc = device->discipline->verify_path(
1941 device, device->path_data.tbvpm);
1943 dasd_device_set_timer(device, 50);
1945 device->path_data.tbvpm = 0;
1950 * Go through all request on the dasd_device request queue,
1951 * terminate them on the cdev if necessary, and return them to the
1952 * submitting layer via callback.
1954 * Make sure that all 'submitting layers' still exist when
1955 * this function is called!. In other words, when 'device' is a base
1956 * device then all block layer requests must have been removed before
1957 * via dasd_flush_block_queue.
1959 int dasd_flush_device_queue(struct dasd_device *device)
1961 struct dasd_ccw_req *cqr, *n;
1963 struct list_head flush_queue;
1965 INIT_LIST_HEAD(&flush_queue);
1966 spin_lock_irq(get_ccwdev_lock(device->cdev));
1968 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1969 /* Check status and move request to flush_queue */
1970 switch (cqr->status) {
1971 case DASD_CQR_IN_IO:
1972 rc = device->discipline->term_IO(cqr);
1974 /* unable to terminate requeust */
1975 dev_err(&device->cdev->dev,
1976 "Flushing the DASD request queue "
1977 "failed for request %p\n", cqr);
1978 /* stop flush processing */
1982 case DASD_CQR_QUEUED:
1983 cqr->stopclk = get_tod_clock();
1984 cqr->status = DASD_CQR_CLEARED;
1986 default: /* no need to modify the others */
1989 list_move_tail(&cqr->devlist, &flush_queue);
1992 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1994 * After this point all requests must be in state CLEAR_PENDING,
1995 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1996 * one of the others.
1998 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1999 wait_event(dasd_flush_wq,
2000 (cqr->status != DASD_CQR_CLEAR_PENDING));
2002 * Now set each request back to TERMINATED, DONE or NEED_ERP
2003 * and call the callback function of flushed requests
2005 __dasd_device_process_final_queue(device, &flush_queue);
2008 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2011 * Acquire the device lock and process queues for the device.
2013 static void dasd_device_tasklet(struct dasd_device *device)
2015 struct list_head final_queue;
2017 atomic_set (&device->tasklet_scheduled, 0);
2018 INIT_LIST_HEAD(&final_queue);
2019 spin_lock_irq(get_ccwdev_lock(device->cdev));
2020 /* Check expire time of first request on the ccw queue. */
2021 __dasd_device_check_expire(device);
2022 /* find final requests on ccw queue */
2023 __dasd_device_process_ccw_queue(device, &final_queue);
2024 __dasd_device_check_path_events(device);
2025 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2026 /* Now call the callback function of requests with final status */
2027 __dasd_device_process_final_queue(device, &final_queue);
2028 spin_lock_irq(get_ccwdev_lock(device->cdev));
2029 /* Now check if the head of the ccw queue needs to be started. */
2030 __dasd_device_start_head(device);
2031 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2032 if (waitqueue_active(&shutdown_waitq))
2033 wake_up(&shutdown_waitq);
2034 dasd_put_device(device);
2038 * Schedules a call to dasd_tasklet over the device tasklet.
2040 void dasd_schedule_device_bh(struct dasd_device *device)
2042 /* Protect against rescheduling. */
2043 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2045 dasd_get_device(device);
2046 tasklet_hi_schedule(&device->tasklet);
2048 EXPORT_SYMBOL(dasd_schedule_device_bh);
2050 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2052 device->stopped |= bits;
2054 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2056 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2058 device->stopped &= ~bits;
2059 if (!device->stopped)
2060 wake_up(&generic_waitq);
2062 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2065 * Queue a request to the head of the device ccw_queue.
2066 * Start the I/O if possible.
2068 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2070 struct dasd_device *device;
2071 unsigned long flags;
2073 device = cqr->startdev;
2074 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2075 cqr->status = DASD_CQR_QUEUED;
2076 list_add(&cqr->devlist, &device->ccw_queue);
2077 /* let the bh start the request to keep them in order */
2078 dasd_schedule_device_bh(device);
2079 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2081 EXPORT_SYMBOL(dasd_add_request_head);
2084 * Queue a request to the tail of the device ccw_queue.
2085 * Start the I/O if possible.
2087 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2089 struct dasd_device *device;
2090 unsigned long flags;
2092 device = cqr->startdev;
2093 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2094 cqr->status = DASD_CQR_QUEUED;
2095 list_add_tail(&cqr->devlist, &device->ccw_queue);
2096 /* let the bh start the request to keep them in order */
2097 dasd_schedule_device_bh(device);
2098 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2100 EXPORT_SYMBOL(dasd_add_request_tail);
2103 * Wakeup helper for the 'sleep_on' functions.
2105 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2107 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2108 cqr->callback_data = DASD_SLEEPON_END_TAG;
2109 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2110 wake_up(&generic_waitq);
2112 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2114 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2116 struct dasd_device *device;
2119 device = cqr->startdev;
2120 spin_lock_irq(get_ccwdev_lock(device->cdev));
2121 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2122 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2127 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2129 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2131 struct dasd_device *device;
2132 dasd_erp_fn_t erp_fn;
2134 if (cqr->status == DASD_CQR_FILLED)
2136 device = cqr->startdev;
2137 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2138 if (cqr->status == DASD_CQR_TERMINATED) {
2139 device->discipline->handle_terminated_request(cqr);
2142 if (cqr->status == DASD_CQR_NEED_ERP) {
2143 erp_fn = device->discipline->erp_action(cqr);
2147 if (cqr->status == DASD_CQR_FAILED)
2148 dasd_log_sense(cqr, &cqr->irb);
2150 __dasd_process_erp(device, cqr);
2157 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2159 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2160 if (cqr->refers) /* erp is not done yet */
2162 return ((cqr->status != DASD_CQR_DONE) &&
2163 (cqr->status != DASD_CQR_FAILED));
2165 return (cqr->status == DASD_CQR_FILLED);
2168 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2170 struct dasd_device *device;
2172 struct list_head ccw_queue;
2173 struct dasd_ccw_req *cqr;
2175 INIT_LIST_HEAD(&ccw_queue);
2176 maincqr->status = DASD_CQR_FILLED;
2177 device = maincqr->startdev;
2178 list_add(&maincqr->blocklist, &ccw_queue);
2179 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2180 cqr = list_first_entry(&ccw_queue,
2181 struct dasd_ccw_req, blocklist)) {
2183 if (__dasd_sleep_on_erp(cqr))
2185 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2187 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2188 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2189 cqr->status = DASD_CQR_FAILED;
2190 cqr->intrc = -EPERM;
2193 /* Non-temporary stop condition will trigger fail fast */
2194 if (device->stopped & ~DASD_STOPPED_PENDING &&
2195 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2196 (!dasd_eer_enabled(device))) {
2197 cqr->status = DASD_CQR_FAILED;
2198 cqr->intrc = -ENOLINK;
2202 * Don't try to start requests if device is stopped
2203 * except path verification requests
2205 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2206 if (interruptible) {
2207 rc = wait_event_interruptible(
2208 generic_waitq, !(device->stopped));
2209 if (rc == -ERESTARTSYS) {
2210 cqr->status = DASD_CQR_FAILED;
2211 maincqr->intrc = rc;
2215 wait_event(generic_waitq, !(device->stopped));
2218 cqr->callback = dasd_wakeup_cb;
2220 cqr->callback_data = DASD_SLEEPON_START_TAG;
2221 dasd_add_request_tail(cqr);
2222 if (interruptible) {
2223 rc = wait_event_interruptible(
2224 generic_waitq, _wait_for_wakeup(cqr));
2225 if (rc == -ERESTARTSYS) {
2226 dasd_cancel_req(cqr);
2227 /* wait (non-interruptible) for final status */
2228 wait_event(generic_waitq,
2229 _wait_for_wakeup(cqr));
2230 cqr->status = DASD_CQR_FAILED;
2231 maincqr->intrc = rc;
2235 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2238 maincqr->endclk = get_tod_clock();
2239 if ((maincqr->status != DASD_CQR_DONE) &&
2240 (maincqr->intrc != -ERESTARTSYS))
2241 dasd_log_sense(maincqr, &maincqr->irb);
2242 if (maincqr->status == DASD_CQR_DONE)
2244 else if (maincqr->intrc)
2245 rc = maincqr->intrc;
2251 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2253 struct dasd_ccw_req *cqr;
2255 list_for_each_entry(cqr, ccw_queue, blocklist) {
2256 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2263 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2265 struct dasd_device *device;
2266 struct dasd_ccw_req *cqr, *n;
2270 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2271 device = cqr->startdev;
2272 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2275 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2276 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2277 cqr->status = DASD_CQR_FAILED;
2278 cqr->intrc = -EPERM;
2281 /*Non-temporary stop condition will trigger fail fast*/
2282 if (device->stopped & ~DASD_STOPPED_PENDING &&
2283 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2284 !dasd_eer_enabled(device)) {
2285 cqr->status = DASD_CQR_FAILED;
2286 cqr->intrc = -EAGAIN;
2290 /*Don't try to start requests if device is stopped*/
2291 if (interruptible) {
2292 rc = wait_event_interruptible(
2293 generic_waitq, !device->stopped);
2294 if (rc == -ERESTARTSYS) {
2295 cqr->status = DASD_CQR_FAILED;
2300 wait_event(generic_waitq, !(device->stopped));
2303 cqr->callback = dasd_wakeup_cb;
2304 cqr->callback_data = DASD_SLEEPON_START_TAG;
2305 dasd_add_request_tail(cqr);
2308 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2311 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2313 * for alias devices simplify error recovery and
2314 * return to upper layer
2315 * do not skip ERP requests
2317 if (cqr->startdev != cqr->basedev && !cqr->refers &&
2318 (cqr->status == DASD_CQR_TERMINATED ||
2319 cqr->status == DASD_CQR_NEED_ERP))
2322 /* normal recovery for basedev IO */
2323 if (__dasd_sleep_on_erp(cqr))
2324 /* handle erp first */
2332 * Queue a request to the tail of the device ccw_queue and wait for
2335 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2337 return _dasd_sleep_on(cqr, 0);
2339 EXPORT_SYMBOL(dasd_sleep_on);
2342 * Start requests from a ccw_queue and wait for their completion.
2344 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2346 return _dasd_sleep_on_queue(ccw_queue, 0);
2348 EXPORT_SYMBOL(dasd_sleep_on_queue);
2351 * Queue a request to the tail of the device ccw_queue and wait
2352 * interruptible for it's completion.
2354 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2356 return _dasd_sleep_on(cqr, 1);
2358 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2361 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2362 * for eckd devices) the currently running request has to be terminated
2363 * and be put back to status queued, before the special request is added
2364 * to the head of the queue. Then the special request is waited on normally.
2366 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2368 struct dasd_ccw_req *cqr;
2371 if (list_empty(&device->ccw_queue))
2373 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2374 rc = device->discipline->term_IO(cqr);
2377 * CQR terminated because a more important request is pending.
2378 * Undo decreasing of retry counter because this is
2379 * not an error case.
2385 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2387 struct dasd_device *device;
2390 device = cqr->startdev;
2391 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2392 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2393 cqr->status = DASD_CQR_FAILED;
2394 cqr->intrc = -EPERM;
2397 spin_lock_irq(get_ccwdev_lock(device->cdev));
2398 rc = _dasd_term_running_cqr(device);
2400 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2403 cqr->callback = dasd_wakeup_cb;
2404 cqr->callback_data = DASD_SLEEPON_START_TAG;
2405 cqr->status = DASD_CQR_QUEUED;
2407 * add new request as second
2408 * first the terminated cqr needs to be finished
2410 list_add(&cqr->devlist, device->ccw_queue.next);
2412 /* let the bh start the request to keep them in order */
2413 dasd_schedule_device_bh(device);
2415 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2417 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2419 if (cqr->status == DASD_CQR_DONE)
2421 else if (cqr->intrc)
2427 dasd_schedule_device_bh(device);
2429 dasd_schedule_block_bh(device->block);
2433 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2436 * Cancels a request that was started with dasd_sleep_on_req.
2437 * This is useful to timeout requests. The request will be
2438 * terminated if it is currently in i/o.
2439 * Returns 0 if request termination was successful
2440 * negative error code if termination failed
2441 * Cancellation of a request is an asynchronous operation! The calling
2442 * function has to wait until the request is properly returned via callback.
2444 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2446 struct dasd_device *device = cqr->startdev;
2447 unsigned long flags;
2451 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2452 switch (cqr->status) {
2453 case DASD_CQR_QUEUED:
2454 /* request was not started - just set to cleared */
2455 cqr->status = DASD_CQR_CLEARED;
2456 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2457 cqr->callback_data = DASD_SLEEPON_END_TAG;
2459 case DASD_CQR_IN_IO:
2460 /* request in IO - terminate IO and release again */
2461 rc = device->discipline->term_IO(cqr);
2463 dev_err(&device->cdev->dev,
2464 "Cancelling request %p failed with rc=%d\n",
2467 cqr->stopclk = get_tod_clock();
2470 default: /* already finished or clear pending - do nothing */
2473 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2474 dasd_schedule_device_bh(device);
2477 EXPORT_SYMBOL(dasd_cancel_req);
2480 * SECTION: Operations of the dasd_block layer.
2484 * Timeout function for dasd_block. This is used when the block layer
2485 * is waiting for something that may not come reliably, (e.g. a state
2488 static void dasd_block_timeout(unsigned long ptr)
2490 unsigned long flags;
2491 struct dasd_block *block;
2493 block = (struct dasd_block *) ptr;
2494 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2495 /* re-activate request queue */
2496 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2497 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2498 dasd_schedule_block_bh(block);
2502 * Setup timeout for a dasd_block in jiffies.
2504 void dasd_block_set_timer(struct dasd_block *block, int expires)
2507 del_timer(&block->timer);
2509 mod_timer(&block->timer, jiffies + expires);
2511 EXPORT_SYMBOL(dasd_block_set_timer);
2514 * Clear timeout for a dasd_block.
2516 void dasd_block_clear_timer(struct dasd_block *block)
2518 del_timer(&block->timer);
2520 EXPORT_SYMBOL(dasd_block_clear_timer);
2523 * Process finished error recovery ccw.
2525 static void __dasd_process_erp(struct dasd_device *device,
2526 struct dasd_ccw_req *cqr)
2528 dasd_erp_fn_t erp_fn;
2530 if (cqr->status == DASD_CQR_DONE)
2531 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2533 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2534 erp_fn = device->discipline->erp_postaction(cqr);
2539 * Fetch requests from the block device queue.
2541 static void __dasd_process_request_queue(struct dasd_block *block)
2543 struct request_queue *queue;
2544 struct request *req;
2545 struct dasd_ccw_req *cqr;
2546 struct dasd_device *basedev;
2547 unsigned long flags;
2548 queue = block->request_queue;
2549 basedev = block->base;
2550 /* No queue ? Then there is nothing to do. */
2555 * We requeue request from the block device queue to the ccw
2556 * queue only in two states. In state DASD_STATE_READY the
2557 * partition detection is done and we need to requeue requests
2558 * for that. State DASD_STATE_ONLINE is normal block device
2561 if (basedev->state < DASD_STATE_READY) {
2562 while ((req = blk_fetch_request(block->request_queue)))
2563 __blk_end_request_all(req, -EIO);
2567 /* if device ist stopped do not fetch new requests */
2568 if (basedev->stopped)
2571 /* Now we try to fetch requests from the request queue */
2572 while ((req = blk_peek_request(queue))) {
2573 if (basedev->features & DASD_FEATURE_READONLY &&
2574 rq_data_dir(req) == WRITE) {
2575 DBF_DEV_EVENT(DBF_ERR, basedev,
2576 "Rejecting write request %p",
2578 blk_start_request(req);
2579 __blk_end_request_all(req, -EIO);
2582 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2583 (basedev->features & DASD_FEATURE_FAILFAST ||
2584 blk_noretry_request(req))) {
2585 DBF_DEV_EVENT(DBF_ERR, basedev,
2586 "Rejecting failfast request %p",
2588 blk_start_request(req);
2589 __blk_end_request_all(req, -ETIMEDOUT);
2592 cqr = basedev->discipline->build_cp(basedev, block, req);
2594 if (PTR_ERR(cqr) == -EBUSY)
2595 break; /* normal end condition */
2596 if (PTR_ERR(cqr) == -ENOMEM)
2597 break; /* terminate request queue loop */
2598 if (PTR_ERR(cqr) == -EAGAIN) {
2600 * The current request cannot be build right
2601 * now, we have to try later. If this request
2602 * is the head-of-queue we stop the device
2605 if (!list_empty(&block->ccw_queue))
2608 get_ccwdev_lock(basedev->cdev), flags);
2609 dasd_device_set_stop_bits(basedev,
2610 DASD_STOPPED_PENDING);
2611 spin_unlock_irqrestore(
2612 get_ccwdev_lock(basedev->cdev), flags);
2613 dasd_block_set_timer(block, HZ/2);
2616 DBF_DEV_EVENT(DBF_ERR, basedev,
2617 "CCW creation failed (rc=%ld) "
2620 blk_start_request(req);
2621 __blk_end_request_all(req, -EIO);
2625 * Note: callback is set to dasd_return_cqr_cb in
2626 * __dasd_block_start_head to cover erp requests as well
2628 cqr->callback_data = (void *) req;
2629 cqr->status = DASD_CQR_FILLED;
2630 req->completion_data = cqr;
2631 blk_start_request(req);
2632 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2633 INIT_LIST_HEAD(&cqr->devlist);
2634 dasd_profile_start(block, cqr, req);
2638 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2640 struct request *req;
2644 req = (struct request *) cqr->callback_data;
2645 dasd_profile_end(cqr->block, cqr, req);
2646 status = cqr->block->base->discipline->free_cp(cqr, req);
2649 else if (status == 0) {
2650 if (cqr->intrc == -EPERM)
2652 else if (cqr->intrc == -ENOLINK ||
2653 cqr->intrc == -ETIMEDOUT)
2658 __blk_end_request_all(req, error);
2662 * Process ccw request queue.
2664 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2665 struct list_head *final_queue)
2667 struct list_head *l, *n;
2668 struct dasd_ccw_req *cqr;
2669 dasd_erp_fn_t erp_fn;
2670 unsigned long flags;
2671 struct dasd_device *base = block->base;
2674 /* Process request with final status. */
2675 list_for_each_safe(l, n, &block->ccw_queue) {
2676 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2677 if (cqr->status != DASD_CQR_DONE &&
2678 cqr->status != DASD_CQR_FAILED &&
2679 cqr->status != DASD_CQR_NEED_ERP &&
2680 cqr->status != DASD_CQR_TERMINATED)
2683 if (cqr->status == DASD_CQR_TERMINATED) {
2684 base->discipline->handle_terminated_request(cqr);
2688 /* Process requests that may be recovered */
2689 if (cqr->status == DASD_CQR_NEED_ERP) {
2690 erp_fn = base->discipline->erp_action(cqr);
2691 if (IS_ERR(erp_fn(cqr)))
2696 /* log sense for fatal error */
2697 if (cqr->status == DASD_CQR_FAILED) {
2698 dasd_log_sense(cqr, &cqr->irb);
2701 /* First of all call extended error reporting. */
2702 if (dasd_eer_enabled(base) &&
2703 cqr->status == DASD_CQR_FAILED) {
2704 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2706 /* restart request */
2707 cqr->status = DASD_CQR_FILLED;
2709 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2710 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2711 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2716 /* Process finished ERP request. */
2718 __dasd_process_erp(base, cqr);
2722 /* Rechain finished requests to final queue */
2723 cqr->endclk = get_tod_clock();
2724 list_move_tail(&cqr->blocklist, final_queue);
2728 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2730 dasd_schedule_block_bh(cqr->block);
2733 static void __dasd_block_start_head(struct dasd_block *block)
2735 struct dasd_ccw_req *cqr;
2737 if (list_empty(&block->ccw_queue))
2739 /* We allways begin with the first requests on the queue, as some
2740 * of previously started requests have to be enqueued on a
2741 * dasd_device again for error recovery.
2743 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2744 if (cqr->status != DASD_CQR_FILLED)
2746 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2747 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2748 cqr->status = DASD_CQR_FAILED;
2749 cqr->intrc = -EPERM;
2750 dasd_schedule_block_bh(block);
2753 /* Non-temporary stop condition will trigger fail fast */
2754 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2755 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2756 (!dasd_eer_enabled(block->base))) {
2757 cqr->status = DASD_CQR_FAILED;
2758 cqr->intrc = -ENOLINK;
2759 dasd_schedule_block_bh(block);
2762 /* Don't try to start requests if device is stopped */
2763 if (block->base->stopped)
2766 /* just a fail safe check, should not happen */
2768 cqr->startdev = block->base;
2770 /* make sure that the requests we submit find their way back */
2771 cqr->callback = dasd_return_cqr_cb;
2773 dasd_add_request_tail(cqr);
2778 * Central dasd_block layer routine. Takes requests from the generic
2779 * block layer request queue, creates ccw requests, enqueues them on
2780 * a dasd_device and processes ccw requests that have been returned.
2782 static void dasd_block_tasklet(struct dasd_block *block)
2784 struct list_head final_queue;
2785 struct list_head *l, *n;
2786 struct dasd_ccw_req *cqr;
2788 atomic_set(&block->tasklet_scheduled, 0);
2789 INIT_LIST_HEAD(&final_queue);
2790 spin_lock(&block->queue_lock);
2791 /* Finish off requests on ccw queue */
2792 __dasd_process_block_ccw_queue(block, &final_queue);
2793 spin_unlock(&block->queue_lock);
2794 /* Now call the callback function of requests with final status */
2795 spin_lock_irq(&block->request_queue_lock);
2796 list_for_each_safe(l, n, &final_queue) {
2797 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2798 list_del_init(&cqr->blocklist);
2799 __dasd_cleanup_cqr(cqr);
2801 spin_lock(&block->queue_lock);
2802 /* Get new request from the block device request queue */
2803 __dasd_process_request_queue(block);
2804 /* Now check if the head of the ccw queue needs to be started. */
2805 __dasd_block_start_head(block);
2806 spin_unlock(&block->queue_lock);
2807 spin_unlock_irq(&block->request_queue_lock);
2808 if (waitqueue_active(&shutdown_waitq))
2809 wake_up(&shutdown_waitq);
2810 dasd_put_device(block->base);
2813 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2815 wake_up(&dasd_flush_wq);
2819 * Requeue a request back to the block request queue
2820 * only works for block requests
2822 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2824 struct dasd_block *block = cqr->block;
2825 struct request *req;
2826 unsigned long flags;
2830 spin_lock_irqsave(&block->queue_lock, flags);
2831 req = (struct request *) cqr->callback_data;
2832 blk_requeue_request(block->request_queue, req);
2833 spin_unlock_irqrestore(&block->queue_lock, flags);
2839 * Go through all request on the dasd_block request queue, cancel them
2840 * on the respective dasd_device, and return them to the generic
2843 static int dasd_flush_block_queue(struct dasd_block *block)
2845 struct dasd_ccw_req *cqr, *n;
2847 struct list_head flush_queue;
2849 INIT_LIST_HEAD(&flush_queue);
2850 spin_lock_bh(&block->queue_lock);
2853 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2854 /* if this request currently owned by a dasd_device cancel it */
2855 if (cqr->status >= DASD_CQR_QUEUED)
2856 rc = dasd_cancel_req(cqr);
2859 /* Rechain request (including erp chain) so it won't be
2860 * touched by the dasd_block_tasklet anymore.
2861 * Replace the callback so we notice when the request
2862 * is returned from the dasd_device layer.
2864 cqr->callback = _dasd_wake_block_flush_cb;
2865 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2866 list_move_tail(&cqr->blocklist, &flush_queue);
2868 /* moved more than one request - need to restart */
2871 spin_unlock_bh(&block->queue_lock);
2872 /* Now call the callback function of flushed requests */
2874 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2875 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2876 /* Process finished ERP request. */
2878 spin_lock_bh(&block->queue_lock);
2879 __dasd_process_erp(block->base, cqr);
2880 spin_unlock_bh(&block->queue_lock);
2881 /* restart list_for_xx loop since dasd_process_erp
2882 * might remove multiple elements */
2885 /* call the callback function */
2886 spin_lock_irq(&block->request_queue_lock);
2887 cqr->endclk = get_tod_clock();
2888 list_del_init(&cqr->blocklist);
2889 __dasd_cleanup_cqr(cqr);
2890 spin_unlock_irq(&block->request_queue_lock);
2896 * Schedules a call to dasd_tasklet over the device tasklet.
2898 void dasd_schedule_block_bh(struct dasd_block *block)
2900 /* Protect against rescheduling. */
2901 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2903 /* life cycle of block is bound to it's base device */
2904 dasd_get_device(block->base);
2905 tasklet_hi_schedule(&block->tasklet);
2907 EXPORT_SYMBOL(dasd_schedule_block_bh);
2911 * SECTION: external block device operations
2912 * (request queue handling, open, release, etc.)
2916 * Dasd request queue function. Called from ll_rw_blk.c
2918 static void do_dasd_request(struct request_queue *queue)
2920 struct dasd_block *block;
2922 block = queue->queuedata;
2923 spin_lock(&block->queue_lock);
2924 /* Get new request from the block device request queue */
2925 __dasd_process_request_queue(block);
2926 /* Now check if the head of the ccw queue needs to be started. */
2927 __dasd_block_start_head(block);
2928 spin_unlock(&block->queue_lock);
2932 * Block timeout callback, called from the block layer
2934 * request_queue lock is held on entry.
2937 * BLK_EH_RESET_TIMER if the request should be left running
2938 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2941 enum blk_eh_timer_return dasd_times_out(struct request *req)
2943 struct dasd_ccw_req *cqr = req->completion_data;
2944 struct dasd_block *block = req->q->queuedata;
2945 struct dasd_device *device;
2949 return BLK_EH_NOT_HANDLED;
2951 device = cqr->startdev ? cqr->startdev : block->base;
2952 if (!device->blk_timeout)
2953 return BLK_EH_RESET_TIMER;
2954 DBF_DEV_EVENT(DBF_WARNING, device,
2955 " dasd_times_out cqr %p status %x",
2958 spin_lock(&block->queue_lock);
2959 spin_lock(get_ccwdev_lock(device->cdev));
2961 cqr->intrc = -ETIMEDOUT;
2962 if (cqr->status >= DASD_CQR_QUEUED) {
2963 spin_unlock(get_ccwdev_lock(device->cdev));
2964 rc = dasd_cancel_req(cqr);
2965 } else if (cqr->status == DASD_CQR_FILLED ||
2966 cqr->status == DASD_CQR_NEED_ERP) {
2967 cqr->status = DASD_CQR_TERMINATED;
2968 spin_unlock(get_ccwdev_lock(device->cdev));
2969 } else if (cqr->status == DASD_CQR_IN_ERP) {
2970 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2972 list_for_each_entry_safe(searchcqr, nextcqr,
2973 &block->ccw_queue, blocklist) {
2975 while (tmpcqr->refers)
2976 tmpcqr = tmpcqr->refers;
2979 /* searchcqr is an ERP request for cqr */
2980 searchcqr->retries = -1;
2981 searchcqr->intrc = -ETIMEDOUT;
2982 if (searchcqr->status >= DASD_CQR_QUEUED) {
2983 spin_unlock(get_ccwdev_lock(device->cdev));
2984 rc = dasd_cancel_req(searchcqr);
2985 spin_lock(get_ccwdev_lock(device->cdev));
2986 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2987 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2988 searchcqr->status = DASD_CQR_TERMINATED;
2990 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2992 * Shouldn't happen; most recent ERP
2993 * request is at the front of queue
2999 spin_unlock(get_ccwdev_lock(device->cdev));
3001 dasd_schedule_block_bh(block);
3002 spin_unlock(&block->queue_lock);
3004 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3008 * Allocate and initialize request queue and default I/O scheduler.
3010 static int dasd_alloc_queue(struct dasd_block *block)
3012 block->request_queue = blk_init_queue(do_dasd_request,
3013 &block->request_queue_lock);
3014 if (block->request_queue == NULL)
3017 block->request_queue->queuedata = block;
3023 * Allocate and initialize request queue.
3025 static void dasd_setup_queue(struct dasd_block *block)
3029 if (block->base->features & DASD_FEATURE_USERAW) {
3031 * the max_blocks value for raw_track access is 256
3032 * it is higher than the native ECKD value because we
3033 * only need one ccw per track
3034 * so the max_hw_sectors are
3035 * 2048 x 512B = 1024kB = 16 tracks
3039 max = block->base->discipline->max_blocks << block->s2b_shift;
3041 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, block->request_queue);
3042 block->request_queue->limits.max_dev_sectors = max;
3043 blk_queue_logical_block_size(block->request_queue,
3045 blk_queue_max_hw_sectors(block->request_queue, max);
3046 blk_queue_max_segments(block->request_queue, -1L);
3047 /* with page sized segments we can translate each segement into
3050 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3051 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
3055 * Deactivate and free request queue.
3057 static void dasd_free_queue(struct dasd_block *block)
3059 if (block->request_queue) {
3060 blk_cleanup_queue(block->request_queue);
3061 block->request_queue = NULL;
3066 * Flush request on the request queue.
3068 static void dasd_flush_request_queue(struct dasd_block *block)
3070 struct request *req;
3072 if (!block->request_queue)
3075 spin_lock_irq(&block->request_queue_lock);
3076 while ((req = blk_fetch_request(block->request_queue)))
3077 __blk_end_request_all(req, -EIO);
3078 spin_unlock_irq(&block->request_queue_lock);
3081 static int dasd_open(struct block_device *bdev, fmode_t mode)
3083 struct dasd_device *base;
3086 base = dasd_device_from_gendisk(bdev->bd_disk);
3090 atomic_inc(&base->block->open_count);
3091 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3096 if (!try_module_get(base->discipline->owner)) {
3101 if (dasd_probeonly) {
3102 dev_info(&base->cdev->dev,
3103 "Accessing the DASD failed because it is in "
3104 "probeonly mode\n");
3109 if (base->state <= DASD_STATE_BASIC) {
3110 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3111 " Cannot open unrecognized device");
3116 if ((mode & FMODE_WRITE) &&
3117 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3118 (base->features & DASD_FEATURE_READONLY))) {
3123 dasd_put_device(base);
3127 module_put(base->discipline->owner);
3129 atomic_dec(&base->block->open_count);
3130 dasd_put_device(base);
3134 static void dasd_release(struct gendisk *disk, fmode_t mode)
3136 struct dasd_device *base = dasd_device_from_gendisk(disk);
3138 atomic_dec(&base->block->open_count);
3139 module_put(base->discipline->owner);
3140 dasd_put_device(base);
3145 * Return disk geometry.
3147 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3149 struct dasd_device *base;
3151 base = dasd_device_from_gendisk(bdev->bd_disk);
3155 if (!base->discipline ||
3156 !base->discipline->fill_geometry) {
3157 dasd_put_device(base);
3160 base->discipline->fill_geometry(base->block, geo);
3161 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3162 dasd_put_device(base);
3166 const struct block_device_operations
3167 dasd_device_operations = {
3168 .owner = THIS_MODULE,
3170 .release = dasd_release,
3171 .ioctl = dasd_ioctl,
3172 .compat_ioctl = dasd_ioctl,
3173 .getgeo = dasd_getgeo,
3176 /*******************************************************************************
3177 * end of block device operations
3183 #ifdef CONFIG_PROC_FS
3187 if (dasd_page_cache != NULL) {
3188 kmem_cache_destroy(dasd_page_cache);
3189 dasd_page_cache = NULL;
3191 dasd_gendisk_exit();
3193 if (dasd_debug_area != NULL) {
3194 debug_unregister(dasd_debug_area);
3195 dasd_debug_area = NULL;
3197 dasd_statistics_removeroot();
3201 * SECTION: common functions for ccw_driver use
3205 * Is the device read-only?
3206 * Note that this function does not report the setting of the
3207 * readonly device attribute, but how it is configured in z/VM.
3209 int dasd_device_is_ro(struct dasd_device *device)
3211 struct ccw_dev_id dev_id;
3212 struct diag210 diag_data;
3217 ccw_device_get_id(device->cdev, &dev_id);
3218 memset(&diag_data, 0, sizeof(diag_data));
3219 diag_data.vrdcdvno = dev_id.devno;
3220 diag_data.vrdclen = sizeof(diag_data);
3221 rc = diag210(&diag_data);
3222 if (rc == 0 || rc == 2) {
3223 return diag_data.vrdcvfla & 0x80;
3225 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3230 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3232 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3234 struct ccw_device *cdev = data;
3237 ret = ccw_device_set_online(cdev);
3239 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3240 dev_name(&cdev->dev), ret);
3244 * Initial attempt at a probe function. this can be simplified once
3245 * the other detection code is gone.
3247 int dasd_generic_probe(struct ccw_device *cdev,
3248 struct dasd_discipline *discipline)
3252 ret = dasd_add_sysfs_files(cdev);
3254 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3255 "dasd_generic_probe: could not add "
3259 cdev->handler = &dasd_int_handler;
3262 * Automatically online either all dasd devices (dasd_autodetect)
3263 * or all devices specified with dasd= parameters during
3266 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3267 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3268 async_schedule(dasd_generic_auto_online, cdev);
3271 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3274 * This will one day be called from a global not_oper handler.
3275 * It is also used by driver_unregister during module unload.
3277 void dasd_generic_remove(struct ccw_device *cdev)
3279 struct dasd_device *device;
3280 struct dasd_block *block;
3282 cdev->handler = NULL;
3284 device = dasd_device_from_cdev(cdev);
3285 if (IS_ERR(device)) {
3286 dasd_remove_sysfs_files(cdev);
3289 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3290 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3291 /* Already doing offline processing */
3292 dasd_put_device(device);
3293 dasd_remove_sysfs_files(cdev);
3297 * This device is removed unconditionally. Set offline
3298 * flag to prevent dasd_open from opening it while it is
3299 * no quite down yet.
3301 dasd_set_target_state(device, DASD_STATE_NEW);
3302 /* dasd_delete_device destroys the device reference. */
3303 block = device->block;
3304 dasd_delete_device(device);
3306 * life cycle of block is bound to device, so delete it after
3307 * device was safely removed
3310 dasd_free_block(block);
3312 dasd_remove_sysfs_files(cdev);
3314 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3317 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3318 * the device is detected for the first time and is supposed to be used
3319 * or the user has started activation through sysfs.
3321 int dasd_generic_set_online(struct ccw_device *cdev,
3322 struct dasd_discipline *base_discipline)
3324 struct dasd_discipline *discipline;
3325 struct dasd_device *device;
3328 /* first online clears initial online feature flag */
3329 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3330 device = dasd_create_device(cdev);
3332 return PTR_ERR(device);
3334 discipline = base_discipline;
3335 if (device->features & DASD_FEATURE_USEDIAG) {
3336 if (!dasd_diag_discipline_pointer) {
3337 /* Try to load the required module. */
3338 rc = request_module(DASD_DIAG_MOD);
3340 pr_warn("%s Setting the DASD online failed "
3341 "because the required module %s "
3342 "could not be loaded (rc=%d)\n",
3343 dev_name(&cdev->dev), DASD_DIAG_MOD,
3345 dasd_delete_device(device);
3349 /* Module init could have failed, so check again here after
3350 * request_module(). */
3351 if (!dasd_diag_discipline_pointer) {
3352 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3353 dev_name(&cdev->dev));
3354 dasd_delete_device(device);
3357 discipline = dasd_diag_discipline_pointer;
3359 if (!try_module_get(base_discipline->owner)) {
3360 dasd_delete_device(device);
3363 if (!try_module_get(discipline->owner)) {
3364 module_put(base_discipline->owner);
3365 dasd_delete_device(device);
3368 device->base_discipline = base_discipline;
3369 device->discipline = discipline;
3371 /* check_device will allocate block device if necessary */
3372 rc = discipline->check_device(device);
3374 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3375 dev_name(&cdev->dev), discipline->name, rc);
3376 module_put(discipline->owner);
3377 module_put(base_discipline->owner);
3378 dasd_delete_device(device);
3382 dasd_set_target_state(device, DASD_STATE_ONLINE);
3383 if (device->state <= DASD_STATE_KNOWN) {
3384 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3385 dev_name(&cdev->dev));
3387 dasd_set_target_state(device, DASD_STATE_NEW);
3389 dasd_free_block(device->block);
3390 dasd_delete_device(device);
3392 pr_debug("dasd_generic device %s found\n",
3393 dev_name(&cdev->dev));
3395 wait_event(dasd_init_waitq, _wait_for_device(device));
3397 dasd_put_device(device);
3400 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3402 int dasd_generic_set_offline(struct ccw_device *cdev)
3404 struct dasd_device *device;
3405 struct dasd_block *block;
3406 int max_count, open_count, rc;
3409 device = dasd_device_from_cdev(cdev);
3411 return PTR_ERR(device);
3414 * We must make sure that this device is currently not in use.
3415 * The open_count is increased for every opener, that includes
3416 * the blkdev_get in dasd_scan_partitions. We are only interested
3417 * in the other openers.
3419 if (device->block) {
3420 max_count = device->block->bdev ? 0 : -1;
3421 open_count = atomic_read(&device->block->open_count);
3422 if (open_count > max_count) {
3424 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3425 dev_name(&cdev->dev), open_count);
3427 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3428 dev_name(&cdev->dev));
3429 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3430 dasd_put_device(device);
3435 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3437 * safe offline already running
3438 * could only be called by normal offline so safe_offline flag
3439 * needs to be removed to run normal offline and kill all I/O
3441 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3442 /* Already doing normal offline processing */
3443 dasd_put_device(device);
3446 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3449 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3450 /* Already doing offline processing */
3451 dasd_put_device(device);
3456 * if safe_offline called set safe_offline_running flag and
3457 * clear safe_offline so that a call to normal offline
3458 * can overrun safe_offline processing
3460 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3461 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3463 * If we want to set the device safe offline all IO operations
3464 * should be finished before continuing the offline process
3465 * so sync bdev first and then wait for our queues to become
3468 /* sync blockdev and partitions */
3469 rc = fsync_bdev(device->block->bdev);
3473 /* schedule device tasklet and wait for completion */
3474 dasd_schedule_device_bh(device);
3475 rc = wait_event_interruptible(shutdown_waitq,
3476 _wait_for_empty_queues(device));
3481 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3482 dasd_set_target_state(device, DASD_STATE_NEW);
3483 /* dasd_delete_device destroys the device reference. */
3484 block = device->block;
3485 dasd_delete_device(device);
3487 * life cycle of block is bound to device, so delete it after
3488 * device was safely removed
3491 dasd_free_block(block);
3495 /* interrupted by signal */
3496 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3497 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3498 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3499 dasd_put_device(device);
3502 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3504 int dasd_generic_last_path_gone(struct dasd_device *device)
3506 struct dasd_ccw_req *cqr;
3508 dev_warn(&device->cdev->dev, "No operational channel path is left "
3509 "for the device\n");
3510 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3511 /* First of all call extended error reporting. */
3512 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3514 if (device->state < DASD_STATE_BASIC)
3516 /* Device is active. We want to keep it. */
3517 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3518 if ((cqr->status == DASD_CQR_IN_IO) ||
3519 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3520 cqr->status = DASD_CQR_QUEUED;
3523 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3524 dasd_device_clear_timer(device);
3525 dasd_schedule_device_bh(device);
3528 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3530 int dasd_generic_path_operational(struct dasd_device *device)
3532 dev_info(&device->cdev->dev, "A channel path to the device has become "
3534 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3535 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3536 if (device->stopped & DASD_UNRESUMED_PM) {
3537 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3538 dasd_restore_device(device);
3541 dasd_schedule_device_bh(device);
3543 dasd_schedule_block_bh(device->block);
3545 if (!device->stopped)
3546 wake_up(&generic_waitq);
3550 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3552 int dasd_generic_notify(struct ccw_device *cdev, int event)
3554 struct dasd_device *device;
3557 device = dasd_device_from_cdev_locked(cdev);
3565 device->path_data.opm = 0;
3566 device->path_data.ppm = 0;
3567 device->path_data.npm = 0;
3568 ret = dasd_generic_last_path_gone(device);
3572 if (device->path_data.opm)
3573 ret = dasd_generic_path_operational(device);
3576 dasd_put_device(device);
3579 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3581 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3584 __u8 oldopm, eventlpm;
3585 struct dasd_device *device;
3587 device = dasd_device_from_cdev_locked(cdev);
3590 for (chp = 0; chp < 8; chp++) {
3591 eventlpm = 0x80 >> chp;
3592 if (path_event[chp] & PE_PATH_GONE) {
3593 oldopm = device->path_data.opm;
3594 device->path_data.opm &= ~eventlpm;
3595 device->path_data.ppm &= ~eventlpm;
3596 device->path_data.npm &= ~eventlpm;
3597 if (oldopm && !device->path_data.opm) {
3598 dev_warn(&device->cdev->dev,
3599 "No verified channel paths remain "
3600 "for the device\n");
3601 DBF_DEV_EVENT(DBF_WARNING, device,
3602 "%s", "last verified path gone");
3603 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3604 dasd_device_set_stop_bits(device,
3605 DASD_STOPPED_DC_WAIT);
3608 if (path_event[chp] & PE_PATH_AVAILABLE) {
3609 device->path_data.opm &= ~eventlpm;
3610 device->path_data.ppm &= ~eventlpm;
3611 device->path_data.npm &= ~eventlpm;
3612 device->path_data.tbvpm |= eventlpm;
3613 dasd_schedule_device_bh(device);
3615 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3616 if (!(device->path_data.opm & eventlpm) &&
3617 !(device->path_data.tbvpm & eventlpm)) {
3619 * we can not establish a pathgroup on an
3620 * unavailable path, so trigger a path
3621 * verification first
3623 device->path_data.tbvpm |= eventlpm;
3624 dasd_schedule_device_bh(device);
3626 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3627 "Pathgroup re-established\n");
3628 if (device->discipline->kick_validate)
3629 device->discipline->kick_validate(device);
3632 dasd_put_device(device);
3634 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3636 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3638 if (!device->path_data.opm && lpm) {
3639 device->path_data.opm = lpm;
3640 dasd_generic_path_operational(device);
3642 device->path_data.opm |= lpm;
3645 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3648 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3650 struct dasd_device *device = dasd_device_from_cdev(cdev);
3651 struct list_head freeze_queue;
3652 struct dasd_ccw_req *cqr, *n;
3653 struct dasd_ccw_req *refers;
3657 return PTR_ERR(device);
3659 /* mark device as suspended */
3660 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3662 if (device->discipline->freeze)
3663 rc = device->discipline->freeze(device);
3665 /* disallow new I/O */
3666 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3668 /* clear active requests and requeue them to block layer if possible */
3669 INIT_LIST_HEAD(&freeze_queue);
3670 spin_lock_irq(get_ccwdev_lock(cdev));
3672 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3673 /* Check status and move request to flush_queue */
3674 if (cqr->status == DASD_CQR_IN_IO) {
3675 rc = device->discipline->term_IO(cqr);
3677 /* unable to terminate requeust */
3678 dev_err(&device->cdev->dev,
3679 "Unable to terminate request %p "
3680 "on suspend\n", cqr);
3681 spin_unlock_irq(get_ccwdev_lock(cdev));
3682 dasd_put_device(device);
3686 list_move_tail(&cqr->devlist, &freeze_queue);
3688 spin_unlock_irq(get_ccwdev_lock(cdev));
3690 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3691 wait_event(dasd_flush_wq,
3692 (cqr->status != DASD_CQR_CLEAR_PENDING));
3693 if (cqr->status == DASD_CQR_CLEARED)
3694 cqr->status = DASD_CQR_QUEUED;
3696 /* requeue requests to blocklayer will only work for
3697 block device requests */
3698 if (_dasd_requeue_request(cqr))
3701 /* remove requests from device and block queue */
3702 list_del_init(&cqr->devlist);
3703 while (cqr->refers != NULL) {
3704 refers = cqr->refers;
3705 /* remove the request from the block queue */
3706 list_del(&cqr->blocklist);
3707 /* free the finished erp request */
3708 dasd_free_erp_request(cqr, cqr->memdev);
3712 list_del_init(&cqr->blocklist);
3713 cqr->block->base->discipline->free_cp(
3714 cqr, (struct request *) cqr->callback_data);
3718 * if requests remain then they are internal request
3719 * and go back to the device queue
3721 if (!list_empty(&freeze_queue)) {
3722 /* move freeze_queue to start of the ccw_queue */
3723 spin_lock_irq(get_ccwdev_lock(cdev));
3724 list_splice_tail(&freeze_queue, &device->ccw_queue);
3725 spin_unlock_irq(get_ccwdev_lock(cdev));
3727 dasd_put_device(device);
3730 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3732 int dasd_generic_restore_device(struct ccw_device *cdev)
3734 struct dasd_device *device = dasd_device_from_cdev(cdev);
3738 return PTR_ERR(device);
3740 /* allow new IO again */
3741 dasd_device_remove_stop_bits(device,
3742 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3744 dasd_schedule_device_bh(device);
3747 * call discipline restore function
3748 * if device is stopped do nothing e.g. for disconnected devices
3750 if (device->discipline->restore && !(device->stopped))
3751 rc = device->discipline->restore(device);
3752 if (rc || device->stopped)
3754 * if the resume failed for the DASD we put it in
3755 * an UNRESUMED stop state
3757 device->stopped |= DASD_UNRESUMED_PM;
3760 dasd_schedule_block_bh(device->block);
3762 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3763 dasd_put_device(device);
3766 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3768 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3770 int rdc_buffer_size,
3773 struct dasd_ccw_req *cqr;
3775 unsigned long *idaw;
3777 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3780 /* internal error 13 - Allocating the RDC request failed*/
3781 dev_err(&device->cdev->dev,
3782 "An error occurred in the DASD device driver, "
3783 "reason=%s\n", "13");
3788 ccw->cmd_code = CCW_CMD_RDC;
3789 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3790 idaw = (unsigned long *) (cqr->data);
3791 ccw->cda = (__u32)(addr_t) idaw;
3792 ccw->flags = CCW_FLAG_IDA;
3793 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3795 ccw->cda = (__u32)(addr_t) rdc_buffer;
3799 ccw->count = rdc_buffer_size;
3800 cqr->startdev = device;
3801 cqr->memdev = device;
3802 cqr->expires = 10*HZ;
3804 cqr->buildclk = get_tod_clock();
3805 cqr->status = DASD_CQR_FILLED;
3810 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3811 void *rdc_buffer, int rdc_buffer_size)
3814 struct dasd_ccw_req *cqr;
3816 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3819 return PTR_ERR(cqr);
3821 ret = dasd_sleep_on(cqr);
3822 dasd_sfree_request(cqr, cqr->memdev);
3825 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3828 * In command mode and transport mode we need to look for sense
3829 * data in different places. The sense data itself is allways
3830 * an array of 32 bytes, so we can unify the sense data access
3833 char *dasd_get_sense(struct irb *irb)
3835 struct tsb *tsb = NULL;
3838 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3839 if (irb->scsw.tm.tcw)
3840 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3842 if (tsb && tsb->length == 64 && tsb->flags)
3843 switch (tsb->flags & 0x07) {
3844 case 1: /* tsa_iostat */
3845 sense = tsb->tsa.iostat.sense;
3847 case 2: /* tsa_ddpc */
3848 sense = tsb->tsa.ddpc.sense;
3851 /* currently we don't use interrogate data */
3854 } else if (irb->esw.esw0.erw.cons) {
3859 EXPORT_SYMBOL_GPL(dasd_get_sense);
3861 void dasd_generic_shutdown(struct ccw_device *cdev)
3863 struct dasd_device *device;
3865 device = dasd_device_from_cdev(cdev);
3870 dasd_schedule_block_bh(device->block);
3872 dasd_schedule_device_bh(device);
3874 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3876 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3878 static int __init dasd_init(void)
3882 init_waitqueue_head(&dasd_init_waitq);
3883 init_waitqueue_head(&dasd_flush_wq);
3884 init_waitqueue_head(&generic_waitq);
3885 init_waitqueue_head(&shutdown_waitq);
3887 /* register 'common' DASD debug area, used for all DBF_XXX calls */
3888 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3889 if (dasd_debug_area == NULL) {
3893 debug_register_view(dasd_debug_area, &debug_sprintf_view);
3894 debug_set_level(dasd_debug_area, DBF_WARNING);
3896 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3898 dasd_diag_discipline_pointer = NULL;
3900 dasd_statistics_createroot();
3902 rc = dasd_devmap_init();
3905 rc = dasd_gendisk_init();
3911 rc = dasd_eer_init();
3914 #ifdef CONFIG_PROC_FS
3915 rc = dasd_proc_init();
3922 pr_info("The DASD device driver could not be initialized\n");
3927 module_init(dasd_init);
3928 module_exit(dasd_exit);