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;
280 * Release the irq line for the device. Terminate any running i/o.
282 static int dasd_state_basic_to_known(struct dasd_device *device)
286 dasd_profile_exit(&device->block->profile);
287 if (device->block->debugfs_dentry)
288 debugfs_remove(device->block->debugfs_dentry);
289 dasd_gendisk_free(device->block);
290 dasd_block_clear_timer(device->block);
292 rc = dasd_flush_device_queue(device);
295 dasd_device_clear_timer(device);
296 dasd_profile_exit(&device->profile);
297 if (device->debugfs_dentry)
298 debugfs_remove(device->debugfs_dentry);
300 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
301 if (device->debug_area != NULL) {
302 debug_unregister(device->debug_area);
303 device->debug_area = NULL;
305 device->state = DASD_STATE_KNOWN;
310 * Do the initial analysis. The do_analysis function may return
311 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
312 * until the discipline decides to continue the startup sequence
313 * by calling the function dasd_change_state. The eckd disciplines
314 * uses this to start a ccw that detects the format. The completion
315 * interrupt for this detection ccw uses the kernel event daemon to
316 * trigger the call to dasd_change_state. All this is done in the
317 * discipline code, see dasd_eckd.c.
318 * After the analysis ccw is done (do_analysis returned 0) the block
320 * In case the analysis returns an error, the device setup is stopped
321 * (a fake disk was already added to allow formatting).
323 static int dasd_state_basic_to_ready(struct dasd_device *device)
326 struct dasd_block *block;
329 block = device->block;
330 /* make disk known with correct capacity */
332 if (block->base->discipline->do_analysis != NULL)
333 rc = block->base->discipline->do_analysis(block);
336 device->state = DASD_STATE_UNFMT;
339 dasd_setup_queue(block);
340 set_capacity(block->gdp,
341 block->blocks << block->s2b_shift);
342 device->state = DASD_STATE_READY;
343 rc = dasd_scan_partitions(block);
345 device->state = DASD_STATE_BASIC;
347 device->state = DASD_STATE_READY;
353 int _wait_for_empty_queues(struct dasd_device *device)
356 return list_empty(&device->ccw_queue) &&
357 list_empty(&device->block->ccw_queue);
359 return list_empty(&device->ccw_queue);
363 * Remove device from block device layer. Destroy dirty buffers.
364 * Forget format information. Check if the target level is basic
365 * and if it is create fake disk for formatting.
367 static int dasd_state_ready_to_basic(struct dasd_device *device)
371 device->state = DASD_STATE_BASIC;
373 struct dasd_block *block = device->block;
374 rc = dasd_flush_block_queue(block);
376 device->state = DASD_STATE_READY;
379 dasd_flush_request_queue(block);
380 dasd_destroy_partitions(block);
383 block->s2b_shift = 0;
391 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
393 device->state = DASD_STATE_BASIC;
398 * Make the device online and schedule the bottom half to start
399 * the requeueing of requests from the linux request queue to the
403 dasd_state_ready_to_online(struct dasd_device * device)
406 struct gendisk *disk;
407 struct disk_part_iter piter;
408 struct hd_struct *part;
410 if (device->discipline->ready_to_online) {
411 rc = device->discipline->ready_to_online(device);
415 device->state = DASD_STATE_ONLINE;
417 dasd_schedule_block_bh(device->block);
418 if ((device->features & DASD_FEATURE_USERAW)) {
419 disk = device->block->gdp;
420 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
423 disk = device->block->bdev->bd_disk;
424 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
425 while ((part = disk_part_iter_next(&piter)))
426 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
427 disk_part_iter_exit(&piter);
433 * Stop the requeueing of requests again.
435 static int dasd_state_online_to_ready(struct dasd_device *device)
438 struct gendisk *disk;
439 struct disk_part_iter piter;
440 struct hd_struct *part;
442 if (device->discipline->online_to_ready) {
443 rc = device->discipline->online_to_ready(device);
447 device->state = DASD_STATE_READY;
448 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
449 disk = device->block->bdev->bd_disk;
450 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
451 while ((part = disk_part_iter_next(&piter)))
452 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
453 disk_part_iter_exit(&piter);
459 * Device startup state changes.
461 static int dasd_increase_state(struct dasd_device *device)
466 if (device->state == DASD_STATE_NEW &&
467 device->target >= DASD_STATE_KNOWN)
468 rc = dasd_state_new_to_known(device);
471 device->state == DASD_STATE_KNOWN &&
472 device->target >= DASD_STATE_BASIC)
473 rc = dasd_state_known_to_basic(device);
476 device->state == DASD_STATE_BASIC &&
477 device->target >= DASD_STATE_READY)
478 rc = dasd_state_basic_to_ready(device);
481 device->state == DASD_STATE_UNFMT &&
482 device->target > DASD_STATE_UNFMT)
486 device->state == DASD_STATE_READY &&
487 device->target >= DASD_STATE_ONLINE)
488 rc = dasd_state_ready_to_online(device);
494 * Device shutdown state changes.
496 static int dasd_decrease_state(struct dasd_device *device)
501 if (device->state == DASD_STATE_ONLINE &&
502 device->target <= DASD_STATE_READY)
503 rc = dasd_state_online_to_ready(device);
506 device->state == DASD_STATE_READY &&
507 device->target <= DASD_STATE_BASIC)
508 rc = dasd_state_ready_to_basic(device);
511 device->state == DASD_STATE_UNFMT &&
512 device->target <= DASD_STATE_BASIC)
513 rc = dasd_state_unfmt_to_basic(device);
516 device->state == DASD_STATE_BASIC &&
517 device->target <= DASD_STATE_KNOWN)
518 rc = dasd_state_basic_to_known(device);
521 device->state == DASD_STATE_KNOWN &&
522 device->target <= DASD_STATE_NEW)
523 rc = dasd_state_known_to_new(device);
529 * This is the main startup/shutdown routine.
531 static void dasd_change_state(struct dasd_device *device)
535 if (device->state == device->target)
536 /* Already where we want to go today... */
538 if (device->state < device->target)
539 rc = dasd_increase_state(device);
541 rc = dasd_decrease_state(device);
545 device->target = device->state;
547 /* let user-space know that the device status changed */
548 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
550 if (device->state == device->target)
551 wake_up(&dasd_init_waitq);
555 * Kick starter for devices that did not complete the startup/shutdown
556 * procedure or were sleeping because of a pending state.
557 * dasd_kick_device will schedule a call do do_kick_device to the kernel
560 static void do_kick_device(struct work_struct *work)
562 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
563 mutex_lock(&device->state_mutex);
564 dasd_change_state(device);
565 mutex_unlock(&device->state_mutex);
566 dasd_schedule_device_bh(device);
567 dasd_put_device(device);
570 void dasd_kick_device(struct dasd_device *device)
572 dasd_get_device(device);
573 /* queue call to dasd_kick_device to the kernel event daemon. */
574 schedule_work(&device->kick_work);
578 * dasd_reload_device will schedule a call do do_reload_device to the kernel
581 static void do_reload_device(struct work_struct *work)
583 struct dasd_device *device = container_of(work, struct dasd_device,
585 device->discipline->reload(device);
586 dasd_put_device(device);
589 void dasd_reload_device(struct dasd_device *device)
591 dasd_get_device(device);
592 /* queue call to dasd_reload_device to the kernel event daemon. */
593 schedule_work(&device->reload_device);
595 EXPORT_SYMBOL(dasd_reload_device);
598 * dasd_restore_device will schedule a call do do_restore_device to the kernel
601 static void do_restore_device(struct work_struct *work)
603 struct dasd_device *device = container_of(work, struct dasd_device,
605 device->cdev->drv->restore(device->cdev);
606 dasd_put_device(device);
609 void dasd_restore_device(struct dasd_device *device)
611 dasd_get_device(device);
612 /* queue call to dasd_restore_device to the kernel event daemon. */
613 schedule_work(&device->restore_device);
617 * Set the target state for a device and starts the state change.
619 void dasd_set_target_state(struct dasd_device *device, int target)
621 dasd_get_device(device);
622 mutex_lock(&device->state_mutex);
623 /* If we are in probeonly mode stop at DASD_STATE_READY. */
624 if (dasd_probeonly && target > DASD_STATE_READY)
625 target = DASD_STATE_READY;
626 if (device->target != target) {
627 if (device->state == target)
628 wake_up(&dasd_init_waitq);
629 device->target = target;
631 if (device->state != device->target)
632 dasd_change_state(device);
633 mutex_unlock(&device->state_mutex);
634 dasd_put_device(device);
638 * Enable devices with device numbers in [from..to].
640 static inline int _wait_for_device(struct dasd_device *device)
642 return (device->state == device->target);
645 void dasd_enable_device(struct dasd_device *device)
647 dasd_set_target_state(device, DASD_STATE_ONLINE);
648 if (device->state <= DASD_STATE_KNOWN)
649 /* No discipline for device found. */
650 dasd_set_target_state(device, DASD_STATE_NEW);
651 /* Now wait for the devices to come up. */
652 wait_event(dasd_init_waitq, _wait_for_device(device));
654 dasd_reload_device(device);
655 if (device->discipline->kick_validate)
656 device->discipline->kick_validate(device);
660 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
663 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
665 #ifdef CONFIG_DASD_PROFILE
666 struct dasd_profile_info dasd_global_profile_data;
667 static struct dentry *dasd_global_profile_dentry;
668 static struct dentry *dasd_debugfs_global_entry;
671 * Add profiling information for cqr before execution.
673 static void dasd_profile_start(struct dasd_block *block,
674 struct dasd_ccw_req *cqr,
678 unsigned int counter;
679 struct dasd_device *device;
681 /* count the length of the chanq for statistics */
683 if (dasd_global_profile_level || block->profile.data)
684 list_for_each(l, &block->ccw_queue)
688 if (dasd_global_profile_level) {
689 dasd_global_profile_data.dasd_io_nr_req[counter]++;
690 if (rq_data_dir(req) == READ)
691 dasd_global_profile_data.dasd_read_nr_req[counter]++;
694 spin_lock(&block->profile.lock);
695 if (block->profile.data)
696 block->profile.data->dasd_io_nr_req[counter]++;
697 if (rq_data_dir(req) == READ)
698 block->profile.data->dasd_read_nr_req[counter]++;
699 spin_unlock(&block->profile.lock);
702 * We count the request for the start device, even though it may run on
703 * some other device due to error recovery. This way we make sure that
704 * we count each request only once.
706 device = cqr->startdev;
707 if (device->profile.data) {
708 counter = 1; /* request is not yet queued on the start device */
709 list_for_each(l, &device->ccw_queue)
713 spin_lock(&device->profile.lock);
714 if (device->profile.data) {
715 device->profile.data->dasd_io_nr_req[counter]++;
716 if (rq_data_dir(req) == READ)
717 device->profile.data->dasd_read_nr_req[counter]++;
719 spin_unlock(&device->profile.lock);
723 * Add profiling information for cqr after execution.
726 #define dasd_profile_counter(value, index) \
728 for (index = 0; index < 31 && value >> (2+index); index++) \
732 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
745 /* in case of an overflow, reset the whole profile */
746 if (data->dasd_io_reqs == UINT_MAX) {
747 memset(data, 0, sizeof(*data));
748 getnstimeofday(&data->starttod);
750 data->dasd_io_reqs++;
751 data->dasd_io_sects += sectors;
753 data->dasd_io_alias++;
757 data->dasd_io_secs[sectors_ind]++;
758 data->dasd_io_times[tottime_ind]++;
759 data->dasd_io_timps[tottimeps_ind]++;
760 data->dasd_io_time1[strtime_ind]++;
761 data->dasd_io_time2[irqtime_ind]++;
762 data->dasd_io_time2ps[irqtimeps_ind]++;
763 data->dasd_io_time3[endtime_ind]++;
766 data->dasd_read_reqs++;
767 data->dasd_read_sects += sectors;
769 data->dasd_read_alias++;
771 data->dasd_read_tpm++;
772 data->dasd_read_secs[sectors_ind]++;
773 data->dasd_read_times[tottime_ind]++;
774 data->dasd_read_time1[strtime_ind]++;
775 data->dasd_read_time2[irqtime_ind]++;
776 data->dasd_read_time3[endtime_ind]++;
780 static void dasd_profile_end(struct dasd_block *block,
781 struct dasd_ccw_req *cqr,
784 long strtime, irqtime, endtime, tottime; /* in microseconds */
785 long tottimeps, sectors;
786 struct dasd_device *device;
787 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
788 int irqtime_ind, irqtimeps_ind, endtime_ind;
790 device = cqr->startdev;
791 if (!(dasd_global_profile_level ||
792 block->profile.data ||
793 device->profile.data))
796 sectors = blk_rq_sectors(req);
797 if (!cqr->buildclk || !cqr->startclk ||
798 !cqr->stopclk || !cqr->endclk ||
802 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
803 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
804 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
805 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
806 tottimeps = tottime / sectors;
808 dasd_profile_counter(sectors, sectors_ind);
809 dasd_profile_counter(tottime, tottime_ind);
810 dasd_profile_counter(tottimeps, tottimeps_ind);
811 dasd_profile_counter(strtime, strtime_ind);
812 dasd_profile_counter(irqtime, irqtime_ind);
813 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
814 dasd_profile_counter(endtime, endtime_ind);
816 if (dasd_global_profile_level) {
817 dasd_profile_end_add_data(&dasd_global_profile_data,
818 cqr->startdev != block->base,
820 rq_data_dir(req) == READ,
821 sectors, sectors_ind, tottime_ind,
822 tottimeps_ind, strtime_ind,
823 irqtime_ind, irqtimeps_ind,
827 spin_lock(&block->profile.lock);
828 if (block->profile.data)
829 dasd_profile_end_add_data(block->profile.data,
830 cqr->startdev != block->base,
832 rq_data_dir(req) == READ,
833 sectors, sectors_ind, tottime_ind,
834 tottimeps_ind, strtime_ind,
835 irqtime_ind, irqtimeps_ind,
837 spin_unlock(&block->profile.lock);
839 spin_lock(&device->profile.lock);
840 if (device->profile.data)
841 dasd_profile_end_add_data(device->profile.data,
842 cqr->startdev != block->base,
844 rq_data_dir(req) == READ,
845 sectors, sectors_ind, tottime_ind,
846 tottimeps_ind, strtime_ind,
847 irqtime_ind, irqtimeps_ind,
849 spin_unlock(&device->profile.lock);
852 void dasd_profile_reset(struct dasd_profile *profile)
854 struct dasd_profile_info *data;
856 spin_lock_bh(&profile->lock);
857 data = profile->data;
859 spin_unlock_bh(&profile->lock);
862 memset(data, 0, sizeof(*data));
863 getnstimeofday(&data->starttod);
864 spin_unlock_bh(&profile->lock);
867 void dasd_global_profile_reset(void)
869 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
870 getnstimeofday(&dasd_global_profile_data.starttod);
873 int dasd_profile_on(struct dasd_profile *profile)
875 struct dasd_profile_info *data;
877 data = kzalloc(sizeof(*data), GFP_KERNEL);
880 spin_lock_bh(&profile->lock);
882 spin_unlock_bh(&profile->lock);
886 getnstimeofday(&data->starttod);
887 profile->data = data;
888 spin_unlock_bh(&profile->lock);
892 void dasd_profile_off(struct dasd_profile *profile)
894 spin_lock_bh(&profile->lock);
895 kfree(profile->data);
896 profile->data = NULL;
897 spin_unlock_bh(&profile->lock);
900 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
904 buffer = vmalloc(user_len + 1);
906 return ERR_PTR(-ENOMEM);
907 if (copy_from_user(buffer, user_buf, user_len) != 0) {
909 return ERR_PTR(-EFAULT);
911 /* got the string, now strip linefeed. */
912 if (buffer[user_len - 1] == '\n')
913 buffer[user_len - 1] = 0;
915 buffer[user_len] = 0;
919 static ssize_t dasd_stats_write(struct file *file,
920 const char __user *user_buf,
921 size_t user_len, loff_t *pos)
925 struct seq_file *m = (struct seq_file *)file->private_data;
926 struct dasd_profile *prof = m->private;
928 if (user_len > 65536)
930 buffer = dasd_get_user_string(user_buf, user_len);
932 return PTR_ERR(buffer);
934 str = skip_spaces(buffer);
936 if (strncmp(str, "reset", 5) == 0) {
937 dasd_profile_reset(prof);
938 } else if (strncmp(str, "on", 2) == 0) {
939 rc = dasd_profile_on(prof);
942 } else if (strncmp(str, "off", 3) == 0) {
943 dasd_profile_off(prof);
950 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
954 for (i = 0; i < 32; i++)
955 seq_printf(m, "%u ", array[i]);
959 static void dasd_stats_seq_print(struct seq_file *m,
960 struct dasd_profile_info *data)
962 seq_printf(m, "start_time %ld.%09ld\n",
963 data->starttod.tv_sec, data->starttod.tv_nsec);
964 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
965 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
966 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
967 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
968 seq_printf(m, "histogram_sectors ");
969 dasd_stats_array(m, data->dasd_io_secs);
970 seq_printf(m, "histogram_io_times ");
971 dasd_stats_array(m, data->dasd_io_times);
972 seq_printf(m, "histogram_io_times_weighted ");
973 dasd_stats_array(m, data->dasd_io_timps);
974 seq_printf(m, "histogram_time_build_to_ssch ");
975 dasd_stats_array(m, data->dasd_io_time1);
976 seq_printf(m, "histogram_time_ssch_to_irq ");
977 dasd_stats_array(m, data->dasd_io_time2);
978 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
979 dasd_stats_array(m, data->dasd_io_time2ps);
980 seq_printf(m, "histogram_time_irq_to_end ");
981 dasd_stats_array(m, data->dasd_io_time3);
982 seq_printf(m, "histogram_ccw_queue_length ");
983 dasd_stats_array(m, data->dasd_io_nr_req);
984 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
985 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
986 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
987 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
988 seq_printf(m, "histogram_read_sectors ");
989 dasd_stats_array(m, data->dasd_read_secs);
990 seq_printf(m, "histogram_read_times ");
991 dasd_stats_array(m, data->dasd_read_times);
992 seq_printf(m, "histogram_read_time_build_to_ssch ");
993 dasd_stats_array(m, data->dasd_read_time1);
994 seq_printf(m, "histogram_read_time_ssch_to_irq ");
995 dasd_stats_array(m, data->dasd_read_time2);
996 seq_printf(m, "histogram_read_time_irq_to_end ");
997 dasd_stats_array(m, data->dasd_read_time3);
998 seq_printf(m, "histogram_read_ccw_queue_length ");
999 dasd_stats_array(m, data->dasd_read_nr_req);
1002 static int dasd_stats_show(struct seq_file *m, void *v)
1004 struct dasd_profile *profile;
1005 struct dasd_profile_info *data;
1007 profile = m->private;
1008 spin_lock_bh(&profile->lock);
1009 data = profile->data;
1011 spin_unlock_bh(&profile->lock);
1012 seq_printf(m, "disabled\n");
1015 dasd_stats_seq_print(m, data);
1016 spin_unlock_bh(&profile->lock);
1020 static int dasd_stats_open(struct inode *inode, struct file *file)
1022 struct dasd_profile *profile = inode->i_private;
1023 return single_open(file, dasd_stats_show, profile);
1026 static const struct file_operations dasd_stats_raw_fops = {
1027 .owner = THIS_MODULE,
1028 .open = dasd_stats_open,
1030 .llseek = seq_lseek,
1031 .release = single_release,
1032 .write = dasd_stats_write,
1035 static ssize_t dasd_stats_global_write(struct file *file,
1036 const char __user *user_buf,
1037 size_t user_len, loff_t *pos)
1042 if (user_len > 65536)
1044 buffer = dasd_get_user_string(user_buf, user_len);
1046 return PTR_ERR(buffer);
1047 str = skip_spaces(buffer);
1049 if (strncmp(str, "reset", 5) == 0) {
1050 dasd_global_profile_reset();
1051 } else if (strncmp(str, "on", 2) == 0) {
1052 dasd_global_profile_reset();
1053 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1054 } else if (strncmp(str, "off", 3) == 0) {
1055 dasd_global_profile_level = DASD_PROFILE_OFF;
1062 static int dasd_stats_global_show(struct seq_file *m, void *v)
1064 if (!dasd_global_profile_level) {
1065 seq_printf(m, "disabled\n");
1068 dasd_stats_seq_print(m, &dasd_global_profile_data);
1072 static int dasd_stats_global_open(struct inode *inode, struct file *file)
1074 return single_open(file, dasd_stats_global_show, NULL);
1077 static const struct file_operations dasd_stats_global_fops = {
1078 .owner = THIS_MODULE,
1079 .open = dasd_stats_global_open,
1081 .llseek = seq_lseek,
1082 .release = single_release,
1083 .write = dasd_stats_global_write,
1086 static void dasd_profile_init(struct dasd_profile *profile,
1087 struct dentry *base_dentry)
1094 profile->dentry = NULL;
1095 profile->data = NULL;
1096 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1097 pde = debugfs_create_file("statistics", mode, base_dentry,
1098 profile, &dasd_stats_raw_fops);
1099 if (pde && !IS_ERR(pde))
1100 profile->dentry = pde;
1104 static void dasd_profile_exit(struct dasd_profile *profile)
1106 dasd_profile_off(profile);
1107 if (profile->dentry) {
1108 debugfs_remove(profile->dentry);
1109 profile->dentry = NULL;
1113 static void dasd_statistics_removeroot(void)
1115 dasd_global_profile_level = DASD_PROFILE_OFF;
1116 if (dasd_global_profile_dentry) {
1117 debugfs_remove(dasd_global_profile_dentry);
1118 dasd_global_profile_dentry = NULL;
1120 if (dasd_debugfs_global_entry)
1121 debugfs_remove(dasd_debugfs_global_entry);
1122 if (dasd_debugfs_root_entry)
1123 debugfs_remove(dasd_debugfs_root_entry);
1126 static void dasd_statistics_createroot(void)
1131 dasd_debugfs_root_entry = NULL;
1132 dasd_debugfs_global_entry = NULL;
1133 dasd_global_profile_dentry = NULL;
1134 pde = debugfs_create_dir("dasd", NULL);
1135 if (!pde || IS_ERR(pde))
1137 dasd_debugfs_root_entry = pde;
1138 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1139 if (!pde || IS_ERR(pde))
1141 dasd_debugfs_global_entry = pde;
1143 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1144 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1145 NULL, &dasd_stats_global_fops);
1146 if (!pde || IS_ERR(pde))
1148 dasd_global_profile_dentry = pde;
1152 DBF_EVENT(DBF_ERR, "%s",
1153 "Creation of the dasd debugfs interface failed");
1154 dasd_statistics_removeroot();
1159 #define dasd_profile_start(block, cqr, req) do {} while (0)
1160 #define dasd_profile_end(block, cqr, req) do {} while (0)
1162 static void dasd_statistics_createroot(void)
1167 static void dasd_statistics_removeroot(void)
1172 int dasd_stats_generic_show(struct seq_file *m, void *v)
1174 seq_printf(m, "Statistics are not activated in this kernel\n");
1178 static void dasd_profile_init(struct dasd_profile *profile,
1179 struct dentry *base_dentry)
1184 static void dasd_profile_exit(struct dasd_profile *profile)
1189 int dasd_profile_on(struct dasd_profile *profile)
1194 #endif /* CONFIG_DASD_PROFILE */
1197 * Allocate memory for a channel program with 'cplength' channel
1198 * command words and 'datasize' additional space. There are two
1199 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1200 * memory and 2) dasd_smalloc_request uses the static ccw memory
1201 * that gets allocated for each device.
1203 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
1205 struct dasd_device *device)
1207 struct dasd_ccw_req *cqr;
1210 BUG_ON(datasize > PAGE_SIZE ||
1211 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1213 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1215 return ERR_PTR(-ENOMEM);
1218 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1219 GFP_ATOMIC | GFP_DMA);
1220 if (cqr->cpaddr == NULL) {
1222 return ERR_PTR(-ENOMEM);
1227 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1228 if (cqr->data == NULL) {
1231 return ERR_PTR(-ENOMEM);
1235 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1236 dasd_get_device(device);
1240 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
1242 struct dasd_device *device)
1244 unsigned long flags;
1245 struct dasd_ccw_req *cqr;
1249 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1251 size += cplength * sizeof(struct ccw1);
1254 spin_lock_irqsave(&device->mem_lock, flags);
1255 cqr = (struct dasd_ccw_req *)
1256 dasd_alloc_chunk(&device->ccw_chunks, size);
1257 spin_unlock_irqrestore(&device->mem_lock, flags);
1259 return ERR_PTR(-ENOMEM);
1260 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1261 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1264 cqr->cpaddr = (struct ccw1 *) data;
1265 data += cplength*sizeof(struct ccw1);
1266 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1271 memset(cqr->data, 0, datasize);
1274 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1275 dasd_get_device(device);
1280 * Free memory of a channel program. This function needs to free all the
1281 * idal lists that might have been created by dasd_set_cda and the
1282 * struct dasd_ccw_req itself.
1284 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1289 /* Clear any idals used for the request. */
1292 clear_normalized_cda(ccw);
1293 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1298 dasd_put_device(device);
1301 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1303 unsigned long flags;
1305 spin_lock_irqsave(&device->mem_lock, flags);
1306 dasd_free_chunk(&device->ccw_chunks, cqr);
1307 spin_unlock_irqrestore(&device->mem_lock, flags);
1308 dasd_put_device(device);
1312 * Check discipline magic in cqr.
1314 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1316 struct dasd_device *device;
1320 device = cqr->startdev;
1321 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1322 DBF_DEV_EVENT(DBF_WARNING, device,
1323 " dasd_ccw_req 0x%08x magic doesn't match"
1324 " discipline 0x%08x",
1326 *(unsigned int *) device->discipline->name);
1333 * Terminate the current i/o and set the request to clear_pending.
1334 * Timer keeps device runnig.
1335 * ccw_device_clear can fail if the i/o subsystem
1338 int dasd_term_IO(struct dasd_ccw_req *cqr)
1340 struct dasd_device *device;
1342 char errorstring[ERRORLENGTH];
1345 rc = dasd_check_cqr(cqr);
1349 device = (struct dasd_device *) cqr->startdev;
1350 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1351 rc = ccw_device_clear(device->cdev, (long) cqr);
1353 case 0: /* termination successful */
1354 cqr->status = DASD_CQR_CLEAR_PENDING;
1355 cqr->stopclk = get_tod_clock();
1357 DBF_DEV_EVENT(DBF_DEBUG, device,
1358 "terminate cqr %p successful",
1362 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1363 "device gone, retry");
1366 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1367 "I/O error, retry");
1371 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1372 "device busy, retry later");
1375 /* internal error 10 - unknown rc*/
1376 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1377 dev_err(&device->cdev->dev, "An error occurred in the "
1378 "DASD device driver, reason=%s\n", errorstring);
1384 dasd_schedule_device_bh(device);
1389 * Start the i/o. This start_IO can fail if the channel is really busy.
1390 * In that case set up a timer to start the request later.
1392 int dasd_start_IO(struct dasd_ccw_req *cqr)
1394 struct dasd_device *device;
1396 char errorstring[ERRORLENGTH];
1399 rc = dasd_check_cqr(cqr);
1404 device = (struct dasd_device *) cqr->startdev;
1406 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1407 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1408 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1409 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1410 "because of stolen lock", cqr);
1411 cqr->status = DASD_CQR_ERROR;
1412 cqr->intrc = -EPERM;
1415 if (cqr->retries < 0) {
1416 /* internal error 14 - start_IO run out of retries */
1417 sprintf(errorstring, "14 %p", cqr);
1418 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1419 "device driver, reason=%s\n", errorstring);
1420 cqr->status = DASD_CQR_ERROR;
1423 cqr->startclk = get_tod_clock();
1424 cqr->starttime = jiffies;
1426 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1427 cqr->lpm &= device->path_data.opm;
1429 cqr->lpm = device->path_data.opm;
1431 if (cqr->cpmode == 1) {
1432 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1433 (long) cqr, cqr->lpm);
1435 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1436 (long) cqr, cqr->lpm, 0);
1440 cqr->status = DASD_CQR_IN_IO;
1443 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1444 "start_IO: device busy, retry later");
1447 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1448 "start_IO: request timeout, retry later");
1451 /* -EACCES indicates that the request used only a subset of the
1452 * available paths and all these paths are gone. If the lpm of
1453 * this request was only a subset of the opm (e.g. the ppm) then
1454 * we just do a retry with all available paths.
1455 * If we already use the full opm, something is amiss, and we
1456 * need a full path verification.
1458 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1459 DBF_DEV_EVENT(DBF_WARNING, device,
1460 "start_IO: selected paths gone (%x)",
1462 } else if (cqr->lpm != device->path_data.opm) {
1463 cqr->lpm = device->path_data.opm;
1464 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1465 "start_IO: selected paths gone,"
1466 " retry on all paths");
1468 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1469 "start_IO: all paths in opm gone,"
1470 " do path verification");
1471 dasd_generic_last_path_gone(device);
1472 device->path_data.opm = 0;
1473 device->path_data.ppm = 0;
1474 device->path_data.npm = 0;
1475 device->path_data.tbvpm =
1476 ccw_device_get_path_mask(device->cdev);
1480 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1481 "start_IO: -ENODEV device gone, retry");
1484 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1485 "start_IO: -EIO device gone, retry");
1488 /* most likely caused in power management context */
1489 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1490 "start_IO: -EINVAL device currently "
1494 /* internal error 11 - unknown rc */
1495 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1496 dev_err(&device->cdev->dev,
1497 "An error occurred in the DASD device driver, "
1498 "reason=%s\n", errorstring);
1507 * Timeout function for dasd devices. This is used for different purposes
1508 * 1) missing interrupt handler for normal operation
1509 * 2) delayed start of request where start_IO failed with -EBUSY
1510 * 3) timeout for missing state change interrupts
1511 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1512 * DASD_CQR_QUEUED for 2) and 3).
1514 static void dasd_device_timeout(unsigned long ptr)
1516 unsigned long flags;
1517 struct dasd_device *device;
1519 device = (struct dasd_device *) ptr;
1520 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1521 /* re-activate request queue */
1522 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1523 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1524 dasd_schedule_device_bh(device);
1528 * Setup timeout for a device in jiffies.
1530 void dasd_device_set_timer(struct dasd_device *device, int expires)
1533 del_timer(&device->timer);
1535 mod_timer(&device->timer, jiffies + expires);
1539 * Clear timeout for a device.
1541 void dasd_device_clear_timer(struct dasd_device *device)
1543 del_timer(&device->timer);
1546 static void dasd_handle_killed_request(struct ccw_device *cdev,
1547 unsigned long intparm)
1549 struct dasd_ccw_req *cqr;
1550 struct dasd_device *device;
1554 cqr = (struct dasd_ccw_req *) intparm;
1555 if (cqr->status != DASD_CQR_IN_IO) {
1556 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1557 "invalid status in handle_killed_request: "
1558 "%02x", cqr->status);
1562 device = dasd_device_from_cdev_locked(cdev);
1563 if (IS_ERR(device)) {
1564 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1565 "unable to get device from cdev");
1569 if (!cqr->startdev ||
1570 device != cqr->startdev ||
1571 strncmp(cqr->startdev->discipline->ebcname,
1572 (char *) &cqr->magic, 4)) {
1573 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1574 "invalid device in request");
1575 dasd_put_device(device);
1579 /* Schedule request to be retried. */
1580 cqr->status = DASD_CQR_QUEUED;
1582 dasd_device_clear_timer(device);
1583 dasd_schedule_device_bh(device);
1584 dasd_put_device(device);
1587 void dasd_generic_handle_state_change(struct dasd_device *device)
1589 /* First of all start sense subsystem status request. */
1590 dasd_eer_snss(device);
1592 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1593 dasd_schedule_device_bh(device);
1595 dasd_schedule_block_bh(device->block);
1599 * Interrupt handler for "normal" ssch-io based dasd devices.
1601 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1604 struct dasd_ccw_req *cqr, *next;
1605 struct dasd_device *device;
1606 unsigned long long now;
1610 switch (PTR_ERR(irb)) {
1614 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1615 "request timed out\n", __func__);
1618 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1619 "unknown error %ld\n", __func__,
1622 dasd_handle_killed_request(cdev, intparm);
1626 now = get_tod_clock();
1627 cqr = (struct dasd_ccw_req *) intparm;
1628 /* check for conditions that should be handled immediately */
1630 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1631 scsw_cstat(&irb->scsw) == 0)) {
1633 memcpy(&cqr->irb, irb, sizeof(*irb));
1634 device = dasd_device_from_cdev_locked(cdev);
1637 /* ignore unsolicited interrupts for DIAG discipline */
1638 if (device->discipline == dasd_diag_discipline_pointer) {
1639 dasd_put_device(device);
1642 device->discipline->dump_sense_dbf(device, irb, "int");
1643 if (device->features & DASD_FEATURE_ERPLOG)
1644 device->discipline->dump_sense(device, cqr, irb);
1645 device->discipline->check_for_device_change(device, cqr, irb);
1646 dasd_put_device(device);
1651 device = (struct dasd_device *) cqr->startdev;
1653 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1654 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1655 "invalid device in request");
1659 /* Check for clear pending */
1660 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1661 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1662 cqr->status = DASD_CQR_CLEARED;
1663 dasd_device_clear_timer(device);
1664 wake_up(&dasd_flush_wq);
1665 dasd_schedule_device_bh(device);
1669 /* check status - the request might have been killed by dyn detach */
1670 if (cqr->status != DASD_CQR_IN_IO) {
1671 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1672 "status %02x", dev_name(&cdev->dev), cqr->status);
1678 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1679 scsw_cstat(&irb->scsw) == 0) {
1680 /* request was completed successfully */
1681 cqr->status = DASD_CQR_SUCCESS;
1683 /* Start first request on queue if possible -> fast_io. */
1684 if (cqr->devlist.next != &device->ccw_queue) {
1685 next = list_entry(cqr->devlist.next,
1686 struct dasd_ccw_req, devlist);
1688 } else { /* error */
1690 * If we don't want complex ERP for this request, then just
1691 * reset this and retry it in the fastpath
1693 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1695 if (cqr->lpm == device->path_data.opm)
1696 DBF_DEV_EVENT(DBF_DEBUG, device,
1697 "default ERP in fastpath "
1698 "(%i retries left)",
1700 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1701 cqr->lpm = device->path_data.opm;
1702 cqr->status = DASD_CQR_QUEUED;
1705 cqr->status = DASD_CQR_ERROR;
1707 if (next && (next->status == DASD_CQR_QUEUED) &&
1708 (!device->stopped)) {
1709 if (device->discipline->start_IO(next) == 0)
1710 expires = next->expires;
1713 dasd_device_set_timer(device, expires);
1715 dasd_device_clear_timer(device);
1716 dasd_schedule_device_bh(device);
1719 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1721 struct dasd_device *device;
1723 device = dasd_device_from_cdev_locked(cdev);
1727 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1728 device->state != device->target ||
1729 !device->discipline->check_for_device_change){
1730 dasd_put_device(device);
1733 if (device->discipline->dump_sense_dbf)
1734 device->discipline->dump_sense_dbf(device, irb, "uc");
1735 device->discipline->check_for_device_change(device, NULL, irb);
1736 dasd_put_device(device);
1738 return UC_TODO_RETRY;
1740 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1743 * If we have an error on a dasd_block layer request then we cancel
1744 * and return all further requests from the same dasd_block as well.
1746 static void __dasd_device_recovery(struct dasd_device *device,
1747 struct dasd_ccw_req *ref_cqr)
1749 struct list_head *l, *n;
1750 struct dasd_ccw_req *cqr;
1753 * only requeue request that came from the dasd_block layer
1755 if (!ref_cqr->block)
1758 list_for_each_safe(l, n, &device->ccw_queue) {
1759 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1760 if (cqr->status == DASD_CQR_QUEUED &&
1761 ref_cqr->block == cqr->block) {
1762 cqr->status = DASD_CQR_CLEARED;
1768 * Remove those ccw requests from the queue that need to be returned
1769 * to the upper layer.
1771 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1772 struct list_head *final_queue)
1774 struct list_head *l, *n;
1775 struct dasd_ccw_req *cqr;
1777 /* Process request with final status. */
1778 list_for_each_safe(l, n, &device->ccw_queue) {
1779 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1781 /* Stop list processing at the first non-final request. */
1782 if (cqr->status == DASD_CQR_QUEUED ||
1783 cqr->status == DASD_CQR_IN_IO ||
1784 cqr->status == DASD_CQR_CLEAR_PENDING)
1786 if (cqr->status == DASD_CQR_ERROR) {
1787 __dasd_device_recovery(device, cqr);
1789 /* Rechain finished requests to final queue */
1790 list_move_tail(&cqr->devlist, final_queue);
1795 * the cqrs from the final queue are returned to the upper layer
1796 * by setting a dasd_block state and calling the callback function
1798 static void __dasd_device_process_final_queue(struct dasd_device *device,
1799 struct list_head *final_queue)
1801 struct list_head *l, *n;
1802 struct dasd_ccw_req *cqr;
1803 struct dasd_block *block;
1804 void (*callback)(struct dasd_ccw_req *, void *data);
1805 void *callback_data;
1806 char errorstring[ERRORLENGTH];
1808 list_for_each_safe(l, n, final_queue) {
1809 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1810 list_del_init(&cqr->devlist);
1812 callback = cqr->callback;
1813 callback_data = cqr->callback_data;
1815 spin_lock_bh(&block->queue_lock);
1816 switch (cqr->status) {
1817 case DASD_CQR_SUCCESS:
1818 cqr->status = DASD_CQR_DONE;
1820 case DASD_CQR_ERROR:
1821 cqr->status = DASD_CQR_NEED_ERP;
1823 case DASD_CQR_CLEARED:
1824 cqr->status = DASD_CQR_TERMINATED;
1827 /* internal error 12 - wrong cqr status*/
1828 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1829 dev_err(&device->cdev->dev,
1830 "An error occurred in the DASD device driver, "
1831 "reason=%s\n", errorstring);
1834 if (cqr->callback != NULL)
1835 (callback)(cqr, callback_data);
1837 spin_unlock_bh(&block->queue_lock);
1842 * Take a look at the first request on the ccw queue and check
1843 * if it reached its expire time. If so, terminate the IO.
1845 static void __dasd_device_check_expire(struct dasd_device *device)
1847 struct dasd_ccw_req *cqr;
1849 if (list_empty(&device->ccw_queue))
1851 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1852 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1853 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1854 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1856 * IO in safe offline processing should not
1857 * run out of retries
1861 if (device->discipline->term_IO(cqr) != 0) {
1862 /* Hmpf, try again in 5 sec */
1863 dev_err(&device->cdev->dev,
1864 "cqr %p timed out (%lus) but cannot be "
1865 "ended, retrying in 5 s\n",
1866 cqr, (cqr->expires/HZ));
1867 cqr->expires += 5*HZ;
1868 dasd_device_set_timer(device, 5*HZ);
1870 dev_err(&device->cdev->dev,
1871 "cqr %p timed out (%lus), %i retries "
1872 "remaining\n", cqr, (cqr->expires/HZ),
1879 * Take a look at the first request on the ccw queue and check
1880 * if it needs to be started.
1882 static void __dasd_device_start_head(struct dasd_device *device)
1884 struct dasd_ccw_req *cqr;
1887 if (list_empty(&device->ccw_queue))
1889 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1890 if (cqr->status != DASD_CQR_QUEUED)
1892 /* when device is stopped, return request to previous layer
1893 * exception: only the disconnect or unresumed bits are set and the
1894 * cqr is a path verification request
1896 if (device->stopped &&
1897 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1898 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1899 cqr->intrc = -EAGAIN;
1900 cqr->status = DASD_CQR_CLEARED;
1901 dasd_schedule_device_bh(device);
1905 rc = device->discipline->start_IO(cqr);
1907 dasd_device_set_timer(device, cqr->expires);
1908 else if (rc == -EACCES) {
1909 dasd_schedule_device_bh(device);
1911 /* Hmpf, try again in 1/2 sec */
1912 dasd_device_set_timer(device, 50);
1915 static void __dasd_device_check_path_events(struct dasd_device *device)
1919 if (device->path_data.tbvpm) {
1920 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1923 rc = device->discipline->verify_path(
1924 device, device->path_data.tbvpm);
1926 dasd_device_set_timer(device, 50);
1928 device->path_data.tbvpm = 0;
1933 * Go through all request on the dasd_device request queue,
1934 * terminate them on the cdev if necessary, and return them to the
1935 * submitting layer via callback.
1937 * Make sure that all 'submitting layers' still exist when
1938 * this function is called!. In other words, when 'device' is a base
1939 * device then all block layer requests must have been removed before
1940 * via dasd_flush_block_queue.
1942 int dasd_flush_device_queue(struct dasd_device *device)
1944 struct dasd_ccw_req *cqr, *n;
1946 struct list_head flush_queue;
1948 INIT_LIST_HEAD(&flush_queue);
1949 spin_lock_irq(get_ccwdev_lock(device->cdev));
1951 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1952 /* Check status and move request to flush_queue */
1953 switch (cqr->status) {
1954 case DASD_CQR_IN_IO:
1955 rc = device->discipline->term_IO(cqr);
1957 /* unable to terminate requeust */
1958 dev_err(&device->cdev->dev,
1959 "Flushing the DASD request queue "
1960 "failed for request %p\n", cqr);
1961 /* stop flush processing */
1965 case DASD_CQR_QUEUED:
1966 cqr->stopclk = get_tod_clock();
1967 cqr->status = DASD_CQR_CLEARED;
1969 default: /* no need to modify the others */
1972 list_move_tail(&cqr->devlist, &flush_queue);
1975 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1977 * After this point all requests must be in state CLEAR_PENDING,
1978 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1979 * one of the others.
1981 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1982 wait_event(dasd_flush_wq,
1983 (cqr->status != DASD_CQR_CLEAR_PENDING));
1985 * Now set each request back to TERMINATED, DONE or NEED_ERP
1986 * and call the callback function of flushed requests
1988 __dasd_device_process_final_queue(device, &flush_queue);
1993 * Acquire the device lock and process queues for the device.
1995 static void dasd_device_tasklet(struct dasd_device *device)
1997 struct list_head final_queue;
1999 atomic_set (&device->tasklet_scheduled, 0);
2000 INIT_LIST_HEAD(&final_queue);
2001 spin_lock_irq(get_ccwdev_lock(device->cdev));
2002 /* Check expire time of first request on the ccw queue. */
2003 __dasd_device_check_expire(device);
2004 /* find final requests on ccw queue */
2005 __dasd_device_process_ccw_queue(device, &final_queue);
2006 __dasd_device_check_path_events(device);
2007 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2008 /* Now call the callback function of requests with final status */
2009 __dasd_device_process_final_queue(device, &final_queue);
2010 spin_lock_irq(get_ccwdev_lock(device->cdev));
2011 /* Now check if the head of the ccw queue needs to be started. */
2012 __dasd_device_start_head(device);
2013 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2014 if (waitqueue_active(&shutdown_waitq))
2015 wake_up(&shutdown_waitq);
2016 dasd_put_device(device);
2020 * Schedules a call to dasd_tasklet over the device tasklet.
2022 void dasd_schedule_device_bh(struct dasd_device *device)
2024 /* Protect against rescheduling. */
2025 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2027 dasd_get_device(device);
2028 tasklet_hi_schedule(&device->tasklet);
2031 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2033 device->stopped |= bits;
2035 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2037 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2039 device->stopped &= ~bits;
2040 if (!device->stopped)
2041 wake_up(&generic_waitq);
2043 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2046 * Queue a request to the head of the device ccw_queue.
2047 * Start the I/O if possible.
2049 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2051 struct dasd_device *device;
2052 unsigned long flags;
2054 device = cqr->startdev;
2055 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2056 cqr->status = DASD_CQR_QUEUED;
2057 list_add(&cqr->devlist, &device->ccw_queue);
2058 /* let the bh start the request to keep them in order */
2059 dasd_schedule_device_bh(device);
2060 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2064 * Queue a request to the tail of the device ccw_queue.
2065 * Start the I/O if possible.
2067 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2069 struct dasd_device *device;
2070 unsigned long flags;
2072 device = cqr->startdev;
2073 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2074 cqr->status = DASD_CQR_QUEUED;
2075 list_add_tail(&cqr->devlist, &device->ccw_queue);
2076 /* let the bh start the request to keep them in order */
2077 dasd_schedule_device_bh(device);
2078 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2082 * Wakeup helper for the 'sleep_on' functions.
2084 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2086 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2087 cqr->callback_data = DASD_SLEEPON_END_TAG;
2088 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2089 wake_up(&generic_waitq);
2091 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2093 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2095 struct dasd_device *device;
2098 device = cqr->startdev;
2099 spin_lock_irq(get_ccwdev_lock(device->cdev));
2100 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2101 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2106 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2108 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2110 struct dasd_device *device;
2111 dasd_erp_fn_t erp_fn;
2113 if (cqr->status == DASD_CQR_FILLED)
2115 device = cqr->startdev;
2116 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2117 if (cqr->status == DASD_CQR_TERMINATED) {
2118 device->discipline->handle_terminated_request(cqr);
2121 if (cqr->status == DASD_CQR_NEED_ERP) {
2122 erp_fn = device->discipline->erp_action(cqr);
2126 if (cqr->status == DASD_CQR_FAILED)
2127 dasd_log_sense(cqr, &cqr->irb);
2129 __dasd_process_erp(device, cqr);
2136 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2138 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2139 if (cqr->refers) /* erp is not done yet */
2141 return ((cqr->status != DASD_CQR_DONE) &&
2142 (cqr->status != DASD_CQR_FAILED));
2144 return (cqr->status == DASD_CQR_FILLED);
2147 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2149 struct dasd_device *device;
2151 struct list_head ccw_queue;
2152 struct dasd_ccw_req *cqr;
2154 INIT_LIST_HEAD(&ccw_queue);
2155 maincqr->status = DASD_CQR_FILLED;
2156 device = maincqr->startdev;
2157 list_add(&maincqr->blocklist, &ccw_queue);
2158 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2159 cqr = list_first_entry(&ccw_queue,
2160 struct dasd_ccw_req, blocklist)) {
2162 if (__dasd_sleep_on_erp(cqr))
2164 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2166 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2167 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2168 cqr->status = DASD_CQR_FAILED;
2169 cqr->intrc = -EPERM;
2172 /* Non-temporary stop condition will trigger fail fast */
2173 if (device->stopped & ~DASD_STOPPED_PENDING &&
2174 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2175 (!dasd_eer_enabled(device))) {
2176 cqr->status = DASD_CQR_FAILED;
2177 cqr->intrc = -EAGAIN;
2180 /* Don't try to start requests if device is stopped */
2181 if (interruptible) {
2182 rc = wait_event_interruptible(
2183 generic_waitq, !(device->stopped));
2184 if (rc == -ERESTARTSYS) {
2185 cqr->status = DASD_CQR_FAILED;
2186 maincqr->intrc = rc;
2190 wait_event(generic_waitq, !(device->stopped));
2193 cqr->callback = dasd_wakeup_cb;
2195 cqr->callback_data = DASD_SLEEPON_START_TAG;
2196 dasd_add_request_tail(cqr);
2197 if (interruptible) {
2198 rc = wait_event_interruptible(
2199 generic_waitq, _wait_for_wakeup(cqr));
2200 if (rc == -ERESTARTSYS) {
2201 dasd_cancel_req(cqr);
2202 /* wait (non-interruptible) for final status */
2203 wait_event(generic_waitq,
2204 _wait_for_wakeup(cqr));
2205 cqr->status = DASD_CQR_FAILED;
2206 maincqr->intrc = rc;
2210 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2213 maincqr->endclk = get_tod_clock();
2214 if ((maincqr->status != DASD_CQR_DONE) &&
2215 (maincqr->intrc != -ERESTARTSYS))
2216 dasd_log_sense(maincqr, &maincqr->irb);
2217 if (maincqr->status == DASD_CQR_DONE)
2219 else if (maincqr->intrc)
2220 rc = maincqr->intrc;
2227 * Queue a request to the tail of the device ccw_queue and wait for
2230 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2232 return _dasd_sleep_on(cqr, 0);
2236 * Queue a request to the tail of the device ccw_queue and wait
2237 * interruptible for it's completion.
2239 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2241 return _dasd_sleep_on(cqr, 1);
2245 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2246 * for eckd devices) the currently running request has to be terminated
2247 * and be put back to status queued, before the special request is added
2248 * to the head of the queue. Then the special request is waited on normally.
2250 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2252 struct dasd_ccw_req *cqr;
2255 if (list_empty(&device->ccw_queue))
2257 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2258 rc = device->discipline->term_IO(cqr);
2261 * CQR terminated because a more important request is pending.
2262 * Undo decreasing of retry counter because this is
2263 * not an error case.
2269 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2271 struct dasd_device *device;
2274 device = cqr->startdev;
2275 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2276 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2277 cqr->status = DASD_CQR_FAILED;
2278 cqr->intrc = -EPERM;
2281 spin_lock_irq(get_ccwdev_lock(device->cdev));
2282 rc = _dasd_term_running_cqr(device);
2284 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2287 cqr->callback = dasd_wakeup_cb;
2288 cqr->callback_data = DASD_SLEEPON_START_TAG;
2289 cqr->status = DASD_CQR_QUEUED;
2291 * add new request as second
2292 * first the terminated cqr needs to be finished
2294 list_add(&cqr->devlist, device->ccw_queue.next);
2296 /* let the bh start the request to keep them in order */
2297 dasd_schedule_device_bh(device);
2299 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2301 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2303 if (cqr->status == DASD_CQR_DONE)
2305 else if (cqr->intrc)
2313 * Cancels a request that was started with dasd_sleep_on_req.
2314 * This is useful to timeout requests. The request will be
2315 * terminated if it is currently in i/o.
2316 * Returns 1 if the request has been terminated.
2317 * 0 if there was no need to terminate the request (not started yet)
2318 * negative error code if termination failed
2319 * Cancellation of a request is an asynchronous operation! The calling
2320 * function has to wait until the request is properly returned via callback.
2322 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2324 struct dasd_device *device = cqr->startdev;
2325 unsigned long flags;
2329 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2330 switch (cqr->status) {
2331 case DASD_CQR_QUEUED:
2332 /* request was not started - just set to cleared */
2333 cqr->status = DASD_CQR_CLEARED;
2335 case DASD_CQR_IN_IO:
2336 /* request in IO - terminate IO and release again */
2337 rc = device->discipline->term_IO(cqr);
2339 dev_err(&device->cdev->dev,
2340 "Cancelling request %p failed with rc=%d\n",
2343 cqr->stopclk = get_tod_clock();
2346 default: /* already finished or clear pending - do nothing */
2349 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2350 dasd_schedule_device_bh(device);
2356 * SECTION: Operations of the dasd_block layer.
2360 * Timeout function for dasd_block. This is used when the block layer
2361 * is waiting for something that may not come reliably, (e.g. a state
2364 static void dasd_block_timeout(unsigned long ptr)
2366 unsigned long flags;
2367 struct dasd_block *block;
2369 block = (struct dasd_block *) ptr;
2370 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2371 /* re-activate request queue */
2372 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2373 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2374 dasd_schedule_block_bh(block);
2378 * Setup timeout for a dasd_block in jiffies.
2380 void dasd_block_set_timer(struct dasd_block *block, int expires)
2383 del_timer(&block->timer);
2385 mod_timer(&block->timer, jiffies + expires);
2389 * Clear timeout for a dasd_block.
2391 void dasd_block_clear_timer(struct dasd_block *block)
2393 del_timer(&block->timer);
2397 * Process finished error recovery ccw.
2399 static void __dasd_process_erp(struct dasd_device *device,
2400 struct dasd_ccw_req *cqr)
2402 dasd_erp_fn_t erp_fn;
2404 if (cqr->status == DASD_CQR_DONE)
2405 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2407 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2408 erp_fn = device->discipline->erp_postaction(cqr);
2413 * Fetch requests from the block device queue.
2415 static void __dasd_process_request_queue(struct dasd_block *block)
2417 struct request_queue *queue;
2418 struct request *req;
2419 struct dasd_ccw_req *cqr;
2420 struct dasd_device *basedev;
2421 unsigned long flags;
2422 queue = block->request_queue;
2423 basedev = block->base;
2424 /* No queue ? Then there is nothing to do. */
2429 * We requeue request from the block device queue to the ccw
2430 * queue only in two states. In state DASD_STATE_READY the
2431 * partition detection is done and we need to requeue requests
2432 * for that. State DASD_STATE_ONLINE is normal block device
2435 if (basedev->state < DASD_STATE_READY) {
2436 while ((req = blk_fetch_request(block->request_queue)))
2437 __blk_end_request_all(req, -EIO);
2440 /* Now we try to fetch requests from the request queue */
2441 while ((req = blk_peek_request(queue))) {
2442 if (basedev->features & DASD_FEATURE_READONLY &&
2443 rq_data_dir(req) == WRITE) {
2444 DBF_DEV_EVENT(DBF_ERR, basedev,
2445 "Rejecting write request %p",
2447 blk_start_request(req);
2448 __blk_end_request_all(req, -EIO);
2451 cqr = basedev->discipline->build_cp(basedev, block, req);
2453 if (PTR_ERR(cqr) == -EBUSY)
2454 break; /* normal end condition */
2455 if (PTR_ERR(cqr) == -ENOMEM)
2456 break; /* terminate request queue loop */
2457 if (PTR_ERR(cqr) == -EAGAIN) {
2459 * The current request cannot be build right
2460 * now, we have to try later. If this request
2461 * is the head-of-queue we stop the device
2464 if (!list_empty(&block->ccw_queue))
2467 get_ccwdev_lock(basedev->cdev), flags);
2468 dasd_device_set_stop_bits(basedev,
2469 DASD_STOPPED_PENDING);
2470 spin_unlock_irqrestore(
2471 get_ccwdev_lock(basedev->cdev), flags);
2472 dasd_block_set_timer(block, HZ/2);
2475 DBF_DEV_EVENT(DBF_ERR, basedev,
2476 "CCW creation failed (rc=%ld) "
2479 blk_start_request(req);
2480 __blk_end_request_all(req, -EIO);
2484 * Note: callback is set to dasd_return_cqr_cb in
2485 * __dasd_block_start_head to cover erp requests as well
2487 cqr->callback_data = (void *) req;
2488 cqr->status = DASD_CQR_FILLED;
2489 blk_start_request(req);
2490 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2491 dasd_profile_start(block, cqr, req);
2495 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2497 struct request *req;
2501 req = (struct request *) cqr->callback_data;
2502 dasd_profile_end(cqr->block, cqr, req);
2503 status = cqr->block->base->discipline->free_cp(cqr, req);
2505 error = status ? status : -EIO;
2506 __blk_end_request_all(req, error);
2510 * Process ccw request queue.
2512 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2513 struct list_head *final_queue)
2515 struct list_head *l, *n;
2516 struct dasd_ccw_req *cqr;
2517 dasd_erp_fn_t erp_fn;
2518 unsigned long flags;
2519 struct dasd_device *base = block->base;
2522 /* Process request with final status. */
2523 list_for_each_safe(l, n, &block->ccw_queue) {
2524 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2525 if (cqr->status != DASD_CQR_DONE &&
2526 cqr->status != DASD_CQR_FAILED &&
2527 cqr->status != DASD_CQR_NEED_ERP &&
2528 cqr->status != DASD_CQR_TERMINATED)
2531 if (cqr->status == DASD_CQR_TERMINATED) {
2532 base->discipline->handle_terminated_request(cqr);
2536 /* Process requests that may be recovered */
2537 if (cqr->status == DASD_CQR_NEED_ERP) {
2538 erp_fn = base->discipline->erp_action(cqr);
2539 if (IS_ERR(erp_fn(cqr)))
2544 /* log sense for fatal error */
2545 if (cqr->status == DASD_CQR_FAILED) {
2546 dasd_log_sense(cqr, &cqr->irb);
2549 /* First of all call extended error reporting. */
2550 if (dasd_eer_enabled(base) &&
2551 cqr->status == DASD_CQR_FAILED) {
2552 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2554 /* restart request */
2555 cqr->status = DASD_CQR_FILLED;
2557 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2558 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2559 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2564 /* Process finished ERP request. */
2566 __dasd_process_erp(base, cqr);
2570 /* Rechain finished requests to final queue */
2571 cqr->endclk = get_tod_clock();
2572 list_move_tail(&cqr->blocklist, final_queue);
2576 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2578 dasd_schedule_block_bh(cqr->block);
2581 static void __dasd_block_start_head(struct dasd_block *block)
2583 struct dasd_ccw_req *cqr;
2585 if (list_empty(&block->ccw_queue))
2587 /* We allways begin with the first requests on the queue, as some
2588 * of previously started requests have to be enqueued on a
2589 * dasd_device again for error recovery.
2591 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2592 if (cqr->status != DASD_CQR_FILLED)
2594 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2595 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2596 cqr->status = DASD_CQR_FAILED;
2597 cqr->intrc = -EPERM;
2598 dasd_schedule_block_bh(block);
2601 /* Non-temporary stop condition will trigger fail fast */
2602 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2603 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2604 (!dasd_eer_enabled(block->base))) {
2605 cqr->status = DASD_CQR_FAILED;
2606 dasd_schedule_block_bh(block);
2609 /* Don't try to start requests if device is stopped */
2610 if (block->base->stopped)
2613 /* just a fail safe check, should not happen */
2615 cqr->startdev = block->base;
2617 /* make sure that the requests we submit find their way back */
2618 cqr->callback = dasd_return_cqr_cb;
2620 dasd_add_request_tail(cqr);
2625 * Central dasd_block layer routine. Takes requests from the generic
2626 * block layer request queue, creates ccw requests, enqueues them on
2627 * a dasd_device and processes ccw requests that have been returned.
2629 static void dasd_block_tasklet(struct dasd_block *block)
2631 struct list_head final_queue;
2632 struct list_head *l, *n;
2633 struct dasd_ccw_req *cqr;
2635 atomic_set(&block->tasklet_scheduled, 0);
2636 INIT_LIST_HEAD(&final_queue);
2637 spin_lock(&block->queue_lock);
2638 /* Finish off requests on ccw queue */
2639 __dasd_process_block_ccw_queue(block, &final_queue);
2640 spin_unlock(&block->queue_lock);
2641 /* Now call the callback function of requests with final status */
2642 spin_lock_irq(&block->request_queue_lock);
2643 list_for_each_safe(l, n, &final_queue) {
2644 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2645 list_del_init(&cqr->blocklist);
2646 __dasd_cleanup_cqr(cqr);
2648 spin_lock(&block->queue_lock);
2649 /* Get new request from the block device request queue */
2650 __dasd_process_request_queue(block);
2651 /* Now check if the head of the ccw queue needs to be started. */
2652 __dasd_block_start_head(block);
2653 spin_unlock(&block->queue_lock);
2654 spin_unlock_irq(&block->request_queue_lock);
2655 if (waitqueue_active(&shutdown_waitq))
2656 wake_up(&shutdown_waitq);
2657 dasd_put_device(block->base);
2660 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2662 wake_up(&dasd_flush_wq);
2666 * Go through all request on the dasd_block request queue, cancel them
2667 * on the respective dasd_device, and return them to the generic
2670 static int dasd_flush_block_queue(struct dasd_block *block)
2672 struct dasd_ccw_req *cqr, *n;
2674 struct list_head flush_queue;
2676 INIT_LIST_HEAD(&flush_queue);
2677 spin_lock_bh(&block->queue_lock);
2680 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2681 /* if this request currently owned by a dasd_device cancel it */
2682 if (cqr->status >= DASD_CQR_QUEUED)
2683 rc = dasd_cancel_req(cqr);
2686 /* Rechain request (including erp chain) so it won't be
2687 * touched by the dasd_block_tasklet anymore.
2688 * Replace the callback so we notice when the request
2689 * is returned from the dasd_device layer.
2691 cqr->callback = _dasd_wake_block_flush_cb;
2692 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2693 list_move_tail(&cqr->blocklist, &flush_queue);
2695 /* moved more than one request - need to restart */
2698 spin_unlock_bh(&block->queue_lock);
2699 /* Now call the callback function of flushed requests */
2701 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2702 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2703 /* Process finished ERP request. */
2705 spin_lock_bh(&block->queue_lock);
2706 __dasd_process_erp(block->base, cqr);
2707 spin_unlock_bh(&block->queue_lock);
2708 /* restart list_for_xx loop since dasd_process_erp
2709 * might remove multiple elements */
2712 /* call the callback function */
2713 spin_lock_irq(&block->request_queue_lock);
2714 cqr->endclk = get_tod_clock();
2715 list_del_init(&cqr->blocklist);
2716 __dasd_cleanup_cqr(cqr);
2717 spin_unlock_irq(&block->request_queue_lock);
2723 * Schedules a call to dasd_tasklet over the device tasklet.
2725 void dasd_schedule_block_bh(struct dasd_block *block)
2727 /* Protect against rescheduling. */
2728 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2730 /* life cycle of block is bound to it's base device */
2731 dasd_get_device(block->base);
2732 tasklet_hi_schedule(&block->tasklet);
2737 * SECTION: external block device operations
2738 * (request queue handling, open, release, etc.)
2742 * Dasd request queue function. Called from ll_rw_blk.c
2744 static void do_dasd_request(struct request_queue *queue)
2746 struct dasd_block *block;
2748 block = queue->queuedata;
2749 spin_lock(&block->queue_lock);
2750 /* Get new request from the block device request queue */
2751 __dasd_process_request_queue(block);
2752 /* Now check if the head of the ccw queue needs to be started. */
2753 __dasd_block_start_head(block);
2754 spin_unlock(&block->queue_lock);
2758 * Allocate and initialize request queue and default I/O scheduler.
2760 static int dasd_alloc_queue(struct dasd_block *block)
2764 block->request_queue = blk_init_queue(do_dasd_request,
2765 &block->request_queue_lock);
2766 if (block->request_queue == NULL)
2769 block->request_queue->queuedata = block;
2771 elevator_exit(block->request_queue->elevator);
2772 block->request_queue->elevator = NULL;
2773 rc = elevator_init(block->request_queue, "deadline");
2775 blk_cleanup_queue(block->request_queue);
2782 * Allocate and initialize request queue.
2784 static void dasd_setup_queue(struct dasd_block *block)
2788 if (block->base->features & DASD_FEATURE_USERAW) {
2790 * the max_blocks value for raw_track access is 256
2791 * it is higher than the native ECKD value because we
2792 * only need one ccw per track
2793 * so the max_hw_sectors are
2794 * 2048 x 512B = 1024kB = 16 tracks
2798 max = block->base->discipline->max_blocks << block->s2b_shift;
2800 blk_queue_logical_block_size(block->request_queue,
2802 blk_queue_max_hw_sectors(block->request_queue, max);
2803 blk_queue_max_segments(block->request_queue, -1L);
2804 /* with page sized segments we can translate each segement into
2807 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2808 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2812 * Deactivate and free request queue.
2814 static void dasd_free_queue(struct dasd_block *block)
2816 if (block->request_queue) {
2817 blk_cleanup_queue(block->request_queue);
2818 block->request_queue = NULL;
2823 * Flush request on the request queue.
2825 static void dasd_flush_request_queue(struct dasd_block *block)
2827 struct request *req;
2829 if (!block->request_queue)
2832 spin_lock_irq(&block->request_queue_lock);
2833 while ((req = blk_fetch_request(block->request_queue)))
2834 __blk_end_request_all(req, -EIO);
2835 spin_unlock_irq(&block->request_queue_lock);
2838 static int dasd_open(struct block_device *bdev, fmode_t mode)
2840 struct dasd_device *base;
2843 base = dasd_device_from_gendisk(bdev->bd_disk);
2847 atomic_inc(&base->block->open_count);
2848 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2853 if (!try_module_get(base->discipline->owner)) {
2858 if (dasd_probeonly) {
2859 dev_info(&base->cdev->dev,
2860 "Accessing the DASD failed because it is in "
2861 "probeonly mode\n");
2866 if (base->state <= DASD_STATE_BASIC) {
2867 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2868 " Cannot open unrecognized device");
2873 if ((mode & FMODE_WRITE) &&
2874 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2875 (base->features & DASD_FEATURE_READONLY))) {
2880 dasd_put_device(base);
2884 module_put(base->discipline->owner);
2886 atomic_dec(&base->block->open_count);
2887 dasd_put_device(base);
2891 static int dasd_release(struct gendisk *disk, fmode_t mode)
2893 struct dasd_device *base;
2895 base = dasd_device_from_gendisk(disk);
2899 atomic_dec(&base->block->open_count);
2900 module_put(base->discipline->owner);
2901 dasd_put_device(base);
2906 * Return disk geometry.
2908 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2910 struct dasd_device *base;
2912 base = dasd_device_from_gendisk(bdev->bd_disk);
2916 if (!base->discipline ||
2917 !base->discipline->fill_geometry) {
2918 dasd_put_device(base);
2921 base->discipline->fill_geometry(base->block, geo);
2922 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
2923 dasd_put_device(base);
2927 const struct block_device_operations
2928 dasd_device_operations = {
2929 .owner = THIS_MODULE,
2931 .release = dasd_release,
2932 .ioctl = dasd_ioctl,
2933 .compat_ioctl = dasd_ioctl,
2934 .getgeo = dasd_getgeo,
2937 /*******************************************************************************
2938 * end of block device operations
2944 #ifdef CONFIG_PROC_FS
2948 if (dasd_page_cache != NULL) {
2949 kmem_cache_destroy(dasd_page_cache);
2950 dasd_page_cache = NULL;
2952 dasd_gendisk_exit();
2954 if (dasd_debug_area != NULL) {
2955 debug_unregister(dasd_debug_area);
2956 dasd_debug_area = NULL;
2958 dasd_statistics_removeroot();
2962 * SECTION: common functions for ccw_driver use
2966 * Is the device read-only?
2967 * Note that this function does not report the setting of the
2968 * readonly device attribute, but how it is configured in z/VM.
2970 int dasd_device_is_ro(struct dasd_device *device)
2972 struct ccw_dev_id dev_id;
2973 struct diag210 diag_data;
2978 ccw_device_get_id(device->cdev, &dev_id);
2979 memset(&diag_data, 0, sizeof(diag_data));
2980 diag_data.vrdcdvno = dev_id.devno;
2981 diag_data.vrdclen = sizeof(diag_data);
2982 rc = diag210(&diag_data);
2983 if (rc == 0 || rc == 2) {
2984 return diag_data.vrdcvfla & 0x80;
2986 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2991 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2993 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2995 struct ccw_device *cdev = data;
2998 ret = ccw_device_set_online(cdev);
3000 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3001 dev_name(&cdev->dev), ret);
3005 * Initial attempt at a probe function. this can be simplified once
3006 * the other detection code is gone.
3008 int dasd_generic_probe(struct ccw_device *cdev,
3009 struct dasd_discipline *discipline)
3013 ret = dasd_add_sysfs_files(cdev);
3015 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3016 "dasd_generic_probe: could not add "
3020 cdev->handler = &dasd_int_handler;
3023 * Automatically online either all dasd devices (dasd_autodetect)
3024 * or all devices specified with dasd= parameters during
3027 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3028 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3029 async_schedule(dasd_generic_auto_online, cdev);
3034 * This will one day be called from a global not_oper handler.
3035 * It is also used by driver_unregister during module unload.
3037 void dasd_generic_remove(struct ccw_device *cdev)
3039 struct dasd_device *device;
3040 struct dasd_block *block;
3042 cdev->handler = NULL;
3044 device = dasd_device_from_cdev(cdev);
3045 if (IS_ERR(device)) {
3046 dasd_remove_sysfs_files(cdev);
3049 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3050 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3051 /* Already doing offline processing */
3052 dasd_put_device(device);
3053 dasd_remove_sysfs_files(cdev);
3057 * This device is removed unconditionally. Set offline
3058 * flag to prevent dasd_open from opening it while it is
3059 * no quite down yet.
3061 dasd_set_target_state(device, DASD_STATE_NEW);
3062 /* dasd_delete_device destroys the device reference. */
3063 block = device->block;
3064 dasd_delete_device(device);
3066 * life cycle of block is bound to device, so delete it after
3067 * device was safely removed
3070 dasd_free_block(block);
3072 dasd_remove_sysfs_files(cdev);
3076 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3077 * the device is detected for the first time and is supposed to be used
3078 * or the user has started activation through sysfs.
3080 int dasd_generic_set_online(struct ccw_device *cdev,
3081 struct dasd_discipline *base_discipline)
3083 struct dasd_discipline *discipline;
3084 struct dasd_device *device;
3087 /* first online clears initial online feature flag */
3088 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3089 device = dasd_create_device(cdev);
3091 return PTR_ERR(device);
3093 discipline = base_discipline;
3094 if (device->features & DASD_FEATURE_USEDIAG) {
3095 if (!dasd_diag_discipline_pointer) {
3096 pr_warning("%s Setting the DASD online failed because "
3097 "of missing DIAG discipline\n",
3098 dev_name(&cdev->dev));
3099 dasd_delete_device(device);
3102 discipline = dasd_diag_discipline_pointer;
3104 if (!try_module_get(base_discipline->owner)) {
3105 dasd_delete_device(device);
3108 if (!try_module_get(discipline->owner)) {
3109 module_put(base_discipline->owner);
3110 dasd_delete_device(device);
3113 device->base_discipline = base_discipline;
3114 device->discipline = discipline;
3116 /* check_device will allocate block device if necessary */
3117 rc = discipline->check_device(device);
3119 pr_warning("%s Setting the DASD online with discipline %s "
3120 "failed with rc=%i\n",
3121 dev_name(&cdev->dev), discipline->name, rc);
3122 module_put(discipline->owner);
3123 module_put(base_discipline->owner);
3124 dasd_delete_device(device);
3128 dasd_set_target_state(device, DASD_STATE_ONLINE);
3129 if (device->state <= DASD_STATE_KNOWN) {
3130 pr_warning("%s Setting the DASD online failed because of a "
3131 "missing discipline\n", dev_name(&cdev->dev));
3133 dasd_set_target_state(device, DASD_STATE_NEW);
3135 dasd_free_block(device->block);
3136 dasd_delete_device(device);
3138 pr_debug("dasd_generic device %s found\n",
3139 dev_name(&cdev->dev));
3141 wait_event(dasd_init_waitq, _wait_for_device(device));
3143 dasd_put_device(device);
3147 int dasd_generic_set_offline(struct ccw_device *cdev)
3149 struct dasd_device *device;
3150 struct dasd_block *block;
3151 int max_count, open_count, rc;
3154 device = dasd_device_from_cdev(cdev);
3156 return PTR_ERR(device);
3159 * We must make sure that this device is currently not in use.
3160 * The open_count is increased for every opener, that includes
3161 * the blkdev_get in dasd_scan_partitions. We are only interested
3162 * in the other openers.
3164 if (device->block) {
3165 max_count = device->block->bdev ? 0 : -1;
3166 open_count = atomic_read(&device->block->open_count);
3167 if (open_count > max_count) {
3169 pr_warning("%s: The DASD cannot be set offline "
3170 "with open count %i\n",
3171 dev_name(&cdev->dev), open_count);
3173 pr_warning("%s: The DASD cannot be set offline "
3174 "while it is in use\n",
3175 dev_name(&cdev->dev));
3176 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3177 dasd_put_device(device);
3182 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3184 * safe offline allready running
3185 * could only be called by normal offline so safe_offline flag
3186 * needs to be removed to run normal offline and kill all I/O
3188 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3189 /* Already doing normal offline processing */
3190 dasd_put_device(device);
3193 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3196 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3197 /* Already doing offline processing */
3198 dasd_put_device(device);
3203 * if safe_offline called set safe_offline_running flag and
3204 * clear safe_offline so that a call to normal offline
3205 * can overrun safe_offline processing
3207 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3208 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3210 * If we want to set the device safe offline all IO operations
3211 * should be finished before continuing the offline process
3212 * so sync bdev first and then wait for our queues to become
3215 /* sync blockdev and partitions */
3216 rc = fsync_bdev(device->block->bdev);
3220 /* schedule device tasklet and wait for completion */
3221 dasd_schedule_device_bh(device);
3222 rc = wait_event_interruptible(shutdown_waitq,
3223 _wait_for_empty_queues(device));
3228 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3229 dasd_set_target_state(device, DASD_STATE_NEW);
3230 /* dasd_delete_device destroys the device reference. */
3231 block = device->block;
3232 dasd_delete_device(device);
3234 * life cycle of block is bound to device, so delete it after
3235 * device was safely removed
3238 dasd_free_block(block);
3242 /* interrupted by signal */
3243 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3244 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3245 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3246 dasd_put_device(device);
3250 int dasd_generic_last_path_gone(struct dasd_device *device)
3252 struct dasd_ccw_req *cqr;
3254 dev_warn(&device->cdev->dev, "No operational channel path is left "
3255 "for the device\n");
3256 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3257 /* First of all call extended error reporting. */
3258 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3260 if (device->state < DASD_STATE_BASIC)
3262 /* Device is active. We want to keep it. */
3263 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3264 if ((cqr->status == DASD_CQR_IN_IO) ||
3265 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3266 cqr->status = DASD_CQR_QUEUED;
3269 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3270 dasd_device_clear_timer(device);
3271 dasd_schedule_device_bh(device);
3274 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3276 int dasd_generic_path_operational(struct dasd_device *device)
3278 dev_info(&device->cdev->dev, "A channel path to the device has become "
3280 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3281 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3282 if (device->stopped & DASD_UNRESUMED_PM) {
3283 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3284 dasd_restore_device(device);
3287 dasd_schedule_device_bh(device);
3289 dasd_schedule_block_bh(device->block);
3292 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3294 int dasd_generic_notify(struct ccw_device *cdev, int event)
3296 struct dasd_device *device;
3299 device = dasd_device_from_cdev_locked(cdev);
3307 device->path_data.opm = 0;
3308 device->path_data.ppm = 0;
3309 device->path_data.npm = 0;
3310 ret = dasd_generic_last_path_gone(device);
3314 if (device->path_data.opm)
3315 ret = dasd_generic_path_operational(device);
3318 dasd_put_device(device);
3322 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3325 __u8 oldopm, eventlpm;
3326 struct dasd_device *device;
3328 device = dasd_device_from_cdev_locked(cdev);
3331 for (chp = 0; chp < 8; chp++) {
3332 eventlpm = 0x80 >> chp;
3333 if (path_event[chp] & PE_PATH_GONE) {
3334 oldopm = device->path_data.opm;
3335 device->path_data.opm &= ~eventlpm;
3336 device->path_data.ppm &= ~eventlpm;
3337 device->path_data.npm &= ~eventlpm;
3338 if (oldopm && !device->path_data.opm)
3339 dasd_generic_last_path_gone(device);
3341 if (path_event[chp] & PE_PATH_AVAILABLE) {
3342 device->path_data.opm &= ~eventlpm;
3343 device->path_data.ppm &= ~eventlpm;
3344 device->path_data.npm &= ~eventlpm;
3345 device->path_data.tbvpm |= eventlpm;
3346 dasd_schedule_device_bh(device);
3348 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3349 if (!(device->path_data.opm & eventlpm) &&
3350 !(device->path_data.tbvpm & eventlpm)) {
3352 * we can not establish a pathgroup on an
3353 * unavailable path, so trigger a path
3354 * verification first
3356 device->path_data.tbvpm |= eventlpm;
3357 dasd_schedule_device_bh(device);
3359 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3360 "Pathgroup re-established\n");
3361 if (device->discipline->kick_validate)
3362 device->discipline->kick_validate(device);
3365 dasd_put_device(device);
3367 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3369 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3371 if (!device->path_data.opm && lpm) {
3372 device->path_data.opm = lpm;
3373 dasd_generic_path_operational(device);
3375 device->path_data.opm |= lpm;
3378 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3381 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3383 struct dasd_ccw_req *cqr, *n;
3385 struct list_head freeze_queue;
3386 struct dasd_device *device = dasd_device_from_cdev(cdev);
3389 return PTR_ERR(device);
3391 /* mark device as suspended */
3392 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3394 if (device->discipline->freeze)
3395 rc = device->discipline->freeze(device);
3397 /* disallow new I/O */
3398 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3399 /* clear active requests */
3400 INIT_LIST_HEAD(&freeze_queue);
3401 spin_lock_irq(get_ccwdev_lock(cdev));
3403 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3404 /* Check status and move request to flush_queue */
3405 if (cqr->status == DASD_CQR_IN_IO) {
3406 rc = device->discipline->term_IO(cqr);
3408 /* unable to terminate requeust */
3409 dev_err(&device->cdev->dev,
3410 "Unable to terminate request %p "
3411 "on suspend\n", cqr);
3412 spin_unlock_irq(get_ccwdev_lock(cdev));
3413 dasd_put_device(device);
3417 list_move_tail(&cqr->devlist, &freeze_queue);
3420 spin_unlock_irq(get_ccwdev_lock(cdev));
3422 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3423 wait_event(dasd_flush_wq,
3424 (cqr->status != DASD_CQR_CLEAR_PENDING));
3425 if (cqr->status == DASD_CQR_CLEARED)
3426 cqr->status = DASD_CQR_QUEUED;
3428 /* move freeze_queue to start of the ccw_queue */
3429 spin_lock_irq(get_ccwdev_lock(cdev));
3430 list_splice_tail(&freeze_queue, &device->ccw_queue);
3431 spin_unlock_irq(get_ccwdev_lock(cdev));
3433 dasd_put_device(device);
3436 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3438 int dasd_generic_restore_device(struct ccw_device *cdev)
3440 struct dasd_device *device = dasd_device_from_cdev(cdev);
3444 return PTR_ERR(device);
3446 /* allow new IO again */
3447 dasd_device_remove_stop_bits(device,
3448 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3450 dasd_schedule_device_bh(device);
3453 * call discipline restore function
3454 * if device is stopped do nothing e.g. for disconnected devices
3456 if (device->discipline->restore && !(device->stopped))
3457 rc = device->discipline->restore(device);
3458 if (rc || device->stopped)
3460 * if the resume failed for the DASD we put it in
3461 * an UNRESUMED stop state
3463 device->stopped |= DASD_UNRESUMED_PM;
3466 dasd_schedule_block_bh(device->block);
3468 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3469 dasd_put_device(device);
3472 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3474 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3476 int rdc_buffer_size,
3479 struct dasd_ccw_req *cqr;
3481 unsigned long *idaw;
3483 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3486 /* internal error 13 - Allocating the RDC request failed*/
3487 dev_err(&device->cdev->dev,
3488 "An error occurred in the DASD device driver, "
3489 "reason=%s\n", "13");
3494 ccw->cmd_code = CCW_CMD_RDC;
3495 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3496 idaw = (unsigned long *) (cqr->data);
3497 ccw->cda = (__u32)(addr_t) idaw;
3498 ccw->flags = CCW_FLAG_IDA;
3499 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3501 ccw->cda = (__u32)(addr_t) rdc_buffer;
3505 ccw->count = rdc_buffer_size;
3506 cqr->startdev = device;
3507 cqr->memdev = device;
3508 cqr->expires = 10*HZ;
3510 cqr->buildclk = get_tod_clock();
3511 cqr->status = DASD_CQR_FILLED;
3516 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3517 void *rdc_buffer, int rdc_buffer_size)
3520 struct dasd_ccw_req *cqr;
3522 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3525 return PTR_ERR(cqr);
3527 ret = dasd_sleep_on(cqr);
3528 dasd_sfree_request(cqr, cqr->memdev);
3531 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3534 * In command mode and transport mode we need to look for sense
3535 * data in different places. The sense data itself is allways
3536 * an array of 32 bytes, so we can unify the sense data access
3539 char *dasd_get_sense(struct irb *irb)
3541 struct tsb *tsb = NULL;
3544 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3545 if (irb->scsw.tm.tcw)
3546 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3548 if (tsb && tsb->length == 64 && tsb->flags)
3549 switch (tsb->flags & 0x07) {
3550 case 1: /* tsa_iostat */
3551 sense = tsb->tsa.iostat.sense;
3553 case 2: /* tsa_ddpc */
3554 sense = tsb->tsa.ddpc.sense;
3557 /* currently we don't use interrogate data */
3560 } else if (irb->esw.esw0.erw.cons) {
3565 EXPORT_SYMBOL_GPL(dasd_get_sense);
3567 void dasd_generic_shutdown(struct ccw_device *cdev)
3569 struct dasd_device *device;
3571 device = dasd_device_from_cdev(cdev);
3576 dasd_schedule_block_bh(device->block);
3578 dasd_schedule_device_bh(device);
3580 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3582 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3584 static int __init dasd_init(void)
3588 init_waitqueue_head(&dasd_init_waitq);
3589 init_waitqueue_head(&dasd_flush_wq);
3590 init_waitqueue_head(&generic_waitq);
3591 init_waitqueue_head(&shutdown_waitq);
3593 /* register 'common' DASD debug area, used for all DBF_XXX calls */
3594 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3595 if (dasd_debug_area == NULL) {
3599 debug_register_view(dasd_debug_area, &debug_sprintf_view);
3600 debug_set_level(dasd_debug_area, DBF_WARNING);
3602 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3604 dasd_diag_discipline_pointer = NULL;
3606 dasd_statistics_createroot();
3608 rc = dasd_devmap_init();
3611 rc = dasd_gendisk_init();
3617 rc = dasd_eer_init();
3620 #ifdef CONFIG_PROC_FS
3621 rc = dasd_proc_init();
3628 pr_info("The DASD device driver could not be initialized\n");
3633 module_init(dasd_init);
3634 module_exit(dasd_exit);
3636 EXPORT_SYMBOL(dasd_debug_area);
3637 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3639 EXPORT_SYMBOL(dasd_add_request_head);
3640 EXPORT_SYMBOL(dasd_add_request_tail);
3641 EXPORT_SYMBOL(dasd_cancel_req);
3642 EXPORT_SYMBOL(dasd_device_clear_timer);
3643 EXPORT_SYMBOL(dasd_block_clear_timer);
3644 EXPORT_SYMBOL(dasd_enable_device);
3645 EXPORT_SYMBOL(dasd_int_handler);
3646 EXPORT_SYMBOL(dasd_kfree_request);
3647 EXPORT_SYMBOL(dasd_kick_device);
3648 EXPORT_SYMBOL(dasd_kmalloc_request);
3649 EXPORT_SYMBOL(dasd_schedule_device_bh);
3650 EXPORT_SYMBOL(dasd_schedule_block_bh);
3651 EXPORT_SYMBOL(dasd_set_target_state);
3652 EXPORT_SYMBOL(dasd_device_set_timer);
3653 EXPORT_SYMBOL(dasd_block_set_timer);
3654 EXPORT_SYMBOL(dasd_sfree_request);
3655 EXPORT_SYMBOL(dasd_sleep_on);
3656 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3657 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3658 EXPORT_SYMBOL(dasd_smalloc_request);
3659 EXPORT_SYMBOL(dasd_start_IO);
3660 EXPORT_SYMBOL(dasd_term_IO);
3662 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3663 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3664 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3665 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3666 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3667 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3668 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3669 EXPORT_SYMBOL_GPL(dasd_alloc_block);
3670 EXPORT_SYMBOL_GPL(dasd_free_block);