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 timer_setup(&block->timer, dasd_block_timeout, 0);
182 spin_lock_init(&block->profile.lock);
186 EXPORT_SYMBOL_GPL(dasd_alloc_block);
189 * Free memory of a device structure.
191 void dasd_free_block(struct dasd_block *block)
195 EXPORT_SYMBOL_GPL(dasd_free_block);
198 * Make a new device known to the system.
200 static int dasd_state_new_to_known(struct dasd_device *device)
205 * As long as the device is not in state DASD_STATE_NEW we want to
206 * keep the reference count > 0.
208 dasd_get_device(device);
211 rc = dasd_alloc_queue(device->block);
213 dasd_put_device(device);
217 device->state = DASD_STATE_KNOWN;
222 * Let the system forget about a device.
224 static int dasd_state_known_to_new(struct dasd_device *device)
226 /* Disable extended error reporting for this device. */
227 dasd_eer_disable(device);
228 device->state = DASD_STATE_NEW;
231 dasd_free_queue(device->block);
233 /* Give up reference we took in dasd_state_new_to_known. */
234 dasd_put_device(device);
238 static struct dentry *dasd_debugfs_setup(const char *name,
239 struct dentry *base_dentry)
245 pde = debugfs_create_dir(name, base_dentry);
246 if (!pde || IS_ERR(pde))
252 * Request the irq line for the device.
254 static int dasd_state_known_to_basic(struct dasd_device *device)
256 struct dasd_block *block = device->block;
259 /* Allocate and register gendisk structure. */
261 rc = dasd_gendisk_alloc(block);
264 block->debugfs_dentry =
265 dasd_debugfs_setup(block->gdp->disk_name,
266 dasd_debugfs_root_entry);
267 dasd_profile_init(&block->profile, block->debugfs_dentry);
268 if (dasd_global_profile_level == DASD_PROFILE_ON)
269 dasd_profile_on(&device->block->profile);
271 device->debugfs_dentry =
272 dasd_debugfs_setup(dev_name(&device->cdev->dev),
273 dasd_debugfs_root_entry);
274 dasd_profile_init(&device->profile, device->debugfs_dentry);
275 dasd_hosts_init(device->debugfs_dentry, device);
277 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
278 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
280 debug_register_view(device->debug_area, &debug_sprintf_view);
281 debug_set_level(device->debug_area, DBF_WARNING);
282 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
284 device->state = DASD_STATE_BASIC;
290 * Release the irq line for the device. Terminate any running i/o.
292 static int dasd_state_basic_to_known(struct dasd_device *device)
296 if (device->discipline->basic_to_known) {
297 rc = device->discipline->basic_to_known(device);
303 dasd_profile_exit(&device->block->profile);
304 debugfs_remove(device->block->debugfs_dentry);
305 dasd_gendisk_free(device->block);
306 dasd_block_clear_timer(device->block);
308 rc = dasd_flush_device_queue(device);
311 dasd_device_clear_timer(device);
312 dasd_profile_exit(&device->profile);
313 dasd_hosts_exit(device);
314 debugfs_remove(device->debugfs_dentry);
315 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
316 if (device->debug_area != NULL) {
317 debug_unregister(device->debug_area);
318 device->debug_area = NULL;
320 device->state = DASD_STATE_KNOWN;
325 * Do the initial analysis. The do_analysis function may return
326 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
327 * until the discipline decides to continue the startup sequence
328 * by calling the function dasd_change_state. The eckd disciplines
329 * uses this to start a ccw that detects the format. The completion
330 * interrupt for this detection ccw uses the kernel event daemon to
331 * trigger the call to dasd_change_state. All this is done in the
332 * discipline code, see dasd_eckd.c.
333 * After the analysis ccw is done (do_analysis returned 0) the block
335 * In case the analysis returns an error, the device setup is stopped
336 * (a fake disk was already added to allow formatting).
338 static int dasd_state_basic_to_ready(struct dasd_device *device)
341 struct dasd_block *block;
342 struct gendisk *disk;
345 block = device->block;
346 /* make disk known with correct capacity */
348 if (block->base->discipline->do_analysis != NULL)
349 rc = block->base->discipline->do_analysis(block);
352 device->state = DASD_STATE_UNFMT;
353 disk = device->block->gdp;
354 kobject_uevent(&disk_to_dev(disk)->kobj,
360 if (device->discipline->setup_blk_queue)
361 device->discipline->setup_blk_queue(block);
362 set_capacity(block->gdp,
363 block->blocks << block->s2b_shift);
364 device->state = DASD_STATE_READY;
365 rc = dasd_scan_partitions(block);
367 device->state = DASD_STATE_BASIC;
371 device->state = DASD_STATE_READY;
374 if (device->discipline->basic_to_ready)
375 rc = device->discipline->basic_to_ready(device);
380 int _wait_for_empty_queues(struct dasd_device *device)
383 return list_empty(&device->ccw_queue) &&
384 list_empty(&device->block->ccw_queue);
386 return list_empty(&device->ccw_queue);
390 * Remove device from block device layer. Destroy dirty buffers.
391 * Forget format information. Check if the target level is basic
392 * and if it is create fake disk for formatting.
394 static int dasd_state_ready_to_basic(struct dasd_device *device)
398 device->state = DASD_STATE_BASIC;
400 struct dasd_block *block = device->block;
401 rc = dasd_flush_block_queue(block);
403 device->state = DASD_STATE_READY;
406 dasd_destroy_partitions(block);
409 block->s2b_shift = 0;
417 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
419 device->state = DASD_STATE_BASIC;
424 * Make the device online and schedule the bottom half to start
425 * the requeueing of requests from the linux request queue to the
429 dasd_state_ready_to_online(struct dasd_device * device)
431 struct gendisk *disk;
432 struct disk_part_iter piter;
433 struct hd_struct *part;
435 device->state = DASD_STATE_ONLINE;
437 dasd_schedule_block_bh(device->block);
438 if ((device->features & DASD_FEATURE_USERAW)) {
439 disk = device->block->gdp;
440 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
443 disk = device->block->bdev->bd_disk;
444 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
445 while ((part = disk_part_iter_next(&piter)))
446 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
447 disk_part_iter_exit(&piter);
453 * Stop the requeueing of requests again.
455 static int dasd_state_online_to_ready(struct dasd_device *device)
458 struct gendisk *disk;
459 struct disk_part_iter piter;
460 struct hd_struct *part;
462 if (device->discipline->online_to_ready) {
463 rc = device->discipline->online_to_ready(device);
468 device->state = DASD_STATE_READY;
469 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
470 disk = device->block->bdev->bd_disk;
471 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
472 while ((part = disk_part_iter_next(&piter)))
473 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
474 disk_part_iter_exit(&piter);
480 * Device startup state changes.
482 static int dasd_increase_state(struct dasd_device *device)
487 if (device->state == DASD_STATE_NEW &&
488 device->target >= DASD_STATE_KNOWN)
489 rc = dasd_state_new_to_known(device);
492 device->state == DASD_STATE_KNOWN &&
493 device->target >= DASD_STATE_BASIC)
494 rc = dasd_state_known_to_basic(device);
497 device->state == DASD_STATE_BASIC &&
498 device->target >= DASD_STATE_READY)
499 rc = dasd_state_basic_to_ready(device);
502 device->state == DASD_STATE_UNFMT &&
503 device->target > DASD_STATE_UNFMT)
507 device->state == DASD_STATE_READY &&
508 device->target >= DASD_STATE_ONLINE)
509 rc = dasd_state_ready_to_online(device);
515 * Device shutdown state changes.
517 static int dasd_decrease_state(struct dasd_device *device)
522 if (device->state == DASD_STATE_ONLINE &&
523 device->target <= DASD_STATE_READY)
524 rc = dasd_state_online_to_ready(device);
527 device->state == DASD_STATE_READY &&
528 device->target <= DASD_STATE_BASIC)
529 rc = dasd_state_ready_to_basic(device);
532 device->state == DASD_STATE_UNFMT &&
533 device->target <= DASD_STATE_BASIC)
534 rc = dasd_state_unfmt_to_basic(device);
537 device->state == DASD_STATE_BASIC &&
538 device->target <= DASD_STATE_KNOWN)
539 rc = dasd_state_basic_to_known(device);
542 device->state == DASD_STATE_KNOWN &&
543 device->target <= DASD_STATE_NEW)
544 rc = dasd_state_known_to_new(device);
550 * This is the main startup/shutdown routine.
552 static void dasd_change_state(struct dasd_device *device)
556 if (device->state == device->target)
557 /* Already where we want to go today... */
559 if (device->state < device->target)
560 rc = dasd_increase_state(device);
562 rc = dasd_decrease_state(device);
566 device->target = device->state;
568 /* let user-space know that the device status changed */
569 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
571 if (device->state == device->target)
572 wake_up(&dasd_init_waitq);
576 * Kick starter for devices that did not complete the startup/shutdown
577 * procedure or were sleeping because of a pending state.
578 * dasd_kick_device will schedule a call do do_kick_device to the kernel
581 static void do_kick_device(struct work_struct *work)
583 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
584 mutex_lock(&device->state_mutex);
585 dasd_change_state(device);
586 mutex_unlock(&device->state_mutex);
587 dasd_schedule_device_bh(device);
588 dasd_put_device(device);
591 void dasd_kick_device(struct dasd_device *device)
593 dasd_get_device(device);
594 /* queue call to dasd_kick_device to the kernel event daemon. */
595 if (!schedule_work(&device->kick_work))
596 dasd_put_device(device);
598 EXPORT_SYMBOL(dasd_kick_device);
601 * dasd_reload_device will schedule a call do do_reload_device to the kernel
604 static void do_reload_device(struct work_struct *work)
606 struct dasd_device *device = container_of(work, struct dasd_device,
608 device->discipline->reload(device);
609 dasd_put_device(device);
612 void dasd_reload_device(struct dasd_device *device)
614 dasd_get_device(device);
615 /* queue call to dasd_reload_device to the kernel event daemon. */
616 if (!schedule_work(&device->reload_device))
617 dasd_put_device(device);
619 EXPORT_SYMBOL(dasd_reload_device);
622 * dasd_restore_device will schedule a call do do_restore_device to the kernel
625 static void do_restore_device(struct work_struct *work)
627 struct dasd_device *device = container_of(work, struct dasd_device,
629 device->cdev->drv->restore(device->cdev);
630 dasd_put_device(device);
633 void dasd_restore_device(struct dasd_device *device)
635 dasd_get_device(device);
636 /* queue call to dasd_restore_device to the kernel event daemon. */
637 if (!schedule_work(&device->restore_device))
638 dasd_put_device(device);
642 * Set the target state for a device and starts the state change.
644 void dasd_set_target_state(struct dasd_device *device, int target)
646 dasd_get_device(device);
647 mutex_lock(&device->state_mutex);
648 /* If we are in probeonly mode stop at DASD_STATE_READY. */
649 if (dasd_probeonly && target > DASD_STATE_READY)
650 target = DASD_STATE_READY;
651 if (device->target != target) {
652 if (device->state == target)
653 wake_up(&dasd_init_waitq);
654 device->target = target;
656 if (device->state != device->target)
657 dasd_change_state(device);
658 mutex_unlock(&device->state_mutex);
659 dasd_put_device(device);
661 EXPORT_SYMBOL(dasd_set_target_state);
664 * Enable devices with device numbers in [from..to].
666 static inline int _wait_for_device(struct dasd_device *device)
668 return (device->state == device->target);
671 void dasd_enable_device(struct dasd_device *device)
673 dasd_set_target_state(device, DASD_STATE_ONLINE);
674 if (device->state <= DASD_STATE_KNOWN)
675 /* No discipline for device found. */
676 dasd_set_target_state(device, DASD_STATE_NEW);
677 /* Now wait for the devices to come up. */
678 wait_event(dasd_init_waitq, _wait_for_device(device));
680 dasd_reload_device(device);
681 if (device->discipline->kick_validate)
682 device->discipline->kick_validate(device);
684 EXPORT_SYMBOL(dasd_enable_device);
687 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
690 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
692 #ifdef CONFIG_DASD_PROFILE
693 struct dasd_profile dasd_global_profile = {
694 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
696 static struct dentry *dasd_debugfs_global_entry;
699 * Add profiling information for cqr before execution.
701 static void dasd_profile_start(struct dasd_block *block,
702 struct dasd_ccw_req *cqr,
706 unsigned int counter;
707 struct dasd_device *device;
709 /* count the length of the chanq for statistics */
711 if (dasd_global_profile_level || block->profile.data)
712 list_for_each(l, &block->ccw_queue)
716 spin_lock(&dasd_global_profile.lock);
717 if (dasd_global_profile.data) {
718 dasd_global_profile.data->dasd_io_nr_req[counter]++;
719 if (rq_data_dir(req) == READ)
720 dasd_global_profile.data->dasd_read_nr_req[counter]++;
722 spin_unlock(&dasd_global_profile.lock);
724 spin_lock(&block->profile.lock);
725 if (block->profile.data) {
726 block->profile.data->dasd_io_nr_req[counter]++;
727 if (rq_data_dir(req) == READ)
728 block->profile.data->dasd_read_nr_req[counter]++;
730 spin_unlock(&block->profile.lock);
733 * We count the request for the start device, even though it may run on
734 * some other device due to error recovery. This way we make sure that
735 * we count each request only once.
737 device = cqr->startdev;
738 if (device->profile.data) {
739 counter = 1; /* request is not yet queued on the start device */
740 list_for_each(l, &device->ccw_queue)
744 spin_lock(&device->profile.lock);
745 if (device->profile.data) {
746 device->profile.data->dasd_io_nr_req[counter]++;
747 if (rq_data_dir(req) == READ)
748 device->profile.data->dasd_read_nr_req[counter]++;
750 spin_unlock(&device->profile.lock);
754 * Add profiling information for cqr after execution.
757 #define dasd_profile_counter(value, index) \
759 for (index = 0; index < 31 && value >> (2+index); index++) \
763 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
776 /* in case of an overflow, reset the whole profile */
777 if (data->dasd_io_reqs == UINT_MAX) {
778 memset(data, 0, sizeof(*data));
779 ktime_get_real_ts64(&data->starttod);
781 data->dasd_io_reqs++;
782 data->dasd_io_sects += sectors;
784 data->dasd_io_alias++;
788 data->dasd_io_secs[sectors_ind]++;
789 data->dasd_io_times[tottime_ind]++;
790 data->dasd_io_timps[tottimeps_ind]++;
791 data->dasd_io_time1[strtime_ind]++;
792 data->dasd_io_time2[irqtime_ind]++;
793 data->dasd_io_time2ps[irqtimeps_ind]++;
794 data->dasd_io_time3[endtime_ind]++;
797 data->dasd_read_reqs++;
798 data->dasd_read_sects += sectors;
800 data->dasd_read_alias++;
802 data->dasd_read_tpm++;
803 data->dasd_read_secs[sectors_ind]++;
804 data->dasd_read_times[tottime_ind]++;
805 data->dasd_read_time1[strtime_ind]++;
806 data->dasd_read_time2[irqtime_ind]++;
807 data->dasd_read_time3[endtime_ind]++;
811 static void dasd_profile_end(struct dasd_block *block,
812 struct dasd_ccw_req *cqr,
815 unsigned long strtime, irqtime, endtime, tottime;
816 unsigned long tottimeps, sectors;
817 struct dasd_device *device;
818 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
819 int irqtime_ind, irqtimeps_ind, endtime_ind;
820 struct dasd_profile_info *data;
822 device = cqr->startdev;
823 if (!(dasd_global_profile_level ||
824 block->profile.data ||
825 device->profile.data))
828 sectors = blk_rq_sectors(req);
829 if (!cqr->buildclk || !cqr->startclk ||
830 !cqr->stopclk || !cqr->endclk ||
834 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
835 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
836 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
837 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
838 tottimeps = tottime / sectors;
840 dasd_profile_counter(sectors, sectors_ind);
841 dasd_profile_counter(tottime, tottime_ind);
842 dasd_profile_counter(tottimeps, tottimeps_ind);
843 dasd_profile_counter(strtime, strtime_ind);
844 dasd_profile_counter(irqtime, irqtime_ind);
845 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
846 dasd_profile_counter(endtime, endtime_ind);
848 spin_lock(&dasd_global_profile.lock);
849 if (dasd_global_profile.data) {
850 data = dasd_global_profile.data;
851 data->dasd_sum_times += tottime;
852 data->dasd_sum_time_str += strtime;
853 data->dasd_sum_time_irq += irqtime;
854 data->dasd_sum_time_end += endtime;
855 dasd_profile_end_add_data(dasd_global_profile.data,
856 cqr->startdev != block->base,
858 rq_data_dir(req) == READ,
859 sectors, sectors_ind, tottime_ind,
860 tottimeps_ind, strtime_ind,
861 irqtime_ind, irqtimeps_ind,
864 spin_unlock(&dasd_global_profile.lock);
866 spin_lock(&block->profile.lock);
867 if (block->profile.data) {
868 data = block->profile.data;
869 data->dasd_sum_times += tottime;
870 data->dasd_sum_time_str += strtime;
871 data->dasd_sum_time_irq += irqtime;
872 data->dasd_sum_time_end += endtime;
873 dasd_profile_end_add_data(block->profile.data,
874 cqr->startdev != block->base,
876 rq_data_dir(req) == READ,
877 sectors, sectors_ind, tottime_ind,
878 tottimeps_ind, strtime_ind,
879 irqtime_ind, irqtimeps_ind,
882 spin_unlock(&block->profile.lock);
884 spin_lock(&device->profile.lock);
885 if (device->profile.data) {
886 data = device->profile.data;
887 data->dasd_sum_times += tottime;
888 data->dasd_sum_time_str += strtime;
889 data->dasd_sum_time_irq += irqtime;
890 data->dasd_sum_time_end += endtime;
891 dasd_profile_end_add_data(device->profile.data,
892 cqr->startdev != block->base,
894 rq_data_dir(req) == READ,
895 sectors, sectors_ind, tottime_ind,
896 tottimeps_ind, strtime_ind,
897 irqtime_ind, irqtimeps_ind,
900 spin_unlock(&device->profile.lock);
903 void dasd_profile_reset(struct dasd_profile *profile)
905 struct dasd_profile_info *data;
907 spin_lock_bh(&profile->lock);
908 data = profile->data;
910 spin_unlock_bh(&profile->lock);
913 memset(data, 0, sizeof(*data));
914 ktime_get_real_ts64(&data->starttod);
915 spin_unlock_bh(&profile->lock);
918 int dasd_profile_on(struct dasd_profile *profile)
920 struct dasd_profile_info *data;
922 data = kzalloc(sizeof(*data), GFP_KERNEL);
925 spin_lock_bh(&profile->lock);
927 spin_unlock_bh(&profile->lock);
931 ktime_get_real_ts64(&data->starttod);
932 profile->data = data;
933 spin_unlock_bh(&profile->lock);
937 void dasd_profile_off(struct dasd_profile *profile)
939 spin_lock_bh(&profile->lock);
940 kfree(profile->data);
941 profile->data = NULL;
942 spin_unlock_bh(&profile->lock);
945 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
949 buffer = vmalloc(user_len + 1);
951 return ERR_PTR(-ENOMEM);
952 if (copy_from_user(buffer, user_buf, user_len) != 0) {
954 return ERR_PTR(-EFAULT);
956 /* got the string, now strip linefeed. */
957 if (buffer[user_len - 1] == '\n')
958 buffer[user_len - 1] = 0;
960 buffer[user_len] = 0;
964 static ssize_t dasd_stats_write(struct file *file,
965 const char __user *user_buf,
966 size_t user_len, loff_t *pos)
970 struct seq_file *m = (struct seq_file *)file->private_data;
971 struct dasd_profile *prof = m->private;
973 if (user_len > 65536)
975 buffer = dasd_get_user_string(user_buf, user_len);
977 return PTR_ERR(buffer);
979 str = skip_spaces(buffer);
981 if (strncmp(str, "reset", 5) == 0) {
982 dasd_profile_reset(prof);
983 } else if (strncmp(str, "on", 2) == 0) {
984 rc = dasd_profile_on(prof);
988 if (prof == &dasd_global_profile) {
989 dasd_profile_reset(prof);
990 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
992 } else if (strncmp(str, "off", 3) == 0) {
993 if (prof == &dasd_global_profile)
994 dasd_global_profile_level = DASD_PROFILE_OFF;
995 dasd_profile_off(prof);
1003 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
1007 for (i = 0; i < 32; i++)
1008 seq_printf(m, "%u ", array[i]);
1012 static void dasd_stats_seq_print(struct seq_file *m,
1013 struct dasd_profile_info *data)
1015 seq_printf(m, "start_time %lld.%09ld\n",
1016 (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
1017 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
1018 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1019 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1020 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
1021 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1022 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1023 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1024 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1025 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1026 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1027 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1028 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
1029 seq_puts(m, "histogram_sectors ");
1030 dasd_stats_array(m, data->dasd_io_secs);
1031 seq_puts(m, "histogram_io_times ");
1032 dasd_stats_array(m, data->dasd_io_times);
1033 seq_puts(m, "histogram_io_times_weighted ");
1034 dasd_stats_array(m, data->dasd_io_timps);
1035 seq_puts(m, "histogram_time_build_to_ssch ");
1036 dasd_stats_array(m, data->dasd_io_time1);
1037 seq_puts(m, "histogram_time_ssch_to_irq ");
1038 dasd_stats_array(m, data->dasd_io_time2);
1039 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
1040 dasd_stats_array(m, data->dasd_io_time2ps);
1041 seq_puts(m, "histogram_time_irq_to_end ");
1042 dasd_stats_array(m, data->dasd_io_time3);
1043 seq_puts(m, "histogram_ccw_queue_length ");
1044 dasd_stats_array(m, data->dasd_io_nr_req);
1045 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1046 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1047 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1048 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
1049 seq_puts(m, "histogram_read_sectors ");
1050 dasd_stats_array(m, data->dasd_read_secs);
1051 seq_puts(m, "histogram_read_times ");
1052 dasd_stats_array(m, data->dasd_read_times);
1053 seq_puts(m, "histogram_read_time_build_to_ssch ");
1054 dasd_stats_array(m, data->dasd_read_time1);
1055 seq_puts(m, "histogram_read_time_ssch_to_irq ");
1056 dasd_stats_array(m, data->dasd_read_time2);
1057 seq_puts(m, "histogram_read_time_irq_to_end ");
1058 dasd_stats_array(m, data->dasd_read_time3);
1059 seq_puts(m, "histogram_read_ccw_queue_length ");
1060 dasd_stats_array(m, data->dasd_read_nr_req);
1063 static int dasd_stats_show(struct seq_file *m, void *v)
1065 struct dasd_profile *profile;
1066 struct dasd_profile_info *data;
1068 profile = m->private;
1069 spin_lock_bh(&profile->lock);
1070 data = profile->data;
1072 spin_unlock_bh(&profile->lock);
1073 seq_puts(m, "disabled\n");
1076 dasd_stats_seq_print(m, data);
1077 spin_unlock_bh(&profile->lock);
1081 static int dasd_stats_open(struct inode *inode, struct file *file)
1083 struct dasd_profile *profile = inode->i_private;
1084 return single_open(file, dasd_stats_show, profile);
1087 static const struct file_operations dasd_stats_raw_fops = {
1088 .owner = THIS_MODULE,
1089 .open = dasd_stats_open,
1091 .llseek = seq_lseek,
1092 .release = single_release,
1093 .write = dasd_stats_write,
1096 static void dasd_profile_init(struct dasd_profile *profile,
1097 struct dentry *base_dentry)
1104 profile->dentry = NULL;
1105 profile->data = NULL;
1106 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1107 pde = debugfs_create_file("statistics", mode, base_dentry,
1108 profile, &dasd_stats_raw_fops);
1109 if (pde && !IS_ERR(pde))
1110 profile->dentry = pde;
1114 static void dasd_profile_exit(struct dasd_profile *profile)
1116 dasd_profile_off(profile);
1117 debugfs_remove(profile->dentry);
1118 profile->dentry = NULL;
1121 static void dasd_statistics_removeroot(void)
1123 dasd_global_profile_level = DASD_PROFILE_OFF;
1124 dasd_profile_exit(&dasd_global_profile);
1125 debugfs_remove(dasd_debugfs_global_entry);
1126 debugfs_remove(dasd_debugfs_root_entry);
1129 static void dasd_statistics_createroot(void)
1133 dasd_debugfs_root_entry = NULL;
1134 pde = debugfs_create_dir("dasd", NULL);
1135 if (!pde || IS_ERR(pde))
1137 dasd_debugfs_root_entry = pde;
1138 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1139 if (!pde || IS_ERR(pde))
1141 dasd_debugfs_global_entry = pde;
1142 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
1146 DBF_EVENT(DBF_ERR, "%s",
1147 "Creation of the dasd debugfs interface failed");
1148 dasd_statistics_removeroot();
1153 #define dasd_profile_start(block, cqr, req) do {} while (0)
1154 #define dasd_profile_end(block, cqr, req) do {} while (0)
1156 static void dasd_statistics_createroot(void)
1161 static void dasd_statistics_removeroot(void)
1166 int dasd_stats_generic_show(struct seq_file *m, void *v)
1168 seq_puts(m, "Statistics are not activated in this kernel\n");
1172 static void dasd_profile_init(struct dasd_profile *profile,
1173 struct dentry *base_dentry)
1178 static void dasd_profile_exit(struct dasd_profile *profile)
1183 int dasd_profile_on(struct dasd_profile *profile)
1188 #endif /* CONFIG_DASD_PROFILE */
1190 static int dasd_hosts_show(struct seq_file *m, void *v)
1192 struct dasd_device *device;
1193 int rc = -EOPNOTSUPP;
1195 device = m->private;
1196 dasd_get_device(device);
1198 if (device->discipline->hosts_print)
1199 rc = device->discipline->hosts_print(device, m);
1201 dasd_put_device(device);
1205 DEFINE_SHOW_ATTRIBUTE(dasd_hosts);
1207 static void dasd_hosts_exit(struct dasd_device *device)
1209 debugfs_remove(device->hosts_dentry);
1210 device->hosts_dentry = NULL;
1213 static void dasd_hosts_init(struct dentry *base_dentry,
1214 struct dasd_device *device)
1222 mode = S_IRUSR | S_IFREG;
1223 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1224 device, &dasd_hosts_fops);
1225 if (pde && !IS_ERR(pde))
1226 device->hosts_dentry = pde;
1229 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
1230 struct dasd_device *device,
1231 struct dasd_ccw_req *cqr)
1233 unsigned long flags;
1238 size += cplength * sizeof(struct ccw1);
1242 size += (sizeof(*cqr) + 7L) & -8L;
1244 spin_lock_irqsave(&device->mem_lock, flags);
1245 data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
1246 spin_unlock_irqrestore(&device->mem_lock, flags);
1248 return ERR_PTR(-ENOMEM);
1250 cqr = (void *) data;
1251 data += (sizeof(*cqr) + 7L) & -8L;
1253 memset(cqr, 0, sizeof(*cqr));
1254 cqr->mem_chunk = chunk;
1257 data += cplength * sizeof(struct ccw1);
1258 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1262 memset(cqr->data, 0, datasize);
1265 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1266 dasd_get_device(device);
1269 EXPORT_SYMBOL(dasd_smalloc_request);
1271 struct dasd_ccw_req *dasd_fmalloc_request(int magic, int cplength,
1273 struct dasd_device *device)
1275 struct dasd_ccw_req *cqr;
1276 unsigned long flags;
1280 cqr_size = (sizeof(*cqr) + 7L) & -8L;
1283 size += cplength * sizeof(struct ccw1);
1287 spin_lock_irqsave(&device->mem_lock, flags);
1288 cqr = dasd_alloc_chunk(&device->ese_chunks, size);
1289 spin_unlock_irqrestore(&device->mem_lock, flags);
1291 return ERR_PTR(-ENOMEM);
1292 memset(cqr, 0, sizeof(*cqr));
1293 data = (char *)cqr + cqr_size;
1297 data += cplength * sizeof(struct ccw1);
1298 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1303 memset(cqr->data, 0, datasize);
1307 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1308 dasd_get_device(device);
1312 EXPORT_SYMBOL(dasd_fmalloc_request);
1314 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1316 unsigned long flags;
1318 spin_lock_irqsave(&device->mem_lock, flags);
1319 dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
1320 spin_unlock_irqrestore(&device->mem_lock, flags);
1321 dasd_put_device(device);
1323 EXPORT_SYMBOL(dasd_sfree_request);
1325 void dasd_ffree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1327 unsigned long flags;
1329 spin_lock_irqsave(&device->mem_lock, flags);
1330 dasd_free_chunk(&device->ese_chunks, cqr);
1331 spin_unlock_irqrestore(&device->mem_lock, flags);
1332 dasd_put_device(device);
1334 EXPORT_SYMBOL(dasd_ffree_request);
1337 * Check discipline magic in cqr.
1339 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1341 struct dasd_device *device;
1345 device = cqr->startdev;
1346 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1347 DBF_DEV_EVENT(DBF_WARNING, device,
1348 " dasd_ccw_req 0x%08x magic doesn't match"
1349 " discipline 0x%08x",
1351 *(unsigned int *) device->discipline->name);
1358 * Terminate the current i/o and set the request to clear_pending.
1359 * Timer keeps device runnig.
1360 * ccw_device_clear can fail if the i/o subsystem
1363 int dasd_term_IO(struct dasd_ccw_req *cqr)
1365 struct dasd_device *device;
1367 char errorstring[ERRORLENGTH];
1370 rc = dasd_check_cqr(cqr);
1374 device = (struct dasd_device *) cqr->startdev;
1375 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1376 rc = ccw_device_clear(device->cdev, (long) cqr);
1378 case 0: /* termination successful */
1379 cqr->status = DASD_CQR_CLEAR_PENDING;
1380 cqr->stopclk = get_tod_clock();
1382 DBF_DEV_EVENT(DBF_DEBUG, device,
1383 "terminate cqr %p successful",
1387 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1388 "device gone, retry");
1392 * device not valid so no I/O could be running
1393 * handle CQR as termination successful
1395 cqr->status = DASD_CQR_CLEARED;
1396 cqr->stopclk = get_tod_clock();
1398 /* no retries for invalid devices */
1400 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1401 "EINVAL, handle as terminated");
1402 /* fake rc to success */
1406 /* internal error 10 - unknown rc*/
1407 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1408 dev_err(&device->cdev->dev, "An error occurred in the "
1409 "DASD device driver, reason=%s\n", errorstring);
1415 dasd_schedule_device_bh(device);
1418 EXPORT_SYMBOL(dasd_term_IO);
1421 * Start the i/o. This start_IO can fail if the channel is really busy.
1422 * In that case set up a timer to start the request later.
1424 int dasd_start_IO(struct dasd_ccw_req *cqr)
1426 struct dasd_device *device;
1428 char errorstring[ERRORLENGTH];
1431 rc = dasd_check_cqr(cqr);
1436 device = (struct dasd_device *) cqr->startdev;
1438 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1439 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1440 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1441 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1442 "because of stolen lock", cqr);
1443 cqr->status = DASD_CQR_ERROR;
1444 cqr->intrc = -EPERM;
1447 if (cqr->retries < 0) {
1448 /* internal error 14 - start_IO run out of retries */
1449 sprintf(errorstring, "14 %p", cqr);
1450 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1451 "device driver, reason=%s\n", errorstring);
1452 cqr->status = DASD_CQR_ERROR;
1455 cqr->startclk = get_tod_clock();
1456 cqr->starttime = jiffies;
1458 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1459 cqr->lpm &= dasd_path_get_opm(device);
1461 cqr->lpm = dasd_path_get_opm(device);
1463 if (cqr->cpmode == 1) {
1464 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1465 (long) cqr, cqr->lpm);
1467 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1468 (long) cqr, cqr->lpm, 0);
1472 cqr->status = DASD_CQR_IN_IO;
1475 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1476 "start_IO: device busy, retry later");
1479 /* -EACCES indicates that the request used only a subset of the
1480 * available paths and all these paths are gone. If the lpm of
1481 * this request was only a subset of the opm (e.g. the ppm) then
1482 * we just do a retry with all available paths.
1483 * If we already use the full opm, something is amiss, and we
1484 * need a full path verification.
1486 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1487 DBF_DEV_EVENT(DBF_WARNING, device,
1488 "start_IO: selected paths gone (%x)",
1490 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1491 cqr->lpm = dasd_path_get_opm(device);
1492 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1493 "start_IO: selected paths gone,"
1494 " retry on all paths");
1496 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1497 "start_IO: all paths in opm gone,"
1498 " do path verification");
1499 dasd_generic_last_path_gone(device);
1500 dasd_path_no_path(device);
1501 dasd_path_set_tbvpm(device,
1502 ccw_device_get_path_mask(
1507 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1508 "start_IO: -ENODEV device gone, retry");
1511 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1512 "start_IO: -EIO device gone, retry");
1515 /* most likely caused in power management context */
1516 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1517 "start_IO: -EINVAL device currently "
1521 /* internal error 11 - unknown rc */
1522 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1523 dev_err(&device->cdev->dev,
1524 "An error occurred in the DASD device driver, "
1525 "reason=%s\n", errorstring);
1532 EXPORT_SYMBOL(dasd_start_IO);
1535 * Timeout function for dasd devices. This is used for different purposes
1536 * 1) missing interrupt handler for normal operation
1537 * 2) delayed start of request where start_IO failed with -EBUSY
1538 * 3) timeout for missing state change interrupts
1539 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1540 * DASD_CQR_QUEUED for 2) and 3).
1542 static void dasd_device_timeout(struct timer_list *t)
1544 unsigned long flags;
1545 struct dasd_device *device;
1547 device = from_timer(device, t, timer);
1548 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1549 /* re-activate request queue */
1550 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1551 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1552 dasd_schedule_device_bh(device);
1556 * Setup timeout for a device in jiffies.
1558 void dasd_device_set_timer(struct dasd_device *device, int expires)
1561 del_timer(&device->timer);
1563 mod_timer(&device->timer, jiffies + expires);
1565 EXPORT_SYMBOL(dasd_device_set_timer);
1568 * Clear timeout for a device.
1570 void dasd_device_clear_timer(struct dasd_device *device)
1572 del_timer(&device->timer);
1574 EXPORT_SYMBOL(dasd_device_clear_timer);
1576 static void dasd_handle_killed_request(struct ccw_device *cdev,
1577 unsigned long intparm)
1579 struct dasd_ccw_req *cqr;
1580 struct dasd_device *device;
1584 cqr = (struct dasd_ccw_req *) intparm;
1585 if (cqr->status != DASD_CQR_IN_IO) {
1586 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1587 "invalid status in handle_killed_request: "
1588 "%02x", cqr->status);
1592 device = dasd_device_from_cdev_locked(cdev);
1593 if (IS_ERR(device)) {
1594 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1595 "unable to get device from cdev");
1599 if (!cqr->startdev ||
1600 device != cqr->startdev ||
1601 strncmp(cqr->startdev->discipline->ebcname,
1602 (char *) &cqr->magic, 4)) {
1603 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1604 "invalid device in request");
1605 dasd_put_device(device);
1609 /* Schedule request to be retried. */
1610 cqr->status = DASD_CQR_QUEUED;
1612 dasd_device_clear_timer(device);
1613 dasd_schedule_device_bh(device);
1614 dasd_put_device(device);
1617 void dasd_generic_handle_state_change(struct dasd_device *device)
1619 /* First of all start sense subsystem status request. */
1620 dasd_eer_snss(device);
1622 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1623 dasd_schedule_device_bh(device);
1624 if (device->block) {
1625 dasd_schedule_block_bh(device->block);
1626 if (device->block->request_queue)
1627 blk_mq_run_hw_queues(device->block->request_queue,
1631 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1633 static int dasd_check_hpf_error(struct irb *irb)
1635 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1636 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1637 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1640 static int dasd_ese_needs_format(struct dasd_block *block, struct irb *irb)
1642 struct dasd_device *device = NULL;
1647 device = block->base;
1648 if (!device || !device->discipline->is_ese)
1650 if (!device->discipline->is_ese(device))
1653 sense = dasd_get_sense(irb);
1657 return !!(sense[1] & SNS1_NO_REC_FOUND) ||
1658 !!(sense[1] & SNS1_FILE_PROTECTED) ||
1659 scsw_cstat(&irb->scsw) == SCHN_STAT_INCORR_LEN;
1662 static int dasd_ese_oos_cond(u8 *sense)
1664 return sense[0] & SNS0_EQUIPMENT_CHECK &&
1665 sense[1] & SNS1_PERM_ERR &&
1666 sense[1] & SNS1_WRITE_INHIBITED &&
1671 * Interrupt handler for "normal" ssch-io based dasd devices.
1673 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1676 struct dasd_ccw_req *cqr, *next, *fcqr;
1677 struct dasd_device *device;
1679 int nrf_suppressed = 0;
1680 int fp_suppressed = 0;
1684 cqr = (struct dasd_ccw_req *) intparm;
1686 switch (PTR_ERR(irb)) {
1688 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1689 device = cqr->startdev;
1690 cqr->status = DASD_CQR_CLEARED;
1691 dasd_device_clear_timer(device);
1692 wake_up(&dasd_flush_wq);
1693 dasd_schedule_device_bh(device);
1698 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1699 "request timed out\n", __func__);
1702 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1703 "unknown error %ld\n", __func__,
1706 dasd_handle_killed_request(cdev, intparm);
1710 now = get_tod_clock();
1711 /* check for conditions that should be handled immediately */
1713 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1714 scsw_cstat(&irb->scsw) == 0)) {
1716 memcpy(&cqr->irb, irb, sizeof(*irb));
1717 device = dasd_device_from_cdev_locked(cdev);
1720 /* ignore unsolicited interrupts for DIAG discipline */
1721 if (device->discipline == dasd_diag_discipline_pointer) {
1722 dasd_put_device(device);
1727 * In some cases 'File Protected' or 'No Record Found' errors
1728 * might be expected and debug log messages for the
1729 * corresponding interrupts shouldn't be written then.
1730 * Check if either of the according suppress bits is set.
1732 sense = dasd_get_sense(irb);
1734 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1735 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1736 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1737 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1740 * Extent pool probably out-of-space.
1741 * Stop device and check exhaust level.
1743 if (dasd_ese_oos_cond(sense)) {
1744 dasd_generic_space_exhaust(device, cqr);
1745 device->discipline->ext_pool_exhaust(device, cqr);
1746 dasd_put_device(device);
1750 if (!(fp_suppressed || nrf_suppressed))
1751 device->discipline->dump_sense_dbf(device, irb, "int");
1753 if (device->features & DASD_FEATURE_ERPLOG)
1754 device->discipline->dump_sense(device, cqr, irb);
1755 device->discipline->check_for_device_change(device, cqr, irb);
1756 dasd_put_device(device);
1759 /* check for for attention message */
1760 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1761 device = dasd_device_from_cdev_locked(cdev);
1762 if (!IS_ERR(device)) {
1763 device->discipline->check_attention(device,
1764 irb->esw.esw1.lpum);
1765 dasd_put_device(device);
1772 device = (struct dasd_device *) cqr->startdev;
1774 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1775 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1776 "invalid device in request");
1780 if (dasd_ese_needs_format(cqr->block, irb)) {
1781 if (rq_data_dir((struct request *)cqr->callback_data) == READ) {
1782 device->discipline->ese_read(cqr);
1783 cqr->status = DASD_CQR_SUCCESS;
1785 dasd_device_clear_timer(device);
1786 dasd_schedule_device_bh(device);
1789 fcqr = device->discipline->ese_format(device, cqr);
1792 * If we can't format now, let the request go
1793 * one extra round. Maybe we can format later.
1795 cqr->status = DASD_CQR_QUEUED;
1797 fcqr->status = DASD_CQR_QUEUED;
1798 cqr->status = DASD_CQR_QUEUED;
1799 list_add(&fcqr->devlist, &device->ccw_queue);
1800 dasd_schedule_device_bh(device);
1805 /* Check for clear pending */
1806 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1807 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1808 cqr->status = DASD_CQR_CLEARED;
1809 dasd_device_clear_timer(device);
1810 wake_up(&dasd_flush_wq);
1811 dasd_schedule_device_bh(device);
1815 /* check status - the request might have been killed by dyn detach */
1816 if (cqr->status != DASD_CQR_IN_IO) {
1817 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1818 "status %02x", dev_name(&cdev->dev), cqr->status);
1824 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1825 scsw_cstat(&irb->scsw) == 0) {
1826 /* request was completed successfully */
1827 cqr->status = DASD_CQR_SUCCESS;
1829 /* Start first request on queue if possible -> fast_io. */
1830 if (cqr->devlist.next != &device->ccw_queue) {
1831 next = list_entry(cqr->devlist.next,
1832 struct dasd_ccw_req, devlist);
1834 } else { /* error */
1835 /* check for HPF error
1836 * call discipline function to requeue all requests
1837 * and disable HPF accordingly
1839 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1840 device->discipline->handle_hpf_error)
1841 device->discipline->handle_hpf_error(device, irb);
1843 * If we don't want complex ERP for this request, then just
1844 * reset this and retry it in the fastpath
1846 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1848 if (cqr->lpm == dasd_path_get_opm(device))
1849 DBF_DEV_EVENT(DBF_DEBUG, device,
1850 "default ERP in fastpath "
1851 "(%i retries left)",
1853 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1854 cqr->lpm = dasd_path_get_opm(device);
1855 cqr->status = DASD_CQR_QUEUED;
1858 cqr->status = DASD_CQR_ERROR;
1860 if (next && (next->status == DASD_CQR_QUEUED) &&
1861 (!device->stopped)) {
1862 if (device->discipline->start_IO(next) == 0)
1863 expires = next->expires;
1866 dasd_device_set_timer(device, expires);
1868 dasd_device_clear_timer(device);
1869 dasd_schedule_device_bh(device);
1871 EXPORT_SYMBOL(dasd_int_handler);
1873 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1875 struct dasd_device *device;
1877 device = dasd_device_from_cdev_locked(cdev);
1881 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1882 device->state != device->target ||
1883 !device->discipline->check_for_device_change){
1884 dasd_put_device(device);
1887 if (device->discipline->dump_sense_dbf)
1888 device->discipline->dump_sense_dbf(device, irb, "uc");
1889 device->discipline->check_for_device_change(device, NULL, irb);
1890 dasd_put_device(device);
1892 return UC_TODO_RETRY;
1894 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1897 * If we have an error on a dasd_block layer request then we cancel
1898 * and return all further requests from the same dasd_block as well.
1900 static void __dasd_device_recovery(struct dasd_device *device,
1901 struct dasd_ccw_req *ref_cqr)
1903 struct list_head *l, *n;
1904 struct dasd_ccw_req *cqr;
1907 * only requeue request that came from the dasd_block layer
1909 if (!ref_cqr->block)
1912 list_for_each_safe(l, n, &device->ccw_queue) {
1913 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1914 if (cqr->status == DASD_CQR_QUEUED &&
1915 ref_cqr->block == cqr->block) {
1916 cqr->status = DASD_CQR_CLEARED;
1922 * Remove those ccw requests from the queue that need to be returned
1923 * to the upper layer.
1925 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1926 struct list_head *final_queue)
1928 struct list_head *l, *n;
1929 struct dasd_ccw_req *cqr;
1931 /* Process request with final status. */
1932 list_for_each_safe(l, n, &device->ccw_queue) {
1933 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1935 /* Skip any non-final request. */
1936 if (cqr->status == DASD_CQR_QUEUED ||
1937 cqr->status == DASD_CQR_IN_IO ||
1938 cqr->status == DASD_CQR_CLEAR_PENDING)
1940 if (cqr->status == DASD_CQR_ERROR) {
1941 __dasd_device_recovery(device, cqr);
1943 /* Rechain finished requests to final queue */
1944 list_move_tail(&cqr->devlist, final_queue);
1948 static void __dasd_process_cqr(struct dasd_device *device,
1949 struct dasd_ccw_req *cqr)
1951 char errorstring[ERRORLENGTH];
1953 switch (cqr->status) {
1954 case DASD_CQR_SUCCESS:
1955 cqr->status = DASD_CQR_DONE;
1957 case DASD_CQR_ERROR:
1958 cqr->status = DASD_CQR_NEED_ERP;
1960 case DASD_CQR_CLEARED:
1961 cqr->status = DASD_CQR_TERMINATED;
1964 /* internal error 12 - wrong cqr status*/
1965 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1966 dev_err(&device->cdev->dev,
1967 "An error occurred in the DASD device driver, "
1968 "reason=%s\n", errorstring);
1972 cqr->callback(cqr, cqr->callback_data);
1976 * the cqrs from the final queue are returned to the upper layer
1977 * by setting a dasd_block state and calling the callback function
1979 static void __dasd_device_process_final_queue(struct dasd_device *device,
1980 struct list_head *final_queue)
1982 struct list_head *l, *n;
1983 struct dasd_ccw_req *cqr;
1984 struct dasd_block *block;
1986 list_for_each_safe(l, n, final_queue) {
1987 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1988 list_del_init(&cqr->devlist);
1991 __dasd_process_cqr(device, cqr);
1993 spin_lock_bh(&block->queue_lock);
1994 __dasd_process_cqr(device, cqr);
1995 spin_unlock_bh(&block->queue_lock);
2001 * Take a look at the first request on the ccw queue and check
2002 * if it reached its expire time. If so, terminate the IO.
2004 static void __dasd_device_check_expire(struct dasd_device *device)
2006 struct dasd_ccw_req *cqr;
2008 if (list_empty(&device->ccw_queue))
2010 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2011 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
2012 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
2013 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2015 * IO in safe offline processing should not
2016 * run out of retries
2020 if (device->discipline->term_IO(cqr) != 0) {
2021 /* Hmpf, try again in 5 sec */
2022 dev_err(&device->cdev->dev,
2023 "cqr %p timed out (%lus) but cannot be "
2024 "ended, retrying in 5 s\n",
2025 cqr, (cqr->expires/HZ));
2026 cqr->expires += 5*HZ;
2027 dasd_device_set_timer(device, 5*HZ);
2029 dev_err(&device->cdev->dev,
2030 "cqr %p timed out (%lus), %i retries "
2031 "remaining\n", cqr, (cqr->expires/HZ),
2038 * return 1 when device is not eligible for IO
2040 static int __dasd_device_is_unusable(struct dasd_device *device,
2041 struct dasd_ccw_req *cqr)
2043 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM | DASD_STOPPED_NOSPC);
2045 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2046 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2048 * dasd is being set offline
2049 * but it is no safe offline where we have to allow I/O
2053 if (device->stopped) {
2054 if (device->stopped & mask) {
2055 /* stopped and CQR will not change that. */
2058 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2059 /* CQR is not able to change device to
2063 /* CQR required to get device operational. */
2069 * Take a look at the first request on the ccw queue and check
2070 * if it needs to be started.
2072 static void __dasd_device_start_head(struct dasd_device *device)
2074 struct dasd_ccw_req *cqr;
2077 if (list_empty(&device->ccw_queue))
2079 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2080 if (cqr->status != DASD_CQR_QUEUED)
2082 /* if device is not usable return request to upper layer */
2083 if (__dasd_device_is_unusable(device, cqr)) {
2084 cqr->intrc = -EAGAIN;
2085 cqr->status = DASD_CQR_CLEARED;
2086 dasd_schedule_device_bh(device);
2090 rc = device->discipline->start_IO(cqr);
2092 dasd_device_set_timer(device, cqr->expires);
2093 else if (rc == -EACCES) {
2094 dasd_schedule_device_bh(device);
2096 /* Hmpf, try again in 1/2 sec */
2097 dasd_device_set_timer(device, 50);
2100 static void __dasd_device_check_path_events(struct dasd_device *device)
2104 if (!dasd_path_get_tbvpm(device))
2107 if (device->stopped &
2108 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2110 rc = device->discipline->verify_path(device,
2111 dasd_path_get_tbvpm(device));
2113 dasd_device_set_timer(device, 50);
2115 dasd_path_clear_all_verify(device);
2119 * Go through all request on the dasd_device request queue,
2120 * terminate them on the cdev if necessary, and return them to the
2121 * submitting layer via callback.
2123 * Make sure that all 'submitting layers' still exist when
2124 * this function is called!. In other words, when 'device' is a base
2125 * device then all block layer requests must have been removed before
2126 * via dasd_flush_block_queue.
2128 int dasd_flush_device_queue(struct dasd_device *device)
2130 struct dasd_ccw_req *cqr, *n;
2132 struct list_head flush_queue;
2134 INIT_LIST_HEAD(&flush_queue);
2135 spin_lock_irq(get_ccwdev_lock(device->cdev));
2137 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2138 /* Check status and move request to flush_queue */
2139 switch (cqr->status) {
2140 case DASD_CQR_IN_IO:
2141 rc = device->discipline->term_IO(cqr);
2143 /* unable to terminate requeust */
2144 dev_err(&device->cdev->dev,
2145 "Flushing the DASD request queue "
2146 "failed for request %p\n", cqr);
2147 /* stop flush processing */
2151 case DASD_CQR_QUEUED:
2152 cqr->stopclk = get_tod_clock();
2153 cqr->status = DASD_CQR_CLEARED;
2155 default: /* no need to modify the others */
2158 list_move_tail(&cqr->devlist, &flush_queue);
2161 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2163 * After this point all requests must be in state CLEAR_PENDING,
2164 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2165 * one of the others.
2167 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2168 wait_event(dasd_flush_wq,
2169 (cqr->status != DASD_CQR_CLEAR_PENDING));
2171 * Now set each request back to TERMINATED, DONE or NEED_ERP
2172 * and call the callback function of flushed requests
2174 __dasd_device_process_final_queue(device, &flush_queue);
2177 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2180 * Acquire the device lock and process queues for the device.
2182 static void dasd_device_tasklet(unsigned long data)
2184 struct dasd_device *device = (struct dasd_device *) data;
2185 struct list_head final_queue;
2187 atomic_set (&device->tasklet_scheduled, 0);
2188 INIT_LIST_HEAD(&final_queue);
2189 spin_lock_irq(get_ccwdev_lock(device->cdev));
2190 /* Check expire time of first request on the ccw queue. */
2191 __dasd_device_check_expire(device);
2192 /* find final requests on ccw queue */
2193 __dasd_device_process_ccw_queue(device, &final_queue);
2194 __dasd_device_check_path_events(device);
2195 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2196 /* Now call the callback function of requests with final status */
2197 __dasd_device_process_final_queue(device, &final_queue);
2198 spin_lock_irq(get_ccwdev_lock(device->cdev));
2199 /* Now check if the head of the ccw queue needs to be started. */
2200 __dasd_device_start_head(device);
2201 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2202 if (waitqueue_active(&shutdown_waitq))
2203 wake_up(&shutdown_waitq);
2204 dasd_put_device(device);
2208 * Schedules a call to dasd_tasklet over the device tasklet.
2210 void dasd_schedule_device_bh(struct dasd_device *device)
2212 /* Protect against rescheduling. */
2213 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2215 dasd_get_device(device);
2216 tasklet_hi_schedule(&device->tasklet);
2218 EXPORT_SYMBOL(dasd_schedule_device_bh);
2220 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2222 device->stopped |= bits;
2224 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2226 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2228 device->stopped &= ~bits;
2229 if (!device->stopped)
2230 wake_up(&generic_waitq);
2232 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2235 * Queue a request to the head of the device ccw_queue.
2236 * Start the I/O if possible.
2238 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2240 struct dasd_device *device;
2241 unsigned long flags;
2243 device = cqr->startdev;
2244 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2245 cqr->status = DASD_CQR_QUEUED;
2246 list_add(&cqr->devlist, &device->ccw_queue);
2247 /* let the bh start the request to keep them in order */
2248 dasd_schedule_device_bh(device);
2249 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2251 EXPORT_SYMBOL(dasd_add_request_head);
2254 * Queue a request to the tail of the device ccw_queue.
2255 * Start the I/O if possible.
2257 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2259 struct dasd_device *device;
2260 unsigned long flags;
2262 device = cqr->startdev;
2263 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2264 cqr->status = DASD_CQR_QUEUED;
2265 list_add_tail(&cqr->devlist, &device->ccw_queue);
2266 /* let the bh start the request to keep them in order */
2267 dasd_schedule_device_bh(device);
2268 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2270 EXPORT_SYMBOL(dasd_add_request_tail);
2273 * Wakeup helper for the 'sleep_on' functions.
2275 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2277 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2278 cqr->callback_data = DASD_SLEEPON_END_TAG;
2279 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2280 wake_up(&generic_waitq);
2282 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2284 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2286 struct dasd_device *device;
2289 device = cqr->startdev;
2290 spin_lock_irq(get_ccwdev_lock(device->cdev));
2291 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2292 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2297 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2299 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2301 struct dasd_device *device;
2302 dasd_erp_fn_t erp_fn;
2304 if (cqr->status == DASD_CQR_FILLED)
2306 device = cqr->startdev;
2307 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2308 if (cqr->status == DASD_CQR_TERMINATED) {
2309 device->discipline->handle_terminated_request(cqr);
2312 if (cqr->status == DASD_CQR_NEED_ERP) {
2313 erp_fn = device->discipline->erp_action(cqr);
2317 if (cqr->status == DASD_CQR_FAILED)
2318 dasd_log_sense(cqr, &cqr->irb);
2320 __dasd_process_erp(device, cqr);
2327 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2329 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2330 if (cqr->refers) /* erp is not done yet */
2332 return ((cqr->status != DASD_CQR_DONE) &&
2333 (cqr->status != DASD_CQR_FAILED));
2335 return (cqr->status == DASD_CQR_FILLED);
2338 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2340 struct dasd_device *device;
2342 struct list_head ccw_queue;
2343 struct dasd_ccw_req *cqr;
2345 INIT_LIST_HEAD(&ccw_queue);
2346 maincqr->status = DASD_CQR_FILLED;
2347 device = maincqr->startdev;
2348 list_add(&maincqr->blocklist, &ccw_queue);
2349 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2350 cqr = list_first_entry(&ccw_queue,
2351 struct dasd_ccw_req, blocklist)) {
2353 if (__dasd_sleep_on_erp(cqr))
2355 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2357 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2358 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2359 cqr->status = DASD_CQR_FAILED;
2360 cqr->intrc = -EPERM;
2363 /* Non-temporary stop condition will trigger fail fast */
2364 if (device->stopped & ~DASD_STOPPED_PENDING &&
2365 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2366 (!dasd_eer_enabled(device))) {
2367 cqr->status = DASD_CQR_FAILED;
2368 cqr->intrc = -ENOLINK;
2372 * Don't try to start requests if device is in
2373 * offline processing, it might wait forever
2375 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2376 cqr->status = DASD_CQR_FAILED;
2377 cqr->intrc = -ENODEV;
2381 * Don't try to start requests if device is stopped
2382 * except path verification requests
2384 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2385 if (interruptible) {
2386 rc = wait_event_interruptible(
2387 generic_waitq, !(device->stopped));
2388 if (rc == -ERESTARTSYS) {
2389 cqr->status = DASD_CQR_FAILED;
2390 maincqr->intrc = rc;
2394 wait_event(generic_waitq, !(device->stopped));
2397 cqr->callback = dasd_wakeup_cb;
2399 cqr->callback_data = DASD_SLEEPON_START_TAG;
2400 dasd_add_request_tail(cqr);
2401 if (interruptible) {
2402 rc = wait_event_interruptible(
2403 generic_waitq, _wait_for_wakeup(cqr));
2404 if (rc == -ERESTARTSYS) {
2405 dasd_cancel_req(cqr);
2406 /* wait (non-interruptible) for final status */
2407 wait_event(generic_waitq,
2408 _wait_for_wakeup(cqr));
2409 cqr->status = DASD_CQR_FAILED;
2410 maincqr->intrc = rc;
2414 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2417 maincqr->endclk = get_tod_clock();
2418 if ((maincqr->status != DASD_CQR_DONE) &&
2419 (maincqr->intrc != -ERESTARTSYS))
2420 dasd_log_sense(maincqr, &maincqr->irb);
2421 if (maincqr->status == DASD_CQR_DONE)
2423 else if (maincqr->intrc)
2424 rc = maincqr->intrc;
2430 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2432 struct dasd_ccw_req *cqr;
2434 list_for_each_entry(cqr, ccw_queue, blocklist) {
2435 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2442 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2444 struct dasd_device *device;
2445 struct dasd_ccw_req *cqr, *n;
2450 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2451 device = cqr->startdev;
2452 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2455 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2456 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2457 cqr->status = DASD_CQR_FAILED;
2458 cqr->intrc = -EPERM;
2461 /*Non-temporary stop condition will trigger fail fast*/
2462 if (device->stopped & ~DASD_STOPPED_PENDING &&
2463 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2464 !dasd_eer_enabled(device)) {
2465 cqr->status = DASD_CQR_FAILED;
2466 cqr->intrc = -EAGAIN;
2470 /*Don't try to start requests if device is stopped*/
2471 if (interruptible) {
2472 rc = wait_event_interruptible(
2473 generic_waitq, !device->stopped);
2474 if (rc == -ERESTARTSYS) {
2475 cqr->status = DASD_CQR_FAILED;
2480 wait_event(generic_waitq, !(device->stopped));
2483 cqr->callback = dasd_wakeup_cb;
2484 cqr->callback_data = DASD_SLEEPON_START_TAG;
2485 dasd_add_request_tail(cqr);
2488 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2491 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2493 * In some cases the 'File Protected' or 'Incorrect Length'
2494 * error might be expected and error recovery would be
2495 * unnecessary in these cases. Check if the according suppress
2498 sense = dasd_get_sense(&cqr->irb);
2499 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2500 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2502 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2503 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2507 * for alias devices simplify error recovery and
2508 * return to upper layer
2509 * do not skip ERP requests
2511 if (cqr->startdev != cqr->basedev && !cqr->refers &&
2512 (cqr->status == DASD_CQR_TERMINATED ||
2513 cqr->status == DASD_CQR_NEED_ERP))
2516 /* normal recovery for basedev IO */
2517 if (__dasd_sleep_on_erp(cqr))
2518 /* handle erp first */
2526 * Queue a request to the tail of the device ccw_queue and wait for
2529 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2531 return _dasd_sleep_on(cqr, 0);
2533 EXPORT_SYMBOL(dasd_sleep_on);
2536 * Start requests from a ccw_queue and wait for their completion.
2538 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2540 return _dasd_sleep_on_queue(ccw_queue, 0);
2542 EXPORT_SYMBOL(dasd_sleep_on_queue);
2545 * Start requests from a ccw_queue and wait interruptible for their completion.
2547 int dasd_sleep_on_queue_interruptible(struct list_head *ccw_queue)
2549 return _dasd_sleep_on_queue(ccw_queue, 1);
2551 EXPORT_SYMBOL(dasd_sleep_on_queue_interruptible);
2554 * Queue a request to the tail of the device ccw_queue and wait
2555 * interruptible for it's completion.
2557 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2559 return _dasd_sleep_on(cqr, 1);
2561 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2564 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2565 * for eckd devices) the currently running request has to be terminated
2566 * and be put back to status queued, before the special request is added
2567 * to the head of the queue. Then the special request is waited on normally.
2569 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2571 struct dasd_ccw_req *cqr;
2574 if (list_empty(&device->ccw_queue))
2576 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2577 rc = device->discipline->term_IO(cqr);
2580 * CQR terminated because a more important request is pending.
2581 * Undo decreasing of retry counter because this is
2582 * not an error case.
2588 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2590 struct dasd_device *device;
2593 device = cqr->startdev;
2594 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2595 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2596 cqr->status = DASD_CQR_FAILED;
2597 cqr->intrc = -EPERM;
2600 spin_lock_irq(get_ccwdev_lock(device->cdev));
2601 rc = _dasd_term_running_cqr(device);
2603 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2606 cqr->callback = dasd_wakeup_cb;
2607 cqr->callback_data = DASD_SLEEPON_START_TAG;
2608 cqr->status = DASD_CQR_QUEUED;
2610 * add new request as second
2611 * first the terminated cqr needs to be finished
2613 list_add(&cqr->devlist, device->ccw_queue.next);
2615 /* let the bh start the request to keep them in order */
2616 dasd_schedule_device_bh(device);
2618 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2620 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2622 if (cqr->status == DASD_CQR_DONE)
2624 else if (cqr->intrc)
2630 dasd_schedule_device_bh(device);
2632 dasd_schedule_block_bh(device->block);
2636 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2639 * Cancels a request that was started with dasd_sleep_on_req.
2640 * This is useful to timeout requests. The request will be
2641 * terminated if it is currently in i/o.
2642 * Returns 0 if request termination was successful
2643 * negative error code if termination failed
2644 * Cancellation of a request is an asynchronous operation! The calling
2645 * function has to wait until the request is properly returned via callback.
2647 static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
2649 struct dasd_device *device = cqr->startdev;
2652 switch (cqr->status) {
2653 case DASD_CQR_QUEUED:
2654 /* request was not started - just set to cleared */
2655 cqr->status = DASD_CQR_CLEARED;
2657 case DASD_CQR_IN_IO:
2658 /* request in IO - terminate IO and release again */
2659 rc = device->discipline->term_IO(cqr);
2661 dev_err(&device->cdev->dev,
2662 "Cancelling request %p failed with rc=%d\n",
2665 cqr->stopclk = get_tod_clock();
2668 default: /* already finished or clear pending - do nothing */
2671 dasd_schedule_device_bh(device);
2675 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2677 struct dasd_device *device = cqr->startdev;
2678 unsigned long flags;
2681 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2682 rc = __dasd_cancel_req(cqr);
2683 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2688 * SECTION: Operations of the dasd_block layer.
2692 * Timeout function for dasd_block. This is used when the block layer
2693 * is waiting for something that may not come reliably, (e.g. a state
2696 static void dasd_block_timeout(struct timer_list *t)
2698 unsigned long flags;
2699 struct dasd_block *block;
2701 block = from_timer(block, t, timer);
2702 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2703 /* re-activate request queue */
2704 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2705 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2706 dasd_schedule_block_bh(block);
2707 blk_mq_run_hw_queues(block->request_queue, true);
2711 * Setup timeout for a dasd_block in jiffies.
2713 void dasd_block_set_timer(struct dasd_block *block, int expires)
2716 del_timer(&block->timer);
2718 mod_timer(&block->timer, jiffies + expires);
2720 EXPORT_SYMBOL(dasd_block_set_timer);
2723 * Clear timeout for a dasd_block.
2725 void dasd_block_clear_timer(struct dasd_block *block)
2727 del_timer(&block->timer);
2729 EXPORT_SYMBOL(dasd_block_clear_timer);
2732 * Process finished error recovery ccw.
2734 static void __dasd_process_erp(struct dasd_device *device,
2735 struct dasd_ccw_req *cqr)
2737 dasd_erp_fn_t erp_fn;
2739 if (cqr->status == DASD_CQR_DONE)
2740 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2742 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2743 erp_fn = device->discipline->erp_postaction(cqr);
2747 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2749 struct request *req;
2750 blk_status_t error = BLK_STS_OK;
2753 req = (struct request *) cqr->callback_data;
2754 dasd_profile_end(cqr->block, cqr, req);
2756 status = cqr->block->base->discipline->free_cp(cqr, req);
2758 error = errno_to_blk_status(status);
2759 else if (status == 0) {
2760 switch (cqr->intrc) {
2762 error = BLK_STS_NEXUS;
2765 error = BLK_STS_TRANSPORT;
2768 error = BLK_STS_TIMEOUT;
2771 error = BLK_STS_IOERR;
2777 * We need to take care for ETIMEDOUT errors here since the
2778 * complete callback does not get called in this case.
2779 * Take care of all errors here and avoid additional code to
2780 * transfer the error value to the complete callback.
2783 blk_mq_end_request(req, error);
2784 blk_mq_run_hw_queues(req->q, true);
2786 blk_mq_complete_request(req);
2791 * Process ccw request queue.
2793 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2794 struct list_head *final_queue)
2796 struct list_head *l, *n;
2797 struct dasd_ccw_req *cqr;
2798 dasd_erp_fn_t erp_fn;
2799 unsigned long flags;
2800 struct dasd_device *base = block->base;
2803 /* Process request with final status. */
2804 list_for_each_safe(l, n, &block->ccw_queue) {
2805 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2806 if (cqr->status != DASD_CQR_DONE &&
2807 cqr->status != DASD_CQR_FAILED &&
2808 cqr->status != DASD_CQR_NEED_ERP &&
2809 cqr->status != DASD_CQR_TERMINATED)
2812 if (cqr->status == DASD_CQR_TERMINATED) {
2813 base->discipline->handle_terminated_request(cqr);
2817 /* Process requests that may be recovered */
2818 if (cqr->status == DASD_CQR_NEED_ERP) {
2819 erp_fn = base->discipline->erp_action(cqr);
2820 if (IS_ERR(erp_fn(cqr)))
2825 /* log sense for fatal error */
2826 if (cqr->status == DASD_CQR_FAILED) {
2827 dasd_log_sense(cqr, &cqr->irb);
2830 /* First of all call extended error reporting. */
2831 if (dasd_eer_enabled(base) &&
2832 cqr->status == DASD_CQR_FAILED) {
2833 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2835 /* restart request */
2836 cqr->status = DASD_CQR_FILLED;
2838 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2839 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2840 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2845 /* Process finished ERP request. */
2847 __dasd_process_erp(base, cqr);
2851 /* Rechain finished requests to final queue */
2852 cqr->endclk = get_tod_clock();
2853 list_move_tail(&cqr->blocklist, final_queue);
2857 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2859 dasd_schedule_block_bh(cqr->block);
2862 static void __dasd_block_start_head(struct dasd_block *block)
2864 struct dasd_ccw_req *cqr;
2866 if (list_empty(&block->ccw_queue))
2868 /* We allways begin with the first requests on the queue, as some
2869 * of previously started requests have to be enqueued on a
2870 * dasd_device again for error recovery.
2872 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2873 if (cqr->status != DASD_CQR_FILLED)
2875 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2876 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2877 cqr->status = DASD_CQR_FAILED;
2878 cqr->intrc = -EPERM;
2879 dasd_schedule_block_bh(block);
2882 /* Non-temporary stop condition will trigger fail fast */
2883 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2884 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2885 (!dasd_eer_enabled(block->base))) {
2886 cqr->status = DASD_CQR_FAILED;
2887 cqr->intrc = -ENOLINK;
2888 dasd_schedule_block_bh(block);
2891 /* Don't try to start requests if device is stopped */
2892 if (block->base->stopped)
2895 /* just a fail safe check, should not happen */
2897 cqr->startdev = block->base;
2899 /* make sure that the requests we submit find their way back */
2900 cqr->callback = dasd_return_cqr_cb;
2902 dasd_add_request_tail(cqr);
2907 * Central dasd_block layer routine. Takes requests from the generic
2908 * block layer request queue, creates ccw requests, enqueues them on
2909 * a dasd_device and processes ccw requests that have been returned.
2911 static void dasd_block_tasklet(unsigned long data)
2913 struct dasd_block *block = (struct dasd_block *) data;
2914 struct list_head final_queue;
2915 struct list_head *l, *n;
2916 struct dasd_ccw_req *cqr;
2917 struct dasd_queue *dq;
2919 atomic_set(&block->tasklet_scheduled, 0);
2920 INIT_LIST_HEAD(&final_queue);
2921 spin_lock_irq(&block->queue_lock);
2922 /* Finish off requests on ccw queue */
2923 __dasd_process_block_ccw_queue(block, &final_queue);
2924 spin_unlock_irq(&block->queue_lock);
2926 /* Now call the callback function of requests with final status */
2927 list_for_each_safe(l, n, &final_queue) {
2928 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2930 spin_lock_irq(&dq->lock);
2931 list_del_init(&cqr->blocklist);
2932 __dasd_cleanup_cqr(cqr);
2933 spin_unlock_irq(&dq->lock);
2936 spin_lock_irq(&block->queue_lock);
2937 /* Now check if the head of the ccw queue needs to be started. */
2938 __dasd_block_start_head(block);
2939 spin_unlock_irq(&block->queue_lock);
2941 if (waitqueue_active(&shutdown_waitq))
2942 wake_up(&shutdown_waitq);
2943 dasd_put_device(block->base);
2946 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2948 wake_up(&dasd_flush_wq);
2952 * Requeue a request back to the block request queue
2953 * only works for block requests
2955 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2957 struct dasd_block *block = cqr->block;
2958 struct request *req;
2962 spin_lock_irq(&cqr->dq->lock);
2963 req = (struct request *) cqr->callback_data;
2964 blk_mq_requeue_request(req, false);
2965 spin_unlock_irq(&cqr->dq->lock);
2971 * Go through all request on the dasd_block request queue, cancel them
2972 * on the respective dasd_device, and return them to the generic
2975 static int dasd_flush_block_queue(struct dasd_block *block)
2977 struct dasd_ccw_req *cqr, *n;
2979 struct list_head flush_queue;
2980 unsigned long flags;
2982 INIT_LIST_HEAD(&flush_queue);
2983 spin_lock_bh(&block->queue_lock);
2986 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2987 /* if this request currently owned by a dasd_device cancel it */
2988 if (cqr->status >= DASD_CQR_QUEUED)
2989 rc = dasd_cancel_req(cqr);
2992 /* Rechain request (including erp chain) so it won't be
2993 * touched by the dasd_block_tasklet anymore.
2994 * Replace the callback so we notice when the request
2995 * is returned from the dasd_device layer.
2997 cqr->callback = _dasd_wake_block_flush_cb;
2998 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2999 list_move_tail(&cqr->blocklist, &flush_queue);
3001 /* moved more than one request - need to restart */
3004 spin_unlock_bh(&block->queue_lock);
3005 /* Now call the callback function of flushed requests */
3007 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
3008 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
3009 /* Process finished ERP request. */
3011 spin_lock_bh(&block->queue_lock);
3012 __dasd_process_erp(block->base, cqr);
3013 spin_unlock_bh(&block->queue_lock);
3014 /* restart list_for_xx loop since dasd_process_erp
3015 * might remove multiple elements */
3018 /* call the callback function */
3019 spin_lock_irqsave(&cqr->dq->lock, flags);
3020 cqr->endclk = get_tod_clock();
3021 list_del_init(&cqr->blocklist);
3022 __dasd_cleanup_cqr(cqr);
3023 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3029 * Schedules a call to dasd_tasklet over the device tasklet.
3031 void dasd_schedule_block_bh(struct dasd_block *block)
3033 /* Protect against rescheduling. */
3034 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3036 /* life cycle of block is bound to it's base device */
3037 dasd_get_device(block->base);
3038 tasklet_hi_schedule(&block->tasklet);
3040 EXPORT_SYMBOL(dasd_schedule_block_bh);
3044 * SECTION: external block device operations
3045 * (request queue handling, open, release, etc.)
3049 * Dasd request queue function. Called from ll_rw_blk.c
3051 static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
3052 const struct blk_mq_queue_data *qd)
3054 struct dasd_block *block = hctx->queue->queuedata;
3055 struct dasd_queue *dq = hctx->driver_data;
3056 struct request *req = qd->rq;
3057 struct dasd_device *basedev;
3058 struct dasd_ccw_req *cqr;
3059 blk_status_t rc = BLK_STS_OK;
3061 basedev = block->base;
3062 spin_lock_irq(&dq->lock);
3063 if (basedev->state < DASD_STATE_READY) {
3064 DBF_DEV_EVENT(DBF_ERR, basedev,
3065 "device not ready for request %p", req);
3071 * if device is stopped do not fetch new requests
3072 * except failfast is active which will let requests fail
3073 * immediately in __dasd_block_start_head()
3075 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
3076 DBF_DEV_EVENT(DBF_ERR, basedev,
3077 "device stopped request %p", req);
3078 rc = BLK_STS_RESOURCE;
3082 if (basedev->features & DASD_FEATURE_READONLY &&
3083 rq_data_dir(req) == WRITE) {
3084 DBF_DEV_EVENT(DBF_ERR, basedev,
3085 "Rejecting write request %p", req);
3090 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
3091 (basedev->features & DASD_FEATURE_FAILFAST ||
3092 blk_noretry_request(req))) {
3093 DBF_DEV_EVENT(DBF_ERR, basedev,
3094 "Rejecting failfast request %p", req);
3099 cqr = basedev->discipline->build_cp(basedev, block, req);
3101 if (PTR_ERR(cqr) == -EBUSY ||
3102 PTR_ERR(cqr) == -ENOMEM ||
3103 PTR_ERR(cqr) == -EAGAIN) {
3104 rc = BLK_STS_RESOURCE;
3107 DBF_DEV_EVENT(DBF_ERR, basedev,
3108 "CCW creation failed (rc=%ld) on request %p",
3114 * Note: callback is set to dasd_return_cqr_cb in
3115 * __dasd_block_start_head to cover erp requests as well
3117 cqr->callback_data = req;
3118 cqr->status = DASD_CQR_FILLED;
3121 blk_mq_start_request(req);
3122 spin_lock(&block->queue_lock);
3123 list_add_tail(&cqr->blocklist, &block->ccw_queue);
3124 INIT_LIST_HEAD(&cqr->devlist);
3125 dasd_profile_start(block, cqr, req);
3126 dasd_schedule_block_bh(block);
3127 spin_unlock(&block->queue_lock);
3130 spin_unlock_irq(&dq->lock);
3135 * Block timeout callback, called from the block layer
3138 * BLK_EH_RESET_TIMER if the request should be left running
3139 * BLK_EH_DONE if the request is handled or terminated
3142 enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
3144 struct dasd_block *block = req->q->queuedata;
3145 struct dasd_device *device;
3146 struct dasd_ccw_req *cqr;
3147 unsigned long flags;
3150 cqr = blk_mq_rq_to_pdu(req);
3154 spin_lock_irqsave(&cqr->dq->lock, flags);
3155 device = cqr->startdev ? cqr->startdev : block->base;
3156 if (!device->blk_timeout) {
3157 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3158 return BLK_EH_RESET_TIMER;
3160 DBF_DEV_EVENT(DBF_WARNING, device,
3161 " dasd_times_out cqr %p status %x",
3164 spin_lock(&block->queue_lock);
3165 spin_lock(get_ccwdev_lock(device->cdev));
3167 cqr->intrc = -ETIMEDOUT;
3168 if (cqr->status >= DASD_CQR_QUEUED) {
3169 rc = __dasd_cancel_req(cqr);
3170 } else if (cqr->status == DASD_CQR_FILLED ||
3171 cqr->status == DASD_CQR_NEED_ERP) {
3172 cqr->status = DASD_CQR_TERMINATED;
3173 } else if (cqr->status == DASD_CQR_IN_ERP) {
3174 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3176 list_for_each_entry_safe(searchcqr, nextcqr,
3177 &block->ccw_queue, blocklist) {
3179 while (tmpcqr->refers)
3180 tmpcqr = tmpcqr->refers;
3183 /* searchcqr is an ERP request for cqr */
3184 searchcqr->retries = -1;
3185 searchcqr->intrc = -ETIMEDOUT;
3186 if (searchcqr->status >= DASD_CQR_QUEUED) {
3187 rc = __dasd_cancel_req(searchcqr);
3188 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3189 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3190 searchcqr->status = DASD_CQR_TERMINATED;
3192 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3194 * Shouldn't happen; most recent ERP
3195 * request is at the front of queue
3202 spin_unlock(get_ccwdev_lock(device->cdev));
3203 dasd_schedule_block_bh(block);
3204 spin_unlock(&block->queue_lock);
3205 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3207 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
3210 static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3213 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3218 spin_lock_init(&dq->lock);
3219 hctx->driver_data = dq;
3224 static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3226 kfree(hctx->driver_data);
3227 hctx->driver_data = NULL;
3230 static void dasd_request_done(struct request *req)
3232 blk_mq_end_request(req, 0);
3233 blk_mq_run_hw_queues(req->q, true);
3236 static struct blk_mq_ops dasd_mq_ops = {
3237 .queue_rq = do_dasd_request,
3238 .complete = dasd_request_done,
3239 .timeout = dasd_times_out,
3240 .init_hctx = dasd_init_hctx,
3241 .exit_hctx = dasd_exit_hctx,
3245 * Allocate and initialize request queue and default I/O scheduler.
3247 static int dasd_alloc_queue(struct dasd_block *block)
3251 block->tag_set.ops = &dasd_mq_ops;
3252 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
3253 block->tag_set.nr_hw_queues = nr_hw_queues;
3254 block->tag_set.queue_depth = queue_depth;
3255 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
3256 block->tag_set.numa_node = NUMA_NO_NODE;
3258 rc = blk_mq_alloc_tag_set(&block->tag_set);
3262 block->request_queue = blk_mq_init_queue(&block->tag_set);
3263 if (IS_ERR(block->request_queue))
3264 return PTR_ERR(block->request_queue);
3266 block->request_queue->queuedata = block;
3272 * Deactivate and free request queue.
3274 static void dasd_free_queue(struct dasd_block *block)
3276 if (block->request_queue) {
3277 blk_cleanup_queue(block->request_queue);
3278 blk_mq_free_tag_set(&block->tag_set);
3279 block->request_queue = NULL;
3283 static int dasd_open(struct block_device *bdev, fmode_t mode)
3285 struct dasd_device *base;
3288 base = dasd_device_from_gendisk(bdev->bd_disk);
3292 atomic_inc(&base->block->open_count);
3293 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3298 if (!try_module_get(base->discipline->owner)) {
3303 if (dasd_probeonly) {
3304 dev_info(&base->cdev->dev,
3305 "Accessing the DASD failed because it is in "
3306 "probeonly mode\n");
3311 if (base->state <= DASD_STATE_BASIC) {
3312 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3313 " Cannot open unrecognized device");
3318 if ((mode & FMODE_WRITE) &&
3319 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3320 (base->features & DASD_FEATURE_READONLY))) {
3325 dasd_put_device(base);
3329 module_put(base->discipline->owner);
3331 atomic_dec(&base->block->open_count);
3332 dasd_put_device(base);
3336 static void dasd_release(struct gendisk *disk, fmode_t mode)
3338 struct dasd_device *base = dasd_device_from_gendisk(disk);
3340 atomic_dec(&base->block->open_count);
3341 module_put(base->discipline->owner);
3342 dasd_put_device(base);
3347 * Return disk geometry.
3349 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3351 struct dasd_device *base;
3353 base = dasd_device_from_gendisk(bdev->bd_disk);
3357 if (!base->discipline ||
3358 !base->discipline->fill_geometry) {
3359 dasd_put_device(base);
3362 base->discipline->fill_geometry(base->block, geo);
3363 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3364 dasd_put_device(base);
3368 const struct block_device_operations
3369 dasd_device_operations = {
3370 .owner = THIS_MODULE,
3372 .release = dasd_release,
3373 .ioctl = dasd_ioctl,
3374 .compat_ioctl = dasd_ioctl,
3375 .getgeo = dasd_getgeo,
3378 /*******************************************************************************
3379 * end of block device operations
3385 #ifdef CONFIG_PROC_FS
3389 kmem_cache_destroy(dasd_page_cache);
3390 dasd_page_cache = NULL;
3391 dasd_gendisk_exit();
3393 if (dasd_debug_area != NULL) {
3394 debug_unregister(dasd_debug_area);
3395 dasd_debug_area = NULL;
3397 dasd_statistics_removeroot();
3401 * SECTION: common functions for ccw_driver use
3405 * Is the device read-only?
3406 * Note that this function does not report the setting of the
3407 * readonly device attribute, but how it is configured in z/VM.
3409 int dasd_device_is_ro(struct dasd_device *device)
3411 struct ccw_dev_id dev_id;
3412 struct diag210 diag_data;
3417 ccw_device_get_id(device->cdev, &dev_id);
3418 memset(&diag_data, 0, sizeof(diag_data));
3419 diag_data.vrdcdvno = dev_id.devno;
3420 diag_data.vrdclen = sizeof(diag_data);
3421 rc = diag210(&diag_data);
3422 if (rc == 0 || rc == 2) {
3423 return diag_data.vrdcvfla & 0x80;
3425 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3430 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3432 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3434 struct ccw_device *cdev = data;
3437 ret = ccw_device_set_online(cdev);
3439 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3440 dev_name(&cdev->dev), ret);
3444 * Initial attempt at a probe function. this can be simplified once
3445 * the other detection code is gone.
3447 int dasd_generic_probe(struct ccw_device *cdev,
3448 struct dasd_discipline *discipline)
3452 ret = dasd_add_sysfs_files(cdev);
3454 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3455 "dasd_generic_probe: could not add "
3459 cdev->handler = &dasd_int_handler;
3462 * Automatically online either all dasd devices (dasd_autodetect)
3463 * or all devices specified with dasd= parameters during
3466 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3467 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3468 async_schedule(dasd_generic_auto_online, cdev);
3471 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3473 void dasd_generic_free_discipline(struct dasd_device *device)
3475 /* Forget the discipline information. */
3476 if (device->discipline) {
3477 if (device->discipline->uncheck_device)
3478 device->discipline->uncheck_device(device);
3479 module_put(device->discipline->owner);
3480 device->discipline = NULL;
3482 if (device->base_discipline) {
3483 module_put(device->base_discipline->owner);
3484 device->base_discipline = NULL;
3487 EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3490 * This will one day be called from a global not_oper handler.
3491 * It is also used by driver_unregister during module unload.
3493 void dasd_generic_remove(struct ccw_device *cdev)
3495 struct dasd_device *device;
3496 struct dasd_block *block;
3498 cdev->handler = NULL;
3500 device = dasd_device_from_cdev(cdev);
3501 if (IS_ERR(device)) {
3502 dasd_remove_sysfs_files(cdev);
3505 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3506 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3507 /* Already doing offline processing */
3508 dasd_put_device(device);
3509 dasd_remove_sysfs_files(cdev);
3513 * This device is removed unconditionally. Set offline
3514 * flag to prevent dasd_open from opening it while it is
3515 * no quite down yet.
3517 dasd_set_target_state(device, DASD_STATE_NEW);
3518 /* dasd_delete_device destroys the device reference. */
3519 block = device->block;
3520 dasd_delete_device(device);
3522 * life cycle of block is bound to device, so delete it after
3523 * device was safely removed
3526 dasd_free_block(block);
3528 dasd_remove_sysfs_files(cdev);
3530 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3533 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3534 * the device is detected for the first time and is supposed to be used
3535 * or the user has started activation through sysfs.
3537 int dasd_generic_set_online(struct ccw_device *cdev,
3538 struct dasd_discipline *base_discipline)
3540 struct dasd_discipline *discipline;
3541 struct dasd_device *device;
3544 /* first online clears initial online feature flag */
3545 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3546 device = dasd_create_device(cdev);
3548 return PTR_ERR(device);
3550 discipline = base_discipline;
3551 if (device->features & DASD_FEATURE_USEDIAG) {
3552 if (!dasd_diag_discipline_pointer) {
3553 /* Try to load the required module. */
3554 rc = request_module(DASD_DIAG_MOD);
3556 pr_warn("%s Setting the DASD online failed "
3557 "because the required module %s "
3558 "could not be loaded (rc=%d)\n",
3559 dev_name(&cdev->dev), DASD_DIAG_MOD,
3561 dasd_delete_device(device);
3565 /* Module init could have failed, so check again here after
3566 * request_module(). */
3567 if (!dasd_diag_discipline_pointer) {
3568 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3569 dev_name(&cdev->dev));
3570 dasd_delete_device(device);
3573 discipline = dasd_diag_discipline_pointer;
3575 if (!try_module_get(base_discipline->owner)) {
3576 dasd_delete_device(device);
3579 if (!try_module_get(discipline->owner)) {
3580 module_put(base_discipline->owner);
3581 dasd_delete_device(device);
3584 device->base_discipline = base_discipline;
3585 device->discipline = discipline;
3587 /* check_device will allocate block device if necessary */
3588 rc = discipline->check_device(device);
3590 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3591 dev_name(&cdev->dev), discipline->name, rc);
3592 module_put(discipline->owner);
3593 module_put(base_discipline->owner);
3594 dasd_delete_device(device);
3598 dasd_set_target_state(device, DASD_STATE_ONLINE);
3599 if (device->state <= DASD_STATE_KNOWN) {
3600 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3601 dev_name(&cdev->dev));
3603 dasd_set_target_state(device, DASD_STATE_NEW);
3605 dasd_free_block(device->block);
3606 dasd_delete_device(device);
3608 pr_debug("dasd_generic device %s found\n",
3609 dev_name(&cdev->dev));
3611 wait_event(dasd_init_waitq, _wait_for_device(device));
3613 dasd_put_device(device);
3616 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3618 int dasd_generic_set_offline(struct ccw_device *cdev)
3620 struct dasd_device *device;
3621 struct dasd_block *block;
3622 int max_count, open_count, rc;
3623 unsigned long flags;
3626 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3627 device = dasd_device_from_cdev_locked(cdev);
3628 if (IS_ERR(device)) {
3629 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3630 return PTR_ERR(device);
3634 * We must make sure that this device is currently not in use.
3635 * The open_count is increased for every opener, that includes
3636 * the blkdev_get in dasd_scan_partitions. We are only interested
3637 * in the other openers.
3639 if (device->block) {
3640 max_count = device->block->bdev ? 0 : -1;
3641 open_count = atomic_read(&device->block->open_count);
3642 if (open_count > max_count) {
3644 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3645 dev_name(&cdev->dev), open_count);
3647 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3648 dev_name(&cdev->dev));
3655 * Test if the offline processing is already running and exit if so.
3656 * If a safe offline is being processed this could only be a normal
3657 * offline that should be able to overtake the safe offline and
3658 * cancel any I/O we do not want to wait for any longer
3660 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3661 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3662 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3669 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3672 * if safe_offline is called set safe_offline_running flag and
3673 * clear safe_offline so that a call to normal offline
3674 * can overrun safe_offline processing
3676 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3677 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3678 /* need to unlock here to wait for outstanding I/O */
3679 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3681 * If we want to set the device safe offline all IO operations
3682 * should be finished before continuing the offline process
3683 * so sync bdev first and then wait for our queues to become
3686 if (device->block) {
3687 rc = fsync_bdev(device->block->bdev);
3691 dasd_schedule_device_bh(device);
3692 rc = wait_event_interruptible(shutdown_waitq,
3693 _wait_for_empty_queues(device));
3698 * check if a normal offline process overtook the offline
3699 * processing in this case simply do nothing beside returning
3700 * that we got interrupted
3701 * otherwise mark safe offline as not running any longer and
3702 * continue with normal offline
3704 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3705 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3709 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3711 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3713 dasd_set_target_state(device, DASD_STATE_NEW);
3714 /* dasd_delete_device destroys the device reference. */
3715 block = device->block;
3716 dasd_delete_device(device);
3718 * life cycle of block is bound to device, so delete it after
3719 * device was safely removed
3722 dasd_free_block(block);
3727 /* interrupted by signal */
3728 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3729 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3730 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3732 dasd_put_device(device);
3733 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3736 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3738 int dasd_generic_last_path_gone(struct dasd_device *device)
3740 struct dasd_ccw_req *cqr;
3742 dev_warn(&device->cdev->dev, "No operational channel path is left "
3743 "for the device\n");
3744 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3745 /* First of all call extended error reporting. */
3746 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3748 if (device->state < DASD_STATE_BASIC)
3750 /* Device is active. We want to keep it. */
3751 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3752 if ((cqr->status == DASD_CQR_IN_IO) ||
3753 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3754 cqr->status = DASD_CQR_QUEUED;
3757 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3758 dasd_device_clear_timer(device);
3759 dasd_schedule_device_bh(device);
3762 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3764 int dasd_generic_path_operational(struct dasd_device *device)
3766 dev_info(&device->cdev->dev, "A channel path to the device has become "
3768 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3769 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3770 if (device->stopped & DASD_UNRESUMED_PM) {
3771 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3772 dasd_restore_device(device);
3775 dasd_schedule_device_bh(device);
3776 if (device->block) {
3777 dasd_schedule_block_bh(device->block);
3778 if (device->block->request_queue)
3779 blk_mq_run_hw_queues(device->block->request_queue,
3783 if (!device->stopped)
3784 wake_up(&generic_waitq);
3788 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3790 int dasd_generic_notify(struct ccw_device *cdev, int event)
3792 struct dasd_device *device;
3795 device = dasd_device_from_cdev_locked(cdev);
3803 dasd_path_no_path(device);
3804 ret = dasd_generic_last_path_gone(device);
3808 if (dasd_path_get_opm(device))
3809 ret = dasd_generic_path_operational(device);
3812 dasd_put_device(device);
3815 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3817 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3819 struct dasd_device *device;
3820 int chp, oldopm, hpfpm, ifccpm;
3822 device = dasd_device_from_cdev_locked(cdev);
3826 oldopm = dasd_path_get_opm(device);
3827 for (chp = 0; chp < 8; chp++) {
3828 if (path_event[chp] & PE_PATH_GONE) {
3829 dasd_path_notoper(device, chp);
3831 if (path_event[chp] & PE_PATH_AVAILABLE) {
3832 dasd_path_available(device, chp);
3833 dasd_schedule_device_bh(device);
3835 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3836 if (!dasd_path_is_operational(device, chp) &&
3837 !dasd_path_need_verify(device, chp)) {
3839 * we can not establish a pathgroup on an
3840 * unavailable path, so trigger a path
3841 * verification first
3843 dasd_path_available(device, chp);
3844 dasd_schedule_device_bh(device);
3846 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3847 "Pathgroup re-established\n");
3848 if (device->discipline->kick_validate)
3849 device->discipline->kick_validate(device);
3852 hpfpm = dasd_path_get_hpfpm(device);
3853 ifccpm = dasd_path_get_ifccpm(device);
3854 if (!dasd_path_get_opm(device) && hpfpm) {
3856 * device has no operational paths but at least one path is
3857 * disabled due to HPF errors
3858 * disable HPF at all and use the path(s) again
3860 if (device->discipline->disable_hpf)
3861 device->discipline->disable_hpf(device);
3862 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3863 dasd_path_set_tbvpm(device, hpfpm);
3864 dasd_schedule_device_bh(device);
3865 dasd_schedule_requeue(device);
3866 } else if (!dasd_path_get_opm(device) && ifccpm) {
3868 * device has no operational paths but at least one path is
3869 * disabled due to IFCC errors
3870 * trigger path verification on paths with IFCC errors
3872 dasd_path_set_tbvpm(device, ifccpm);
3873 dasd_schedule_device_bh(device);
3875 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
3876 dev_warn(&device->cdev->dev,
3877 "No verified channel paths remain for the device\n");
3878 DBF_DEV_EVENT(DBF_WARNING, device,
3879 "%s", "last verified path gone");
3880 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3881 dasd_device_set_stop_bits(device,
3882 DASD_STOPPED_DC_WAIT);
3884 dasd_put_device(device);
3886 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3888 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3890 if (!dasd_path_get_opm(device) && lpm) {
3891 dasd_path_set_opm(device, lpm);
3892 dasd_generic_path_operational(device);
3894 dasd_path_add_opm(device, lpm);
3897 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3899 void dasd_generic_space_exhaust(struct dasd_device *device,
3900 struct dasd_ccw_req *cqr)
3902 dasd_eer_write(device, NULL, DASD_EER_NOSPC);
3904 if (device->state < DASD_STATE_BASIC)
3907 if (cqr->status == DASD_CQR_IN_IO ||
3908 cqr->status == DASD_CQR_CLEAR_PENDING) {
3909 cqr->status = DASD_CQR_QUEUED;
3912 dasd_device_set_stop_bits(device, DASD_STOPPED_NOSPC);
3913 dasd_device_clear_timer(device);
3914 dasd_schedule_device_bh(device);
3916 EXPORT_SYMBOL_GPL(dasd_generic_space_exhaust);
3918 void dasd_generic_space_avail(struct dasd_device *device)
3920 dev_info(&device->cdev->dev, "Extent pool space is available\n");
3921 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "space available");
3923 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOSPC);
3924 dasd_schedule_device_bh(device);
3926 if (device->block) {
3927 dasd_schedule_block_bh(device->block);
3928 if (device->block->request_queue)
3929 blk_mq_run_hw_queues(device->block->request_queue, true);
3931 if (!device->stopped)
3932 wake_up(&generic_waitq);
3934 EXPORT_SYMBOL_GPL(dasd_generic_space_avail);
3937 * clear active requests and requeue them to block layer if possible
3939 static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3941 struct list_head requeue_queue;
3942 struct dasd_ccw_req *cqr, *n;
3943 struct dasd_ccw_req *refers;
3946 INIT_LIST_HEAD(&requeue_queue);
3947 spin_lock_irq(get_ccwdev_lock(device->cdev));
3949 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3950 /* Check status and move request to flush_queue */
3951 if (cqr->status == DASD_CQR_IN_IO) {
3952 rc = device->discipline->term_IO(cqr);
3954 /* unable to terminate requeust */
3955 dev_err(&device->cdev->dev,
3956 "Unable to terminate request %p "
3957 "on suspend\n", cqr);
3958 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3959 dasd_put_device(device);
3963 list_move_tail(&cqr->devlist, &requeue_queue);
3965 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3967 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3968 wait_event(dasd_flush_wq,
3969 (cqr->status != DASD_CQR_CLEAR_PENDING));
3972 * requeue requests to blocklayer will only work
3973 * for block device requests
3975 if (_dasd_requeue_request(cqr))
3978 /* remove requests from device and block queue */
3979 list_del_init(&cqr->devlist);
3980 while (cqr->refers != NULL) {
3981 refers = cqr->refers;
3982 /* remove the request from the block queue */
3983 list_del(&cqr->blocklist);
3984 /* free the finished erp request */
3985 dasd_free_erp_request(cqr, cqr->memdev);
3990 * _dasd_requeue_request already checked for a valid
3991 * blockdevice, no need to check again
3992 * all erp requests (cqr->refers) have a cqr->block
3993 * pointer copy from the original cqr
3995 list_del_init(&cqr->blocklist);
3996 cqr->block->base->discipline->free_cp(
3997 cqr, (struct request *) cqr->callback_data);
4001 * if requests remain then they are internal request
4002 * and go back to the device queue
4004 if (!list_empty(&requeue_queue)) {
4005 /* move freeze_queue to start of the ccw_queue */
4006 spin_lock_irq(get_ccwdev_lock(device->cdev));
4007 list_splice_tail(&requeue_queue, &device->ccw_queue);
4008 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4010 dasd_schedule_device_bh(device);
4014 static void do_requeue_requests(struct work_struct *work)
4016 struct dasd_device *device = container_of(work, struct dasd_device,
4018 dasd_generic_requeue_all_requests(device);
4019 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
4021 dasd_schedule_block_bh(device->block);
4022 dasd_put_device(device);
4025 void dasd_schedule_requeue(struct dasd_device *device)
4027 dasd_get_device(device);
4028 /* queue call to dasd_reload_device to the kernel event daemon. */
4029 if (!schedule_work(&device->requeue_requests))
4030 dasd_put_device(device);
4032 EXPORT_SYMBOL(dasd_schedule_requeue);
4034 int dasd_generic_pm_freeze(struct ccw_device *cdev)
4036 struct dasd_device *device = dasd_device_from_cdev(cdev);
4039 return PTR_ERR(device);
4041 /* mark device as suspended */
4042 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
4044 if (device->discipline->freeze)
4045 device->discipline->freeze(device);
4047 /* disallow new I/O */
4048 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
4050 return dasd_generic_requeue_all_requests(device);
4052 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
4054 int dasd_generic_restore_device(struct ccw_device *cdev)
4056 struct dasd_device *device = dasd_device_from_cdev(cdev);
4060 return PTR_ERR(device);
4062 /* allow new IO again */
4063 dasd_device_remove_stop_bits(device,
4064 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
4066 dasd_schedule_device_bh(device);
4069 * call discipline restore function
4070 * if device is stopped do nothing e.g. for disconnected devices
4072 if (device->discipline->restore && !(device->stopped))
4073 rc = device->discipline->restore(device);
4074 if (rc || device->stopped)
4076 * if the resume failed for the DASD we put it in
4077 * an UNRESUMED stop state
4079 device->stopped |= DASD_UNRESUMED_PM;
4081 if (device->block) {
4082 dasd_schedule_block_bh(device->block);
4083 if (device->block->request_queue)
4084 blk_mq_run_hw_queues(device->block->request_queue,
4088 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
4089 dasd_put_device(device);
4092 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
4094 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
4095 int rdc_buffer_size,
4098 struct dasd_ccw_req *cqr;
4101 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
4105 /* internal error 13 - Allocating the RDC request failed*/
4106 dev_err(&device->cdev->dev,
4107 "An error occurred in the DASD device driver, "
4108 "reason=%s\n", "13");
4113 ccw->cmd_code = CCW_CMD_RDC;
4114 ccw->cda = (__u32)(addr_t) cqr->data;
4116 ccw->count = rdc_buffer_size;
4117 cqr->startdev = device;
4118 cqr->memdev = device;
4119 cqr->expires = 10*HZ;
4121 cqr->buildclk = get_tod_clock();
4122 cqr->status = DASD_CQR_FILLED;
4127 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
4128 void *rdc_buffer, int rdc_buffer_size)
4131 struct dasd_ccw_req *cqr;
4133 cqr = dasd_generic_build_rdc(device, rdc_buffer_size, magic);
4135 return PTR_ERR(cqr);
4137 ret = dasd_sleep_on(cqr);
4139 memcpy(rdc_buffer, cqr->data, rdc_buffer_size);
4140 dasd_sfree_request(cqr, cqr->memdev);
4143 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
4146 * In command mode and transport mode we need to look for sense
4147 * data in different places. The sense data itself is allways
4148 * an array of 32 bytes, so we can unify the sense data access
4151 char *dasd_get_sense(struct irb *irb)
4153 struct tsb *tsb = NULL;
4156 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4157 if (irb->scsw.tm.tcw)
4158 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4160 if (tsb && tsb->length == 64 && tsb->flags)
4161 switch (tsb->flags & 0x07) {
4162 case 1: /* tsa_iostat */
4163 sense = tsb->tsa.iostat.sense;
4165 case 2: /* tsa_ddpc */
4166 sense = tsb->tsa.ddpc.sense;
4169 /* currently we don't use interrogate data */
4172 } else if (irb->esw.esw0.erw.cons) {
4177 EXPORT_SYMBOL_GPL(dasd_get_sense);
4179 void dasd_generic_shutdown(struct ccw_device *cdev)
4181 struct dasd_device *device;
4183 device = dasd_device_from_cdev(cdev);
4188 dasd_schedule_block_bh(device->block);
4190 dasd_schedule_device_bh(device);
4192 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4194 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4196 static int __init dasd_init(void)
4200 init_waitqueue_head(&dasd_init_waitq);
4201 init_waitqueue_head(&dasd_flush_wq);
4202 init_waitqueue_head(&generic_waitq);
4203 init_waitqueue_head(&shutdown_waitq);
4205 /* register 'common' DASD debug area, used for all DBF_XXX calls */
4206 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
4207 if (dasd_debug_area == NULL) {
4211 debug_register_view(dasd_debug_area, &debug_sprintf_view);
4212 debug_set_level(dasd_debug_area, DBF_WARNING);
4214 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4216 dasd_diag_discipline_pointer = NULL;
4218 dasd_statistics_createroot();
4220 rc = dasd_devmap_init();
4223 rc = dasd_gendisk_init();
4229 rc = dasd_eer_init();
4232 #ifdef CONFIG_PROC_FS
4233 rc = dasd_proc_init();
4240 pr_info("The DASD device driver could not be initialized\n");
4245 module_init(dasd_init);
4246 module_exit(dasd_exit);