1 // SPDX-License-Identifier: GPL-2.0
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
8 * Copyright IBM Corp. 1999, 2009
11 #define KMSG_COMPONENT "dasd"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kmod.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/ctype.h>
18 #include <linux/major.h>
19 #include <linux/slab.h>
20 #include <linux/hdreg.h>
21 #include <linux/async.h>
22 #include <linux/mutex.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/vmalloc.h>
27 #include <asm/ccwdev.h>
28 #include <asm/ebcdic.h>
29 #include <asm/idals.h>
34 #define PRINTK_HEADER "dasd:"
38 * SECTION: Constant definitions to be used within this file
40 #define DASD_CHANQ_MAX_SIZE 4
42 #define DASD_DIAG_MOD "dasd_diag_mod"
44 static unsigned int queue_depth = 32;
45 static unsigned int nr_hw_queues = 4;
47 module_param(queue_depth, uint, 0444);
48 MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
50 module_param(nr_hw_queues, uint, 0444);
51 MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
54 * SECTION: exported variables of dasd.c
56 debug_info_t *dasd_debug_area;
57 EXPORT_SYMBOL(dasd_debug_area);
58 static struct dentry *dasd_debugfs_root_entry;
59 struct dasd_discipline *dasd_diag_discipline_pointer;
60 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
61 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
63 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
64 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
65 " Copyright IBM Corp. 2000");
66 MODULE_SUPPORTED_DEVICE("dasd");
67 MODULE_LICENSE("GPL");
70 * SECTION: prototypes for static functions of dasd.c
72 static int dasd_alloc_queue(struct dasd_block *);
73 static void dasd_free_queue(struct dasd_block *);
74 static int dasd_flush_block_queue(struct dasd_block *);
75 static void dasd_device_tasklet(unsigned long);
76 static void dasd_block_tasklet(unsigned long);
77 static void do_kick_device(struct work_struct *);
78 static void do_restore_device(struct work_struct *);
79 static void do_reload_device(struct work_struct *);
80 static void do_requeue_requests(struct work_struct *);
81 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
82 static void dasd_device_timeout(struct timer_list *);
83 static void dasd_block_timeout(struct timer_list *);
84 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
85 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
86 static void dasd_profile_exit(struct dasd_profile *);
87 static void dasd_hosts_init(struct dentry *, struct dasd_device *);
88 static void dasd_hosts_exit(struct dasd_device *);
91 * SECTION: Operations on the device structure.
93 static wait_queue_head_t dasd_init_waitq;
94 static wait_queue_head_t dasd_flush_wq;
95 static wait_queue_head_t generic_waitq;
96 static wait_queue_head_t shutdown_waitq;
99 * Allocate memory for a new device structure.
101 struct dasd_device *dasd_alloc_device(void)
103 struct dasd_device *device;
105 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
107 return ERR_PTR(-ENOMEM);
109 /* Get two pages for normal block device operations. */
110 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
111 if (!device->ccw_mem) {
113 return ERR_PTR(-ENOMEM);
115 /* Get one page for error recovery. */
116 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
117 if (!device->erp_mem) {
118 free_pages((unsigned long) device->ccw_mem, 1);
120 return ERR_PTR(-ENOMEM);
122 /* Get two pages for ese format. */
123 device->ese_mem = (void *)__get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
124 if (!device->ese_mem) {
125 free_page((unsigned long) device->erp_mem);
126 free_pages((unsigned long) device->ccw_mem, 1);
128 return ERR_PTR(-ENOMEM);
131 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
132 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
133 dasd_init_chunklist(&device->ese_chunks, device->ese_mem, PAGE_SIZE * 2);
134 spin_lock_init(&device->mem_lock);
135 atomic_set(&device->tasklet_scheduled, 0);
136 tasklet_init(&device->tasklet, dasd_device_tasklet,
137 (unsigned long) device);
138 INIT_LIST_HEAD(&device->ccw_queue);
139 timer_setup(&device->timer, dasd_device_timeout, 0);
140 INIT_WORK(&device->kick_work, do_kick_device);
141 INIT_WORK(&device->restore_device, do_restore_device);
142 INIT_WORK(&device->reload_device, do_reload_device);
143 INIT_WORK(&device->requeue_requests, do_requeue_requests);
144 device->state = DASD_STATE_NEW;
145 device->target = DASD_STATE_NEW;
146 mutex_init(&device->state_mutex);
147 spin_lock_init(&device->profile.lock);
152 * Free memory of a device structure.
154 void dasd_free_device(struct dasd_device *device)
156 kfree(device->private);
157 free_pages((unsigned long) device->ese_mem, 1);
158 free_page((unsigned long) device->erp_mem);
159 free_pages((unsigned long) device->ccw_mem, 1);
164 * Allocate memory for a new device structure.
166 struct dasd_block *dasd_alloc_block(void)
168 struct dasd_block *block;
170 block = kzalloc(sizeof(*block), GFP_ATOMIC);
172 return ERR_PTR(-ENOMEM);
173 /* open_count = 0 means device online but not in use */
174 atomic_set(&block->open_count, -1);
176 atomic_set(&block->tasklet_scheduled, 0);
177 tasklet_init(&block->tasklet, dasd_block_tasklet,
178 (unsigned long) block);
179 INIT_LIST_HEAD(&block->ccw_queue);
180 spin_lock_init(&block->queue_lock);
181 INIT_LIST_HEAD(&block->format_list);
182 spin_lock_init(&block->format_lock);
183 timer_setup(&block->timer, dasd_block_timeout, 0);
184 spin_lock_init(&block->profile.lock);
188 EXPORT_SYMBOL_GPL(dasd_alloc_block);
191 * Free memory of a device structure.
193 void dasd_free_block(struct dasd_block *block)
197 EXPORT_SYMBOL_GPL(dasd_free_block);
200 * Make a new device known to the system.
202 static int dasd_state_new_to_known(struct dasd_device *device)
207 * As long as the device is not in state DASD_STATE_NEW we want to
208 * keep the reference count > 0.
210 dasd_get_device(device);
213 rc = dasd_alloc_queue(device->block);
215 dasd_put_device(device);
219 device->state = DASD_STATE_KNOWN;
224 * Let the system forget about a device.
226 static int dasd_state_known_to_new(struct dasd_device *device)
228 /* Disable extended error reporting for this device. */
229 dasd_eer_disable(device);
230 device->state = DASD_STATE_NEW;
233 dasd_free_queue(device->block);
235 /* Give up reference we took in dasd_state_new_to_known. */
236 dasd_put_device(device);
240 static struct dentry *dasd_debugfs_setup(const char *name,
241 struct dentry *base_dentry)
247 pde = debugfs_create_dir(name, base_dentry);
248 if (!pde || IS_ERR(pde))
254 * Request the irq line for the device.
256 static int dasd_state_known_to_basic(struct dasd_device *device)
258 struct dasd_block *block = device->block;
261 /* Allocate and register gendisk structure. */
263 rc = dasd_gendisk_alloc(block);
266 block->debugfs_dentry =
267 dasd_debugfs_setup(block->gdp->disk_name,
268 dasd_debugfs_root_entry);
269 dasd_profile_init(&block->profile, block->debugfs_dentry);
270 if (dasd_global_profile_level == DASD_PROFILE_ON)
271 dasd_profile_on(&device->block->profile);
273 device->debugfs_dentry =
274 dasd_debugfs_setup(dev_name(&device->cdev->dev),
275 dasd_debugfs_root_entry);
276 dasd_profile_init(&device->profile, device->debugfs_dentry);
277 dasd_hosts_init(device->debugfs_dentry, device);
279 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
280 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
282 debug_register_view(device->debug_area, &debug_sprintf_view);
283 debug_set_level(device->debug_area, DBF_WARNING);
284 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
286 device->state = DASD_STATE_BASIC;
292 * Release the irq line for the device. Terminate any running i/o.
294 static int dasd_state_basic_to_known(struct dasd_device *device)
298 if (device->discipline->basic_to_known) {
299 rc = device->discipline->basic_to_known(device);
305 dasd_profile_exit(&device->block->profile);
306 debugfs_remove(device->block->debugfs_dentry);
307 dasd_gendisk_free(device->block);
308 dasd_block_clear_timer(device->block);
310 rc = dasd_flush_device_queue(device);
313 dasd_device_clear_timer(device);
314 dasd_profile_exit(&device->profile);
315 dasd_hosts_exit(device);
316 debugfs_remove(device->debugfs_dentry);
317 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
318 if (device->debug_area != NULL) {
319 debug_unregister(device->debug_area);
320 device->debug_area = NULL;
322 device->state = DASD_STATE_KNOWN;
327 * Do the initial analysis. The do_analysis function may return
328 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
329 * until the discipline decides to continue the startup sequence
330 * by calling the function dasd_change_state. The eckd disciplines
331 * uses this to start a ccw that detects the format. The completion
332 * interrupt for this detection ccw uses the kernel event daemon to
333 * trigger the call to dasd_change_state. All this is done in the
334 * discipline code, see dasd_eckd.c.
335 * After the analysis ccw is done (do_analysis returned 0) the block
337 * In case the analysis returns an error, the device setup is stopped
338 * (a fake disk was already added to allow formatting).
340 static int dasd_state_basic_to_ready(struct dasd_device *device)
343 struct dasd_block *block;
344 struct gendisk *disk;
347 block = device->block;
348 /* make disk known with correct capacity */
350 if (block->base->discipline->do_analysis != NULL)
351 rc = block->base->discipline->do_analysis(block);
354 device->state = DASD_STATE_UNFMT;
355 disk = device->block->gdp;
356 kobject_uevent(&disk_to_dev(disk)->kobj,
362 if (device->discipline->setup_blk_queue)
363 device->discipline->setup_blk_queue(block);
364 set_capacity(block->gdp,
365 block->blocks << block->s2b_shift);
366 device->state = DASD_STATE_READY;
367 rc = dasd_scan_partitions(block);
369 device->state = DASD_STATE_BASIC;
373 device->state = DASD_STATE_READY;
376 if (device->discipline->basic_to_ready)
377 rc = device->discipline->basic_to_ready(device);
382 int _wait_for_empty_queues(struct dasd_device *device)
385 return list_empty(&device->ccw_queue) &&
386 list_empty(&device->block->ccw_queue);
388 return list_empty(&device->ccw_queue);
392 * Remove device from block device layer. Destroy dirty buffers.
393 * Forget format information. Check if the target level is basic
394 * and if it is create fake disk for formatting.
396 static int dasd_state_ready_to_basic(struct dasd_device *device)
400 device->state = DASD_STATE_BASIC;
402 struct dasd_block *block = device->block;
403 rc = dasd_flush_block_queue(block);
405 device->state = DASD_STATE_READY;
408 dasd_destroy_partitions(block);
411 block->s2b_shift = 0;
419 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
421 device->state = DASD_STATE_BASIC;
426 * Make the device online and schedule the bottom half to start
427 * the requeueing of requests from the linux request queue to the
431 dasd_state_ready_to_online(struct dasd_device * device)
433 struct gendisk *disk;
434 struct disk_part_iter piter;
435 struct hd_struct *part;
437 device->state = DASD_STATE_ONLINE;
439 dasd_schedule_block_bh(device->block);
440 if ((device->features & DASD_FEATURE_USERAW)) {
441 disk = device->block->gdp;
442 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
445 disk = device->block->bdev->bd_disk;
446 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
447 while ((part = disk_part_iter_next(&piter)))
448 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
449 disk_part_iter_exit(&piter);
455 * Stop the requeueing of requests again.
457 static int dasd_state_online_to_ready(struct dasd_device *device)
460 struct gendisk *disk;
461 struct disk_part_iter piter;
462 struct hd_struct *part;
464 if (device->discipline->online_to_ready) {
465 rc = device->discipline->online_to_ready(device);
470 device->state = DASD_STATE_READY;
471 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
472 disk = device->block->bdev->bd_disk;
473 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
474 while ((part = disk_part_iter_next(&piter)))
475 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
476 disk_part_iter_exit(&piter);
482 * Device startup state changes.
484 static int dasd_increase_state(struct dasd_device *device)
489 if (device->state == DASD_STATE_NEW &&
490 device->target >= DASD_STATE_KNOWN)
491 rc = dasd_state_new_to_known(device);
494 device->state == DASD_STATE_KNOWN &&
495 device->target >= DASD_STATE_BASIC)
496 rc = dasd_state_known_to_basic(device);
499 device->state == DASD_STATE_BASIC &&
500 device->target >= DASD_STATE_READY)
501 rc = dasd_state_basic_to_ready(device);
504 device->state == DASD_STATE_UNFMT &&
505 device->target > DASD_STATE_UNFMT)
509 device->state == DASD_STATE_READY &&
510 device->target >= DASD_STATE_ONLINE)
511 rc = dasd_state_ready_to_online(device);
517 * Device shutdown state changes.
519 static int dasd_decrease_state(struct dasd_device *device)
524 if (device->state == DASD_STATE_ONLINE &&
525 device->target <= DASD_STATE_READY)
526 rc = dasd_state_online_to_ready(device);
529 device->state == DASD_STATE_READY &&
530 device->target <= DASD_STATE_BASIC)
531 rc = dasd_state_ready_to_basic(device);
534 device->state == DASD_STATE_UNFMT &&
535 device->target <= DASD_STATE_BASIC)
536 rc = dasd_state_unfmt_to_basic(device);
539 device->state == DASD_STATE_BASIC &&
540 device->target <= DASD_STATE_KNOWN)
541 rc = dasd_state_basic_to_known(device);
544 device->state == DASD_STATE_KNOWN &&
545 device->target <= DASD_STATE_NEW)
546 rc = dasd_state_known_to_new(device);
552 * This is the main startup/shutdown routine.
554 static void dasd_change_state(struct dasd_device *device)
558 if (device->state == device->target)
559 /* Already where we want to go today... */
561 if (device->state < device->target)
562 rc = dasd_increase_state(device);
564 rc = dasd_decrease_state(device);
568 device->target = device->state;
570 /* let user-space know that the device status changed */
571 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
573 if (device->state == device->target)
574 wake_up(&dasd_init_waitq);
578 * Kick starter for devices that did not complete the startup/shutdown
579 * procedure or were sleeping because of a pending state.
580 * dasd_kick_device will schedule a call do do_kick_device to the kernel
583 static void do_kick_device(struct work_struct *work)
585 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
586 mutex_lock(&device->state_mutex);
587 dasd_change_state(device);
588 mutex_unlock(&device->state_mutex);
589 dasd_schedule_device_bh(device);
590 dasd_put_device(device);
593 void dasd_kick_device(struct dasd_device *device)
595 dasd_get_device(device);
596 /* queue call to dasd_kick_device to the kernel event daemon. */
597 if (!schedule_work(&device->kick_work))
598 dasd_put_device(device);
600 EXPORT_SYMBOL(dasd_kick_device);
603 * dasd_reload_device will schedule a call do do_reload_device to the kernel
606 static void do_reload_device(struct work_struct *work)
608 struct dasd_device *device = container_of(work, struct dasd_device,
610 device->discipline->reload(device);
611 dasd_put_device(device);
614 void dasd_reload_device(struct dasd_device *device)
616 dasd_get_device(device);
617 /* queue call to dasd_reload_device to the kernel event daemon. */
618 if (!schedule_work(&device->reload_device))
619 dasd_put_device(device);
621 EXPORT_SYMBOL(dasd_reload_device);
624 * dasd_restore_device will schedule a call do do_restore_device to the kernel
627 static void do_restore_device(struct work_struct *work)
629 struct dasd_device *device = container_of(work, struct dasd_device,
631 device->cdev->drv->restore(device->cdev);
632 dasd_put_device(device);
635 void dasd_restore_device(struct dasd_device *device)
637 dasd_get_device(device);
638 /* queue call to dasd_restore_device to the kernel event daemon. */
639 if (!schedule_work(&device->restore_device))
640 dasd_put_device(device);
644 * Set the target state for a device and starts the state change.
646 void dasd_set_target_state(struct dasd_device *device, int target)
648 dasd_get_device(device);
649 mutex_lock(&device->state_mutex);
650 /* If we are in probeonly mode stop at DASD_STATE_READY. */
651 if (dasd_probeonly && target > DASD_STATE_READY)
652 target = DASD_STATE_READY;
653 if (device->target != target) {
654 if (device->state == target)
655 wake_up(&dasd_init_waitq);
656 device->target = target;
658 if (device->state != device->target)
659 dasd_change_state(device);
660 mutex_unlock(&device->state_mutex);
661 dasd_put_device(device);
663 EXPORT_SYMBOL(dasd_set_target_state);
666 * Enable devices with device numbers in [from..to].
668 static inline int _wait_for_device(struct dasd_device *device)
670 return (device->state == device->target);
673 void dasd_enable_device(struct dasd_device *device)
675 dasd_set_target_state(device, DASD_STATE_ONLINE);
676 if (device->state <= DASD_STATE_KNOWN)
677 /* No discipline for device found. */
678 dasd_set_target_state(device, DASD_STATE_NEW);
679 /* Now wait for the devices to come up. */
680 wait_event(dasd_init_waitq, _wait_for_device(device));
682 dasd_reload_device(device);
683 if (device->discipline->kick_validate)
684 device->discipline->kick_validate(device);
686 EXPORT_SYMBOL(dasd_enable_device);
689 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
692 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
694 #ifdef CONFIG_DASD_PROFILE
695 struct dasd_profile dasd_global_profile = {
696 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
698 static struct dentry *dasd_debugfs_global_entry;
701 * Add profiling information for cqr before execution.
703 static void dasd_profile_start(struct dasd_block *block,
704 struct dasd_ccw_req *cqr,
708 unsigned int counter;
709 struct dasd_device *device;
711 /* count the length of the chanq for statistics */
713 if (dasd_global_profile_level || block->profile.data)
714 list_for_each(l, &block->ccw_queue)
718 spin_lock(&dasd_global_profile.lock);
719 if (dasd_global_profile.data) {
720 dasd_global_profile.data->dasd_io_nr_req[counter]++;
721 if (rq_data_dir(req) == READ)
722 dasd_global_profile.data->dasd_read_nr_req[counter]++;
724 spin_unlock(&dasd_global_profile.lock);
726 spin_lock(&block->profile.lock);
727 if (block->profile.data) {
728 block->profile.data->dasd_io_nr_req[counter]++;
729 if (rq_data_dir(req) == READ)
730 block->profile.data->dasd_read_nr_req[counter]++;
732 spin_unlock(&block->profile.lock);
735 * We count the request for the start device, even though it may run on
736 * some other device due to error recovery. This way we make sure that
737 * we count each request only once.
739 device = cqr->startdev;
740 if (device->profile.data) {
741 counter = 1; /* request is not yet queued on the start device */
742 list_for_each(l, &device->ccw_queue)
746 spin_lock(&device->profile.lock);
747 if (device->profile.data) {
748 device->profile.data->dasd_io_nr_req[counter]++;
749 if (rq_data_dir(req) == READ)
750 device->profile.data->dasd_read_nr_req[counter]++;
752 spin_unlock(&device->profile.lock);
756 * Add profiling information for cqr after execution.
759 #define dasd_profile_counter(value, index) \
761 for (index = 0; index < 31 && value >> (2+index); index++) \
765 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
778 /* in case of an overflow, reset the whole profile */
779 if (data->dasd_io_reqs == UINT_MAX) {
780 memset(data, 0, sizeof(*data));
781 ktime_get_real_ts64(&data->starttod);
783 data->dasd_io_reqs++;
784 data->dasd_io_sects += sectors;
786 data->dasd_io_alias++;
790 data->dasd_io_secs[sectors_ind]++;
791 data->dasd_io_times[tottime_ind]++;
792 data->dasd_io_timps[tottimeps_ind]++;
793 data->dasd_io_time1[strtime_ind]++;
794 data->dasd_io_time2[irqtime_ind]++;
795 data->dasd_io_time2ps[irqtimeps_ind]++;
796 data->dasd_io_time3[endtime_ind]++;
799 data->dasd_read_reqs++;
800 data->dasd_read_sects += sectors;
802 data->dasd_read_alias++;
804 data->dasd_read_tpm++;
805 data->dasd_read_secs[sectors_ind]++;
806 data->dasd_read_times[tottime_ind]++;
807 data->dasd_read_time1[strtime_ind]++;
808 data->dasd_read_time2[irqtime_ind]++;
809 data->dasd_read_time3[endtime_ind]++;
813 static void dasd_profile_end(struct dasd_block *block,
814 struct dasd_ccw_req *cqr,
817 unsigned long strtime, irqtime, endtime, tottime;
818 unsigned long tottimeps, sectors;
819 struct dasd_device *device;
820 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
821 int irqtime_ind, irqtimeps_ind, endtime_ind;
822 struct dasd_profile_info *data;
824 device = cqr->startdev;
825 if (!(dasd_global_profile_level ||
826 block->profile.data ||
827 device->profile.data))
830 sectors = blk_rq_sectors(req);
831 if (!cqr->buildclk || !cqr->startclk ||
832 !cqr->stopclk || !cqr->endclk ||
836 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
837 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
838 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
839 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
840 tottimeps = tottime / sectors;
842 dasd_profile_counter(sectors, sectors_ind);
843 dasd_profile_counter(tottime, tottime_ind);
844 dasd_profile_counter(tottimeps, tottimeps_ind);
845 dasd_profile_counter(strtime, strtime_ind);
846 dasd_profile_counter(irqtime, irqtime_ind);
847 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
848 dasd_profile_counter(endtime, endtime_ind);
850 spin_lock(&dasd_global_profile.lock);
851 if (dasd_global_profile.data) {
852 data = dasd_global_profile.data;
853 data->dasd_sum_times += tottime;
854 data->dasd_sum_time_str += strtime;
855 data->dasd_sum_time_irq += irqtime;
856 data->dasd_sum_time_end += endtime;
857 dasd_profile_end_add_data(dasd_global_profile.data,
858 cqr->startdev != block->base,
860 rq_data_dir(req) == READ,
861 sectors, sectors_ind, tottime_ind,
862 tottimeps_ind, strtime_ind,
863 irqtime_ind, irqtimeps_ind,
866 spin_unlock(&dasd_global_profile.lock);
868 spin_lock(&block->profile.lock);
869 if (block->profile.data) {
870 data = block->profile.data;
871 data->dasd_sum_times += tottime;
872 data->dasd_sum_time_str += strtime;
873 data->dasd_sum_time_irq += irqtime;
874 data->dasd_sum_time_end += endtime;
875 dasd_profile_end_add_data(block->profile.data,
876 cqr->startdev != block->base,
878 rq_data_dir(req) == READ,
879 sectors, sectors_ind, tottime_ind,
880 tottimeps_ind, strtime_ind,
881 irqtime_ind, irqtimeps_ind,
884 spin_unlock(&block->profile.lock);
886 spin_lock(&device->profile.lock);
887 if (device->profile.data) {
888 data = device->profile.data;
889 data->dasd_sum_times += tottime;
890 data->dasd_sum_time_str += strtime;
891 data->dasd_sum_time_irq += irqtime;
892 data->dasd_sum_time_end += endtime;
893 dasd_profile_end_add_data(device->profile.data,
894 cqr->startdev != block->base,
896 rq_data_dir(req) == READ,
897 sectors, sectors_ind, tottime_ind,
898 tottimeps_ind, strtime_ind,
899 irqtime_ind, irqtimeps_ind,
902 spin_unlock(&device->profile.lock);
905 void dasd_profile_reset(struct dasd_profile *profile)
907 struct dasd_profile_info *data;
909 spin_lock_bh(&profile->lock);
910 data = profile->data;
912 spin_unlock_bh(&profile->lock);
915 memset(data, 0, sizeof(*data));
916 ktime_get_real_ts64(&data->starttod);
917 spin_unlock_bh(&profile->lock);
920 int dasd_profile_on(struct dasd_profile *profile)
922 struct dasd_profile_info *data;
924 data = kzalloc(sizeof(*data), GFP_KERNEL);
927 spin_lock_bh(&profile->lock);
929 spin_unlock_bh(&profile->lock);
933 ktime_get_real_ts64(&data->starttod);
934 profile->data = data;
935 spin_unlock_bh(&profile->lock);
939 void dasd_profile_off(struct dasd_profile *profile)
941 spin_lock_bh(&profile->lock);
942 kfree(profile->data);
943 profile->data = NULL;
944 spin_unlock_bh(&profile->lock);
947 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
951 buffer = vmalloc(user_len + 1);
953 return ERR_PTR(-ENOMEM);
954 if (copy_from_user(buffer, user_buf, user_len) != 0) {
956 return ERR_PTR(-EFAULT);
958 /* got the string, now strip linefeed. */
959 if (buffer[user_len - 1] == '\n')
960 buffer[user_len - 1] = 0;
962 buffer[user_len] = 0;
966 static ssize_t dasd_stats_write(struct file *file,
967 const char __user *user_buf,
968 size_t user_len, loff_t *pos)
972 struct seq_file *m = (struct seq_file *)file->private_data;
973 struct dasd_profile *prof = m->private;
975 if (user_len > 65536)
977 buffer = dasd_get_user_string(user_buf, user_len);
979 return PTR_ERR(buffer);
981 str = skip_spaces(buffer);
983 if (strncmp(str, "reset", 5) == 0) {
984 dasd_profile_reset(prof);
985 } else if (strncmp(str, "on", 2) == 0) {
986 rc = dasd_profile_on(prof);
990 if (prof == &dasd_global_profile) {
991 dasd_profile_reset(prof);
992 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
994 } else if (strncmp(str, "off", 3) == 0) {
995 if (prof == &dasd_global_profile)
996 dasd_global_profile_level = DASD_PROFILE_OFF;
997 dasd_profile_off(prof);
1005 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
1009 for (i = 0; i < 32; i++)
1010 seq_printf(m, "%u ", array[i]);
1014 static void dasd_stats_seq_print(struct seq_file *m,
1015 struct dasd_profile_info *data)
1017 seq_printf(m, "start_time %lld.%09ld\n",
1018 (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
1019 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
1020 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1021 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1022 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
1023 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1024 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1025 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1026 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1027 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1028 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1029 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1030 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
1031 seq_puts(m, "histogram_sectors ");
1032 dasd_stats_array(m, data->dasd_io_secs);
1033 seq_puts(m, "histogram_io_times ");
1034 dasd_stats_array(m, data->dasd_io_times);
1035 seq_puts(m, "histogram_io_times_weighted ");
1036 dasd_stats_array(m, data->dasd_io_timps);
1037 seq_puts(m, "histogram_time_build_to_ssch ");
1038 dasd_stats_array(m, data->dasd_io_time1);
1039 seq_puts(m, "histogram_time_ssch_to_irq ");
1040 dasd_stats_array(m, data->dasd_io_time2);
1041 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
1042 dasd_stats_array(m, data->dasd_io_time2ps);
1043 seq_puts(m, "histogram_time_irq_to_end ");
1044 dasd_stats_array(m, data->dasd_io_time3);
1045 seq_puts(m, "histogram_ccw_queue_length ");
1046 dasd_stats_array(m, data->dasd_io_nr_req);
1047 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1048 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1049 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1050 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
1051 seq_puts(m, "histogram_read_sectors ");
1052 dasd_stats_array(m, data->dasd_read_secs);
1053 seq_puts(m, "histogram_read_times ");
1054 dasd_stats_array(m, data->dasd_read_times);
1055 seq_puts(m, "histogram_read_time_build_to_ssch ");
1056 dasd_stats_array(m, data->dasd_read_time1);
1057 seq_puts(m, "histogram_read_time_ssch_to_irq ");
1058 dasd_stats_array(m, data->dasd_read_time2);
1059 seq_puts(m, "histogram_read_time_irq_to_end ");
1060 dasd_stats_array(m, data->dasd_read_time3);
1061 seq_puts(m, "histogram_read_ccw_queue_length ");
1062 dasd_stats_array(m, data->dasd_read_nr_req);
1065 static int dasd_stats_show(struct seq_file *m, void *v)
1067 struct dasd_profile *profile;
1068 struct dasd_profile_info *data;
1070 profile = m->private;
1071 spin_lock_bh(&profile->lock);
1072 data = profile->data;
1074 spin_unlock_bh(&profile->lock);
1075 seq_puts(m, "disabled\n");
1078 dasd_stats_seq_print(m, data);
1079 spin_unlock_bh(&profile->lock);
1083 static int dasd_stats_open(struct inode *inode, struct file *file)
1085 struct dasd_profile *profile = inode->i_private;
1086 return single_open(file, dasd_stats_show, profile);
1089 static const struct file_operations dasd_stats_raw_fops = {
1090 .owner = THIS_MODULE,
1091 .open = dasd_stats_open,
1093 .llseek = seq_lseek,
1094 .release = single_release,
1095 .write = dasd_stats_write,
1098 static void dasd_profile_init(struct dasd_profile *profile,
1099 struct dentry *base_dentry)
1106 profile->dentry = NULL;
1107 profile->data = NULL;
1108 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1109 pde = debugfs_create_file("statistics", mode, base_dentry,
1110 profile, &dasd_stats_raw_fops);
1111 if (pde && !IS_ERR(pde))
1112 profile->dentry = pde;
1116 static void dasd_profile_exit(struct dasd_profile *profile)
1118 dasd_profile_off(profile);
1119 debugfs_remove(profile->dentry);
1120 profile->dentry = NULL;
1123 static void dasd_statistics_removeroot(void)
1125 dasd_global_profile_level = DASD_PROFILE_OFF;
1126 dasd_profile_exit(&dasd_global_profile);
1127 debugfs_remove(dasd_debugfs_global_entry);
1128 debugfs_remove(dasd_debugfs_root_entry);
1131 static void dasd_statistics_createroot(void)
1135 dasd_debugfs_root_entry = NULL;
1136 pde = debugfs_create_dir("dasd", NULL);
1137 if (!pde || IS_ERR(pde))
1139 dasd_debugfs_root_entry = pde;
1140 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1141 if (!pde || IS_ERR(pde))
1143 dasd_debugfs_global_entry = pde;
1144 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
1148 DBF_EVENT(DBF_ERR, "%s",
1149 "Creation of the dasd debugfs interface failed");
1150 dasd_statistics_removeroot();
1155 #define dasd_profile_start(block, cqr, req) do {} while (0)
1156 #define dasd_profile_end(block, cqr, req) do {} while (0)
1158 static void dasd_statistics_createroot(void)
1163 static void dasd_statistics_removeroot(void)
1168 int dasd_stats_generic_show(struct seq_file *m, void *v)
1170 seq_puts(m, "Statistics are not activated in this kernel\n");
1174 static void dasd_profile_init(struct dasd_profile *profile,
1175 struct dentry *base_dentry)
1180 static void dasd_profile_exit(struct dasd_profile *profile)
1185 int dasd_profile_on(struct dasd_profile *profile)
1190 #endif /* CONFIG_DASD_PROFILE */
1192 static int dasd_hosts_show(struct seq_file *m, void *v)
1194 struct dasd_device *device;
1195 int rc = -EOPNOTSUPP;
1197 device = m->private;
1198 dasd_get_device(device);
1200 if (device->discipline->hosts_print)
1201 rc = device->discipline->hosts_print(device, m);
1203 dasd_put_device(device);
1207 DEFINE_SHOW_ATTRIBUTE(dasd_hosts);
1209 static void dasd_hosts_exit(struct dasd_device *device)
1211 debugfs_remove(device->hosts_dentry);
1212 device->hosts_dentry = NULL;
1215 static void dasd_hosts_init(struct dentry *base_dentry,
1216 struct dasd_device *device)
1224 mode = S_IRUSR | S_IFREG;
1225 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1226 device, &dasd_hosts_fops);
1227 if (pde && !IS_ERR(pde))
1228 device->hosts_dentry = pde;
1231 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
1232 struct dasd_device *device,
1233 struct dasd_ccw_req *cqr)
1235 unsigned long flags;
1240 size += cplength * sizeof(struct ccw1);
1244 size += (sizeof(*cqr) + 7L) & -8L;
1246 spin_lock_irqsave(&device->mem_lock, flags);
1247 data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
1248 spin_unlock_irqrestore(&device->mem_lock, flags);
1250 return ERR_PTR(-ENOMEM);
1252 cqr = (void *) data;
1253 data += (sizeof(*cqr) + 7L) & -8L;
1255 memset(cqr, 0, sizeof(*cqr));
1256 cqr->mem_chunk = chunk;
1259 data += cplength * sizeof(struct ccw1);
1260 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1264 memset(cqr->data, 0, datasize);
1267 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1268 dasd_get_device(device);
1271 EXPORT_SYMBOL(dasd_smalloc_request);
1273 struct dasd_ccw_req *dasd_fmalloc_request(int magic, int cplength,
1275 struct dasd_device *device)
1277 struct dasd_ccw_req *cqr;
1278 unsigned long flags;
1282 cqr_size = (sizeof(*cqr) + 7L) & -8L;
1285 size += cplength * sizeof(struct ccw1);
1289 spin_lock_irqsave(&device->mem_lock, flags);
1290 cqr = dasd_alloc_chunk(&device->ese_chunks, size);
1291 spin_unlock_irqrestore(&device->mem_lock, flags);
1293 return ERR_PTR(-ENOMEM);
1294 memset(cqr, 0, sizeof(*cqr));
1295 data = (char *)cqr + cqr_size;
1299 data += cplength * sizeof(struct ccw1);
1300 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1305 memset(cqr->data, 0, datasize);
1309 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1310 dasd_get_device(device);
1314 EXPORT_SYMBOL(dasd_fmalloc_request);
1316 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1318 unsigned long flags;
1320 spin_lock_irqsave(&device->mem_lock, flags);
1321 dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
1322 spin_unlock_irqrestore(&device->mem_lock, flags);
1323 dasd_put_device(device);
1325 EXPORT_SYMBOL(dasd_sfree_request);
1327 void dasd_ffree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1329 unsigned long flags;
1331 spin_lock_irqsave(&device->mem_lock, flags);
1332 dasd_free_chunk(&device->ese_chunks, cqr);
1333 spin_unlock_irqrestore(&device->mem_lock, flags);
1334 dasd_put_device(device);
1336 EXPORT_SYMBOL(dasd_ffree_request);
1339 * Check discipline magic in cqr.
1341 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1343 struct dasd_device *device;
1347 device = cqr->startdev;
1348 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1349 DBF_DEV_EVENT(DBF_WARNING, device,
1350 " dasd_ccw_req 0x%08x magic doesn't match"
1351 " discipline 0x%08x",
1353 *(unsigned int *) device->discipline->name);
1360 * Terminate the current i/o and set the request to clear_pending.
1361 * Timer keeps device runnig.
1362 * ccw_device_clear can fail if the i/o subsystem
1365 int dasd_term_IO(struct dasd_ccw_req *cqr)
1367 struct dasd_device *device;
1369 char errorstring[ERRORLENGTH];
1372 rc = dasd_check_cqr(cqr);
1376 device = (struct dasd_device *) cqr->startdev;
1377 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1378 rc = ccw_device_clear(device->cdev, (long) cqr);
1380 case 0: /* termination successful */
1381 cqr->status = DASD_CQR_CLEAR_PENDING;
1382 cqr->stopclk = get_tod_clock();
1384 DBF_DEV_EVENT(DBF_DEBUG, device,
1385 "terminate cqr %p successful",
1389 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1390 "device gone, retry");
1394 * device not valid so no I/O could be running
1395 * handle CQR as termination successful
1397 cqr->status = DASD_CQR_CLEARED;
1398 cqr->stopclk = get_tod_clock();
1400 /* no retries for invalid devices */
1402 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1403 "EINVAL, handle as terminated");
1404 /* fake rc to success */
1408 /* internal error 10 - unknown rc*/
1409 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1410 dev_err(&device->cdev->dev, "An error occurred in the "
1411 "DASD device driver, reason=%s\n", errorstring);
1417 dasd_schedule_device_bh(device);
1420 EXPORT_SYMBOL(dasd_term_IO);
1423 * Start the i/o. This start_IO can fail if the channel is really busy.
1424 * In that case set up a timer to start the request later.
1426 int dasd_start_IO(struct dasd_ccw_req *cqr)
1428 struct dasd_device *device;
1430 char errorstring[ERRORLENGTH];
1433 rc = dasd_check_cqr(cqr);
1438 device = (struct dasd_device *) cqr->startdev;
1440 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1441 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1442 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1443 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1444 "because of stolen lock", cqr);
1445 cqr->status = DASD_CQR_ERROR;
1446 cqr->intrc = -EPERM;
1449 if (cqr->retries < 0) {
1450 /* internal error 14 - start_IO run out of retries */
1451 sprintf(errorstring, "14 %p", cqr);
1452 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1453 "device driver, reason=%s\n", errorstring);
1454 cqr->status = DASD_CQR_ERROR;
1457 cqr->startclk = get_tod_clock();
1458 cqr->starttime = jiffies;
1460 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1461 cqr->lpm &= dasd_path_get_opm(device);
1463 cqr->lpm = dasd_path_get_opm(device);
1465 if (cqr->cpmode == 1) {
1466 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1467 (long) cqr, cqr->lpm);
1469 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1470 (long) cqr, cqr->lpm, 0);
1474 cqr->status = DASD_CQR_IN_IO;
1477 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1478 "start_IO: device busy, retry later");
1481 /* -EACCES indicates that the request used only a subset of the
1482 * available paths and all these paths are gone. If the lpm of
1483 * this request was only a subset of the opm (e.g. the ppm) then
1484 * we just do a retry with all available paths.
1485 * If we already use the full opm, something is amiss, and we
1486 * need a full path verification.
1488 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1489 DBF_DEV_EVENT(DBF_WARNING, device,
1490 "start_IO: selected paths gone (%x)",
1492 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1493 cqr->lpm = dasd_path_get_opm(device);
1494 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1495 "start_IO: selected paths gone,"
1496 " retry on all paths");
1498 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1499 "start_IO: all paths in opm gone,"
1500 " do path verification");
1501 dasd_generic_last_path_gone(device);
1502 dasd_path_no_path(device);
1503 dasd_path_set_tbvpm(device,
1504 ccw_device_get_path_mask(
1509 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1510 "start_IO: -ENODEV device gone, retry");
1513 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1514 "start_IO: -EIO device gone, retry");
1517 /* most likely caused in power management context */
1518 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1519 "start_IO: -EINVAL device currently "
1523 /* internal error 11 - unknown rc */
1524 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1525 dev_err(&device->cdev->dev,
1526 "An error occurred in the DASD device driver, "
1527 "reason=%s\n", errorstring);
1534 EXPORT_SYMBOL(dasd_start_IO);
1537 * Timeout function for dasd devices. This is used for different purposes
1538 * 1) missing interrupt handler for normal operation
1539 * 2) delayed start of request where start_IO failed with -EBUSY
1540 * 3) timeout for missing state change interrupts
1541 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1542 * DASD_CQR_QUEUED for 2) and 3).
1544 static void dasd_device_timeout(struct timer_list *t)
1546 unsigned long flags;
1547 struct dasd_device *device;
1549 device = from_timer(device, t, timer);
1550 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1551 /* re-activate request queue */
1552 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1553 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1554 dasd_schedule_device_bh(device);
1558 * Setup timeout for a device in jiffies.
1560 void dasd_device_set_timer(struct dasd_device *device, int expires)
1563 del_timer(&device->timer);
1565 mod_timer(&device->timer, jiffies + expires);
1567 EXPORT_SYMBOL(dasd_device_set_timer);
1570 * Clear timeout for a device.
1572 void dasd_device_clear_timer(struct dasd_device *device)
1574 del_timer(&device->timer);
1576 EXPORT_SYMBOL(dasd_device_clear_timer);
1578 static void dasd_handle_killed_request(struct ccw_device *cdev,
1579 unsigned long intparm)
1581 struct dasd_ccw_req *cqr;
1582 struct dasd_device *device;
1586 cqr = (struct dasd_ccw_req *) intparm;
1587 if (cqr->status != DASD_CQR_IN_IO) {
1588 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1589 "invalid status in handle_killed_request: "
1590 "%02x", cqr->status);
1594 device = dasd_device_from_cdev_locked(cdev);
1595 if (IS_ERR(device)) {
1596 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1597 "unable to get device from cdev");
1601 if (!cqr->startdev ||
1602 device != cqr->startdev ||
1603 strncmp(cqr->startdev->discipline->ebcname,
1604 (char *) &cqr->magic, 4)) {
1605 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1606 "invalid device in request");
1607 dasd_put_device(device);
1611 /* Schedule request to be retried. */
1612 cqr->status = DASD_CQR_QUEUED;
1614 dasd_device_clear_timer(device);
1615 dasd_schedule_device_bh(device);
1616 dasd_put_device(device);
1619 void dasd_generic_handle_state_change(struct dasd_device *device)
1621 /* First of all start sense subsystem status request. */
1622 dasd_eer_snss(device);
1624 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1625 dasd_schedule_device_bh(device);
1626 if (device->block) {
1627 dasd_schedule_block_bh(device->block);
1628 if (device->block->request_queue)
1629 blk_mq_run_hw_queues(device->block->request_queue,
1633 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1635 static int dasd_check_hpf_error(struct irb *irb)
1637 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1638 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1639 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1642 static int dasd_ese_needs_format(struct dasd_block *block, struct irb *irb)
1644 struct dasd_device *device = NULL;
1649 device = block->base;
1650 if (!device || !device->discipline->is_ese)
1652 if (!device->discipline->is_ese(device))
1655 sense = dasd_get_sense(irb);
1659 return !!(sense[1] & SNS1_NO_REC_FOUND) ||
1660 !!(sense[1] & SNS1_FILE_PROTECTED) ||
1661 scsw_cstat(&irb->scsw) == SCHN_STAT_INCORR_LEN;
1664 static int dasd_ese_oos_cond(u8 *sense)
1666 return sense[0] & SNS0_EQUIPMENT_CHECK &&
1667 sense[1] & SNS1_PERM_ERR &&
1668 sense[1] & SNS1_WRITE_INHIBITED &&
1673 * Interrupt handler for "normal" ssch-io based dasd devices.
1675 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1678 struct dasd_ccw_req *cqr, *next, *fcqr;
1679 struct dasd_device *device;
1681 int nrf_suppressed = 0;
1682 int fp_suppressed = 0;
1686 cqr = (struct dasd_ccw_req *) intparm;
1688 switch (PTR_ERR(irb)) {
1690 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1691 device = cqr->startdev;
1692 cqr->status = DASD_CQR_CLEARED;
1693 dasd_device_clear_timer(device);
1694 wake_up(&dasd_flush_wq);
1695 dasd_schedule_device_bh(device);
1700 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1701 "request timed out\n", __func__);
1704 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1705 "unknown error %ld\n", __func__,
1708 dasd_handle_killed_request(cdev, intparm);
1712 now = get_tod_clock();
1713 /* check for conditions that should be handled immediately */
1715 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1716 scsw_cstat(&irb->scsw) == 0)) {
1718 memcpy(&cqr->irb, irb, sizeof(*irb));
1719 device = dasd_device_from_cdev_locked(cdev);
1722 /* ignore unsolicited interrupts for DIAG discipline */
1723 if (device->discipline == dasd_diag_discipline_pointer) {
1724 dasd_put_device(device);
1729 * In some cases 'File Protected' or 'No Record Found' errors
1730 * might be expected and debug log messages for the
1731 * corresponding interrupts shouldn't be written then.
1732 * Check if either of the according suppress bits is set.
1734 sense = dasd_get_sense(irb);
1736 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1737 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1738 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1739 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1742 * Extent pool probably out-of-space.
1743 * Stop device and check exhaust level.
1745 if (dasd_ese_oos_cond(sense)) {
1746 dasd_generic_space_exhaust(device, cqr);
1747 device->discipline->ext_pool_exhaust(device, cqr);
1748 dasd_put_device(device);
1752 if (!(fp_suppressed || nrf_suppressed))
1753 device->discipline->dump_sense_dbf(device, irb, "int");
1755 if (device->features & DASD_FEATURE_ERPLOG)
1756 device->discipline->dump_sense(device, cqr, irb);
1757 device->discipline->check_for_device_change(device, cqr, irb);
1758 dasd_put_device(device);
1761 /* check for for attention message */
1762 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1763 device = dasd_device_from_cdev_locked(cdev);
1764 if (!IS_ERR(device)) {
1765 device->discipline->check_attention(device,
1766 irb->esw.esw1.lpum);
1767 dasd_put_device(device);
1774 device = (struct dasd_device *) cqr->startdev;
1776 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1777 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1778 "invalid device in request");
1782 if (dasd_ese_needs_format(cqr->block, irb)) {
1783 if (rq_data_dir((struct request *)cqr->callback_data) == READ) {
1784 device->discipline->ese_read(cqr, irb);
1785 cqr->status = DASD_CQR_SUCCESS;
1787 dasd_device_clear_timer(device);
1788 dasd_schedule_device_bh(device);
1791 fcqr = device->discipline->ese_format(device, cqr, irb);
1793 if (PTR_ERR(fcqr) == -EINVAL) {
1794 cqr->status = DASD_CQR_ERROR;
1798 * If we can't format now, let the request go
1799 * one extra round. Maybe we can format later.
1801 cqr->status = DASD_CQR_QUEUED;
1802 dasd_schedule_device_bh(device);
1805 fcqr->status = DASD_CQR_QUEUED;
1806 cqr->status = DASD_CQR_QUEUED;
1807 list_add(&fcqr->devlist, &device->ccw_queue);
1808 dasd_schedule_device_bh(device);
1813 /* Check for clear pending */
1814 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1815 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1816 cqr->status = DASD_CQR_CLEARED;
1817 dasd_device_clear_timer(device);
1818 wake_up(&dasd_flush_wq);
1819 dasd_schedule_device_bh(device);
1823 /* check status - the request might have been killed by dyn detach */
1824 if (cqr->status != DASD_CQR_IN_IO) {
1825 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1826 "status %02x", dev_name(&cdev->dev), cqr->status);
1832 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1833 scsw_cstat(&irb->scsw) == 0) {
1834 /* request was completed successfully */
1835 cqr->status = DASD_CQR_SUCCESS;
1837 /* Start first request on queue if possible -> fast_io. */
1838 if (cqr->devlist.next != &device->ccw_queue) {
1839 next = list_entry(cqr->devlist.next,
1840 struct dasd_ccw_req, devlist);
1842 } else { /* error */
1843 /* check for HPF error
1844 * call discipline function to requeue all requests
1845 * and disable HPF accordingly
1847 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1848 device->discipline->handle_hpf_error)
1849 device->discipline->handle_hpf_error(device, irb);
1851 * If we don't want complex ERP for this request, then just
1852 * reset this and retry it in the fastpath
1854 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1856 if (cqr->lpm == dasd_path_get_opm(device))
1857 DBF_DEV_EVENT(DBF_DEBUG, device,
1858 "default ERP in fastpath "
1859 "(%i retries left)",
1861 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1862 cqr->lpm = dasd_path_get_opm(device);
1863 cqr->status = DASD_CQR_QUEUED;
1866 cqr->status = DASD_CQR_ERROR;
1868 if (next && (next->status == DASD_CQR_QUEUED) &&
1869 (!device->stopped)) {
1870 if (device->discipline->start_IO(next) == 0)
1871 expires = next->expires;
1874 dasd_device_set_timer(device, expires);
1876 dasd_device_clear_timer(device);
1877 dasd_schedule_device_bh(device);
1879 EXPORT_SYMBOL(dasd_int_handler);
1881 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1883 struct dasd_device *device;
1885 device = dasd_device_from_cdev_locked(cdev);
1889 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1890 device->state != device->target ||
1891 !device->discipline->check_for_device_change){
1892 dasd_put_device(device);
1895 if (device->discipline->dump_sense_dbf)
1896 device->discipline->dump_sense_dbf(device, irb, "uc");
1897 device->discipline->check_for_device_change(device, NULL, irb);
1898 dasd_put_device(device);
1900 return UC_TODO_RETRY;
1902 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1905 * If we have an error on a dasd_block layer request then we cancel
1906 * and return all further requests from the same dasd_block as well.
1908 static void __dasd_device_recovery(struct dasd_device *device,
1909 struct dasd_ccw_req *ref_cqr)
1911 struct list_head *l, *n;
1912 struct dasd_ccw_req *cqr;
1915 * only requeue request that came from the dasd_block layer
1917 if (!ref_cqr->block)
1920 list_for_each_safe(l, n, &device->ccw_queue) {
1921 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1922 if (cqr->status == DASD_CQR_QUEUED &&
1923 ref_cqr->block == cqr->block) {
1924 cqr->status = DASD_CQR_CLEARED;
1930 * Remove those ccw requests from the queue that need to be returned
1931 * to the upper layer.
1933 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1934 struct list_head *final_queue)
1936 struct list_head *l, *n;
1937 struct dasd_ccw_req *cqr;
1939 /* Process request with final status. */
1940 list_for_each_safe(l, n, &device->ccw_queue) {
1941 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1943 /* Skip any non-final request. */
1944 if (cqr->status == DASD_CQR_QUEUED ||
1945 cqr->status == DASD_CQR_IN_IO ||
1946 cqr->status == DASD_CQR_CLEAR_PENDING)
1948 if (cqr->status == DASD_CQR_ERROR) {
1949 __dasd_device_recovery(device, cqr);
1951 /* Rechain finished requests to final queue */
1952 list_move_tail(&cqr->devlist, final_queue);
1956 static void __dasd_process_cqr(struct dasd_device *device,
1957 struct dasd_ccw_req *cqr)
1959 char errorstring[ERRORLENGTH];
1961 switch (cqr->status) {
1962 case DASD_CQR_SUCCESS:
1963 cqr->status = DASD_CQR_DONE;
1965 case DASD_CQR_ERROR:
1966 cqr->status = DASD_CQR_NEED_ERP;
1968 case DASD_CQR_CLEARED:
1969 cqr->status = DASD_CQR_TERMINATED;
1972 /* internal error 12 - wrong cqr status*/
1973 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1974 dev_err(&device->cdev->dev,
1975 "An error occurred in the DASD device driver, "
1976 "reason=%s\n", errorstring);
1980 cqr->callback(cqr, cqr->callback_data);
1984 * the cqrs from the final queue are returned to the upper layer
1985 * by setting a dasd_block state and calling the callback function
1987 static void __dasd_device_process_final_queue(struct dasd_device *device,
1988 struct list_head *final_queue)
1990 struct list_head *l, *n;
1991 struct dasd_ccw_req *cqr;
1992 struct dasd_block *block;
1994 list_for_each_safe(l, n, final_queue) {
1995 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1996 list_del_init(&cqr->devlist);
1999 __dasd_process_cqr(device, cqr);
2001 spin_lock_bh(&block->queue_lock);
2002 __dasd_process_cqr(device, cqr);
2003 spin_unlock_bh(&block->queue_lock);
2009 * Take a look at the first request on the ccw queue and check
2010 * if it reached its expire time. If so, terminate the IO.
2012 static void __dasd_device_check_expire(struct dasd_device *device)
2014 struct dasd_ccw_req *cqr;
2016 if (list_empty(&device->ccw_queue))
2018 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2019 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
2020 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
2021 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2023 * IO in safe offline processing should not
2024 * run out of retries
2028 if (device->discipline->term_IO(cqr) != 0) {
2029 /* Hmpf, try again in 5 sec */
2030 dev_err(&device->cdev->dev,
2031 "cqr %p timed out (%lus) but cannot be "
2032 "ended, retrying in 5 s\n",
2033 cqr, (cqr->expires/HZ));
2034 cqr->expires += 5*HZ;
2035 dasd_device_set_timer(device, 5*HZ);
2037 dev_err(&device->cdev->dev,
2038 "cqr %p timed out (%lus), %i retries "
2039 "remaining\n", cqr, (cqr->expires/HZ),
2046 * return 1 when device is not eligible for IO
2048 static int __dasd_device_is_unusable(struct dasd_device *device,
2049 struct dasd_ccw_req *cqr)
2051 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM | DASD_STOPPED_NOSPC);
2053 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2054 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2056 * dasd is being set offline
2057 * but it is no safe offline where we have to allow I/O
2061 if (device->stopped) {
2062 if (device->stopped & mask) {
2063 /* stopped and CQR will not change that. */
2066 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2067 /* CQR is not able to change device to
2071 /* CQR required to get device operational. */
2077 * Take a look at the first request on the ccw queue and check
2078 * if it needs to be started.
2080 static void __dasd_device_start_head(struct dasd_device *device)
2082 struct dasd_ccw_req *cqr;
2085 if (list_empty(&device->ccw_queue))
2087 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2088 if (cqr->status != DASD_CQR_QUEUED)
2090 /* if device is not usable return request to upper layer */
2091 if (__dasd_device_is_unusable(device, cqr)) {
2092 cqr->intrc = -EAGAIN;
2093 cqr->status = DASD_CQR_CLEARED;
2094 dasd_schedule_device_bh(device);
2098 rc = device->discipline->start_IO(cqr);
2100 dasd_device_set_timer(device, cqr->expires);
2101 else if (rc == -EACCES) {
2102 dasd_schedule_device_bh(device);
2104 /* Hmpf, try again in 1/2 sec */
2105 dasd_device_set_timer(device, 50);
2108 static void __dasd_device_check_path_events(struct dasd_device *device)
2112 if (!dasd_path_get_tbvpm(device))
2115 if (device->stopped &
2116 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2118 rc = device->discipline->verify_path(device,
2119 dasd_path_get_tbvpm(device));
2121 dasd_device_set_timer(device, 50);
2123 dasd_path_clear_all_verify(device);
2127 * Go through all request on the dasd_device request queue,
2128 * terminate them on the cdev if necessary, and return them to the
2129 * submitting layer via callback.
2131 * Make sure that all 'submitting layers' still exist when
2132 * this function is called!. In other words, when 'device' is a base
2133 * device then all block layer requests must have been removed before
2134 * via dasd_flush_block_queue.
2136 int dasd_flush_device_queue(struct dasd_device *device)
2138 struct dasd_ccw_req *cqr, *n;
2140 struct list_head flush_queue;
2142 INIT_LIST_HEAD(&flush_queue);
2143 spin_lock_irq(get_ccwdev_lock(device->cdev));
2145 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2146 /* Check status and move request to flush_queue */
2147 switch (cqr->status) {
2148 case DASD_CQR_IN_IO:
2149 rc = device->discipline->term_IO(cqr);
2151 /* unable to terminate requeust */
2152 dev_err(&device->cdev->dev,
2153 "Flushing the DASD request queue "
2154 "failed for request %p\n", cqr);
2155 /* stop flush processing */
2159 case DASD_CQR_QUEUED:
2160 cqr->stopclk = get_tod_clock();
2161 cqr->status = DASD_CQR_CLEARED;
2163 default: /* no need to modify the others */
2166 list_move_tail(&cqr->devlist, &flush_queue);
2169 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2171 * After this point all requests must be in state CLEAR_PENDING,
2172 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2173 * one of the others.
2175 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2176 wait_event(dasd_flush_wq,
2177 (cqr->status != DASD_CQR_CLEAR_PENDING));
2179 * Now set each request back to TERMINATED, DONE or NEED_ERP
2180 * and call the callback function of flushed requests
2182 __dasd_device_process_final_queue(device, &flush_queue);
2185 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2188 * Acquire the device lock and process queues for the device.
2190 static void dasd_device_tasklet(unsigned long data)
2192 struct dasd_device *device = (struct dasd_device *) data;
2193 struct list_head final_queue;
2195 atomic_set (&device->tasklet_scheduled, 0);
2196 INIT_LIST_HEAD(&final_queue);
2197 spin_lock_irq(get_ccwdev_lock(device->cdev));
2198 /* Check expire time of first request on the ccw queue. */
2199 __dasd_device_check_expire(device);
2200 /* find final requests on ccw queue */
2201 __dasd_device_process_ccw_queue(device, &final_queue);
2202 __dasd_device_check_path_events(device);
2203 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2204 /* Now call the callback function of requests with final status */
2205 __dasd_device_process_final_queue(device, &final_queue);
2206 spin_lock_irq(get_ccwdev_lock(device->cdev));
2207 /* Now check if the head of the ccw queue needs to be started. */
2208 __dasd_device_start_head(device);
2209 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2210 if (waitqueue_active(&shutdown_waitq))
2211 wake_up(&shutdown_waitq);
2212 dasd_put_device(device);
2216 * Schedules a call to dasd_tasklet over the device tasklet.
2218 void dasd_schedule_device_bh(struct dasd_device *device)
2220 /* Protect against rescheduling. */
2221 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2223 dasd_get_device(device);
2224 tasklet_hi_schedule(&device->tasklet);
2226 EXPORT_SYMBOL(dasd_schedule_device_bh);
2228 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2230 device->stopped |= bits;
2232 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2234 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2236 device->stopped &= ~bits;
2237 if (!device->stopped)
2238 wake_up(&generic_waitq);
2240 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2243 * Queue a request to the head of the device ccw_queue.
2244 * Start the I/O if possible.
2246 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2248 struct dasd_device *device;
2249 unsigned long flags;
2251 device = cqr->startdev;
2252 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2253 cqr->status = DASD_CQR_QUEUED;
2254 list_add(&cqr->devlist, &device->ccw_queue);
2255 /* let the bh start the request to keep them in order */
2256 dasd_schedule_device_bh(device);
2257 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2259 EXPORT_SYMBOL(dasd_add_request_head);
2262 * Queue a request to the tail of the device ccw_queue.
2263 * Start the I/O if possible.
2265 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2267 struct dasd_device *device;
2268 unsigned long flags;
2270 device = cqr->startdev;
2271 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2272 cqr->status = DASD_CQR_QUEUED;
2273 list_add_tail(&cqr->devlist, &device->ccw_queue);
2274 /* let the bh start the request to keep them in order */
2275 dasd_schedule_device_bh(device);
2276 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2278 EXPORT_SYMBOL(dasd_add_request_tail);
2281 * Wakeup helper for the 'sleep_on' functions.
2283 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2285 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2286 cqr->callback_data = DASD_SLEEPON_END_TAG;
2287 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2288 wake_up(&generic_waitq);
2290 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2292 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2294 struct dasd_device *device;
2297 device = cqr->startdev;
2298 spin_lock_irq(get_ccwdev_lock(device->cdev));
2299 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2300 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2305 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2307 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2309 struct dasd_device *device;
2310 dasd_erp_fn_t erp_fn;
2312 if (cqr->status == DASD_CQR_FILLED)
2314 device = cqr->startdev;
2315 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2316 if (cqr->status == DASD_CQR_TERMINATED) {
2317 device->discipline->handle_terminated_request(cqr);
2320 if (cqr->status == DASD_CQR_NEED_ERP) {
2321 erp_fn = device->discipline->erp_action(cqr);
2325 if (cqr->status == DASD_CQR_FAILED)
2326 dasd_log_sense(cqr, &cqr->irb);
2328 __dasd_process_erp(device, cqr);
2335 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2337 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2338 if (cqr->refers) /* erp is not done yet */
2340 return ((cqr->status != DASD_CQR_DONE) &&
2341 (cqr->status != DASD_CQR_FAILED));
2343 return (cqr->status == DASD_CQR_FILLED);
2346 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2348 struct dasd_device *device;
2350 struct list_head ccw_queue;
2351 struct dasd_ccw_req *cqr;
2353 INIT_LIST_HEAD(&ccw_queue);
2354 maincqr->status = DASD_CQR_FILLED;
2355 device = maincqr->startdev;
2356 list_add(&maincqr->blocklist, &ccw_queue);
2357 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2358 cqr = list_first_entry(&ccw_queue,
2359 struct dasd_ccw_req, blocklist)) {
2361 if (__dasd_sleep_on_erp(cqr))
2363 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2365 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2366 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2367 cqr->status = DASD_CQR_FAILED;
2368 cqr->intrc = -EPERM;
2371 /* Non-temporary stop condition will trigger fail fast */
2372 if (device->stopped & ~DASD_STOPPED_PENDING &&
2373 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2374 (!dasd_eer_enabled(device))) {
2375 cqr->status = DASD_CQR_FAILED;
2376 cqr->intrc = -ENOLINK;
2380 * Don't try to start requests if device is in
2381 * offline processing, it might wait forever
2383 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2384 cqr->status = DASD_CQR_FAILED;
2385 cqr->intrc = -ENODEV;
2389 * Don't try to start requests if device is stopped
2390 * except path verification requests
2392 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2393 if (interruptible) {
2394 rc = wait_event_interruptible(
2395 generic_waitq, !(device->stopped));
2396 if (rc == -ERESTARTSYS) {
2397 cqr->status = DASD_CQR_FAILED;
2398 maincqr->intrc = rc;
2402 wait_event(generic_waitq, !(device->stopped));
2405 cqr->callback = dasd_wakeup_cb;
2407 cqr->callback_data = DASD_SLEEPON_START_TAG;
2408 dasd_add_request_tail(cqr);
2409 if (interruptible) {
2410 rc = wait_event_interruptible(
2411 generic_waitq, _wait_for_wakeup(cqr));
2412 if (rc == -ERESTARTSYS) {
2413 dasd_cancel_req(cqr);
2414 /* wait (non-interruptible) for final status */
2415 wait_event(generic_waitq,
2416 _wait_for_wakeup(cqr));
2417 cqr->status = DASD_CQR_FAILED;
2418 maincqr->intrc = rc;
2422 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2425 maincqr->endclk = get_tod_clock();
2426 if ((maincqr->status != DASD_CQR_DONE) &&
2427 (maincqr->intrc != -ERESTARTSYS))
2428 dasd_log_sense(maincqr, &maincqr->irb);
2429 if (maincqr->status == DASD_CQR_DONE)
2431 else if (maincqr->intrc)
2432 rc = maincqr->intrc;
2438 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2440 struct dasd_ccw_req *cqr;
2442 list_for_each_entry(cqr, ccw_queue, blocklist) {
2443 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2450 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2452 struct dasd_device *device;
2453 struct dasd_ccw_req *cqr, *n;
2458 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2459 device = cqr->startdev;
2460 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2463 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2464 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2465 cqr->status = DASD_CQR_FAILED;
2466 cqr->intrc = -EPERM;
2469 /*Non-temporary stop condition will trigger fail fast*/
2470 if (device->stopped & ~DASD_STOPPED_PENDING &&
2471 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2472 !dasd_eer_enabled(device)) {
2473 cqr->status = DASD_CQR_FAILED;
2474 cqr->intrc = -EAGAIN;
2478 /*Don't try to start requests if device is stopped*/
2479 if (interruptible) {
2480 rc = wait_event_interruptible(
2481 generic_waitq, !device->stopped);
2482 if (rc == -ERESTARTSYS) {
2483 cqr->status = DASD_CQR_FAILED;
2488 wait_event(generic_waitq, !(device->stopped));
2491 cqr->callback = dasd_wakeup_cb;
2492 cqr->callback_data = DASD_SLEEPON_START_TAG;
2493 dasd_add_request_tail(cqr);
2496 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2499 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2501 * In some cases the 'File Protected' or 'Incorrect Length'
2502 * error might be expected and error recovery would be
2503 * unnecessary in these cases. Check if the according suppress
2506 sense = dasd_get_sense(&cqr->irb);
2507 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2508 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2510 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2511 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2515 * for alias devices simplify error recovery and
2516 * return to upper layer
2517 * do not skip ERP requests
2519 if (cqr->startdev != cqr->basedev && !cqr->refers &&
2520 (cqr->status == DASD_CQR_TERMINATED ||
2521 cqr->status == DASD_CQR_NEED_ERP))
2524 /* normal recovery for basedev IO */
2525 if (__dasd_sleep_on_erp(cqr))
2526 /* handle erp first */
2534 * Queue a request to the tail of the device ccw_queue and wait for
2537 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2539 return _dasd_sleep_on(cqr, 0);
2541 EXPORT_SYMBOL(dasd_sleep_on);
2544 * Start requests from a ccw_queue and wait for their completion.
2546 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2548 return _dasd_sleep_on_queue(ccw_queue, 0);
2550 EXPORT_SYMBOL(dasd_sleep_on_queue);
2553 * Start requests from a ccw_queue and wait interruptible for their completion.
2555 int dasd_sleep_on_queue_interruptible(struct list_head *ccw_queue)
2557 return _dasd_sleep_on_queue(ccw_queue, 1);
2559 EXPORT_SYMBOL(dasd_sleep_on_queue_interruptible);
2562 * Queue a request to the tail of the device ccw_queue and wait
2563 * interruptible for it's completion.
2565 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2567 return _dasd_sleep_on(cqr, 1);
2569 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2572 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2573 * for eckd devices) the currently running request has to be terminated
2574 * and be put back to status queued, before the special request is added
2575 * to the head of the queue. Then the special request is waited on normally.
2577 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2579 struct dasd_ccw_req *cqr;
2582 if (list_empty(&device->ccw_queue))
2584 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2585 rc = device->discipline->term_IO(cqr);
2588 * CQR terminated because a more important request is pending.
2589 * Undo decreasing of retry counter because this is
2590 * not an error case.
2596 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2598 struct dasd_device *device;
2601 device = cqr->startdev;
2602 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2603 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2604 cqr->status = DASD_CQR_FAILED;
2605 cqr->intrc = -EPERM;
2608 spin_lock_irq(get_ccwdev_lock(device->cdev));
2609 rc = _dasd_term_running_cqr(device);
2611 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2614 cqr->callback = dasd_wakeup_cb;
2615 cqr->callback_data = DASD_SLEEPON_START_TAG;
2616 cqr->status = DASD_CQR_QUEUED;
2618 * add new request as second
2619 * first the terminated cqr needs to be finished
2621 list_add(&cqr->devlist, device->ccw_queue.next);
2623 /* let the bh start the request to keep them in order */
2624 dasd_schedule_device_bh(device);
2626 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2628 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2630 if (cqr->status == DASD_CQR_DONE)
2632 else if (cqr->intrc)
2638 dasd_schedule_device_bh(device);
2640 dasd_schedule_block_bh(device->block);
2644 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2647 * Cancels a request that was started with dasd_sleep_on_req.
2648 * This is useful to timeout requests. The request will be
2649 * terminated if it is currently in i/o.
2650 * Returns 0 if request termination was successful
2651 * negative error code if termination failed
2652 * Cancellation of a request is an asynchronous operation! The calling
2653 * function has to wait until the request is properly returned via callback.
2655 static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
2657 struct dasd_device *device = cqr->startdev;
2660 switch (cqr->status) {
2661 case DASD_CQR_QUEUED:
2662 /* request was not started - just set to cleared */
2663 cqr->status = DASD_CQR_CLEARED;
2665 case DASD_CQR_IN_IO:
2666 /* request in IO - terminate IO and release again */
2667 rc = device->discipline->term_IO(cqr);
2669 dev_err(&device->cdev->dev,
2670 "Cancelling request %p failed with rc=%d\n",
2673 cqr->stopclk = get_tod_clock();
2676 default: /* already finished or clear pending - do nothing */
2679 dasd_schedule_device_bh(device);
2683 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2685 struct dasd_device *device = cqr->startdev;
2686 unsigned long flags;
2689 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2690 rc = __dasd_cancel_req(cqr);
2691 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2696 * SECTION: Operations of the dasd_block layer.
2700 * Timeout function for dasd_block. This is used when the block layer
2701 * is waiting for something that may not come reliably, (e.g. a state
2704 static void dasd_block_timeout(struct timer_list *t)
2706 unsigned long flags;
2707 struct dasd_block *block;
2709 block = from_timer(block, t, timer);
2710 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2711 /* re-activate request queue */
2712 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2713 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2714 dasd_schedule_block_bh(block);
2715 blk_mq_run_hw_queues(block->request_queue, true);
2719 * Setup timeout for a dasd_block in jiffies.
2721 void dasd_block_set_timer(struct dasd_block *block, int expires)
2724 del_timer(&block->timer);
2726 mod_timer(&block->timer, jiffies + expires);
2728 EXPORT_SYMBOL(dasd_block_set_timer);
2731 * Clear timeout for a dasd_block.
2733 void dasd_block_clear_timer(struct dasd_block *block)
2735 del_timer(&block->timer);
2737 EXPORT_SYMBOL(dasd_block_clear_timer);
2740 * Process finished error recovery ccw.
2742 static void __dasd_process_erp(struct dasd_device *device,
2743 struct dasd_ccw_req *cqr)
2745 dasd_erp_fn_t erp_fn;
2747 if (cqr->status == DASD_CQR_DONE)
2748 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2750 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2751 erp_fn = device->discipline->erp_postaction(cqr);
2755 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2757 struct request *req;
2758 blk_status_t error = BLK_STS_OK;
2759 unsigned int proc_bytes;
2762 req = (struct request *) cqr->callback_data;
2763 dasd_profile_end(cqr->block, cqr, req);
2765 proc_bytes = cqr->proc_bytes;
2766 status = cqr->block->base->discipline->free_cp(cqr, req);
2768 error = errno_to_blk_status(status);
2769 else if (status == 0) {
2770 switch (cqr->intrc) {
2772 error = BLK_STS_NEXUS;
2775 error = BLK_STS_TRANSPORT;
2778 error = BLK_STS_TIMEOUT;
2781 error = BLK_STS_IOERR;
2787 * We need to take care for ETIMEDOUT errors here since the
2788 * complete callback does not get called in this case.
2789 * Take care of all errors here and avoid additional code to
2790 * transfer the error value to the complete callback.
2793 blk_mq_end_request(req, error);
2794 blk_mq_run_hw_queues(req->q, true);
2797 * Partial completed requests can happen with ESE devices.
2798 * During read we might have gotten a NRF error and have to
2799 * complete a request partially.
2802 blk_update_request(req, BLK_STS_OK,
2803 blk_rq_bytes(req) - proc_bytes);
2804 blk_mq_requeue_request(req, true);
2805 } else if (likely(!blk_should_fake_timeout(req->q))) {
2806 blk_mq_complete_request(req);
2812 * Process ccw request queue.
2814 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2815 struct list_head *final_queue)
2817 struct list_head *l, *n;
2818 struct dasd_ccw_req *cqr;
2819 dasd_erp_fn_t erp_fn;
2820 unsigned long flags;
2821 struct dasd_device *base = block->base;
2824 /* Process request with final status. */
2825 list_for_each_safe(l, n, &block->ccw_queue) {
2826 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2827 if (cqr->status != DASD_CQR_DONE &&
2828 cqr->status != DASD_CQR_FAILED &&
2829 cqr->status != DASD_CQR_NEED_ERP &&
2830 cqr->status != DASD_CQR_TERMINATED)
2833 if (cqr->status == DASD_CQR_TERMINATED) {
2834 base->discipline->handle_terminated_request(cqr);
2838 /* Process requests that may be recovered */
2839 if (cqr->status == DASD_CQR_NEED_ERP) {
2840 erp_fn = base->discipline->erp_action(cqr);
2841 if (IS_ERR(erp_fn(cqr)))
2846 /* log sense for fatal error */
2847 if (cqr->status == DASD_CQR_FAILED) {
2848 dasd_log_sense(cqr, &cqr->irb);
2851 /* First of all call extended error reporting. */
2852 if (dasd_eer_enabled(base) &&
2853 cqr->status == DASD_CQR_FAILED) {
2854 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2856 /* restart request */
2857 cqr->status = DASD_CQR_FILLED;
2859 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2860 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2861 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2866 /* Process finished ERP request. */
2868 __dasd_process_erp(base, cqr);
2872 /* Rechain finished requests to final queue */
2873 cqr->endclk = get_tod_clock();
2874 list_move_tail(&cqr->blocklist, final_queue);
2878 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2880 dasd_schedule_block_bh(cqr->block);
2883 static void __dasd_block_start_head(struct dasd_block *block)
2885 struct dasd_ccw_req *cqr;
2887 if (list_empty(&block->ccw_queue))
2889 /* We allways begin with the first requests on the queue, as some
2890 * of previously started requests have to be enqueued on a
2891 * dasd_device again for error recovery.
2893 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2894 if (cqr->status != DASD_CQR_FILLED)
2896 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2897 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2898 cqr->status = DASD_CQR_FAILED;
2899 cqr->intrc = -EPERM;
2900 dasd_schedule_block_bh(block);
2903 /* Non-temporary stop condition will trigger fail fast */
2904 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2905 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2906 (!dasd_eer_enabled(block->base))) {
2907 cqr->status = DASD_CQR_FAILED;
2908 cqr->intrc = -ENOLINK;
2909 dasd_schedule_block_bh(block);
2912 /* Don't try to start requests if device is stopped */
2913 if (block->base->stopped)
2916 /* just a fail safe check, should not happen */
2918 cqr->startdev = block->base;
2920 /* make sure that the requests we submit find their way back */
2921 cqr->callback = dasd_return_cqr_cb;
2923 dasd_add_request_tail(cqr);
2928 * Central dasd_block layer routine. Takes requests from the generic
2929 * block layer request queue, creates ccw requests, enqueues them on
2930 * a dasd_device and processes ccw requests that have been returned.
2932 static void dasd_block_tasklet(unsigned long data)
2934 struct dasd_block *block = (struct dasd_block *) data;
2935 struct list_head final_queue;
2936 struct list_head *l, *n;
2937 struct dasd_ccw_req *cqr;
2938 struct dasd_queue *dq;
2940 atomic_set(&block->tasklet_scheduled, 0);
2941 INIT_LIST_HEAD(&final_queue);
2942 spin_lock_irq(&block->queue_lock);
2943 /* Finish off requests on ccw queue */
2944 __dasd_process_block_ccw_queue(block, &final_queue);
2945 spin_unlock_irq(&block->queue_lock);
2947 /* Now call the callback function of requests with final status */
2948 list_for_each_safe(l, n, &final_queue) {
2949 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2951 spin_lock_irq(&dq->lock);
2952 list_del_init(&cqr->blocklist);
2953 __dasd_cleanup_cqr(cqr);
2954 spin_unlock_irq(&dq->lock);
2957 spin_lock_irq(&block->queue_lock);
2958 /* Now check if the head of the ccw queue needs to be started. */
2959 __dasd_block_start_head(block);
2960 spin_unlock_irq(&block->queue_lock);
2962 if (waitqueue_active(&shutdown_waitq))
2963 wake_up(&shutdown_waitq);
2964 dasd_put_device(block->base);
2967 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2969 wake_up(&dasd_flush_wq);
2973 * Requeue a request back to the block request queue
2974 * only works for block requests
2976 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2978 struct dasd_block *block = cqr->block;
2979 struct request *req;
2983 spin_lock_irq(&cqr->dq->lock);
2984 req = (struct request *) cqr->callback_data;
2985 blk_mq_requeue_request(req, false);
2986 spin_unlock_irq(&cqr->dq->lock);
2992 * Go through all request on the dasd_block request queue, cancel them
2993 * on the respective dasd_device, and return them to the generic
2996 static int dasd_flush_block_queue(struct dasd_block *block)
2998 struct dasd_ccw_req *cqr, *n;
3000 struct list_head flush_queue;
3001 unsigned long flags;
3003 INIT_LIST_HEAD(&flush_queue);
3004 spin_lock_bh(&block->queue_lock);
3007 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
3008 /* if this request currently owned by a dasd_device cancel it */
3009 if (cqr->status >= DASD_CQR_QUEUED)
3010 rc = dasd_cancel_req(cqr);
3013 /* Rechain request (including erp chain) so it won't be
3014 * touched by the dasd_block_tasklet anymore.
3015 * Replace the callback so we notice when the request
3016 * is returned from the dasd_device layer.
3018 cqr->callback = _dasd_wake_block_flush_cb;
3019 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
3020 list_move_tail(&cqr->blocklist, &flush_queue);
3022 /* moved more than one request - need to restart */
3025 spin_unlock_bh(&block->queue_lock);
3026 /* Now call the callback function of flushed requests */
3028 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
3029 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
3030 /* Process finished ERP request. */
3032 spin_lock_bh(&block->queue_lock);
3033 __dasd_process_erp(block->base, cqr);
3034 spin_unlock_bh(&block->queue_lock);
3035 /* restart list_for_xx loop since dasd_process_erp
3036 * might remove multiple elements */
3039 /* call the callback function */
3040 spin_lock_irqsave(&cqr->dq->lock, flags);
3041 cqr->endclk = get_tod_clock();
3042 list_del_init(&cqr->blocklist);
3043 __dasd_cleanup_cqr(cqr);
3044 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3050 * Schedules a call to dasd_tasklet over the device tasklet.
3052 void dasd_schedule_block_bh(struct dasd_block *block)
3054 /* Protect against rescheduling. */
3055 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3057 /* life cycle of block is bound to it's base device */
3058 dasd_get_device(block->base);
3059 tasklet_hi_schedule(&block->tasklet);
3061 EXPORT_SYMBOL(dasd_schedule_block_bh);
3065 * SECTION: external block device operations
3066 * (request queue handling, open, release, etc.)
3070 * Dasd request queue function. Called from ll_rw_blk.c
3072 static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
3073 const struct blk_mq_queue_data *qd)
3075 struct dasd_block *block = hctx->queue->queuedata;
3076 struct dasd_queue *dq = hctx->driver_data;
3077 struct request *req = qd->rq;
3078 struct dasd_device *basedev;
3079 struct dasd_ccw_req *cqr;
3080 blk_status_t rc = BLK_STS_OK;
3082 basedev = block->base;
3083 spin_lock_irq(&dq->lock);
3084 if (basedev->state < DASD_STATE_READY) {
3085 DBF_DEV_EVENT(DBF_ERR, basedev,
3086 "device not ready for request %p", req);
3092 * if device is stopped do not fetch new requests
3093 * except failfast is active which will let requests fail
3094 * immediately in __dasd_block_start_head()
3096 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
3097 DBF_DEV_EVENT(DBF_ERR, basedev,
3098 "device stopped request %p", req);
3099 rc = BLK_STS_RESOURCE;
3103 if (basedev->features & DASD_FEATURE_READONLY &&
3104 rq_data_dir(req) == WRITE) {
3105 DBF_DEV_EVENT(DBF_ERR, basedev,
3106 "Rejecting write request %p", req);
3111 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
3112 (basedev->features & DASD_FEATURE_FAILFAST ||
3113 blk_noretry_request(req))) {
3114 DBF_DEV_EVENT(DBF_ERR, basedev,
3115 "Rejecting failfast request %p", req);
3120 cqr = basedev->discipline->build_cp(basedev, block, req);
3122 if (PTR_ERR(cqr) == -EBUSY ||
3123 PTR_ERR(cqr) == -ENOMEM ||
3124 PTR_ERR(cqr) == -EAGAIN) {
3125 rc = BLK_STS_RESOURCE;
3128 DBF_DEV_EVENT(DBF_ERR, basedev,
3129 "CCW creation failed (rc=%ld) on request %p",
3135 * Note: callback is set to dasd_return_cqr_cb in
3136 * __dasd_block_start_head to cover erp requests as well
3138 cqr->callback_data = req;
3139 cqr->status = DASD_CQR_FILLED;
3142 blk_mq_start_request(req);
3143 spin_lock(&block->queue_lock);
3144 list_add_tail(&cqr->blocklist, &block->ccw_queue);
3145 INIT_LIST_HEAD(&cqr->devlist);
3146 dasd_profile_start(block, cqr, req);
3147 dasd_schedule_block_bh(block);
3148 spin_unlock(&block->queue_lock);
3151 spin_unlock_irq(&dq->lock);
3156 * Block timeout callback, called from the block layer
3159 * BLK_EH_RESET_TIMER if the request should be left running
3160 * BLK_EH_DONE if the request is handled or terminated
3163 enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
3165 struct dasd_block *block = req->q->queuedata;
3166 struct dasd_device *device;
3167 struct dasd_ccw_req *cqr;
3168 unsigned long flags;
3171 cqr = blk_mq_rq_to_pdu(req);
3175 spin_lock_irqsave(&cqr->dq->lock, flags);
3176 device = cqr->startdev ? cqr->startdev : block->base;
3177 if (!device->blk_timeout) {
3178 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3179 return BLK_EH_RESET_TIMER;
3181 DBF_DEV_EVENT(DBF_WARNING, device,
3182 " dasd_times_out cqr %p status %x",
3185 spin_lock(&block->queue_lock);
3186 spin_lock(get_ccwdev_lock(device->cdev));
3188 cqr->intrc = -ETIMEDOUT;
3189 if (cqr->status >= DASD_CQR_QUEUED) {
3190 rc = __dasd_cancel_req(cqr);
3191 } else if (cqr->status == DASD_CQR_FILLED ||
3192 cqr->status == DASD_CQR_NEED_ERP) {
3193 cqr->status = DASD_CQR_TERMINATED;
3194 } else if (cqr->status == DASD_CQR_IN_ERP) {
3195 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3197 list_for_each_entry_safe(searchcqr, nextcqr,
3198 &block->ccw_queue, blocklist) {
3200 while (tmpcqr->refers)
3201 tmpcqr = tmpcqr->refers;
3204 /* searchcqr is an ERP request for cqr */
3205 searchcqr->retries = -1;
3206 searchcqr->intrc = -ETIMEDOUT;
3207 if (searchcqr->status >= DASD_CQR_QUEUED) {
3208 rc = __dasd_cancel_req(searchcqr);
3209 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3210 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3211 searchcqr->status = DASD_CQR_TERMINATED;
3213 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3215 * Shouldn't happen; most recent ERP
3216 * request is at the front of queue
3223 spin_unlock(get_ccwdev_lock(device->cdev));
3224 dasd_schedule_block_bh(block);
3225 spin_unlock(&block->queue_lock);
3226 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3228 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
3231 static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3234 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3239 spin_lock_init(&dq->lock);
3240 hctx->driver_data = dq;
3245 static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3247 kfree(hctx->driver_data);
3248 hctx->driver_data = NULL;
3251 static void dasd_request_done(struct request *req)
3253 blk_mq_end_request(req, 0);
3254 blk_mq_run_hw_queues(req->q, true);
3257 static struct blk_mq_ops dasd_mq_ops = {
3258 .queue_rq = do_dasd_request,
3259 .complete = dasd_request_done,
3260 .timeout = dasd_times_out,
3261 .init_hctx = dasd_init_hctx,
3262 .exit_hctx = dasd_exit_hctx,
3266 * Allocate and initialize request queue and default I/O scheduler.
3268 static int dasd_alloc_queue(struct dasd_block *block)
3272 block->tag_set.ops = &dasd_mq_ops;
3273 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
3274 block->tag_set.nr_hw_queues = nr_hw_queues;
3275 block->tag_set.queue_depth = queue_depth;
3276 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
3277 block->tag_set.numa_node = NUMA_NO_NODE;
3279 rc = blk_mq_alloc_tag_set(&block->tag_set);
3283 block->request_queue = blk_mq_init_queue(&block->tag_set);
3284 if (IS_ERR(block->request_queue))
3285 return PTR_ERR(block->request_queue);
3287 block->request_queue->queuedata = block;
3293 * Deactivate and free request queue.
3295 static void dasd_free_queue(struct dasd_block *block)
3297 if (block->request_queue) {
3298 blk_cleanup_queue(block->request_queue);
3299 blk_mq_free_tag_set(&block->tag_set);
3300 block->request_queue = NULL;
3304 static int dasd_open(struct block_device *bdev, fmode_t mode)
3306 struct dasd_device *base;
3309 base = dasd_device_from_gendisk(bdev->bd_disk);
3313 atomic_inc(&base->block->open_count);
3314 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3319 if (!try_module_get(base->discipline->owner)) {
3324 if (dasd_probeonly) {
3325 dev_info(&base->cdev->dev,
3326 "Accessing the DASD failed because it is in "
3327 "probeonly mode\n");
3332 if (base->state <= DASD_STATE_BASIC) {
3333 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3334 " Cannot open unrecognized device");
3339 if ((mode & FMODE_WRITE) &&
3340 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3341 (base->features & DASD_FEATURE_READONLY))) {
3346 dasd_put_device(base);
3350 module_put(base->discipline->owner);
3352 atomic_dec(&base->block->open_count);
3353 dasd_put_device(base);
3357 static void dasd_release(struct gendisk *disk, fmode_t mode)
3359 struct dasd_device *base = dasd_device_from_gendisk(disk);
3361 atomic_dec(&base->block->open_count);
3362 module_put(base->discipline->owner);
3363 dasd_put_device(base);
3368 * Return disk geometry.
3370 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3372 struct dasd_device *base;
3374 base = dasd_device_from_gendisk(bdev->bd_disk);
3378 if (!base->discipline ||
3379 !base->discipline->fill_geometry) {
3380 dasd_put_device(base);
3383 base->discipline->fill_geometry(base->block, geo);
3384 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3385 dasd_put_device(base);
3389 const struct block_device_operations
3390 dasd_device_operations = {
3391 .owner = THIS_MODULE,
3393 .release = dasd_release,
3394 .ioctl = dasd_ioctl,
3395 .compat_ioctl = dasd_ioctl,
3396 .getgeo = dasd_getgeo,
3399 /*******************************************************************************
3400 * end of block device operations
3406 #ifdef CONFIG_PROC_FS
3410 kmem_cache_destroy(dasd_page_cache);
3411 dasd_page_cache = NULL;
3412 dasd_gendisk_exit();
3414 if (dasd_debug_area != NULL) {
3415 debug_unregister(dasd_debug_area);
3416 dasd_debug_area = NULL;
3418 dasd_statistics_removeroot();
3422 * SECTION: common functions for ccw_driver use
3426 * Is the device read-only?
3427 * Note that this function does not report the setting of the
3428 * readonly device attribute, but how it is configured in z/VM.
3430 int dasd_device_is_ro(struct dasd_device *device)
3432 struct ccw_dev_id dev_id;
3433 struct diag210 diag_data;
3438 ccw_device_get_id(device->cdev, &dev_id);
3439 memset(&diag_data, 0, sizeof(diag_data));
3440 diag_data.vrdcdvno = dev_id.devno;
3441 diag_data.vrdclen = sizeof(diag_data);
3442 rc = diag210(&diag_data);
3443 if (rc == 0 || rc == 2) {
3444 return diag_data.vrdcvfla & 0x80;
3446 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3451 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3453 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3455 struct ccw_device *cdev = data;
3458 ret = ccw_device_set_online(cdev);
3460 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3461 dev_name(&cdev->dev), ret);
3465 * Initial attempt at a probe function. this can be simplified once
3466 * the other detection code is gone.
3468 int dasd_generic_probe(struct ccw_device *cdev,
3469 struct dasd_discipline *discipline)
3473 ret = dasd_add_sysfs_files(cdev);
3475 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3476 "dasd_generic_probe: could not add "
3480 cdev->handler = &dasd_int_handler;
3483 * Automatically online either all dasd devices (dasd_autodetect)
3484 * or all devices specified with dasd= parameters during
3487 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3488 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3489 async_schedule(dasd_generic_auto_online, cdev);
3492 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3494 void dasd_generic_free_discipline(struct dasd_device *device)
3496 /* Forget the discipline information. */
3497 if (device->discipline) {
3498 if (device->discipline->uncheck_device)
3499 device->discipline->uncheck_device(device);
3500 module_put(device->discipline->owner);
3501 device->discipline = NULL;
3503 if (device->base_discipline) {
3504 module_put(device->base_discipline->owner);
3505 device->base_discipline = NULL;
3508 EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3511 * This will one day be called from a global not_oper handler.
3512 * It is also used by driver_unregister during module unload.
3514 void dasd_generic_remove(struct ccw_device *cdev)
3516 struct dasd_device *device;
3517 struct dasd_block *block;
3519 cdev->handler = NULL;
3521 device = dasd_device_from_cdev(cdev);
3522 if (IS_ERR(device)) {
3523 dasd_remove_sysfs_files(cdev);
3526 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3527 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3528 /* Already doing offline processing */
3529 dasd_put_device(device);
3530 dasd_remove_sysfs_files(cdev);
3534 * This device is removed unconditionally. Set offline
3535 * flag to prevent dasd_open from opening it while it is
3536 * no quite down yet.
3538 dasd_set_target_state(device, DASD_STATE_NEW);
3539 /* dasd_delete_device destroys the device reference. */
3540 block = device->block;
3541 dasd_delete_device(device);
3543 * life cycle of block is bound to device, so delete it after
3544 * device was safely removed
3547 dasd_free_block(block);
3549 dasd_remove_sysfs_files(cdev);
3551 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3554 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3555 * the device is detected for the first time and is supposed to be used
3556 * or the user has started activation through sysfs.
3558 int dasd_generic_set_online(struct ccw_device *cdev,
3559 struct dasd_discipline *base_discipline)
3561 struct dasd_discipline *discipline;
3562 struct dasd_device *device;
3565 /* first online clears initial online feature flag */
3566 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3567 device = dasd_create_device(cdev);
3569 return PTR_ERR(device);
3571 discipline = base_discipline;
3572 if (device->features & DASD_FEATURE_USEDIAG) {
3573 if (!dasd_diag_discipline_pointer) {
3574 /* Try to load the required module. */
3575 rc = request_module(DASD_DIAG_MOD);
3577 pr_warn("%s Setting the DASD online failed "
3578 "because the required module %s "
3579 "could not be loaded (rc=%d)\n",
3580 dev_name(&cdev->dev), DASD_DIAG_MOD,
3582 dasd_delete_device(device);
3586 /* Module init could have failed, so check again here after
3587 * request_module(). */
3588 if (!dasd_diag_discipline_pointer) {
3589 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3590 dev_name(&cdev->dev));
3591 dasd_delete_device(device);
3594 discipline = dasd_diag_discipline_pointer;
3596 if (!try_module_get(base_discipline->owner)) {
3597 dasd_delete_device(device);
3600 if (!try_module_get(discipline->owner)) {
3601 module_put(base_discipline->owner);
3602 dasd_delete_device(device);
3605 device->base_discipline = base_discipline;
3606 device->discipline = discipline;
3608 /* check_device will allocate block device if necessary */
3609 rc = discipline->check_device(device);
3611 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3612 dev_name(&cdev->dev), discipline->name, rc);
3613 module_put(discipline->owner);
3614 module_put(base_discipline->owner);
3615 dasd_delete_device(device);
3619 dasd_set_target_state(device, DASD_STATE_ONLINE);
3620 if (device->state <= DASD_STATE_KNOWN) {
3621 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3622 dev_name(&cdev->dev));
3624 dasd_set_target_state(device, DASD_STATE_NEW);
3626 dasd_free_block(device->block);
3627 dasd_delete_device(device);
3629 pr_debug("dasd_generic device %s found\n",
3630 dev_name(&cdev->dev));
3632 wait_event(dasd_init_waitq, _wait_for_device(device));
3634 dasd_put_device(device);
3637 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3639 int dasd_generic_set_offline(struct ccw_device *cdev)
3641 struct dasd_device *device;
3642 struct dasd_block *block;
3643 int max_count, open_count, rc;
3644 unsigned long flags;
3647 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3648 device = dasd_device_from_cdev_locked(cdev);
3649 if (IS_ERR(device)) {
3650 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3651 return PTR_ERR(device);
3655 * We must make sure that this device is currently not in use.
3656 * The open_count is increased for every opener, that includes
3657 * the blkdev_get in dasd_scan_partitions. We are only interested
3658 * in the other openers.
3660 if (device->block) {
3661 max_count = device->block->bdev ? 0 : -1;
3662 open_count = atomic_read(&device->block->open_count);
3663 if (open_count > max_count) {
3665 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3666 dev_name(&cdev->dev), open_count);
3668 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3669 dev_name(&cdev->dev));
3676 * Test if the offline processing is already running and exit if so.
3677 * If a safe offline is being processed this could only be a normal
3678 * offline that should be able to overtake the safe offline and
3679 * cancel any I/O we do not want to wait for any longer
3681 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3682 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3683 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3690 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3693 * if safe_offline is called set safe_offline_running flag and
3694 * clear safe_offline so that a call to normal offline
3695 * can overrun safe_offline processing
3697 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3698 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3699 /* need to unlock here to wait for outstanding I/O */
3700 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3702 * If we want to set the device safe offline all IO operations
3703 * should be finished before continuing the offline process
3704 * so sync bdev first and then wait for our queues to become
3707 if (device->block) {
3708 rc = fsync_bdev(device->block->bdev);
3712 dasd_schedule_device_bh(device);
3713 rc = wait_event_interruptible(shutdown_waitq,
3714 _wait_for_empty_queues(device));
3719 * check if a normal offline process overtook the offline
3720 * processing in this case simply do nothing beside returning
3721 * that we got interrupted
3722 * otherwise mark safe offline as not running any longer and
3723 * continue with normal offline
3725 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3726 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3730 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3732 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3734 dasd_set_target_state(device, DASD_STATE_NEW);
3735 /* dasd_delete_device destroys the device reference. */
3736 block = device->block;
3737 dasd_delete_device(device);
3739 * life cycle of block is bound to device, so delete it after
3740 * device was safely removed
3743 dasd_free_block(block);
3748 /* interrupted by signal */
3749 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3750 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3751 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3753 dasd_put_device(device);
3754 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3757 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3759 int dasd_generic_last_path_gone(struct dasd_device *device)
3761 struct dasd_ccw_req *cqr;
3763 dev_warn(&device->cdev->dev, "No operational channel path is left "
3764 "for the device\n");
3765 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3766 /* First of all call extended error reporting. */
3767 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3769 if (device->state < DASD_STATE_BASIC)
3771 /* Device is active. We want to keep it. */
3772 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3773 if ((cqr->status == DASD_CQR_IN_IO) ||
3774 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3775 cqr->status = DASD_CQR_QUEUED;
3778 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3779 dasd_device_clear_timer(device);
3780 dasd_schedule_device_bh(device);
3783 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3785 int dasd_generic_path_operational(struct dasd_device *device)
3787 dev_info(&device->cdev->dev, "A channel path to the device has become "
3789 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3790 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3791 if (device->stopped & DASD_UNRESUMED_PM) {
3792 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3793 dasd_restore_device(device);
3796 dasd_schedule_device_bh(device);
3797 if (device->block) {
3798 dasd_schedule_block_bh(device->block);
3799 if (device->block->request_queue)
3800 blk_mq_run_hw_queues(device->block->request_queue,
3804 if (!device->stopped)
3805 wake_up(&generic_waitq);
3809 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3811 int dasd_generic_notify(struct ccw_device *cdev, int event)
3813 struct dasd_device *device;
3816 device = dasd_device_from_cdev_locked(cdev);
3824 dasd_path_no_path(device);
3825 ret = dasd_generic_last_path_gone(device);
3829 if (dasd_path_get_opm(device))
3830 ret = dasd_generic_path_operational(device);
3833 dasd_put_device(device);
3836 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3838 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3840 struct dasd_device *device;
3841 int chp, oldopm, hpfpm, ifccpm;
3843 device = dasd_device_from_cdev_locked(cdev);
3847 oldopm = dasd_path_get_opm(device);
3848 for (chp = 0; chp < 8; chp++) {
3849 if (path_event[chp] & PE_PATH_GONE) {
3850 dasd_path_notoper(device, chp);
3852 if (path_event[chp] & PE_PATH_AVAILABLE) {
3853 dasd_path_available(device, chp);
3854 dasd_schedule_device_bh(device);
3856 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3857 if (!dasd_path_is_operational(device, chp) &&
3858 !dasd_path_need_verify(device, chp)) {
3860 * we can not establish a pathgroup on an
3861 * unavailable path, so trigger a path
3862 * verification first
3864 dasd_path_available(device, chp);
3865 dasd_schedule_device_bh(device);
3867 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3868 "Pathgroup re-established\n");
3869 if (device->discipline->kick_validate)
3870 device->discipline->kick_validate(device);
3873 hpfpm = dasd_path_get_hpfpm(device);
3874 ifccpm = dasd_path_get_ifccpm(device);
3875 if (!dasd_path_get_opm(device) && hpfpm) {
3877 * device has no operational paths but at least one path is
3878 * disabled due to HPF errors
3879 * disable HPF at all and use the path(s) again
3881 if (device->discipline->disable_hpf)
3882 device->discipline->disable_hpf(device);
3883 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3884 dasd_path_set_tbvpm(device, hpfpm);
3885 dasd_schedule_device_bh(device);
3886 dasd_schedule_requeue(device);
3887 } else if (!dasd_path_get_opm(device) && ifccpm) {
3889 * device has no operational paths but at least one path is
3890 * disabled due to IFCC errors
3891 * trigger path verification on paths with IFCC errors
3893 dasd_path_set_tbvpm(device, ifccpm);
3894 dasd_schedule_device_bh(device);
3896 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
3897 dev_warn(&device->cdev->dev,
3898 "No verified channel paths remain for the device\n");
3899 DBF_DEV_EVENT(DBF_WARNING, device,
3900 "%s", "last verified path gone");
3901 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3902 dasd_device_set_stop_bits(device,
3903 DASD_STOPPED_DC_WAIT);
3905 dasd_put_device(device);
3907 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3909 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3911 if (!dasd_path_get_opm(device) && lpm) {
3912 dasd_path_set_opm(device, lpm);
3913 dasd_generic_path_operational(device);
3915 dasd_path_add_opm(device, lpm);
3918 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3920 void dasd_generic_space_exhaust(struct dasd_device *device,
3921 struct dasd_ccw_req *cqr)
3923 dasd_eer_write(device, NULL, DASD_EER_NOSPC);
3925 if (device->state < DASD_STATE_BASIC)
3928 if (cqr->status == DASD_CQR_IN_IO ||
3929 cqr->status == DASD_CQR_CLEAR_PENDING) {
3930 cqr->status = DASD_CQR_QUEUED;
3933 dasd_device_set_stop_bits(device, DASD_STOPPED_NOSPC);
3934 dasd_device_clear_timer(device);
3935 dasd_schedule_device_bh(device);
3937 EXPORT_SYMBOL_GPL(dasd_generic_space_exhaust);
3939 void dasd_generic_space_avail(struct dasd_device *device)
3941 dev_info(&device->cdev->dev, "Extent pool space is available\n");
3942 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "space available");
3944 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOSPC);
3945 dasd_schedule_device_bh(device);
3947 if (device->block) {
3948 dasd_schedule_block_bh(device->block);
3949 if (device->block->request_queue)
3950 blk_mq_run_hw_queues(device->block->request_queue, true);
3952 if (!device->stopped)
3953 wake_up(&generic_waitq);
3955 EXPORT_SYMBOL_GPL(dasd_generic_space_avail);
3958 * clear active requests and requeue them to block layer if possible
3960 static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3962 struct list_head requeue_queue;
3963 struct dasd_ccw_req *cqr, *n;
3964 struct dasd_ccw_req *refers;
3967 INIT_LIST_HEAD(&requeue_queue);
3968 spin_lock_irq(get_ccwdev_lock(device->cdev));
3970 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3971 /* Check status and move request to flush_queue */
3972 if (cqr->status == DASD_CQR_IN_IO) {
3973 rc = device->discipline->term_IO(cqr);
3975 /* unable to terminate requeust */
3976 dev_err(&device->cdev->dev,
3977 "Unable to terminate request %p "
3978 "on suspend\n", cqr);
3979 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3980 dasd_put_device(device);
3984 list_move_tail(&cqr->devlist, &requeue_queue);
3986 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3988 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3989 wait_event(dasd_flush_wq,
3990 (cqr->status != DASD_CQR_CLEAR_PENDING));
3993 * requeue requests to blocklayer will only work
3994 * for block device requests
3996 if (_dasd_requeue_request(cqr))
3999 /* remove requests from device and block queue */
4000 list_del_init(&cqr->devlist);
4001 while (cqr->refers != NULL) {
4002 refers = cqr->refers;
4003 /* remove the request from the block queue */
4004 list_del(&cqr->blocklist);
4005 /* free the finished erp request */
4006 dasd_free_erp_request(cqr, cqr->memdev);
4011 * _dasd_requeue_request already checked for a valid
4012 * blockdevice, no need to check again
4013 * all erp requests (cqr->refers) have a cqr->block
4014 * pointer copy from the original cqr
4016 list_del_init(&cqr->blocklist);
4017 cqr->block->base->discipline->free_cp(
4018 cqr, (struct request *) cqr->callback_data);
4022 * if requests remain then they are internal request
4023 * and go back to the device queue
4025 if (!list_empty(&requeue_queue)) {
4026 /* move freeze_queue to start of the ccw_queue */
4027 spin_lock_irq(get_ccwdev_lock(device->cdev));
4028 list_splice_tail(&requeue_queue, &device->ccw_queue);
4029 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4031 dasd_schedule_device_bh(device);
4035 static void do_requeue_requests(struct work_struct *work)
4037 struct dasd_device *device = container_of(work, struct dasd_device,
4039 dasd_generic_requeue_all_requests(device);
4040 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
4042 dasd_schedule_block_bh(device->block);
4043 dasd_put_device(device);
4046 void dasd_schedule_requeue(struct dasd_device *device)
4048 dasd_get_device(device);
4049 /* queue call to dasd_reload_device to the kernel event daemon. */
4050 if (!schedule_work(&device->requeue_requests))
4051 dasd_put_device(device);
4053 EXPORT_SYMBOL(dasd_schedule_requeue);
4055 int dasd_generic_pm_freeze(struct ccw_device *cdev)
4057 struct dasd_device *device = dasd_device_from_cdev(cdev);
4060 return PTR_ERR(device);
4062 /* mark device as suspended */
4063 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
4065 if (device->discipline->freeze)
4066 device->discipline->freeze(device);
4068 /* disallow new I/O */
4069 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
4071 return dasd_generic_requeue_all_requests(device);
4073 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
4075 int dasd_generic_restore_device(struct ccw_device *cdev)
4077 struct dasd_device *device = dasd_device_from_cdev(cdev);
4081 return PTR_ERR(device);
4083 /* allow new IO again */
4084 dasd_device_remove_stop_bits(device,
4085 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
4087 dasd_schedule_device_bh(device);
4090 * call discipline restore function
4091 * if device is stopped do nothing e.g. for disconnected devices
4093 if (device->discipline->restore && !(device->stopped))
4094 rc = device->discipline->restore(device);
4095 if (rc || device->stopped)
4097 * if the resume failed for the DASD we put it in
4098 * an UNRESUMED stop state
4100 device->stopped |= DASD_UNRESUMED_PM;
4102 if (device->block) {
4103 dasd_schedule_block_bh(device->block);
4104 if (device->block->request_queue)
4105 blk_mq_run_hw_queues(device->block->request_queue,
4109 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
4110 dasd_put_device(device);
4113 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
4115 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
4116 int rdc_buffer_size,
4119 struct dasd_ccw_req *cqr;
4122 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
4126 /* internal error 13 - Allocating the RDC request failed*/
4127 dev_err(&device->cdev->dev,
4128 "An error occurred in the DASD device driver, "
4129 "reason=%s\n", "13");
4134 ccw->cmd_code = CCW_CMD_RDC;
4135 ccw->cda = (__u32)(addr_t) cqr->data;
4137 ccw->count = rdc_buffer_size;
4138 cqr->startdev = device;
4139 cqr->memdev = device;
4140 cqr->expires = 10*HZ;
4142 cqr->buildclk = get_tod_clock();
4143 cqr->status = DASD_CQR_FILLED;
4148 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
4149 void *rdc_buffer, int rdc_buffer_size)
4152 struct dasd_ccw_req *cqr;
4154 cqr = dasd_generic_build_rdc(device, rdc_buffer_size, magic);
4156 return PTR_ERR(cqr);
4158 ret = dasd_sleep_on(cqr);
4160 memcpy(rdc_buffer, cqr->data, rdc_buffer_size);
4161 dasd_sfree_request(cqr, cqr->memdev);
4164 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
4167 * In command mode and transport mode we need to look for sense
4168 * data in different places. The sense data itself is allways
4169 * an array of 32 bytes, so we can unify the sense data access
4172 char *dasd_get_sense(struct irb *irb)
4174 struct tsb *tsb = NULL;
4177 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4178 if (irb->scsw.tm.tcw)
4179 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4181 if (tsb && tsb->length == 64 && tsb->flags)
4182 switch (tsb->flags & 0x07) {
4183 case 1: /* tsa_iostat */
4184 sense = tsb->tsa.iostat.sense;
4186 case 2: /* tsa_ddpc */
4187 sense = tsb->tsa.ddpc.sense;
4190 /* currently we don't use interrogate data */
4193 } else if (irb->esw.esw0.erw.cons) {
4198 EXPORT_SYMBOL_GPL(dasd_get_sense);
4200 void dasd_generic_shutdown(struct ccw_device *cdev)
4202 struct dasd_device *device;
4204 device = dasd_device_from_cdev(cdev);
4209 dasd_schedule_block_bh(device->block);
4211 dasd_schedule_device_bh(device);
4213 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4215 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4217 static int __init dasd_init(void)
4221 init_waitqueue_head(&dasd_init_waitq);
4222 init_waitqueue_head(&dasd_flush_wq);
4223 init_waitqueue_head(&generic_waitq);
4224 init_waitqueue_head(&shutdown_waitq);
4226 /* register 'common' DASD debug area, used for all DBF_XXX calls */
4227 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
4228 if (dasd_debug_area == NULL) {
4232 debug_register_view(dasd_debug_area, &debug_sprintf_view);
4233 debug_set_level(dasd_debug_area, DBF_WARNING);
4235 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4237 dasd_diag_discipline_pointer = NULL;
4239 dasd_statistics_createroot();
4241 rc = dasd_devmap_init();
4244 rc = dasd_gendisk_init();
4250 rc = dasd_eer_init();
4253 #ifdef CONFIG_PROC_FS
4254 rc = dasd_proc_init();
4261 pr_info("The DASD device driver could not be initialized\n");
4266 module_init(dasd_init);
4267 module_exit(dasd_exit);