2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
7 * Copyright IBM Corp. 1999, 2009
10 #define KMSG_COMPONENT "dasd"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kmod.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/ctype.h>
17 #include <linux/major.h>
18 #include <linux/slab.h>
19 #include <linux/hdreg.h>
20 #include <linux/async.h>
21 #include <linux/mutex.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/vmalloc.h>
26 #include <asm/ccwdev.h>
27 #include <asm/ebcdic.h>
28 #include <asm/idals.h>
33 #define PRINTK_HEADER "dasd:"
37 * SECTION: Constant definitions to be used within this file
39 #define DASD_CHANQ_MAX_SIZE 4
41 #define DASD_SLEEPON_START_TAG (void *) 1
42 #define DASD_SLEEPON_END_TAG (void *) 2
45 * SECTION: exported variables of dasd.c
47 debug_info_t *dasd_debug_area;
48 static struct dentry *dasd_debugfs_root_entry;
49 struct dasd_discipline *dasd_diag_discipline_pointer;
50 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
52 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
53 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
54 " Copyright IBM Corp. 2000");
55 MODULE_SUPPORTED_DEVICE("dasd");
56 MODULE_LICENSE("GPL");
59 * SECTION: prototypes for static functions of dasd.c
61 static int dasd_alloc_queue(struct dasd_block *);
62 static void dasd_setup_queue(struct dasd_block *);
63 static void dasd_free_queue(struct dasd_block *);
64 static void dasd_flush_request_queue(struct dasd_block *);
65 static int dasd_flush_block_queue(struct dasd_block *);
66 static void dasd_device_tasklet(struct dasd_device *);
67 static void dasd_block_tasklet(struct dasd_block *);
68 static void do_kick_device(struct work_struct *);
69 static void do_restore_device(struct work_struct *);
70 static void do_reload_device(struct work_struct *);
71 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
72 static void dasd_device_timeout(unsigned long);
73 static void dasd_block_timeout(unsigned long);
74 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
75 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
76 static void dasd_profile_exit(struct dasd_profile *);
79 * SECTION: Operations on the device structure.
81 static wait_queue_head_t dasd_init_waitq;
82 static wait_queue_head_t dasd_flush_wq;
83 static wait_queue_head_t generic_waitq;
84 static wait_queue_head_t shutdown_waitq;
87 * Allocate memory for a new device structure.
89 struct dasd_device *dasd_alloc_device(void)
91 struct dasd_device *device;
93 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
95 return ERR_PTR(-ENOMEM);
97 /* Get two pages for normal block device operations. */
98 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
99 if (!device->ccw_mem) {
101 return ERR_PTR(-ENOMEM);
103 /* Get one page for error recovery. */
104 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
105 if (!device->erp_mem) {
106 free_pages((unsigned long) device->ccw_mem, 1);
108 return ERR_PTR(-ENOMEM);
111 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
112 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
113 spin_lock_init(&device->mem_lock);
114 atomic_set(&device->tasklet_scheduled, 0);
115 tasklet_init(&device->tasklet,
116 (void (*)(unsigned long)) dasd_device_tasklet,
117 (unsigned long) device);
118 INIT_LIST_HEAD(&device->ccw_queue);
119 init_timer(&device->timer);
120 device->timer.function = dasd_device_timeout;
121 device->timer.data = (unsigned long) device;
122 INIT_WORK(&device->kick_work, do_kick_device);
123 INIT_WORK(&device->restore_device, do_restore_device);
124 INIT_WORK(&device->reload_device, do_reload_device);
125 device->state = DASD_STATE_NEW;
126 device->target = DASD_STATE_NEW;
127 mutex_init(&device->state_mutex);
128 spin_lock_init(&device->profile.lock);
133 * Free memory of a device structure.
135 void dasd_free_device(struct dasd_device *device)
137 kfree(device->private);
138 free_page((unsigned long) device->erp_mem);
139 free_pages((unsigned long) device->ccw_mem, 1);
144 * Allocate memory for a new device structure.
146 struct dasd_block *dasd_alloc_block(void)
148 struct dasd_block *block;
150 block = kzalloc(sizeof(*block), GFP_ATOMIC);
152 return ERR_PTR(-ENOMEM);
153 /* open_count = 0 means device online but not in use */
154 atomic_set(&block->open_count, -1);
156 spin_lock_init(&block->request_queue_lock);
157 atomic_set(&block->tasklet_scheduled, 0);
158 tasklet_init(&block->tasklet,
159 (void (*)(unsigned long)) dasd_block_tasklet,
160 (unsigned long) block);
161 INIT_LIST_HEAD(&block->ccw_queue);
162 spin_lock_init(&block->queue_lock);
163 init_timer(&block->timer);
164 block->timer.function = dasd_block_timeout;
165 block->timer.data = (unsigned long) block;
166 spin_lock_init(&block->profile.lock);
172 * Free memory of a device structure.
174 void dasd_free_block(struct dasd_block *block)
180 * Make a new device known to the system.
182 static int dasd_state_new_to_known(struct dasd_device *device)
187 * As long as the device is not in state DASD_STATE_NEW we want to
188 * keep the reference count > 0.
190 dasd_get_device(device);
193 rc = dasd_alloc_queue(device->block);
195 dasd_put_device(device);
199 device->state = DASD_STATE_KNOWN;
204 * Let the system forget about a device.
206 static int dasd_state_known_to_new(struct dasd_device *device)
208 /* Disable extended error reporting for this device. */
209 dasd_eer_disable(device);
210 /* Forget the discipline information. */
211 if (device->discipline) {
212 if (device->discipline->uncheck_device)
213 device->discipline->uncheck_device(device);
214 module_put(device->discipline->owner);
216 device->discipline = NULL;
217 if (device->base_discipline)
218 module_put(device->base_discipline->owner);
219 device->base_discipline = NULL;
220 device->state = DASD_STATE_NEW;
223 dasd_free_queue(device->block);
225 /* Give up reference we took in dasd_state_new_to_known. */
226 dasd_put_device(device);
230 static struct dentry *dasd_debugfs_setup(const char *name,
231 struct dentry *base_dentry)
237 pde = debugfs_create_dir(name, base_dentry);
238 if (!pde || IS_ERR(pde))
244 * Request the irq line for the device.
246 static int dasd_state_known_to_basic(struct dasd_device *device)
248 struct dasd_block *block = device->block;
251 /* Allocate and register gendisk structure. */
253 rc = dasd_gendisk_alloc(block);
256 block->debugfs_dentry =
257 dasd_debugfs_setup(block->gdp->disk_name,
258 dasd_debugfs_root_entry);
259 dasd_profile_init(&block->profile, block->debugfs_dentry);
260 if (dasd_global_profile_level == DASD_PROFILE_ON)
261 dasd_profile_on(&device->block->profile);
263 device->debugfs_dentry =
264 dasd_debugfs_setup(dev_name(&device->cdev->dev),
265 dasd_debugfs_root_entry);
266 dasd_profile_init(&device->profile, device->debugfs_dentry);
268 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
269 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
271 debug_register_view(device->debug_area, &debug_sprintf_view);
272 debug_set_level(device->debug_area, DBF_WARNING);
273 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
275 device->state = DASD_STATE_BASIC;
281 * Release the irq line for the device. Terminate any running i/o.
283 static int dasd_state_basic_to_known(struct dasd_device *device)
288 dasd_profile_exit(&device->block->profile);
289 if (device->block->debugfs_dentry)
290 debugfs_remove(device->block->debugfs_dentry);
291 dasd_gendisk_free(device->block);
292 dasd_block_clear_timer(device->block);
294 rc = dasd_flush_device_queue(device);
297 dasd_device_clear_timer(device);
298 dasd_profile_exit(&device->profile);
299 if (device->debugfs_dentry)
300 debugfs_remove(device->debugfs_dentry);
302 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
303 if (device->debug_area != NULL) {
304 debug_unregister(device->debug_area);
305 device->debug_area = NULL;
307 device->state = DASD_STATE_KNOWN;
312 * Do the initial analysis. The do_analysis function may return
313 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
314 * until the discipline decides to continue the startup sequence
315 * by calling the function dasd_change_state. The eckd disciplines
316 * uses this to start a ccw that detects the format. The completion
317 * interrupt for this detection ccw uses the kernel event daemon to
318 * trigger the call to dasd_change_state. All this is done in the
319 * discipline code, see dasd_eckd.c.
320 * After the analysis ccw is done (do_analysis returned 0) the block
322 * In case the analysis returns an error, the device setup is stopped
323 * (a fake disk was already added to allow formatting).
325 static int dasd_state_basic_to_ready(struct dasd_device *device)
328 struct dasd_block *block;
331 block = device->block;
332 /* make disk known with correct capacity */
334 if (block->base->discipline->do_analysis != NULL)
335 rc = block->base->discipline->do_analysis(block);
338 device->state = DASD_STATE_UNFMT;
343 dasd_setup_queue(block);
344 set_capacity(block->gdp,
345 block->blocks << block->s2b_shift);
346 device->state = DASD_STATE_READY;
347 rc = dasd_scan_partitions(block);
349 device->state = DASD_STATE_BASIC;
353 device->state = DASD_STATE_READY;
356 if (device->discipline->basic_to_ready)
357 rc = device->discipline->basic_to_ready(device);
362 int _wait_for_empty_queues(struct dasd_device *device)
365 return list_empty(&device->ccw_queue) &&
366 list_empty(&device->block->ccw_queue);
368 return list_empty(&device->ccw_queue);
372 * Remove device from block device layer. Destroy dirty buffers.
373 * Forget format information. Check if the target level is basic
374 * and if it is create fake disk for formatting.
376 static int dasd_state_ready_to_basic(struct dasd_device *device)
380 if (device->discipline->ready_to_basic) {
381 rc = device->discipline->ready_to_basic(device);
385 device->state = DASD_STATE_BASIC;
387 struct dasd_block *block = device->block;
388 rc = dasd_flush_block_queue(block);
390 device->state = DASD_STATE_READY;
393 dasd_flush_request_queue(block);
394 dasd_destroy_partitions(block);
397 block->s2b_shift = 0;
405 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
407 device->state = DASD_STATE_BASIC;
412 * Make the device online and schedule the bottom half to start
413 * the requeueing of requests from the linux request queue to the
417 dasd_state_ready_to_online(struct dasd_device * device)
419 struct gendisk *disk;
420 struct disk_part_iter piter;
421 struct hd_struct *part;
423 device->state = DASD_STATE_ONLINE;
425 dasd_schedule_block_bh(device->block);
426 if ((device->features & DASD_FEATURE_USERAW)) {
427 disk = device->block->gdp;
428 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
431 disk = device->block->bdev->bd_disk;
432 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
433 while ((part = disk_part_iter_next(&piter)))
434 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
435 disk_part_iter_exit(&piter);
441 * Stop the requeueing of requests again.
443 static int dasd_state_online_to_ready(struct dasd_device *device)
446 struct gendisk *disk;
447 struct disk_part_iter piter;
448 struct hd_struct *part;
450 if (device->discipline->online_to_ready) {
451 rc = device->discipline->online_to_ready(device);
456 device->state = DASD_STATE_READY;
457 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
458 disk = device->block->bdev->bd_disk;
459 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
460 while ((part = disk_part_iter_next(&piter)))
461 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
462 disk_part_iter_exit(&piter);
468 * Device startup state changes.
470 static int dasd_increase_state(struct dasd_device *device)
475 if (device->state == DASD_STATE_NEW &&
476 device->target >= DASD_STATE_KNOWN)
477 rc = dasd_state_new_to_known(device);
480 device->state == DASD_STATE_KNOWN &&
481 device->target >= DASD_STATE_BASIC)
482 rc = dasd_state_known_to_basic(device);
485 device->state == DASD_STATE_BASIC &&
486 device->target >= DASD_STATE_READY)
487 rc = dasd_state_basic_to_ready(device);
490 device->state == DASD_STATE_UNFMT &&
491 device->target > DASD_STATE_UNFMT)
495 device->state == DASD_STATE_READY &&
496 device->target >= DASD_STATE_ONLINE)
497 rc = dasd_state_ready_to_online(device);
503 * Device shutdown state changes.
505 static int dasd_decrease_state(struct dasd_device *device)
510 if (device->state == DASD_STATE_ONLINE &&
511 device->target <= DASD_STATE_READY)
512 rc = dasd_state_online_to_ready(device);
515 device->state == DASD_STATE_READY &&
516 device->target <= DASD_STATE_BASIC)
517 rc = dasd_state_ready_to_basic(device);
520 device->state == DASD_STATE_UNFMT &&
521 device->target <= DASD_STATE_BASIC)
522 rc = dasd_state_unfmt_to_basic(device);
525 device->state == DASD_STATE_BASIC &&
526 device->target <= DASD_STATE_KNOWN)
527 rc = dasd_state_basic_to_known(device);
530 device->state == DASD_STATE_KNOWN &&
531 device->target <= DASD_STATE_NEW)
532 rc = dasd_state_known_to_new(device);
538 * This is the main startup/shutdown routine.
540 static void dasd_change_state(struct dasd_device *device)
544 if (device->state == device->target)
545 /* Already where we want to go today... */
547 if (device->state < device->target)
548 rc = dasd_increase_state(device);
550 rc = dasd_decrease_state(device);
554 device->target = device->state;
556 /* let user-space know that the device status changed */
557 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
559 if (device->state == device->target)
560 wake_up(&dasd_init_waitq);
564 * Kick starter for devices that did not complete the startup/shutdown
565 * procedure or were sleeping because of a pending state.
566 * dasd_kick_device will schedule a call do do_kick_device to the kernel
569 static void do_kick_device(struct work_struct *work)
571 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
572 mutex_lock(&device->state_mutex);
573 dasd_change_state(device);
574 mutex_unlock(&device->state_mutex);
575 dasd_schedule_device_bh(device);
576 dasd_put_device(device);
579 void dasd_kick_device(struct dasd_device *device)
581 dasd_get_device(device);
582 /* queue call to dasd_kick_device to the kernel event daemon. */
583 schedule_work(&device->kick_work);
587 * dasd_reload_device will schedule a call do do_reload_device to the kernel
590 static void do_reload_device(struct work_struct *work)
592 struct dasd_device *device = container_of(work, struct dasd_device,
594 device->discipline->reload(device);
595 dasd_put_device(device);
598 void dasd_reload_device(struct dasd_device *device)
600 dasd_get_device(device);
601 /* queue call to dasd_reload_device to the kernel event daemon. */
602 schedule_work(&device->reload_device);
604 EXPORT_SYMBOL(dasd_reload_device);
607 * dasd_restore_device will schedule a call do do_restore_device to the kernel
610 static void do_restore_device(struct work_struct *work)
612 struct dasd_device *device = container_of(work, struct dasd_device,
614 device->cdev->drv->restore(device->cdev);
615 dasd_put_device(device);
618 void dasd_restore_device(struct dasd_device *device)
620 dasd_get_device(device);
621 /* queue call to dasd_restore_device to the kernel event daemon. */
622 schedule_work(&device->restore_device);
626 * Set the target state for a device and starts the state change.
628 void dasd_set_target_state(struct dasd_device *device, int target)
630 dasd_get_device(device);
631 mutex_lock(&device->state_mutex);
632 /* If we are in probeonly mode stop at DASD_STATE_READY. */
633 if (dasd_probeonly && target > DASD_STATE_READY)
634 target = DASD_STATE_READY;
635 if (device->target != target) {
636 if (device->state == target)
637 wake_up(&dasd_init_waitq);
638 device->target = target;
640 if (device->state != device->target)
641 dasd_change_state(device);
642 mutex_unlock(&device->state_mutex);
643 dasd_put_device(device);
647 * Enable devices with device numbers in [from..to].
649 static inline int _wait_for_device(struct dasd_device *device)
651 return (device->state == device->target);
654 void dasd_enable_device(struct dasd_device *device)
656 dasd_set_target_state(device, DASD_STATE_ONLINE);
657 if (device->state <= DASD_STATE_KNOWN)
658 /* No discipline for device found. */
659 dasd_set_target_state(device, DASD_STATE_NEW);
660 /* Now wait for the devices to come up. */
661 wait_event(dasd_init_waitq, _wait_for_device(device));
663 dasd_reload_device(device);
664 if (device->discipline->kick_validate)
665 device->discipline->kick_validate(device);
669 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
672 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
674 #ifdef CONFIG_DASD_PROFILE
675 struct dasd_profile_info dasd_global_profile_data;
676 static struct dentry *dasd_global_profile_dentry;
677 static struct dentry *dasd_debugfs_global_entry;
680 * Add profiling information for cqr before execution.
682 static void dasd_profile_start(struct dasd_block *block,
683 struct dasd_ccw_req *cqr,
687 unsigned int counter;
688 struct dasd_device *device;
690 /* count the length of the chanq for statistics */
692 if (dasd_global_profile_level || block->profile.data)
693 list_for_each(l, &block->ccw_queue)
697 if (dasd_global_profile_level) {
698 dasd_global_profile_data.dasd_io_nr_req[counter]++;
699 if (rq_data_dir(req) == READ)
700 dasd_global_profile_data.dasd_read_nr_req[counter]++;
703 spin_lock(&block->profile.lock);
704 if (block->profile.data)
705 block->profile.data->dasd_io_nr_req[counter]++;
706 if (rq_data_dir(req) == READ)
707 block->profile.data->dasd_read_nr_req[counter]++;
708 spin_unlock(&block->profile.lock);
711 * We count the request for the start device, even though it may run on
712 * some other device due to error recovery. This way we make sure that
713 * we count each request only once.
715 device = cqr->startdev;
716 if (device->profile.data) {
717 counter = 1; /* request is not yet queued on the start device */
718 list_for_each(l, &device->ccw_queue)
722 spin_lock(&device->profile.lock);
723 if (device->profile.data) {
724 device->profile.data->dasd_io_nr_req[counter]++;
725 if (rq_data_dir(req) == READ)
726 device->profile.data->dasd_read_nr_req[counter]++;
728 spin_unlock(&device->profile.lock);
732 * Add profiling information for cqr after execution.
735 #define dasd_profile_counter(value, index) \
737 for (index = 0; index < 31 && value >> (2+index); index++) \
741 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
754 /* in case of an overflow, reset the whole profile */
755 if (data->dasd_io_reqs == UINT_MAX) {
756 memset(data, 0, sizeof(*data));
757 getnstimeofday(&data->starttod);
759 data->dasd_io_reqs++;
760 data->dasd_io_sects += sectors;
762 data->dasd_io_alias++;
766 data->dasd_io_secs[sectors_ind]++;
767 data->dasd_io_times[tottime_ind]++;
768 data->dasd_io_timps[tottimeps_ind]++;
769 data->dasd_io_time1[strtime_ind]++;
770 data->dasd_io_time2[irqtime_ind]++;
771 data->dasd_io_time2ps[irqtimeps_ind]++;
772 data->dasd_io_time3[endtime_ind]++;
775 data->dasd_read_reqs++;
776 data->dasd_read_sects += sectors;
778 data->dasd_read_alias++;
780 data->dasd_read_tpm++;
781 data->dasd_read_secs[sectors_ind]++;
782 data->dasd_read_times[tottime_ind]++;
783 data->dasd_read_time1[strtime_ind]++;
784 data->dasd_read_time2[irqtime_ind]++;
785 data->dasd_read_time3[endtime_ind]++;
789 static void dasd_profile_end(struct dasd_block *block,
790 struct dasd_ccw_req *cqr,
793 long strtime, irqtime, endtime, tottime; /* in microseconds */
794 long tottimeps, sectors;
795 struct dasd_device *device;
796 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
797 int irqtime_ind, irqtimeps_ind, endtime_ind;
799 device = cqr->startdev;
800 if (!(dasd_global_profile_level ||
801 block->profile.data ||
802 device->profile.data))
805 sectors = blk_rq_sectors(req);
806 if (!cqr->buildclk || !cqr->startclk ||
807 !cqr->stopclk || !cqr->endclk ||
811 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
812 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
813 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
814 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
815 tottimeps = tottime / sectors;
817 dasd_profile_counter(sectors, sectors_ind);
818 dasd_profile_counter(tottime, tottime_ind);
819 dasd_profile_counter(tottimeps, tottimeps_ind);
820 dasd_profile_counter(strtime, strtime_ind);
821 dasd_profile_counter(irqtime, irqtime_ind);
822 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
823 dasd_profile_counter(endtime, endtime_ind);
825 if (dasd_global_profile_level) {
826 dasd_profile_end_add_data(&dasd_global_profile_data,
827 cqr->startdev != block->base,
829 rq_data_dir(req) == READ,
830 sectors, sectors_ind, tottime_ind,
831 tottimeps_ind, strtime_ind,
832 irqtime_ind, irqtimeps_ind,
836 spin_lock(&block->profile.lock);
837 if (block->profile.data)
838 dasd_profile_end_add_data(block->profile.data,
839 cqr->startdev != block->base,
841 rq_data_dir(req) == READ,
842 sectors, sectors_ind, tottime_ind,
843 tottimeps_ind, strtime_ind,
844 irqtime_ind, irqtimeps_ind,
846 spin_unlock(&block->profile.lock);
848 spin_lock(&device->profile.lock);
849 if (device->profile.data)
850 dasd_profile_end_add_data(device->profile.data,
851 cqr->startdev != block->base,
853 rq_data_dir(req) == READ,
854 sectors, sectors_ind, tottime_ind,
855 tottimeps_ind, strtime_ind,
856 irqtime_ind, irqtimeps_ind,
858 spin_unlock(&device->profile.lock);
861 void dasd_profile_reset(struct dasd_profile *profile)
863 struct dasd_profile_info *data;
865 spin_lock_bh(&profile->lock);
866 data = profile->data;
868 spin_unlock_bh(&profile->lock);
871 memset(data, 0, sizeof(*data));
872 getnstimeofday(&data->starttod);
873 spin_unlock_bh(&profile->lock);
876 void dasd_global_profile_reset(void)
878 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
879 getnstimeofday(&dasd_global_profile_data.starttod);
882 int dasd_profile_on(struct dasd_profile *profile)
884 struct dasd_profile_info *data;
886 data = kzalloc(sizeof(*data), GFP_KERNEL);
889 spin_lock_bh(&profile->lock);
891 spin_unlock_bh(&profile->lock);
895 getnstimeofday(&data->starttod);
896 profile->data = data;
897 spin_unlock_bh(&profile->lock);
901 void dasd_profile_off(struct dasd_profile *profile)
903 spin_lock_bh(&profile->lock);
904 kfree(profile->data);
905 profile->data = NULL;
906 spin_unlock_bh(&profile->lock);
909 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
913 buffer = vmalloc(user_len + 1);
915 return ERR_PTR(-ENOMEM);
916 if (copy_from_user(buffer, user_buf, user_len) != 0) {
918 return ERR_PTR(-EFAULT);
920 /* got the string, now strip linefeed. */
921 if (buffer[user_len - 1] == '\n')
922 buffer[user_len - 1] = 0;
924 buffer[user_len] = 0;
928 static ssize_t dasd_stats_write(struct file *file,
929 const char __user *user_buf,
930 size_t user_len, loff_t *pos)
934 struct seq_file *m = (struct seq_file *)file->private_data;
935 struct dasd_profile *prof = m->private;
937 if (user_len > 65536)
939 buffer = dasd_get_user_string(user_buf, user_len);
941 return PTR_ERR(buffer);
943 str = skip_spaces(buffer);
945 if (strncmp(str, "reset", 5) == 0) {
946 dasd_profile_reset(prof);
947 } else if (strncmp(str, "on", 2) == 0) {
948 rc = dasd_profile_on(prof);
951 } else if (strncmp(str, "off", 3) == 0) {
952 dasd_profile_off(prof);
959 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
963 for (i = 0; i < 32; i++)
964 seq_printf(m, "%u ", array[i]);
968 static void dasd_stats_seq_print(struct seq_file *m,
969 struct dasd_profile_info *data)
971 seq_printf(m, "start_time %ld.%09ld\n",
972 data->starttod.tv_sec, data->starttod.tv_nsec);
973 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
974 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
975 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
976 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
977 seq_printf(m, "histogram_sectors ");
978 dasd_stats_array(m, data->dasd_io_secs);
979 seq_printf(m, "histogram_io_times ");
980 dasd_stats_array(m, data->dasd_io_times);
981 seq_printf(m, "histogram_io_times_weighted ");
982 dasd_stats_array(m, data->dasd_io_timps);
983 seq_printf(m, "histogram_time_build_to_ssch ");
984 dasd_stats_array(m, data->dasd_io_time1);
985 seq_printf(m, "histogram_time_ssch_to_irq ");
986 dasd_stats_array(m, data->dasd_io_time2);
987 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
988 dasd_stats_array(m, data->dasd_io_time2ps);
989 seq_printf(m, "histogram_time_irq_to_end ");
990 dasd_stats_array(m, data->dasd_io_time3);
991 seq_printf(m, "histogram_ccw_queue_length ");
992 dasd_stats_array(m, data->dasd_io_nr_req);
993 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
994 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
995 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
996 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
997 seq_printf(m, "histogram_read_sectors ");
998 dasd_stats_array(m, data->dasd_read_secs);
999 seq_printf(m, "histogram_read_times ");
1000 dasd_stats_array(m, data->dasd_read_times);
1001 seq_printf(m, "histogram_read_time_build_to_ssch ");
1002 dasd_stats_array(m, data->dasd_read_time1);
1003 seq_printf(m, "histogram_read_time_ssch_to_irq ");
1004 dasd_stats_array(m, data->dasd_read_time2);
1005 seq_printf(m, "histogram_read_time_irq_to_end ");
1006 dasd_stats_array(m, data->dasd_read_time3);
1007 seq_printf(m, "histogram_read_ccw_queue_length ");
1008 dasd_stats_array(m, data->dasd_read_nr_req);
1011 static int dasd_stats_show(struct seq_file *m, void *v)
1013 struct dasd_profile *profile;
1014 struct dasd_profile_info *data;
1016 profile = m->private;
1017 spin_lock_bh(&profile->lock);
1018 data = profile->data;
1020 spin_unlock_bh(&profile->lock);
1021 seq_printf(m, "disabled\n");
1024 dasd_stats_seq_print(m, data);
1025 spin_unlock_bh(&profile->lock);
1029 static int dasd_stats_open(struct inode *inode, struct file *file)
1031 struct dasd_profile *profile = inode->i_private;
1032 return single_open(file, dasd_stats_show, profile);
1035 static const struct file_operations dasd_stats_raw_fops = {
1036 .owner = THIS_MODULE,
1037 .open = dasd_stats_open,
1039 .llseek = seq_lseek,
1040 .release = single_release,
1041 .write = dasd_stats_write,
1044 static ssize_t dasd_stats_global_write(struct file *file,
1045 const char __user *user_buf,
1046 size_t user_len, loff_t *pos)
1051 if (user_len > 65536)
1053 buffer = dasd_get_user_string(user_buf, user_len);
1055 return PTR_ERR(buffer);
1056 str = skip_spaces(buffer);
1058 if (strncmp(str, "reset", 5) == 0) {
1059 dasd_global_profile_reset();
1060 } else if (strncmp(str, "on", 2) == 0) {
1061 dasd_global_profile_reset();
1062 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1063 } else if (strncmp(str, "off", 3) == 0) {
1064 dasd_global_profile_level = DASD_PROFILE_OFF;
1071 static int dasd_stats_global_show(struct seq_file *m, void *v)
1073 if (!dasd_global_profile_level) {
1074 seq_printf(m, "disabled\n");
1077 dasd_stats_seq_print(m, &dasd_global_profile_data);
1081 static int dasd_stats_global_open(struct inode *inode, struct file *file)
1083 return single_open(file, dasd_stats_global_show, NULL);
1086 static const struct file_operations dasd_stats_global_fops = {
1087 .owner = THIS_MODULE,
1088 .open = dasd_stats_global_open,
1090 .llseek = seq_lseek,
1091 .release = single_release,
1092 .write = dasd_stats_global_write,
1095 static void dasd_profile_init(struct dasd_profile *profile,
1096 struct dentry *base_dentry)
1103 profile->dentry = NULL;
1104 profile->data = NULL;
1105 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1106 pde = debugfs_create_file("statistics", mode, base_dentry,
1107 profile, &dasd_stats_raw_fops);
1108 if (pde && !IS_ERR(pde))
1109 profile->dentry = pde;
1113 static void dasd_profile_exit(struct dasd_profile *profile)
1115 dasd_profile_off(profile);
1116 if (profile->dentry) {
1117 debugfs_remove(profile->dentry);
1118 profile->dentry = NULL;
1122 static void dasd_statistics_removeroot(void)
1124 dasd_global_profile_level = DASD_PROFILE_OFF;
1125 if (dasd_global_profile_dentry) {
1126 debugfs_remove(dasd_global_profile_dentry);
1127 dasd_global_profile_dentry = NULL;
1129 if (dasd_debugfs_global_entry)
1130 debugfs_remove(dasd_debugfs_global_entry);
1131 if (dasd_debugfs_root_entry)
1132 debugfs_remove(dasd_debugfs_root_entry);
1135 static void dasd_statistics_createroot(void)
1140 dasd_debugfs_root_entry = NULL;
1141 dasd_debugfs_global_entry = NULL;
1142 dasd_global_profile_dentry = NULL;
1143 pde = debugfs_create_dir("dasd", NULL);
1144 if (!pde || IS_ERR(pde))
1146 dasd_debugfs_root_entry = pde;
1147 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1148 if (!pde || IS_ERR(pde))
1150 dasd_debugfs_global_entry = pde;
1152 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1153 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1154 NULL, &dasd_stats_global_fops);
1155 if (!pde || IS_ERR(pde))
1157 dasd_global_profile_dentry = pde;
1161 DBF_EVENT(DBF_ERR, "%s",
1162 "Creation of the dasd debugfs interface failed");
1163 dasd_statistics_removeroot();
1168 #define dasd_profile_start(block, cqr, req) do {} while (0)
1169 #define dasd_profile_end(block, cqr, req) do {} while (0)
1171 static void dasd_statistics_createroot(void)
1176 static void dasd_statistics_removeroot(void)
1181 int dasd_stats_generic_show(struct seq_file *m, void *v)
1183 seq_printf(m, "Statistics are not activated in this kernel\n");
1187 static void dasd_profile_init(struct dasd_profile *profile,
1188 struct dentry *base_dentry)
1193 static void dasd_profile_exit(struct dasd_profile *profile)
1198 int dasd_profile_on(struct dasd_profile *profile)
1203 #endif /* CONFIG_DASD_PROFILE */
1206 * Allocate memory for a channel program with 'cplength' channel
1207 * command words and 'datasize' additional space. There are two
1208 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1209 * memory and 2) dasd_smalloc_request uses the static ccw memory
1210 * that gets allocated for each device.
1212 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
1214 struct dasd_device *device)
1216 struct dasd_ccw_req *cqr;
1219 BUG_ON(datasize > PAGE_SIZE ||
1220 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1222 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1224 return ERR_PTR(-ENOMEM);
1227 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1228 GFP_ATOMIC | GFP_DMA);
1229 if (cqr->cpaddr == NULL) {
1231 return ERR_PTR(-ENOMEM);
1236 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1237 if (cqr->data == NULL) {
1240 return ERR_PTR(-ENOMEM);
1244 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1245 dasd_get_device(device);
1249 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
1251 struct dasd_device *device)
1253 unsigned long flags;
1254 struct dasd_ccw_req *cqr;
1258 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1260 size += cplength * sizeof(struct ccw1);
1263 spin_lock_irqsave(&device->mem_lock, flags);
1264 cqr = (struct dasd_ccw_req *)
1265 dasd_alloc_chunk(&device->ccw_chunks, size);
1266 spin_unlock_irqrestore(&device->mem_lock, flags);
1268 return ERR_PTR(-ENOMEM);
1269 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1270 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1273 cqr->cpaddr = (struct ccw1 *) data;
1274 data += cplength*sizeof(struct ccw1);
1275 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1280 memset(cqr->data, 0, datasize);
1283 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1284 dasd_get_device(device);
1289 * Free memory of a channel program. This function needs to free all the
1290 * idal lists that might have been created by dasd_set_cda and the
1291 * struct dasd_ccw_req itself.
1293 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1298 /* Clear any idals used for the request. */
1301 clear_normalized_cda(ccw);
1302 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1307 dasd_put_device(device);
1310 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1312 unsigned long flags;
1314 spin_lock_irqsave(&device->mem_lock, flags);
1315 dasd_free_chunk(&device->ccw_chunks, cqr);
1316 spin_unlock_irqrestore(&device->mem_lock, flags);
1317 dasd_put_device(device);
1321 * Check discipline magic in cqr.
1323 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1325 struct dasd_device *device;
1329 device = cqr->startdev;
1330 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1331 DBF_DEV_EVENT(DBF_WARNING, device,
1332 " dasd_ccw_req 0x%08x magic doesn't match"
1333 " discipline 0x%08x",
1335 *(unsigned int *) device->discipline->name);
1342 * Terminate the current i/o and set the request to clear_pending.
1343 * Timer keeps device runnig.
1344 * ccw_device_clear can fail if the i/o subsystem
1347 int dasd_term_IO(struct dasd_ccw_req *cqr)
1349 struct dasd_device *device;
1351 char errorstring[ERRORLENGTH];
1354 rc = dasd_check_cqr(cqr);
1358 device = (struct dasd_device *) cqr->startdev;
1359 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1360 rc = ccw_device_clear(device->cdev, (long) cqr);
1362 case 0: /* termination successful */
1363 cqr->status = DASD_CQR_CLEAR_PENDING;
1364 cqr->stopclk = get_tod_clock();
1366 DBF_DEV_EVENT(DBF_DEBUG, device,
1367 "terminate cqr %p successful",
1371 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1372 "device gone, retry");
1375 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1376 "I/O error, retry");
1380 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1381 "device busy, retry later");
1384 /* internal error 10 - unknown rc*/
1385 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1386 dev_err(&device->cdev->dev, "An error occurred in the "
1387 "DASD device driver, reason=%s\n", errorstring);
1393 dasd_schedule_device_bh(device);
1398 * Start the i/o. This start_IO can fail if the channel is really busy.
1399 * In that case set up a timer to start the request later.
1401 int dasd_start_IO(struct dasd_ccw_req *cqr)
1403 struct dasd_device *device;
1405 char errorstring[ERRORLENGTH];
1408 rc = dasd_check_cqr(cqr);
1413 device = (struct dasd_device *) cqr->startdev;
1415 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1416 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1417 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1418 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1419 "because of stolen lock", cqr);
1420 cqr->status = DASD_CQR_ERROR;
1421 cqr->intrc = -EPERM;
1424 if (cqr->retries < 0) {
1425 /* internal error 14 - start_IO run out of retries */
1426 sprintf(errorstring, "14 %p", cqr);
1427 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1428 "device driver, reason=%s\n", errorstring);
1429 cqr->status = DASD_CQR_ERROR;
1432 cqr->startclk = get_tod_clock();
1433 cqr->starttime = jiffies;
1435 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1436 cqr->lpm &= device->path_data.opm;
1438 cqr->lpm = device->path_data.opm;
1440 if (cqr->cpmode == 1) {
1441 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1442 (long) cqr, cqr->lpm);
1444 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1445 (long) cqr, cqr->lpm, 0);
1449 cqr->status = DASD_CQR_IN_IO;
1452 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1453 "start_IO: device busy, retry later");
1456 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1457 "start_IO: request timeout, retry later");
1460 /* -EACCES indicates that the request used only a subset of the
1461 * available paths and all these paths are gone. If the lpm of
1462 * this request was only a subset of the opm (e.g. the ppm) then
1463 * we just do a retry with all available paths.
1464 * If we already use the full opm, something is amiss, and we
1465 * need a full path verification.
1467 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1468 DBF_DEV_EVENT(DBF_WARNING, device,
1469 "start_IO: selected paths gone (%x)",
1471 } else if (cqr->lpm != device->path_data.opm) {
1472 cqr->lpm = device->path_data.opm;
1473 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1474 "start_IO: selected paths gone,"
1475 " retry on all paths");
1477 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1478 "start_IO: all paths in opm gone,"
1479 " do path verification");
1480 dasd_generic_last_path_gone(device);
1481 device->path_data.opm = 0;
1482 device->path_data.ppm = 0;
1483 device->path_data.npm = 0;
1484 device->path_data.tbvpm =
1485 ccw_device_get_path_mask(device->cdev);
1489 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1490 "start_IO: -ENODEV device gone, retry");
1493 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1494 "start_IO: -EIO device gone, retry");
1497 /* most likely caused in power management context */
1498 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1499 "start_IO: -EINVAL device currently "
1503 /* internal error 11 - unknown rc */
1504 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1505 dev_err(&device->cdev->dev,
1506 "An error occurred in the DASD device driver, "
1507 "reason=%s\n", errorstring);
1516 * Timeout function for dasd devices. This is used for different purposes
1517 * 1) missing interrupt handler for normal operation
1518 * 2) delayed start of request where start_IO failed with -EBUSY
1519 * 3) timeout for missing state change interrupts
1520 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1521 * DASD_CQR_QUEUED for 2) and 3).
1523 static void dasd_device_timeout(unsigned long ptr)
1525 unsigned long flags;
1526 struct dasd_device *device;
1528 device = (struct dasd_device *) ptr;
1529 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1530 /* re-activate request queue */
1531 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1532 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1533 dasd_schedule_device_bh(device);
1537 * Setup timeout for a device in jiffies.
1539 void dasd_device_set_timer(struct dasd_device *device, int expires)
1542 del_timer(&device->timer);
1544 mod_timer(&device->timer, jiffies + expires);
1548 * Clear timeout for a device.
1550 void dasd_device_clear_timer(struct dasd_device *device)
1552 del_timer(&device->timer);
1555 static void dasd_handle_killed_request(struct ccw_device *cdev,
1556 unsigned long intparm)
1558 struct dasd_ccw_req *cqr;
1559 struct dasd_device *device;
1563 cqr = (struct dasd_ccw_req *) intparm;
1564 if (cqr->status != DASD_CQR_IN_IO) {
1565 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1566 "invalid status in handle_killed_request: "
1567 "%02x", cqr->status);
1571 device = dasd_device_from_cdev_locked(cdev);
1572 if (IS_ERR(device)) {
1573 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1574 "unable to get device from cdev");
1578 if (!cqr->startdev ||
1579 device != cqr->startdev ||
1580 strncmp(cqr->startdev->discipline->ebcname,
1581 (char *) &cqr->magic, 4)) {
1582 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1583 "invalid device in request");
1584 dasd_put_device(device);
1588 /* Schedule request to be retried. */
1589 cqr->status = DASD_CQR_QUEUED;
1591 dasd_device_clear_timer(device);
1592 dasd_schedule_device_bh(device);
1593 dasd_put_device(device);
1596 void dasd_generic_handle_state_change(struct dasd_device *device)
1598 /* First of all start sense subsystem status request. */
1599 dasd_eer_snss(device);
1601 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1602 dasd_schedule_device_bh(device);
1604 dasd_schedule_block_bh(device->block);
1608 * Interrupt handler for "normal" ssch-io based dasd devices.
1610 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1613 struct dasd_ccw_req *cqr, *next;
1614 struct dasd_device *device;
1615 unsigned long long now;
1619 switch (PTR_ERR(irb)) {
1623 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1624 "request timed out\n", __func__);
1627 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1628 "unknown error %ld\n", __func__,
1631 dasd_handle_killed_request(cdev, intparm);
1635 now = get_tod_clock();
1636 cqr = (struct dasd_ccw_req *) intparm;
1637 /* check for conditions that should be handled immediately */
1639 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1640 scsw_cstat(&irb->scsw) == 0)) {
1642 memcpy(&cqr->irb, irb, sizeof(*irb));
1643 device = dasd_device_from_cdev_locked(cdev);
1646 /* ignore unsolicited interrupts for DIAG discipline */
1647 if (device->discipline == dasd_diag_discipline_pointer) {
1648 dasd_put_device(device);
1651 device->discipline->dump_sense_dbf(device, irb, "int");
1652 if (device->features & DASD_FEATURE_ERPLOG)
1653 device->discipline->dump_sense(device, cqr, irb);
1654 device->discipline->check_for_device_change(device, cqr, irb);
1655 dasd_put_device(device);
1660 device = (struct dasd_device *) cqr->startdev;
1662 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1663 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1664 "invalid device in request");
1668 /* Check for clear pending */
1669 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1670 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1671 cqr->status = DASD_CQR_CLEARED;
1672 dasd_device_clear_timer(device);
1673 wake_up(&dasd_flush_wq);
1674 dasd_schedule_device_bh(device);
1678 /* check status - the request might have been killed by dyn detach */
1679 if (cqr->status != DASD_CQR_IN_IO) {
1680 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1681 "status %02x", dev_name(&cdev->dev), cqr->status);
1687 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1688 scsw_cstat(&irb->scsw) == 0) {
1689 /* request was completed successfully */
1690 cqr->status = DASD_CQR_SUCCESS;
1692 /* Start first request on queue if possible -> fast_io. */
1693 if (cqr->devlist.next != &device->ccw_queue) {
1694 next = list_entry(cqr->devlist.next,
1695 struct dasd_ccw_req, devlist);
1697 } else { /* error */
1699 * If we don't want complex ERP for this request, then just
1700 * reset this and retry it in the fastpath
1702 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1704 if (cqr->lpm == device->path_data.opm)
1705 DBF_DEV_EVENT(DBF_DEBUG, device,
1706 "default ERP in fastpath "
1707 "(%i retries left)",
1709 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1710 cqr->lpm = device->path_data.opm;
1711 cqr->status = DASD_CQR_QUEUED;
1714 cqr->status = DASD_CQR_ERROR;
1716 if (next && (next->status == DASD_CQR_QUEUED) &&
1717 (!device->stopped)) {
1718 if (device->discipline->start_IO(next) == 0)
1719 expires = next->expires;
1722 dasd_device_set_timer(device, expires);
1724 dasd_device_clear_timer(device);
1725 dasd_schedule_device_bh(device);
1728 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1730 struct dasd_device *device;
1732 device = dasd_device_from_cdev_locked(cdev);
1736 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1737 device->state != device->target ||
1738 !device->discipline->check_for_device_change){
1739 dasd_put_device(device);
1742 if (device->discipline->dump_sense_dbf)
1743 device->discipline->dump_sense_dbf(device, irb, "uc");
1744 device->discipline->check_for_device_change(device, NULL, irb);
1745 dasd_put_device(device);
1747 return UC_TODO_RETRY;
1749 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1752 * If we have an error on a dasd_block layer request then we cancel
1753 * and return all further requests from the same dasd_block as well.
1755 static void __dasd_device_recovery(struct dasd_device *device,
1756 struct dasd_ccw_req *ref_cqr)
1758 struct list_head *l, *n;
1759 struct dasd_ccw_req *cqr;
1762 * only requeue request that came from the dasd_block layer
1764 if (!ref_cqr->block)
1767 list_for_each_safe(l, n, &device->ccw_queue) {
1768 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1769 if (cqr->status == DASD_CQR_QUEUED &&
1770 ref_cqr->block == cqr->block) {
1771 cqr->status = DASD_CQR_CLEARED;
1777 * Remove those ccw requests from the queue that need to be returned
1778 * to the upper layer.
1780 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1781 struct list_head *final_queue)
1783 struct list_head *l, *n;
1784 struct dasd_ccw_req *cqr;
1786 /* Process request with final status. */
1787 list_for_each_safe(l, n, &device->ccw_queue) {
1788 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1790 /* Stop list processing at the first non-final request. */
1791 if (cqr->status == DASD_CQR_QUEUED ||
1792 cqr->status == DASD_CQR_IN_IO ||
1793 cqr->status == DASD_CQR_CLEAR_PENDING)
1795 if (cqr->status == DASD_CQR_ERROR) {
1796 __dasd_device_recovery(device, cqr);
1798 /* Rechain finished requests to final queue */
1799 list_move_tail(&cqr->devlist, final_queue);
1804 * the cqrs from the final queue are returned to the upper layer
1805 * by setting a dasd_block state and calling the callback function
1807 static void __dasd_device_process_final_queue(struct dasd_device *device,
1808 struct list_head *final_queue)
1810 struct list_head *l, *n;
1811 struct dasd_ccw_req *cqr;
1812 struct dasd_block *block;
1813 void (*callback)(struct dasd_ccw_req *, void *data);
1814 void *callback_data;
1815 char errorstring[ERRORLENGTH];
1817 list_for_each_safe(l, n, final_queue) {
1818 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1819 list_del_init(&cqr->devlist);
1821 callback = cqr->callback;
1822 callback_data = cqr->callback_data;
1824 spin_lock_bh(&block->queue_lock);
1825 switch (cqr->status) {
1826 case DASD_CQR_SUCCESS:
1827 cqr->status = DASD_CQR_DONE;
1829 case DASD_CQR_ERROR:
1830 cqr->status = DASD_CQR_NEED_ERP;
1832 case DASD_CQR_CLEARED:
1833 cqr->status = DASD_CQR_TERMINATED;
1836 /* internal error 12 - wrong cqr status*/
1837 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1838 dev_err(&device->cdev->dev,
1839 "An error occurred in the DASD device driver, "
1840 "reason=%s\n", errorstring);
1843 if (cqr->callback != NULL)
1844 (callback)(cqr, callback_data);
1846 spin_unlock_bh(&block->queue_lock);
1851 * Take a look at the first request on the ccw queue and check
1852 * if it reached its expire time. If so, terminate the IO.
1854 static void __dasd_device_check_expire(struct dasd_device *device)
1856 struct dasd_ccw_req *cqr;
1858 if (list_empty(&device->ccw_queue))
1860 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1861 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1862 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1863 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1865 * IO in safe offline processing should not
1866 * run out of retries
1870 if (device->discipline->term_IO(cqr) != 0) {
1871 /* Hmpf, try again in 5 sec */
1872 dev_err(&device->cdev->dev,
1873 "cqr %p timed out (%lus) but cannot be "
1874 "ended, retrying in 5 s\n",
1875 cqr, (cqr->expires/HZ));
1876 cqr->expires += 5*HZ;
1877 dasd_device_set_timer(device, 5*HZ);
1879 dev_err(&device->cdev->dev,
1880 "cqr %p timed out (%lus), %i retries "
1881 "remaining\n", cqr, (cqr->expires/HZ),
1888 * Take a look at the first request on the ccw queue and check
1889 * if it needs to be started.
1891 static void __dasd_device_start_head(struct dasd_device *device)
1893 struct dasd_ccw_req *cqr;
1896 if (list_empty(&device->ccw_queue))
1898 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1899 if (cqr->status != DASD_CQR_QUEUED)
1901 /* when device is stopped, return request to previous layer
1902 * exception: only the disconnect or unresumed bits are set and the
1903 * cqr is a path verification request
1905 if (device->stopped &&
1906 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1907 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1908 cqr->intrc = -EAGAIN;
1909 cqr->status = DASD_CQR_CLEARED;
1910 dasd_schedule_device_bh(device);
1914 rc = device->discipline->start_IO(cqr);
1916 dasd_device_set_timer(device, cqr->expires);
1917 else if (rc == -EACCES) {
1918 dasd_schedule_device_bh(device);
1920 /* Hmpf, try again in 1/2 sec */
1921 dasd_device_set_timer(device, 50);
1924 static void __dasd_device_check_path_events(struct dasd_device *device)
1928 if (device->path_data.tbvpm) {
1929 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1932 rc = device->discipline->verify_path(
1933 device, device->path_data.tbvpm);
1935 dasd_device_set_timer(device, 50);
1937 device->path_data.tbvpm = 0;
1942 * Go through all request on the dasd_device request queue,
1943 * terminate them on the cdev if necessary, and return them to the
1944 * submitting layer via callback.
1946 * Make sure that all 'submitting layers' still exist when
1947 * this function is called!. In other words, when 'device' is a base
1948 * device then all block layer requests must have been removed before
1949 * via dasd_flush_block_queue.
1951 int dasd_flush_device_queue(struct dasd_device *device)
1953 struct dasd_ccw_req *cqr, *n;
1955 struct list_head flush_queue;
1957 INIT_LIST_HEAD(&flush_queue);
1958 spin_lock_irq(get_ccwdev_lock(device->cdev));
1960 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1961 /* Check status and move request to flush_queue */
1962 switch (cqr->status) {
1963 case DASD_CQR_IN_IO:
1964 rc = device->discipline->term_IO(cqr);
1966 /* unable to terminate requeust */
1967 dev_err(&device->cdev->dev,
1968 "Flushing the DASD request queue "
1969 "failed for request %p\n", cqr);
1970 /* stop flush processing */
1974 case DASD_CQR_QUEUED:
1975 cqr->stopclk = get_tod_clock();
1976 cqr->status = DASD_CQR_CLEARED;
1978 default: /* no need to modify the others */
1981 list_move_tail(&cqr->devlist, &flush_queue);
1984 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1986 * After this point all requests must be in state CLEAR_PENDING,
1987 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1988 * one of the others.
1990 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1991 wait_event(dasd_flush_wq,
1992 (cqr->status != DASD_CQR_CLEAR_PENDING));
1994 * Now set each request back to TERMINATED, DONE or NEED_ERP
1995 * and call the callback function of flushed requests
1997 __dasd_device_process_final_queue(device, &flush_queue);
2002 * Acquire the device lock and process queues for the device.
2004 static void dasd_device_tasklet(struct dasd_device *device)
2006 struct list_head final_queue;
2008 atomic_set (&device->tasklet_scheduled, 0);
2009 INIT_LIST_HEAD(&final_queue);
2010 spin_lock_irq(get_ccwdev_lock(device->cdev));
2011 /* Check expire time of first request on the ccw queue. */
2012 __dasd_device_check_expire(device);
2013 /* find final requests on ccw queue */
2014 __dasd_device_process_ccw_queue(device, &final_queue);
2015 __dasd_device_check_path_events(device);
2016 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2017 /* Now call the callback function of requests with final status */
2018 __dasd_device_process_final_queue(device, &final_queue);
2019 spin_lock_irq(get_ccwdev_lock(device->cdev));
2020 /* Now check if the head of the ccw queue needs to be started. */
2021 __dasd_device_start_head(device);
2022 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2023 if (waitqueue_active(&shutdown_waitq))
2024 wake_up(&shutdown_waitq);
2025 dasd_put_device(device);
2029 * Schedules a call to dasd_tasklet over the device tasklet.
2031 void dasd_schedule_device_bh(struct dasd_device *device)
2033 /* Protect against rescheduling. */
2034 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2036 dasd_get_device(device);
2037 tasklet_hi_schedule(&device->tasklet);
2040 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2042 device->stopped |= bits;
2044 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2046 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2048 device->stopped &= ~bits;
2049 if (!device->stopped)
2050 wake_up(&generic_waitq);
2052 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2055 * Queue a request to the head of the device ccw_queue.
2056 * Start the I/O if possible.
2058 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2060 struct dasd_device *device;
2061 unsigned long flags;
2063 device = cqr->startdev;
2064 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2065 cqr->status = DASD_CQR_QUEUED;
2066 list_add(&cqr->devlist, &device->ccw_queue);
2067 /* let the bh start the request to keep them in order */
2068 dasd_schedule_device_bh(device);
2069 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2073 * Queue a request to the tail of the device ccw_queue.
2074 * Start the I/O if possible.
2076 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2078 struct dasd_device *device;
2079 unsigned long flags;
2081 device = cqr->startdev;
2082 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2083 cqr->status = DASD_CQR_QUEUED;
2084 list_add_tail(&cqr->devlist, &device->ccw_queue);
2085 /* let the bh start the request to keep them in order */
2086 dasd_schedule_device_bh(device);
2087 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2091 * Wakeup helper for the 'sleep_on' functions.
2093 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2095 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2096 cqr->callback_data = DASD_SLEEPON_END_TAG;
2097 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2098 wake_up(&generic_waitq);
2100 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2102 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2104 struct dasd_device *device;
2107 device = cqr->startdev;
2108 spin_lock_irq(get_ccwdev_lock(device->cdev));
2109 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2110 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2115 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2117 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2119 struct dasd_device *device;
2120 dasd_erp_fn_t erp_fn;
2122 if (cqr->status == DASD_CQR_FILLED)
2124 device = cqr->startdev;
2125 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2126 if (cqr->status == DASD_CQR_TERMINATED) {
2127 device->discipline->handle_terminated_request(cqr);
2130 if (cqr->status == DASD_CQR_NEED_ERP) {
2131 erp_fn = device->discipline->erp_action(cqr);
2135 if (cqr->status == DASD_CQR_FAILED)
2136 dasd_log_sense(cqr, &cqr->irb);
2138 __dasd_process_erp(device, cqr);
2145 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2147 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2148 if (cqr->refers) /* erp is not done yet */
2150 return ((cqr->status != DASD_CQR_DONE) &&
2151 (cqr->status != DASD_CQR_FAILED));
2153 return (cqr->status == DASD_CQR_FILLED);
2156 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2158 struct dasd_device *device;
2160 struct list_head ccw_queue;
2161 struct dasd_ccw_req *cqr;
2163 INIT_LIST_HEAD(&ccw_queue);
2164 maincqr->status = DASD_CQR_FILLED;
2165 device = maincqr->startdev;
2166 list_add(&maincqr->blocklist, &ccw_queue);
2167 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2168 cqr = list_first_entry(&ccw_queue,
2169 struct dasd_ccw_req, blocklist)) {
2171 if (__dasd_sleep_on_erp(cqr))
2173 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2175 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2176 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2177 cqr->status = DASD_CQR_FAILED;
2178 cqr->intrc = -EPERM;
2181 /* Non-temporary stop condition will trigger fail fast */
2182 if (device->stopped & ~DASD_STOPPED_PENDING &&
2183 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2184 (!dasd_eer_enabled(device))) {
2185 cqr->status = DASD_CQR_FAILED;
2186 cqr->intrc = -EAGAIN;
2189 /* Don't try to start requests if device is stopped */
2190 if (interruptible) {
2191 rc = wait_event_interruptible(
2192 generic_waitq, !(device->stopped));
2193 if (rc == -ERESTARTSYS) {
2194 cqr->status = DASD_CQR_FAILED;
2195 maincqr->intrc = rc;
2199 wait_event(generic_waitq, !(device->stopped));
2202 cqr->callback = dasd_wakeup_cb;
2204 cqr->callback_data = DASD_SLEEPON_START_TAG;
2205 dasd_add_request_tail(cqr);
2206 if (interruptible) {
2207 rc = wait_event_interruptible(
2208 generic_waitq, _wait_for_wakeup(cqr));
2209 if (rc == -ERESTARTSYS) {
2210 dasd_cancel_req(cqr);
2211 /* wait (non-interruptible) for final status */
2212 wait_event(generic_waitq,
2213 _wait_for_wakeup(cqr));
2214 cqr->status = DASD_CQR_FAILED;
2215 maincqr->intrc = rc;
2219 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2222 maincqr->endclk = get_tod_clock();
2223 if ((maincqr->status != DASD_CQR_DONE) &&
2224 (maincqr->intrc != -ERESTARTSYS))
2225 dasd_log_sense(maincqr, &maincqr->irb);
2226 if (maincqr->status == DASD_CQR_DONE)
2228 else if (maincqr->intrc)
2229 rc = maincqr->intrc;
2235 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2237 struct dasd_ccw_req *cqr;
2239 list_for_each_entry(cqr, ccw_queue, blocklist) {
2240 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2247 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2249 struct dasd_device *device;
2251 struct dasd_ccw_req *cqr, *n;
2254 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2255 device = cqr->startdev;
2256 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2259 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2260 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2261 cqr->status = DASD_CQR_FAILED;
2262 cqr->intrc = -EPERM;
2265 /*Non-temporary stop condition will trigger fail fast*/
2266 if (device->stopped & ~DASD_STOPPED_PENDING &&
2267 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2268 !dasd_eer_enabled(device)) {
2269 cqr->status = DASD_CQR_FAILED;
2270 cqr->intrc = -EAGAIN;
2274 /*Don't try to start requests if device is stopped*/
2275 if (interruptible) {
2276 rc = wait_event_interruptible(
2277 generic_waitq, !device->stopped);
2278 if (rc == -ERESTARTSYS) {
2279 cqr->status = DASD_CQR_FAILED;
2284 wait_event(generic_waitq, !(device->stopped));
2287 cqr->callback = dasd_wakeup_cb;
2288 cqr->callback_data = DASD_SLEEPON_START_TAG;
2289 dasd_add_request_tail(cqr);
2292 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2295 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2296 if (__dasd_sleep_on_erp(cqr))
2307 * Queue a request to the tail of the device ccw_queue and wait for
2310 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2312 return _dasd_sleep_on(cqr, 0);
2316 * Start requests from a ccw_queue and wait for their completion.
2318 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2320 return _dasd_sleep_on_queue(ccw_queue, 0);
2322 EXPORT_SYMBOL(dasd_sleep_on_queue);
2325 * Queue a request to the tail of the device ccw_queue and wait
2326 * interruptible for it's completion.
2328 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2330 return _dasd_sleep_on(cqr, 1);
2334 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2335 * for eckd devices) the currently running request has to be terminated
2336 * and be put back to status queued, before the special request is added
2337 * to the head of the queue. Then the special request is waited on normally.
2339 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2341 struct dasd_ccw_req *cqr;
2344 if (list_empty(&device->ccw_queue))
2346 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2347 rc = device->discipline->term_IO(cqr);
2350 * CQR terminated because a more important request is pending.
2351 * Undo decreasing of retry counter because this is
2352 * not an error case.
2358 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2360 struct dasd_device *device;
2363 device = cqr->startdev;
2364 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2365 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2366 cqr->status = DASD_CQR_FAILED;
2367 cqr->intrc = -EPERM;
2370 spin_lock_irq(get_ccwdev_lock(device->cdev));
2371 rc = _dasd_term_running_cqr(device);
2373 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2376 cqr->callback = dasd_wakeup_cb;
2377 cqr->callback_data = DASD_SLEEPON_START_TAG;
2378 cqr->status = DASD_CQR_QUEUED;
2380 * add new request as second
2381 * first the terminated cqr needs to be finished
2383 list_add(&cqr->devlist, device->ccw_queue.next);
2385 /* let the bh start the request to keep them in order */
2386 dasd_schedule_device_bh(device);
2388 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2390 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2392 if (cqr->status == DASD_CQR_DONE)
2394 else if (cqr->intrc)
2402 * Cancels a request that was started with dasd_sleep_on_req.
2403 * This is useful to timeout requests. The request will be
2404 * terminated if it is currently in i/o.
2405 * Returns 1 if the request has been terminated.
2406 * 0 if there was no need to terminate the request (not started yet)
2407 * negative error code if termination failed
2408 * Cancellation of a request is an asynchronous operation! The calling
2409 * function has to wait until the request is properly returned via callback.
2411 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2413 struct dasd_device *device = cqr->startdev;
2414 unsigned long flags;
2418 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2419 switch (cqr->status) {
2420 case DASD_CQR_QUEUED:
2421 /* request was not started - just set to cleared */
2422 cqr->status = DASD_CQR_CLEARED;
2424 case DASD_CQR_IN_IO:
2425 /* request in IO - terminate IO and release again */
2426 rc = device->discipline->term_IO(cqr);
2428 dev_err(&device->cdev->dev,
2429 "Cancelling request %p failed with rc=%d\n",
2432 cqr->stopclk = get_tod_clock();
2435 default: /* already finished or clear pending - do nothing */
2438 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2439 dasd_schedule_device_bh(device);
2445 * SECTION: Operations of the dasd_block layer.
2449 * Timeout function for dasd_block. This is used when the block layer
2450 * is waiting for something that may not come reliably, (e.g. a state
2453 static void dasd_block_timeout(unsigned long ptr)
2455 unsigned long flags;
2456 struct dasd_block *block;
2458 block = (struct dasd_block *) ptr;
2459 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2460 /* re-activate request queue */
2461 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2462 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2463 dasd_schedule_block_bh(block);
2467 * Setup timeout for a dasd_block in jiffies.
2469 void dasd_block_set_timer(struct dasd_block *block, int expires)
2472 del_timer(&block->timer);
2474 mod_timer(&block->timer, jiffies + expires);
2478 * Clear timeout for a dasd_block.
2480 void dasd_block_clear_timer(struct dasd_block *block)
2482 del_timer(&block->timer);
2486 * Process finished error recovery ccw.
2488 static void __dasd_process_erp(struct dasd_device *device,
2489 struct dasd_ccw_req *cqr)
2491 dasd_erp_fn_t erp_fn;
2493 if (cqr->status == DASD_CQR_DONE)
2494 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2496 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2497 erp_fn = device->discipline->erp_postaction(cqr);
2502 * Fetch requests from the block device queue.
2504 static void __dasd_process_request_queue(struct dasd_block *block)
2506 struct request_queue *queue;
2507 struct request *req;
2508 struct dasd_ccw_req *cqr;
2509 struct dasd_device *basedev;
2510 unsigned long flags;
2511 queue = block->request_queue;
2512 basedev = block->base;
2513 /* No queue ? Then there is nothing to do. */
2518 * We requeue request from the block device queue to the ccw
2519 * queue only in two states. In state DASD_STATE_READY the
2520 * partition detection is done and we need to requeue requests
2521 * for that. State DASD_STATE_ONLINE is normal block device
2524 if (basedev->state < DASD_STATE_READY) {
2525 while ((req = blk_fetch_request(block->request_queue)))
2526 __blk_end_request_all(req, -EIO);
2529 /* Now we try to fetch requests from the request queue */
2530 while ((req = blk_peek_request(queue))) {
2531 if (basedev->features & DASD_FEATURE_READONLY &&
2532 rq_data_dir(req) == WRITE) {
2533 DBF_DEV_EVENT(DBF_ERR, basedev,
2534 "Rejecting write request %p",
2536 blk_start_request(req);
2537 __blk_end_request_all(req, -EIO);
2540 cqr = basedev->discipline->build_cp(basedev, block, req);
2542 if (PTR_ERR(cqr) == -EBUSY)
2543 break; /* normal end condition */
2544 if (PTR_ERR(cqr) == -ENOMEM)
2545 break; /* terminate request queue loop */
2546 if (PTR_ERR(cqr) == -EAGAIN) {
2548 * The current request cannot be build right
2549 * now, we have to try later. If this request
2550 * is the head-of-queue we stop the device
2553 if (!list_empty(&block->ccw_queue))
2556 get_ccwdev_lock(basedev->cdev), flags);
2557 dasd_device_set_stop_bits(basedev,
2558 DASD_STOPPED_PENDING);
2559 spin_unlock_irqrestore(
2560 get_ccwdev_lock(basedev->cdev), flags);
2561 dasd_block_set_timer(block, HZ/2);
2564 DBF_DEV_EVENT(DBF_ERR, basedev,
2565 "CCW creation failed (rc=%ld) "
2568 blk_start_request(req);
2569 __blk_end_request_all(req, -EIO);
2573 * Note: callback is set to dasd_return_cqr_cb in
2574 * __dasd_block_start_head to cover erp requests as well
2576 cqr->callback_data = (void *) req;
2577 cqr->status = DASD_CQR_FILLED;
2578 blk_start_request(req);
2579 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2580 dasd_profile_start(block, cqr, req);
2584 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2586 struct request *req;
2590 req = (struct request *) cqr->callback_data;
2591 dasd_profile_end(cqr->block, cqr, req);
2592 status = cqr->block->base->discipline->free_cp(cqr, req);
2594 error = status ? status : -EIO;
2595 __blk_end_request_all(req, error);
2599 * Process ccw request queue.
2601 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2602 struct list_head *final_queue)
2604 struct list_head *l, *n;
2605 struct dasd_ccw_req *cqr;
2606 dasd_erp_fn_t erp_fn;
2607 unsigned long flags;
2608 struct dasd_device *base = block->base;
2611 /* Process request with final status. */
2612 list_for_each_safe(l, n, &block->ccw_queue) {
2613 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2614 if (cqr->status != DASD_CQR_DONE &&
2615 cqr->status != DASD_CQR_FAILED &&
2616 cqr->status != DASD_CQR_NEED_ERP &&
2617 cqr->status != DASD_CQR_TERMINATED)
2620 if (cqr->status == DASD_CQR_TERMINATED) {
2621 base->discipline->handle_terminated_request(cqr);
2625 /* Process requests that may be recovered */
2626 if (cqr->status == DASD_CQR_NEED_ERP) {
2627 erp_fn = base->discipline->erp_action(cqr);
2628 if (IS_ERR(erp_fn(cqr)))
2633 /* log sense for fatal error */
2634 if (cqr->status == DASD_CQR_FAILED) {
2635 dasd_log_sense(cqr, &cqr->irb);
2638 /* First of all call extended error reporting. */
2639 if (dasd_eer_enabled(base) &&
2640 cqr->status == DASD_CQR_FAILED) {
2641 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2643 /* restart request */
2644 cqr->status = DASD_CQR_FILLED;
2646 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2647 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2648 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2653 /* Process finished ERP request. */
2655 __dasd_process_erp(base, cqr);
2659 /* Rechain finished requests to final queue */
2660 cqr->endclk = get_tod_clock();
2661 list_move_tail(&cqr->blocklist, final_queue);
2665 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2667 dasd_schedule_block_bh(cqr->block);
2670 static void __dasd_block_start_head(struct dasd_block *block)
2672 struct dasd_ccw_req *cqr;
2674 if (list_empty(&block->ccw_queue))
2676 /* We allways begin with the first requests on the queue, as some
2677 * of previously started requests have to be enqueued on a
2678 * dasd_device again for error recovery.
2680 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2681 if (cqr->status != DASD_CQR_FILLED)
2683 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2684 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2685 cqr->status = DASD_CQR_FAILED;
2686 cqr->intrc = -EPERM;
2687 dasd_schedule_block_bh(block);
2690 /* Non-temporary stop condition will trigger fail fast */
2691 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2692 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2693 (!dasd_eer_enabled(block->base))) {
2694 cqr->status = DASD_CQR_FAILED;
2695 dasd_schedule_block_bh(block);
2698 /* Don't try to start requests if device is stopped */
2699 if (block->base->stopped)
2702 /* just a fail safe check, should not happen */
2704 cqr->startdev = block->base;
2706 /* make sure that the requests we submit find their way back */
2707 cqr->callback = dasd_return_cqr_cb;
2709 dasd_add_request_tail(cqr);
2714 * Central dasd_block layer routine. Takes requests from the generic
2715 * block layer request queue, creates ccw requests, enqueues them on
2716 * a dasd_device and processes ccw requests that have been returned.
2718 static void dasd_block_tasklet(struct dasd_block *block)
2720 struct list_head final_queue;
2721 struct list_head *l, *n;
2722 struct dasd_ccw_req *cqr;
2724 atomic_set(&block->tasklet_scheduled, 0);
2725 INIT_LIST_HEAD(&final_queue);
2726 spin_lock(&block->queue_lock);
2727 /* Finish off requests on ccw queue */
2728 __dasd_process_block_ccw_queue(block, &final_queue);
2729 spin_unlock(&block->queue_lock);
2730 /* Now call the callback function of requests with final status */
2731 spin_lock_irq(&block->request_queue_lock);
2732 list_for_each_safe(l, n, &final_queue) {
2733 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2734 list_del_init(&cqr->blocklist);
2735 __dasd_cleanup_cqr(cqr);
2737 spin_lock(&block->queue_lock);
2738 /* Get new request from the block device request queue */
2739 __dasd_process_request_queue(block);
2740 /* Now check if the head of the ccw queue needs to be started. */
2741 __dasd_block_start_head(block);
2742 spin_unlock(&block->queue_lock);
2743 spin_unlock_irq(&block->request_queue_lock);
2744 if (waitqueue_active(&shutdown_waitq))
2745 wake_up(&shutdown_waitq);
2746 dasd_put_device(block->base);
2749 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2751 wake_up(&dasd_flush_wq);
2755 * Requeue a request back to the block request queue
2756 * only works for block requests
2758 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2760 struct dasd_block *block = cqr->block;
2761 struct request *req;
2762 unsigned long flags;
2766 spin_lock_irqsave(&block->queue_lock, flags);
2767 req = (struct request *) cqr->callback_data;
2768 blk_requeue_request(block->request_queue, req);
2769 spin_unlock_irqrestore(&block->queue_lock, flags);
2775 * Go through all request on the dasd_block request queue, cancel them
2776 * on the respective dasd_device, and return them to the generic
2779 static int dasd_flush_block_queue(struct dasd_block *block)
2781 struct dasd_ccw_req *cqr, *n;
2783 struct list_head flush_queue;
2785 INIT_LIST_HEAD(&flush_queue);
2786 spin_lock_bh(&block->queue_lock);
2789 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2790 /* if this request currently owned by a dasd_device cancel it */
2791 if (cqr->status >= DASD_CQR_QUEUED)
2792 rc = dasd_cancel_req(cqr);
2795 /* Rechain request (including erp chain) so it won't be
2796 * touched by the dasd_block_tasklet anymore.
2797 * Replace the callback so we notice when the request
2798 * is returned from the dasd_device layer.
2800 cqr->callback = _dasd_wake_block_flush_cb;
2801 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2802 list_move_tail(&cqr->blocklist, &flush_queue);
2804 /* moved more than one request - need to restart */
2807 spin_unlock_bh(&block->queue_lock);
2808 /* Now call the callback function of flushed requests */
2810 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2811 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2812 /* Process finished ERP request. */
2814 spin_lock_bh(&block->queue_lock);
2815 __dasd_process_erp(block->base, cqr);
2816 spin_unlock_bh(&block->queue_lock);
2817 /* restart list_for_xx loop since dasd_process_erp
2818 * might remove multiple elements */
2821 /* call the callback function */
2822 spin_lock_irq(&block->request_queue_lock);
2823 cqr->endclk = get_tod_clock();
2824 list_del_init(&cqr->blocklist);
2825 __dasd_cleanup_cqr(cqr);
2826 spin_unlock_irq(&block->request_queue_lock);
2832 * Schedules a call to dasd_tasklet over the device tasklet.
2834 void dasd_schedule_block_bh(struct dasd_block *block)
2836 /* Protect against rescheduling. */
2837 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2839 /* life cycle of block is bound to it's base device */
2840 dasd_get_device(block->base);
2841 tasklet_hi_schedule(&block->tasklet);
2846 * SECTION: external block device operations
2847 * (request queue handling, open, release, etc.)
2851 * Dasd request queue function. Called from ll_rw_blk.c
2853 static void do_dasd_request(struct request_queue *queue)
2855 struct dasd_block *block;
2857 block = queue->queuedata;
2858 spin_lock(&block->queue_lock);
2859 /* Get new request from the block device request queue */
2860 __dasd_process_request_queue(block);
2861 /* Now check if the head of the ccw queue needs to be started. */
2862 __dasd_block_start_head(block);
2863 spin_unlock(&block->queue_lock);
2867 * Allocate and initialize request queue and default I/O scheduler.
2869 static int dasd_alloc_queue(struct dasd_block *block)
2873 block->request_queue = blk_init_queue(do_dasd_request,
2874 &block->request_queue_lock);
2875 if (block->request_queue == NULL)
2878 block->request_queue->queuedata = block;
2880 elevator_exit(block->request_queue->elevator);
2881 block->request_queue->elevator = NULL;
2882 rc = elevator_init(block->request_queue, "deadline");
2884 blk_cleanup_queue(block->request_queue);
2891 * Allocate and initialize request queue.
2893 static void dasd_setup_queue(struct dasd_block *block)
2897 if (block->base->features & DASD_FEATURE_USERAW) {
2899 * the max_blocks value for raw_track access is 256
2900 * it is higher than the native ECKD value because we
2901 * only need one ccw per track
2902 * so the max_hw_sectors are
2903 * 2048 x 512B = 1024kB = 16 tracks
2907 max = block->base->discipline->max_blocks << block->s2b_shift;
2909 blk_queue_logical_block_size(block->request_queue,
2911 blk_queue_max_hw_sectors(block->request_queue, max);
2912 blk_queue_max_segments(block->request_queue, -1L);
2913 /* with page sized segments we can translate each segement into
2916 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2917 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2921 * Deactivate and free request queue.
2923 static void dasd_free_queue(struct dasd_block *block)
2925 if (block->request_queue) {
2926 blk_cleanup_queue(block->request_queue);
2927 block->request_queue = NULL;
2932 * Flush request on the request queue.
2934 static void dasd_flush_request_queue(struct dasd_block *block)
2936 struct request *req;
2938 if (!block->request_queue)
2941 spin_lock_irq(&block->request_queue_lock);
2942 while ((req = blk_fetch_request(block->request_queue)))
2943 __blk_end_request_all(req, -EIO);
2944 spin_unlock_irq(&block->request_queue_lock);
2947 static int dasd_open(struct block_device *bdev, fmode_t mode)
2949 struct dasd_device *base;
2952 base = dasd_device_from_gendisk(bdev->bd_disk);
2956 atomic_inc(&base->block->open_count);
2957 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2962 if (!try_module_get(base->discipline->owner)) {
2967 if (dasd_probeonly) {
2968 dev_info(&base->cdev->dev,
2969 "Accessing the DASD failed because it is in "
2970 "probeonly mode\n");
2975 if (base->state <= DASD_STATE_BASIC) {
2976 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2977 " Cannot open unrecognized device");
2982 if ((mode & FMODE_WRITE) &&
2983 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2984 (base->features & DASD_FEATURE_READONLY))) {
2989 dasd_put_device(base);
2993 module_put(base->discipline->owner);
2995 atomic_dec(&base->block->open_count);
2996 dasd_put_device(base);
3000 static void dasd_release(struct gendisk *disk, fmode_t mode)
3002 struct dasd_device *base = dasd_device_from_gendisk(disk);
3004 atomic_dec(&base->block->open_count);
3005 module_put(base->discipline->owner);
3006 dasd_put_device(base);
3011 * Return disk geometry.
3013 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3015 struct dasd_device *base;
3017 base = dasd_device_from_gendisk(bdev->bd_disk);
3021 if (!base->discipline ||
3022 !base->discipline->fill_geometry) {
3023 dasd_put_device(base);
3026 base->discipline->fill_geometry(base->block, geo);
3027 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3028 dasd_put_device(base);
3032 const struct block_device_operations
3033 dasd_device_operations = {
3034 .owner = THIS_MODULE,
3036 .release = dasd_release,
3037 .ioctl = dasd_ioctl,
3038 .compat_ioctl = dasd_ioctl,
3039 .getgeo = dasd_getgeo,
3042 /*******************************************************************************
3043 * end of block device operations
3049 #ifdef CONFIG_PROC_FS
3053 if (dasd_page_cache != NULL) {
3054 kmem_cache_destroy(dasd_page_cache);
3055 dasd_page_cache = NULL;
3057 dasd_gendisk_exit();
3059 if (dasd_debug_area != NULL) {
3060 debug_unregister(dasd_debug_area);
3061 dasd_debug_area = NULL;
3063 dasd_statistics_removeroot();
3067 * SECTION: common functions for ccw_driver use
3071 * Is the device read-only?
3072 * Note that this function does not report the setting of the
3073 * readonly device attribute, but how it is configured in z/VM.
3075 int dasd_device_is_ro(struct dasd_device *device)
3077 struct ccw_dev_id dev_id;
3078 struct diag210 diag_data;
3083 ccw_device_get_id(device->cdev, &dev_id);
3084 memset(&diag_data, 0, sizeof(diag_data));
3085 diag_data.vrdcdvno = dev_id.devno;
3086 diag_data.vrdclen = sizeof(diag_data);
3087 rc = diag210(&diag_data);
3088 if (rc == 0 || rc == 2) {
3089 return diag_data.vrdcvfla & 0x80;
3091 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3096 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3098 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3100 struct ccw_device *cdev = data;
3103 ret = ccw_device_set_online(cdev);
3105 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3106 dev_name(&cdev->dev), ret);
3110 * Initial attempt at a probe function. this can be simplified once
3111 * the other detection code is gone.
3113 int dasd_generic_probe(struct ccw_device *cdev,
3114 struct dasd_discipline *discipline)
3118 ret = dasd_add_sysfs_files(cdev);
3120 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3121 "dasd_generic_probe: could not add "
3125 cdev->handler = &dasd_int_handler;
3128 * Automatically online either all dasd devices (dasd_autodetect)
3129 * or all devices specified with dasd= parameters during
3132 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3133 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3134 async_schedule(dasd_generic_auto_online, cdev);
3139 * This will one day be called from a global not_oper handler.
3140 * It is also used by driver_unregister during module unload.
3142 void dasd_generic_remove(struct ccw_device *cdev)
3144 struct dasd_device *device;
3145 struct dasd_block *block;
3147 cdev->handler = NULL;
3149 device = dasd_device_from_cdev(cdev);
3150 if (IS_ERR(device)) {
3151 dasd_remove_sysfs_files(cdev);
3154 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3155 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3156 /* Already doing offline processing */
3157 dasd_put_device(device);
3158 dasd_remove_sysfs_files(cdev);
3162 * This device is removed unconditionally. Set offline
3163 * flag to prevent dasd_open from opening it while it is
3164 * no quite down yet.
3166 dasd_set_target_state(device, DASD_STATE_NEW);
3167 /* dasd_delete_device destroys the device reference. */
3168 block = device->block;
3169 dasd_delete_device(device);
3171 * life cycle of block is bound to device, so delete it after
3172 * device was safely removed
3175 dasd_free_block(block);
3177 dasd_remove_sysfs_files(cdev);
3181 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3182 * the device is detected for the first time and is supposed to be used
3183 * or the user has started activation through sysfs.
3185 int dasd_generic_set_online(struct ccw_device *cdev,
3186 struct dasd_discipline *base_discipline)
3188 struct dasd_discipline *discipline;
3189 struct dasd_device *device;
3192 /* first online clears initial online feature flag */
3193 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3194 device = dasd_create_device(cdev);
3196 return PTR_ERR(device);
3198 discipline = base_discipline;
3199 if (device->features & DASD_FEATURE_USEDIAG) {
3200 if (!dasd_diag_discipline_pointer) {
3201 pr_warning("%s Setting the DASD online failed because "
3202 "of missing DIAG discipline\n",
3203 dev_name(&cdev->dev));
3204 dasd_delete_device(device);
3207 discipline = dasd_diag_discipline_pointer;
3209 if (!try_module_get(base_discipline->owner)) {
3210 dasd_delete_device(device);
3213 if (!try_module_get(discipline->owner)) {
3214 module_put(base_discipline->owner);
3215 dasd_delete_device(device);
3218 device->base_discipline = base_discipline;
3219 device->discipline = discipline;
3221 /* check_device will allocate block device if necessary */
3222 rc = discipline->check_device(device);
3224 pr_warning("%s Setting the DASD online with discipline %s "
3225 "failed with rc=%i\n",
3226 dev_name(&cdev->dev), discipline->name, rc);
3227 module_put(discipline->owner);
3228 module_put(base_discipline->owner);
3229 dasd_delete_device(device);
3233 dasd_set_target_state(device, DASD_STATE_ONLINE);
3234 if (device->state <= DASD_STATE_KNOWN) {
3235 pr_warning("%s Setting the DASD online failed because of a "
3236 "missing discipline\n", dev_name(&cdev->dev));
3238 dasd_set_target_state(device, DASD_STATE_NEW);
3240 dasd_free_block(device->block);
3241 dasd_delete_device(device);
3243 pr_debug("dasd_generic device %s found\n",
3244 dev_name(&cdev->dev));
3246 wait_event(dasd_init_waitq, _wait_for_device(device));
3248 dasd_put_device(device);
3252 int dasd_generic_set_offline(struct ccw_device *cdev)
3254 struct dasd_device *device;
3255 struct dasd_block *block;
3256 int max_count, open_count, rc;
3259 device = dasd_device_from_cdev(cdev);
3261 return PTR_ERR(device);
3264 * We must make sure that this device is currently not in use.
3265 * The open_count is increased for every opener, that includes
3266 * the blkdev_get in dasd_scan_partitions. We are only interested
3267 * in the other openers.
3269 if (device->block) {
3270 max_count = device->block->bdev ? 0 : -1;
3271 open_count = atomic_read(&device->block->open_count);
3272 if (open_count > max_count) {
3274 pr_warning("%s: The DASD cannot be set offline "
3275 "with open count %i\n",
3276 dev_name(&cdev->dev), open_count);
3278 pr_warning("%s: The DASD cannot be set offline "
3279 "while it is in use\n",
3280 dev_name(&cdev->dev));
3281 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3282 dasd_put_device(device);
3287 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3289 * safe offline allready running
3290 * could only be called by normal offline so safe_offline flag
3291 * needs to be removed to run normal offline and kill all I/O
3293 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3294 /* Already doing normal offline processing */
3295 dasd_put_device(device);
3298 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3301 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3302 /* Already doing offline processing */
3303 dasd_put_device(device);
3308 * if safe_offline called set safe_offline_running flag and
3309 * clear safe_offline so that a call to normal offline
3310 * can overrun safe_offline processing
3312 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3313 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3315 * If we want to set the device safe offline all IO operations
3316 * should be finished before continuing the offline process
3317 * so sync bdev first and then wait for our queues to become
3320 /* sync blockdev and partitions */
3321 rc = fsync_bdev(device->block->bdev);
3325 /* schedule device tasklet and wait for completion */
3326 dasd_schedule_device_bh(device);
3327 rc = wait_event_interruptible(shutdown_waitq,
3328 _wait_for_empty_queues(device));
3333 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3334 dasd_set_target_state(device, DASD_STATE_NEW);
3335 /* dasd_delete_device destroys the device reference. */
3336 block = device->block;
3337 dasd_delete_device(device);
3339 * life cycle of block is bound to device, so delete it after
3340 * device was safely removed
3343 dasd_free_block(block);
3347 /* interrupted by signal */
3348 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3349 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3350 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3351 dasd_put_device(device);
3355 int dasd_generic_last_path_gone(struct dasd_device *device)
3357 struct dasd_ccw_req *cqr;
3359 dev_warn(&device->cdev->dev, "No operational channel path is left "
3360 "for the device\n");
3361 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3362 /* First of all call extended error reporting. */
3363 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3365 if (device->state < DASD_STATE_BASIC)
3367 /* Device is active. We want to keep it. */
3368 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3369 if ((cqr->status == DASD_CQR_IN_IO) ||
3370 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3371 cqr->status = DASD_CQR_QUEUED;
3374 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3375 dasd_device_clear_timer(device);
3376 dasd_schedule_device_bh(device);
3379 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3381 int dasd_generic_path_operational(struct dasd_device *device)
3383 dev_info(&device->cdev->dev, "A channel path to the device has become "
3385 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3386 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3387 if (device->stopped & DASD_UNRESUMED_PM) {
3388 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3389 dasd_restore_device(device);
3392 dasd_schedule_device_bh(device);
3394 dasd_schedule_block_bh(device->block);
3397 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3399 int dasd_generic_notify(struct ccw_device *cdev, int event)
3401 struct dasd_device *device;
3404 device = dasd_device_from_cdev_locked(cdev);
3412 device->path_data.opm = 0;
3413 device->path_data.ppm = 0;
3414 device->path_data.npm = 0;
3415 ret = dasd_generic_last_path_gone(device);
3419 if (device->path_data.opm)
3420 ret = dasd_generic_path_operational(device);
3423 dasd_put_device(device);
3427 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3430 __u8 oldopm, eventlpm;
3431 struct dasd_device *device;
3433 device = dasd_device_from_cdev_locked(cdev);
3436 for (chp = 0; chp < 8; chp++) {
3437 eventlpm = 0x80 >> chp;
3438 if (path_event[chp] & PE_PATH_GONE) {
3439 oldopm = device->path_data.opm;
3440 device->path_data.opm &= ~eventlpm;
3441 device->path_data.ppm &= ~eventlpm;
3442 device->path_data.npm &= ~eventlpm;
3443 if (oldopm && !device->path_data.opm)
3444 dasd_generic_last_path_gone(device);
3446 if (path_event[chp] & PE_PATH_AVAILABLE) {
3447 device->path_data.opm &= ~eventlpm;
3448 device->path_data.ppm &= ~eventlpm;
3449 device->path_data.npm &= ~eventlpm;
3450 device->path_data.tbvpm |= eventlpm;
3451 dasd_schedule_device_bh(device);
3453 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3454 if (!(device->path_data.opm & eventlpm) &&
3455 !(device->path_data.tbvpm & eventlpm)) {
3457 * we can not establish a pathgroup on an
3458 * unavailable path, so trigger a path
3459 * verification first
3461 device->path_data.tbvpm |= eventlpm;
3462 dasd_schedule_device_bh(device);
3464 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3465 "Pathgroup re-established\n");
3466 if (device->discipline->kick_validate)
3467 device->discipline->kick_validate(device);
3470 dasd_put_device(device);
3472 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3474 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3476 if (!device->path_data.opm && lpm) {
3477 device->path_data.opm = lpm;
3478 dasd_generic_path_operational(device);
3480 device->path_data.opm |= lpm;
3483 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3486 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3488 struct dasd_device *device = dasd_device_from_cdev(cdev);
3489 struct list_head freeze_queue;
3490 struct dasd_ccw_req *cqr, *n;
3491 struct dasd_ccw_req *refers;
3495 return PTR_ERR(device);
3497 /* mark device as suspended */
3498 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3500 if (device->discipline->freeze)
3501 rc = device->discipline->freeze(device);
3503 /* disallow new I/O */
3504 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3506 /* clear active requests and requeue them to block layer if possible */
3507 INIT_LIST_HEAD(&freeze_queue);
3508 spin_lock_irq(get_ccwdev_lock(cdev));
3510 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3511 /* Check status and move request to flush_queue */
3512 if (cqr->status == DASD_CQR_IN_IO) {
3513 rc = device->discipline->term_IO(cqr);
3515 /* unable to terminate requeust */
3516 dev_err(&device->cdev->dev,
3517 "Unable to terminate request %p "
3518 "on suspend\n", cqr);
3519 spin_unlock_irq(get_ccwdev_lock(cdev));
3520 dasd_put_device(device);
3524 list_move_tail(&cqr->devlist, &freeze_queue);
3526 spin_unlock_irq(get_ccwdev_lock(cdev));
3528 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3529 wait_event(dasd_flush_wq,
3530 (cqr->status != DASD_CQR_CLEAR_PENDING));
3531 if (cqr->status == DASD_CQR_CLEARED)
3532 cqr->status = DASD_CQR_QUEUED;
3534 /* requeue requests to blocklayer will only work for
3535 block device requests */
3536 if (_dasd_requeue_request(cqr))
3539 /* remove requests from device and block queue */
3540 list_del_init(&cqr->devlist);
3541 while (cqr->refers != NULL) {
3542 refers = cqr->refers;
3543 /* remove the request from the block queue */
3544 list_del(&cqr->blocklist);
3545 /* free the finished erp request */
3546 dasd_free_erp_request(cqr, cqr->memdev);
3550 list_del_init(&cqr->blocklist);
3551 cqr->block->base->discipline->free_cp(
3552 cqr, (struct request *) cqr->callback_data);
3556 * if requests remain then they are internal request
3557 * and go back to the device queue
3559 if (!list_empty(&freeze_queue)) {
3560 /* move freeze_queue to start of the ccw_queue */
3561 spin_lock_irq(get_ccwdev_lock(cdev));
3562 list_splice_tail(&freeze_queue, &device->ccw_queue);
3563 spin_unlock_irq(get_ccwdev_lock(cdev));
3565 dasd_put_device(device);
3568 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3570 int dasd_generic_restore_device(struct ccw_device *cdev)
3572 struct dasd_device *device = dasd_device_from_cdev(cdev);
3576 return PTR_ERR(device);
3578 /* allow new IO again */
3579 dasd_device_remove_stop_bits(device,
3580 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3582 dasd_schedule_device_bh(device);
3585 * call discipline restore function
3586 * if device is stopped do nothing e.g. for disconnected devices
3588 if (device->discipline->restore && !(device->stopped))
3589 rc = device->discipline->restore(device);
3590 if (rc || device->stopped)
3592 * if the resume failed for the DASD we put it in
3593 * an UNRESUMED stop state
3595 device->stopped |= DASD_UNRESUMED_PM;
3598 dasd_schedule_block_bh(device->block);
3600 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3601 dasd_put_device(device);
3604 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3606 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3608 int rdc_buffer_size,
3611 struct dasd_ccw_req *cqr;
3613 unsigned long *idaw;
3615 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3618 /* internal error 13 - Allocating the RDC request failed*/
3619 dev_err(&device->cdev->dev,
3620 "An error occurred in the DASD device driver, "
3621 "reason=%s\n", "13");
3626 ccw->cmd_code = CCW_CMD_RDC;
3627 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3628 idaw = (unsigned long *) (cqr->data);
3629 ccw->cda = (__u32)(addr_t) idaw;
3630 ccw->flags = CCW_FLAG_IDA;
3631 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3633 ccw->cda = (__u32)(addr_t) rdc_buffer;
3637 ccw->count = rdc_buffer_size;
3638 cqr->startdev = device;
3639 cqr->memdev = device;
3640 cqr->expires = 10*HZ;
3642 cqr->buildclk = get_tod_clock();
3643 cqr->status = DASD_CQR_FILLED;
3648 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3649 void *rdc_buffer, int rdc_buffer_size)
3652 struct dasd_ccw_req *cqr;
3654 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3657 return PTR_ERR(cqr);
3659 ret = dasd_sleep_on(cqr);
3660 dasd_sfree_request(cqr, cqr->memdev);
3663 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3666 * In command mode and transport mode we need to look for sense
3667 * data in different places. The sense data itself is allways
3668 * an array of 32 bytes, so we can unify the sense data access
3671 char *dasd_get_sense(struct irb *irb)
3673 struct tsb *tsb = NULL;
3676 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3677 if (irb->scsw.tm.tcw)
3678 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3680 if (tsb && tsb->length == 64 && tsb->flags)
3681 switch (tsb->flags & 0x07) {
3682 case 1: /* tsa_iostat */
3683 sense = tsb->tsa.iostat.sense;
3685 case 2: /* tsa_ddpc */
3686 sense = tsb->tsa.ddpc.sense;
3689 /* currently we don't use interrogate data */
3692 } else if (irb->esw.esw0.erw.cons) {
3697 EXPORT_SYMBOL_GPL(dasd_get_sense);
3699 void dasd_generic_shutdown(struct ccw_device *cdev)
3701 struct dasd_device *device;
3703 device = dasd_device_from_cdev(cdev);
3708 dasd_schedule_block_bh(device->block);
3710 dasd_schedule_device_bh(device);
3712 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3714 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3716 static int __init dasd_init(void)
3720 init_waitqueue_head(&dasd_init_waitq);
3721 init_waitqueue_head(&dasd_flush_wq);
3722 init_waitqueue_head(&generic_waitq);
3723 init_waitqueue_head(&shutdown_waitq);
3725 /* register 'common' DASD debug area, used for all DBF_XXX calls */
3726 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3727 if (dasd_debug_area == NULL) {
3731 debug_register_view(dasd_debug_area, &debug_sprintf_view);
3732 debug_set_level(dasd_debug_area, DBF_WARNING);
3734 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3736 dasd_diag_discipline_pointer = NULL;
3738 dasd_statistics_createroot();
3740 rc = dasd_devmap_init();
3743 rc = dasd_gendisk_init();
3749 rc = dasd_eer_init();
3752 #ifdef CONFIG_PROC_FS
3753 rc = dasd_proc_init();
3760 pr_info("The DASD device driver could not be initialized\n");
3765 module_init(dasd_init);
3766 module_exit(dasd_exit);
3768 EXPORT_SYMBOL(dasd_debug_area);
3769 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3771 EXPORT_SYMBOL(dasd_add_request_head);
3772 EXPORT_SYMBOL(dasd_add_request_tail);
3773 EXPORT_SYMBOL(dasd_cancel_req);
3774 EXPORT_SYMBOL(dasd_device_clear_timer);
3775 EXPORT_SYMBOL(dasd_block_clear_timer);
3776 EXPORT_SYMBOL(dasd_enable_device);
3777 EXPORT_SYMBOL(dasd_int_handler);
3778 EXPORT_SYMBOL(dasd_kfree_request);
3779 EXPORT_SYMBOL(dasd_kick_device);
3780 EXPORT_SYMBOL(dasd_kmalloc_request);
3781 EXPORT_SYMBOL(dasd_schedule_device_bh);
3782 EXPORT_SYMBOL(dasd_schedule_block_bh);
3783 EXPORT_SYMBOL(dasd_set_target_state);
3784 EXPORT_SYMBOL(dasd_device_set_timer);
3785 EXPORT_SYMBOL(dasd_block_set_timer);
3786 EXPORT_SYMBOL(dasd_sfree_request);
3787 EXPORT_SYMBOL(dasd_sleep_on);
3788 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3789 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3790 EXPORT_SYMBOL(dasd_smalloc_request);
3791 EXPORT_SYMBOL(dasd_start_IO);
3792 EXPORT_SYMBOL(dasd_term_IO);
3794 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3795 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3796 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3797 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3798 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3799 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3800 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3801 EXPORT_SYMBOL_GPL(dasd_alloc_block);
3802 EXPORT_SYMBOL_GPL(dasd_free_block);