Merge branch 'for-linus' of git://git.infradead.org/users/sameo/mfd-2.6
[linux-btrfs-devel.git] / drivers / s390 / block / dasd.c
bloba1d3ddba99ccad4b90d4a9a06af1fd371b2d0033
1 /*
2 * File...........: linux/drivers/s390/block/dasd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
8 * Copyright IBM Corp. 1999, 2009
9 */
11 #define KMSG_COMPONENT "dasd"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kernel_stat.h>
15 #include <linux/kmod.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ctype.h>
19 #include <linux/major.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/hdreg.h>
23 #include <linux/async.h>
24 #include <linux/mutex.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/vmalloc.h>
29 #include <asm/ccwdev.h>
30 #include <asm/ebcdic.h>
31 #include <asm/idals.h>
32 #include <asm/itcw.h>
33 #include <asm/diag.h>
35 /* This is ugly... */
36 #define PRINTK_HEADER "dasd:"
38 #include "dasd_int.h"
40 * SECTION: Constant definitions to be used within this file
42 #define DASD_CHANQ_MAX_SIZE 4
44 #define DASD_SLEEPON_START_TAG (void *) 1
45 #define DASD_SLEEPON_END_TAG (void *) 2
48 * SECTION: exported variables of dasd.c
50 debug_info_t *dasd_debug_area;
51 static struct dentry *dasd_debugfs_root_entry;
52 struct dasd_discipline *dasd_diag_discipline_pointer;
53 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
55 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
56 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
57 " Copyright 2000 IBM Corporation");
58 MODULE_SUPPORTED_DEVICE("dasd");
59 MODULE_LICENSE("GPL");
62 * SECTION: prototypes for static functions of dasd.c
64 static int dasd_alloc_queue(struct dasd_block *);
65 static void dasd_setup_queue(struct dasd_block *);
66 static void dasd_free_queue(struct dasd_block *);
67 static void dasd_flush_request_queue(struct dasd_block *);
68 static int dasd_flush_block_queue(struct dasd_block *);
69 static void dasd_device_tasklet(struct dasd_device *);
70 static void dasd_block_tasklet(struct dasd_block *);
71 static void do_kick_device(struct work_struct *);
72 static void do_restore_device(struct work_struct *);
73 static void do_reload_device(struct work_struct *);
74 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
75 static void dasd_device_timeout(unsigned long);
76 static void dasd_block_timeout(unsigned long);
77 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
78 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
79 static void dasd_profile_exit(struct dasd_profile *);
82 * SECTION: Operations on the device structure.
84 static wait_queue_head_t dasd_init_waitq;
85 static wait_queue_head_t dasd_flush_wq;
86 static wait_queue_head_t generic_waitq;
89 * Allocate memory for a new device structure.
91 struct dasd_device *dasd_alloc_device(void)
93 struct dasd_device *device;
95 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
96 if (!device)
97 return ERR_PTR(-ENOMEM);
99 /* Get two pages for normal block device operations. */
100 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
101 if (!device->ccw_mem) {
102 kfree(device);
103 return ERR_PTR(-ENOMEM);
105 /* Get one page for error recovery. */
106 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
107 if (!device->erp_mem) {
108 free_pages((unsigned long) device->ccw_mem, 1);
109 kfree(device);
110 return ERR_PTR(-ENOMEM);
113 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
114 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
115 spin_lock_init(&device->mem_lock);
116 atomic_set(&device->tasklet_scheduled, 0);
117 tasklet_init(&device->tasklet,
118 (void (*)(unsigned long)) dasd_device_tasklet,
119 (unsigned long) device);
120 INIT_LIST_HEAD(&device->ccw_queue);
121 init_timer(&device->timer);
122 device->timer.function = dasd_device_timeout;
123 device->timer.data = (unsigned long) device;
124 INIT_WORK(&device->kick_work, do_kick_device);
125 INIT_WORK(&device->restore_device, do_restore_device);
126 INIT_WORK(&device->reload_device, do_reload_device);
127 device->state = DASD_STATE_NEW;
128 device->target = DASD_STATE_NEW;
129 mutex_init(&device->state_mutex);
130 spin_lock_init(&device->profile.lock);
131 return device;
135 * Free memory of a device structure.
137 void dasd_free_device(struct dasd_device *device)
139 kfree(device->private);
140 free_page((unsigned long) device->erp_mem);
141 free_pages((unsigned long) device->ccw_mem, 1);
142 kfree(device);
146 * Allocate memory for a new device structure.
148 struct dasd_block *dasd_alloc_block(void)
150 struct dasd_block *block;
152 block = kzalloc(sizeof(*block), GFP_ATOMIC);
153 if (!block)
154 return ERR_PTR(-ENOMEM);
155 /* open_count = 0 means device online but not in use */
156 atomic_set(&block->open_count, -1);
158 spin_lock_init(&block->request_queue_lock);
159 atomic_set(&block->tasklet_scheduled, 0);
160 tasklet_init(&block->tasklet,
161 (void (*)(unsigned long)) dasd_block_tasklet,
162 (unsigned long) block);
163 INIT_LIST_HEAD(&block->ccw_queue);
164 spin_lock_init(&block->queue_lock);
165 init_timer(&block->timer);
166 block->timer.function = dasd_block_timeout;
167 block->timer.data = (unsigned long) block;
168 spin_lock_init(&block->profile.lock);
170 return block;
174 * Free memory of a device structure.
176 void dasd_free_block(struct dasd_block *block)
178 kfree(block);
182 * Make a new device known to the system.
184 static int dasd_state_new_to_known(struct dasd_device *device)
186 int rc;
189 * As long as the device is not in state DASD_STATE_NEW we want to
190 * keep the reference count > 0.
192 dasd_get_device(device);
194 if (device->block) {
195 rc = dasd_alloc_queue(device->block);
196 if (rc) {
197 dasd_put_device(device);
198 return rc;
201 device->state = DASD_STATE_KNOWN;
202 return 0;
206 * Let the system forget about a device.
208 static int dasd_state_known_to_new(struct dasd_device *device)
210 /* Disable extended error reporting for this device. */
211 dasd_eer_disable(device);
212 /* Forget the discipline information. */
213 if (device->discipline) {
214 if (device->discipline->uncheck_device)
215 device->discipline->uncheck_device(device);
216 module_put(device->discipline->owner);
218 device->discipline = NULL;
219 if (device->base_discipline)
220 module_put(device->base_discipline->owner);
221 device->base_discipline = NULL;
222 device->state = DASD_STATE_NEW;
224 if (device->block)
225 dasd_free_queue(device->block);
227 /* Give up reference we took in dasd_state_new_to_known. */
228 dasd_put_device(device);
229 return 0;
232 static struct dentry *dasd_debugfs_setup(const char *name,
233 struct dentry *base_dentry)
235 struct dentry *pde;
237 if (!base_dentry)
238 return NULL;
239 pde = debugfs_create_dir(name, base_dentry);
240 if (!pde || IS_ERR(pde))
241 return NULL;
242 return pde;
246 * Request the irq line for the device.
248 static int dasd_state_known_to_basic(struct dasd_device *device)
250 struct dasd_block *block = device->block;
251 int rc;
253 /* Allocate and register gendisk structure. */
254 if (block) {
255 rc = dasd_gendisk_alloc(block);
256 if (rc)
257 return rc;
258 block->debugfs_dentry =
259 dasd_debugfs_setup(block->gdp->disk_name,
260 dasd_debugfs_root_entry);
261 dasd_profile_init(&block->profile, block->debugfs_dentry);
262 if (dasd_global_profile_level == DASD_PROFILE_ON)
263 dasd_profile_on(&device->block->profile);
265 device->debugfs_dentry =
266 dasd_debugfs_setup(dev_name(&device->cdev->dev),
267 dasd_debugfs_root_entry);
268 dasd_profile_init(&device->profile, device->debugfs_dentry);
270 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
271 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
272 8 * sizeof(long));
273 debug_register_view(device->debug_area, &debug_sprintf_view);
274 debug_set_level(device->debug_area, DBF_WARNING);
275 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
277 device->state = DASD_STATE_BASIC;
278 return 0;
282 * Release the irq line for the device. Terminate any running i/o.
284 static int dasd_state_basic_to_known(struct dasd_device *device)
286 int rc;
287 if (device->block) {
288 dasd_profile_exit(&device->block->profile);
289 if (device->block->debugfs_dentry)
290 debugfs_remove(device->block->debugfs_dentry);
291 dasd_gendisk_free(device->block);
292 dasd_block_clear_timer(device->block);
294 rc = dasd_flush_device_queue(device);
295 if (rc)
296 return rc;
297 dasd_device_clear_timer(device);
298 dasd_profile_exit(&device->profile);
299 if (device->debugfs_dentry)
300 debugfs_remove(device->debugfs_dentry);
302 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
303 if (device->debug_area != NULL) {
304 debug_unregister(device->debug_area);
305 device->debug_area = NULL;
307 device->state = DASD_STATE_KNOWN;
308 return 0;
312 * Do the initial analysis. The do_analysis function may return
313 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
314 * until the discipline decides to continue the startup sequence
315 * by calling the function dasd_change_state. The eckd disciplines
316 * uses this to start a ccw that detects the format. The completion
317 * interrupt for this detection ccw uses the kernel event daemon to
318 * trigger the call to dasd_change_state. All this is done in the
319 * discipline code, see dasd_eckd.c.
320 * After the analysis ccw is done (do_analysis returned 0) the block
321 * device is setup.
322 * In case the analysis returns an error, the device setup is stopped
323 * (a fake disk was already added to allow formatting).
325 static int dasd_state_basic_to_ready(struct dasd_device *device)
327 int rc;
328 struct dasd_block *block;
330 rc = 0;
331 block = device->block;
332 /* make disk known with correct capacity */
333 if (block) {
334 if (block->base->discipline->do_analysis != NULL)
335 rc = block->base->discipline->do_analysis(block);
336 if (rc) {
337 if (rc != -EAGAIN)
338 device->state = DASD_STATE_UNFMT;
339 return rc;
341 dasd_setup_queue(block);
342 set_capacity(block->gdp,
343 block->blocks << block->s2b_shift);
344 device->state = DASD_STATE_READY;
345 rc = dasd_scan_partitions(block);
346 if (rc)
347 device->state = DASD_STATE_BASIC;
348 } else {
349 device->state = DASD_STATE_READY;
351 return rc;
355 * Remove device from block device layer. Destroy dirty buffers.
356 * Forget format information. Check if the target level is basic
357 * and if it is create fake disk for formatting.
359 static int dasd_state_ready_to_basic(struct dasd_device *device)
361 int rc;
363 device->state = DASD_STATE_BASIC;
364 if (device->block) {
365 struct dasd_block *block = device->block;
366 rc = dasd_flush_block_queue(block);
367 if (rc) {
368 device->state = DASD_STATE_READY;
369 return rc;
371 dasd_flush_request_queue(block);
372 dasd_destroy_partitions(block);
373 block->blocks = 0;
374 block->bp_block = 0;
375 block->s2b_shift = 0;
377 return 0;
381 * Back to basic.
383 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
385 device->state = DASD_STATE_BASIC;
386 return 0;
390 * Make the device online and schedule the bottom half to start
391 * the requeueing of requests from the linux request queue to the
392 * ccw queue.
394 static int
395 dasd_state_ready_to_online(struct dasd_device * device)
397 int rc;
398 struct gendisk *disk;
399 struct disk_part_iter piter;
400 struct hd_struct *part;
402 if (device->discipline->ready_to_online) {
403 rc = device->discipline->ready_to_online(device);
404 if (rc)
405 return rc;
407 device->state = DASD_STATE_ONLINE;
408 if (device->block) {
409 dasd_schedule_block_bh(device->block);
410 if ((device->features & DASD_FEATURE_USERAW)) {
411 disk = device->block->gdp;
412 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
413 return 0;
415 disk = device->block->bdev->bd_disk;
416 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
417 while ((part = disk_part_iter_next(&piter)))
418 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
419 disk_part_iter_exit(&piter);
421 return 0;
425 * Stop the requeueing of requests again.
427 static int dasd_state_online_to_ready(struct dasd_device *device)
429 int rc;
430 struct gendisk *disk;
431 struct disk_part_iter piter;
432 struct hd_struct *part;
434 if (device->discipline->online_to_ready) {
435 rc = device->discipline->online_to_ready(device);
436 if (rc)
437 return rc;
439 device->state = DASD_STATE_READY;
440 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
441 disk = device->block->bdev->bd_disk;
442 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
443 while ((part = disk_part_iter_next(&piter)))
444 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
445 disk_part_iter_exit(&piter);
447 return 0;
451 * Device startup state changes.
453 static int dasd_increase_state(struct dasd_device *device)
455 int rc;
457 rc = 0;
458 if (device->state == DASD_STATE_NEW &&
459 device->target >= DASD_STATE_KNOWN)
460 rc = dasd_state_new_to_known(device);
462 if (!rc &&
463 device->state == DASD_STATE_KNOWN &&
464 device->target >= DASD_STATE_BASIC)
465 rc = dasd_state_known_to_basic(device);
467 if (!rc &&
468 device->state == DASD_STATE_BASIC &&
469 device->target >= DASD_STATE_READY)
470 rc = dasd_state_basic_to_ready(device);
472 if (!rc &&
473 device->state == DASD_STATE_UNFMT &&
474 device->target > DASD_STATE_UNFMT)
475 rc = -EPERM;
477 if (!rc &&
478 device->state == DASD_STATE_READY &&
479 device->target >= DASD_STATE_ONLINE)
480 rc = dasd_state_ready_to_online(device);
482 return rc;
486 * Device shutdown state changes.
488 static int dasd_decrease_state(struct dasd_device *device)
490 int rc;
492 rc = 0;
493 if (device->state == DASD_STATE_ONLINE &&
494 device->target <= DASD_STATE_READY)
495 rc = dasd_state_online_to_ready(device);
497 if (!rc &&
498 device->state == DASD_STATE_READY &&
499 device->target <= DASD_STATE_BASIC)
500 rc = dasd_state_ready_to_basic(device);
502 if (!rc &&
503 device->state == DASD_STATE_UNFMT &&
504 device->target <= DASD_STATE_BASIC)
505 rc = dasd_state_unfmt_to_basic(device);
507 if (!rc &&
508 device->state == DASD_STATE_BASIC &&
509 device->target <= DASD_STATE_KNOWN)
510 rc = dasd_state_basic_to_known(device);
512 if (!rc &&
513 device->state == DASD_STATE_KNOWN &&
514 device->target <= DASD_STATE_NEW)
515 rc = dasd_state_known_to_new(device);
517 return rc;
521 * This is the main startup/shutdown routine.
523 static void dasd_change_state(struct dasd_device *device)
525 int rc;
527 if (device->state == device->target)
528 /* Already where we want to go today... */
529 return;
530 if (device->state < device->target)
531 rc = dasd_increase_state(device);
532 else
533 rc = dasd_decrease_state(device);
534 if (rc == -EAGAIN)
535 return;
536 if (rc)
537 device->target = device->state;
539 if (device->state == device->target)
540 wake_up(&dasd_init_waitq);
542 /* let user-space know that the device status changed */
543 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
547 * Kick starter for devices that did not complete the startup/shutdown
548 * procedure or were sleeping because of a pending state.
549 * dasd_kick_device will schedule a call do do_kick_device to the kernel
550 * event daemon.
552 static void do_kick_device(struct work_struct *work)
554 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
555 mutex_lock(&device->state_mutex);
556 dasd_change_state(device);
557 mutex_unlock(&device->state_mutex);
558 dasd_schedule_device_bh(device);
559 dasd_put_device(device);
562 void dasd_kick_device(struct dasd_device *device)
564 dasd_get_device(device);
565 /* queue call to dasd_kick_device to the kernel event daemon. */
566 schedule_work(&device->kick_work);
570 * dasd_reload_device will schedule a call do do_reload_device to the kernel
571 * event daemon.
573 static void do_reload_device(struct work_struct *work)
575 struct dasd_device *device = container_of(work, struct dasd_device,
576 reload_device);
577 device->discipline->reload(device);
578 dasd_put_device(device);
581 void dasd_reload_device(struct dasd_device *device)
583 dasd_get_device(device);
584 /* queue call to dasd_reload_device to the kernel event daemon. */
585 schedule_work(&device->reload_device);
587 EXPORT_SYMBOL(dasd_reload_device);
590 * dasd_restore_device will schedule a call do do_restore_device to the kernel
591 * event daemon.
593 static void do_restore_device(struct work_struct *work)
595 struct dasd_device *device = container_of(work, struct dasd_device,
596 restore_device);
597 device->cdev->drv->restore(device->cdev);
598 dasd_put_device(device);
601 void dasd_restore_device(struct dasd_device *device)
603 dasd_get_device(device);
604 /* queue call to dasd_restore_device to the kernel event daemon. */
605 schedule_work(&device->restore_device);
609 * Set the target state for a device and starts the state change.
611 void dasd_set_target_state(struct dasd_device *device, int target)
613 dasd_get_device(device);
614 mutex_lock(&device->state_mutex);
615 /* If we are in probeonly mode stop at DASD_STATE_READY. */
616 if (dasd_probeonly && target > DASD_STATE_READY)
617 target = DASD_STATE_READY;
618 if (device->target != target) {
619 if (device->state == target)
620 wake_up(&dasd_init_waitq);
621 device->target = target;
623 if (device->state != device->target)
624 dasd_change_state(device);
625 mutex_unlock(&device->state_mutex);
626 dasd_put_device(device);
630 * Enable devices with device numbers in [from..to].
632 static inline int _wait_for_device(struct dasd_device *device)
634 return (device->state == device->target);
637 void dasd_enable_device(struct dasd_device *device)
639 dasd_set_target_state(device, DASD_STATE_ONLINE);
640 if (device->state <= DASD_STATE_KNOWN)
641 /* No discipline for device found. */
642 dasd_set_target_state(device, DASD_STATE_NEW);
643 /* Now wait for the devices to come up. */
644 wait_event(dasd_init_waitq, _wait_for_device(device));
648 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
651 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
653 #ifdef CONFIG_DASD_PROFILE
654 struct dasd_profile_info dasd_global_profile_data;
655 static struct dentry *dasd_global_profile_dentry;
656 static struct dentry *dasd_debugfs_global_entry;
659 * Add profiling information for cqr before execution.
661 static void dasd_profile_start(struct dasd_block *block,
662 struct dasd_ccw_req *cqr,
663 struct request *req)
665 struct list_head *l;
666 unsigned int counter;
667 struct dasd_device *device;
669 /* count the length of the chanq for statistics */
670 counter = 0;
671 if (dasd_global_profile_level || block->profile.data)
672 list_for_each(l, &block->ccw_queue)
673 if (++counter >= 31)
674 break;
676 if (dasd_global_profile_level) {
677 dasd_global_profile_data.dasd_io_nr_req[counter]++;
678 if (rq_data_dir(req) == READ)
679 dasd_global_profile_data.dasd_read_nr_req[counter]++;
682 spin_lock(&block->profile.lock);
683 if (block->profile.data)
684 block->profile.data->dasd_io_nr_req[counter]++;
685 if (rq_data_dir(req) == READ)
686 block->profile.data->dasd_read_nr_req[counter]++;
687 spin_unlock(&block->profile.lock);
690 * We count the request for the start device, even though it may run on
691 * some other device due to error recovery. This way we make sure that
692 * we count each request only once.
694 device = cqr->startdev;
695 if (device->profile.data) {
696 counter = 1; /* request is not yet queued on the start device */
697 list_for_each(l, &device->ccw_queue)
698 if (++counter >= 31)
699 break;
701 spin_lock(&device->profile.lock);
702 if (device->profile.data) {
703 device->profile.data->dasd_io_nr_req[counter]++;
704 if (rq_data_dir(req) == READ)
705 device->profile.data->dasd_read_nr_req[counter]++;
707 spin_unlock(&device->profile.lock);
711 * Add profiling information for cqr after execution.
714 #define dasd_profile_counter(value, index) \
716 for (index = 0; index < 31 && value >> (2+index); index++) \
720 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
721 int is_alias,
722 int is_tpm,
723 int is_read,
724 long sectors,
725 int sectors_ind,
726 int tottime_ind,
727 int tottimeps_ind,
728 int strtime_ind,
729 int irqtime_ind,
730 int irqtimeps_ind,
731 int endtime_ind)
733 /* in case of an overflow, reset the whole profile */
734 if (data->dasd_io_reqs == UINT_MAX) {
735 memset(data, 0, sizeof(*data));
736 getnstimeofday(&data->starttod);
738 data->dasd_io_reqs++;
739 data->dasd_io_sects += sectors;
740 if (is_alias)
741 data->dasd_io_alias++;
742 if (is_tpm)
743 data->dasd_io_tpm++;
745 data->dasd_io_secs[sectors_ind]++;
746 data->dasd_io_times[tottime_ind]++;
747 data->dasd_io_timps[tottimeps_ind]++;
748 data->dasd_io_time1[strtime_ind]++;
749 data->dasd_io_time2[irqtime_ind]++;
750 data->dasd_io_time2ps[irqtimeps_ind]++;
751 data->dasd_io_time3[endtime_ind]++;
753 if (is_read) {
754 data->dasd_read_reqs++;
755 data->dasd_read_sects += sectors;
756 if (is_alias)
757 data->dasd_read_alias++;
758 if (is_tpm)
759 data->dasd_read_tpm++;
760 data->dasd_read_secs[sectors_ind]++;
761 data->dasd_read_times[tottime_ind]++;
762 data->dasd_read_time1[strtime_ind]++;
763 data->dasd_read_time2[irqtime_ind]++;
764 data->dasd_read_time3[endtime_ind]++;
768 static void dasd_profile_end(struct dasd_block *block,
769 struct dasd_ccw_req *cqr,
770 struct request *req)
772 long strtime, irqtime, endtime, tottime; /* in microseconds */
773 long tottimeps, sectors;
774 struct dasd_device *device;
775 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
776 int irqtime_ind, irqtimeps_ind, endtime_ind;
778 device = cqr->startdev;
779 if (!(dasd_global_profile_level ||
780 block->profile.data ||
781 device->profile.data))
782 return;
784 sectors = blk_rq_sectors(req);
785 if (!cqr->buildclk || !cqr->startclk ||
786 !cqr->stopclk || !cqr->endclk ||
787 !sectors)
788 return;
790 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
791 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
792 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
793 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
794 tottimeps = tottime / sectors;
796 dasd_profile_counter(sectors, sectors_ind);
797 dasd_profile_counter(tottime, tottime_ind);
798 dasd_profile_counter(tottimeps, tottimeps_ind);
799 dasd_profile_counter(strtime, strtime_ind);
800 dasd_profile_counter(irqtime, irqtime_ind);
801 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
802 dasd_profile_counter(endtime, endtime_ind);
804 if (dasd_global_profile_level) {
805 dasd_profile_end_add_data(&dasd_global_profile_data,
806 cqr->startdev != block->base,
807 cqr->cpmode == 1,
808 rq_data_dir(req) == READ,
809 sectors, sectors_ind, tottime_ind,
810 tottimeps_ind, strtime_ind,
811 irqtime_ind, irqtimeps_ind,
812 endtime_ind);
815 spin_lock(&block->profile.lock);
816 if (block->profile.data)
817 dasd_profile_end_add_data(block->profile.data,
818 cqr->startdev != block->base,
819 cqr->cpmode == 1,
820 rq_data_dir(req) == READ,
821 sectors, sectors_ind, tottime_ind,
822 tottimeps_ind, strtime_ind,
823 irqtime_ind, irqtimeps_ind,
824 endtime_ind);
825 spin_unlock(&block->profile.lock);
827 spin_lock(&device->profile.lock);
828 if (device->profile.data)
829 dasd_profile_end_add_data(device->profile.data,
830 cqr->startdev != block->base,
831 cqr->cpmode == 1,
832 rq_data_dir(req) == READ,
833 sectors, sectors_ind, tottime_ind,
834 tottimeps_ind, strtime_ind,
835 irqtime_ind, irqtimeps_ind,
836 endtime_ind);
837 spin_unlock(&device->profile.lock);
840 void dasd_profile_reset(struct dasd_profile *profile)
842 struct dasd_profile_info *data;
844 spin_lock_bh(&profile->lock);
845 data = profile->data;
846 if (!data) {
847 spin_unlock_bh(&profile->lock);
848 return;
850 memset(data, 0, sizeof(*data));
851 getnstimeofday(&data->starttod);
852 spin_unlock_bh(&profile->lock);
855 void dasd_global_profile_reset(void)
857 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
858 getnstimeofday(&dasd_global_profile_data.starttod);
861 int dasd_profile_on(struct dasd_profile *profile)
863 struct dasd_profile_info *data;
865 data = kzalloc(sizeof(*data), GFP_KERNEL);
866 if (!data)
867 return -ENOMEM;
868 spin_lock_bh(&profile->lock);
869 if (profile->data) {
870 spin_unlock_bh(&profile->lock);
871 kfree(data);
872 return 0;
874 getnstimeofday(&data->starttod);
875 profile->data = data;
876 spin_unlock_bh(&profile->lock);
877 return 0;
880 void dasd_profile_off(struct dasd_profile *profile)
882 spin_lock_bh(&profile->lock);
883 kfree(profile->data);
884 profile->data = NULL;
885 spin_unlock_bh(&profile->lock);
888 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
890 char *buffer;
892 buffer = vmalloc(user_len + 1);
893 if (buffer == NULL)
894 return ERR_PTR(-ENOMEM);
895 if (copy_from_user(buffer, user_buf, user_len) != 0) {
896 vfree(buffer);
897 return ERR_PTR(-EFAULT);
899 /* got the string, now strip linefeed. */
900 if (buffer[user_len - 1] == '\n')
901 buffer[user_len - 1] = 0;
902 else
903 buffer[user_len] = 0;
904 return buffer;
907 static ssize_t dasd_stats_write(struct file *file,
908 const char __user *user_buf,
909 size_t user_len, loff_t *pos)
911 char *buffer, *str;
912 int rc;
913 struct seq_file *m = (struct seq_file *)file->private_data;
914 struct dasd_profile *prof = m->private;
916 if (user_len > 65536)
917 user_len = 65536;
918 buffer = dasd_get_user_string(user_buf, user_len);
919 if (IS_ERR(buffer))
920 return PTR_ERR(buffer);
922 str = skip_spaces(buffer);
923 rc = user_len;
924 if (strncmp(str, "reset", 5) == 0) {
925 dasd_profile_reset(prof);
926 } else if (strncmp(str, "on", 2) == 0) {
927 rc = dasd_profile_on(prof);
928 if (!rc)
929 rc = user_len;
930 } else if (strncmp(str, "off", 3) == 0) {
931 dasd_profile_off(prof);
932 } else
933 rc = -EINVAL;
934 vfree(buffer);
935 return rc;
938 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
940 int i;
942 for (i = 0; i < 32; i++)
943 seq_printf(m, "%u ", array[i]);
944 seq_putc(m, '\n');
947 static void dasd_stats_seq_print(struct seq_file *m,
948 struct dasd_profile_info *data)
950 seq_printf(m, "start_time %ld.%09ld\n",
951 data->starttod.tv_sec, data->starttod.tv_nsec);
952 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
953 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
954 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
955 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
956 seq_printf(m, "histogram_sectors ");
957 dasd_stats_array(m, data->dasd_io_secs);
958 seq_printf(m, "histogram_io_times ");
959 dasd_stats_array(m, data->dasd_io_times);
960 seq_printf(m, "histogram_io_times_weighted ");
961 dasd_stats_array(m, data->dasd_io_timps);
962 seq_printf(m, "histogram_time_build_to_ssch ");
963 dasd_stats_array(m, data->dasd_io_time1);
964 seq_printf(m, "histogram_time_ssch_to_irq ");
965 dasd_stats_array(m, data->dasd_io_time2);
966 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
967 dasd_stats_array(m, data->dasd_io_time2ps);
968 seq_printf(m, "histogram_time_irq_to_end ");
969 dasd_stats_array(m, data->dasd_io_time3);
970 seq_printf(m, "histogram_ccw_queue_length ");
971 dasd_stats_array(m, data->dasd_io_nr_req);
972 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
973 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
974 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
975 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
976 seq_printf(m, "histogram_read_sectors ");
977 dasd_stats_array(m, data->dasd_read_secs);
978 seq_printf(m, "histogram_read_times ");
979 dasd_stats_array(m, data->dasd_read_times);
980 seq_printf(m, "histogram_read_time_build_to_ssch ");
981 dasd_stats_array(m, data->dasd_read_time1);
982 seq_printf(m, "histogram_read_time_ssch_to_irq ");
983 dasd_stats_array(m, data->dasd_read_time2);
984 seq_printf(m, "histogram_read_time_irq_to_end ");
985 dasd_stats_array(m, data->dasd_read_time3);
986 seq_printf(m, "histogram_read_ccw_queue_length ");
987 dasd_stats_array(m, data->dasd_read_nr_req);
990 static int dasd_stats_show(struct seq_file *m, void *v)
992 struct dasd_profile *profile;
993 struct dasd_profile_info *data;
995 profile = m->private;
996 spin_lock_bh(&profile->lock);
997 data = profile->data;
998 if (!data) {
999 spin_unlock_bh(&profile->lock);
1000 seq_printf(m, "disabled\n");
1001 return 0;
1003 dasd_stats_seq_print(m, data);
1004 spin_unlock_bh(&profile->lock);
1005 return 0;
1008 static int dasd_stats_open(struct inode *inode, struct file *file)
1010 struct dasd_profile *profile = inode->i_private;
1011 return single_open(file, dasd_stats_show, profile);
1014 static const struct file_operations dasd_stats_raw_fops = {
1015 .owner = THIS_MODULE,
1016 .open = dasd_stats_open,
1017 .read = seq_read,
1018 .llseek = seq_lseek,
1019 .release = single_release,
1020 .write = dasd_stats_write,
1023 static ssize_t dasd_stats_global_write(struct file *file,
1024 const char __user *user_buf,
1025 size_t user_len, loff_t *pos)
1027 char *buffer, *str;
1028 ssize_t rc;
1030 if (user_len > 65536)
1031 user_len = 65536;
1032 buffer = dasd_get_user_string(user_buf, user_len);
1033 if (IS_ERR(buffer))
1034 return PTR_ERR(buffer);
1035 str = skip_spaces(buffer);
1036 rc = user_len;
1037 if (strncmp(str, "reset", 5) == 0) {
1038 dasd_global_profile_reset();
1039 } else if (strncmp(str, "on", 2) == 0) {
1040 dasd_global_profile_reset();
1041 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1042 } else if (strncmp(str, "off", 3) == 0) {
1043 dasd_global_profile_level = DASD_PROFILE_OFF;
1044 } else
1045 rc = -EINVAL;
1046 vfree(buffer);
1047 return rc;
1050 static int dasd_stats_global_show(struct seq_file *m, void *v)
1052 if (!dasd_global_profile_level) {
1053 seq_printf(m, "disabled\n");
1054 return 0;
1056 dasd_stats_seq_print(m, &dasd_global_profile_data);
1057 return 0;
1060 static int dasd_stats_global_open(struct inode *inode, struct file *file)
1062 return single_open(file, dasd_stats_global_show, NULL);
1065 static const struct file_operations dasd_stats_global_fops = {
1066 .owner = THIS_MODULE,
1067 .open = dasd_stats_global_open,
1068 .read = seq_read,
1069 .llseek = seq_lseek,
1070 .release = single_release,
1071 .write = dasd_stats_global_write,
1074 static void dasd_profile_init(struct dasd_profile *profile,
1075 struct dentry *base_dentry)
1077 mode_t mode;
1078 struct dentry *pde;
1080 if (!base_dentry)
1081 return;
1082 profile->dentry = NULL;
1083 profile->data = NULL;
1084 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1085 pde = debugfs_create_file("statistics", mode, base_dentry,
1086 profile, &dasd_stats_raw_fops);
1087 if (pde && !IS_ERR(pde))
1088 profile->dentry = pde;
1089 return;
1092 static void dasd_profile_exit(struct dasd_profile *profile)
1094 dasd_profile_off(profile);
1095 if (profile->dentry) {
1096 debugfs_remove(profile->dentry);
1097 profile->dentry = NULL;
1101 static void dasd_statistics_removeroot(void)
1103 dasd_global_profile_level = DASD_PROFILE_OFF;
1104 if (dasd_global_profile_dentry) {
1105 debugfs_remove(dasd_global_profile_dentry);
1106 dasd_global_profile_dentry = NULL;
1108 if (dasd_debugfs_global_entry)
1109 debugfs_remove(dasd_debugfs_global_entry);
1110 if (dasd_debugfs_root_entry)
1111 debugfs_remove(dasd_debugfs_root_entry);
1114 static void dasd_statistics_createroot(void)
1116 mode_t mode;
1117 struct dentry *pde;
1119 dasd_debugfs_root_entry = NULL;
1120 dasd_debugfs_global_entry = NULL;
1121 dasd_global_profile_dentry = NULL;
1122 pde = debugfs_create_dir("dasd", NULL);
1123 if (!pde || IS_ERR(pde))
1124 goto error;
1125 dasd_debugfs_root_entry = pde;
1126 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1127 if (!pde || IS_ERR(pde))
1128 goto error;
1129 dasd_debugfs_global_entry = pde;
1131 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1132 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1133 NULL, &dasd_stats_global_fops);
1134 if (!pde || IS_ERR(pde))
1135 goto error;
1136 dasd_global_profile_dentry = pde;
1137 return;
1139 error:
1140 DBF_EVENT(DBF_ERR, "%s",
1141 "Creation of the dasd debugfs interface failed");
1142 dasd_statistics_removeroot();
1143 return;
1146 #else
1147 #define dasd_profile_start(block, cqr, req) do {} while (0)
1148 #define dasd_profile_end(block, cqr, req) do {} while (0)
1150 static void dasd_statistics_createroot(void)
1152 return;
1155 static void dasd_statistics_removeroot(void)
1157 return;
1160 int dasd_stats_generic_show(struct seq_file *m, void *v)
1162 seq_printf(m, "Statistics are not activated in this kernel\n");
1163 return 0;
1166 static void dasd_profile_init(struct dasd_profile *profile,
1167 struct dentry *base_dentry)
1169 return;
1172 static void dasd_profile_exit(struct dasd_profile *profile)
1174 return;
1177 int dasd_profile_on(struct dasd_profile *profile)
1179 return 0;
1182 #endif /* CONFIG_DASD_PROFILE */
1185 * Allocate memory for a channel program with 'cplength' channel
1186 * command words and 'datasize' additional space. There are two
1187 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1188 * memory and 2) dasd_smalloc_request uses the static ccw memory
1189 * that gets allocated for each device.
1191 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
1192 int datasize,
1193 struct dasd_device *device)
1195 struct dasd_ccw_req *cqr;
1197 /* Sanity checks */
1198 BUG_ON(datasize > PAGE_SIZE ||
1199 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1201 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1202 if (cqr == NULL)
1203 return ERR_PTR(-ENOMEM);
1204 cqr->cpaddr = NULL;
1205 if (cplength > 0) {
1206 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1207 GFP_ATOMIC | GFP_DMA);
1208 if (cqr->cpaddr == NULL) {
1209 kfree(cqr);
1210 return ERR_PTR(-ENOMEM);
1213 cqr->data = NULL;
1214 if (datasize > 0) {
1215 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1216 if (cqr->data == NULL) {
1217 kfree(cqr->cpaddr);
1218 kfree(cqr);
1219 return ERR_PTR(-ENOMEM);
1222 cqr->magic = magic;
1223 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1224 dasd_get_device(device);
1225 return cqr;
1228 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
1229 int datasize,
1230 struct dasd_device *device)
1232 unsigned long flags;
1233 struct dasd_ccw_req *cqr;
1234 char *data;
1235 int size;
1237 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1238 if (cplength > 0)
1239 size += cplength * sizeof(struct ccw1);
1240 if (datasize > 0)
1241 size += datasize;
1242 spin_lock_irqsave(&device->mem_lock, flags);
1243 cqr = (struct dasd_ccw_req *)
1244 dasd_alloc_chunk(&device->ccw_chunks, size);
1245 spin_unlock_irqrestore(&device->mem_lock, flags);
1246 if (cqr == NULL)
1247 return ERR_PTR(-ENOMEM);
1248 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1249 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1250 cqr->cpaddr = NULL;
1251 if (cplength > 0) {
1252 cqr->cpaddr = (struct ccw1 *) data;
1253 data += cplength*sizeof(struct ccw1);
1254 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1256 cqr->data = NULL;
1257 if (datasize > 0) {
1258 cqr->data = data;
1259 memset(cqr->data, 0, datasize);
1261 cqr->magic = magic;
1262 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1263 dasd_get_device(device);
1264 return cqr;
1268 * Free memory of a channel program. This function needs to free all the
1269 * idal lists that might have been created by dasd_set_cda and the
1270 * struct dasd_ccw_req itself.
1272 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1274 #ifdef CONFIG_64BIT
1275 struct ccw1 *ccw;
1277 /* Clear any idals used for the request. */
1278 ccw = cqr->cpaddr;
1279 do {
1280 clear_normalized_cda(ccw);
1281 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1282 #endif
1283 kfree(cqr->cpaddr);
1284 kfree(cqr->data);
1285 kfree(cqr);
1286 dasd_put_device(device);
1289 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1291 unsigned long flags;
1293 spin_lock_irqsave(&device->mem_lock, flags);
1294 dasd_free_chunk(&device->ccw_chunks, cqr);
1295 spin_unlock_irqrestore(&device->mem_lock, flags);
1296 dasd_put_device(device);
1300 * Check discipline magic in cqr.
1302 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1304 struct dasd_device *device;
1306 if (cqr == NULL)
1307 return -EINVAL;
1308 device = cqr->startdev;
1309 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1310 DBF_DEV_EVENT(DBF_WARNING, device,
1311 " dasd_ccw_req 0x%08x magic doesn't match"
1312 " discipline 0x%08x",
1313 cqr->magic,
1314 *(unsigned int *) device->discipline->name);
1315 return -EINVAL;
1317 return 0;
1321 * Terminate the current i/o and set the request to clear_pending.
1322 * Timer keeps device runnig.
1323 * ccw_device_clear can fail if the i/o subsystem
1324 * is in a bad mood.
1326 int dasd_term_IO(struct dasd_ccw_req *cqr)
1328 struct dasd_device *device;
1329 int retries, rc;
1330 char errorstring[ERRORLENGTH];
1332 /* Check the cqr */
1333 rc = dasd_check_cqr(cqr);
1334 if (rc)
1335 return rc;
1336 retries = 0;
1337 device = (struct dasd_device *) cqr->startdev;
1338 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1339 rc = ccw_device_clear(device->cdev, (long) cqr);
1340 switch (rc) {
1341 case 0: /* termination successful */
1342 cqr->status = DASD_CQR_CLEAR_PENDING;
1343 cqr->stopclk = get_clock();
1344 cqr->starttime = 0;
1345 DBF_DEV_EVENT(DBF_DEBUG, device,
1346 "terminate cqr %p successful",
1347 cqr);
1348 break;
1349 case -ENODEV:
1350 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1351 "device gone, retry");
1352 break;
1353 case -EIO:
1354 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1355 "I/O error, retry");
1356 break;
1357 case -EINVAL:
1358 case -EBUSY:
1359 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1360 "device busy, retry later");
1361 break;
1362 default:
1363 /* internal error 10 - unknown rc*/
1364 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1365 dev_err(&device->cdev->dev, "An error occurred in the "
1366 "DASD device driver, reason=%s\n", errorstring);
1367 BUG();
1368 break;
1370 retries++;
1372 dasd_schedule_device_bh(device);
1373 return rc;
1377 * Start the i/o. This start_IO can fail if the channel is really busy.
1378 * In that case set up a timer to start the request later.
1380 int dasd_start_IO(struct dasd_ccw_req *cqr)
1382 struct dasd_device *device;
1383 int rc;
1384 char errorstring[ERRORLENGTH];
1386 /* Check the cqr */
1387 rc = dasd_check_cqr(cqr);
1388 if (rc) {
1389 cqr->intrc = rc;
1390 return rc;
1392 device = (struct dasd_device *) cqr->startdev;
1393 if (((cqr->block &&
1394 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1395 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1396 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1397 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1398 "because of stolen lock", cqr);
1399 cqr->status = DASD_CQR_ERROR;
1400 cqr->intrc = -EPERM;
1401 return -EPERM;
1403 if (cqr->retries < 0) {
1404 /* internal error 14 - start_IO run out of retries */
1405 sprintf(errorstring, "14 %p", cqr);
1406 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1407 "device driver, reason=%s\n", errorstring);
1408 cqr->status = DASD_CQR_ERROR;
1409 return -EIO;
1411 cqr->startclk = get_clock();
1412 cqr->starttime = jiffies;
1413 cqr->retries--;
1414 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1415 cqr->lpm &= device->path_data.opm;
1416 if (!cqr->lpm)
1417 cqr->lpm = device->path_data.opm;
1419 if (cqr->cpmode == 1) {
1420 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1421 (long) cqr, cqr->lpm);
1422 } else {
1423 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1424 (long) cqr, cqr->lpm, 0);
1426 switch (rc) {
1427 case 0:
1428 cqr->status = DASD_CQR_IN_IO;
1429 break;
1430 case -EBUSY:
1431 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1432 "start_IO: device busy, retry later");
1433 break;
1434 case -ETIMEDOUT:
1435 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1436 "start_IO: request timeout, retry later");
1437 break;
1438 case -EACCES:
1439 /* -EACCES indicates that the request used only a subset of the
1440 * available paths and all these paths are gone. If the lpm of
1441 * this request was only a subset of the opm (e.g. the ppm) then
1442 * we just do a retry with all available paths.
1443 * If we already use the full opm, something is amiss, and we
1444 * need a full path verification.
1446 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1447 DBF_DEV_EVENT(DBF_WARNING, device,
1448 "start_IO: selected paths gone (%x)",
1449 cqr->lpm);
1450 } else if (cqr->lpm != device->path_data.opm) {
1451 cqr->lpm = device->path_data.opm;
1452 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1453 "start_IO: selected paths gone,"
1454 " retry on all paths");
1455 } else {
1456 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1457 "start_IO: all paths in opm gone,"
1458 " do path verification");
1459 dasd_generic_last_path_gone(device);
1460 device->path_data.opm = 0;
1461 device->path_data.ppm = 0;
1462 device->path_data.npm = 0;
1463 device->path_data.tbvpm =
1464 ccw_device_get_path_mask(device->cdev);
1466 break;
1467 case -ENODEV:
1468 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1469 "start_IO: -ENODEV device gone, retry");
1470 break;
1471 case -EIO:
1472 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1473 "start_IO: -EIO device gone, retry");
1474 break;
1475 case -EINVAL:
1476 /* most likely caused in power management context */
1477 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1478 "start_IO: -EINVAL device currently "
1479 "not accessible");
1480 break;
1481 default:
1482 /* internal error 11 - unknown rc */
1483 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1484 dev_err(&device->cdev->dev,
1485 "An error occurred in the DASD device driver, "
1486 "reason=%s\n", errorstring);
1487 BUG();
1488 break;
1490 cqr->intrc = rc;
1491 return rc;
1495 * Timeout function for dasd devices. This is used for different purposes
1496 * 1) missing interrupt handler for normal operation
1497 * 2) delayed start of request where start_IO failed with -EBUSY
1498 * 3) timeout for missing state change interrupts
1499 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1500 * DASD_CQR_QUEUED for 2) and 3).
1502 static void dasd_device_timeout(unsigned long ptr)
1504 unsigned long flags;
1505 struct dasd_device *device;
1507 device = (struct dasd_device *) ptr;
1508 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1509 /* re-activate request queue */
1510 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1511 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1512 dasd_schedule_device_bh(device);
1516 * Setup timeout for a device in jiffies.
1518 void dasd_device_set_timer(struct dasd_device *device, int expires)
1520 if (expires == 0)
1521 del_timer(&device->timer);
1522 else
1523 mod_timer(&device->timer, jiffies + expires);
1527 * Clear timeout for a device.
1529 void dasd_device_clear_timer(struct dasd_device *device)
1531 del_timer(&device->timer);
1534 static void dasd_handle_killed_request(struct ccw_device *cdev,
1535 unsigned long intparm)
1537 struct dasd_ccw_req *cqr;
1538 struct dasd_device *device;
1540 if (!intparm)
1541 return;
1542 cqr = (struct dasd_ccw_req *) intparm;
1543 if (cqr->status != DASD_CQR_IN_IO) {
1544 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1545 "invalid status in handle_killed_request: "
1546 "%02x", cqr->status);
1547 return;
1550 device = dasd_device_from_cdev_locked(cdev);
1551 if (IS_ERR(device)) {
1552 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1553 "unable to get device from cdev");
1554 return;
1557 if (!cqr->startdev ||
1558 device != cqr->startdev ||
1559 strncmp(cqr->startdev->discipline->ebcname,
1560 (char *) &cqr->magic, 4)) {
1561 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1562 "invalid device in request");
1563 dasd_put_device(device);
1564 return;
1567 /* Schedule request to be retried. */
1568 cqr->status = DASD_CQR_QUEUED;
1570 dasd_device_clear_timer(device);
1571 dasd_schedule_device_bh(device);
1572 dasd_put_device(device);
1575 void dasd_generic_handle_state_change(struct dasd_device *device)
1577 /* First of all start sense subsystem status request. */
1578 dasd_eer_snss(device);
1580 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1581 dasd_schedule_device_bh(device);
1582 if (device->block)
1583 dasd_schedule_block_bh(device->block);
1587 * Interrupt handler for "normal" ssch-io based dasd devices.
1589 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1590 struct irb *irb)
1592 struct dasd_ccw_req *cqr, *next;
1593 struct dasd_device *device;
1594 unsigned long long now;
1595 int expires;
1597 kstat_cpu(smp_processor_id()).irqs[IOINT_DAS]++;
1598 if (IS_ERR(irb)) {
1599 switch (PTR_ERR(irb)) {
1600 case -EIO:
1601 break;
1602 case -ETIMEDOUT:
1603 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1604 "request timed out\n", __func__);
1605 break;
1606 default:
1607 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1608 "unknown error %ld\n", __func__,
1609 PTR_ERR(irb));
1611 dasd_handle_killed_request(cdev, intparm);
1612 return;
1615 now = get_clock();
1616 cqr = (struct dasd_ccw_req *) intparm;
1617 /* check for conditions that should be handled immediately */
1618 if (!cqr ||
1619 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1620 scsw_cstat(&irb->scsw) == 0)) {
1621 if (cqr)
1622 memcpy(&cqr->irb, irb, sizeof(*irb));
1623 device = dasd_device_from_cdev_locked(cdev);
1624 if (IS_ERR(device))
1625 return;
1626 /* ignore unsolicited interrupts for DIAG discipline */
1627 if (device->discipline == dasd_diag_discipline_pointer) {
1628 dasd_put_device(device);
1629 return;
1631 device->discipline->dump_sense_dbf(device, irb, "int");
1632 if (device->features & DASD_FEATURE_ERPLOG)
1633 device->discipline->dump_sense(device, cqr, irb);
1634 device->discipline->check_for_device_change(device, cqr, irb);
1635 dasd_put_device(device);
1637 if (!cqr)
1638 return;
1640 device = (struct dasd_device *) cqr->startdev;
1641 if (!device ||
1642 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1643 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1644 "invalid device in request");
1645 return;
1648 /* Check for clear pending */
1649 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1650 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1651 cqr->status = DASD_CQR_CLEARED;
1652 dasd_device_clear_timer(device);
1653 wake_up(&dasd_flush_wq);
1654 dasd_schedule_device_bh(device);
1655 return;
1658 /* check status - the request might have been killed by dyn detach */
1659 if (cqr->status != DASD_CQR_IN_IO) {
1660 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1661 "status %02x", dev_name(&cdev->dev), cqr->status);
1662 return;
1665 next = NULL;
1666 expires = 0;
1667 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1668 scsw_cstat(&irb->scsw) == 0) {
1669 /* request was completed successfully */
1670 cqr->status = DASD_CQR_SUCCESS;
1671 cqr->stopclk = now;
1672 /* Start first request on queue if possible -> fast_io. */
1673 if (cqr->devlist.next != &device->ccw_queue) {
1674 next = list_entry(cqr->devlist.next,
1675 struct dasd_ccw_req, devlist);
1677 } else { /* error */
1679 * If we don't want complex ERP for this request, then just
1680 * reset this and retry it in the fastpath
1682 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1683 cqr->retries > 0) {
1684 if (cqr->lpm == device->path_data.opm)
1685 DBF_DEV_EVENT(DBF_DEBUG, device,
1686 "default ERP in fastpath "
1687 "(%i retries left)",
1688 cqr->retries);
1689 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1690 cqr->lpm = device->path_data.opm;
1691 cqr->status = DASD_CQR_QUEUED;
1692 next = cqr;
1693 } else
1694 cqr->status = DASD_CQR_ERROR;
1696 if (next && (next->status == DASD_CQR_QUEUED) &&
1697 (!device->stopped)) {
1698 if (device->discipline->start_IO(next) == 0)
1699 expires = next->expires;
1701 if (expires != 0)
1702 dasd_device_set_timer(device, expires);
1703 else
1704 dasd_device_clear_timer(device);
1705 dasd_schedule_device_bh(device);
1708 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1710 struct dasd_device *device;
1712 device = dasd_device_from_cdev_locked(cdev);
1714 if (IS_ERR(device))
1715 goto out;
1716 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1717 device->state != device->target ||
1718 !device->discipline->check_for_device_change){
1719 dasd_put_device(device);
1720 goto out;
1722 if (device->discipline->dump_sense_dbf)
1723 device->discipline->dump_sense_dbf(device, irb, "uc");
1724 device->discipline->check_for_device_change(device, NULL, irb);
1725 dasd_put_device(device);
1726 out:
1727 return UC_TODO_RETRY;
1729 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1732 * If we have an error on a dasd_block layer request then we cancel
1733 * and return all further requests from the same dasd_block as well.
1735 static void __dasd_device_recovery(struct dasd_device *device,
1736 struct dasd_ccw_req *ref_cqr)
1738 struct list_head *l, *n;
1739 struct dasd_ccw_req *cqr;
1742 * only requeue request that came from the dasd_block layer
1744 if (!ref_cqr->block)
1745 return;
1747 list_for_each_safe(l, n, &device->ccw_queue) {
1748 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1749 if (cqr->status == DASD_CQR_QUEUED &&
1750 ref_cqr->block == cqr->block) {
1751 cqr->status = DASD_CQR_CLEARED;
1757 * Remove those ccw requests from the queue that need to be returned
1758 * to the upper layer.
1760 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1761 struct list_head *final_queue)
1763 struct list_head *l, *n;
1764 struct dasd_ccw_req *cqr;
1766 /* Process request with final status. */
1767 list_for_each_safe(l, n, &device->ccw_queue) {
1768 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1770 /* Stop list processing at the first non-final request. */
1771 if (cqr->status == DASD_CQR_QUEUED ||
1772 cqr->status == DASD_CQR_IN_IO ||
1773 cqr->status == DASD_CQR_CLEAR_PENDING)
1774 break;
1775 if (cqr->status == DASD_CQR_ERROR) {
1776 __dasd_device_recovery(device, cqr);
1778 /* Rechain finished requests to final queue */
1779 list_move_tail(&cqr->devlist, final_queue);
1784 * the cqrs from the final queue are returned to the upper layer
1785 * by setting a dasd_block state and calling the callback function
1787 static void __dasd_device_process_final_queue(struct dasd_device *device,
1788 struct list_head *final_queue)
1790 struct list_head *l, *n;
1791 struct dasd_ccw_req *cqr;
1792 struct dasd_block *block;
1793 void (*callback)(struct dasd_ccw_req *, void *data);
1794 void *callback_data;
1795 char errorstring[ERRORLENGTH];
1797 list_for_each_safe(l, n, final_queue) {
1798 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1799 list_del_init(&cqr->devlist);
1800 block = cqr->block;
1801 callback = cqr->callback;
1802 callback_data = cqr->callback_data;
1803 if (block)
1804 spin_lock_bh(&block->queue_lock);
1805 switch (cqr->status) {
1806 case DASD_CQR_SUCCESS:
1807 cqr->status = DASD_CQR_DONE;
1808 break;
1809 case DASD_CQR_ERROR:
1810 cqr->status = DASD_CQR_NEED_ERP;
1811 break;
1812 case DASD_CQR_CLEARED:
1813 cqr->status = DASD_CQR_TERMINATED;
1814 break;
1815 default:
1816 /* internal error 12 - wrong cqr status*/
1817 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1818 dev_err(&device->cdev->dev,
1819 "An error occurred in the DASD device driver, "
1820 "reason=%s\n", errorstring);
1821 BUG();
1823 if (cqr->callback != NULL)
1824 (callback)(cqr, callback_data);
1825 if (block)
1826 spin_unlock_bh(&block->queue_lock);
1831 * Take a look at the first request on the ccw queue and check
1832 * if it reached its expire time. If so, terminate the IO.
1834 static void __dasd_device_check_expire(struct dasd_device *device)
1836 struct dasd_ccw_req *cqr;
1838 if (list_empty(&device->ccw_queue))
1839 return;
1840 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1841 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1842 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1843 if (device->discipline->term_IO(cqr) != 0) {
1844 /* Hmpf, try again in 5 sec */
1845 dev_err(&device->cdev->dev,
1846 "cqr %p timed out (%lus) but cannot be "
1847 "ended, retrying in 5 s\n",
1848 cqr, (cqr->expires/HZ));
1849 cqr->expires += 5*HZ;
1850 dasd_device_set_timer(device, 5*HZ);
1851 } else {
1852 dev_err(&device->cdev->dev,
1853 "cqr %p timed out (%lus), %i retries "
1854 "remaining\n", cqr, (cqr->expires/HZ),
1855 cqr->retries);
1861 * Take a look at the first request on the ccw queue and check
1862 * if it needs to be started.
1864 static void __dasd_device_start_head(struct dasd_device *device)
1866 struct dasd_ccw_req *cqr;
1867 int rc;
1869 if (list_empty(&device->ccw_queue))
1870 return;
1871 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1872 if (cqr->status != DASD_CQR_QUEUED)
1873 return;
1874 /* when device is stopped, return request to previous layer
1875 * exception: only the disconnect or unresumed bits are set and the
1876 * cqr is a path verification request
1878 if (device->stopped &&
1879 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1880 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1881 cqr->intrc = -EAGAIN;
1882 cqr->status = DASD_CQR_CLEARED;
1883 dasd_schedule_device_bh(device);
1884 return;
1887 rc = device->discipline->start_IO(cqr);
1888 if (rc == 0)
1889 dasd_device_set_timer(device, cqr->expires);
1890 else if (rc == -EACCES) {
1891 dasd_schedule_device_bh(device);
1892 } else
1893 /* Hmpf, try again in 1/2 sec */
1894 dasd_device_set_timer(device, 50);
1897 static void __dasd_device_check_path_events(struct dasd_device *device)
1899 int rc;
1901 if (device->path_data.tbvpm) {
1902 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1903 DASD_UNRESUMED_PM))
1904 return;
1905 rc = device->discipline->verify_path(
1906 device, device->path_data.tbvpm);
1907 if (rc)
1908 dasd_device_set_timer(device, 50);
1909 else
1910 device->path_data.tbvpm = 0;
1915 * Go through all request on the dasd_device request queue,
1916 * terminate them on the cdev if necessary, and return them to the
1917 * submitting layer via callback.
1918 * Note:
1919 * Make sure that all 'submitting layers' still exist when
1920 * this function is called!. In other words, when 'device' is a base
1921 * device then all block layer requests must have been removed before
1922 * via dasd_flush_block_queue.
1924 int dasd_flush_device_queue(struct dasd_device *device)
1926 struct dasd_ccw_req *cqr, *n;
1927 int rc;
1928 struct list_head flush_queue;
1930 INIT_LIST_HEAD(&flush_queue);
1931 spin_lock_irq(get_ccwdev_lock(device->cdev));
1932 rc = 0;
1933 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1934 /* Check status and move request to flush_queue */
1935 switch (cqr->status) {
1936 case DASD_CQR_IN_IO:
1937 rc = device->discipline->term_IO(cqr);
1938 if (rc) {
1939 /* unable to terminate requeust */
1940 dev_err(&device->cdev->dev,
1941 "Flushing the DASD request queue "
1942 "failed for request %p\n", cqr);
1943 /* stop flush processing */
1944 goto finished;
1946 break;
1947 case DASD_CQR_QUEUED:
1948 cqr->stopclk = get_clock();
1949 cqr->status = DASD_CQR_CLEARED;
1950 break;
1951 default: /* no need to modify the others */
1952 break;
1954 list_move_tail(&cqr->devlist, &flush_queue);
1956 finished:
1957 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1959 * After this point all requests must be in state CLEAR_PENDING,
1960 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1961 * one of the others.
1963 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1964 wait_event(dasd_flush_wq,
1965 (cqr->status != DASD_CQR_CLEAR_PENDING));
1967 * Now set each request back to TERMINATED, DONE or NEED_ERP
1968 * and call the callback function of flushed requests
1970 __dasd_device_process_final_queue(device, &flush_queue);
1971 return rc;
1975 * Acquire the device lock and process queues for the device.
1977 static void dasd_device_tasklet(struct dasd_device *device)
1979 struct list_head final_queue;
1981 atomic_set (&device->tasklet_scheduled, 0);
1982 INIT_LIST_HEAD(&final_queue);
1983 spin_lock_irq(get_ccwdev_lock(device->cdev));
1984 /* Check expire time of first request on the ccw queue. */
1985 __dasd_device_check_expire(device);
1986 /* find final requests on ccw queue */
1987 __dasd_device_process_ccw_queue(device, &final_queue);
1988 __dasd_device_check_path_events(device);
1989 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1990 /* Now call the callback function of requests with final status */
1991 __dasd_device_process_final_queue(device, &final_queue);
1992 spin_lock_irq(get_ccwdev_lock(device->cdev));
1993 /* Now check if the head of the ccw queue needs to be started. */
1994 __dasd_device_start_head(device);
1995 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1996 dasd_put_device(device);
2000 * Schedules a call to dasd_tasklet over the device tasklet.
2002 void dasd_schedule_device_bh(struct dasd_device *device)
2004 /* Protect against rescheduling. */
2005 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2006 return;
2007 dasd_get_device(device);
2008 tasklet_hi_schedule(&device->tasklet);
2011 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2013 device->stopped |= bits;
2015 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2017 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2019 device->stopped &= ~bits;
2020 if (!device->stopped)
2021 wake_up(&generic_waitq);
2023 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2026 * Queue a request to the head of the device ccw_queue.
2027 * Start the I/O if possible.
2029 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2031 struct dasd_device *device;
2032 unsigned long flags;
2034 device = cqr->startdev;
2035 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2036 cqr->status = DASD_CQR_QUEUED;
2037 list_add(&cqr->devlist, &device->ccw_queue);
2038 /* let the bh start the request to keep them in order */
2039 dasd_schedule_device_bh(device);
2040 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2044 * Queue a request to the tail of the device ccw_queue.
2045 * Start the I/O if possible.
2047 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2049 struct dasd_device *device;
2050 unsigned long flags;
2052 device = cqr->startdev;
2053 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2054 cqr->status = DASD_CQR_QUEUED;
2055 list_add_tail(&cqr->devlist, &device->ccw_queue);
2056 /* let the bh start the request to keep them in order */
2057 dasd_schedule_device_bh(device);
2058 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2062 * Wakeup helper for the 'sleep_on' functions.
2064 static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2066 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2067 cqr->callback_data = DASD_SLEEPON_END_TAG;
2068 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2069 wake_up(&generic_waitq);
2072 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2074 struct dasd_device *device;
2075 int rc;
2077 device = cqr->startdev;
2078 spin_lock_irq(get_ccwdev_lock(device->cdev));
2079 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2080 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2081 return rc;
2085 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2087 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2089 struct dasd_device *device;
2090 dasd_erp_fn_t erp_fn;
2092 if (cqr->status == DASD_CQR_FILLED)
2093 return 0;
2094 device = cqr->startdev;
2095 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2096 if (cqr->status == DASD_CQR_TERMINATED) {
2097 device->discipline->handle_terminated_request(cqr);
2098 return 1;
2100 if (cqr->status == DASD_CQR_NEED_ERP) {
2101 erp_fn = device->discipline->erp_action(cqr);
2102 erp_fn(cqr);
2103 return 1;
2105 if (cqr->status == DASD_CQR_FAILED)
2106 dasd_log_sense(cqr, &cqr->irb);
2107 if (cqr->refers) {
2108 __dasd_process_erp(device, cqr);
2109 return 1;
2112 return 0;
2115 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2117 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2118 if (cqr->refers) /* erp is not done yet */
2119 return 1;
2120 return ((cqr->status != DASD_CQR_DONE) &&
2121 (cqr->status != DASD_CQR_FAILED));
2122 } else
2123 return (cqr->status == DASD_CQR_FILLED);
2126 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2128 struct dasd_device *device;
2129 int rc;
2130 struct list_head ccw_queue;
2131 struct dasd_ccw_req *cqr;
2133 INIT_LIST_HEAD(&ccw_queue);
2134 maincqr->status = DASD_CQR_FILLED;
2135 device = maincqr->startdev;
2136 list_add(&maincqr->blocklist, &ccw_queue);
2137 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2138 cqr = list_first_entry(&ccw_queue,
2139 struct dasd_ccw_req, blocklist)) {
2141 if (__dasd_sleep_on_erp(cqr))
2142 continue;
2143 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2144 continue;
2145 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2146 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2147 cqr->status = DASD_CQR_FAILED;
2148 cqr->intrc = -EPERM;
2149 continue;
2151 /* Non-temporary stop condition will trigger fail fast */
2152 if (device->stopped & ~DASD_STOPPED_PENDING &&
2153 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2154 (!dasd_eer_enabled(device))) {
2155 cqr->status = DASD_CQR_FAILED;
2156 continue;
2158 /* Don't try to start requests if device is stopped */
2159 if (interruptible) {
2160 rc = wait_event_interruptible(
2161 generic_waitq, !(device->stopped));
2162 if (rc == -ERESTARTSYS) {
2163 cqr->status = DASD_CQR_FAILED;
2164 maincqr->intrc = rc;
2165 continue;
2167 } else
2168 wait_event(generic_waitq, !(device->stopped));
2170 cqr->callback = dasd_wakeup_cb;
2171 cqr->callback_data = DASD_SLEEPON_START_TAG;
2172 dasd_add_request_tail(cqr);
2173 if (interruptible) {
2174 rc = wait_event_interruptible(
2175 generic_waitq, _wait_for_wakeup(cqr));
2176 if (rc == -ERESTARTSYS) {
2177 dasd_cancel_req(cqr);
2178 /* wait (non-interruptible) for final status */
2179 wait_event(generic_waitq,
2180 _wait_for_wakeup(cqr));
2181 cqr->status = DASD_CQR_FAILED;
2182 maincqr->intrc = rc;
2183 continue;
2185 } else
2186 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2189 maincqr->endclk = get_clock();
2190 if ((maincqr->status != DASD_CQR_DONE) &&
2191 (maincqr->intrc != -ERESTARTSYS))
2192 dasd_log_sense(maincqr, &maincqr->irb);
2193 if (maincqr->status == DASD_CQR_DONE)
2194 rc = 0;
2195 else if (maincqr->intrc)
2196 rc = maincqr->intrc;
2197 else
2198 rc = -EIO;
2199 return rc;
2203 * Queue a request to the tail of the device ccw_queue and wait for
2204 * it's completion.
2206 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2208 return _dasd_sleep_on(cqr, 0);
2212 * Queue a request to the tail of the device ccw_queue and wait
2213 * interruptible for it's completion.
2215 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2217 return _dasd_sleep_on(cqr, 1);
2221 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2222 * for eckd devices) the currently running request has to be terminated
2223 * and be put back to status queued, before the special request is added
2224 * to the head of the queue. Then the special request is waited on normally.
2226 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2228 struct dasd_ccw_req *cqr;
2229 int rc;
2231 if (list_empty(&device->ccw_queue))
2232 return 0;
2233 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2234 rc = device->discipline->term_IO(cqr);
2235 if (!rc)
2237 * CQR terminated because a more important request is pending.
2238 * Undo decreasing of retry counter because this is
2239 * not an error case.
2241 cqr->retries++;
2242 return rc;
2245 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2247 struct dasd_device *device;
2248 int rc;
2250 device = cqr->startdev;
2251 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2252 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2253 cqr->status = DASD_CQR_FAILED;
2254 cqr->intrc = -EPERM;
2255 return -EIO;
2257 spin_lock_irq(get_ccwdev_lock(device->cdev));
2258 rc = _dasd_term_running_cqr(device);
2259 if (rc) {
2260 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2261 return rc;
2263 cqr->callback = dasd_wakeup_cb;
2264 cqr->callback_data = DASD_SLEEPON_START_TAG;
2265 cqr->status = DASD_CQR_QUEUED;
2266 list_add(&cqr->devlist, &device->ccw_queue);
2268 /* let the bh start the request to keep them in order */
2269 dasd_schedule_device_bh(device);
2271 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2273 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2275 if (cqr->status == DASD_CQR_DONE)
2276 rc = 0;
2277 else if (cqr->intrc)
2278 rc = cqr->intrc;
2279 else
2280 rc = -EIO;
2281 return rc;
2285 * Cancels a request that was started with dasd_sleep_on_req.
2286 * This is useful to timeout requests. The request will be
2287 * terminated if it is currently in i/o.
2288 * Returns 1 if the request has been terminated.
2289 * 0 if there was no need to terminate the request (not started yet)
2290 * negative error code if termination failed
2291 * Cancellation of a request is an asynchronous operation! The calling
2292 * function has to wait until the request is properly returned via callback.
2294 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2296 struct dasd_device *device = cqr->startdev;
2297 unsigned long flags;
2298 int rc;
2300 rc = 0;
2301 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2302 switch (cqr->status) {
2303 case DASD_CQR_QUEUED:
2304 /* request was not started - just set to cleared */
2305 cqr->status = DASD_CQR_CLEARED;
2306 break;
2307 case DASD_CQR_IN_IO:
2308 /* request in IO - terminate IO and release again */
2309 rc = device->discipline->term_IO(cqr);
2310 if (rc) {
2311 dev_err(&device->cdev->dev,
2312 "Cancelling request %p failed with rc=%d\n",
2313 cqr, rc);
2314 } else {
2315 cqr->stopclk = get_clock();
2317 break;
2318 default: /* already finished or clear pending - do nothing */
2319 break;
2321 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2322 dasd_schedule_device_bh(device);
2323 return rc;
2328 * SECTION: Operations of the dasd_block layer.
2332 * Timeout function for dasd_block. This is used when the block layer
2333 * is waiting for something that may not come reliably, (e.g. a state
2334 * change interrupt)
2336 static void dasd_block_timeout(unsigned long ptr)
2338 unsigned long flags;
2339 struct dasd_block *block;
2341 block = (struct dasd_block *) ptr;
2342 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2343 /* re-activate request queue */
2344 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2345 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2346 dasd_schedule_block_bh(block);
2350 * Setup timeout for a dasd_block in jiffies.
2352 void dasd_block_set_timer(struct dasd_block *block, int expires)
2354 if (expires == 0)
2355 del_timer(&block->timer);
2356 else
2357 mod_timer(&block->timer, jiffies + expires);
2361 * Clear timeout for a dasd_block.
2363 void dasd_block_clear_timer(struct dasd_block *block)
2365 del_timer(&block->timer);
2369 * Process finished error recovery ccw.
2371 static void __dasd_process_erp(struct dasd_device *device,
2372 struct dasd_ccw_req *cqr)
2374 dasd_erp_fn_t erp_fn;
2376 if (cqr->status == DASD_CQR_DONE)
2377 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2378 else
2379 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2380 erp_fn = device->discipline->erp_postaction(cqr);
2381 erp_fn(cqr);
2385 * Fetch requests from the block device queue.
2387 static void __dasd_process_request_queue(struct dasd_block *block)
2389 struct request_queue *queue;
2390 struct request *req;
2391 struct dasd_ccw_req *cqr;
2392 struct dasd_device *basedev;
2393 unsigned long flags;
2394 queue = block->request_queue;
2395 basedev = block->base;
2396 /* No queue ? Then there is nothing to do. */
2397 if (queue == NULL)
2398 return;
2401 * We requeue request from the block device queue to the ccw
2402 * queue only in two states. In state DASD_STATE_READY the
2403 * partition detection is done and we need to requeue requests
2404 * for that. State DASD_STATE_ONLINE is normal block device
2405 * operation.
2407 if (basedev->state < DASD_STATE_READY) {
2408 while ((req = blk_fetch_request(block->request_queue)))
2409 __blk_end_request_all(req, -EIO);
2410 return;
2412 /* Now we try to fetch requests from the request queue */
2413 while ((req = blk_peek_request(queue))) {
2414 if (basedev->features & DASD_FEATURE_READONLY &&
2415 rq_data_dir(req) == WRITE) {
2416 DBF_DEV_EVENT(DBF_ERR, basedev,
2417 "Rejecting write request %p",
2418 req);
2419 blk_start_request(req);
2420 __blk_end_request_all(req, -EIO);
2421 continue;
2423 cqr = basedev->discipline->build_cp(basedev, block, req);
2424 if (IS_ERR(cqr)) {
2425 if (PTR_ERR(cqr) == -EBUSY)
2426 break; /* normal end condition */
2427 if (PTR_ERR(cqr) == -ENOMEM)
2428 break; /* terminate request queue loop */
2429 if (PTR_ERR(cqr) == -EAGAIN) {
2431 * The current request cannot be build right
2432 * now, we have to try later. If this request
2433 * is the head-of-queue we stop the device
2434 * for 1/2 second.
2436 if (!list_empty(&block->ccw_queue))
2437 break;
2438 spin_lock_irqsave(
2439 get_ccwdev_lock(basedev->cdev), flags);
2440 dasd_device_set_stop_bits(basedev,
2441 DASD_STOPPED_PENDING);
2442 spin_unlock_irqrestore(
2443 get_ccwdev_lock(basedev->cdev), flags);
2444 dasd_block_set_timer(block, HZ/2);
2445 break;
2447 DBF_DEV_EVENT(DBF_ERR, basedev,
2448 "CCW creation failed (rc=%ld) "
2449 "on request %p",
2450 PTR_ERR(cqr), req);
2451 blk_start_request(req);
2452 __blk_end_request_all(req, -EIO);
2453 continue;
2456 * Note: callback is set to dasd_return_cqr_cb in
2457 * __dasd_block_start_head to cover erp requests as well
2459 cqr->callback_data = (void *) req;
2460 cqr->status = DASD_CQR_FILLED;
2461 blk_start_request(req);
2462 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2463 dasd_profile_start(block, cqr, req);
2467 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2469 struct request *req;
2470 int status;
2471 int error = 0;
2473 req = (struct request *) cqr->callback_data;
2474 dasd_profile_end(cqr->block, cqr, req);
2475 status = cqr->block->base->discipline->free_cp(cqr, req);
2476 if (status <= 0)
2477 error = status ? status : -EIO;
2478 __blk_end_request_all(req, error);
2482 * Process ccw request queue.
2484 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2485 struct list_head *final_queue)
2487 struct list_head *l, *n;
2488 struct dasd_ccw_req *cqr;
2489 dasd_erp_fn_t erp_fn;
2490 unsigned long flags;
2491 struct dasd_device *base = block->base;
2493 restart:
2494 /* Process request with final status. */
2495 list_for_each_safe(l, n, &block->ccw_queue) {
2496 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2497 if (cqr->status != DASD_CQR_DONE &&
2498 cqr->status != DASD_CQR_FAILED &&
2499 cqr->status != DASD_CQR_NEED_ERP &&
2500 cqr->status != DASD_CQR_TERMINATED)
2501 continue;
2503 if (cqr->status == DASD_CQR_TERMINATED) {
2504 base->discipline->handle_terminated_request(cqr);
2505 goto restart;
2508 /* Process requests that may be recovered */
2509 if (cqr->status == DASD_CQR_NEED_ERP) {
2510 erp_fn = base->discipline->erp_action(cqr);
2511 if (IS_ERR(erp_fn(cqr)))
2512 continue;
2513 goto restart;
2516 /* log sense for fatal error */
2517 if (cqr->status == DASD_CQR_FAILED) {
2518 dasd_log_sense(cqr, &cqr->irb);
2521 /* First of all call extended error reporting. */
2522 if (dasd_eer_enabled(base) &&
2523 cqr->status == DASD_CQR_FAILED) {
2524 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2526 /* restart request */
2527 cqr->status = DASD_CQR_FILLED;
2528 cqr->retries = 255;
2529 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2530 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2531 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2532 flags);
2533 goto restart;
2536 /* Process finished ERP request. */
2537 if (cqr->refers) {
2538 __dasd_process_erp(base, cqr);
2539 goto restart;
2542 /* Rechain finished requests to final queue */
2543 cqr->endclk = get_clock();
2544 list_move_tail(&cqr->blocklist, final_queue);
2548 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2550 dasd_schedule_block_bh(cqr->block);
2553 static void __dasd_block_start_head(struct dasd_block *block)
2555 struct dasd_ccw_req *cqr;
2557 if (list_empty(&block->ccw_queue))
2558 return;
2559 /* We allways begin with the first requests on the queue, as some
2560 * of previously started requests have to be enqueued on a
2561 * dasd_device again for error recovery.
2563 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2564 if (cqr->status != DASD_CQR_FILLED)
2565 continue;
2566 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2567 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2568 cqr->status = DASD_CQR_FAILED;
2569 cqr->intrc = -EPERM;
2570 dasd_schedule_block_bh(block);
2571 continue;
2573 /* Non-temporary stop condition will trigger fail fast */
2574 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2575 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2576 (!dasd_eer_enabled(block->base))) {
2577 cqr->status = DASD_CQR_FAILED;
2578 dasd_schedule_block_bh(block);
2579 continue;
2581 /* Don't try to start requests if device is stopped */
2582 if (block->base->stopped)
2583 return;
2585 /* just a fail safe check, should not happen */
2586 if (!cqr->startdev)
2587 cqr->startdev = block->base;
2589 /* make sure that the requests we submit find their way back */
2590 cqr->callback = dasd_return_cqr_cb;
2592 dasd_add_request_tail(cqr);
2597 * Central dasd_block layer routine. Takes requests from the generic
2598 * block layer request queue, creates ccw requests, enqueues them on
2599 * a dasd_device and processes ccw requests that have been returned.
2601 static void dasd_block_tasklet(struct dasd_block *block)
2603 struct list_head final_queue;
2604 struct list_head *l, *n;
2605 struct dasd_ccw_req *cqr;
2607 atomic_set(&block->tasklet_scheduled, 0);
2608 INIT_LIST_HEAD(&final_queue);
2609 spin_lock(&block->queue_lock);
2610 /* Finish off requests on ccw queue */
2611 __dasd_process_block_ccw_queue(block, &final_queue);
2612 spin_unlock(&block->queue_lock);
2613 /* Now call the callback function of requests with final status */
2614 spin_lock_irq(&block->request_queue_lock);
2615 list_for_each_safe(l, n, &final_queue) {
2616 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2617 list_del_init(&cqr->blocklist);
2618 __dasd_cleanup_cqr(cqr);
2620 spin_lock(&block->queue_lock);
2621 /* Get new request from the block device request queue */
2622 __dasd_process_request_queue(block);
2623 /* Now check if the head of the ccw queue needs to be started. */
2624 __dasd_block_start_head(block);
2625 spin_unlock(&block->queue_lock);
2626 spin_unlock_irq(&block->request_queue_lock);
2627 dasd_put_device(block->base);
2630 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2632 wake_up(&dasd_flush_wq);
2636 * Go through all request on the dasd_block request queue, cancel them
2637 * on the respective dasd_device, and return them to the generic
2638 * block layer.
2640 static int dasd_flush_block_queue(struct dasd_block *block)
2642 struct dasd_ccw_req *cqr, *n;
2643 int rc, i;
2644 struct list_head flush_queue;
2646 INIT_LIST_HEAD(&flush_queue);
2647 spin_lock_bh(&block->queue_lock);
2648 rc = 0;
2649 restart:
2650 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2651 /* if this request currently owned by a dasd_device cancel it */
2652 if (cqr->status >= DASD_CQR_QUEUED)
2653 rc = dasd_cancel_req(cqr);
2654 if (rc < 0)
2655 break;
2656 /* Rechain request (including erp chain) so it won't be
2657 * touched by the dasd_block_tasklet anymore.
2658 * Replace the callback so we notice when the request
2659 * is returned from the dasd_device layer.
2661 cqr->callback = _dasd_wake_block_flush_cb;
2662 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2663 list_move_tail(&cqr->blocklist, &flush_queue);
2664 if (i > 1)
2665 /* moved more than one request - need to restart */
2666 goto restart;
2668 spin_unlock_bh(&block->queue_lock);
2669 /* Now call the callback function of flushed requests */
2670 restart_cb:
2671 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2672 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2673 /* Process finished ERP request. */
2674 if (cqr->refers) {
2675 spin_lock_bh(&block->queue_lock);
2676 __dasd_process_erp(block->base, cqr);
2677 spin_unlock_bh(&block->queue_lock);
2678 /* restart list_for_xx loop since dasd_process_erp
2679 * might remove multiple elements */
2680 goto restart_cb;
2682 /* call the callback function */
2683 spin_lock_irq(&block->request_queue_lock);
2684 cqr->endclk = get_clock();
2685 list_del_init(&cqr->blocklist);
2686 __dasd_cleanup_cqr(cqr);
2687 spin_unlock_irq(&block->request_queue_lock);
2689 return rc;
2693 * Schedules a call to dasd_tasklet over the device tasklet.
2695 void dasd_schedule_block_bh(struct dasd_block *block)
2697 /* Protect against rescheduling. */
2698 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2699 return;
2700 /* life cycle of block is bound to it's base device */
2701 dasd_get_device(block->base);
2702 tasklet_hi_schedule(&block->tasklet);
2707 * SECTION: external block device operations
2708 * (request queue handling, open, release, etc.)
2712 * Dasd request queue function. Called from ll_rw_blk.c
2714 static void do_dasd_request(struct request_queue *queue)
2716 struct dasd_block *block;
2718 block = queue->queuedata;
2719 spin_lock(&block->queue_lock);
2720 /* Get new request from the block device request queue */
2721 __dasd_process_request_queue(block);
2722 /* Now check if the head of the ccw queue needs to be started. */
2723 __dasd_block_start_head(block);
2724 spin_unlock(&block->queue_lock);
2728 * Allocate and initialize request queue and default I/O scheduler.
2730 static int dasd_alloc_queue(struct dasd_block *block)
2732 int rc;
2734 block->request_queue = blk_init_queue(do_dasd_request,
2735 &block->request_queue_lock);
2736 if (block->request_queue == NULL)
2737 return -ENOMEM;
2739 block->request_queue->queuedata = block;
2741 elevator_exit(block->request_queue->elevator);
2742 block->request_queue->elevator = NULL;
2743 rc = elevator_init(block->request_queue, "deadline");
2744 if (rc) {
2745 blk_cleanup_queue(block->request_queue);
2746 return rc;
2748 return 0;
2752 * Allocate and initialize request queue.
2754 static void dasd_setup_queue(struct dasd_block *block)
2756 int max;
2758 if (block->base->features & DASD_FEATURE_USERAW) {
2760 * the max_blocks value for raw_track access is 256
2761 * it is higher than the native ECKD value because we
2762 * only need one ccw per track
2763 * so the max_hw_sectors are
2764 * 2048 x 512B = 1024kB = 16 tracks
2766 max = 2048;
2767 } else {
2768 max = block->base->discipline->max_blocks << block->s2b_shift;
2770 blk_queue_logical_block_size(block->request_queue,
2771 block->bp_block);
2772 blk_queue_max_hw_sectors(block->request_queue, max);
2773 blk_queue_max_segments(block->request_queue, -1L);
2774 /* with page sized segments we can translate each segement into
2775 * one idaw/tidaw
2777 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2778 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2782 * Deactivate and free request queue.
2784 static void dasd_free_queue(struct dasd_block *block)
2786 if (block->request_queue) {
2787 blk_cleanup_queue(block->request_queue);
2788 block->request_queue = NULL;
2793 * Flush request on the request queue.
2795 static void dasd_flush_request_queue(struct dasd_block *block)
2797 struct request *req;
2799 if (!block->request_queue)
2800 return;
2802 spin_lock_irq(&block->request_queue_lock);
2803 while ((req = blk_fetch_request(block->request_queue)))
2804 __blk_end_request_all(req, -EIO);
2805 spin_unlock_irq(&block->request_queue_lock);
2808 static int dasd_open(struct block_device *bdev, fmode_t mode)
2810 struct dasd_device *base;
2811 int rc;
2813 base = dasd_device_from_gendisk(bdev->bd_disk);
2814 if (!base)
2815 return -ENODEV;
2817 atomic_inc(&base->block->open_count);
2818 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2819 rc = -ENODEV;
2820 goto unlock;
2823 if (!try_module_get(base->discipline->owner)) {
2824 rc = -EINVAL;
2825 goto unlock;
2828 if (dasd_probeonly) {
2829 dev_info(&base->cdev->dev,
2830 "Accessing the DASD failed because it is in "
2831 "probeonly mode\n");
2832 rc = -EPERM;
2833 goto out;
2836 if (base->state <= DASD_STATE_BASIC) {
2837 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2838 " Cannot open unrecognized device");
2839 rc = -ENODEV;
2840 goto out;
2843 if ((mode & FMODE_WRITE) &&
2844 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2845 (base->features & DASD_FEATURE_READONLY))) {
2846 rc = -EROFS;
2847 goto out;
2850 dasd_put_device(base);
2851 return 0;
2853 out:
2854 module_put(base->discipline->owner);
2855 unlock:
2856 atomic_dec(&base->block->open_count);
2857 dasd_put_device(base);
2858 return rc;
2861 static int dasd_release(struct gendisk *disk, fmode_t mode)
2863 struct dasd_device *base;
2865 base = dasd_device_from_gendisk(disk);
2866 if (!base)
2867 return -ENODEV;
2869 atomic_dec(&base->block->open_count);
2870 module_put(base->discipline->owner);
2871 dasd_put_device(base);
2872 return 0;
2876 * Return disk geometry.
2878 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2880 struct dasd_device *base;
2882 base = dasd_device_from_gendisk(bdev->bd_disk);
2883 if (!base)
2884 return -ENODEV;
2886 if (!base->discipline ||
2887 !base->discipline->fill_geometry) {
2888 dasd_put_device(base);
2889 return -EINVAL;
2891 base->discipline->fill_geometry(base->block, geo);
2892 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
2893 dasd_put_device(base);
2894 return 0;
2897 const struct block_device_operations
2898 dasd_device_operations = {
2899 .owner = THIS_MODULE,
2900 .open = dasd_open,
2901 .release = dasd_release,
2902 .ioctl = dasd_ioctl,
2903 .compat_ioctl = dasd_ioctl,
2904 .getgeo = dasd_getgeo,
2907 /*******************************************************************************
2908 * end of block device operations
2911 static void
2912 dasd_exit(void)
2914 #ifdef CONFIG_PROC_FS
2915 dasd_proc_exit();
2916 #endif
2917 dasd_eer_exit();
2918 if (dasd_page_cache != NULL) {
2919 kmem_cache_destroy(dasd_page_cache);
2920 dasd_page_cache = NULL;
2922 dasd_gendisk_exit();
2923 dasd_devmap_exit();
2924 if (dasd_debug_area != NULL) {
2925 debug_unregister(dasd_debug_area);
2926 dasd_debug_area = NULL;
2928 dasd_statistics_removeroot();
2932 * SECTION: common functions for ccw_driver use
2936 * Is the device read-only?
2937 * Note that this function does not report the setting of the
2938 * readonly device attribute, but how it is configured in z/VM.
2940 int dasd_device_is_ro(struct dasd_device *device)
2942 struct ccw_dev_id dev_id;
2943 struct diag210 diag_data;
2944 int rc;
2946 if (!MACHINE_IS_VM)
2947 return 0;
2948 ccw_device_get_id(device->cdev, &dev_id);
2949 memset(&diag_data, 0, sizeof(diag_data));
2950 diag_data.vrdcdvno = dev_id.devno;
2951 diag_data.vrdclen = sizeof(diag_data);
2952 rc = diag210(&diag_data);
2953 if (rc == 0 || rc == 2) {
2954 return diag_data.vrdcvfla & 0x80;
2955 } else {
2956 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2957 dev_id.devno, rc);
2958 return 0;
2961 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2963 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2965 struct ccw_device *cdev = data;
2966 int ret;
2968 ret = ccw_device_set_online(cdev);
2969 if (ret)
2970 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2971 dev_name(&cdev->dev), ret);
2975 * Initial attempt at a probe function. this can be simplified once
2976 * the other detection code is gone.
2978 int dasd_generic_probe(struct ccw_device *cdev,
2979 struct dasd_discipline *discipline)
2981 int ret;
2983 ret = dasd_add_sysfs_files(cdev);
2984 if (ret) {
2985 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2986 "dasd_generic_probe: could not add "
2987 "sysfs entries");
2988 return ret;
2990 cdev->handler = &dasd_int_handler;
2993 * Automatically online either all dasd devices (dasd_autodetect)
2994 * or all devices specified with dasd= parameters during
2995 * initial probe.
2997 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2998 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
2999 async_schedule(dasd_generic_auto_online, cdev);
3000 return 0;
3004 * This will one day be called from a global not_oper handler.
3005 * It is also used by driver_unregister during module unload.
3007 void dasd_generic_remove(struct ccw_device *cdev)
3009 struct dasd_device *device;
3010 struct dasd_block *block;
3012 cdev->handler = NULL;
3014 dasd_remove_sysfs_files(cdev);
3015 device = dasd_device_from_cdev(cdev);
3016 if (IS_ERR(device))
3017 return;
3018 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3019 /* Already doing offline processing */
3020 dasd_put_device(device);
3021 return;
3024 * This device is removed unconditionally. Set offline
3025 * flag to prevent dasd_open from opening it while it is
3026 * no quite down yet.
3028 dasd_set_target_state(device, DASD_STATE_NEW);
3029 /* dasd_delete_device destroys the device reference. */
3030 block = device->block;
3031 dasd_delete_device(device);
3033 * life cycle of block is bound to device, so delete it after
3034 * device was safely removed
3036 if (block)
3037 dasd_free_block(block);
3041 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3042 * the device is detected for the first time and is supposed to be used
3043 * or the user has started activation through sysfs.
3045 int dasd_generic_set_online(struct ccw_device *cdev,
3046 struct dasd_discipline *base_discipline)
3048 struct dasd_discipline *discipline;
3049 struct dasd_device *device;
3050 int rc;
3052 /* first online clears initial online feature flag */
3053 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3054 device = dasd_create_device(cdev);
3055 if (IS_ERR(device))
3056 return PTR_ERR(device);
3058 discipline = base_discipline;
3059 if (device->features & DASD_FEATURE_USEDIAG) {
3060 if (!dasd_diag_discipline_pointer) {
3061 pr_warning("%s Setting the DASD online failed because "
3062 "of missing DIAG discipline\n",
3063 dev_name(&cdev->dev));
3064 dasd_delete_device(device);
3065 return -ENODEV;
3067 discipline = dasd_diag_discipline_pointer;
3069 if (!try_module_get(base_discipline->owner)) {
3070 dasd_delete_device(device);
3071 return -EINVAL;
3073 if (!try_module_get(discipline->owner)) {
3074 module_put(base_discipline->owner);
3075 dasd_delete_device(device);
3076 return -EINVAL;
3078 device->base_discipline = base_discipline;
3079 device->discipline = discipline;
3081 /* check_device will allocate block device if necessary */
3082 rc = discipline->check_device(device);
3083 if (rc) {
3084 pr_warning("%s Setting the DASD online with discipline %s "
3085 "failed with rc=%i\n",
3086 dev_name(&cdev->dev), discipline->name, rc);
3087 module_put(discipline->owner);
3088 module_put(base_discipline->owner);
3089 dasd_delete_device(device);
3090 return rc;
3093 dasd_set_target_state(device, DASD_STATE_ONLINE);
3094 if (device->state <= DASD_STATE_KNOWN) {
3095 pr_warning("%s Setting the DASD online failed because of a "
3096 "missing discipline\n", dev_name(&cdev->dev));
3097 rc = -ENODEV;
3098 dasd_set_target_state(device, DASD_STATE_NEW);
3099 if (device->block)
3100 dasd_free_block(device->block);
3101 dasd_delete_device(device);
3102 } else
3103 pr_debug("dasd_generic device %s found\n",
3104 dev_name(&cdev->dev));
3106 wait_event(dasd_init_waitq, _wait_for_device(device));
3108 dasd_put_device(device);
3109 return rc;
3112 int dasd_generic_set_offline(struct ccw_device *cdev)
3114 struct dasd_device *device;
3115 struct dasd_block *block;
3116 int max_count, open_count;
3118 device = dasd_device_from_cdev(cdev);
3119 if (IS_ERR(device))
3120 return PTR_ERR(device);
3121 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3122 /* Already doing offline processing */
3123 dasd_put_device(device);
3124 return 0;
3127 * We must make sure that this device is currently not in use.
3128 * The open_count is increased for every opener, that includes
3129 * the blkdev_get in dasd_scan_partitions. We are only interested
3130 * in the other openers.
3132 if (device->block) {
3133 max_count = device->block->bdev ? 0 : -1;
3134 open_count = atomic_read(&device->block->open_count);
3135 if (open_count > max_count) {
3136 if (open_count > 0)
3137 pr_warning("%s: The DASD cannot be set offline "
3138 "with open count %i\n",
3139 dev_name(&cdev->dev), open_count);
3140 else
3141 pr_warning("%s: The DASD cannot be set offline "
3142 "while it is in use\n",
3143 dev_name(&cdev->dev));
3144 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3145 dasd_put_device(device);
3146 return -EBUSY;
3149 dasd_set_target_state(device, DASD_STATE_NEW);
3150 /* dasd_delete_device destroys the device reference. */
3151 block = device->block;
3152 dasd_delete_device(device);
3154 * life cycle of block is bound to device, so delete it after
3155 * device was safely removed
3157 if (block)
3158 dasd_free_block(block);
3159 return 0;
3162 int dasd_generic_last_path_gone(struct dasd_device *device)
3164 struct dasd_ccw_req *cqr;
3166 dev_warn(&device->cdev->dev, "No operational channel path is left "
3167 "for the device\n");
3168 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3169 /* First of all call extended error reporting. */
3170 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3172 if (device->state < DASD_STATE_BASIC)
3173 return 0;
3174 /* Device is active. We want to keep it. */
3175 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3176 if ((cqr->status == DASD_CQR_IN_IO) ||
3177 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3178 cqr->status = DASD_CQR_QUEUED;
3179 cqr->retries++;
3181 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3182 dasd_device_clear_timer(device);
3183 dasd_schedule_device_bh(device);
3184 return 1;
3186 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3188 int dasd_generic_path_operational(struct dasd_device *device)
3190 dev_info(&device->cdev->dev, "A channel path to the device has become "
3191 "operational\n");
3192 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3193 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3194 if (device->stopped & DASD_UNRESUMED_PM) {
3195 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3196 dasd_restore_device(device);
3197 return 1;
3199 dasd_schedule_device_bh(device);
3200 if (device->block)
3201 dasd_schedule_block_bh(device->block);
3202 return 1;
3204 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3206 int dasd_generic_notify(struct ccw_device *cdev, int event)
3208 struct dasd_device *device;
3209 int ret;
3211 device = dasd_device_from_cdev_locked(cdev);
3212 if (IS_ERR(device))
3213 return 0;
3214 ret = 0;
3215 switch (event) {
3216 case CIO_GONE:
3217 case CIO_BOXED:
3218 case CIO_NO_PATH:
3219 device->path_data.opm = 0;
3220 device->path_data.ppm = 0;
3221 device->path_data.npm = 0;
3222 ret = dasd_generic_last_path_gone(device);
3223 break;
3224 case CIO_OPER:
3225 ret = 1;
3226 if (device->path_data.opm)
3227 ret = dasd_generic_path_operational(device);
3228 break;
3230 dasd_put_device(device);
3231 return ret;
3234 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3236 int chp;
3237 __u8 oldopm, eventlpm;
3238 struct dasd_device *device;
3240 device = dasd_device_from_cdev_locked(cdev);
3241 if (IS_ERR(device))
3242 return;
3243 for (chp = 0; chp < 8; chp++) {
3244 eventlpm = 0x80 >> chp;
3245 if (path_event[chp] & PE_PATH_GONE) {
3246 oldopm = device->path_data.opm;
3247 device->path_data.opm &= ~eventlpm;
3248 device->path_data.ppm &= ~eventlpm;
3249 device->path_data.npm &= ~eventlpm;
3250 if (oldopm && !device->path_data.opm)
3251 dasd_generic_last_path_gone(device);
3253 if (path_event[chp] & PE_PATH_AVAILABLE) {
3254 device->path_data.opm &= ~eventlpm;
3255 device->path_data.ppm &= ~eventlpm;
3256 device->path_data.npm &= ~eventlpm;
3257 device->path_data.tbvpm |= eventlpm;
3258 dasd_schedule_device_bh(device);
3261 dasd_put_device(device);
3263 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3265 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3267 if (!device->path_data.opm && lpm) {
3268 device->path_data.opm = lpm;
3269 dasd_generic_path_operational(device);
3270 } else
3271 device->path_data.opm |= lpm;
3272 return 0;
3274 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3277 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3279 struct dasd_ccw_req *cqr, *n;
3280 int rc;
3281 struct list_head freeze_queue;
3282 struct dasd_device *device = dasd_device_from_cdev(cdev);
3284 if (IS_ERR(device))
3285 return PTR_ERR(device);
3287 if (device->discipline->freeze)
3288 rc = device->discipline->freeze(device);
3290 /* disallow new I/O */
3291 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3292 /* clear active requests */
3293 INIT_LIST_HEAD(&freeze_queue);
3294 spin_lock_irq(get_ccwdev_lock(cdev));
3295 rc = 0;
3296 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3297 /* Check status and move request to flush_queue */
3298 if (cqr->status == DASD_CQR_IN_IO) {
3299 rc = device->discipline->term_IO(cqr);
3300 if (rc) {
3301 /* unable to terminate requeust */
3302 dev_err(&device->cdev->dev,
3303 "Unable to terminate request %p "
3304 "on suspend\n", cqr);
3305 spin_unlock_irq(get_ccwdev_lock(cdev));
3306 dasd_put_device(device);
3307 return rc;
3310 list_move_tail(&cqr->devlist, &freeze_queue);
3313 spin_unlock_irq(get_ccwdev_lock(cdev));
3315 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3316 wait_event(dasd_flush_wq,
3317 (cqr->status != DASD_CQR_CLEAR_PENDING));
3318 if (cqr->status == DASD_CQR_CLEARED)
3319 cqr->status = DASD_CQR_QUEUED;
3321 /* move freeze_queue to start of the ccw_queue */
3322 spin_lock_irq(get_ccwdev_lock(cdev));
3323 list_splice_tail(&freeze_queue, &device->ccw_queue);
3324 spin_unlock_irq(get_ccwdev_lock(cdev));
3326 dasd_put_device(device);
3327 return rc;
3329 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3331 int dasd_generic_restore_device(struct ccw_device *cdev)
3333 struct dasd_device *device = dasd_device_from_cdev(cdev);
3334 int rc = 0;
3336 if (IS_ERR(device))
3337 return PTR_ERR(device);
3339 /* allow new IO again */
3340 dasd_device_remove_stop_bits(device,
3341 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3343 dasd_schedule_device_bh(device);
3346 * call discipline restore function
3347 * if device is stopped do nothing e.g. for disconnected devices
3349 if (device->discipline->restore && !(device->stopped))
3350 rc = device->discipline->restore(device);
3351 if (rc || device->stopped)
3353 * if the resume failed for the DASD we put it in
3354 * an UNRESUMED stop state
3356 device->stopped |= DASD_UNRESUMED_PM;
3358 if (device->block)
3359 dasd_schedule_block_bh(device->block);
3361 dasd_put_device(device);
3362 return 0;
3364 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3366 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3367 void *rdc_buffer,
3368 int rdc_buffer_size,
3369 int magic)
3371 struct dasd_ccw_req *cqr;
3372 struct ccw1 *ccw;
3373 unsigned long *idaw;
3375 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3377 if (IS_ERR(cqr)) {
3378 /* internal error 13 - Allocating the RDC request failed*/
3379 dev_err(&device->cdev->dev,
3380 "An error occurred in the DASD device driver, "
3381 "reason=%s\n", "13");
3382 return cqr;
3385 ccw = cqr->cpaddr;
3386 ccw->cmd_code = CCW_CMD_RDC;
3387 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3388 idaw = (unsigned long *) (cqr->data);
3389 ccw->cda = (__u32)(addr_t) idaw;
3390 ccw->flags = CCW_FLAG_IDA;
3391 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3392 } else {
3393 ccw->cda = (__u32)(addr_t) rdc_buffer;
3394 ccw->flags = 0;
3397 ccw->count = rdc_buffer_size;
3398 cqr->startdev = device;
3399 cqr->memdev = device;
3400 cqr->expires = 10*HZ;
3401 cqr->retries = 256;
3402 cqr->buildclk = get_clock();
3403 cqr->status = DASD_CQR_FILLED;
3404 return cqr;
3408 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3409 void *rdc_buffer, int rdc_buffer_size)
3411 int ret;
3412 struct dasd_ccw_req *cqr;
3414 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3415 magic);
3416 if (IS_ERR(cqr))
3417 return PTR_ERR(cqr);
3419 ret = dasd_sleep_on(cqr);
3420 dasd_sfree_request(cqr, cqr->memdev);
3421 return ret;
3423 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3426 * In command mode and transport mode we need to look for sense
3427 * data in different places. The sense data itself is allways
3428 * an array of 32 bytes, so we can unify the sense data access
3429 * for both modes.
3431 char *dasd_get_sense(struct irb *irb)
3433 struct tsb *tsb = NULL;
3434 char *sense = NULL;
3436 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3437 if (irb->scsw.tm.tcw)
3438 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3439 irb->scsw.tm.tcw);
3440 if (tsb && tsb->length == 64 && tsb->flags)
3441 switch (tsb->flags & 0x07) {
3442 case 1: /* tsa_iostat */
3443 sense = tsb->tsa.iostat.sense;
3444 break;
3445 case 2: /* tsa_ddpc */
3446 sense = tsb->tsa.ddpc.sense;
3447 break;
3448 default:
3449 /* currently we don't use interrogate data */
3450 break;
3452 } else if (irb->esw.esw0.erw.cons) {
3453 sense = irb->ecw;
3455 return sense;
3457 EXPORT_SYMBOL_GPL(dasd_get_sense);
3459 static int __init dasd_init(void)
3461 int rc;
3463 init_waitqueue_head(&dasd_init_waitq);
3464 init_waitqueue_head(&dasd_flush_wq);
3465 init_waitqueue_head(&generic_waitq);
3467 /* register 'common' DASD debug area, used for all DBF_XXX calls */
3468 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3469 if (dasd_debug_area == NULL) {
3470 rc = -ENOMEM;
3471 goto failed;
3473 debug_register_view(dasd_debug_area, &debug_sprintf_view);
3474 debug_set_level(dasd_debug_area, DBF_WARNING);
3476 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3478 dasd_diag_discipline_pointer = NULL;
3480 dasd_statistics_createroot();
3482 rc = dasd_devmap_init();
3483 if (rc)
3484 goto failed;
3485 rc = dasd_gendisk_init();
3486 if (rc)
3487 goto failed;
3488 rc = dasd_parse();
3489 if (rc)
3490 goto failed;
3491 rc = dasd_eer_init();
3492 if (rc)
3493 goto failed;
3494 #ifdef CONFIG_PROC_FS
3495 rc = dasd_proc_init();
3496 if (rc)
3497 goto failed;
3498 #endif
3500 return 0;
3501 failed:
3502 pr_info("The DASD device driver could not be initialized\n");
3503 dasd_exit();
3504 return rc;
3507 module_init(dasd_init);
3508 module_exit(dasd_exit);
3510 EXPORT_SYMBOL(dasd_debug_area);
3511 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3513 EXPORT_SYMBOL(dasd_add_request_head);
3514 EXPORT_SYMBOL(dasd_add_request_tail);
3515 EXPORT_SYMBOL(dasd_cancel_req);
3516 EXPORT_SYMBOL(dasd_device_clear_timer);
3517 EXPORT_SYMBOL(dasd_block_clear_timer);
3518 EXPORT_SYMBOL(dasd_enable_device);
3519 EXPORT_SYMBOL(dasd_int_handler);
3520 EXPORT_SYMBOL(dasd_kfree_request);
3521 EXPORT_SYMBOL(dasd_kick_device);
3522 EXPORT_SYMBOL(dasd_kmalloc_request);
3523 EXPORT_SYMBOL(dasd_schedule_device_bh);
3524 EXPORT_SYMBOL(dasd_schedule_block_bh);
3525 EXPORT_SYMBOL(dasd_set_target_state);
3526 EXPORT_SYMBOL(dasd_device_set_timer);
3527 EXPORT_SYMBOL(dasd_block_set_timer);
3528 EXPORT_SYMBOL(dasd_sfree_request);
3529 EXPORT_SYMBOL(dasd_sleep_on);
3530 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3531 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3532 EXPORT_SYMBOL(dasd_smalloc_request);
3533 EXPORT_SYMBOL(dasd_start_IO);
3534 EXPORT_SYMBOL(dasd_term_IO);
3536 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3537 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3538 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3539 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3540 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3541 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3542 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3543 EXPORT_SYMBOL_GPL(dasd_alloc_block);
3544 EXPORT_SYMBOL_GPL(dasd_free_block);