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);
1466 * remember the amount of formatted tracks to prevent double format on
1470 cqr->trkcount = atomic_read(&cqr->block->trkcount);
1472 if (cqr->cpmode == 1) {
1473 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1474 (long) cqr, cqr->lpm);
1476 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1477 (long) cqr, cqr->lpm, 0);
1481 cqr->status = DASD_CQR_IN_IO;
1484 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1485 "start_IO: device busy, retry later");
1488 /* -EACCES indicates that the request used only a subset of the
1489 * available paths and all these paths are gone. If the lpm of
1490 * this request was only a subset of the opm (e.g. the ppm) then
1491 * we just do a retry with all available paths.
1492 * If we already use the full opm, something is amiss, and we
1493 * need a full path verification.
1495 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1496 DBF_DEV_EVENT(DBF_WARNING, device,
1497 "start_IO: selected paths gone (%x)",
1499 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1500 cqr->lpm = dasd_path_get_opm(device);
1501 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1502 "start_IO: selected paths gone,"
1503 " retry on all paths");
1505 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1506 "start_IO: all paths in opm gone,"
1507 " do path verification");
1508 dasd_generic_last_path_gone(device);
1509 dasd_path_no_path(device);
1510 dasd_path_set_tbvpm(device,
1511 ccw_device_get_path_mask(
1516 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1517 "start_IO: -ENODEV device gone, retry");
1520 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1521 "start_IO: -EIO device gone, retry");
1524 /* most likely caused in power management context */
1525 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1526 "start_IO: -EINVAL device currently "
1530 /* internal error 11 - unknown rc */
1531 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1532 dev_err(&device->cdev->dev,
1533 "An error occurred in the DASD device driver, "
1534 "reason=%s\n", errorstring);
1541 EXPORT_SYMBOL(dasd_start_IO);
1544 * Timeout function for dasd devices. This is used for different purposes
1545 * 1) missing interrupt handler for normal operation
1546 * 2) delayed start of request where start_IO failed with -EBUSY
1547 * 3) timeout for missing state change interrupts
1548 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1549 * DASD_CQR_QUEUED for 2) and 3).
1551 static void dasd_device_timeout(struct timer_list *t)
1553 unsigned long flags;
1554 struct dasd_device *device;
1556 device = from_timer(device, t, timer);
1557 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1558 /* re-activate request queue */
1559 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1560 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1561 dasd_schedule_device_bh(device);
1565 * Setup timeout for a device in jiffies.
1567 void dasd_device_set_timer(struct dasd_device *device, int expires)
1570 del_timer(&device->timer);
1572 mod_timer(&device->timer, jiffies + expires);
1574 EXPORT_SYMBOL(dasd_device_set_timer);
1577 * Clear timeout for a device.
1579 void dasd_device_clear_timer(struct dasd_device *device)
1581 del_timer(&device->timer);
1583 EXPORT_SYMBOL(dasd_device_clear_timer);
1585 static void dasd_handle_killed_request(struct ccw_device *cdev,
1586 unsigned long intparm)
1588 struct dasd_ccw_req *cqr;
1589 struct dasd_device *device;
1593 cqr = (struct dasd_ccw_req *) intparm;
1594 if (cqr->status != DASD_CQR_IN_IO) {
1595 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1596 "invalid status in handle_killed_request: "
1597 "%02x", cqr->status);
1601 device = dasd_device_from_cdev_locked(cdev);
1602 if (IS_ERR(device)) {
1603 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1604 "unable to get device from cdev");
1608 if (!cqr->startdev ||
1609 device != cqr->startdev ||
1610 strncmp(cqr->startdev->discipline->ebcname,
1611 (char *) &cqr->magic, 4)) {
1612 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1613 "invalid device in request");
1614 dasd_put_device(device);
1618 /* Schedule request to be retried. */
1619 cqr->status = DASD_CQR_QUEUED;
1621 dasd_device_clear_timer(device);
1622 dasd_schedule_device_bh(device);
1623 dasd_put_device(device);
1626 void dasd_generic_handle_state_change(struct dasd_device *device)
1628 /* First of all start sense subsystem status request. */
1629 dasd_eer_snss(device);
1631 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1632 dasd_schedule_device_bh(device);
1633 if (device->block) {
1634 dasd_schedule_block_bh(device->block);
1635 if (device->block->request_queue)
1636 blk_mq_run_hw_queues(device->block->request_queue,
1640 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1642 static int dasd_check_hpf_error(struct irb *irb)
1644 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1645 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1646 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1649 static int dasd_ese_needs_format(struct dasd_block *block, struct irb *irb)
1651 struct dasd_device *device = NULL;
1656 device = block->base;
1657 if (!device || !device->discipline->is_ese)
1659 if (!device->discipline->is_ese(device))
1662 sense = dasd_get_sense(irb);
1666 return !!(sense[1] & SNS1_NO_REC_FOUND) ||
1667 !!(sense[1] & SNS1_FILE_PROTECTED) ||
1668 scsw_cstat(&irb->scsw) == SCHN_STAT_INCORR_LEN;
1671 static int dasd_ese_oos_cond(u8 *sense)
1673 return sense[0] & SNS0_EQUIPMENT_CHECK &&
1674 sense[1] & SNS1_PERM_ERR &&
1675 sense[1] & SNS1_WRITE_INHIBITED &&
1680 * Interrupt handler for "normal" ssch-io based dasd devices.
1682 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1685 struct dasd_ccw_req *cqr, *next, *fcqr;
1686 struct dasd_device *device;
1688 int nrf_suppressed = 0;
1689 int fp_suppressed = 0;
1690 struct request *req;
1694 cqr = (struct dasd_ccw_req *) intparm;
1696 switch (PTR_ERR(irb)) {
1698 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1699 device = cqr->startdev;
1700 cqr->status = DASD_CQR_CLEARED;
1701 dasd_device_clear_timer(device);
1702 wake_up(&dasd_flush_wq);
1703 dasd_schedule_device_bh(device);
1708 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1709 "request timed out\n", __func__);
1712 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1713 "unknown error %ld\n", __func__,
1716 dasd_handle_killed_request(cdev, intparm);
1720 now = get_tod_clock();
1721 /* check for conditions that should be handled immediately */
1723 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1724 scsw_cstat(&irb->scsw) == 0)) {
1726 memcpy(&cqr->irb, irb, sizeof(*irb));
1727 device = dasd_device_from_cdev_locked(cdev);
1730 /* ignore unsolicited interrupts for DIAG discipline */
1731 if (device->discipline == dasd_diag_discipline_pointer) {
1732 dasd_put_device(device);
1737 * In some cases 'File Protected' or 'No Record Found' errors
1738 * might be expected and debug log messages for the
1739 * corresponding interrupts shouldn't be written then.
1740 * Check if either of the according suppress bits is set.
1742 sense = dasd_get_sense(irb);
1744 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1745 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1746 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1747 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1750 * Extent pool probably out-of-space.
1751 * Stop device and check exhaust level.
1753 if (dasd_ese_oos_cond(sense)) {
1754 dasd_generic_space_exhaust(device, cqr);
1755 device->discipline->ext_pool_exhaust(device, cqr);
1756 dasd_put_device(device);
1760 if (!(fp_suppressed || nrf_suppressed))
1761 device->discipline->dump_sense_dbf(device, irb, "int");
1763 if (device->features & DASD_FEATURE_ERPLOG)
1764 device->discipline->dump_sense(device, cqr, irb);
1765 device->discipline->check_for_device_change(device, cqr, irb);
1766 dasd_put_device(device);
1769 /* check for for attention message */
1770 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1771 device = dasd_device_from_cdev_locked(cdev);
1772 if (!IS_ERR(device)) {
1773 device->discipline->check_attention(device,
1774 irb->esw.esw1.lpum);
1775 dasd_put_device(device);
1782 device = (struct dasd_device *) cqr->startdev;
1784 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1785 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1786 "invalid device in request");
1790 if (dasd_ese_needs_format(cqr->block, irb)) {
1791 req = dasd_get_callback_data(cqr);
1793 cqr->status = DASD_CQR_ERROR;
1796 if (rq_data_dir(req) == READ) {
1797 device->discipline->ese_read(cqr, irb);
1798 cqr->status = DASD_CQR_SUCCESS;
1800 dasd_device_clear_timer(device);
1801 dasd_schedule_device_bh(device);
1804 fcqr = device->discipline->ese_format(device, cqr, irb);
1806 if (PTR_ERR(fcqr) == -EINVAL) {
1807 cqr->status = DASD_CQR_ERROR;
1811 * If we can't format now, let the request go
1812 * one extra round. Maybe we can format later.
1814 cqr->status = DASD_CQR_QUEUED;
1815 dasd_schedule_device_bh(device);
1818 fcqr->status = DASD_CQR_QUEUED;
1819 cqr->status = DASD_CQR_QUEUED;
1820 list_add(&fcqr->devlist, &device->ccw_queue);
1821 dasd_schedule_device_bh(device);
1826 /* Check for clear pending */
1827 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1828 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1829 cqr->status = DASD_CQR_CLEARED;
1830 dasd_device_clear_timer(device);
1831 wake_up(&dasd_flush_wq);
1832 dasd_schedule_device_bh(device);
1836 /* check status - the request might have been killed by dyn detach */
1837 if (cqr->status != DASD_CQR_IN_IO) {
1838 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1839 "status %02x", dev_name(&cdev->dev), cqr->status);
1845 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1846 scsw_cstat(&irb->scsw) == 0) {
1847 /* request was completed successfully */
1848 cqr->status = DASD_CQR_SUCCESS;
1850 /* Start first request on queue if possible -> fast_io. */
1851 if (cqr->devlist.next != &device->ccw_queue) {
1852 next = list_entry(cqr->devlist.next,
1853 struct dasd_ccw_req, devlist);
1855 } else { /* error */
1856 /* check for HPF error
1857 * call discipline function to requeue all requests
1858 * and disable HPF accordingly
1860 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1861 device->discipline->handle_hpf_error)
1862 device->discipline->handle_hpf_error(device, irb);
1864 * If we don't want complex ERP for this request, then just
1865 * reset this and retry it in the fastpath
1867 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1869 if (cqr->lpm == dasd_path_get_opm(device))
1870 DBF_DEV_EVENT(DBF_DEBUG, device,
1871 "default ERP in fastpath "
1872 "(%i retries left)",
1874 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1875 cqr->lpm = dasd_path_get_opm(device);
1876 cqr->status = DASD_CQR_QUEUED;
1879 cqr->status = DASD_CQR_ERROR;
1881 if (next && (next->status == DASD_CQR_QUEUED) &&
1882 (!device->stopped)) {
1883 if (device->discipline->start_IO(next) == 0)
1884 expires = next->expires;
1887 dasd_device_set_timer(device, expires);
1889 dasd_device_clear_timer(device);
1890 dasd_schedule_device_bh(device);
1892 EXPORT_SYMBOL(dasd_int_handler);
1894 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1896 struct dasd_device *device;
1898 device = dasd_device_from_cdev_locked(cdev);
1902 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1903 device->state != device->target ||
1904 !device->discipline->check_for_device_change){
1905 dasd_put_device(device);
1908 if (device->discipline->dump_sense_dbf)
1909 device->discipline->dump_sense_dbf(device, irb, "uc");
1910 device->discipline->check_for_device_change(device, NULL, irb);
1911 dasd_put_device(device);
1913 return UC_TODO_RETRY;
1915 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1918 * If we have an error on a dasd_block layer request then we cancel
1919 * and return all further requests from the same dasd_block as well.
1921 static void __dasd_device_recovery(struct dasd_device *device,
1922 struct dasd_ccw_req *ref_cqr)
1924 struct list_head *l, *n;
1925 struct dasd_ccw_req *cqr;
1928 * only requeue request that came from the dasd_block layer
1930 if (!ref_cqr->block)
1933 list_for_each_safe(l, n, &device->ccw_queue) {
1934 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1935 if (cqr->status == DASD_CQR_QUEUED &&
1936 ref_cqr->block == cqr->block) {
1937 cqr->status = DASD_CQR_CLEARED;
1943 * Remove those ccw requests from the queue that need to be returned
1944 * to the upper layer.
1946 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1947 struct list_head *final_queue)
1949 struct list_head *l, *n;
1950 struct dasd_ccw_req *cqr;
1952 /* Process request with final status. */
1953 list_for_each_safe(l, n, &device->ccw_queue) {
1954 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1956 /* Skip any non-final request. */
1957 if (cqr->status == DASD_CQR_QUEUED ||
1958 cqr->status == DASD_CQR_IN_IO ||
1959 cqr->status == DASD_CQR_CLEAR_PENDING)
1961 if (cqr->status == DASD_CQR_ERROR) {
1962 __dasd_device_recovery(device, cqr);
1964 /* Rechain finished requests to final queue */
1965 list_move_tail(&cqr->devlist, final_queue);
1969 static void __dasd_process_cqr(struct dasd_device *device,
1970 struct dasd_ccw_req *cqr)
1972 char errorstring[ERRORLENGTH];
1974 switch (cqr->status) {
1975 case DASD_CQR_SUCCESS:
1976 cqr->status = DASD_CQR_DONE;
1978 case DASD_CQR_ERROR:
1979 cqr->status = DASD_CQR_NEED_ERP;
1981 case DASD_CQR_CLEARED:
1982 cqr->status = DASD_CQR_TERMINATED;
1985 /* internal error 12 - wrong cqr status*/
1986 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1987 dev_err(&device->cdev->dev,
1988 "An error occurred in the DASD device driver, "
1989 "reason=%s\n", errorstring);
1993 cqr->callback(cqr, cqr->callback_data);
1997 * the cqrs from the final queue are returned to the upper layer
1998 * by setting a dasd_block state and calling the callback function
2000 static void __dasd_device_process_final_queue(struct dasd_device *device,
2001 struct list_head *final_queue)
2003 struct list_head *l, *n;
2004 struct dasd_ccw_req *cqr;
2005 struct dasd_block *block;
2007 list_for_each_safe(l, n, final_queue) {
2008 cqr = list_entry(l, struct dasd_ccw_req, devlist);
2009 list_del_init(&cqr->devlist);
2012 __dasd_process_cqr(device, cqr);
2014 spin_lock_bh(&block->queue_lock);
2015 __dasd_process_cqr(device, cqr);
2016 spin_unlock_bh(&block->queue_lock);
2022 * Take a look at the first request on the ccw queue and check
2023 * if it reached its expire time. If so, terminate the IO.
2025 static void __dasd_device_check_expire(struct dasd_device *device)
2027 struct dasd_ccw_req *cqr;
2029 if (list_empty(&device->ccw_queue))
2031 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2032 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
2033 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
2034 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2036 * IO in safe offline processing should not
2037 * run out of retries
2041 if (device->discipline->term_IO(cqr) != 0) {
2042 /* Hmpf, try again in 5 sec */
2043 dev_err(&device->cdev->dev,
2044 "cqr %p timed out (%lus) but cannot be "
2045 "ended, retrying in 5 s\n",
2046 cqr, (cqr->expires/HZ));
2047 cqr->expires += 5*HZ;
2048 dasd_device_set_timer(device, 5*HZ);
2050 dev_err(&device->cdev->dev,
2051 "cqr %p timed out (%lus), %i retries "
2052 "remaining\n", cqr, (cqr->expires/HZ),
2059 * return 1 when device is not eligible for IO
2061 static int __dasd_device_is_unusable(struct dasd_device *device,
2062 struct dasd_ccw_req *cqr)
2064 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM | DASD_STOPPED_NOSPC);
2066 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2067 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2069 * dasd is being set offline
2070 * but it is no safe offline where we have to allow I/O
2074 if (device->stopped) {
2075 if (device->stopped & mask) {
2076 /* stopped and CQR will not change that. */
2079 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2080 /* CQR is not able to change device to
2084 /* CQR required to get device operational. */
2090 * Take a look at the first request on the ccw queue and check
2091 * if it needs to be started.
2093 static void __dasd_device_start_head(struct dasd_device *device)
2095 struct dasd_ccw_req *cqr;
2098 if (list_empty(&device->ccw_queue))
2100 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2101 if (cqr->status != DASD_CQR_QUEUED)
2103 /* if device is not usable return request to upper layer */
2104 if (__dasd_device_is_unusable(device, cqr)) {
2105 cqr->intrc = -EAGAIN;
2106 cqr->status = DASD_CQR_CLEARED;
2107 dasd_schedule_device_bh(device);
2111 rc = device->discipline->start_IO(cqr);
2113 dasd_device_set_timer(device, cqr->expires);
2114 else if (rc == -EACCES) {
2115 dasd_schedule_device_bh(device);
2117 /* Hmpf, try again in 1/2 sec */
2118 dasd_device_set_timer(device, 50);
2121 static void __dasd_device_check_path_events(struct dasd_device *device)
2125 if (!dasd_path_get_tbvpm(device))
2128 if (device->stopped &
2129 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2131 rc = device->discipline->verify_path(device,
2132 dasd_path_get_tbvpm(device));
2134 dasd_device_set_timer(device, 50);
2136 dasd_path_clear_all_verify(device);
2140 * Go through all request on the dasd_device request queue,
2141 * terminate them on the cdev if necessary, and return them to the
2142 * submitting layer via callback.
2144 * Make sure that all 'submitting layers' still exist when
2145 * this function is called!. In other words, when 'device' is a base
2146 * device then all block layer requests must have been removed before
2147 * via dasd_flush_block_queue.
2149 int dasd_flush_device_queue(struct dasd_device *device)
2151 struct dasd_ccw_req *cqr, *n;
2153 struct list_head flush_queue;
2155 INIT_LIST_HEAD(&flush_queue);
2156 spin_lock_irq(get_ccwdev_lock(device->cdev));
2158 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2159 /* Check status and move request to flush_queue */
2160 switch (cqr->status) {
2161 case DASD_CQR_IN_IO:
2162 rc = device->discipline->term_IO(cqr);
2164 /* unable to terminate requeust */
2165 dev_err(&device->cdev->dev,
2166 "Flushing the DASD request queue "
2167 "failed for request %p\n", cqr);
2168 /* stop flush processing */
2172 case DASD_CQR_QUEUED:
2173 cqr->stopclk = get_tod_clock();
2174 cqr->status = DASD_CQR_CLEARED;
2176 default: /* no need to modify the others */
2179 list_move_tail(&cqr->devlist, &flush_queue);
2182 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2184 * After this point all requests must be in state CLEAR_PENDING,
2185 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2186 * one of the others.
2188 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2189 wait_event(dasd_flush_wq,
2190 (cqr->status != DASD_CQR_CLEAR_PENDING));
2192 * Now set each request back to TERMINATED, DONE or NEED_ERP
2193 * and call the callback function of flushed requests
2195 __dasd_device_process_final_queue(device, &flush_queue);
2198 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2201 * Acquire the device lock and process queues for the device.
2203 static void dasd_device_tasklet(unsigned long data)
2205 struct dasd_device *device = (struct dasd_device *) data;
2206 struct list_head final_queue;
2208 atomic_set (&device->tasklet_scheduled, 0);
2209 INIT_LIST_HEAD(&final_queue);
2210 spin_lock_irq(get_ccwdev_lock(device->cdev));
2211 /* Check expire time of first request on the ccw queue. */
2212 __dasd_device_check_expire(device);
2213 /* find final requests on ccw queue */
2214 __dasd_device_process_ccw_queue(device, &final_queue);
2215 __dasd_device_check_path_events(device);
2216 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2217 /* Now call the callback function of requests with final status */
2218 __dasd_device_process_final_queue(device, &final_queue);
2219 spin_lock_irq(get_ccwdev_lock(device->cdev));
2220 /* Now check if the head of the ccw queue needs to be started. */
2221 __dasd_device_start_head(device);
2222 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2223 if (waitqueue_active(&shutdown_waitq))
2224 wake_up(&shutdown_waitq);
2225 dasd_put_device(device);
2229 * Schedules a call to dasd_tasklet over the device tasklet.
2231 void dasd_schedule_device_bh(struct dasd_device *device)
2233 /* Protect against rescheduling. */
2234 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2236 dasd_get_device(device);
2237 tasklet_hi_schedule(&device->tasklet);
2239 EXPORT_SYMBOL(dasd_schedule_device_bh);
2241 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2243 device->stopped |= bits;
2245 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2247 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2249 device->stopped &= ~bits;
2250 if (!device->stopped)
2251 wake_up(&generic_waitq);
2253 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2256 * Queue a request to the head of the device ccw_queue.
2257 * Start the I/O if possible.
2259 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2261 struct dasd_device *device;
2262 unsigned long flags;
2264 device = cqr->startdev;
2265 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2266 cqr->status = DASD_CQR_QUEUED;
2267 list_add(&cqr->devlist, &device->ccw_queue);
2268 /* let the bh start the request to keep them in order */
2269 dasd_schedule_device_bh(device);
2270 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2272 EXPORT_SYMBOL(dasd_add_request_head);
2275 * Queue a request to the tail of the device ccw_queue.
2276 * Start the I/O if possible.
2278 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2280 struct dasd_device *device;
2281 unsigned long flags;
2283 device = cqr->startdev;
2284 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2285 cqr->status = DASD_CQR_QUEUED;
2286 list_add_tail(&cqr->devlist, &device->ccw_queue);
2287 /* let the bh start the request to keep them in order */
2288 dasd_schedule_device_bh(device);
2289 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2291 EXPORT_SYMBOL(dasd_add_request_tail);
2294 * Wakeup helper for the 'sleep_on' functions.
2296 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2298 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2299 cqr->callback_data = DASD_SLEEPON_END_TAG;
2300 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2301 wake_up(&generic_waitq);
2303 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2305 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2307 struct dasd_device *device;
2310 device = cqr->startdev;
2311 spin_lock_irq(get_ccwdev_lock(device->cdev));
2312 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2313 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2318 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2320 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2322 struct dasd_device *device;
2323 dasd_erp_fn_t erp_fn;
2325 if (cqr->status == DASD_CQR_FILLED)
2327 device = cqr->startdev;
2328 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2329 if (cqr->status == DASD_CQR_TERMINATED) {
2330 device->discipline->handle_terminated_request(cqr);
2333 if (cqr->status == DASD_CQR_NEED_ERP) {
2334 erp_fn = device->discipline->erp_action(cqr);
2338 if (cqr->status == DASD_CQR_FAILED)
2339 dasd_log_sense(cqr, &cqr->irb);
2341 __dasd_process_erp(device, cqr);
2348 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2350 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2351 if (cqr->refers) /* erp is not done yet */
2353 return ((cqr->status != DASD_CQR_DONE) &&
2354 (cqr->status != DASD_CQR_FAILED));
2356 return (cqr->status == DASD_CQR_FILLED);
2359 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2361 struct dasd_device *device;
2363 struct list_head ccw_queue;
2364 struct dasd_ccw_req *cqr;
2366 INIT_LIST_HEAD(&ccw_queue);
2367 maincqr->status = DASD_CQR_FILLED;
2368 device = maincqr->startdev;
2369 list_add(&maincqr->blocklist, &ccw_queue);
2370 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2371 cqr = list_first_entry(&ccw_queue,
2372 struct dasd_ccw_req, blocklist)) {
2374 if (__dasd_sleep_on_erp(cqr))
2376 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2378 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2379 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2380 cqr->status = DASD_CQR_FAILED;
2381 cqr->intrc = -EPERM;
2384 /* Non-temporary stop condition will trigger fail fast */
2385 if (device->stopped & ~DASD_STOPPED_PENDING &&
2386 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2387 (!dasd_eer_enabled(device))) {
2388 cqr->status = DASD_CQR_FAILED;
2389 cqr->intrc = -ENOLINK;
2393 * Don't try to start requests if device is in
2394 * offline processing, it might wait forever
2396 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2397 cqr->status = DASD_CQR_FAILED;
2398 cqr->intrc = -ENODEV;
2402 * Don't try to start requests if device is stopped
2403 * except path verification requests
2405 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2406 if (interruptible) {
2407 rc = wait_event_interruptible(
2408 generic_waitq, !(device->stopped));
2409 if (rc == -ERESTARTSYS) {
2410 cqr->status = DASD_CQR_FAILED;
2411 maincqr->intrc = rc;
2415 wait_event(generic_waitq, !(device->stopped));
2418 cqr->callback = dasd_wakeup_cb;
2420 cqr->callback_data = DASD_SLEEPON_START_TAG;
2421 dasd_add_request_tail(cqr);
2422 if (interruptible) {
2423 rc = wait_event_interruptible(
2424 generic_waitq, _wait_for_wakeup(cqr));
2425 if (rc == -ERESTARTSYS) {
2426 dasd_cancel_req(cqr);
2427 /* wait (non-interruptible) for final status */
2428 wait_event(generic_waitq,
2429 _wait_for_wakeup(cqr));
2430 cqr->status = DASD_CQR_FAILED;
2431 maincqr->intrc = rc;
2435 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2438 maincqr->endclk = get_tod_clock();
2439 if ((maincqr->status != DASD_CQR_DONE) &&
2440 (maincqr->intrc != -ERESTARTSYS))
2441 dasd_log_sense(maincqr, &maincqr->irb);
2442 if (maincqr->status == DASD_CQR_DONE)
2444 else if (maincqr->intrc)
2445 rc = maincqr->intrc;
2451 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2453 struct dasd_ccw_req *cqr;
2455 list_for_each_entry(cqr, ccw_queue, blocklist) {
2456 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2463 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2465 struct dasd_device *device;
2466 struct dasd_ccw_req *cqr, *n;
2471 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2472 device = cqr->startdev;
2473 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2476 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2477 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2478 cqr->status = DASD_CQR_FAILED;
2479 cqr->intrc = -EPERM;
2482 /*Non-temporary stop condition will trigger fail fast*/
2483 if (device->stopped & ~DASD_STOPPED_PENDING &&
2484 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2485 !dasd_eer_enabled(device)) {
2486 cqr->status = DASD_CQR_FAILED;
2487 cqr->intrc = -EAGAIN;
2491 /*Don't try to start requests if device is stopped*/
2492 if (interruptible) {
2493 rc = wait_event_interruptible(
2494 generic_waitq, !device->stopped);
2495 if (rc == -ERESTARTSYS) {
2496 cqr->status = DASD_CQR_FAILED;
2501 wait_event(generic_waitq, !(device->stopped));
2504 cqr->callback = dasd_wakeup_cb;
2505 cqr->callback_data = DASD_SLEEPON_START_TAG;
2506 dasd_add_request_tail(cqr);
2509 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2512 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2514 * In some cases the 'File Protected' or 'Incorrect Length'
2515 * error might be expected and error recovery would be
2516 * unnecessary in these cases. Check if the according suppress
2519 sense = dasd_get_sense(&cqr->irb);
2520 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2521 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2523 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2524 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2528 * for alias devices simplify error recovery and
2529 * return to upper layer
2530 * do not skip ERP requests
2532 if (cqr->startdev != cqr->basedev && !cqr->refers &&
2533 (cqr->status == DASD_CQR_TERMINATED ||
2534 cqr->status == DASD_CQR_NEED_ERP))
2537 /* normal recovery for basedev IO */
2538 if (__dasd_sleep_on_erp(cqr))
2539 /* handle erp first */
2547 * Queue a request to the tail of the device ccw_queue and wait for
2550 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2552 return _dasd_sleep_on(cqr, 0);
2554 EXPORT_SYMBOL(dasd_sleep_on);
2557 * Start requests from a ccw_queue and wait for their completion.
2559 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2561 return _dasd_sleep_on_queue(ccw_queue, 0);
2563 EXPORT_SYMBOL(dasd_sleep_on_queue);
2566 * Start requests from a ccw_queue and wait interruptible for their completion.
2568 int dasd_sleep_on_queue_interruptible(struct list_head *ccw_queue)
2570 return _dasd_sleep_on_queue(ccw_queue, 1);
2572 EXPORT_SYMBOL(dasd_sleep_on_queue_interruptible);
2575 * Queue a request to the tail of the device ccw_queue and wait
2576 * interruptible for it's completion.
2578 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2580 return _dasd_sleep_on(cqr, 1);
2582 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2585 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2586 * for eckd devices) the currently running request has to be terminated
2587 * and be put back to status queued, before the special request is added
2588 * to the head of the queue. Then the special request is waited on normally.
2590 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2592 struct dasd_ccw_req *cqr;
2595 if (list_empty(&device->ccw_queue))
2597 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2598 rc = device->discipline->term_IO(cqr);
2601 * CQR terminated because a more important request is pending.
2602 * Undo decreasing of retry counter because this is
2603 * not an error case.
2609 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2611 struct dasd_device *device;
2614 device = cqr->startdev;
2615 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2616 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2617 cqr->status = DASD_CQR_FAILED;
2618 cqr->intrc = -EPERM;
2621 spin_lock_irq(get_ccwdev_lock(device->cdev));
2622 rc = _dasd_term_running_cqr(device);
2624 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2627 cqr->callback = dasd_wakeup_cb;
2628 cqr->callback_data = DASD_SLEEPON_START_TAG;
2629 cqr->status = DASD_CQR_QUEUED;
2631 * add new request as second
2632 * first the terminated cqr needs to be finished
2634 list_add(&cqr->devlist, device->ccw_queue.next);
2636 /* let the bh start the request to keep them in order */
2637 dasd_schedule_device_bh(device);
2639 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2641 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2643 if (cqr->status == DASD_CQR_DONE)
2645 else if (cqr->intrc)
2651 dasd_schedule_device_bh(device);
2653 dasd_schedule_block_bh(device->block);
2657 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2660 * Cancels a request that was started with dasd_sleep_on_req.
2661 * This is useful to timeout requests. The request will be
2662 * terminated if it is currently in i/o.
2663 * Returns 0 if request termination was successful
2664 * negative error code if termination failed
2665 * Cancellation of a request is an asynchronous operation! The calling
2666 * function has to wait until the request is properly returned via callback.
2668 static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
2670 struct dasd_device *device = cqr->startdev;
2673 switch (cqr->status) {
2674 case DASD_CQR_QUEUED:
2675 /* request was not started - just set to cleared */
2676 cqr->status = DASD_CQR_CLEARED;
2678 case DASD_CQR_IN_IO:
2679 /* request in IO - terminate IO and release again */
2680 rc = device->discipline->term_IO(cqr);
2682 dev_err(&device->cdev->dev,
2683 "Cancelling request %p failed with rc=%d\n",
2686 cqr->stopclk = get_tod_clock();
2689 default: /* already finished or clear pending - do nothing */
2692 dasd_schedule_device_bh(device);
2696 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2698 struct dasd_device *device = cqr->startdev;
2699 unsigned long flags;
2702 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2703 rc = __dasd_cancel_req(cqr);
2704 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2709 * SECTION: Operations of the dasd_block layer.
2713 * Timeout function for dasd_block. This is used when the block layer
2714 * is waiting for something that may not come reliably, (e.g. a state
2717 static void dasd_block_timeout(struct timer_list *t)
2719 unsigned long flags;
2720 struct dasd_block *block;
2722 block = from_timer(block, t, timer);
2723 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2724 /* re-activate request queue */
2725 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2726 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2727 dasd_schedule_block_bh(block);
2728 blk_mq_run_hw_queues(block->request_queue, true);
2732 * Setup timeout for a dasd_block in jiffies.
2734 void dasd_block_set_timer(struct dasd_block *block, int expires)
2737 del_timer(&block->timer);
2739 mod_timer(&block->timer, jiffies + expires);
2741 EXPORT_SYMBOL(dasd_block_set_timer);
2744 * Clear timeout for a dasd_block.
2746 void dasd_block_clear_timer(struct dasd_block *block)
2748 del_timer(&block->timer);
2750 EXPORT_SYMBOL(dasd_block_clear_timer);
2753 * Process finished error recovery ccw.
2755 static void __dasd_process_erp(struct dasd_device *device,
2756 struct dasd_ccw_req *cqr)
2758 dasd_erp_fn_t erp_fn;
2760 if (cqr->status == DASD_CQR_DONE)
2761 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2763 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2764 erp_fn = device->discipline->erp_postaction(cqr);
2768 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2770 struct request *req;
2771 blk_status_t error = BLK_STS_OK;
2772 unsigned int proc_bytes;
2775 req = (struct request *) cqr->callback_data;
2776 dasd_profile_end(cqr->block, cqr, req);
2778 proc_bytes = cqr->proc_bytes;
2779 status = cqr->block->base->discipline->free_cp(cqr, req);
2781 error = errno_to_blk_status(status);
2782 else if (status == 0) {
2783 switch (cqr->intrc) {
2785 error = BLK_STS_NEXUS;
2788 error = BLK_STS_TRANSPORT;
2791 error = BLK_STS_TIMEOUT;
2794 error = BLK_STS_IOERR;
2800 * We need to take care for ETIMEDOUT errors here since the
2801 * complete callback does not get called in this case.
2802 * Take care of all errors here and avoid additional code to
2803 * transfer the error value to the complete callback.
2806 blk_mq_end_request(req, error);
2807 blk_mq_run_hw_queues(req->q, true);
2810 * Partial completed requests can happen with ESE devices.
2811 * During read we might have gotten a NRF error and have to
2812 * complete a request partially.
2815 blk_update_request(req, BLK_STS_OK, proc_bytes);
2816 blk_mq_requeue_request(req, true);
2817 } else if (likely(!blk_should_fake_timeout(req->q))) {
2818 blk_mq_complete_request(req);
2824 * Process ccw request queue.
2826 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2827 struct list_head *final_queue)
2829 struct list_head *l, *n;
2830 struct dasd_ccw_req *cqr;
2831 dasd_erp_fn_t erp_fn;
2832 unsigned long flags;
2833 struct dasd_device *base = block->base;
2836 /* Process request with final status. */
2837 list_for_each_safe(l, n, &block->ccw_queue) {
2838 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2839 if (cqr->status != DASD_CQR_DONE &&
2840 cqr->status != DASD_CQR_FAILED &&
2841 cqr->status != DASD_CQR_NEED_ERP &&
2842 cqr->status != DASD_CQR_TERMINATED)
2845 if (cqr->status == DASD_CQR_TERMINATED) {
2846 base->discipline->handle_terminated_request(cqr);
2850 /* Process requests that may be recovered */
2851 if (cqr->status == DASD_CQR_NEED_ERP) {
2852 erp_fn = base->discipline->erp_action(cqr);
2853 if (IS_ERR(erp_fn(cqr)))
2858 /* log sense for fatal error */
2859 if (cqr->status == DASD_CQR_FAILED) {
2860 dasd_log_sense(cqr, &cqr->irb);
2863 /* First of all call extended error reporting. */
2864 if (dasd_eer_enabled(base) &&
2865 cqr->status == DASD_CQR_FAILED) {
2866 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2868 /* restart request */
2869 cqr->status = DASD_CQR_FILLED;
2871 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2872 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2873 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2878 /* Process finished ERP request. */
2880 __dasd_process_erp(base, cqr);
2884 /* Rechain finished requests to final queue */
2885 cqr->endclk = get_tod_clock();
2886 list_move_tail(&cqr->blocklist, final_queue);
2890 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2892 dasd_schedule_block_bh(cqr->block);
2895 static void __dasd_block_start_head(struct dasd_block *block)
2897 struct dasd_ccw_req *cqr;
2899 if (list_empty(&block->ccw_queue))
2901 /* We allways begin with the first requests on the queue, as some
2902 * of previously started requests have to be enqueued on a
2903 * dasd_device again for error recovery.
2905 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2906 if (cqr->status != DASD_CQR_FILLED)
2908 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2909 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2910 cqr->status = DASD_CQR_FAILED;
2911 cqr->intrc = -EPERM;
2912 dasd_schedule_block_bh(block);
2915 /* Non-temporary stop condition will trigger fail fast */
2916 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2917 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2918 (!dasd_eer_enabled(block->base))) {
2919 cqr->status = DASD_CQR_FAILED;
2920 cqr->intrc = -ENOLINK;
2921 dasd_schedule_block_bh(block);
2924 /* Don't try to start requests if device is stopped */
2925 if (block->base->stopped)
2928 /* just a fail safe check, should not happen */
2930 cqr->startdev = block->base;
2932 /* make sure that the requests we submit find their way back */
2933 cqr->callback = dasd_return_cqr_cb;
2935 dasd_add_request_tail(cqr);
2940 * Central dasd_block layer routine. Takes requests from the generic
2941 * block layer request queue, creates ccw requests, enqueues them on
2942 * a dasd_device and processes ccw requests that have been returned.
2944 static void dasd_block_tasklet(unsigned long data)
2946 struct dasd_block *block = (struct dasd_block *) data;
2947 struct list_head final_queue;
2948 struct list_head *l, *n;
2949 struct dasd_ccw_req *cqr;
2950 struct dasd_queue *dq;
2952 atomic_set(&block->tasklet_scheduled, 0);
2953 INIT_LIST_HEAD(&final_queue);
2954 spin_lock_irq(&block->queue_lock);
2955 /* Finish off requests on ccw queue */
2956 __dasd_process_block_ccw_queue(block, &final_queue);
2957 spin_unlock_irq(&block->queue_lock);
2959 /* Now call the callback function of requests with final status */
2960 list_for_each_safe(l, n, &final_queue) {
2961 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2963 spin_lock_irq(&dq->lock);
2964 list_del_init(&cqr->blocklist);
2965 __dasd_cleanup_cqr(cqr);
2966 spin_unlock_irq(&dq->lock);
2969 spin_lock_irq(&block->queue_lock);
2970 /* Now check if the head of the ccw queue needs to be started. */
2971 __dasd_block_start_head(block);
2972 spin_unlock_irq(&block->queue_lock);
2974 if (waitqueue_active(&shutdown_waitq))
2975 wake_up(&shutdown_waitq);
2976 dasd_put_device(block->base);
2979 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2981 wake_up(&dasd_flush_wq);
2985 * Requeue a request back to the block request queue
2986 * only works for block requests
2988 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2990 struct dasd_block *block = cqr->block;
2991 struct request *req;
2996 * If the request is an ERP request there is nothing to requeue.
2997 * This will be done with the remaining original request.
3001 spin_lock_irq(&cqr->dq->lock);
3002 req = (struct request *) cqr->callback_data;
3003 blk_mq_requeue_request(req, false);
3004 spin_unlock_irq(&cqr->dq->lock);
3010 * Go through all request on the dasd_block request queue, cancel them
3011 * on the respective dasd_device, and return them to the generic
3014 static int dasd_flush_block_queue(struct dasd_block *block)
3016 struct dasd_ccw_req *cqr, *n;
3018 struct list_head flush_queue;
3019 unsigned long flags;
3021 INIT_LIST_HEAD(&flush_queue);
3022 spin_lock_bh(&block->queue_lock);
3025 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
3026 /* if this request currently owned by a dasd_device cancel it */
3027 if (cqr->status >= DASD_CQR_QUEUED)
3028 rc = dasd_cancel_req(cqr);
3031 /* Rechain request (including erp chain) so it won't be
3032 * touched by the dasd_block_tasklet anymore.
3033 * Replace the callback so we notice when the request
3034 * is returned from the dasd_device layer.
3036 cqr->callback = _dasd_wake_block_flush_cb;
3037 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
3038 list_move_tail(&cqr->blocklist, &flush_queue);
3040 /* moved more than one request - need to restart */
3043 spin_unlock_bh(&block->queue_lock);
3044 /* Now call the callback function of flushed requests */
3046 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
3047 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
3048 /* Process finished ERP request. */
3050 spin_lock_bh(&block->queue_lock);
3051 __dasd_process_erp(block->base, cqr);
3052 spin_unlock_bh(&block->queue_lock);
3053 /* restart list_for_xx loop since dasd_process_erp
3054 * might remove multiple elements */
3057 /* call the callback function */
3058 spin_lock_irqsave(&cqr->dq->lock, flags);
3059 cqr->endclk = get_tod_clock();
3060 list_del_init(&cqr->blocklist);
3061 __dasd_cleanup_cqr(cqr);
3062 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3068 * Schedules a call to dasd_tasklet over the device tasklet.
3070 void dasd_schedule_block_bh(struct dasd_block *block)
3072 /* Protect against rescheduling. */
3073 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3075 /* life cycle of block is bound to it's base device */
3076 dasd_get_device(block->base);
3077 tasklet_hi_schedule(&block->tasklet);
3079 EXPORT_SYMBOL(dasd_schedule_block_bh);
3083 * SECTION: external block device operations
3084 * (request queue handling, open, release, etc.)
3088 * Dasd request queue function. Called from ll_rw_blk.c
3090 static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
3091 const struct blk_mq_queue_data *qd)
3093 struct dasd_block *block = hctx->queue->queuedata;
3094 struct dasd_queue *dq = hctx->driver_data;
3095 struct request *req = qd->rq;
3096 struct dasd_device *basedev;
3097 struct dasd_ccw_req *cqr;
3098 blk_status_t rc = BLK_STS_OK;
3100 basedev = block->base;
3101 spin_lock_irq(&dq->lock);
3102 if (basedev->state < DASD_STATE_READY ||
3103 test_bit(DASD_FLAG_OFFLINE, &basedev->flags)) {
3104 DBF_DEV_EVENT(DBF_ERR, basedev,
3105 "device not ready for request %p", req);
3111 * if device is stopped do not fetch new requests
3112 * except failfast is active which will let requests fail
3113 * immediately in __dasd_block_start_head()
3115 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
3116 DBF_DEV_EVENT(DBF_ERR, basedev,
3117 "device stopped request %p", req);
3118 rc = BLK_STS_RESOURCE;
3122 if (basedev->features & DASD_FEATURE_READONLY &&
3123 rq_data_dir(req) == WRITE) {
3124 DBF_DEV_EVENT(DBF_ERR, basedev,
3125 "Rejecting write request %p", req);
3130 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
3131 (basedev->features & DASD_FEATURE_FAILFAST ||
3132 blk_noretry_request(req))) {
3133 DBF_DEV_EVENT(DBF_ERR, basedev,
3134 "Rejecting failfast request %p", req);
3139 cqr = basedev->discipline->build_cp(basedev, block, req);
3141 if (PTR_ERR(cqr) == -EBUSY ||
3142 PTR_ERR(cqr) == -ENOMEM ||
3143 PTR_ERR(cqr) == -EAGAIN) {
3144 rc = BLK_STS_RESOURCE;
3147 DBF_DEV_EVENT(DBF_ERR, basedev,
3148 "CCW creation failed (rc=%ld) on request %p",
3154 * Note: callback is set to dasd_return_cqr_cb in
3155 * __dasd_block_start_head to cover erp requests as well
3157 cqr->callback_data = req;
3158 cqr->status = DASD_CQR_FILLED;
3161 blk_mq_start_request(req);
3162 spin_lock(&block->queue_lock);
3163 list_add_tail(&cqr->blocklist, &block->ccw_queue);
3164 INIT_LIST_HEAD(&cqr->devlist);
3165 dasd_profile_start(block, cqr, req);
3166 dasd_schedule_block_bh(block);
3167 spin_unlock(&block->queue_lock);
3170 spin_unlock_irq(&dq->lock);
3175 * Block timeout callback, called from the block layer
3178 * BLK_EH_RESET_TIMER if the request should be left running
3179 * BLK_EH_DONE if the request is handled or terminated
3182 enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
3184 struct dasd_block *block = req->q->queuedata;
3185 struct dasd_device *device;
3186 struct dasd_ccw_req *cqr;
3187 unsigned long flags;
3190 cqr = blk_mq_rq_to_pdu(req);
3194 spin_lock_irqsave(&cqr->dq->lock, flags);
3195 device = cqr->startdev ? cqr->startdev : block->base;
3196 if (!device->blk_timeout) {
3197 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3198 return BLK_EH_RESET_TIMER;
3200 DBF_DEV_EVENT(DBF_WARNING, device,
3201 " dasd_times_out cqr %p status %x",
3204 spin_lock(&block->queue_lock);
3205 spin_lock(get_ccwdev_lock(device->cdev));
3207 cqr->intrc = -ETIMEDOUT;
3208 if (cqr->status >= DASD_CQR_QUEUED) {
3209 rc = __dasd_cancel_req(cqr);
3210 } else if (cqr->status == DASD_CQR_FILLED ||
3211 cqr->status == DASD_CQR_NEED_ERP) {
3212 cqr->status = DASD_CQR_TERMINATED;
3213 } else if (cqr->status == DASD_CQR_IN_ERP) {
3214 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3216 list_for_each_entry_safe(searchcqr, nextcqr,
3217 &block->ccw_queue, blocklist) {
3219 while (tmpcqr->refers)
3220 tmpcqr = tmpcqr->refers;
3223 /* searchcqr is an ERP request for cqr */
3224 searchcqr->retries = -1;
3225 searchcqr->intrc = -ETIMEDOUT;
3226 if (searchcqr->status >= DASD_CQR_QUEUED) {
3227 rc = __dasd_cancel_req(searchcqr);
3228 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3229 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3230 searchcqr->status = DASD_CQR_TERMINATED;
3232 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3234 * Shouldn't happen; most recent ERP
3235 * request is at the front of queue
3242 spin_unlock(get_ccwdev_lock(device->cdev));
3243 dasd_schedule_block_bh(block);
3244 spin_unlock(&block->queue_lock);
3245 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3247 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
3250 static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3253 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3258 spin_lock_init(&dq->lock);
3259 hctx->driver_data = dq;
3264 static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3266 kfree(hctx->driver_data);
3267 hctx->driver_data = NULL;
3270 static void dasd_request_done(struct request *req)
3272 blk_mq_end_request(req, 0);
3273 blk_mq_run_hw_queues(req->q, true);
3276 static struct blk_mq_ops dasd_mq_ops = {
3277 .queue_rq = do_dasd_request,
3278 .complete = dasd_request_done,
3279 .timeout = dasd_times_out,
3280 .init_hctx = dasd_init_hctx,
3281 .exit_hctx = dasd_exit_hctx,
3285 * Allocate and initialize request queue and default I/O scheduler.
3287 static int dasd_alloc_queue(struct dasd_block *block)
3291 block->tag_set.ops = &dasd_mq_ops;
3292 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
3293 block->tag_set.nr_hw_queues = nr_hw_queues;
3294 block->tag_set.queue_depth = queue_depth;
3295 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
3296 block->tag_set.numa_node = NUMA_NO_NODE;
3298 rc = blk_mq_alloc_tag_set(&block->tag_set);
3302 block->request_queue = blk_mq_init_queue(&block->tag_set);
3303 if (IS_ERR(block->request_queue))
3304 return PTR_ERR(block->request_queue);
3306 block->request_queue->queuedata = block;
3312 * Deactivate and free request queue.
3314 static void dasd_free_queue(struct dasd_block *block)
3316 if (block->request_queue) {
3317 blk_cleanup_queue(block->request_queue);
3318 blk_mq_free_tag_set(&block->tag_set);
3319 block->request_queue = NULL;
3323 static int dasd_open(struct block_device *bdev, fmode_t mode)
3325 struct dasd_device *base;
3328 base = dasd_device_from_gendisk(bdev->bd_disk);
3332 atomic_inc(&base->block->open_count);
3333 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3338 if (!try_module_get(base->discipline->owner)) {
3343 if (dasd_probeonly) {
3344 dev_info(&base->cdev->dev,
3345 "Accessing the DASD failed because it is in "
3346 "probeonly mode\n");
3351 if (base->state <= DASD_STATE_BASIC) {
3352 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3353 " Cannot open unrecognized device");
3358 if ((mode & FMODE_WRITE) &&
3359 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3360 (base->features & DASD_FEATURE_READONLY))) {
3365 dasd_put_device(base);
3369 module_put(base->discipline->owner);
3371 atomic_dec(&base->block->open_count);
3372 dasd_put_device(base);
3376 static void dasd_release(struct gendisk *disk, fmode_t mode)
3378 struct dasd_device *base = dasd_device_from_gendisk(disk);
3380 atomic_dec(&base->block->open_count);
3381 module_put(base->discipline->owner);
3382 dasd_put_device(base);
3387 * Return disk geometry.
3389 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3391 struct dasd_device *base;
3393 base = dasd_device_from_gendisk(bdev->bd_disk);
3397 if (!base->discipline ||
3398 !base->discipline->fill_geometry) {
3399 dasd_put_device(base);
3402 base->discipline->fill_geometry(base->block, geo);
3403 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3404 dasd_put_device(base);
3408 const struct block_device_operations
3409 dasd_device_operations = {
3410 .owner = THIS_MODULE,
3412 .release = dasd_release,
3413 .ioctl = dasd_ioctl,
3414 .compat_ioctl = dasd_ioctl,
3415 .getgeo = dasd_getgeo,
3418 /*******************************************************************************
3419 * end of block device operations
3425 #ifdef CONFIG_PROC_FS
3429 kmem_cache_destroy(dasd_page_cache);
3430 dasd_page_cache = NULL;
3431 dasd_gendisk_exit();
3433 if (dasd_debug_area != NULL) {
3434 debug_unregister(dasd_debug_area);
3435 dasd_debug_area = NULL;
3437 dasd_statistics_removeroot();
3441 * SECTION: common functions for ccw_driver use
3445 * Is the device read-only?
3446 * Note that this function does not report the setting of the
3447 * readonly device attribute, but how it is configured in z/VM.
3449 int dasd_device_is_ro(struct dasd_device *device)
3451 struct ccw_dev_id dev_id;
3452 struct diag210 diag_data;
3457 ccw_device_get_id(device->cdev, &dev_id);
3458 memset(&diag_data, 0, sizeof(diag_data));
3459 diag_data.vrdcdvno = dev_id.devno;
3460 diag_data.vrdclen = sizeof(diag_data);
3461 rc = diag210(&diag_data);
3462 if (rc == 0 || rc == 2) {
3463 return diag_data.vrdcvfla & 0x80;
3465 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3470 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3472 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3474 struct ccw_device *cdev = data;
3477 ret = ccw_device_set_online(cdev);
3479 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3480 dev_name(&cdev->dev), ret);
3484 * Initial attempt at a probe function. this can be simplified once
3485 * the other detection code is gone.
3487 int dasd_generic_probe(struct ccw_device *cdev,
3488 struct dasd_discipline *discipline)
3492 ret = dasd_add_sysfs_files(cdev);
3494 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3495 "dasd_generic_probe: could not add "
3499 cdev->handler = &dasd_int_handler;
3502 * Automatically online either all dasd devices (dasd_autodetect)
3503 * or all devices specified with dasd= parameters during
3506 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3507 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3508 async_schedule(dasd_generic_auto_online, cdev);
3511 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3513 void dasd_generic_free_discipline(struct dasd_device *device)
3515 /* Forget the discipline information. */
3516 if (device->discipline) {
3517 if (device->discipline->uncheck_device)
3518 device->discipline->uncheck_device(device);
3519 module_put(device->discipline->owner);
3520 device->discipline = NULL;
3522 if (device->base_discipline) {
3523 module_put(device->base_discipline->owner);
3524 device->base_discipline = NULL;
3527 EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3530 * This will one day be called from a global not_oper handler.
3531 * It is also used by driver_unregister during module unload.
3533 void dasd_generic_remove(struct ccw_device *cdev)
3535 struct dasd_device *device;
3536 struct dasd_block *block;
3538 device = dasd_device_from_cdev(cdev);
3539 if (IS_ERR(device)) {
3540 dasd_remove_sysfs_files(cdev);
3543 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3544 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3545 /* Already doing offline processing */
3546 dasd_put_device(device);
3547 dasd_remove_sysfs_files(cdev);
3551 * This device is removed unconditionally. Set offline
3552 * flag to prevent dasd_open from opening it while it is
3553 * no quite down yet.
3555 dasd_set_target_state(device, DASD_STATE_NEW);
3556 cdev->handler = NULL;
3557 /* dasd_delete_device destroys the device reference. */
3558 block = device->block;
3559 dasd_delete_device(device);
3561 * life cycle of block is bound to device, so delete it after
3562 * device was safely removed
3565 dasd_free_block(block);
3567 dasd_remove_sysfs_files(cdev);
3569 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3572 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3573 * the device is detected for the first time and is supposed to be used
3574 * or the user has started activation through sysfs.
3576 int dasd_generic_set_online(struct ccw_device *cdev,
3577 struct dasd_discipline *base_discipline)
3579 struct dasd_discipline *discipline;
3580 struct dasd_device *device;
3583 /* first online clears initial online feature flag */
3584 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3585 device = dasd_create_device(cdev);
3587 return PTR_ERR(device);
3589 discipline = base_discipline;
3590 if (device->features & DASD_FEATURE_USEDIAG) {
3591 if (!dasd_diag_discipline_pointer) {
3592 /* Try to load the required module. */
3593 rc = request_module(DASD_DIAG_MOD);
3595 pr_warn("%s Setting the DASD online failed "
3596 "because the required module %s "
3597 "could not be loaded (rc=%d)\n",
3598 dev_name(&cdev->dev), DASD_DIAG_MOD,
3600 dasd_delete_device(device);
3604 /* Module init could have failed, so check again here after
3605 * request_module(). */
3606 if (!dasd_diag_discipline_pointer) {
3607 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3608 dev_name(&cdev->dev));
3609 dasd_delete_device(device);
3612 discipline = dasd_diag_discipline_pointer;
3614 if (!try_module_get(base_discipline->owner)) {
3615 dasd_delete_device(device);
3618 if (!try_module_get(discipline->owner)) {
3619 module_put(base_discipline->owner);
3620 dasd_delete_device(device);
3623 device->base_discipline = base_discipline;
3624 device->discipline = discipline;
3626 /* check_device will allocate block device if necessary */
3627 rc = discipline->check_device(device);
3629 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3630 dev_name(&cdev->dev), discipline->name, rc);
3631 module_put(discipline->owner);
3632 module_put(base_discipline->owner);
3633 dasd_delete_device(device);
3637 dasd_set_target_state(device, DASD_STATE_ONLINE);
3638 if (device->state <= DASD_STATE_KNOWN) {
3639 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3640 dev_name(&cdev->dev));
3642 dasd_set_target_state(device, DASD_STATE_NEW);
3644 dasd_free_block(device->block);
3645 dasd_delete_device(device);
3647 pr_debug("dasd_generic device %s found\n",
3648 dev_name(&cdev->dev));
3650 wait_event(dasd_init_waitq, _wait_for_device(device));
3652 dasd_put_device(device);
3655 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3657 int dasd_generic_set_offline(struct ccw_device *cdev)
3659 struct dasd_device *device;
3660 struct dasd_block *block;
3661 int max_count, open_count, rc;
3662 unsigned long flags;
3665 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3666 device = dasd_device_from_cdev_locked(cdev);
3667 if (IS_ERR(device)) {
3668 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3669 return PTR_ERR(device);
3673 * We must make sure that this device is currently not in use.
3674 * The open_count is increased for every opener, that includes
3675 * the blkdev_get in dasd_scan_partitions. We are only interested
3676 * in the other openers.
3678 if (device->block) {
3679 max_count = device->block->bdev ? 0 : -1;
3680 open_count = atomic_read(&device->block->open_count);
3681 if (open_count > max_count) {
3683 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3684 dev_name(&cdev->dev), open_count);
3686 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3687 dev_name(&cdev->dev));
3694 * Test if the offline processing is already running and exit if so.
3695 * If a safe offline is being processed this could only be a normal
3696 * offline that should be able to overtake the safe offline and
3697 * cancel any I/O we do not want to wait for any longer
3699 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3700 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3701 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3708 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3711 * if safe_offline is called set safe_offline_running flag and
3712 * clear safe_offline so that a call to normal offline
3713 * can overrun safe_offline processing
3715 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3716 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3717 /* need to unlock here to wait for outstanding I/O */
3718 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3720 * If we want to set the device safe offline all IO operations
3721 * should be finished before continuing the offline process
3722 * so sync bdev first and then wait for our queues to become
3725 if (device->block) {
3726 rc = fsync_bdev(device->block->bdev);
3730 dasd_schedule_device_bh(device);
3731 rc = wait_event_interruptible(shutdown_waitq,
3732 _wait_for_empty_queues(device));
3737 * check if a normal offline process overtook the offline
3738 * processing in this case simply do nothing beside returning
3739 * that we got interrupted
3740 * otherwise mark safe offline as not running any longer and
3741 * continue with normal offline
3743 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3744 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3748 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3750 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3752 dasd_set_target_state(device, DASD_STATE_NEW);
3753 /* dasd_delete_device destroys the device reference. */
3754 block = device->block;
3755 dasd_delete_device(device);
3757 * life cycle of block is bound to device, so delete it after
3758 * device was safely removed
3761 dasd_free_block(block);
3766 /* interrupted by signal */
3767 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3768 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3769 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3771 dasd_put_device(device);
3772 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3775 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3777 int dasd_generic_last_path_gone(struct dasd_device *device)
3779 struct dasd_ccw_req *cqr;
3781 dev_warn(&device->cdev->dev, "No operational channel path is left "
3782 "for the device\n");
3783 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3784 /* First of all call extended error reporting. */
3785 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3787 if (device->state < DASD_STATE_BASIC)
3789 /* Device is active. We want to keep it. */
3790 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3791 if ((cqr->status == DASD_CQR_IN_IO) ||
3792 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3793 cqr->status = DASD_CQR_QUEUED;
3796 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3797 dasd_device_clear_timer(device);
3798 dasd_schedule_device_bh(device);
3801 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3803 int dasd_generic_path_operational(struct dasd_device *device)
3805 dev_info(&device->cdev->dev, "A channel path to the device has become "
3807 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3808 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3809 if (device->stopped & DASD_UNRESUMED_PM) {
3810 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3811 dasd_restore_device(device);
3814 dasd_schedule_device_bh(device);
3815 if (device->block) {
3816 dasd_schedule_block_bh(device->block);
3817 if (device->block->request_queue)
3818 blk_mq_run_hw_queues(device->block->request_queue,
3822 if (!device->stopped)
3823 wake_up(&generic_waitq);
3827 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3829 int dasd_generic_notify(struct ccw_device *cdev, int event)
3831 struct dasd_device *device;
3834 device = dasd_device_from_cdev_locked(cdev);
3842 dasd_path_no_path(device);
3843 ret = dasd_generic_last_path_gone(device);
3847 if (dasd_path_get_opm(device))
3848 ret = dasd_generic_path_operational(device);
3851 dasd_put_device(device);
3854 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3856 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3858 struct dasd_device *device;
3859 int chp, oldopm, hpfpm, ifccpm;
3861 device = dasd_device_from_cdev_locked(cdev);
3865 oldopm = dasd_path_get_opm(device);
3866 for (chp = 0; chp < 8; chp++) {
3867 if (path_event[chp] & PE_PATH_GONE) {
3868 dasd_path_notoper(device, chp);
3870 if (path_event[chp] & PE_PATH_AVAILABLE) {
3871 dasd_path_available(device, chp);
3872 dasd_schedule_device_bh(device);
3874 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3875 if (!dasd_path_is_operational(device, chp) &&
3876 !dasd_path_need_verify(device, chp)) {
3878 * we can not establish a pathgroup on an
3879 * unavailable path, so trigger a path
3880 * verification first
3882 dasd_path_available(device, chp);
3883 dasd_schedule_device_bh(device);
3885 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3886 "Pathgroup re-established\n");
3887 if (device->discipline->kick_validate)
3888 device->discipline->kick_validate(device);
3891 hpfpm = dasd_path_get_hpfpm(device);
3892 ifccpm = dasd_path_get_ifccpm(device);
3893 if (!dasd_path_get_opm(device) && hpfpm) {
3895 * device has no operational paths but at least one path is
3896 * disabled due to HPF errors
3897 * disable HPF at all and use the path(s) again
3899 if (device->discipline->disable_hpf)
3900 device->discipline->disable_hpf(device);
3901 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3902 dasd_path_set_tbvpm(device, hpfpm);
3903 dasd_schedule_device_bh(device);
3904 dasd_schedule_requeue(device);
3905 } else if (!dasd_path_get_opm(device) && ifccpm) {
3907 * device has no operational paths but at least one path is
3908 * disabled due to IFCC errors
3909 * trigger path verification on paths with IFCC errors
3911 dasd_path_set_tbvpm(device, ifccpm);
3912 dasd_schedule_device_bh(device);
3914 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
3915 dev_warn(&device->cdev->dev,
3916 "No verified channel paths remain for the device\n");
3917 DBF_DEV_EVENT(DBF_WARNING, device,
3918 "%s", "last verified path gone");
3919 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3920 dasd_device_set_stop_bits(device,
3921 DASD_STOPPED_DC_WAIT);
3923 dasd_put_device(device);
3925 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3927 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3929 if (!dasd_path_get_opm(device) && lpm) {
3930 dasd_path_set_opm(device, lpm);
3931 dasd_generic_path_operational(device);
3933 dasd_path_add_opm(device, lpm);
3936 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3938 void dasd_generic_space_exhaust(struct dasd_device *device,
3939 struct dasd_ccw_req *cqr)
3941 dasd_eer_write(device, NULL, DASD_EER_NOSPC);
3943 if (device->state < DASD_STATE_BASIC)
3946 if (cqr->status == DASD_CQR_IN_IO ||
3947 cqr->status == DASD_CQR_CLEAR_PENDING) {
3948 cqr->status = DASD_CQR_QUEUED;
3951 dasd_device_set_stop_bits(device, DASD_STOPPED_NOSPC);
3952 dasd_device_clear_timer(device);
3953 dasd_schedule_device_bh(device);
3955 EXPORT_SYMBOL_GPL(dasd_generic_space_exhaust);
3957 void dasd_generic_space_avail(struct dasd_device *device)
3959 dev_info(&device->cdev->dev, "Extent pool space is available\n");
3960 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "space available");
3962 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOSPC);
3963 dasd_schedule_device_bh(device);
3965 if (device->block) {
3966 dasd_schedule_block_bh(device->block);
3967 if (device->block->request_queue)
3968 blk_mq_run_hw_queues(device->block->request_queue, true);
3970 if (!device->stopped)
3971 wake_up(&generic_waitq);
3973 EXPORT_SYMBOL_GPL(dasd_generic_space_avail);
3976 * clear active requests and requeue them to block layer if possible
3978 static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3980 struct list_head requeue_queue;
3981 struct dasd_ccw_req *cqr, *n;
3982 struct dasd_ccw_req *refers;
3985 INIT_LIST_HEAD(&requeue_queue);
3986 spin_lock_irq(get_ccwdev_lock(device->cdev));
3988 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3989 /* Check status and move request to flush_queue */
3990 if (cqr->status == DASD_CQR_IN_IO) {
3991 rc = device->discipline->term_IO(cqr);
3993 /* unable to terminate requeust */
3994 dev_err(&device->cdev->dev,
3995 "Unable to terminate request %p "
3996 "on suspend\n", cqr);
3997 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3998 dasd_put_device(device);
4002 list_move_tail(&cqr->devlist, &requeue_queue);
4004 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4006 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
4007 wait_event(dasd_flush_wq,
4008 (cqr->status != DASD_CQR_CLEAR_PENDING));
4011 * requeue requests to blocklayer will only work
4012 * for block device requests
4014 if (_dasd_requeue_request(cqr))
4017 /* remove requests from device and block queue */
4018 list_del_init(&cqr->devlist);
4019 while (cqr->refers != NULL) {
4020 refers = cqr->refers;
4021 /* remove the request from the block queue */
4022 list_del(&cqr->blocklist);
4023 /* free the finished erp request */
4024 dasd_free_erp_request(cqr, cqr->memdev);
4029 * _dasd_requeue_request already checked for a valid
4030 * blockdevice, no need to check again
4031 * all erp requests (cqr->refers) have a cqr->block
4032 * pointer copy from the original cqr
4034 list_del_init(&cqr->blocklist);
4035 cqr->block->base->discipline->free_cp(
4036 cqr, (struct request *) cqr->callback_data);
4040 * if requests remain then they are internal request
4041 * and go back to the device queue
4043 if (!list_empty(&requeue_queue)) {
4044 /* move freeze_queue to start of the ccw_queue */
4045 spin_lock_irq(get_ccwdev_lock(device->cdev));
4046 list_splice_tail(&requeue_queue, &device->ccw_queue);
4047 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4049 dasd_schedule_device_bh(device);
4053 static void do_requeue_requests(struct work_struct *work)
4055 struct dasd_device *device = container_of(work, struct dasd_device,
4057 dasd_generic_requeue_all_requests(device);
4058 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
4060 dasd_schedule_block_bh(device->block);
4061 dasd_put_device(device);
4064 void dasd_schedule_requeue(struct dasd_device *device)
4066 dasd_get_device(device);
4067 /* queue call to dasd_reload_device to the kernel event daemon. */
4068 if (!schedule_work(&device->requeue_requests))
4069 dasd_put_device(device);
4071 EXPORT_SYMBOL(dasd_schedule_requeue);
4073 int dasd_generic_pm_freeze(struct ccw_device *cdev)
4075 struct dasd_device *device = dasd_device_from_cdev(cdev);
4078 return PTR_ERR(device);
4080 /* mark device as suspended */
4081 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
4083 if (device->discipline->freeze)
4084 device->discipline->freeze(device);
4086 /* disallow new I/O */
4087 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
4089 return dasd_generic_requeue_all_requests(device);
4091 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
4093 int dasd_generic_restore_device(struct ccw_device *cdev)
4095 struct dasd_device *device = dasd_device_from_cdev(cdev);
4099 return PTR_ERR(device);
4101 /* allow new IO again */
4102 dasd_device_remove_stop_bits(device,
4103 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
4105 dasd_schedule_device_bh(device);
4108 * call discipline restore function
4109 * if device is stopped do nothing e.g. for disconnected devices
4111 if (device->discipline->restore && !(device->stopped))
4112 rc = device->discipline->restore(device);
4113 if (rc || device->stopped)
4115 * if the resume failed for the DASD we put it in
4116 * an UNRESUMED stop state
4118 device->stopped |= DASD_UNRESUMED_PM;
4120 if (device->block) {
4121 dasd_schedule_block_bh(device->block);
4122 if (device->block->request_queue)
4123 blk_mq_run_hw_queues(device->block->request_queue,
4127 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
4128 dasd_put_device(device);
4131 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
4133 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
4134 int rdc_buffer_size,
4137 struct dasd_ccw_req *cqr;
4140 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
4144 /* internal error 13 - Allocating the RDC request failed*/
4145 dev_err(&device->cdev->dev,
4146 "An error occurred in the DASD device driver, "
4147 "reason=%s\n", "13");
4152 ccw->cmd_code = CCW_CMD_RDC;
4153 ccw->cda = (__u32)(addr_t) cqr->data;
4155 ccw->count = rdc_buffer_size;
4156 cqr->startdev = device;
4157 cqr->memdev = device;
4158 cqr->expires = 10*HZ;
4160 cqr->buildclk = get_tod_clock();
4161 cqr->status = DASD_CQR_FILLED;
4166 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
4167 void *rdc_buffer, int rdc_buffer_size)
4170 struct dasd_ccw_req *cqr;
4172 cqr = dasd_generic_build_rdc(device, rdc_buffer_size, magic);
4174 return PTR_ERR(cqr);
4176 ret = dasd_sleep_on(cqr);
4178 memcpy(rdc_buffer, cqr->data, rdc_buffer_size);
4179 dasd_sfree_request(cqr, cqr->memdev);
4182 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
4185 * In command mode and transport mode we need to look for sense
4186 * data in different places. The sense data itself is allways
4187 * an array of 32 bytes, so we can unify the sense data access
4190 char *dasd_get_sense(struct irb *irb)
4192 struct tsb *tsb = NULL;
4195 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4196 if (irb->scsw.tm.tcw)
4197 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4199 if (tsb && tsb->length == 64 && tsb->flags)
4200 switch (tsb->flags & 0x07) {
4201 case 1: /* tsa_iostat */
4202 sense = tsb->tsa.iostat.sense;
4204 case 2: /* tsa_ddpc */
4205 sense = tsb->tsa.ddpc.sense;
4208 /* currently we don't use interrogate data */
4211 } else if (irb->esw.esw0.erw.cons) {
4216 EXPORT_SYMBOL_GPL(dasd_get_sense);
4218 void dasd_generic_shutdown(struct ccw_device *cdev)
4220 struct dasd_device *device;
4222 device = dasd_device_from_cdev(cdev);
4227 dasd_schedule_block_bh(device->block);
4229 dasd_schedule_device_bh(device);
4231 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4233 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4235 static int __init dasd_init(void)
4239 init_waitqueue_head(&dasd_init_waitq);
4240 init_waitqueue_head(&dasd_flush_wq);
4241 init_waitqueue_head(&generic_waitq);
4242 init_waitqueue_head(&shutdown_waitq);
4244 /* register 'common' DASD debug area, used for all DBF_XXX calls */
4245 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
4246 if (dasd_debug_area == NULL) {
4250 debug_register_view(dasd_debug_area, &debug_sprintf_view);
4251 debug_set_level(dasd_debug_area, DBF_WARNING);
4253 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4255 dasd_diag_discipline_pointer = NULL;
4257 dasd_statistics_createroot();
4259 rc = dasd_devmap_init();
4262 rc = dasd_gendisk_init();
4268 rc = dasd_eer_init();
4271 #ifdef CONFIG_PROC_FS
4272 rc = dasd_proc_init();
4279 pr_info("The DASD device driver could not be initialized\n");
4284 module_init(dasd_init);
4285 module_exit(dasd_exit);