mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / s390 / block / dasd.c
blobdf2fd363734eca01bfb7823aef567ebba2300489
1 /*
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
8 */
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>
29 #include <asm/itcw.h>
30 #include <asm/diag.h>
32 /* This is ugly... */
33 #define PRINTK_HEADER "dasd:"
35 #include "dasd_int.h"
37 * SECTION: Constant definitions to be used within this file
39 #define DASD_CHANQ_MAX_SIZE 4
42 * SECTION: exported variables of dasd.c
44 debug_info_t *dasd_debug_area;
45 static struct dentry *dasd_debugfs_root_entry;
46 struct dasd_discipline *dasd_diag_discipline_pointer;
47 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
49 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
50 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
51 " Copyright IBM Corp. 2000");
52 MODULE_SUPPORTED_DEVICE("dasd");
53 MODULE_LICENSE("GPL");
56 * SECTION: prototypes for static functions of dasd.c
58 static int dasd_alloc_queue(struct dasd_block *);
59 static void dasd_setup_queue(struct dasd_block *);
60 static void dasd_free_queue(struct dasd_block *);
61 static void dasd_flush_request_queue(struct dasd_block *);
62 static int dasd_flush_block_queue(struct dasd_block *);
63 static void dasd_device_tasklet(struct dasd_device *);
64 static void dasd_block_tasklet(struct dasd_block *);
65 static void do_kick_device(struct work_struct *);
66 static void do_restore_device(struct work_struct *);
67 static void do_reload_device(struct work_struct *);
68 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
69 static void dasd_device_timeout(unsigned long);
70 static void dasd_block_timeout(unsigned long);
71 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
72 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
73 static void dasd_profile_exit(struct dasd_profile *);
76 * SECTION: Operations on the device structure.
78 static wait_queue_head_t dasd_init_waitq;
79 static wait_queue_head_t dasd_flush_wq;
80 static wait_queue_head_t generic_waitq;
81 static wait_queue_head_t shutdown_waitq;
84 * Allocate memory for a new device structure.
86 struct dasd_device *dasd_alloc_device(void)
88 struct dasd_device *device;
90 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
91 if (!device)
92 return ERR_PTR(-ENOMEM);
94 /* Get two pages for normal block device operations. */
95 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
96 if (!device->ccw_mem) {
97 kfree(device);
98 return ERR_PTR(-ENOMEM);
100 /* Get one page for error recovery. */
101 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
102 if (!device->erp_mem) {
103 free_pages((unsigned long) device->ccw_mem, 1);
104 kfree(device);
105 return ERR_PTR(-ENOMEM);
108 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
109 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
110 spin_lock_init(&device->mem_lock);
111 atomic_set(&device->tasklet_scheduled, 0);
112 tasklet_init(&device->tasklet,
113 (void (*)(unsigned long)) dasd_device_tasklet,
114 (unsigned long) device);
115 INIT_LIST_HEAD(&device->ccw_queue);
116 init_timer(&device->timer);
117 device->timer.function = dasd_device_timeout;
118 device->timer.data = (unsigned long) device;
119 INIT_WORK(&device->kick_work, do_kick_device);
120 INIT_WORK(&device->restore_device, do_restore_device);
121 INIT_WORK(&device->reload_device, do_reload_device);
122 device->state = DASD_STATE_NEW;
123 device->target = DASD_STATE_NEW;
124 mutex_init(&device->state_mutex);
125 spin_lock_init(&device->profile.lock);
126 return device;
130 * Free memory of a device structure.
132 void dasd_free_device(struct dasd_device *device)
134 kfree(device->private);
135 free_page((unsigned long) device->erp_mem);
136 free_pages((unsigned long) device->ccw_mem, 1);
137 kfree(device);
141 * Allocate memory for a new device structure.
143 struct dasd_block *dasd_alloc_block(void)
145 struct dasd_block *block;
147 block = kzalloc(sizeof(*block), GFP_ATOMIC);
148 if (!block)
149 return ERR_PTR(-ENOMEM);
150 /* open_count = 0 means device online but not in use */
151 atomic_set(&block->open_count, -1);
153 spin_lock_init(&block->request_queue_lock);
154 atomic_set(&block->tasklet_scheduled, 0);
155 tasklet_init(&block->tasklet,
156 (void (*)(unsigned long)) dasd_block_tasklet,
157 (unsigned long) block);
158 INIT_LIST_HEAD(&block->ccw_queue);
159 spin_lock_init(&block->queue_lock);
160 init_timer(&block->timer);
161 block->timer.function = dasd_block_timeout;
162 block->timer.data = (unsigned long) block;
163 spin_lock_init(&block->profile.lock);
165 return block;
169 * Free memory of a device structure.
171 void dasd_free_block(struct dasd_block *block)
173 kfree(block);
177 * Make a new device known to the system.
179 static int dasd_state_new_to_known(struct dasd_device *device)
181 int rc;
184 * As long as the device is not in state DASD_STATE_NEW we want to
185 * keep the reference count > 0.
187 dasd_get_device(device);
189 if (device->block) {
190 rc = dasd_alloc_queue(device->block);
191 if (rc) {
192 dasd_put_device(device);
193 return rc;
196 device->state = DASD_STATE_KNOWN;
197 return 0;
201 * Let the system forget about a device.
203 static int dasd_state_known_to_new(struct dasd_device *device)
205 /* Disable extended error reporting for this device. */
206 dasd_eer_disable(device);
207 /* Forget the discipline information. */
208 if (device->discipline) {
209 if (device->discipline->uncheck_device)
210 device->discipline->uncheck_device(device);
211 module_put(device->discipline->owner);
213 device->discipline = NULL;
214 if (device->base_discipline)
215 module_put(device->base_discipline->owner);
216 device->base_discipline = NULL;
217 device->state = DASD_STATE_NEW;
219 if (device->block)
220 dasd_free_queue(device->block);
222 /* Give up reference we took in dasd_state_new_to_known. */
223 dasd_put_device(device);
224 return 0;
227 static struct dentry *dasd_debugfs_setup(const char *name,
228 struct dentry *base_dentry)
230 struct dentry *pde;
232 if (!base_dentry)
233 return NULL;
234 pde = debugfs_create_dir(name, base_dentry);
235 if (!pde || IS_ERR(pde))
236 return NULL;
237 return pde;
241 * Request the irq line for the device.
243 static int dasd_state_known_to_basic(struct dasd_device *device)
245 struct dasd_block *block = device->block;
246 int rc = 0;
248 /* Allocate and register gendisk structure. */
249 if (block) {
250 rc = dasd_gendisk_alloc(block);
251 if (rc)
252 return rc;
253 block->debugfs_dentry =
254 dasd_debugfs_setup(block->gdp->disk_name,
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&block->profile, block->debugfs_dentry);
257 if (dasd_global_profile_level == DASD_PROFILE_ON)
258 dasd_profile_on(&device->block->profile);
260 device->debugfs_dentry =
261 dasd_debugfs_setup(dev_name(&device->cdev->dev),
262 dasd_debugfs_root_entry);
263 dasd_profile_init(&device->profile, device->debugfs_dentry);
265 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
266 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
267 8 * sizeof(long));
268 debug_register_view(device->debug_area, &debug_sprintf_view);
269 debug_set_level(device->debug_area, DBF_WARNING);
270 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
272 device->state = DASD_STATE_BASIC;
274 return rc;
278 * Release the irq line for the device. Terminate any running i/o.
280 static int dasd_state_basic_to_known(struct dasd_device *device)
282 int rc;
284 if (device->block) {
285 dasd_profile_exit(&device->block->profile);
286 if (device->block->debugfs_dentry)
287 debugfs_remove(device->block->debugfs_dentry);
288 dasd_gendisk_free(device->block);
289 dasd_block_clear_timer(device->block);
291 rc = dasd_flush_device_queue(device);
292 if (rc)
293 return rc;
294 dasd_device_clear_timer(device);
295 dasd_profile_exit(&device->profile);
296 if (device->debugfs_dentry)
297 debugfs_remove(device->debugfs_dentry);
299 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
300 if (device->debug_area != NULL) {
301 debug_unregister(device->debug_area);
302 device->debug_area = NULL;
304 device->state = DASD_STATE_KNOWN;
305 return 0;
309 * Do the initial analysis. The do_analysis function may return
310 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
311 * until the discipline decides to continue the startup sequence
312 * by calling the function dasd_change_state. The eckd disciplines
313 * uses this to start a ccw that detects the format. The completion
314 * interrupt for this detection ccw uses the kernel event daemon to
315 * trigger the call to dasd_change_state. All this is done in the
316 * discipline code, see dasd_eckd.c.
317 * After the analysis ccw is done (do_analysis returned 0) the block
318 * device is setup.
319 * In case the analysis returns an error, the device setup is stopped
320 * (a fake disk was already added to allow formatting).
322 static int dasd_state_basic_to_ready(struct dasd_device *device)
324 int rc;
325 struct dasd_block *block;
327 rc = 0;
328 block = device->block;
329 /* make disk known with correct capacity */
330 if (block) {
331 if (block->base->discipline->do_analysis != NULL)
332 rc = block->base->discipline->do_analysis(block);
333 if (rc) {
334 if (rc != -EAGAIN) {
335 device->state = DASD_STATE_UNFMT;
336 goto out;
338 return rc;
340 dasd_setup_queue(block);
341 set_capacity(block->gdp,
342 block->blocks << block->s2b_shift);
343 device->state = DASD_STATE_READY;
344 rc = dasd_scan_partitions(block);
345 if (rc) {
346 device->state = DASD_STATE_BASIC;
347 return rc;
349 } else {
350 device->state = DASD_STATE_READY;
352 out:
353 if (device->discipline->basic_to_ready)
354 rc = device->discipline->basic_to_ready(device);
355 return rc;
358 static inline
359 int _wait_for_empty_queues(struct dasd_device *device)
361 if (device->block)
362 return list_empty(&device->ccw_queue) &&
363 list_empty(&device->block->ccw_queue);
364 else
365 return list_empty(&device->ccw_queue);
369 * Remove device from block device layer. Destroy dirty buffers.
370 * Forget format information. Check if the target level is basic
371 * and if it is create fake disk for formatting.
373 static int dasd_state_ready_to_basic(struct dasd_device *device)
375 int rc;
377 if (device->discipline->ready_to_basic) {
378 rc = device->discipline->ready_to_basic(device);
379 if (rc)
380 return rc;
382 device->state = DASD_STATE_BASIC;
383 if (device->block) {
384 struct dasd_block *block = device->block;
385 rc = dasd_flush_block_queue(block);
386 if (rc) {
387 device->state = DASD_STATE_READY;
388 return rc;
390 dasd_flush_request_queue(block);
391 dasd_destroy_partitions(block);
392 block->blocks = 0;
393 block->bp_block = 0;
394 block->s2b_shift = 0;
396 return 0;
400 * Back to basic.
402 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
404 device->state = DASD_STATE_BASIC;
405 return 0;
409 * Make the device online and schedule the bottom half to start
410 * the requeueing of requests from the linux request queue to the
411 * ccw queue.
413 static int
414 dasd_state_ready_to_online(struct dasd_device * device)
416 struct gendisk *disk;
417 struct disk_part_iter piter;
418 struct hd_struct *part;
420 device->state = DASD_STATE_ONLINE;
421 if (device->block) {
422 dasd_schedule_block_bh(device->block);
423 if ((device->features & DASD_FEATURE_USERAW)) {
424 disk = device->block->gdp;
425 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
426 return 0;
428 disk = device->block->bdev->bd_disk;
429 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
430 while ((part = disk_part_iter_next(&piter)))
431 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
432 disk_part_iter_exit(&piter);
434 return 0;
438 * Stop the requeueing of requests again.
440 static int dasd_state_online_to_ready(struct dasd_device *device)
442 int rc;
443 struct gendisk *disk;
444 struct disk_part_iter piter;
445 struct hd_struct *part;
447 if (device->discipline->online_to_ready) {
448 rc = device->discipline->online_to_ready(device);
449 if (rc)
450 return rc;
453 device->state = DASD_STATE_READY;
454 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
455 disk = device->block->bdev->bd_disk;
456 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
457 while ((part = disk_part_iter_next(&piter)))
458 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
459 disk_part_iter_exit(&piter);
461 return 0;
465 * Device startup state changes.
467 static int dasd_increase_state(struct dasd_device *device)
469 int rc;
471 rc = 0;
472 if (device->state == DASD_STATE_NEW &&
473 device->target >= DASD_STATE_KNOWN)
474 rc = dasd_state_new_to_known(device);
476 if (!rc &&
477 device->state == DASD_STATE_KNOWN &&
478 device->target >= DASD_STATE_BASIC)
479 rc = dasd_state_known_to_basic(device);
481 if (!rc &&
482 device->state == DASD_STATE_BASIC &&
483 device->target >= DASD_STATE_READY)
484 rc = dasd_state_basic_to_ready(device);
486 if (!rc &&
487 device->state == DASD_STATE_UNFMT &&
488 device->target > DASD_STATE_UNFMT)
489 rc = -EPERM;
491 if (!rc &&
492 device->state == DASD_STATE_READY &&
493 device->target >= DASD_STATE_ONLINE)
494 rc = dasd_state_ready_to_online(device);
496 return rc;
500 * Device shutdown state changes.
502 static int dasd_decrease_state(struct dasd_device *device)
504 int rc;
506 rc = 0;
507 if (device->state == DASD_STATE_ONLINE &&
508 device->target <= DASD_STATE_READY)
509 rc = dasd_state_online_to_ready(device);
511 if (!rc &&
512 device->state == DASD_STATE_READY &&
513 device->target <= DASD_STATE_BASIC)
514 rc = dasd_state_ready_to_basic(device);
516 if (!rc &&
517 device->state == DASD_STATE_UNFMT &&
518 device->target <= DASD_STATE_BASIC)
519 rc = dasd_state_unfmt_to_basic(device);
521 if (!rc &&
522 device->state == DASD_STATE_BASIC &&
523 device->target <= DASD_STATE_KNOWN)
524 rc = dasd_state_basic_to_known(device);
526 if (!rc &&
527 device->state == DASD_STATE_KNOWN &&
528 device->target <= DASD_STATE_NEW)
529 rc = dasd_state_known_to_new(device);
531 return rc;
535 * This is the main startup/shutdown routine.
537 static void dasd_change_state(struct dasd_device *device)
539 int rc;
541 if (device->state == device->target)
542 /* Already where we want to go today... */
543 return;
544 if (device->state < device->target)
545 rc = dasd_increase_state(device);
546 else
547 rc = dasd_decrease_state(device);
548 if (rc == -EAGAIN)
549 return;
550 if (rc)
551 device->target = device->state;
553 /* let user-space know that the device status changed */
554 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
556 if (device->state == device->target)
557 wake_up(&dasd_init_waitq);
561 * Kick starter for devices that did not complete the startup/shutdown
562 * procedure or were sleeping because of a pending state.
563 * dasd_kick_device will schedule a call do do_kick_device to the kernel
564 * event daemon.
566 static void do_kick_device(struct work_struct *work)
568 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
569 mutex_lock(&device->state_mutex);
570 dasd_change_state(device);
571 mutex_unlock(&device->state_mutex);
572 dasd_schedule_device_bh(device);
573 dasd_put_device(device);
576 void dasd_kick_device(struct dasd_device *device)
578 dasd_get_device(device);
579 /* queue call to dasd_kick_device to the kernel event daemon. */
580 schedule_work(&device->kick_work);
584 * dasd_reload_device will schedule a call do do_reload_device to the kernel
585 * event daemon.
587 static void do_reload_device(struct work_struct *work)
589 struct dasd_device *device = container_of(work, struct dasd_device,
590 reload_device);
591 device->discipline->reload(device);
592 dasd_put_device(device);
595 void dasd_reload_device(struct dasd_device *device)
597 dasd_get_device(device);
598 /* queue call to dasd_reload_device to the kernel event daemon. */
599 schedule_work(&device->reload_device);
601 EXPORT_SYMBOL(dasd_reload_device);
604 * dasd_restore_device will schedule a call do do_restore_device to the kernel
605 * event daemon.
607 static void do_restore_device(struct work_struct *work)
609 struct dasd_device *device = container_of(work, struct dasd_device,
610 restore_device);
611 device->cdev->drv->restore(device->cdev);
612 dasd_put_device(device);
615 void dasd_restore_device(struct dasd_device *device)
617 dasd_get_device(device);
618 /* queue call to dasd_restore_device to the kernel event daemon. */
619 schedule_work(&device->restore_device);
623 * Set the target state for a device and starts the state change.
625 void dasd_set_target_state(struct dasd_device *device, int target)
627 dasd_get_device(device);
628 mutex_lock(&device->state_mutex);
629 /* If we are in probeonly mode stop at DASD_STATE_READY. */
630 if (dasd_probeonly && target > DASD_STATE_READY)
631 target = DASD_STATE_READY;
632 if (device->target != target) {
633 if (device->state == target)
634 wake_up(&dasd_init_waitq);
635 device->target = target;
637 if (device->state != device->target)
638 dasd_change_state(device);
639 mutex_unlock(&device->state_mutex);
640 dasd_put_device(device);
644 * Enable devices with device numbers in [from..to].
646 static inline int _wait_for_device(struct dasd_device *device)
648 return (device->state == device->target);
651 void dasd_enable_device(struct dasd_device *device)
653 dasd_set_target_state(device, DASD_STATE_ONLINE);
654 if (device->state <= DASD_STATE_KNOWN)
655 /* No discipline for device found. */
656 dasd_set_target_state(device, DASD_STATE_NEW);
657 /* Now wait for the devices to come up. */
658 wait_event(dasd_init_waitq, _wait_for_device(device));
660 dasd_reload_device(device);
661 if (device->discipline->kick_validate)
662 device->discipline->kick_validate(device);
666 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
669 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
671 #ifdef CONFIG_DASD_PROFILE
672 struct dasd_profile_info dasd_global_profile_data;
673 static struct dentry *dasd_global_profile_dentry;
674 static struct dentry *dasd_debugfs_global_entry;
677 * Add profiling information for cqr before execution.
679 static void dasd_profile_start(struct dasd_block *block,
680 struct dasd_ccw_req *cqr,
681 struct request *req)
683 struct list_head *l;
684 unsigned int counter;
685 struct dasd_device *device;
687 /* count the length of the chanq for statistics */
688 counter = 0;
689 if (dasd_global_profile_level || block->profile.data)
690 list_for_each(l, &block->ccw_queue)
691 if (++counter >= 31)
692 break;
694 if (dasd_global_profile_level) {
695 dasd_global_profile_data.dasd_io_nr_req[counter]++;
696 if (rq_data_dir(req) == READ)
697 dasd_global_profile_data.dasd_read_nr_req[counter]++;
700 spin_lock(&block->profile.lock);
701 if (block->profile.data)
702 block->profile.data->dasd_io_nr_req[counter]++;
703 if (rq_data_dir(req) == READ)
704 block->profile.data->dasd_read_nr_req[counter]++;
705 spin_unlock(&block->profile.lock);
708 * We count the request for the start device, even though it may run on
709 * some other device due to error recovery. This way we make sure that
710 * we count each request only once.
712 device = cqr->startdev;
713 if (device->profile.data) {
714 counter = 1; /* request is not yet queued on the start device */
715 list_for_each(l, &device->ccw_queue)
716 if (++counter >= 31)
717 break;
719 spin_lock(&device->profile.lock);
720 if (device->profile.data) {
721 device->profile.data->dasd_io_nr_req[counter]++;
722 if (rq_data_dir(req) == READ)
723 device->profile.data->dasd_read_nr_req[counter]++;
725 spin_unlock(&device->profile.lock);
729 * Add profiling information for cqr after execution.
732 #define dasd_profile_counter(value, index) \
734 for (index = 0; index < 31 && value >> (2+index); index++) \
738 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
739 int is_alias,
740 int is_tpm,
741 int is_read,
742 long sectors,
743 int sectors_ind,
744 int tottime_ind,
745 int tottimeps_ind,
746 int strtime_ind,
747 int irqtime_ind,
748 int irqtimeps_ind,
749 int endtime_ind)
751 /* in case of an overflow, reset the whole profile */
752 if (data->dasd_io_reqs == UINT_MAX) {
753 memset(data, 0, sizeof(*data));
754 getnstimeofday(&data->starttod);
756 data->dasd_io_reqs++;
757 data->dasd_io_sects += sectors;
758 if (is_alias)
759 data->dasd_io_alias++;
760 if (is_tpm)
761 data->dasd_io_tpm++;
763 data->dasd_io_secs[sectors_ind]++;
764 data->dasd_io_times[tottime_ind]++;
765 data->dasd_io_timps[tottimeps_ind]++;
766 data->dasd_io_time1[strtime_ind]++;
767 data->dasd_io_time2[irqtime_ind]++;
768 data->dasd_io_time2ps[irqtimeps_ind]++;
769 data->dasd_io_time3[endtime_ind]++;
771 if (is_read) {
772 data->dasd_read_reqs++;
773 data->dasd_read_sects += sectors;
774 if (is_alias)
775 data->dasd_read_alias++;
776 if (is_tpm)
777 data->dasd_read_tpm++;
778 data->dasd_read_secs[sectors_ind]++;
779 data->dasd_read_times[tottime_ind]++;
780 data->dasd_read_time1[strtime_ind]++;
781 data->dasd_read_time2[irqtime_ind]++;
782 data->dasd_read_time3[endtime_ind]++;
786 static void dasd_profile_end(struct dasd_block *block,
787 struct dasd_ccw_req *cqr,
788 struct request *req)
790 long strtime, irqtime, endtime, tottime; /* in microseconds */
791 long tottimeps, sectors;
792 struct dasd_device *device;
793 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
794 int irqtime_ind, irqtimeps_ind, endtime_ind;
796 device = cqr->startdev;
797 if (!(dasd_global_profile_level ||
798 block->profile.data ||
799 device->profile.data))
800 return;
802 sectors = blk_rq_sectors(req);
803 if (!cqr->buildclk || !cqr->startclk ||
804 !cqr->stopclk || !cqr->endclk ||
805 !sectors)
806 return;
808 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
809 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
810 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
811 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
812 tottimeps = tottime / sectors;
814 dasd_profile_counter(sectors, sectors_ind);
815 dasd_profile_counter(tottime, tottime_ind);
816 dasd_profile_counter(tottimeps, tottimeps_ind);
817 dasd_profile_counter(strtime, strtime_ind);
818 dasd_profile_counter(irqtime, irqtime_ind);
819 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
820 dasd_profile_counter(endtime, endtime_ind);
822 if (dasd_global_profile_level) {
823 dasd_profile_end_add_data(&dasd_global_profile_data,
824 cqr->startdev != block->base,
825 cqr->cpmode == 1,
826 rq_data_dir(req) == READ,
827 sectors, sectors_ind, tottime_ind,
828 tottimeps_ind, strtime_ind,
829 irqtime_ind, irqtimeps_ind,
830 endtime_ind);
833 spin_lock(&block->profile.lock);
834 if (block->profile.data)
835 dasd_profile_end_add_data(block->profile.data,
836 cqr->startdev != block->base,
837 cqr->cpmode == 1,
838 rq_data_dir(req) == READ,
839 sectors, sectors_ind, tottime_ind,
840 tottimeps_ind, strtime_ind,
841 irqtime_ind, irqtimeps_ind,
842 endtime_ind);
843 spin_unlock(&block->profile.lock);
845 spin_lock(&device->profile.lock);
846 if (device->profile.data)
847 dasd_profile_end_add_data(device->profile.data,
848 cqr->startdev != block->base,
849 cqr->cpmode == 1,
850 rq_data_dir(req) == READ,
851 sectors, sectors_ind, tottime_ind,
852 tottimeps_ind, strtime_ind,
853 irqtime_ind, irqtimeps_ind,
854 endtime_ind);
855 spin_unlock(&device->profile.lock);
858 void dasd_profile_reset(struct dasd_profile *profile)
860 struct dasd_profile_info *data;
862 spin_lock_bh(&profile->lock);
863 data = profile->data;
864 if (!data) {
865 spin_unlock_bh(&profile->lock);
866 return;
868 memset(data, 0, sizeof(*data));
869 getnstimeofday(&data->starttod);
870 spin_unlock_bh(&profile->lock);
873 void dasd_global_profile_reset(void)
875 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
876 getnstimeofday(&dasd_global_profile_data.starttod);
879 int dasd_profile_on(struct dasd_profile *profile)
881 struct dasd_profile_info *data;
883 data = kzalloc(sizeof(*data), GFP_KERNEL);
884 if (!data)
885 return -ENOMEM;
886 spin_lock_bh(&profile->lock);
887 if (profile->data) {
888 spin_unlock_bh(&profile->lock);
889 kfree(data);
890 return 0;
892 getnstimeofday(&data->starttod);
893 profile->data = data;
894 spin_unlock_bh(&profile->lock);
895 return 0;
898 void dasd_profile_off(struct dasd_profile *profile)
900 spin_lock_bh(&profile->lock);
901 kfree(profile->data);
902 profile->data = NULL;
903 spin_unlock_bh(&profile->lock);
906 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
908 char *buffer;
910 buffer = vmalloc(user_len + 1);
911 if (buffer == NULL)
912 return ERR_PTR(-ENOMEM);
913 if (copy_from_user(buffer, user_buf, user_len) != 0) {
914 vfree(buffer);
915 return ERR_PTR(-EFAULT);
917 /* got the string, now strip linefeed. */
918 if (buffer[user_len - 1] == '\n')
919 buffer[user_len - 1] = 0;
920 else
921 buffer[user_len] = 0;
922 return buffer;
925 static ssize_t dasd_stats_write(struct file *file,
926 const char __user *user_buf,
927 size_t user_len, loff_t *pos)
929 char *buffer, *str;
930 int rc;
931 struct seq_file *m = (struct seq_file *)file->private_data;
932 struct dasd_profile *prof = m->private;
934 if (user_len > 65536)
935 user_len = 65536;
936 buffer = dasd_get_user_string(user_buf, user_len);
937 if (IS_ERR(buffer))
938 return PTR_ERR(buffer);
940 str = skip_spaces(buffer);
941 rc = user_len;
942 if (strncmp(str, "reset", 5) == 0) {
943 dasd_profile_reset(prof);
944 } else if (strncmp(str, "on", 2) == 0) {
945 rc = dasd_profile_on(prof);
946 if (!rc)
947 rc = user_len;
948 } else if (strncmp(str, "off", 3) == 0) {
949 dasd_profile_off(prof);
950 } else
951 rc = -EINVAL;
952 vfree(buffer);
953 return rc;
956 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
958 int i;
960 for (i = 0; i < 32; i++)
961 seq_printf(m, "%u ", array[i]);
962 seq_putc(m, '\n');
965 static void dasd_stats_seq_print(struct seq_file *m,
966 struct dasd_profile_info *data)
968 seq_printf(m, "start_time %ld.%09ld\n",
969 data->starttod.tv_sec, data->starttod.tv_nsec);
970 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
971 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
972 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
973 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
974 seq_printf(m, "histogram_sectors ");
975 dasd_stats_array(m, data->dasd_io_secs);
976 seq_printf(m, "histogram_io_times ");
977 dasd_stats_array(m, data->dasd_io_times);
978 seq_printf(m, "histogram_io_times_weighted ");
979 dasd_stats_array(m, data->dasd_io_timps);
980 seq_printf(m, "histogram_time_build_to_ssch ");
981 dasd_stats_array(m, data->dasd_io_time1);
982 seq_printf(m, "histogram_time_ssch_to_irq ");
983 dasd_stats_array(m, data->dasd_io_time2);
984 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
985 dasd_stats_array(m, data->dasd_io_time2ps);
986 seq_printf(m, "histogram_time_irq_to_end ");
987 dasd_stats_array(m, data->dasd_io_time3);
988 seq_printf(m, "histogram_ccw_queue_length ");
989 dasd_stats_array(m, data->dasd_io_nr_req);
990 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
991 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
992 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
993 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
994 seq_printf(m, "histogram_read_sectors ");
995 dasd_stats_array(m, data->dasd_read_secs);
996 seq_printf(m, "histogram_read_times ");
997 dasd_stats_array(m, data->dasd_read_times);
998 seq_printf(m, "histogram_read_time_build_to_ssch ");
999 dasd_stats_array(m, data->dasd_read_time1);
1000 seq_printf(m, "histogram_read_time_ssch_to_irq ");
1001 dasd_stats_array(m, data->dasd_read_time2);
1002 seq_printf(m, "histogram_read_time_irq_to_end ");
1003 dasd_stats_array(m, data->dasd_read_time3);
1004 seq_printf(m, "histogram_read_ccw_queue_length ");
1005 dasd_stats_array(m, data->dasd_read_nr_req);
1008 static int dasd_stats_show(struct seq_file *m, void *v)
1010 struct dasd_profile *profile;
1011 struct dasd_profile_info *data;
1013 profile = m->private;
1014 spin_lock_bh(&profile->lock);
1015 data = profile->data;
1016 if (!data) {
1017 spin_unlock_bh(&profile->lock);
1018 seq_printf(m, "disabled\n");
1019 return 0;
1021 dasd_stats_seq_print(m, data);
1022 spin_unlock_bh(&profile->lock);
1023 return 0;
1026 static int dasd_stats_open(struct inode *inode, struct file *file)
1028 struct dasd_profile *profile = inode->i_private;
1029 return single_open(file, dasd_stats_show, profile);
1032 static const struct file_operations dasd_stats_raw_fops = {
1033 .owner = THIS_MODULE,
1034 .open = dasd_stats_open,
1035 .read = seq_read,
1036 .llseek = seq_lseek,
1037 .release = single_release,
1038 .write = dasd_stats_write,
1041 static ssize_t dasd_stats_global_write(struct file *file,
1042 const char __user *user_buf,
1043 size_t user_len, loff_t *pos)
1045 char *buffer, *str;
1046 ssize_t rc;
1048 if (user_len > 65536)
1049 user_len = 65536;
1050 buffer = dasd_get_user_string(user_buf, user_len);
1051 if (IS_ERR(buffer))
1052 return PTR_ERR(buffer);
1053 str = skip_spaces(buffer);
1054 rc = user_len;
1055 if (strncmp(str, "reset", 5) == 0) {
1056 dasd_global_profile_reset();
1057 } else if (strncmp(str, "on", 2) == 0) {
1058 dasd_global_profile_reset();
1059 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1060 } else if (strncmp(str, "off", 3) == 0) {
1061 dasd_global_profile_level = DASD_PROFILE_OFF;
1062 } else
1063 rc = -EINVAL;
1064 vfree(buffer);
1065 return rc;
1068 static int dasd_stats_global_show(struct seq_file *m, void *v)
1070 if (!dasd_global_profile_level) {
1071 seq_printf(m, "disabled\n");
1072 return 0;
1074 dasd_stats_seq_print(m, &dasd_global_profile_data);
1075 return 0;
1078 static int dasd_stats_global_open(struct inode *inode, struct file *file)
1080 return single_open(file, dasd_stats_global_show, NULL);
1083 static const struct file_operations dasd_stats_global_fops = {
1084 .owner = THIS_MODULE,
1085 .open = dasd_stats_global_open,
1086 .read = seq_read,
1087 .llseek = seq_lseek,
1088 .release = single_release,
1089 .write = dasd_stats_global_write,
1092 static void dasd_profile_init(struct dasd_profile *profile,
1093 struct dentry *base_dentry)
1095 umode_t mode;
1096 struct dentry *pde;
1098 if (!base_dentry)
1099 return;
1100 profile->dentry = NULL;
1101 profile->data = NULL;
1102 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1103 pde = debugfs_create_file("statistics", mode, base_dentry,
1104 profile, &dasd_stats_raw_fops);
1105 if (pde && !IS_ERR(pde))
1106 profile->dentry = pde;
1107 return;
1110 static void dasd_profile_exit(struct dasd_profile *profile)
1112 dasd_profile_off(profile);
1113 if (profile->dentry) {
1114 debugfs_remove(profile->dentry);
1115 profile->dentry = NULL;
1119 static void dasd_statistics_removeroot(void)
1121 dasd_global_profile_level = DASD_PROFILE_OFF;
1122 if (dasd_global_profile_dentry) {
1123 debugfs_remove(dasd_global_profile_dentry);
1124 dasd_global_profile_dentry = NULL;
1126 if (dasd_debugfs_global_entry)
1127 debugfs_remove(dasd_debugfs_global_entry);
1128 if (dasd_debugfs_root_entry)
1129 debugfs_remove(dasd_debugfs_root_entry);
1132 static void dasd_statistics_createroot(void)
1134 umode_t mode;
1135 struct dentry *pde;
1137 dasd_debugfs_root_entry = NULL;
1138 dasd_debugfs_global_entry = NULL;
1139 dasd_global_profile_dentry = NULL;
1140 pde = debugfs_create_dir("dasd", NULL);
1141 if (!pde || IS_ERR(pde))
1142 goto error;
1143 dasd_debugfs_root_entry = pde;
1144 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1145 if (!pde || IS_ERR(pde))
1146 goto error;
1147 dasd_debugfs_global_entry = pde;
1149 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1150 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1151 NULL, &dasd_stats_global_fops);
1152 if (!pde || IS_ERR(pde))
1153 goto error;
1154 dasd_global_profile_dentry = pde;
1155 return;
1157 error:
1158 DBF_EVENT(DBF_ERR, "%s",
1159 "Creation of the dasd debugfs interface failed");
1160 dasd_statistics_removeroot();
1161 return;
1164 #else
1165 #define dasd_profile_start(block, cqr, req) do {} while (0)
1166 #define dasd_profile_end(block, cqr, req) do {} while (0)
1168 static void dasd_statistics_createroot(void)
1170 return;
1173 static void dasd_statistics_removeroot(void)
1175 return;
1178 int dasd_stats_generic_show(struct seq_file *m, void *v)
1180 seq_printf(m, "Statistics are not activated in this kernel\n");
1181 return 0;
1184 static void dasd_profile_init(struct dasd_profile *profile,
1185 struct dentry *base_dentry)
1187 return;
1190 static void dasd_profile_exit(struct dasd_profile *profile)
1192 return;
1195 int dasd_profile_on(struct dasd_profile *profile)
1197 return 0;
1200 #endif /* CONFIG_DASD_PROFILE */
1203 * Allocate memory for a channel program with 'cplength' channel
1204 * command words and 'datasize' additional space. There are two
1205 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1206 * memory and 2) dasd_smalloc_request uses the static ccw memory
1207 * that gets allocated for each device.
1209 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
1210 int datasize,
1211 struct dasd_device *device)
1213 struct dasd_ccw_req *cqr;
1215 /* Sanity checks */
1216 BUG_ON(datasize > PAGE_SIZE ||
1217 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1219 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1220 if (cqr == NULL)
1221 return ERR_PTR(-ENOMEM);
1222 cqr->cpaddr = NULL;
1223 if (cplength > 0) {
1224 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1225 GFP_ATOMIC | GFP_DMA);
1226 if (cqr->cpaddr == NULL) {
1227 kfree(cqr);
1228 return ERR_PTR(-ENOMEM);
1231 cqr->data = NULL;
1232 if (datasize > 0) {
1233 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1234 if (cqr->data == NULL) {
1235 kfree(cqr->cpaddr);
1236 kfree(cqr);
1237 return ERR_PTR(-ENOMEM);
1240 cqr->magic = magic;
1241 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1242 dasd_get_device(device);
1243 return cqr;
1246 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
1247 int datasize,
1248 struct dasd_device *device)
1250 unsigned long flags;
1251 struct dasd_ccw_req *cqr;
1252 char *data;
1253 int size;
1255 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1256 if (cplength > 0)
1257 size += cplength * sizeof(struct ccw1);
1258 if (datasize > 0)
1259 size += datasize;
1260 spin_lock_irqsave(&device->mem_lock, flags);
1261 cqr = (struct dasd_ccw_req *)
1262 dasd_alloc_chunk(&device->ccw_chunks, size);
1263 spin_unlock_irqrestore(&device->mem_lock, flags);
1264 if (cqr == NULL)
1265 return ERR_PTR(-ENOMEM);
1266 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1267 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1268 cqr->cpaddr = NULL;
1269 if (cplength > 0) {
1270 cqr->cpaddr = (struct ccw1 *) data;
1271 data += cplength*sizeof(struct ccw1);
1272 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1274 cqr->data = NULL;
1275 if (datasize > 0) {
1276 cqr->data = data;
1277 memset(cqr->data, 0, datasize);
1279 cqr->magic = magic;
1280 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1281 dasd_get_device(device);
1282 return cqr;
1286 * Free memory of a channel program. This function needs to free all the
1287 * idal lists that might have been created by dasd_set_cda and the
1288 * struct dasd_ccw_req itself.
1290 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1292 #ifdef CONFIG_64BIT
1293 struct ccw1 *ccw;
1295 /* Clear any idals used for the request. */
1296 ccw = cqr->cpaddr;
1297 do {
1298 clear_normalized_cda(ccw);
1299 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1300 #endif
1301 kfree(cqr->cpaddr);
1302 kfree(cqr->data);
1303 kfree(cqr);
1304 dasd_put_device(device);
1307 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1309 unsigned long flags;
1311 spin_lock_irqsave(&device->mem_lock, flags);
1312 dasd_free_chunk(&device->ccw_chunks, cqr);
1313 spin_unlock_irqrestore(&device->mem_lock, flags);
1314 dasd_put_device(device);
1318 * Check discipline magic in cqr.
1320 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1322 struct dasd_device *device;
1324 if (cqr == NULL)
1325 return -EINVAL;
1326 device = cqr->startdev;
1327 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1328 DBF_DEV_EVENT(DBF_WARNING, device,
1329 " dasd_ccw_req 0x%08x magic doesn't match"
1330 " discipline 0x%08x",
1331 cqr->magic,
1332 *(unsigned int *) device->discipline->name);
1333 return -EINVAL;
1335 return 0;
1339 * Terminate the current i/o and set the request to clear_pending.
1340 * Timer keeps device runnig.
1341 * ccw_device_clear can fail if the i/o subsystem
1342 * is in a bad mood.
1344 int dasd_term_IO(struct dasd_ccw_req *cqr)
1346 struct dasd_device *device;
1347 int retries, rc;
1348 char errorstring[ERRORLENGTH];
1350 /* Check the cqr */
1351 rc = dasd_check_cqr(cqr);
1352 if (rc)
1353 return rc;
1354 retries = 0;
1355 device = (struct dasd_device *) cqr->startdev;
1356 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1357 rc = ccw_device_clear(device->cdev, (long) cqr);
1358 switch (rc) {
1359 case 0: /* termination successful */
1360 cqr->status = DASD_CQR_CLEAR_PENDING;
1361 cqr->stopclk = get_tod_clock();
1362 cqr->starttime = 0;
1363 DBF_DEV_EVENT(DBF_DEBUG, device,
1364 "terminate cqr %p successful",
1365 cqr);
1366 break;
1367 case -ENODEV:
1368 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1369 "device gone, retry");
1370 break;
1371 case -EIO:
1372 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1373 "I/O error, retry");
1374 break;
1375 case -EINVAL:
1376 case -EBUSY:
1377 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1378 "device busy, retry later");
1379 break;
1380 default:
1381 /* internal error 10 - unknown rc*/
1382 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1383 dev_err(&device->cdev->dev, "An error occurred in the "
1384 "DASD device driver, reason=%s\n", errorstring);
1385 BUG();
1386 break;
1388 retries++;
1390 dasd_schedule_device_bh(device);
1391 return rc;
1395 * Start the i/o. This start_IO can fail if the channel is really busy.
1396 * In that case set up a timer to start the request later.
1398 int dasd_start_IO(struct dasd_ccw_req *cqr)
1400 struct dasd_device *device;
1401 int rc;
1402 char errorstring[ERRORLENGTH];
1404 /* Check the cqr */
1405 rc = dasd_check_cqr(cqr);
1406 if (rc) {
1407 cqr->intrc = rc;
1408 return rc;
1410 device = (struct dasd_device *) cqr->startdev;
1411 if (((cqr->block &&
1412 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1413 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1414 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1415 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1416 "because of stolen lock", cqr);
1417 cqr->status = DASD_CQR_ERROR;
1418 cqr->intrc = -EPERM;
1419 return -EPERM;
1421 if (cqr->retries < 0) {
1422 /* internal error 14 - start_IO run out of retries */
1423 sprintf(errorstring, "14 %p", cqr);
1424 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1425 "device driver, reason=%s\n", errorstring);
1426 cqr->status = DASD_CQR_ERROR;
1427 return -EIO;
1429 cqr->startclk = get_tod_clock();
1430 cqr->starttime = jiffies;
1431 cqr->retries--;
1432 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1433 cqr->lpm &= device->path_data.opm;
1434 if (!cqr->lpm)
1435 cqr->lpm = device->path_data.opm;
1437 if (cqr->cpmode == 1) {
1438 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1439 (long) cqr, cqr->lpm);
1440 } else {
1441 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1442 (long) cqr, cqr->lpm, 0);
1444 switch (rc) {
1445 case 0:
1446 cqr->status = DASD_CQR_IN_IO;
1447 break;
1448 case -EBUSY:
1449 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1450 "start_IO: device busy, retry later");
1451 break;
1452 case -ETIMEDOUT:
1453 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1454 "start_IO: request timeout, retry later");
1455 break;
1456 case -EACCES:
1457 /* -EACCES indicates that the request used only a subset of the
1458 * available paths and all these paths are gone. If the lpm of
1459 * this request was only a subset of the opm (e.g. the ppm) then
1460 * we just do a retry with all available paths.
1461 * If we already use the full opm, something is amiss, and we
1462 * need a full path verification.
1464 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1465 DBF_DEV_EVENT(DBF_WARNING, device,
1466 "start_IO: selected paths gone (%x)",
1467 cqr->lpm);
1468 } else if (cqr->lpm != device->path_data.opm) {
1469 cqr->lpm = device->path_data.opm;
1470 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1471 "start_IO: selected paths gone,"
1472 " retry on all paths");
1473 } else {
1474 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1475 "start_IO: all paths in opm gone,"
1476 " do path verification");
1477 dasd_generic_last_path_gone(device);
1478 device->path_data.opm = 0;
1479 device->path_data.ppm = 0;
1480 device->path_data.npm = 0;
1481 device->path_data.tbvpm =
1482 ccw_device_get_path_mask(device->cdev);
1484 break;
1485 case -ENODEV:
1486 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1487 "start_IO: -ENODEV device gone, retry");
1488 break;
1489 case -EIO:
1490 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1491 "start_IO: -EIO device gone, retry");
1492 break;
1493 case -EINVAL:
1494 /* most likely caused in power management context */
1495 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1496 "start_IO: -EINVAL device currently "
1497 "not accessible");
1498 break;
1499 default:
1500 /* internal error 11 - unknown rc */
1501 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1502 dev_err(&device->cdev->dev,
1503 "An error occurred in the DASD device driver, "
1504 "reason=%s\n", errorstring);
1505 BUG();
1506 break;
1508 cqr->intrc = rc;
1509 return rc;
1513 * Timeout function for dasd devices. This is used for different purposes
1514 * 1) missing interrupt handler for normal operation
1515 * 2) delayed start of request where start_IO failed with -EBUSY
1516 * 3) timeout for missing state change interrupts
1517 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1518 * DASD_CQR_QUEUED for 2) and 3).
1520 static void dasd_device_timeout(unsigned long ptr)
1522 unsigned long flags;
1523 struct dasd_device *device;
1525 device = (struct dasd_device *) ptr;
1526 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1527 /* re-activate request queue */
1528 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1529 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1530 dasd_schedule_device_bh(device);
1534 * Setup timeout for a device in jiffies.
1536 void dasd_device_set_timer(struct dasd_device *device, int expires)
1538 if (expires == 0)
1539 del_timer(&device->timer);
1540 else
1541 mod_timer(&device->timer, jiffies + expires);
1545 * Clear timeout for a device.
1547 void dasd_device_clear_timer(struct dasd_device *device)
1549 del_timer(&device->timer);
1552 static void dasd_handle_killed_request(struct ccw_device *cdev,
1553 unsigned long intparm)
1555 struct dasd_ccw_req *cqr;
1556 struct dasd_device *device;
1558 if (!intparm)
1559 return;
1560 cqr = (struct dasd_ccw_req *) intparm;
1561 if (cqr->status != DASD_CQR_IN_IO) {
1562 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1563 "invalid status in handle_killed_request: "
1564 "%02x", cqr->status);
1565 return;
1568 device = dasd_device_from_cdev_locked(cdev);
1569 if (IS_ERR(device)) {
1570 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1571 "unable to get device from cdev");
1572 return;
1575 if (!cqr->startdev ||
1576 device != cqr->startdev ||
1577 strncmp(cqr->startdev->discipline->ebcname,
1578 (char *) &cqr->magic, 4)) {
1579 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1580 "invalid device in request");
1581 dasd_put_device(device);
1582 return;
1585 /* Schedule request to be retried. */
1586 cqr->status = DASD_CQR_QUEUED;
1588 dasd_device_clear_timer(device);
1589 dasd_schedule_device_bh(device);
1590 dasd_put_device(device);
1593 void dasd_generic_handle_state_change(struct dasd_device *device)
1595 /* First of all start sense subsystem status request. */
1596 dasd_eer_snss(device);
1598 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1599 dasd_schedule_device_bh(device);
1600 if (device->block)
1601 dasd_schedule_block_bh(device->block);
1605 * Interrupt handler for "normal" ssch-io based dasd devices.
1607 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1608 struct irb *irb)
1610 struct dasd_ccw_req *cqr, *next;
1611 struct dasd_device *device;
1612 unsigned long long now;
1613 int expires;
1615 cqr = (struct dasd_ccw_req *) intparm;
1616 if (IS_ERR(irb)) {
1617 switch (PTR_ERR(irb)) {
1618 case -EIO:
1619 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1620 device = (struct dasd_device *) cqr->startdev;
1621 cqr->status = DASD_CQR_CLEARED;
1622 dasd_device_clear_timer(device);
1623 wake_up(&dasd_flush_wq);
1624 dasd_schedule_device_bh(device);
1625 return;
1627 break;
1628 case -ETIMEDOUT:
1629 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1630 "request timed out\n", __func__);
1631 break;
1632 default:
1633 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1634 "unknown error %ld\n", __func__,
1635 PTR_ERR(irb));
1637 dasd_handle_killed_request(cdev, intparm);
1638 return;
1641 now = get_tod_clock();
1642 /* check for conditions that should be handled immediately */
1643 if (!cqr ||
1644 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1645 scsw_cstat(&irb->scsw) == 0)) {
1646 if (cqr)
1647 memcpy(&cqr->irb, irb, sizeof(*irb));
1648 device = dasd_device_from_cdev_locked(cdev);
1649 if (IS_ERR(device))
1650 return;
1651 /* ignore unsolicited interrupts for DIAG discipline */
1652 if (device->discipline == dasd_diag_discipline_pointer) {
1653 dasd_put_device(device);
1654 return;
1656 device->discipline->dump_sense_dbf(device, irb, "int");
1657 if (device->features & DASD_FEATURE_ERPLOG)
1658 device->discipline->dump_sense(device, cqr, irb);
1659 device->discipline->check_for_device_change(device, cqr, irb);
1660 dasd_put_device(device);
1662 if (!cqr)
1663 return;
1665 device = (struct dasd_device *) cqr->startdev;
1666 if (!device ||
1667 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1668 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1669 "invalid device in request");
1670 return;
1673 /* Check for clear pending */
1674 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1675 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1676 cqr->status = DASD_CQR_CLEARED;
1677 dasd_device_clear_timer(device);
1678 wake_up(&dasd_flush_wq);
1679 dasd_schedule_device_bh(device);
1680 return;
1683 /* check status - the request might have been killed by dyn detach */
1684 if (cqr->status != DASD_CQR_IN_IO) {
1685 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1686 "status %02x", dev_name(&cdev->dev), cqr->status);
1687 return;
1690 next = NULL;
1691 expires = 0;
1692 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1693 scsw_cstat(&irb->scsw) == 0) {
1694 /* request was completed successfully */
1695 cqr->status = DASD_CQR_SUCCESS;
1696 cqr->stopclk = now;
1697 /* Start first request on queue if possible -> fast_io. */
1698 if (cqr->devlist.next != &device->ccw_queue) {
1699 next = list_entry(cqr->devlist.next,
1700 struct dasd_ccw_req, devlist);
1702 } else { /* error */
1704 * If we don't want complex ERP for this request, then just
1705 * reset this and retry it in the fastpath
1707 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1708 cqr->retries > 0) {
1709 if (cqr->lpm == device->path_data.opm)
1710 DBF_DEV_EVENT(DBF_DEBUG, device,
1711 "default ERP in fastpath "
1712 "(%i retries left)",
1713 cqr->retries);
1714 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1715 cqr->lpm = device->path_data.opm;
1716 cqr->status = DASD_CQR_QUEUED;
1717 next = cqr;
1718 } else
1719 cqr->status = DASD_CQR_ERROR;
1721 if (next && (next->status == DASD_CQR_QUEUED) &&
1722 (!device->stopped)) {
1723 if (device->discipline->start_IO(next) == 0)
1724 expires = next->expires;
1726 if (expires != 0)
1727 dasd_device_set_timer(device, expires);
1728 else
1729 dasd_device_clear_timer(device);
1730 dasd_schedule_device_bh(device);
1733 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1735 struct dasd_device *device;
1737 device = dasd_device_from_cdev_locked(cdev);
1739 if (IS_ERR(device))
1740 goto out;
1741 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1742 device->state != device->target ||
1743 !device->discipline->check_for_device_change){
1744 dasd_put_device(device);
1745 goto out;
1747 if (device->discipline->dump_sense_dbf)
1748 device->discipline->dump_sense_dbf(device, irb, "uc");
1749 device->discipline->check_for_device_change(device, NULL, irb);
1750 dasd_put_device(device);
1751 out:
1752 return UC_TODO_RETRY;
1754 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1757 * If we have an error on a dasd_block layer request then we cancel
1758 * and return all further requests from the same dasd_block as well.
1760 static void __dasd_device_recovery(struct dasd_device *device,
1761 struct dasd_ccw_req *ref_cqr)
1763 struct list_head *l, *n;
1764 struct dasd_ccw_req *cqr;
1767 * only requeue request that came from the dasd_block layer
1769 if (!ref_cqr->block)
1770 return;
1772 list_for_each_safe(l, n, &device->ccw_queue) {
1773 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1774 if (cqr->status == DASD_CQR_QUEUED &&
1775 ref_cqr->block == cqr->block) {
1776 cqr->status = DASD_CQR_CLEARED;
1782 * Remove those ccw requests from the queue that need to be returned
1783 * to the upper layer.
1785 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1786 struct list_head *final_queue)
1788 struct list_head *l, *n;
1789 struct dasd_ccw_req *cqr;
1791 /* Process request with final status. */
1792 list_for_each_safe(l, n, &device->ccw_queue) {
1793 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1795 /* Skip any non-final request. */
1796 if (cqr->status == DASD_CQR_QUEUED ||
1797 cqr->status == DASD_CQR_IN_IO ||
1798 cqr->status == DASD_CQR_CLEAR_PENDING)
1799 continue;
1800 if (cqr->status == DASD_CQR_ERROR) {
1801 __dasd_device_recovery(device, cqr);
1803 /* Rechain finished requests to final queue */
1804 list_move_tail(&cqr->devlist, final_queue);
1809 * the cqrs from the final queue are returned to the upper layer
1810 * by setting a dasd_block state and calling the callback function
1812 static void __dasd_device_process_final_queue(struct dasd_device *device,
1813 struct list_head *final_queue)
1815 struct list_head *l, *n;
1816 struct dasd_ccw_req *cqr;
1817 struct dasd_block *block;
1818 void (*callback)(struct dasd_ccw_req *, void *data);
1819 void *callback_data;
1820 char errorstring[ERRORLENGTH];
1822 list_for_each_safe(l, n, final_queue) {
1823 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1824 list_del_init(&cqr->devlist);
1825 block = cqr->block;
1826 callback = cqr->callback;
1827 callback_data = cqr->callback_data;
1828 if (block)
1829 spin_lock_bh(&block->queue_lock);
1830 switch (cqr->status) {
1831 case DASD_CQR_SUCCESS:
1832 cqr->status = DASD_CQR_DONE;
1833 break;
1834 case DASD_CQR_ERROR:
1835 cqr->status = DASD_CQR_NEED_ERP;
1836 break;
1837 case DASD_CQR_CLEARED:
1838 cqr->status = DASD_CQR_TERMINATED;
1839 break;
1840 default:
1841 /* internal error 12 - wrong cqr status*/
1842 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1843 dev_err(&device->cdev->dev,
1844 "An error occurred in the DASD device driver, "
1845 "reason=%s\n", errorstring);
1846 BUG();
1848 if (cqr->callback != NULL)
1849 (callback)(cqr, callback_data);
1850 if (block)
1851 spin_unlock_bh(&block->queue_lock);
1856 * Take a look at the first request on the ccw queue and check
1857 * if it reached its expire time. If so, terminate the IO.
1859 static void __dasd_device_check_expire(struct dasd_device *device)
1861 struct dasd_ccw_req *cqr;
1863 if (list_empty(&device->ccw_queue))
1864 return;
1865 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1866 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1867 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1868 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1870 * IO in safe offline processing should not
1871 * run out of retries
1873 cqr->retries++;
1875 if (device->discipline->term_IO(cqr) != 0) {
1876 /* Hmpf, try again in 5 sec */
1877 dev_err(&device->cdev->dev,
1878 "cqr %p timed out (%lus) but cannot be "
1879 "ended, retrying in 5 s\n",
1880 cqr, (cqr->expires/HZ));
1881 cqr->expires += 5*HZ;
1882 dasd_device_set_timer(device, 5*HZ);
1883 } else {
1884 dev_err(&device->cdev->dev,
1885 "cqr %p timed out (%lus), %i retries "
1886 "remaining\n", cqr, (cqr->expires/HZ),
1887 cqr->retries);
1893 * Take a look at the first request on the ccw queue and check
1894 * if it needs to be started.
1896 static void __dasd_device_start_head(struct dasd_device *device)
1898 struct dasd_ccw_req *cqr;
1899 int rc;
1901 if (list_empty(&device->ccw_queue))
1902 return;
1903 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1904 if (cqr->status != DASD_CQR_QUEUED)
1905 return;
1906 /* when device is stopped, return request to previous layer
1907 * exception: only the disconnect or unresumed bits are set and the
1908 * cqr is a path verification request
1910 if (device->stopped &&
1911 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1912 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1913 cqr->intrc = -EAGAIN;
1914 cqr->status = DASD_CQR_CLEARED;
1915 dasd_schedule_device_bh(device);
1916 return;
1919 rc = device->discipline->start_IO(cqr);
1920 if (rc == 0)
1921 dasd_device_set_timer(device, cqr->expires);
1922 else if (rc == -EACCES) {
1923 dasd_schedule_device_bh(device);
1924 } else
1925 /* Hmpf, try again in 1/2 sec */
1926 dasd_device_set_timer(device, 50);
1929 static void __dasd_device_check_path_events(struct dasd_device *device)
1931 int rc;
1933 if (device->path_data.tbvpm) {
1934 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1935 DASD_UNRESUMED_PM))
1936 return;
1937 rc = device->discipline->verify_path(
1938 device, device->path_data.tbvpm);
1939 if (rc)
1940 dasd_device_set_timer(device, 50);
1941 else
1942 device->path_data.tbvpm = 0;
1947 * Go through all request on the dasd_device request queue,
1948 * terminate them on the cdev if necessary, and return them to the
1949 * submitting layer via callback.
1950 * Note:
1951 * Make sure that all 'submitting layers' still exist when
1952 * this function is called!. In other words, when 'device' is a base
1953 * device then all block layer requests must have been removed before
1954 * via dasd_flush_block_queue.
1956 int dasd_flush_device_queue(struct dasd_device *device)
1958 struct dasd_ccw_req *cqr, *n;
1959 int rc;
1960 struct list_head flush_queue;
1962 INIT_LIST_HEAD(&flush_queue);
1963 spin_lock_irq(get_ccwdev_lock(device->cdev));
1964 rc = 0;
1965 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1966 /* Check status and move request to flush_queue */
1967 switch (cqr->status) {
1968 case DASD_CQR_IN_IO:
1969 rc = device->discipline->term_IO(cqr);
1970 if (rc) {
1971 /* unable to terminate requeust */
1972 dev_err(&device->cdev->dev,
1973 "Flushing the DASD request queue "
1974 "failed for request %p\n", cqr);
1975 /* stop flush processing */
1976 goto finished;
1978 break;
1979 case DASD_CQR_QUEUED:
1980 cqr->stopclk = get_tod_clock();
1981 cqr->status = DASD_CQR_CLEARED;
1982 break;
1983 default: /* no need to modify the others */
1984 break;
1986 list_move_tail(&cqr->devlist, &flush_queue);
1988 finished:
1989 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1991 * After this point all requests must be in state CLEAR_PENDING,
1992 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1993 * one of the others.
1995 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1996 wait_event(dasd_flush_wq,
1997 (cqr->status != DASD_CQR_CLEAR_PENDING));
1999 * Now set each request back to TERMINATED, DONE or NEED_ERP
2000 * and call the callback function of flushed requests
2002 __dasd_device_process_final_queue(device, &flush_queue);
2003 return rc;
2007 * Acquire the device lock and process queues for the device.
2009 static void dasd_device_tasklet(struct dasd_device *device)
2011 struct list_head final_queue;
2013 atomic_set (&device->tasklet_scheduled, 0);
2014 INIT_LIST_HEAD(&final_queue);
2015 spin_lock_irq(get_ccwdev_lock(device->cdev));
2016 /* Check expire time of first request on the ccw queue. */
2017 __dasd_device_check_expire(device);
2018 /* find final requests on ccw queue */
2019 __dasd_device_process_ccw_queue(device, &final_queue);
2020 __dasd_device_check_path_events(device);
2021 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2022 /* Now call the callback function of requests with final status */
2023 __dasd_device_process_final_queue(device, &final_queue);
2024 spin_lock_irq(get_ccwdev_lock(device->cdev));
2025 /* Now check if the head of the ccw queue needs to be started. */
2026 __dasd_device_start_head(device);
2027 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2028 if (waitqueue_active(&shutdown_waitq))
2029 wake_up(&shutdown_waitq);
2030 dasd_put_device(device);
2034 * Schedules a call to dasd_tasklet over the device tasklet.
2036 void dasd_schedule_device_bh(struct dasd_device *device)
2038 /* Protect against rescheduling. */
2039 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2040 return;
2041 dasd_get_device(device);
2042 tasklet_hi_schedule(&device->tasklet);
2045 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2047 device->stopped |= bits;
2049 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2051 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2053 device->stopped &= ~bits;
2054 if (!device->stopped)
2055 wake_up(&generic_waitq);
2057 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2060 * Queue a request to the head of the device ccw_queue.
2061 * Start the I/O if possible.
2063 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2065 struct dasd_device *device;
2066 unsigned long flags;
2068 device = cqr->startdev;
2069 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2070 cqr->status = DASD_CQR_QUEUED;
2071 list_add(&cqr->devlist, &device->ccw_queue);
2072 /* let the bh start the request to keep them in order */
2073 dasd_schedule_device_bh(device);
2074 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2078 * Queue a request to the tail of the device ccw_queue.
2079 * Start the I/O if possible.
2081 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2083 struct dasd_device *device;
2084 unsigned long flags;
2086 device = cqr->startdev;
2087 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2088 cqr->status = DASD_CQR_QUEUED;
2089 list_add_tail(&cqr->devlist, &device->ccw_queue);
2090 /* let the bh start the request to keep them in order */
2091 dasd_schedule_device_bh(device);
2092 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2096 * Wakeup helper for the 'sleep_on' functions.
2098 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2100 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2101 cqr->callback_data = DASD_SLEEPON_END_TAG;
2102 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2103 wake_up(&generic_waitq);
2105 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2107 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2109 struct dasd_device *device;
2110 int rc;
2112 device = cqr->startdev;
2113 spin_lock_irq(get_ccwdev_lock(device->cdev));
2114 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2115 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2116 return rc;
2120 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2122 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2124 struct dasd_device *device;
2125 dasd_erp_fn_t erp_fn;
2127 if (cqr->status == DASD_CQR_FILLED)
2128 return 0;
2129 device = cqr->startdev;
2130 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2131 if (cqr->status == DASD_CQR_TERMINATED) {
2132 device->discipline->handle_terminated_request(cqr);
2133 return 1;
2135 if (cqr->status == DASD_CQR_NEED_ERP) {
2136 erp_fn = device->discipline->erp_action(cqr);
2137 erp_fn(cqr);
2138 return 1;
2140 if (cqr->status == DASD_CQR_FAILED)
2141 dasd_log_sense(cqr, &cqr->irb);
2142 if (cqr->refers) {
2143 __dasd_process_erp(device, cqr);
2144 return 1;
2147 return 0;
2150 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2152 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2153 if (cqr->refers) /* erp is not done yet */
2154 return 1;
2155 return ((cqr->status != DASD_CQR_DONE) &&
2156 (cqr->status != DASD_CQR_FAILED));
2157 } else
2158 return (cqr->status == DASD_CQR_FILLED);
2161 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2163 struct dasd_device *device;
2164 int rc;
2165 struct list_head ccw_queue;
2166 struct dasd_ccw_req *cqr;
2168 INIT_LIST_HEAD(&ccw_queue);
2169 maincqr->status = DASD_CQR_FILLED;
2170 device = maincqr->startdev;
2171 list_add(&maincqr->blocklist, &ccw_queue);
2172 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2173 cqr = list_first_entry(&ccw_queue,
2174 struct dasd_ccw_req, blocklist)) {
2176 if (__dasd_sleep_on_erp(cqr))
2177 continue;
2178 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2179 continue;
2180 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2181 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2182 cqr->status = DASD_CQR_FAILED;
2183 cqr->intrc = -EPERM;
2184 continue;
2186 /* Non-temporary stop condition will trigger fail fast */
2187 if (device->stopped & ~DASD_STOPPED_PENDING &&
2188 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2189 (!dasd_eer_enabled(device))) {
2190 cqr->status = DASD_CQR_FAILED;
2191 cqr->intrc = -ENOLINK;
2192 continue;
2194 /* Don't try to start requests if device is stopped */
2195 if (interruptible) {
2196 rc = wait_event_interruptible(
2197 generic_waitq, !(device->stopped));
2198 if (rc == -ERESTARTSYS) {
2199 cqr->status = DASD_CQR_FAILED;
2200 maincqr->intrc = rc;
2201 continue;
2203 } else
2204 wait_event(generic_waitq, !(device->stopped));
2206 if (!cqr->callback)
2207 cqr->callback = dasd_wakeup_cb;
2209 cqr->callback_data = DASD_SLEEPON_START_TAG;
2210 dasd_add_request_tail(cqr);
2211 if (interruptible) {
2212 rc = wait_event_interruptible(
2213 generic_waitq, _wait_for_wakeup(cqr));
2214 if (rc == -ERESTARTSYS) {
2215 dasd_cancel_req(cqr);
2216 /* wait (non-interruptible) for final status */
2217 wait_event(generic_waitq,
2218 _wait_for_wakeup(cqr));
2219 cqr->status = DASD_CQR_FAILED;
2220 maincqr->intrc = rc;
2221 continue;
2223 } else
2224 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2227 maincqr->endclk = get_tod_clock();
2228 if ((maincqr->status != DASD_CQR_DONE) &&
2229 (maincqr->intrc != -ERESTARTSYS))
2230 dasd_log_sense(maincqr, &maincqr->irb);
2231 if (maincqr->status == DASD_CQR_DONE)
2232 rc = 0;
2233 else if (maincqr->intrc)
2234 rc = maincqr->intrc;
2235 else
2236 rc = -EIO;
2237 return rc;
2240 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2242 struct dasd_ccw_req *cqr;
2244 list_for_each_entry(cqr, ccw_queue, blocklist) {
2245 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2246 return 0;
2249 return 1;
2252 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2254 struct dasd_device *device;
2255 int rc;
2256 struct dasd_ccw_req *cqr, *n;
2258 retry:
2259 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2260 device = cqr->startdev;
2261 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2262 continue;
2264 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2265 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2266 cqr->status = DASD_CQR_FAILED;
2267 cqr->intrc = -EPERM;
2268 continue;
2270 /*Non-temporary stop condition will trigger fail fast*/
2271 if (device->stopped & ~DASD_STOPPED_PENDING &&
2272 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2273 !dasd_eer_enabled(device)) {
2274 cqr->status = DASD_CQR_FAILED;
2275 cqr->intrc = -EAGAIN;
2276 continue;
2279 /*Don't try to start requests if device is stopped*/
2280 if (interruptible) {
2281 rc = wait_event_interruptible(
2282 generic_waitq, !device->stopped);
2283 if (rc == -ERESTARTSYS) {
2284 cqr->status = DASD_CQR_FAILED;
2285 cqr->intrc = rc;
2286 continue;
2288 } else
2289 wait_event(generic_waitq, !(device->stopped));
2291 if (!cqr->callback)
2292 cqr->callback = dasd_wakeup_cb;
2293 cqr->callback_data = DASD_SLEEPON_START_TAG;
2294 dasd_add_request_tail(cqr);
2297 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2299 rc = 0;
2300 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2301 if (__dasd_sleep_on_erp(cqr))
2302 rc = 1;
2304 if (rc)
2305 goto retry;
2308 return 0;
2312 * Queue a request to the tail of the device ccw_queue and wait for
2313 * it's completion.
2315 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2317 return _dasd_sleep_on(cqr, 0);
2321 * Start requests from a ccw_queue and wait for their completion.
2323 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2325 return _dasd_sleep_on_queue(ccw_queue, 0);
2327 EXPORT_SYMBOL(dasd_sleep_on_queue);
2330 * Queue a request to the tail of the device ccw_queue and wait
2331 * interruptible for it's completion.
2333 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2335 return _dasd_sleep_on(cqr, 1);
2339 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2340 * for eckd devices) the currently running request has to be terminated
2341 * and be put back to status queued, before the special request is added
2342 * to the head of the queue. Then the special request is waited on normally.
2344 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2346 struct dasd_ccw_req *cqr;
2347 int rc;
2349 if (list_empty(&device->ccw_queue))
2350 return 0;
2351 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2352 rc = device->discipline->term_IO(cqr);
2353 if (!rc)
2355 * CQR terminated because a more important request is pending.
2356 * Undo decreasing of retry counter because this is
2357 * not an error case.
2359 cqr->retries++;
2360 return rc;
2363 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2365 struct dasd_device *device;
2366 int rc;
2368 device = cqr->startdev;
2369 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2370 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2371 cqr->status = DASD_CQR_FAILED;
2372 cqr->intrc = -EPERM;
2373 return -EIO;
2375 spin_lock_irq(get_ccwdev_lock(device->cdev));
2376 rc = _dasd_term_running_cqr(device);
2377 if (rc) {
2378 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2379 return rc;
2381 cqr->callback = dasd_wakeup_cb;
2382 cqr->callback_data = DASD_SLEEPON_START_TAG;
2383 cqr->status = DASD_CQR_QUEUED;
2385 * add new request as second
2386 * first the terminated cqr needs to be finished
2388 list_add(&cqr->devlist, device->ccw_queue.next);
2390 /* let the bh start the request to keep them in order */
2391 dasd_schedule_device_bh(device);
2393 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2395 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2397 if (cqr->status == DASD_CQR_DONE)
2398 rc = 0;
2399 else if (cqr->intrc)
2400 rc = cqr->intrc;
2401 else
2402 rc = -EIO;
2404 /* kick tasklets */
2405 dasd_schedule_device_bh(device);
2406 if (device->block)
2407 dasd_schedule_block_bh(device->block);
2409 return rc;
2413 * Cancels a request that was started with dasd_sleep_on_req.
2414 * This is useful to timeout requests. The request will be
2415 * terminated if it is currently in i/o.
2416 * Returns 0 if request termination was successful
2417 * negative error code if termination failed
2418 * Cancellation of a request is an asynchronous operation! The calling
2419 * function has to wait until the request is properly returned via callback.
2421 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2423 struct dasd_device *device = cqr->startdev;
2424 unsigned long flags;
2425 int rc;
2427 rc = 0;
2428 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2429 switch (cqr->status) {
2430 case DASD_CQR_QUEUED:
2431 /* request was not started - just set to cleared */
2432 cqr->status = DASD_CQR_CLEARED;
2433 break;
2434 case DASD_CQR_IN_IO:
2435 /* request in IO - terminate IO and release again */
2436 rc = device->discipline->term_IO(cqr);
2437 if (rc) {
2438 dev_err(&device->cdev->dev,
2439 "Cancelling request %p failed with rc=%d\n",
2440 cqr, rc);
2441 } else {
2442 cqr->stopclk = get_tod_clock();
2444 break;
2445 default: /* already finished or clear pending - do nothing */
2446 break;
2448 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2449 dasd_schedule_device_bh(device);
2450 return rc;
2454 * SECTION: Operations of the dasd_block layer.
2458 * Timeout function for dasd_block. This is used when the block layer
2459 * is waiting for something that may not come reliably, (e.g. a state
2460 * change interrupt)
2462 static void dasd_block_timeout(unsigned long ptr)
2464 unsigned long flags;
2465 struct dasd_block *block;
2467 block = (struct dasd_block *) ptr;
2468 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2469 /* re-activate request queue */
2470 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2471 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2472 dasd_schedule_block_bh(block);
2476 * Setup timeout for a dasd_block in jiffies.
2478 void dasd_block_set_timer(struct dasd_block *block, int expires)
2480 if (expires == 0)
2481 del_timer(&block->timer);
2482 else
2483 mod_timer(&block->timer, jiffies + expires);
2487 * Clear timeout for a dasd_block.
2489 void dasd_block_clear_timer(struct dasd_block *block)
2491 del_timer(&block->timer);
2495 * Process finished error recovery ccw.
2497 static void __dasd_process_erp(struct dasd_device *device,
2498 struct dasd_ccw_req *cqr)
2500 dasd_erp_fn_t erp_fn;
2502 if (cqr->status == DASD_CQR_DONE)
2503 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2504 else
2505 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2506 erp_fn = device->discipline->erp_postaction(cqr);
2507 erp_fn(cqr);
2511 * Fetch requests from the block device queue.
2513 static void __dasd_process_request_queue(struct dasd_block *block)
2515 struct request_queue *queue;
2516 struct request *req;
2517 struct dasd_ccw_req *cqr;
2518 struct dasd_device *basedev;
2519 unsigned long flags;
2520 queue = block->request_queue;
2521 basedev = block->base;
2522 /* No queue ? Then there is nothing to do. */
2523 if (queue == NULL)
2524 return;
2527 * We requeue request from the block device queue to the ccw
2528 * queue only in two states. In state DASD_STATE_READY the
2529 * partition detection is done and we need to requeue requests
2530 * for that. State DASD_STATE_ONLINE is normal block device
2531 * operation.
2533 if (basedev->state < DASD_STATE_READY) {
2534 while ((req = blk_fetch_request(block->request_queue)))
2535 __blk_end_request_all(req, -EIO);
2536 return;
2538 /* Now we try to fetch requests from the request queue */
2539 while ((req = blk_peek_request(queue))) {
2540 if (basedev->features & DASD_FEATURE_READONLY &&
2541 rq_data_dir(req) == WRITE) {
2542 DBF_DEV_EVENT(DBF_ERR, basedev,
2543 "Rejecting write request %p",
2544 req);
2545 blk_start_request(req);
2546 __blk_end_request_all(req, -EIO);
2547 continue;
2549 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2550 (basedev->features & DASD_FEATURE_FAILFAST ||
2551 blk_noretry_request(req))) {
2552 DBF_DEV_EVENT(DBF_ERR, basedev,
2553 "Rejecting failfast request %p",
2554 req);
2555 blk_start_request(req);
2556 __blk_end_request_all(req, -ETIMEDOUT);
2557 continue;
2559 cqr = basedev->discipline->build_cp(basedev, block, req);
2560 if (IS_ERR(cqr)) {
2561 if (PTR_ERR(cqr) == -EBUSY)
2562 break; /* normal end condition */
2563 if (PTR_ERR(cqr) == -ENOMEM)
2564 break; /* terminate request queue loop */
2565 if (PTR_ERR(cqr) == -EAGAIN) {
2567 * The current request cannot be build right
2568 * now, we have to try later. If this request
2569 * is the head-of-queue we stop the device
2570 * for 1/2 second.
2572 if (!list_empty(&block->ccw_queue))
2573 break;
2574 spin_lock_irqsave(
2575 get_ccwdev_lock(basedev->cdev), flags);
2576 dasd_device_set_stop_bits(basedev,
2577 DASD_STOPPED_PENDING);
2578 spin_unlock_irqrestore(
2579 get_ccwdev_lock(basedev->cdev), flags);
2580 dasd_block_set_timer(block, HZ/2);
2581 break;
2583 DBF_DEV_EVENT(DBF_ERR, basedev,
2584 "CCW creation failed (rc=%ld) "
2585 "on request %p",
2586 PTR_ERR(cqr), req);
2587 blk_start_request(req);
2588 __blk_end_request_all(req, -EIO);
2589 continue;
2592 * Note: callback is set to dasd_return_cqr_cb in
2593 * __dasd_block_start_head to cover erp requests as well
2595 cqr->callback_data = (void *) req;
2596 cqr->status = DASD_CQR_FILLED;
2597 req->completion_data = cqr;
2598 blk_start_request(req);
2599 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2600 INIT_LIST_HEAD(&cqr->devlist);
2601 dasd_profile_start(block, cqr, req);
2605 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2607 struct request *req;
2608 int status;
2609 int error = 0;
2611 req = (struct request *) cqr->callback_data;
2612 dasd_profile_end(cqr->block, cqr, req);
2613 status = cqr->block->base->discipline->free_cp(cqr, req);
2614 if (status < 0)
2615 error = status;
2616 else if (status == 0) {
2617 if (cqr->intrc == -EPERM)
2618 error = -EBADE;
2619 else if (cqr->intrc == -ENOLINK ||
2620 cqr->intrc == -ETIMEDOUT)
2621 error = cqr->intrc;
2622 else
2623 error = -EIO;
2625 __blk_end_request_all(req, error);
2629 * Process ccw request queue.
2631 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2632 struct list_head *final_queue)
2634 struct list_head *l, *n;
2635 struct dasd_ccw_req *cqr;
2636 dasd_erp_fn_t erp_fn;
2637 unsigned long flags;
2638 struct dasd_device *base = block->base;
2640 restart:
2641 /* Process request with final status. */
2642 list_for_each_safe(l, n, &block->ccw_queue) {
2643 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2644 if (cqr->status != DASD_CQR_DONE &&
2645 cqr->status != DASD_CQR_FAILED &&
2646 cqr->status != DASD_CQR_NEED_ERP &&
2647 cqr->status != DASD_CQR_TERMINATED)
2648 continue;
2650 if (cqr->status == DASD_CQR_TERMINATED) {
2651 base->discipline->handle_terminated_request(cqr);
2652 goto restart;
2655 /* Process requests that may be recovered */
2656 if (cqr->status == DASD_CQR_NEED_ERP) {
2657 erp_fn = base->discipline->erp_action(cqr);
2658 if (IS_ERR(erp_fn(cqr)))
2659 continue;
2660 goto restart;
2663 /* log sense for fatal error */
2664 if (cqr->status == DASD_CQR_FAILED) {
2665 dasd_log_sense(cqr, &cqr->irb);
2668 /* First of all call extended error reporting. */
2669 if (dasd_eer_enabled(base) &&
2670 cqr->status == DASD_CQR_FAILED) {
2671 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2673 /* restart request */
2674 cqr->status = DASD_CQR_FILLED;
2675 cqr->retries = 255;
2676 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2677 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2678 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2679 flags);
2680 goto restart;
2683 /* Process finished ERP request. */
2684 if (cqr->refers) {
2685 __dasd_process_erp(base, cqr);
2686 goto restart;
2689 /* Rechain finished requests to final queue */
2690 cqr->endclk = get_tod_clock();
2691 list_move_tail(&cqr->blocklist, final_queue);
2695 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2697 dasd_schedule_block_bh(cqr->block);
2700 static void __dasd_block_start_head(struct dasd_block *block)
2702 struct dasd_ccw_req *cqr;
2704 if (list_empty(&block->ccw_queue))
2705 return;
2706 /* We allways begin with the first requests on the queue, as some
2707 * of previously started requests have to be enqueued on a
2708 * dasd_device again for error recovery.
2710 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2711 if (cqr->status != DASD_CQR_FILLED)
2712 continue;
2713 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2714 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2715 cqr->status = DASD_CQR_FAILED;
2716 cqr->intrc = -EPERM;
2717 dasd_schedule_block_bh(block);
2718 continue;
2720 /* Non-temporary stop condition will trigger fail fast */
2721 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2722 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2723 (!dasd_eer_enabled(block->base))) {
2724 cqr->status = DASD_CQR_FAILED;
2725 cqr->intrc = -ENOLINK;
2726 dasd_schedule_block_bh(block);
2727 continue;
2729 /* Don't try to start requests if device is stopped */
2730 if (block->base->stopped)
2731 return;
2733 /* just a fail safe check, should not happen */
2734 if (!cqr->startdev)
2735 cqr->startdev = block->base;
2737 /* make sure that the requests we submit find their way back */
2738 cqr->callback = dasd_return_cqr_cb;
2740 dasd_add_request_tail(cqr);
2745 * Central dasd_block layer routine. Takes requests from the generic
2746 * block layer request queue, creates ccw requests, enqueues them on
2747 * a dasd_device and processes ccw requests that have been returned.
2749 static void dasd_block_tasklet(struct dasd_block *block)
2751 struct list_head final_queue;
2752 struct list_head *l, *n;
2753 struct dasd_ccw_req *cqr;
2755 atomic_set(&block->tasklet_scheduled, 0);
2756 INIT_LIST_HEAD(&final_queue);
2757 spin_lock(&block->queue_lock);
2758 /* Finish off requests on ccw queue */
2759 __dasd_process_block_ccw_queue(block, &final_queue);
2760 spin_unlock(&block->queue_lock);
2761 /* Now call the callback function of requests with final status */
2762 spin_lock_irq(&block->request_queue_lock);
2763 list_for_each_safe(l, n, &final_queue) {
2764 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2765 list_del_init(&cqr->blocklist);
2766 __dasd_cleanup_cqr(cqr);
2768 spin_lock(&block->queue_lock);
2769 /* Get new request from the block device request queue */
2770 __dasd_process_request_queue(block);
2771 /* Now check if the head of the ccw queue needs to be started. */
2772 __dasd_block_start_head(block);
2773 spin_unlock(&block->queue_lock);
2774 spin_unlock_irq(&block->request_queue_lock);
2775 if (waitqueue_active(&shutdown_waitq))
2776 wake_up(&shutdown_waitq);
2777 dasd_put_device(block->base);
2780 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2782 wake_up(&dasd_flush_wq);
2786 * Requeue a request back to the block request queue
2787 * only works for block requests
2789 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2791 struct dasd_block *block = cqr->block;
2792 struct request *req;
2793 unsigned long flags;
2795 if (!block)
2796 return -EINVAL;
2797 spin_lock_irqsave(&block->queue_lock, flags);
2798 req = (struct request *) cqr->callback_data;
2799 blk_requeue_request(block->request_queue, req);
2800 spin_unlock_irqrestore(&block->queue_lock, flags);
2802 return 0;
2806 * Go through all request on the dasd_block request queue, cancel them
2807 * on the respective dasd_device, and return them to the generic
2808 * block layer.
2810 static int dasd_flush_block_queue(struct dasd_block *block)
2812 struct dasd_ccw_req *cqr, *n;
2813 int rc, i;
2814 struct list_head flush_queue;
2816 INIT_LIST_HEAD(&flush_queue);
2817 spin_lock_bh(&block->queue_lock);
2818 rc = 0;
2819 restart:
2820 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2821 /* if this request currently owned by a dasd_device cancel it */
2822 if (cqr->status >= DASD_CQR_QUEUED)
2823 rc = dasd_cancel_req(cqr);
2824 if (rc < 0)
2825 break;
2826 /* Rechain request (including erp chain) so it won't be
2827 * touched by the dasd_block_tasklet anymore.
2828 * Replace the callback so we notice when the request
2829 * is returned from the dasd_device layer.
2831 cqr->callback = _dasd_wake_block_flush_cb;
2832 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2833 list_move_tail(&cqr->blocklist, &flush_queue);
2834 if (i > 1)
2835 /* moved more than one request - need to restart */
2836 goto restart;
2838 spin_unlock_bh(&block->queue_lock);
2839 /* Now call the callback function of flushed requests */
2840 restart_cb:
2841 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2842 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2843 /* Process finished ERP request. */
2844 if (cqr->refers) {
2845 spin_lock_bh(&block->queue_lock);
2846 __dasd_process_erp(block->base, cqr);
2847 spin_unlock_bh(&block->queue_lock);
2848 /* restart list_for_xx loop since dasd_process_erp
2849 * might remove multiple elements */
2850 goto restart_cb;
2852 /* call the callback function */
2853 spin_lock_irq(&block->request_queue_lock);
2854 cqr->endclk = get_tod_clock();
2855 list_del_init(&cqr->blocklist);
2856 __dasd_cleanup_cqr(cqr);
2857 spin_unlock_irq(&block->request_queue_lock);
2859 return rc;
2863 * Schedules a call to dasd_tasklet over the device tasklet.
2865 void dasd_schedule_block_bh(struct dasd_block *block)
2867 /* Protect against rescheduling. */
2868 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2869 return;
2870 /* life cycle of block is bound to it's base device */
2871 dasd_get_device(block->base);
2872 tasklet_hi_schedule(&block->tasklet);
2877 * SECTION: external block device operations
2878 * (request queue handling, open, release, etc.)
2882 * Dasd request queue function. Called from ll_rw_blk.c
2884 static void do_dasd_request(struct request_queue *queue)
2886 struct dasd_block *block;
2888 block = queue->queuedata;
2889 spin_lock(&block->queue_lock);
2890 /* Get new request from the block device request queue */
2891 __dasd_process_request_queue(block);
2892 /* Now check if the head of the ccw queue needs to be started. */
2893 __dasd_block_start_head(block);
2894 spin_unlock(&block->queue_lock);
2898 * Block timeout callback, called from the block layer
2900 * request_queue lock is held on entry.
2902 * Return values:
2903 * BLK_EH_RESET_TIMER if the request should be left running
2904 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2905 * by the driver.
2907 enum blk_eh_timer_return dasd_times_out(struct request *req)
2909 struct dasd_ccw_req *cqr = req->completion_data;
2910 struct dasd_block *block = req->q->queuedata;
2911 struct dasd_device *device;
2912 int rc = 0;
2914 if (!cqr)
2915 return BLK_EH_NOT_HANDLED;
2917 device = cqr->startdev ? cqr->startdev : block->base;
2918 if (!device->blk_timeout)
2919 return BLK_EH_RESET_TIMER;
2920 DBF_DEV_EVENT(DBF_WARNING, device,
2921 " dasd_times_out cqr %p status %x",
2922 cqr, cqr->status);
2924 spin_lock(&block->queue_lock);
2925 spin_lock(get_ccwdev_lock(device->cdev));
2926 cqr->retries = -1;
2927 cqr->intrc = -ETIMEDOUT;
2928 if (cqr->status >= DASD_CQR_QUEUED) {
2929 spin_unlock(get_ccwdev_lock(device->cdev));
2930 rc = dasd_cancel_req(cqr);
2931 } else if (cqr->status == DASD_CQR_FILLED ||
2932 cqr->status == DASD_CQR_NEED_ERP) {
2933 cqr->status = DASD_CQR_TERMINATED;
2934 spin_unlock(get_ccwdev_lock(device->cdev));
2935 } else if (cqr->status == DASD_CQR_IN_ERP) {
2936 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2938 list_for_each_entry_safe(searchcqr, nextcqr,
2939 &block->ccw_queue, blocklist) {
2940 tmpcqr = searchcqr;
2941 while (tmpcqr->refers)
2942 tmpcqr = tmpcqr->refers;
2943 if (tmpcqr != cqr)
2944 continue;
2945 /* searchcqr is an ERP request for cqr */
2946 searchcqr->retries = -1;
2947 searchcqr->intrc = -ETIMEDOUT;
2948 if (searchcqr->status >= DASD_CQR_QUEUED) {
2949 spin_unlock(get_ccwdev_lock(device->cdev));
2950 rc = dasd_cancel_req(searchcqr);
2951 spin_lock(get_ccwdev_lock(device->cdev));
2952 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2953 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2954 searchcqr->status = DASD_CQR_TERMINATED;
2955 rc = 0;
2956 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2958 * Shouldn't happen; most recent ERP
2959 * request is at the front of queue
2961 continue;
2963 break;
2965 spin_unlock(get_ccwdev_lock(device->cdev));
2967 dasd_schedule_block_bh(block);
2968 spin_unlock(&block->queue_lock);
2970 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2974 * Allocate and initialize request queue and default I/O scheduler.
2976 static int dasd_alloc_queue(struct dasd_block *block)
2978 int rc;
2980 block->request_queue = blk_init_queue(do_dasd_request,
2981 &block->request_queue_lock);
2982 if (block->request_queue == NULL)
2983 return -ENOMEM;
2985 block->request_queue->queuedata = block;
2987 elevator_exit(block->request_queue->elevator);
2988 block->request_queue->elevator = NULL;
2989 mutex_lock(&block->request_queue->sysfs_lock);
2990 rc = elevator_init(block->request_queue, "deadline");
2991 if (rc)
2992 blk_cleanup_queue(block->request_queue);
2993 mutex_unlock(&block->request_queue->sysfs_lock);
2994 return rc;
2998 * Allocate and initialize request queue.
3000 static void dasd_setup_queue(struct dasd_block *block)
3002 int max;
3004 if (block->base->features & DASD_FEATURE_USERAW) {
3006 * the max_blocks value for raw_track access is 256
3007 * it is higher than the native ECKD value because we
3008 * only need one ccw per track
3009 * so the max_hw_sectors are
3010 * 2048 x 512B = 1024kB = 16 tracks
3012 max = 2048;
3013 } else {
3014 max = block->base->discipline->max_blocks << block->s2b_shift;
3016 blk_queue_logical_block_size(block->request_queue,
3017 block->bp_block);
3018 blk_queue_max_hw_sectors(block->request_queue, max);
3019 blk_queue_max_segments(block->request_queue, -1L);
3020 /* with page sized segments we can translate each segement into
3021 * one idaw/tidaw
3023 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3024 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
3028 * Deactivate and free request queue.
3030 static void dasd_free_queue(struct dasd_block *block)
3032 if (block->request_queue) {
3033 blk_cleanup_queue(block->request_queue);
3034 block->request_queue = NULL;
3039 * Flush request on the request queue.
3041 static void dasd_flush_request_queue(struct dasd_block *block)
3043 struct request *req;
3045 if (!block->request_queue)
3046 return;
3048 spin_lock_irq(&block->request_queue_lock);
3049 while ((req = blk_fetch_request(block->request_queue)))
3050 __blk_end_request_all(req, -EIO);
3051 spin_unlock_irq(&block->request_queue_lock);
3054 static int dasd_open(struct block_device *bdev, fmode_t mode)
3056 struct dasd_device *base;
3057 int rc;
3059 base = dasd_device_from_gendisk(bdev->bd_disk);
3060 if (!base)
3061 return -ENODEV;
3063 atomic_inc(&base->block->open_count);
3064 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3065 rc = -ENODEV;
3066 goto unlock;
3069 if (!try_module_get(base->discipline->owner)) {
3070 rc = -EINVAL;
3071 goto unlock;
3074 if (dasd_probeonly) {
3075 dev_info(&base->cdev->dev,
3076 "Accessing the DASD failed because it is in "
3077 "probeonly mode\n");
3078 rc = -EPERM;
3079 goto out;
3082 if (base->state <= DASD_STATE_BASIC) {
3083 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3084 " Cannot open unrecognized device");
3085 rc = -ENODEV;
3086 goto out;
3089 if ((mode & FMODE_WRITE) &&
3090 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3091 (base->features & DASD_FEATURE_READONLY))) {
3092 rc = -EROFS;
3093 goto out;
3096 dasd_put_device(base);
3097 return 0;
3099 out:
3100 module_put(base->discipline->owner);
3101 unlock:
3102 atomic_dec(&base->block->open_count);
3103 dasd_put_device(base);
3104 return rc;
3107 static void dasd_release(struct gendisk *disk, fmode_t mode)
3109 struct dasd_device *base = dasd_device_from_gendisk(disk);
3110 if (base) {
3111 atomic_dec(&base->block->open_count);
3112 module_put(base->discipline->owner);
3113 dasd_put_device(base);
3118 * Return disk geometry.
3120 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3122 struct dasd_device *base;
3124 base = dasd_device_from_gendisk(bdev->bd_disk);
3125 if (!base)
3126 return -ENODEV;
3128 if (!base->discipline ||
3129 !base->discipline->fill_geometry) {
3130 dasd_put_device(base);
3131 return -EINVAL;
3133 base->discipline->fill_geometry(base->block, geo);
3134 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3135 dasd_put_device(base);
3136 return 0;
3139 const struct block_device_operations
3140 dasd_device_operations = {
3141 .owner = THIS_MODULE,
3142 .open = dasd_open,
3143 .release = dasd_release,
3144 .ioctl = dasd_ioctl,
3145 .compat_ioctl = dasd_ioctl,
3146 .getgeo = dasd_getgeo,
3149 /*******************************************************************************
3150 * end of block device operations
3153 static void
3154 dasd_exit(void)
3156 #ifdef CONFIG_PROC_FS
3157 dasd_proc_exit();
3158 #endif
3159 dasd_eer_exit();
3160 if (dasd_page_cache != NULL) {
3161 kmem_cache_destroy(dasd_page_cache);
3162 dasd_page_cache = NULL;
3164 dasd_gendisk_exit();
3165 dasd_devmap_exit();
3166 if (dasd_debug_area != NULL) {
3167 debug_unregister(dasd_debug_area);
3168 dasd_debug_area = NULL;
3170 dasd_statistics_removeroot();
3174 * SECTION: common functions for ccw_driver use
3178 * Is the device read-only?
3179 * Note that this function does not report the setting of the
3180 * readonly device attribute, but how it is configured in z/VM.
3182 int dasd_device_is_ro(struct dasd_device *device)
3184 struct ccw_dev_id dev_id;
3185 struct diag210 diag_data;
3186 int rc;
3188 if (!MACHINE_IS_VM)
3189 return 0;
3190 ccw_device_get_id(device->cdev, &dev_id);
3191 memset(&diag_data, 0, sizeof(diag_data));
3192 diag_data.vrdcdvno = dev_id.devno;
3193 diag_data.vrdclen = sizeof(diag_data);
3194 rc = diag210(&diag_data);
3195 if (rc == 0 || rc == 2) {
3196 return diag_data.vrdcvfla & 0x80;
3197 } else {
3198 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3199 dev_id.devno, rc);
3200 return 0;
3203 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3205 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3207 struct ccw_device *cdev = data;
3208 int ret;
3210 ret = ccw_device_set_online(cdev);
3211 if (ret)
3212 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3213 dev_name(&cdev->dev), ret);
3217 * Initial attempt at a probe function. this can be simplified once
3218 * the other detection code is gone.
3220 int dasd_generic_probe(struct ccw_device *cdev,
3221 struct dasd_discipline *discipline)
3223 int ret;
3225 ret = dasd_add_sysfs_files(cdev);
3226 if (ret) {
3227 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3228 "dasd_generic_probe: could not add "
3229 "sysfs entries");
3230 return ret;
3232 cdev->handler = &dasd_int_handler;
3235 * Automatically online either all dasd devices (dasd_autodetect)
3236 * or all devices specified with dasd= parameters during
3237 * initial probe.
3239 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3240 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3241 async_schedule(dasd_generic_auto_online, cdev);
3242 return 0;
3246 * This will one day be called from a global not_oper handler.
3247 * It is also used by driver_unregister during module unload.
3249 void dasd_generic_remove(struct ccw_device *cdev)
3251 struct dasd_device *device;
3252 struct dasd_block *block;
3254 cdev->handler = NULL;
3256 device = dasd_device_from_cdev(cdev);
3257 if (IS_ERR(device)) {
3258 dasd_remove_sysfs_files(cdev);
3259 return;
3261 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3262 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3263 /* Already doing offline processing */
3264 dasd_put_device(device);
3265 dasd_remove_sysfs_files(cdev);
3266 return;
3269 * This device is removed unconditionally. Set offline
3270 * flag to prevent dasd_open from opening it while it is
3271 * no quite down yet.
3273 dasd_set_target_state(device, DASD_STATE_NEW);
3274 /* dasd_delete_device destroys the device reference. */
3275 block = device->block;
3276 dasd_delete_device(device);
3278 * life cycle of block is bound to device, so delete it after
3279 * device was safely removed
3281 if (block)
3282 dasd_free_block(block);
3284 dasd_remove_sysfs_files(cdev);
3288 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3289 * the device is detected for the first time and is supposed to be used
3290 * or the user has started activation through sysfs.
3292 int dasd_generic_set_online(struct ccw_device *cdev,
3293 struct dasd_discipline *base_discipline)
3295 struct dasd_discipline *discipline;
3296 struct dasd_device *device;
3297 int rc;
3299 /* first online clears initial online feature flag */
3300 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3301 device = dasd_create_device(cdev);
3302 if (IS_ERR(device))
3303 return PTR_ERR(device);
3305 discipline = base_discipline;
3306 if (device->features & DASD_FEATURE_USEDIAG) {
3307 if (!dasd_diag_discipline_pointer) {
3308 pr_warning("%s Setting the DASD online failed because "
3309 "of missing DIAG discipline\n",
3310 dev_name(&cdev->dev));
3311 dasd_delete_device(device);
3312 return -ENODEV;
3314 discipline = dasd_diag_discipline_pointer;
3316 if (!try_module_get(base_discipline->owner)) {
3317 dasd_delete_device(device);
3318 return -EINVAL;
3320 if (!try_module_get(discipline->owner)) {
3321 module_put(base_discipline->owner);
3322 dasd_delete_device(device);
3323 return -EINVAL;
3325 device->base_discipline = base_discipline;
3326 device->discipline = discipline;
3328 /* check_device will allocate block device if necessary */
3329 rc = discipline->check_device(device);
3330 if (rc) {
3331 pr_warning("%s Setting the DASD online with discipline %s "
3332 "failed with rc=%i\n",
3333 dev_name(&cdev->dev), discipline->name, rc);
3334 module_put(discipline->owner);
3335 module_put(base_discipline->owner);
3336 dasd_delete_device(device);
3337 return rc;
3340 dasd_set_target_state(device, DASD_STATE_ONLINE);
3341 if (device->state <= DASD_STATE_KNOWN) {
3342 pr_warning("%s Setting the DASD online failed because of a "
3343 "missing discipline\n", dev_name(&cdev->dev));
3344 rc = -ENODEV;
3345 dasd_set_target_state(device, DASD_STATE_NEW);
3346 if (device->block)
3347 dasd_free_block(device->block);
3348 dasd_delete_device(device);
3349 } else
3350 pr_debug("dasd_generic device %s found\n",
3351 dev_name(&cdev->dev));
3353 wait_event(dasd_init_waitq, _wait_for_device(device));
3355 dasd_put_device(device);
3356 return rc;
3359 int dasd_generic_set_offline(struct ccw_device *cdev)
3361 struct dasd_device *device;
3362 struct dasd_block *block;
3363 int max_count, open_count, rc;
3365 rc = 0;
3366 device = dasd_device_from_cdev(cdev);
3367 if (IS_ERR(device))
3368 return PTR_ERR(device);
3371 * We must make sure that this device is currently not in use.
3372 * The open_count is increased for every opener, that includes
3373 * the blkdev_get in dasd_scan_partitions. We are only interested
3374 * in the other openers.
3376 if (device->block) {
3377 max_count = device->block->bdev ? 0 : -1;
3378 open_count = atomic_read(&device->block->open_count);
3379 if (open_count > max_count) {
3380 if (open_count > 0)
3381 pr_warning("%s: The DASD cannot be set offline "
3382 "with open count %i\n",
3383 dev_name(&cdev->dev), open_count);
3384 else
3385 pr_warning("%s: The DASD cannot be set offline "
3386 "while it is in use\n",
3387 dev_name(&cdev->dev));
3388 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3389 dasd_put_device(device);
3390 return -EBUSY;
3394 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3396 * safe offline allready running
3397 * could only be called by normal offline so safe_offline flag
3398 * needs to be removed to run normal offline and kill all I/O
3400 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3401 /* Already doing normal offline processing */
3402 dasd_put_device(device);
3403 return -EBUSY;
3404 } else
3405 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3407 } else
3408 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3409 /* Already doing offline processing */
3410 dasd_put_device(device);
3411 return -EBUSY;
3415 * if safe_offline called set safe_offline_running flag and
3416 * clear safe_offline so that a call to normal offline
3417 * can overrun safe_offline processing
3419 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3420 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3422 * If we want to set the device safe offline all IO operations
3423 * should be finished before continuing the offline process
3424 * so sync bdev first and then wait for our queues to become
3425 * empty
3427 /* sync blockdev and partitions */
3428 rc = fsync_bdev(device->block->bdev);
3429 if (rc != 0)
3430 goto interrupted;
3432 /* schedule device tasklet and wait for completion */
3433 dasd_schedule_device_bh(device);
3434 rc = wait_event_interruptible(shutdown_waitq,
3435 _wait_for_empty_queues(device));
3436 if (rc != 0)
3437 goto interrupted;
3440 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3441 dasd_set_target_state(device, DASD_STATE_NEW);
3442 /* dasd_delete_device destroys the device reference. */
3443 block = device->block;
3444 dasd_delete_device(device);
3446 * life cycle of block is bound to device, so delete it after
3447 * device was safely removed
3449 if (block)
3450 dasd_free_block(block);
3451 return 0;
3453 interrupted:
3454 /* interrupted by signal */
3455 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3456 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3457 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3458 dasd_put_device(device);
3459 return rc;
3462 int dasd_generic_last_path_gone(struct dasd_device *device)
3464 struct dasd_ccw_req *cqr;
3466 dev_warn(&device->cdev->dev, "No operational channel path is left "
3467 "for the device\n");
3468 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3469 /* First of all call extended error reporting. */
3470 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3472 if (device->state < DASD_STATE_BASIC)
3473 return 0;
3474 /* Device is active. We want to keep it. */
3475 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3476 if ((cqr->status == DASD_CQR_IN_IO) ||
3477 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3478 cqr->status = DASD_CQR_QUEUED;
3479 cqr->retries++;
3481 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3482 dasd_device_clear_timer(device);
3483 dasd_schedule_device_bh(device);
3484 return 1;
3486 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3488 int dasd_generic_path_operational(struct dasd_device *device)
3490 dev_info(&device->cdev->dev, "A channel path to the device has become "
3491 "operational\n");
3492 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3493 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3494 if (device->stopped & DASD_UNRESUMED_PM) {
3495 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3496 dasd_restore_device(device);
3497 return 1;
3499 dasd_schedule_device_bh(device);
3500 if (device->block)
3501 dasd_schedule_block_bh(device->block);
3502 return 1;
3504 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3506 int dasd_generic_notify(struct ccw_device *cdev, int event)
3508 struct dasd_device *device;
3509 int ret;
3511 device = dasd_device_from_cdev_locked(cdev);
3512 if (IS_ERR(device))
3513 return 0;
3514 ret = 0;
3515 switch (event) {
3516 case CIO_GONE:
3517 case CIO_BOXED:
3518 case CIO_NO_PATH:
3519 device->path_data.opm = 0;
3520 device->path_data.ppm = 0;
3521 device->path_data.npm = 0;
3522 ret = dasd_generic_last_path_gone(device);
3523 break;
3524 case CIO_OPER:
3525 ret = 1;
3526 if (device->path_data.opm)
3527 ret = dasd_generic_path_operational(device);
3528 break;
3530 dasd_put_device(device);
3531 return ret;
3534 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3536 int chp;
3537 __u8 oldopm, eventlpm;
3538 struct dasd_device *device;
3540 device = dasd_device_from_cdev_locked(cdev);
3541 if (IS_ERR(device))
3542 return;
3543 for (chp = 0; chp < 8; chp++) {
3544 eventlpm = 0x80 >> chp;
3545 if (path_event[chp] & PE_PATH_GONE) {
3546 oldopm = device->path_data.opm;
3547 device->path_data.opm &= ~eventlpm;
3548 device->path_data.ppm &= ~eventlpm;
3549 device->path_data.npm &= ~eventlpm;
3550 if (oldopm && !device->path_data.opm) {
3551 dev_warn(&device->cdev->dev,
3552 "No verified channel paths remain "
3553 "for the device\n");
3554 DBF_DEV_EVENT(DBF_WARNING, device,
3555 "%s", "last verified path gone");
3556 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3557 dasd_device_set_stop_bits(device,
3558 DASD_STOPPED_DC_WAIT);
3561 if (path_event[chp] & PE_PATH_AVAILABLE) {
3562 device->path_data.opm &= ~eventlpm;
3563 device->path_data.ppm &= ~eventlpm;
3564 device->path_data.npm &= ~eventlpm;
3565 device->path_data.tbvpm |= eventlpm;
3566 dasd_schedule_device_bh(device);
3568 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3569 if (!(device->path_data.opm & eventlpm) &&
3570 !(device->path_data.tbvpm & eventlpm)) {
3572 * we can not establish a pathgroup on an
3573 * unavailable path, so trigger a path
3574 * verification first
3576 device->path_data.tbvpm |= eventlpm;
3577 dasd_schedule_device_bh(device);
3579 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3580 "Pathgroup re-established\n");
3581 if (device->discipline->kick_validate)
3582 device->discipline->kick_validate(device);
3585 dasd_put_device(device);
3587 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3589 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3591 if (!device->path_data.opm && lpm) {
3592 device->path_data.opm = lpm;
3593 dasd_generic_path_operational(device);
3594 } else
3595 device->path_data.opm |= lpm;
3596 return 0;
3598 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3601 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3603 struct dasd_device *device = dasd_device_from_cdev(cdev);
3604 struct list_head freeze_queue;
3605 struct dasd_ccw_req *cqr, *n;
3606 struct dasd_ccw_req *refers;
3607 int rc;
3609 if (IS_ERR(device))
3610 return PTR_ERR(device);
3612 /* mark device as suspended */
3613 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3615 if (device->discipline->freeze)
3616 rc = device->discipline->freeze(device);
3618 /* disallow new I/O */
3619 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3621 /* clear active requests and requeue them to block layer if possible */
3622 INIT_LIST_HEAD(&freeze_queue);
3623 spin_lock_irq(get_ccwdev_lock(cdev));
3624 rc = 0;
3625 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3626 /* Check status and move request to flush_queue */
3627 if (cqr->status == DASD_CQR_IN_IO) {
3628 rc = device->discipline->term_IO(cqr);
3629 if (rc) {
3630 /* unable to terminate requeust */
3631 dev_err(&device->cdev->dev,
3632 "Unable to terminate request %p "
3633 "on suspend\n", cqr);
3634 spin_unlock_irq(get_ccwdev_lock(cdev));
3635 dasd_put_device(device);
3636 return rc;
3639 list_move_tail(&cqr->devlist, &freeze_queue);
3641 spin_unlock_irq(get_ccwdev_lock(cdev));
3643 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3644 wait_event(dasd_flush_wq,
3645 (cqr->status != DASD_CQR_CLEAR_PENDING));
3646 if (cqr->status == DASD_CQR_CLEARED)
3647 cqr->status = DASD_CQR_QUEUED;
3649 /* requeue requests to blocklayer will only work for
3650 block device requests */
3651 if (_dasd_requeue_request(cqr))
3652 continue;
3654 /* remove requests from device and block queue */
3655 list_del_init(&cqr->devlist);
3656 while (cqr->refers != NULL) {
3657 refers = cqr->refers;
3658 /* remove the request from the block queue */
3659 list_del(&cqr->blocklist);
3660 /* free the finished erp request */
3661 dasd_free_erp_request(cqr, cqr->memdev);
3662 cqr = refers;
3664 if (cqr->block)
3665 list_del_init(&cqr->blocklist);
3666 cqr->block->base->discipline->free_cp(
3667 cqr, (struct request *) cqr->callback_data);
3671 * if requests remain then they are internal request
3672 * and go back to the device queue
3674 if (!list_empty(&freeze_queue)) {
3675 /* move freeze_queue to start of the ccw_queue */
3676 spin_lock_irq(get_ccwdev_lock(cdev));
3677 list_splice_tail(&freeze_queue, &device->ccw_queue);
3678 spin_unlock_irq(get_ccwdev_lock(cdev));
3680 dasd_put_device(device);
3681 return rc;
3683 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3685 int dasd_generic_restore_device(struct ccw_device *cdev)
3687 struct dasd_device *device = dasd_device_from_cdev(cdev);
3688 int rc = 0;
3690 if (IS_ERR(device))
3691 return PTR_ERR(device);
3693 /* allow new IO again */
3694 dasd_device_remove_stop_bits(device,
3695 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3697 dasd_schedule_device_bh(device);
3700 * call discipline restore function
3701 * if device is stopped do nothing e.g. for disconnected devices
3703 if (device->discipline->restore && !(device->stopped))
3704 rc = device->discipline->restore(device);
3705 if (rc || device->stopped)
3707 * if the resume failed for the DASD we put it in
3708 * an UNRESUMED stop state
3710 device->stopped |= DASD_UNRESUMED_PM;
3712 if (device->block)
3713 dasd_schedule_block_bh(device->block);
3715 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3716 dasd_put_device(device);
3717 return 0;
3719 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3721 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3722 void *rdc_buffer,
3723 int rdc_buffer_size,
3724 int magic)
3726 struct dasd_ccw_req *cqr;
3727 struct ccw1 *ccw;
3728 unsigned long *idaw;
3730 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3732 if (IS_ERR(cqr)) {
3733 /* internal error 13 - Allocating the RDC request failed*/
3734 dev_err(&device->cdev->dev,
3735 "An error occurred in the DASD device driver, "
3736 "reason=%s\n", "13");
3737 return cqr;
3740 ccw = cqr->cpaddr;
3741 ccw->cmd_code = CCW_CMD_RDC;
3742 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3743 idaw = (unsigned long *) (cqr->data);
3744 ccw->cda = (__u32)(addr_t) idaw;
3745 ccw->flags = CCW_FLAG_IDA;
3746 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3747 } else {
3748 ccw->cda = (__u32)(addr_t) rdc_buffer;
3749 ccw->flags = 0;
3752 ccw->count = rdc_buffer_size;
3753 cqr->startdev = device;
3754 cqr->memdev = device;
3755 cqr->expires = 10*HZ;
3756 cqr->retries = 256;
3757 cqr->buildclk = get_tod_clock();
3758 cqr->status = DASD_CQR_FILLED;
3759 return cqr;
3763 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3764 void *rdc_buffer, int rdc_buffer_size)
3766 int ret;
3767 struct dasd_ccw_req *cqr;
3769 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3770 magic);
3771 if (IS_ERR(cqr))
3772 return PTR_ERR(cqr);
3774 ret = dasd_sleep_on(cqr);
3775 dasd_sfree_request(cqr, cqr->memdev);
3776 return ret;
3778 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3781 * In command mode and transport mode we need to look for sense
3782 * data in different places. The sense data itself is allways
3783 * an array of 32 bytes, so we can unify the sense data access
3784 * for both modes.
3786 char *dasd_get_sense(struct irb *irb)
3788 struct tsb *tsb = NULL;
3789 char *sense = NULL;
3791 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3792 if (irb->scsw.tm.tcw)
3793 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3794 irb->scsw.tm.tcw);
3795 if (tsb && tsb->length == 64 && tsb->flags)
3796 switch (tsb->flags & 0x07) {
3797 case 1: /* tsa_iostat */
3798 sense = tsb->tsa.iostat.sense;
3799 break;
3800 case 2: /* tsa_ddpc */
3801 sense = tsb->tsa.ddpc.sense;
3802 break;
3803 default:
3804 /* currently we don't use interrogate data */
3805 break;
3807 } else if (irb->esw.esw0.erw.cons) {
3808 sense = irb->ecw;
3810 return sense;
3812 EXPORT_SYMBOL_GPL(dasd_get_sense);
3814 void dasd_generic_shutdown(struct ccw_device *cdev)
3816 struct dasd_device *device;
3818 device = dasd_device_from_cdev(cdev);
3819 if (IS_ERR(device))
3820 return;
3822 if (device->block)
3823 dasd_schedule_block_bh(device->block);
3825 dasd_schedule_device_bh(device);
3827 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3829 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3831 static int __init dasd_init(void)
3833 int rc;
3835 init_waitqueue_head(&dasd_init_waitq);
3836 init_waitqueue_head(&dasd_flush_wq);
3837 init_waitqueue_head(&generic_waitq);
3838 init_waitqueue_head(&shutdown_waitq);
3840 /* register 'common' DASD debug area, used for all DBF_XXX calls */
3841 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3842 if (dasd_debug_area == NULL) {
3843 rc = -ENOMEM;
3844 goto failed;
3846 debug_register_view(dasd_debug_area, &debug_sprintf_view);
3847 debug_set_level(dasd_debug_area, DBF_WARNING);
3849 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3851 dasd_diag_discipline_pointer = NULL;
3853 dasd_statistics_createroot();
3855 rc = dasd_devmap_init();
3856 if (rc)
3857 goto failed;
3858 rc = dasd_gendisk_init();
3859 if (rc)
3860 goto failed;
3861 rc = dasd_parse();
3862 if (rc)
3863 goto failed;
3864 rc = dasd_eer_init();
3865 if (rc)
3866 goto failed;
3867 #ifdef CONFIG_PROC_FS
3868 rc = dasd_proc_init();
3869 if (rc)
3870 goto failed;
3871 #endif
3873 return 0;
3874 failed:
3875 pr_info("The DASD device driver could not be initialized\n");
3876 dasd_exit();
3877 return rc;
3880 module_init(dasd_init);
3881 module_exit(dasd_exit);
3883 EXPORT_SYMBOL(dasd_debug_area);
3884 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3886 EXPORT_SYMBOL(dasd_add_request_head);
3887 EXPORT_SYMBOL(dasd_add_request_tail);
3888 EXPORT_SYMBOL(dasd_cancel_req);
3889 EXPORT_SYMBOL(dasd_device_clear_timer);
3890 EXPORT_SYMBOL(dasd_block_clear_timer);
3891 EXPORT_SYMBOL(dasd_enable_device);
3892 EXPORT_SYMBOL(dasd_int_handler);
3893 EXPORT_SYMBOL(dasd_kfree_request);
3894 EXPORT_SYMBOL(dasd_kick_device);
3895 EXPORT_SYMBOL(dasd_kmalloc_request);
3896 EXPORT_SYMBOL(dasd_schedule_device_bh);
3897 EXPORT_SYMBOL(dasd_schedule_block_bh);
3898 EXPORT_SYMBOL(dasd_set_target_state);
3899 EXPORT_SYMBOL(dasd_device_set_timer);
3900 EXPORT_SYMBOL(dasd_block_set_timer);
3901 EXPORT_SYMBOL(dasd_sfree_request);
3902 EXPORT_SYMBOL(dasd_sleep_on);
3903 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3904 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3905 EXPORT_SYMBOL(dasd_smalloc_request);
3906 EXPORT_SYMBOL(dasd_start_IO);
3907 EXPORT_SYMBOL(dasd_term_IO);
3909 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3910 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3911 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3912 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3913 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3914 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3915 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3916 EXPORT_SYMBOL_GPL(dasd_alloc_block);
3917 EXPORT_SYMBOL_GPL(dasd_free_block);