Linux 4.19.133
[linux/fpc-iii.git] / drivers / s390 / block / dasd.c
bloba23e7d394a0ad1f1a74a241f1676accb9a57901a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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/kmod.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/ctype.h>
18 #include <linux/major.h>
19 #include <linux/slab.h>
20 #include <linux/hdreg.h>
21 #include <linux/async.h>
22 #include <linux/mutex.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/vmalloc.h>
27 #include <asm/ccwdev.h>
28 #include <asm/ebcdic.h>
29 #include <asm/idals.h>
30 #include <asm/itcw.h>
31 #include <asm/diag.h>
33 /* This is ugly... */
34 #define PRINTK_HEADER "dasd:"
36 #include "dasd_int.h"
38 * SECTION: Constant definitions to be used within this file
40 #define DASD_CHANQ_MAX_SIZE 4
42 #define DASD_DIAG_MOD "dasd_diag_mod"
44 static unsigned int queue_depth = 32;
45 static unsigned int nr_hw_queues = 4;
47 module_param(queue_depth, uint, 0444);
48 MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
50 module_param(nr_hw_queues, uint, 0444);
51 MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
54 * SECTION: exported variables of dasd.c
56 debug_info_t *dasd_debug_area;
57 EXPORT_SYMBOL(dasd_debug_area);
58 static struct dentry *dasd_debugfs_root_entry;
59 struct dasd_discipline *dasd_diag_discipline_pointer;
60 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
61 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
63 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
64 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
65 " Copyright IBM Corp. 2000");
66 MODULE_SUPPORTED_DEVICE("dasd");
67 MODULE_LICENSE("GPL");
70 * SECTION: prototypes for static functions of dasd.c
72 static int dasd_alloc_queue(struct dasd_block *);
73 static void dasd_setup_queue(struct dasd_block *);
74 static void dasd_free_queue(struct dasd_block *);
75 static int dasd_flush_block_queue(struct dasd_block *);
76 static void dasd_device_tasklet(unsigned long);
77 static void dasd_block_tasklet(unsigned long);
78 static void do_kick_device(struct work_struct *);
79 static void do_restore_device(struct work_struct *);
80 static void do_reload_device(struct work_struct *);
81 static void do_requeue_requests(struct work_struct *);
82 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
83 static void dasd_device_timeout(struct timer_list *);
84 static void dasd_block_timeout(struct timer_list *);
85 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
86 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
87 static void dasd_profile_exit(struct dasd_profile *);
88 static void dasd_hosts_init(struct dentry *, struct dasd_device *);
89 static void dasd_hosts_exit(struct dasd_device *);
92 * SECTION: Operations on the device structure.
94 static wait_queue_head_t dasd_init_waitq;
95 static wait_queue_head_t dasd_flush_wq;
96 static wait_queue_head_t generic_waitq;
97 static wait_queue_head_t shutdown_waitq;
100 * Allocate memory for a new device structure.
102 struct dasd_device *dasd_alloc_device(void)
104 struct dasd_device *device;
106 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
107 if (!device)
108 return ERR_PTR(-ENOMEM);
110 /* Get two pages for normal block device operations. */
111 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
112 if (!device->ccw_mem) {
113 kfree(device);
114 return ERR_PTR(-ENOMEM);
116 /* Get one page for error recovery. */
117 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
118 if (!device->erp_mem) {
119 free_pages((unsigned long) device->ccw_mem, 1);
120 kfree(device);
121 return ERR_PTR(-ENOMEM);
124 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
125 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
126 spin_lock_init(&device->mem_lock);
127 atomic_set(&device->tasklet_scheduled, 0);
128 tasklet_init(&device->tasklet, dasd_device_tasklet,
129 (unsigned long) device);
130 INIT_LIST_HEAD(&device->ccw_queue);
131 timer_setup(&device->timer, dasd_device_timeout, 0);
132 INIT_WORK(&device->kick_work, do_kick_device);
133 INIT_WORK(&device->restore_device, do_restore_device);
134 INIT_WORK(&device->reload_device, do_reload_device);
135 INIT_WORK(&device->requeue_requests, do_requeue_requests);
136 device->state = DASD_STATE_NEW;
137 device->target = DASD_STATE_NEW;
138 mutex_init(&device->state_mutex);
139 spin_lock_init(&device->profile.lock);
140 return device;
144 * Free memory of a device structure.
146 void dasd_free_device(struct dasd_device *device)
148 kfree(device->private);
149 free_page((unsigned long) device->erp_mem);
150 free_pages((unsigned long) device->ccw_mem, 1);
151 kfree(device);
155 * Allocate memory for a new device structure.
157 struct dasd_block *dasd_alloc_block(void)
159 struct dasd_block *block;
161 block = kzalloc(sizeof(*block), GFP_ATOMIC);
162 if (!block)
163 return ERR_PTR(-ENOMEM);
164 /* open_count = 0 means device online but not in use */
165 atomic_set(&block->open_count, -1);
167 atomic_set(&block->tasklet_scheduled, 0);
168 tasklet_init(&block->tasklet, dasd_block_tasklet,
169 (unsigned long) block);
170 INIT_LIST_HEAD(&block->ccw_queue);
171 spin_lock_init(&block->queue_lock);
172 timer_setup(&block->timer, dasd_block_timeout, 0);
173 spin_lock_init(&block->profile.lock);
175 return block;
177 EXPORT_SYMBOL_GPL(dasd_alloc_block);
180 * Free memory of a device structure.
182 void dasd_free_block(struct dasd_block *block)
184 kfree(block);
186 EXPORT_SYMBOL_GPL(dasd_free_block);
189 * Make a new device known to the system.
191 static int dasd_state_new_to_known(struct dasd_device *device)
193 int rc;
196 * As long as the device is not in state DASD_STATE_NEW we want to
197 * keep the reference count > 0.
199 dasd_get_device(device);
201 if (device->block) {
202 rc = dasd_alloc_queue(device->block);
203 if (rc) {
204 dasd_put_device(device);
205 return rc;
208 device->state = DASD_STATE_KNOWN;
209 return 0;
213 * Let the system forget about a device.
215 static int dasd_state_known_to_new(struct dasd_device *device)
217 /* Disable extended error reporting for this device. */
218 dasd_eer_disable(device);
219 device->state = DASD_STATE_NEW;
221 if (device->block)
222 dasd_free_queue(device->block);
224 /* Give up reference we took in dasd_state_new_to_known. */
225 dasd_put_device(device);
226 return 0;
229 static struct dentry *dasd_debugfs_setup(const char *name,
230 struct dentry *base_dentry)
232 struct dentry *pde;
234 if (!base_dentry)
235 return NULL;
236 pde = debugfs_create_dir(name, base_dentry);
237 if (!pde || IS_ERR(pde))
238 return NULL;
239 return pde;
243 * Request the irq line for the device.
245 static int dasd_state_known_to_basic(struct dasd_device *device)
247 struct dasd_block *block = device->block;
248 int rc = 0;
250 /* Allocate and register gendisk structure. */
251 if (block) {
252 rc = dasd_gendisk_alloc(block);
253 if (rc)
254 return rc;
255 block->debugfs_dentry =
256 dasd_debugfs_setup(block->gdp->disk_name,
257 dasd_debugfs_root_entry);
258 dasd_profile_init(&block->profile, block->debugfs_dentry);
259 if (dasd_global_profile_level == DASD_PROFILE_ON)
260 dasd_profile_on(&device->block->profile);
262 device->debugfs_dentry =
263 dasd_debugfs_setup(dev_name(&device->cdev->dev),
264 dasd_debugfs_root_entry);
265 dasd_profile_init(&device->profile, device->debugfs_dentry);
266 dasd_hosts_init(device->debugfs_dentry, device);
268 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
269 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
270 8 * sizeof(long));
271 debug_register_view(device->debug_area, &debug_sprintf_view);
272 debug_set_level(device->debug_area, DBF_WARNING);
273 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
275 device->state = DASD_STATE_BASIC;
277 return rc;
281 * Release the irq line for the device. Terminate any running i/o.
283 static int dasd_state_basic_to_known(struct dasd_device *device)
285 int rc;
287 if (device->discipline->basic_to_known) {
288 rc = device->discipline->basic_to_known(device);
289 if (rc)
290 return rc;
293 if (device->block) {
294 dasd_profile_exit(&device->block->profile);
295 debugfs_remove(device->block->debugfs_dentry);
296 dasd_gendisk_free(device->block);
297 dasd_block_clear_timer(device->block);
299 rc = dasd_flush_device_queue(device);
300 if (rc)
301 return rc;
302 dasd_device_clear_timer(device);
303 dasd_profile_exit(&device->profile);
304 dasd_hosts_exit(device);
305 debugfs_remove(device->debugfs_dentry);
306 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
307 if (device->debug_area != NULL) {
308 debug_unregister(device->debug_area);
309 device->debug_area = NULL;
311 device->state = DASD_STATE_KNOWN;
312 return 0;
316 * Do the initial analysis. The do_analysis function may return
317 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
318 * until the discipline decides to continue the startup sequence
319 * by calling the function dasd_change_state. The eckd disciplines
320 * uses this to start a ccw that detects the format. The completion
321 * interrupt for this detection ccw uses the kernel event daemon to
322 * trigger the call to dasd_change_state. All this is done in the
323 * discipline code, see dasd_eckd.c.
324 * After the analysis ccw is done (do_analysis returned 0) the block
325 * device is setup.
326 * In case the analysis returns an error, the device setup is stopped
327 * (a fake disk was already added to allow formatting).
329 static int dasd_state_basic_to_ready(struct dasd_device *device)
331 int rc;
332 struct dasd_block *block;
333 struct gendisk *disk;
335 rc = 0;
336 block = device->block;
337 /* make disk known with correct capacity */
338 if (block) {
339 if (block->base->discipline->do_analysis != NULL)
340 rc = block->base->discipline->do_analysis(block);
341 if (rc) {
342 if (rc != -EAGAIN) {
343 device->state = DASD_STATE_UNFMT;
344 disk = device->block->gdp;
345 kobject_uevent(&disk_to_dev(disk)->kobj,
346 KOBJ_CHANGE);
347 goto out;
349 return rc;
351 dasd_setup_queue(block);
352 set_capacity(block->gdp,
353 block->blocks << block->s2b_shift);
354 device->state = DASD_STATE_READY;
355 rc = dasd_scan_partitions(block);
356 if (rc) {
357 device->state = DASD_STATE_BASIC;
358 return rc;
360 } else {
361 device->state = DASD_STATE_READY;
363 out:
364 if (device->discipline->basic_to_ready)
365 rc = device->discipline->basic_to_ready(device);
366 return rc;
369 static inline
370 int _wait_for_empty_queues(struct dasd_device *device)
372 if (device->block)
373 return list_empty(&device->ccw_queue) &&
374 list_empty(&device->block->ccw_queue);
375 else
376 return list_empty(&device->ccw_queue);
380 * Remove device from block device layer. Destroy dirty buffers.
381 * Forget format information. Check if the target level is basic
382 * and if it is create fake disk for formatting.
384 static int dasd_state_ready_to_basic(struct dasd_device *device)
386 int rc;
388 device->state = DASD_STATE_BASIC;
389 if (device->block) {
390 struct dasd_block *block = device->block;
391 rc = dasd_flush_block_queue(block);
392 if (rc) {
393 device->state = DASD_STATE_READY;
394 return rc;
396 dasd_destroy_partitions(block);
397 block->blocks = 0;
398 block->bp_block = 0;
399 block->s2b_shift = 0;
401 return 0;
405 * Back to basic.
407 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
409 device->state = DASD_STATE_BASIC;
410 return 0;
414 * Make the device online and schedule the bottom half to start
415 * the requeueing of requests from the linux request queue to the
416 * ccw queue.
418 static int
419 dasd_state_ready_to_online(struct dasd_device * device)
421 struct gendisk *disk;
422 struct disk_part_iter piter;
423 struct hd_struct *part;
425 device->state = DASD_STATE_ONLINE;
426 if (device->block) {
427 dasd_schedule_block_bh(device->block);
428 if ((device->features & DASD_FEATURE_USERAW)) {
429 disk = device->block->gdp;
430 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
431 return 0;
433 disk = device->block->bdev->bd_disk;
434 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
435 while ((part = disk_part_iter_next(&piter)))
436 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
437 disk_part_iter_exit(&piter);
439 return 0;
443 * Stop the requeueing of requests again.
445 static int dasd_state_online_to_ready(struct dasd_device *device)
447 int rc;
448 struct gendisk *disk;
449 struct disk_part_iter piter;
450 struct hd_struct *part;
452 if (device->discipline->online_to_ready) {
453 rc = device->discipline->online_to_ready(device);
454 if (rc)
455 return rc;
458 device->state = DASD_STATE_READY;
459 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
460 disk = device->block->bdev->bd_disk;
461 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
462 while ((part = disk_part_iter_next(&piter)))
463 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
464 disk_part_iter_exit(&piter);
466 return 0;
470 * Device startup state changes.
472 static int dasd_increase_state(struct dasd_device *device)
474 int rc;
476 rc = 0;
477 if (device->state == DASD_STATE_NEW &&
478 device->target >= DASD_STATE_KNOWN)
479 rc = dasd_state_new_to_known(device);
481 if (!rc &&
482 device->state == DASD_STATE_KNOWN &&
483 device->target >= DASD_STATE_BASIC)
484 rc = dasd_state_known_to_basic(device);
486 if (!rc &&
487 device->state == DASD_STATE_BASIC &&
488 device->target >= DASD_STATE_READY)
489 rc = dasd_state_basic_to_ready(device);
491 if (!rc &&
492 device->state == DASD_STATE_UNFMT &&
493 device->target > DASD_STATE_UNFMT)
494 rc = -EPERM;
496 if (!rc &&
497 device->state == DASD_STATE_READY &&
498 device->target >= DASD_STATE_ONLINE)
499 rc = dasd_state_ready_to_online(device);
501 return rc;
505 * Device shutdown state changes.
507 static int dasd_decrease_state(struct dasd_device *device)
509 int rc;
511 rc = 0;
512 if (device->state == DASD_STATE_ONLINE &&
513 device->target <= DASD_STATE_READY)
514 rc = dasd_state_online_to_ready(device);
516 if (!rc &&
517 device->state == DASD_STATE_READY &&
518 device->target <= DASD_STATE_BASIC)
519 rc = dasd_state_ready_to_basic(device);
521 if (!rc &&
522 device->state == DASD_STATE_UNFMT &&
523 device->target <= DASD_STATE_BASIC)
524 rc = dasd_state_unfmt_to_basic(device);
526 if (!rc &&
527 device->state == DASD_STATE_BASIC &&
528 device->target <= DASD_STATE_KNOWN)
529 rc = dasd_state_basic_to_known(device);
531 if (!rc &&
532 device->state == DASD_STATE_KNOWN &&
533 device->target <= DASD_STATE_NEW)
534 rc = dasd_state_known_to_new(device);
536 return rc;
540 * This is the main startup/shutdown routine.
542 static void dasd_change_state(struct dasd_device *device)
544 int rc;
546 if (device->state == device->target)
547 /* Already where we want to go today... */
548 return;
549 if (device->state < device->target)
550 rc = dasd_increase_state(device);
551 else
552 rc = dasd_decrease_state(device);
553 if (rc == -EAGAIN)
554 return;
555 if (rc)
556 device->target = device->state;
558 /* let user-space know that the device status changed */
559 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
561 if (device->state == device->target)
562 wake_up(&dasd_init_waitq);
566 * Kick starter for devices that did not complete the startup/shutdown
567 * procedure or were sleeping because of a pending state.
568 * dasd_kick_device will schedule a call do do_kick_device to the kernel
569 * event daemon.
571 static void do_kick_device(struct work_struct *work)
573 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
574 mutex_lock(&device->state_mutex);
575 dasd_change_state(device);
576 mutex_unlock(&device->state_mutex);
577 dasd_schedule_device_bh(device);
578 dasd_put_device(device);
581 void dasd_kick_device(struct dasd_device *device)
583 dasd_get_device(device);
584 /* queue call to dasd_kick_device to the kernel event daemon. */
585 if (!schedule_work(&device->kick_work))
586 dasd_put_device(device);
588 EXPORT_SYMBOL(dasd_kick_device);
591 * dasd_reload_device will schedule a call do do_reload_device to the kernel
592 * event daemon.
594 static void do_reload_device(struct work_struct *work)
596 struct dasd_device *device = container_of(work, struct dasd_device,
597 reload_device);
598 device->discipline->reload(device);
599 dasd_put_device(device);
602 void dasd_reload_device(struct dasd_device *device)
604 dasd_get_device(device);
605 /* queue call to dasd_reload_device to the kernel event daemon. */
606 if (!schedule_work(&device->reload_device))
607 dasd_put_device(device);
609 EXPORT_SYMBOL(dasd_reload_device);
612 * dasd_restore_device will schedule a call do do_restore_device to the kernel
613 * event daemon.
615 static void do_restore_device(struct work_struct *work)
617 struct dasd_device *device = container_of(work, struct dasd_device,
618 restore_device);
619 device->cdev->drv->restore(device->cdev);
620 dasd_put_device(device);
623 void dasd_restore_device(struct dasd_device *device)
625 dasd_get_device(device);
626 /* queue call to dasd_restore_device to the kernel event daemon. */
627 if (!schedule_work(&device->restore_device))
628 dasd_put_device(device);
632 * Set the target state for a device and starts the state change.
634 void dasd_set_target_state(struct dasd_device *device, int target)
636 dasd_get_device(device);
637 mutex_lock(&device->state_mutex);
638 /* If we are in probeonly mode stop at DASD_STATE_READY. */
639 if (dasd_probeonly && target > DASD_STATE_READY)
640 target = DASD_STATE_READY;
641 if (device->target != target) {
642 if (device->state == target)
643 wake_up(&dasd_init_waitq);
644 device->target = target;
646 if (device->state != device->target)
647 dasd_change_state(device);
648 mutex_unlock(&device->state_mutex);
649 dasd_put_device(device);
651 EXPORT_SYMBOL(dasd_set_target_state);
654 * Enable devices with device numbers in [from..to].
656 static inline int _wait_for_device(struct dasd_device *device)
658 return (device->state == device->target);
661 void dasd_enable_device(struct dasd_device *device)
663 dasd_set_target_state(device, DASD_STATE_ONLINE);
664 if (device->state <= DASD_STATE_KNOWN)
665 /* No discipline for device found. */
666 dasd_set_target_state(device, DASD_STATE_NEW);
667 /* Now wait for the devices to come up. */
668 wait_event(dasd_init_waitq, _wait_for_device(device));
670 dasd_reload_device(device);
671 if (device->discipline->kick_validate)
672 device->discipline->kick_validate(device);
674 EXPORT_SYMBOL(dasd_enable_device);
677 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
680 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
682 #ifdef CONFIG_DASD_PROFILE
683 struct dasd_profile dasd_global_profile = {
684 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
686 static struct dentry *dasd_debugfs_global_entry;
689 * Add profiling information for cqr before execution.
691 static void dasd_profile_start(struct dasd_block *block,
692 struct dasd_ccw_req *cqr,
693 struct request *req)
695 struct list_head *l;
696 unsigned int counter;
697 struct dasd_device *device;
699 /* count the length of the chanq for statistics */
700 counter = 0;
701 if (dasd_global_profile_level || block->profile.data)
702 list_for_each(l, &block->ccw_queue)
703 if (++counter >= 31)
704 break;
706 spin_lock(&dasd_global_profile.lock);
707 if (dasd_global_profile.data) {
708 dasd_global_profile.data->dasd_io_nr_req[counter]++;
709 if (rq_data_dir(req) == READ)
710 dasd_global_profile.data->dasd_read_nr_req[counter]++;
712 spin_unlock(&dasd_global_profile.lock);
714 spin_lock(&block->profile.lock);
715 if (block->profile.data) {
716 block->profile.data->dasd_io_nr_req[counter]++;
717 if (rq_data_dir(req) == READ)
718 block->profile.data->dasd_read_nr_req[counter]++;
720 spin_unlock(&block->profile.lock);
723 * We count the request for the start device, even though it may run on
724 * some other device due to error recovery. This way we make sure that
725 * we count each request only once.
727 device = cqr->startdev;
728 if (device->profile.data) {
729 counter = 1; /* request is not yet queued on the start device */
730 list_for_each(l, &device->ccw_queue)
731 if (++counter >= 31)
732 break;
734 spin_lock(&device->profile.lock);
735 if (device->profile.data) {
736 device->profile.data->dasd_io_nr_req[counter]++;
737 if (rq_data_dir(req) == READ)
738 device->profile.data->dasd_read_nr_req[counter]++;
740 spin_unlock(&device->profile.lock);
744 * Add profiling information for cqr after execution.
747 #define dasd_profile_counter(value, index) \
749 for (index = 0; index < 31 && value >> (2+index); index++) \
753 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
754 int is_alias,
755 int is_tpm,
756 int is_read,
757 long sectors,
758 int sectors_ind,
759 int tottime_ind,
760 int tottimeps_ind,
761 int strtime_ind,
762 int irqtime_ind,
763 int irqtimeps_ind,
764 int endtime_ind)
766 /* in case of an overflow, reset the whole profile */
767 if (data->dasd_io_reqs == UINT_MAX) {
768 memset(data, 0, sizeof(*data));
769 ktime_get_real_ts64(&data->starttod);
771 data->dasd_io_reqs++;
772 data->dasd_io_sects += sectors;
773 if (is_alias)
774 data->dasd_io_alias++;
775 if (is_tpm)
776 data->dasd_io_tpm++;
778 data->dasd_io_secs[sectors_ind]++;
779 data->dasd_io_times[tottime_ind]++;
780 data->dasd_io_timps[tottimeps_ind]++;
781 data->dasd_io_time1[strtime_ind]++;
782 data->dasd_io_time2[irqtime_ind]++;
783 data->dasd_io_time2ps[irqtimeps_ind]++;
784 data->dasd_io_time3[endtime_ind]++;
786 if (is_read) {
787 data->dasd_read_reqs++;
788 data->dasd_read_sects += sectors;
789 if (is_alias)
790 data->dasd_read_alias++;
791 if (is_tpm)
792 data->dasd_read_tpm++;
793 data->dasd_read_secs[sectors_ind]++;
794 data->dasd_read_times[tottime_ind]++;
795 data->dasd_read_time1[strtime_ind]++;
796 data->dasd_read_time2[irqtime_ind]++;
797 data->dasd_read_time3[endtime_ind]++;
801 static void dasd_profile_end(struct dasd_block *block,
802 struct dasd_ccw_req *cqr,
803 struct request *req)
805 unsigned long strtime, irqtime, endtime, tottime;
806 unsigned long tottimeps, sectors;
807 struct dasd_device *device;
808 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
809 int irqtime_ind, irqtimeps_ind, endtime_ind;
810 struct dasd_profile_info *data;
812 device = cqr->startdev;
813 if (!(dasd_global_profile_level ||
814 block->profile.data ||
815 device->profile.data))
816 return;
818 sectors = blk_rq_sectors(req);
819 if (!cqr->buildclk || !cqr->startclk ||
820 !cqr->stopclk || !cqr->endclk ||
821 !sectors)
822 return;
824 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
825 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
826 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
827 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
828 tottimeps = tottime / sectors;
830 dasd_profile_counter(sectors, sectors_ind);
831 dasd_profile_counter(tottime, tottime_ind);
832 dasd_profile_counter(tottimeps, tottimeps_ind);
833 dasd_profile_counter(strtime, strtime_ind);
834 dasd_profile_counter(irqtime, irqtime_ind);
835 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
836 dasd_profile_counter(endtime, endtime_ind);
838 spin_lock(&dasd_global_profile.lock);
839 if (dasd_global_profile.data) {
840 data = dasd_global_profile.data;
841 data->dasd_sum_times += tottime;
842 data->dasd_sum_time_str += strtime;
843 data->dasd_sum_time_irq += irqtime;
844 data->dasd_sum_time_end += endtime;
845 dasd_profile_end_add_data(dasd_global_profile.data,
846 cqr->startdev != block->base,
847 cqr->cpmode == 1,
848 rq_data_dir(req) == READ,
849 sectors, sectors_ind, tottime_ind,
850 tottimeps_ind, strtime_ind,
851 irqtime_ind, irqtimeps_ind,
852 endtime_ind);
854 spin_unlock(&dasd_global_profile.lock);
856 spin_lock(&block->profile.lock);
857 if (block->profile.data) {
858 data = block->profile.data;
859 data->dasd_sum_times += tottime;
860 data->dasd_sum_time_str += strtime;
861 data->dasd_sum_time_irq += irqtime;
862 data->dasd_sum_time_end += endtime;
863 dasd_profile_end_add_data(block->profile.data,
864 cqr->startdev != block->base,
865 cqr->cpmode == 1,
866 rq_data_dir(req) == READ,
867 sectors, sectors_ind, tottime_ind,
868 tottimeps_ind, strtime_ind,
869 irqtime_ind, irqtimeps_ind,
870 endtime_ind);
872 spin_unlock(&block->profile.lock);
874 spin_lock(&device->profile.lock);
875 if (device->profile.data) {
876 data = device->profile.data;
877 data->dasd_sum_times += tottime;
878 data->dasd_sum_time_str += strtime;
879 data->dasd_sum_time_irq += irqtime;
880 data->dasd_sum_time_end += endtime;
881 dasd_profile_end_add_data(device->profile.data,
882 cqr->startdev != block->base,
883 cqr->cpmode == 1,
884 rq_data_dir(req) == READ,
885 sectors, sectors_ind, tottime_ind,
886 tottimeps_ind, strtime_ind,
887 irqtime_ind, irqtimeps_ind,
888 endtime_ind);
890 spin_unlock(&device->profile.lock);
893 void dasd_profile_reset(struct dasd_profile *profile)
895 struct dasd_profile_info *data;
897 spin_lock_bh(&profile->lock);
898 data = profile->data;
899 if (!data) {
900 spin_unlock_bh(&profile->lock);
901 return;
903 memset(data, 0, sizeof(*data));
904 ktime_get_real_ts64(&data->starttod);
905 spin_unlock_bh(&profile->lock);
908 int dasd_profile_on(struct dasd_profile *profile)
910 struct dasd_profile_info *data;
912 data = kzalloc(sizeof(*data), GFP_KERNEL);
913 if (!data)
914 return -ENOMEM;
915 spin_lock_bh(&profile->lock);
916 if (profile->data) {
917 spin_unlock_bh(&profile->lock);
918 kfree(data);
919 return 0;
921 ktime_get_real_ts64(&data->starttod);
922 profile->data = data;
923 spin_unlock_bh(&profile->lock);
924 return 0;
927 void dasd_profile_off(struct dasd_profile *profile)
929 spin_lock_bh(&profile->lock);
930 kfree(profile->data);
931 profile->data = NULL;
932 spin_unlock_bh(&profile->lock);
935 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
937 char *buffer;
939 buffer = vmalloc(user_len + 1);
940 if (buffer == NULL)
941 return ERR_PTR(-ENOMEM);
942 if (copy_from_user(buffer, user_buf, user_len) != 0) {
943 vfree(buffer);
944 return ERR_PTR(-EFAULT);
946 /* got the string, now strip linefeed. */
947 if (buffer[user_len - 1] == '\n')
948 buffer[user_len - 1] = 0;
949 else
950 buffer[user_len] = 0;
951 return buffer;
954 static ssize_t dasd_stats_write(struct file *file,
955 const char __user *user_buf,
956 size_t user_len, loff_t *pos)
958 char *buffer, *str;
959 int rc;
960 struct seq_file *m = (struct seq_file *)file->private_data;
961 struct dasd_profile *prof = m->private;
963 if (user_len > 65536)
964 user_len = 65536;
965 buffer = dasd_get_user_string(user_buf, user_len);
966 if (IS_ERR(buffer))
967 return PTR_ERR(buffer);
969 str = skip_spaces(buffer);
970 rc = user_len;
971 if (strncmp(str, "reset", 5) == 0) {
972 dasd_profile_reset(prof);
973 } else if (strncmp(str, "on", 2) == 0) {
974 rc = dasd_profile_on(prof);
975 if (rc)
976 goto out;
977 rc = user_len;
978 if (prof == &dasd_global_profile) {
979 dasd_profile_reset(prof);
980 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
982 } else if (strncmp(str, "off", 3) == 0) {
983 if (prof == &dasd_global_profile)
984 dasd_global_profile_level = DASD_PROFILE_OFF;
985 dasd_profile_off(prof);
986 } else
987 rc = -EINVAL;
988 out:
989 vfree(buffer);
990 return rc;
993 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
995 int i;
997 for (i = 0; i < 32; i++)
998 seq_printf(m, "%u ", array[i]);
999 seq_putc(m, '\n');
1002 static void dasd_stats_seq_print(struct seq_file *m,
1003 struct dasd_profile_info *data)
1005 seq_printf(m, "start_time %lld.%09ld\n",
1006 (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
1007 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
1008 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1009 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1010 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
1011 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1012 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1013 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1014 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1015 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1016 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1017 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1018 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
1019 seq_puts(m, "histogram_sectors ");
1020 dasd_stats_array(m, data->dasd_io_secs);
1021 seq_puts(m, "histogram_io_times ");
1022 dasd_stats_array(m, data->dasd_io_times);
1023 seq_puts(m, "histogram_io_times_weighted ");
1024 dasd_stats_array(m, data->dasd_io_timps);
1025 seq_puts(m, "histogram_time_build_to_ssch ");
1026 dasd_stats_array(m, data->dasd_io_time1);
1027 seq_puts(m, "histogram_time_ssch_to_irq ");
1028 dasd_stats_array(m, data->dasd_io_time2);
1029 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
1030 dasd_stats_array(m, data->dasd_io_time2ps);
1031 seq_puts(m, "histogram_time_irq_to_end ");
1032 dasd_stats_array(m, data->dasd_io_time3);
1033 seq_puts(m, "histogram_ccw_queue_length ");
1034 dasd_stats_array(m, data->dasd_io_nr_req);
1035 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1036 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1037 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1038 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
1039 seq_puts(m, "histogram_read_sectors ");
1040 dasd_stats_array(m, data->dasd_read_secs);
1041 seq_puts(m, "histogram_read_times ");
1042 dasd_stats_array(m, data->dasd_read_times);
1043 seq_puts(m, "histogram_read_time_build_to_ssch ");
1044 dasd_stats_array(m, data->dasd_read_time1);
1045 seq_puts(m, "histogram_read_time_ssch_to_irq ");
1046 dasd_stats_array(m, data->dasd_read_time2);
1047 seq_puts(m, "histogram_read_time_irq_to_end ");
1048 dasd_stats_array(m, data->dasd_read_time3);
1049 seq_puts(m, "histogram_read_ccw_queue_length ");
1050 dasd_stats_array(m, data->dasd_read_nr_req);
1053 static int dasd_stats_show(struct seq_file *m, void *v)
1055 struct dasd_profile *profile;
1056 struct dasd_profile_info *data;
1058 profile = m->private;
1059 spin_lock_bh(&profile->lock);
1060 data = profile->data;
1061 if (!data) {
1062 spin_unlock_bh(&profile->lock);
1063 seq_puts(m, "disabled\n");
1064 return 0;
1066 dasd_stats_seq_print(m, data);
1067 spin_unlock_bh(&profile->lock);
1068 return 0;
1071 static int dasd_stats_open(struct inode *inode, struct file *file)
1073 struct dasd_profile *profile = inode->i_private;
1074 return single_open(file, dasd_stats_show, profile);
1077 static const struct file_operations dasd_stats_raw_fops = {
1078 .owner = THIS_MODULE,
1079 .open = dasd_stats_open,
1080 .read = seq_read,
1081 .llseek = seq_lseek,
1082 .release = single_release,
1083 .write = dasd_stats_write,
1086 static void dasd_profile_init(struct dasd_profile *profile,
1087 struct dentry *base_dentry)
1089 umode_t mode;
1090 struct dentry *pde;
1092 if (!base_dentry)
1093 return;
1094 profile->dentry = NULL;
1095 profile->data = NULL;
1096 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1097 pde = debugfs_create_file("statistics", mode, base_dentry,
1098 profile, &dasd_stats_raw_fops);
1099 if (pde && !IS_ERR(pde))
1100 profile->dentry = pde;
1101 return;
1104 static void dasd_profile_exit(struct dasd_profile *profile)
1106 dasd_profile_off(profile);
1107 debugfs_remove(profile->dentry);
1108 profile->dentry = NULL;
1111 static void dasd_statistics_removeroot(void)
1113 dasd_global_profile_level = DASD_PROFILE_OFF;
1114 dasd_profile_exit(&dasd_global_profile);
1115 debugfs_remove(dasd_debugfs_global_entry);
1116 debugfs_remove(dasd_debugfs_root_entry);
1119 static void dasd_statistics_createroot(void)
1121 struct dentry *pde;
1123 dasd_debugfs_root_entry = NULL;
1124 pde = debugfs_create_dir("dasd", NULL);
1125 if (!pde || IS_ERR(pde))
1126 goto error;
1127 dasd_debugfs_root_entry = pde;
1128 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1129 if (!pde || IS_ERR(pde))
1130 goto error;
1131 dasd_debugfs_global_entry = pde;
1132 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
1133 return;
1135 error:
1136 DBF_EVENT(DBF_ERR, "%s",
1137 "Creation of the dasd debugfs interface failed");
1138 dasd_statistics_removeroot();
1139 return;
1142 #else
1143 #define dasd_profile_start(block, cqr, req) do {} while (0)
1144 #define dasd_profile_end(block, cqr, req) do {} while (0)
1146 static void dasd_statistics_createroot(void)
1148 return;
1151 static void dasd_statistics_removeroot(void)
1153 return;
1156 int dasd_stats_generic_show(struct seq_file *m, void *v)
1158 seq_puts(m, "Statistics are not activated in this kernel\n");
1159 return 0;
1162 static void dasd_profile_init(struct dasd_profile *profile,
1163 struct dentry *base_dentry)
1165 return;
1168 static void dasd_profile_exit(struct dasd_profile *profile)
1170 return;
1173 int dasd_profile_on(struct dasd_profile *profile)
1175 return 0;
1178 #endif /* CONFIG_DASD_PROFILE */
1180 static int dasd_hosts_show(struct seq_file *m, void *v)
1182 struct dasd_device *device;
1183 int rc = -EOPNOTSUPP;
1185 device = m->private;
1186 dasd_get_device(device);
1188 if (device->discipline->hosts_print)
1189 rc = device->discipline->hosts_print(device, m);
1191 dasd_put_device(device);
1192 return rc;
1195 static int dasd_hosts_open(struct inode *inode, struct file *file)
1197 struct dasd_device *device = inode->i_private;
1199 return single_open(file, dasd_hosts_show, device);
1202 static const struct file_operations dasd_hosts_fops = {
1203 .owner = THIS_MODULE,
1204 .open = dasd_hosts_open,
1205 .read = seq_read,
1206 .llseek = seq_lseek,
1207 .release = single_release,
1210 static void dasd_hosts_exit(struct dasd_device *device)
1212 debugfs_remove(device->hosts_dentry);
1213 device->hosts_dentry = NULL;
1216 static void dasd_hosts_init(struct dentry *base_dentry,
1217 struct dasd_device *device)
1219 struct dentry *pde;
1220 umode_t mode;
1222 if (!base_dentry)
1223 return;
1225 mode = S_IRUSR | S_IFREG;
1226 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1227 device, &dasd_hosts_fops);
1228 if (pde && !IS_ERR(pde))
1229 device->hosts_dentry = pde;
1232 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
1233 struct dasd_device *device,
1234 struct dasd_ccw_req *cqr)
1236 unsigned long flags;
1237 char *data, *chunk;
1238 int size = 0;
1240 if (cplength > 0)
1241 size += cplength * sizeof(struct ccw1);
1242 if (datasize > 0)
1243 size += datasize;
1244 if (!cqr)
1245 size += (sizeof(*cqr) + 7L) & -8L;
1247 spin_lock_irqsave(&device->mem_lock, flags);
1248 data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
1249 spin_unlock_irqrestore(&device->mem_lock, flags);
1250 if (!chunk)
1251 return ERR_PTR(-ENOMEM);
1252 if (!cqr) {
1253 cqr = (void *) data;
1254 data += (sizeof(*cqr) + 7L) & -8L;
1256 memset(cqr, 0, sizeof(*cqr));
1257 cqr->mem_chunk = chunk;
1258 if (cplength > 0) {
1259 cqr->cpaddr = data;
1260 data += cplength * sizeof(struct ccw1);
1261 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1263 if (datasize > 0) {
1264 cqr->data = data;
1265 memset(cqr->data, 0, datasize);
1267 cqr->magic = magic;
1268 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1269 dasd_get_device(device);
1270 return cqr;
1272 EXPORT_SYMBOL(dasd_smalloc_request);
1274 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1276 unsigned long flags;
1278 spin_lock_irqsave(&device->mem_lock, flags);
1279 dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
1280 spin_unlock_irqrestore(&device->mem_lock, flags);
1281 dasd_put_device(device);
1283 EXPORT_SYMBOL(dasd_sfree_request);
1286 * Check discipline magic in cqr.
1288 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1290 struct dasd_device *device;
1292 if (cqr == NULL)
1293 return -EINVAL;
1294 device = cqr->startdev;
1295 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1296 DBF_DEV_EVENT(DBF_WARNING, device,
1297 " dasd_ccw_req 0x%08x magic doesn't match"
1298 " discipline 0x%08x",
1299 cqr->magic,
1300 *(unsigned int *) device->discipline->name);
1301 return -EINVAL;
1303 return 0;
1307 * Terminate the current i/o and set the request to clear_pending.
1308 * Timer keeps device runnig.
1309 * ccw_device_clear can fail if the i/o subsystem
1310 * is in a bad mood.
1312 int dasd_term_IO(struct dasd_ccw_req *cqr)
1314 struct dasd_device *device;
1315 int retries, rc;
1316 char errorstring[ERRORLENGTH];
1318 /* Check the cqr */
1319 rc = dasd_check_cqr(cqr);
1320 if (rc)
1321 return rc;
1322 retries = 0;
1323 device = (struct dasd_device *) cqr->startdev;
1324 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1325 rc = ccw_device_clear(device->cdev, (long) cqr);
1326 switch (rc) {
1327 case 0: /* termination successful */
1328 cqr->status = DASD_CQR_CLEAR_PENDING;
1329 cqr->stopclk = get_tod_clock();
1330 cqr->starttime = 0;
1331 DBF_DEV_EVENT(DBF_DEBUG, device,
1332 "terminate cqr %p successful",
1333 cqr);
1334 break;
1335 case -ENODEV:
1336 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1337 "device gone, retry");
1338 break;
1339 case -EINVAL:
1341 * device not valid so no I/O could be running
1342 * handle CQR as termination successful
1344 cqr->status = DASD_CQR_CLEARED;
1345 cqr->stopclk = get_tod_clock();
1346 cqr->starttime = 0;
1347 /* no retries for invalid devices */
1348 cqr->retries = -1;
1349 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1350 "EINVAL, handle as terminated");
1351 /* fake rc to success */
1352 rc = 0;
1353 break;
1354 default:
1355 /* internal error 10 - unknown rc*/
1356 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1357 dev_err(&device->cdev->dev, "An error occurred in the "
1358 "DASD device driver, reason=%s\n", errorstring);
1359 BUG();
1360 break;
1362 retries++;
1364 dasd_schedule_device_bh(device);
1365 return rc;
1367 EXPORT_SYMBOL(dasd_term_IO);
1370 * Start the i/o. This start_IO can fail if the channel is really busy.
1371 * In that case set up a timer to start the request later.
1373 int dasd_start_IO(struct dasd_ccw_req *cqr)
1375 struct dasd_device *device;
1376 int rc;
1377 char errorstring[ERRORLENGTH];
1379 /* Check the cqr */
1380 rc = dasd_check_cqr(cqr);
1381 if (rc) {
1382 cqr->intrc = rc;
1383 return rc;
1385 device = (struct dasd_device *) cqr->startdev;
1386 if (((cqr->block &&
1387 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1388 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1389 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1390 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1391 "because of stolen lock", cqr);
1392 cqr->status = DASD_CQR_ERROR;
1393 cqr->intrc = -EPERM;
1394 return -EPERM;
1396 if (cqr->retries < 0) {
1397 /* internal error 14 - start_IO run out of retries */
1398 sprintf(errorstring, "14 %p", cqr);
1399 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1400 "device driver, reason=%s\n", errorstring);
1401 cqr->status = DASD_CQR_ERROR;
1402 return -EIO;
1404 cqr->startclk = get_tod_clock();
1405 cqr->starttime = jiffies;
1406 cqr->retries--;
1407 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1408 cqr->lpm &= dasd_path_get_opm(device);
1409 if (!cqr->lpm)
1410 cqr->lpm = dasd_path_get_opm(device);
1412 if (cqr->cpmode == 1) {
1413 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1414 (long) cqr, cqr->lpm);
1415 } else {
1416 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1417 (long) cqr, cqr->lpm, 0);
1419 switch (rc) {
1420 case 0:
1421 cqr->status = DASD_CQR_IN_IO;
1422 break;
1423 case -EBUSY:
1424 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1425 "start_IO: device busy, retry later");
1426 break;
1427 case -EACCES:
1428 /* -EACCES indicates that the request used only a subset of the
1429 * available paths and all these paths are gone. If the lpm of
1430 * this request was only a subset of the opm (e.g. the ppm) then
1431 * we just do a retry with all available paths.
1432 * If we already use the full opm, something is amiss, and we
1433 * need a full path verification.
1435 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1436 DBF_DEV_EVENT(DBF_WARNING, device,
1437 "start_IO: selected paths gone (%x)",
1438 cqr->lpm);
1439 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1440 cqr->lpm = dasd_path_get_opm(device);
1441 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1442 "start_IO: selected paths gone,"
1443 " retry on all paths");
1444 } else {
1445 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1446 "start_IO: all paths in opm gone,"
1447 " do path verification");
1448 dasd_generic_last_path_gone(device);
1449 dasd_path_no_path(device);
1450 dasd_path_set_tbvpm(device,
1451 ccw_device_get_path_mask(
1452 device->cdev));
1454 break;
1455 case -ENODEV:
1456 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1457 "start_IO: -ENODEV device gone, retry");
1458 break;
1459 case -EIO:
1460 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1461 "start_IO: -EIO device gone, retry");
1462 break;
1463 case -EINVAL:
1464 /* most likely caused in power management context */
1465 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1466 "start_IO: -EINVAL device currently "
1467 "not accessible");
1468 break;
1469 default:
1470 /* internal error 11 - unknown rc */
1471 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1472 dev_err(&device->cdev->dev,
1473 "An error occurred in the DASD device driver, "
1474 "reason=%s\n", errorstring);
1475 BUG();
1476 break;
1478 cqr->intrc = rc;
1479 return rc;
1481 EXPORT_SYMBOL(dasd_start_IO);
1484 * Timeout function for dasd devices. This is used for different purposes
1485 * 1) missing interrupt handler for normal operation
1486 * 2) delayed start of request where start_IO failed with -EBUSY
1487 * 3) timeout for missing state change interrupts
1488 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1489 * DASD_CQR_QUEUED for 2) and 3).
1491 static void dasd_device_timeout(struct timer_list *t)
1493 unsigned long flags;
1494 struct dasd_device *device;
1496 device = from_timer(device, t, timer);
1497 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1498 /* re-activate request queue */
1499 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1500 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1501 dasd_schedule_device_bh(device);
1505 * Setup timeout for a device in jiffies.
1507 void dasd_device_set_timer(struct dasd_device *device, int expires)
1509 if (expires == 0)
1510 del_timer(&device->timer);
1511 else
1512 mod_timer(&device->timer, jiffies + expires);
1514 EXPORT_SYMBOL(dasd_device_set_timer);
1517 * Clear timeout for a device.
1519 void dasd_device_clear_timer(struct dasd_device *device)
1521 del_timer(&device->timer);
1523 EXPORT_SYMBOL(dasd_device_clear_timer);
1525 static void dasd_handle_killed_request(struct ccw_device *cdev,
1526 unsigned long intparm)
1528 struct dasd_ccw_req *cqr;
1529 struct dasd_device *device;
1531 if (!intparm)
1532 return;
1533 cqr = (struct dasd_ccw_req *) intparm;
1534 if (cqr->status != DASD_CQR_IN_IO) {
1535 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1536 "invalid status in handle_killed_request: "
1537 "%02x", cqr->status);
1538 return;
1541 device = dasd_device_from_cdev_locked(cdev);
1542 if (IS_ERR(device)) {
1543 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1544 "unable to get device from cdev");
1545 return;
1548 if (!cqr->startdev ||
1549 device != cqr->startdev ||
1550 strncmp(cqr->startdev->discipline->ebcname,
1551 (char *) &cqr->magic, 4)) {
1552 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1553 "invalid device in request");
1554 dasd_put_device(device);
1555 return;
1558 /* Schedule request to be retried. */
1559 cqr->status = DASD_CQR_QUEUED;
1561 dasd_device_clear_timer(device);
1562 dasd_schedule_device_bh(device);
1563 dasd_put_device(device);
1566 void dasd_generic_handle_state_change(struct dasd_device *device)
1568 /* First of all start sense subsystem status request. */
1569 dasd_eer_snss(device);
1571 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1572 dasd_schedule_device_bh(device);
1573 if (device->block) {
1574 dasd_schedule_block_bh(device->block);
1575 if (device->block->request_queue)
1576 blk_mq_run_hw_queues(device->block->request_queue,
1577 true);
1580 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1582 static int dasd_check_hpf_error(struct irb *irb)
1584 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1585 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1586 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1590 * Interrupt handler for "normal" ssch-io based dasd devices.
1592 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1593 struct irb *irb)
1595 struct dasd_ccw_req *cqr, *next;
1596 struct dasd_device *device;
1597 unsigned long now;
1598 int nrf_suppressed = 0;
1599 int fp_suppressed = 0;
1600 u8 *sense = NULL;
1601 int expires;
1603 cqr = (struct dasd_ccw_req *) intparm;
1604 if (IS_ERR(irb)) {
1605 switch (PTR_ERR(irb)) {
1606 case -EIO:
1607 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1608 device = cqr->startdev;
1609 cqr->status = DASD_CQR_CLEARED;
1610 dasd_device_clear_timer(device);
1611 wake_up(&dasd_flush_wq);
1612 dasd_schedule_device_bh(device);
1613 return;
1615 break;
1616 case -ETIMEDOUT:
1617 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1618 "request timed out\n", __func__);
1619 break;
1620 default:
1621 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1622 "unknown error %ld\n", __func__,
1623 PTR_ERR(irb));
1625 dasd_handle_killed_request(cdev, intparm);
1626 return;
1629 now = get_tod_clock();
1630 /* check for conditions that should be handled immediately */
1631 if (!cqr ||
1632 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1633 scsw_cstat(&irb->scsw) == 0)) {
1634 if (cqr)
1635 memcpy(&cqr->irb, irb, sizeof(*irb));
1636 device = dasd_device_from_cdev_locked(cdev);
1637 if (IS_ERR(device))
1638 return;
1639 /* ignore unsolicited interrupts for DIAG discipline */
1640 if (device->discipline == dasd_diag_discipline_pointer) {
1641 dasd_put_device(device);
1642 return;
1646 * In some cases 'File Protected' or 'No Record Found' errors
1647 * might be expected and debug log messages for the
1648 * corresponding interrupts shouldn't be written then.
1649 * Check if either of the according suppress bits is set.
1651 sense = dasd_get_sense(irb);
1652 if (sense) {
1653 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1654 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1655 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1656 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1658 if (!(fp_suppressed || nrf_suppressed))
1659 device->discipline->dump_sense_dbf(device, irb, "int");
1661 if (device->features & DASD_FEATURE_ERPLOG)
1662 device->discipline->dump_sense(device, cqr, irb);
1663 device->discipline->check_for_device_change(device, cqr, irb);
1664 dasd_put_device(device);
1667 /* check for for attention message */
1668 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1669 device = dasd_device_from_cdev_locked(cdev);
1670 if (!IS_ERR(device)) {
1671 device->discipline->check_attention(device,
1672 irb->esw.esw1.lpum);
1673 dasd_put_device(device);
1677 if (!cqr)
1678 return;
1680 device = (struct dasd_device *) cqr->startdev;
1681 if (!device ||
1682 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1683 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1684 "invalid device in request");
1685 return;
1688 /* Check for clear pending */
1689 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1690 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1691 cqr->status = DASD_CQR_CLEARED;
1692 dasd_device_clear_timer(device);
1693 wake_up(&dasd_flush_wq);
1694 dasd_schedule_device_bh(device);
1695 return;
1698 /* check status - the request might have been killed by dyn detach */
1699 if (cqr->status != DASD_CQR_IN_IO) {
1700 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1701 "status %02x", dev_name(&cdev->dev), cqr->status);
1702 return;
1705 next = NULL;
1706 expires = 0;
1707 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1708 scsw_cstat(&irb->scsw) == 0) {
1709 /* request was completed successfully */
1710 cqr->status = DASD_CQR_SUCCESS;
1711 cqr->stopclk = now;
1712 /* Start first request on queue if possible -> fast_io. */
1713 if (cqr->devlist.next != &device->ccw_queue) {
1714 next = list_entry(cqr->devlist.next,
1715 struct dasd_ccw_req, devlist);
1717 } else { /* error */
1718 /* check for HPF error
1719 * call discipline function to requeue all requests
1720 * and disable HPF accordingly
1722 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1723 device->discipline->handle_hpf_error)
1724 device->discipline->handle_hpf_error(device, irb);
1726 * If we don't want complex ERP for this request, then just
1727 * reset this and retry it in the fastpath
1729 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1730 cqr->retries > 0) {
1731 if (cqr->lpm == dasd_path_get_opm(device))
1732 DBF_DEV_EVENT(DBF_DEBUG, device,
1733 "default ERP in fastpath "
1734 "(%i retries left)",
1735 cqr->retries);
1736 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1737 cqr->lpm = dasd_path_get_opm(device);
1738 cqr->status = DASD_CQR_QUEUED;
1739 next = cqr;
1740 } else
1741 cqr->status = DASD_CQR_ERROR;
1743 if (next && (next->status == DASD_CQR_QUEUED) &&
1744 (!device->stopped)) {
1745 if (device->discipline->start_IO(next) == 0)
1746 expires = next->expires;
1748 if (expires != 0)
1749 dasd_device_set_timer(device, expires);
1750 else
1751 dasd_device_clear_timer(device);
1752 dasd_schedule_device_bh(device);
1754 EXPORT_SYMBOL(dasd_int_handler);
1756 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1758 struct dasd_device *device;
1760 device = dasd_device_from_cdev_locked(cdev);
1762 if (IS_ERR(device))
1763 goto out;
1764 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1765 device->state != device->target ||
1766 !device->discipline->check_for_device_change){
1767 dasd_put_device(device);
1768 goto out;
1770 if (device->discipline->dump_sense_dbf)
1771 device->discipline->dump_sense_dbf(device, irb, "uc");
1772 device->discipline->check_for_device_change(device, NULL, irb);
1773 dasd_put_device(device);
1774 out:
1775 return UC_TODO_RETRY;
1777 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1780 * If we have an error on a dasd_block layer request then we cancel
1781 * and return all further requests from the same dasd_block as well.
1783 static void __dasd_device_recovery(struct dasd_device *device,
1784 struct dasd_ccw_req *ref_cqr)
1786 struct list_head *l, *n;
1787 struct dasd_ccw_req *cqr;
1790 * only requeue request that came from the dasd_block layer
1792 if (!ref_cqr->block)
1793 return;
1795 list_for_each_safe(l, n, &device->ccw_queue) {
1796 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1797 if (cqr->status == DASD_CQR_QUEUED &&
1798 ref_cqr->block == cqr->block) {
1799 cqr->status = DASD_CQR_CLEARED;
1805 * Remove those ccw requests from the queue that need to be returned
1806 * to the upper layer.
1808 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1809 struct list_head *final_queue)
1811 struct list_head *l, *n;
1812 struct dasd_ccw_req *cqr;
1814 /* Process request with final status. */
1815 list_for_each_safe(l, n, &device->ccw_queue) {
1816 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1818 /* Skip any non-final request. */
1819 if (cqr->status == DASD_CQR_QUEUED ||
1820 cqr->status == DASD_CQR_IN_IO ||
1821 cqr->status == DASD_CQR_CLEAR_PENDING)
1822 continue;
1823 if (cqr->status == DASD_CQR_ERROR) {
1824 __dasd_device_recovery(device, cqr);
1826 /* Rechain finished requests to final queue */
1827 list_move_tail(&cqr->devlist, final_queue);
1831 static void __dasd_process_cqr(struct dasd_device *device,
1832 struct dasd_ccw_req *cqr)
1834 char errorstring[ERRORLENGTH];
1836 switch (cqr->status) {
1837 case DASD_CQR_SUCCESS:
1838 cqr->status = DASD_CQR_DONE;
1839 break;
1840 case DASD_CQR_ERROR:
1841 cqr->status = DASD_CQR_NEED_ERP;
1842 break;
1843 case DASD_CQR_CLEARED:
1844 cqr->status = DASD_CQR_TERMINATED;
1845 break;
1846 default:
1847 /* internal error 12 - wrong cqr status*/
1848 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1849 dev_err(&device->cdev->dev,
1850 "An error occurred in the DASD device driver, "
1851 "reason=%s\n", errorstring);
1852 BUG();
1854 if (cqr->callback)
1855 cqr->callback(cqr, cqr->callback_data);
1859 * the cqrs from the final queue are returned to the upper layer
1860 * by setting a dasd_block state and calling the callback function
1862 static void __dasd_device_process_final_queue(struct dasd_device *device,
1863 struct list_head *final_queue)
1865 struct list_head *l, *n;
1866 struct dasd_ccw_req *cqr;
1867 struct dasd_block *block;
1869 list_for_each_safe(l, n, final_queue) {
1870 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1871 list_del_init(&cqr->devlist);
1872 block = cqr->block;
1873 if (!block) {
1874 __dasd_process_cqr(device, cqr);
1875 } else {
1876 spin_lock_bh(&block->queue_lock);
1877 __dasd_process_cqr(device, cqr);
1878 spin_unlock_bh(&block->queue_lock);
1884 * Take a look at the first request on the ccw queue and check
1885 * if it reached its expire time. If so, terminate the IO.
1887 static void __dasd_device_check_expire(struct dasd_device *device)
1889 struct dasd_ccw_req *cqr;
1891 if (list_empty(&device->ccw_queue))
1892 return;
1893 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1894 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1895 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1896 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1898 * IO in safe offline processing should not
1899 * run out of retries
1901 cqr->retries++;
1903 if (device->discipline->term_IO(cqr) != 0) {
1904 /* Hmpf, try again in 5 sec */
1905 dev_err(&device->cdev->dev,
1906 "cqr %p timed out (%lus) but cannot be "
1907 "ended, retrying in 5 s\n",
1908 cqr, (cqr->expires/HZ));
1909 cqr->expires += 5*HZ;
1910 dasd_device_set_timer(device, 5*HZ);
1911 } else {
1912 dev_err(&device->cdev->dev,
1913 "cqr %p timed out (%lus), %i retries "
1914 "remaining\n", cqr, (cqr->expires/HZ),
1915 cqr->retries);
1921 * return 1 when device is not eligible for IO
1923 static int __dasd_device_is_unusable(struct dasd_device *device,
1924 struct dasd_ccw_req *cqr)
1926 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1928 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
1929 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1931 * dasd is being set offline
1932 * but it is no safe offline where we have to allow I/O
1934 return 1;
1936 if (device->stopped) {
1937 if (device->stopped & mask) {
1938 /* stopped and CQR will not change that. */
1939 return 1;
1941 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1942 /* CQR is not able to change device to
1943 * operational. */
1944 return 1;
1946 /* CQR required to get device operational. */
1948 return 0;
1952 * Take a look at the first request on the ccw queue and check
1953 * if it needs to be started.
1955 static void __dasd_device_start_head(struct dasd_device *device)
1957 struct dasd_ccw_req *cqr;
1958 int rc;
1960 if (list_empty(&device->ccw_queue))
1961 return;
1962 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1963 if (cqr->status != DASD_CQR_QUEUED)
1964 return;
1965 /* if device is not usable return request to upper layer */
1966 if (__dasd_device_is_unusable(device, cqr)) {
1967 cqr->intrc = -EAGAIN;
1968 cqr->status = DASD_CQR_CLEARED;
1969 dasd_schedule_device_bh(device);
1970 return;
1973 rc = device->discipline->start_IO(cqr);
1974 if (rc == 0)
1975 dasd_device_set_timer(device, cqr->expires);
1976 else if (rc == -EACCES) {
1977 dasd_schedule_device_bh(device);
1978 } else
1979 /* Hmpf, try again in 1/2 sec */
1980 dasd_device_set_timer(device, 50);
1983 static void __dasd_device_check_path_events(struct dasd_device *device)
1985 int rc;
1987 if (!dasd_path_get_tbvpm(device))
1988 return;
1990 if (device->stopped &
1991 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1992 return;
1993 rc = device->discipline->verify_path(device,
1994 dasd_path_get_tbvpm(device));
1995 if (rc)
1996 dasd_device_set_timer(device, 50);
1997 else
1998 dasd_path_clear_all_verify(device);
2002 * Go through all request on the dasd_device request queue,
2003 * terminate them on the cdev if necessary, and return them to the
2004 * submitting layer via callback.
2005 * Note:
2006 * Make sure that all 'submitting layers' still exist when
2007 * this function is called!. In other words, when 'device' is a base
2008 * device then all block layer requests must have been removed before
2009 * via dasd_flush_block_queue.
2011 int dasd_flush_device_queue(struct dasd_device *device)
2013 struct dasd_ccw_req *cqr, *n;
2014 int rc;
2015 struct list_head flush_queue;
2017 INIT_LIST_HEAD(&flush_queue);
2018 spin_lock_irq(get_ccwdev_lock(device->cdev));
2019 rc = 0;
2020 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2021 /* Check status and move request to flush_queue */
2022 switch (cqr->status) {
2023 case DASD_CQR_IN_IO:
2024 rc = device->discipline->term_IO(cqr);
2025 if (rc) {
2026 /* unable to terminate requeust */
2027 dev_err(&device->cdev->dev,
2028 "Flushing the DASD request queue "
2029 "failed for request %p\n", cqr);
2030 /* stop flush processing */
2031 goto finished;
2033 break;
2034 case DASD_CQR_QUEUED:
2035 cqr->stopclk = get_tod_clock();
2036 cqr->status = DASD_CQR_CLEARED;
2037 break;
2038 default: /* no need to modify the others */
2039 break;
2041 list_move_tail(&cqr->devlist, &flush_queue);
2043 finished:
2044 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2046 * After this point all requests must be in state CLEAR_PENDING,
2047 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2048 * one of the others.
2050 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2051 wait_event(dasd_flush_wq,
2052 (cqr->status != DASD_CQR_CLEAR_PENDING));
2054 * Now set each request back to TERMINATED, DONE or NEED_ERP
2055 * and call the callback function of flushed requests
2057 __dasd_device_process_final_queue(device, &flush_queue);
2058 return rc;
2060 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2063 * Acquire the device lock and process queues for the device.
2065 static void dasd_device_tasklet(unsigned long data)
2067 struct dasd_device *device = (struct dasd_device *) data;
2068 struct list_head final_queue;
2070 atomic_set (&device->tasklet_scheduled, 0);
2071 INIT_LIST_HEAD(&final_queue);
2072 spin_lock_irq(get_ccwdev_lock(device->cdev));
2073 /* Check expire time of first request on the ccw queue. */
2074 __dasd_device_check_expire(device);
2075 /* find final requests on ccw queue */
2076 __dasd_device_process_ccw_queue(device, &final_queue);
2077 __dasd_device_check_path_events(device);
2078 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2079 /* Now call the callback function of requests with final status */
2080 __dasd_device_process_final_queue(device, &final_queue);
2081 spin_lock_irq(get_ccwdev_lock(device->cdev));
2082 /* Now check if the head of the ccw queue needs to be started. */
2083 __dasd_device_start_head(device);
2084 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2085 if (waitqueue_active(&shutdown_waitq))
2086 wake_up(&shutdown_waitq);
2087 dasd_put_device(device);
2091 * Schedules a call to dasd_tasklet over the device tasklet.
2093 void dasd_schedule_device_bh(struct dasd_device *device)
2095 /* Protect against rescheduling. */
2096 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2097 return;
2098 dasd_get_device(device);
2099 tasklet_hi_schedule(&device->tasklet);
2101 EXPORT_SYMBOL(dasd_schedule_device_bh);
2103 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2105 device->stopped |= bits;
2107 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2109 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2111 device->stopped &= ~bits;
2112 if (!device->stopped)
2113 wake_up(&generic_waitq);
2115 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2118 * Queue a request to the head of the device ccw_queue.
2119 * Start the I/O if possible.
2121 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2123 struct dasd_device *device;
2124 unsigned long flags;
2126 device = cqr->startdev;
2127 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2128 cqr->status = DASD_CQR_QUEUED;
2129 list_add(&cqr->devlist, &device->ccw_queue);
2130 /* let the bh start the request to keep them in order */
2131 dasd_schedule_device_bh(device);
2132 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2134 EXPORT_SYMBOL(dasd_add_request_head);
2137 * Queue a request to the tail of the device ccw_queue.
2138 * Start the I/O if possible.
2140 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2142 struct dasd_device *device;
2143 unsigned long flags;
2145 device = cqr->startdev;
2146 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2147 cqr->status = DASD_CQR_QUEUED;
2148 list_add_tail(&cqr->devlist, &device->ccw_queue);
2149 /* let the bh start the request to keep them in order */
2150 dasd_schedule_device_bh(device);
2151 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2153 EXPORT_SYMBOL(dasd_add_request_tail);
2156 * Wakeup helper for the 'sleep_on' functions.
2158 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2160 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2161 cqr->callback_data = DASD_SLEEPON_END_TAG;
2162 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2163 wake_up(&generic_waitq);
2165 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2167 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2169 struct dasd_device *device;
2170 int rc;
2172 device = cqr->startdev;
2173 spin_lock_irq(get_ccwdev_lock(device->cdev));
2174 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2175 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2176 return rc;
2180 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2182 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2184 struct dasd_device *device;
2185 dasd_erp_fn_t erp_fn;
2187 if (cqr->status == DASD_CQR_FILLED)
2188 return 0;
2189 device = cqr->startdev;
2190 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2191 if (cqr->status == DASD_CQR_TERMINATED) {
2192 device->discipline->handle_terminated_request(cqr);
2193 return 1;
2195 if (cqr->status == DASD_CQR_NEED_ERP) {
2196 erp_fn = device->discipline->erp_action(cqr);
2197 erp_fn(cqr);
2198 return 1;
2200 if (cqr->status == DASD_CQR_FAILED)
2201 dasd_log_sense(cqr, &cqr->irb);
2202 if (cqr->refers) {
2203 __dasd_process_erp(device, cqr);
2204 return 1;
2207 return 0;
2210 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2212 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2213 if (cqr->refers) /* erp is not done yet */
2214 return 1;
2215 return ((cqr->status != DASD_CQR_DONE) &&
2216 (cqr->status != DASD_CQR_FAILED));
2217 } else
2218 return (cqr->status == DASD_CQR_FILLED);
2221 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2223 struct dasd_device *device;
2224 int rc;
2225 struct list_head ccw_queue;
2226 struct dasd_ccw_req *cqr;
2228 INIT_LIST_HEAD(&ccw_queue);
2229 maincqr->status = DASD_CQR_FILLED;
2230 device = maincqr->startdev;
2231 list_add(&maincqr->blocklist, &ccw_queue);
2232 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2233 cqr = list_first_entry(&ccw_queue,
2234 struct dasd_ccw_req, blocklist)) {
2236 if (__dasd_sleep_on_erp(cqr))
2237 continue;
2238 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2239 continue;
2240 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2241 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2242 cqr->status = DASD_CQR_FAILED;
2243 cqr->intrc = -EPERM;
2244 continue;
2246 /* Non-temporary stop condition will trigger fail fast */
2247 if (device->stopped & ~DASD_STOPPED_PENDING &&
2248 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2249 (!dasd_eer_enabled(device))) {
2250 cqr->status = DASD_CQR_FAILED;
2251 cqr->intrc = -ENOLINK;
2252 continue;
2255 * Don't try to start requests if device is in
2256 * offline processing, it might wait forever
2258 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2259 cqr->status = DASD_CQR_FAILED;
2260 cqr->intrc = -ENODEV;
2261 continue;
2264 * Don't try to start requests if device is stopped
2265 * except path verification requests
2267 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2268 if (interruptible) {
2269 rc = wait_event_interruptible(
2270 generic_waitq, !(device->stopped));
2271 if (rc == -ERESTARTSYS) {
2272 cqr->status = DASD_CQR_FAILED;
2273 maincqr->intrc = rc;
2274 continue;
2276 } else
2277 wait_event(generic_waitq, !(device->stopped));
2279 if (!cqr->callback)
2280 cqr->callback = dasd_wakeup_cb;
2282 cqr->callback_data = DASD_SLEEPON_START_TAG;
2283 dasd_add_request_tail(cqr);
2284 if (interruptible) {
2285 rc = wait_event_interruptible(
2286 generic_waitq, _wait_for_wakeup(cqr));
2287 if (rc == -ERESTARTSYS) {
2288 dasd_cancel_req(cqr);
2289 /* wait (non-interruptible) for final status */
2290 wait_event(generic_waitq,
2291 _wait_for_wakeup(cqr));
2292 cqr->status = DASD_CQR_FAILED;
2293 maincqr->intrc = rc;
2294 continue;
2296 } else
2297 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2300 maincqr->endclk = get_tod_clock();
2301 if ((maincqr->status != DASD_CQR_DONE) &&
2302 (maincqr->intrc != -ERESTARTSYS))
2303 dasd_log_sense(maincqr, &maincqr->irb);
2304 if (maincqr->status == DASD_CQR_DONE)
2305 rc = 0;
2306 else if (maincqr->intrc)
2307 rc = maincqr->intrc;
2308 else
2309 rc = -EIO;
2310 return rc;
2313 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2315 struct dasd_ccw_req *cqr;
2317 list_for_each_entry(cqr, ccw_queue, blocklist) {
2318 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2319 return 0;
2322 return 1;
2325 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2327 struct dasd_device *device;
2328 struct dasd_ccw_req *cqr, *n;
2329 u8 *sense = NULL;
2330 int rc;
2332 retry:
2333 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2334 device = cqr->startdev;
2335 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2336 continue;
2338 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2339 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2340 cqr->status = DASD_CQR_FAILED;
2341 cqr->intrc = -EPERM;
2342 continue;
2344 /*Non-temporary stop condition will trigger fail fast*/
2345 if (device->stopped & ~DASD_STOPPED_PENDING &&
2346 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2347 !dasd_eer_enabled(device)) {
2348 cqr->status = DASD_CQR_FAILED;
2349 cqr->intrc = -EAGAIN;
2350 continue;
2353 /*Don't try to start requests if device is stopped*/
2354 if (interruptible) {
2355 rc = wait_event_interruptible(
2356 generic_waitq, !device->stopped);
2357 if (rc == -ERESTARTSYS) {
2358 cqr->status = DASD_CQR_FAILED;
2359 cqr->intrc = rc;
2360 continue;
2362 } else
2363 wait_event(generic_waitq, !(device->stopped));
2365 if (!cqr->callback)
2366 cqr->callback = dasd_wakeup_cb;
2367 cqr->callback_data = DASD_SLEEPON_START_TAG;
2368 dasd_add_request_tail(cqr);
2371 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2373 rc = 0;
2374 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2376 * In some cases the 'File Protected' or 'Incorrect Length'
2377 * error might be expected and error recovery would be
2378 * unnecessary in these cases. Check if the according suppress
2379 * bit is set.
2381 sense = dasd_get_sense(&cqr->irb);
2382 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2383 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2384 continue;
2385 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2386 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2387 continue;
2390 * for alias devices simplify error recovery and
2391 * return to upper layer
2392 * do not skip ERP requests
2394 if (cqr->startdev != cqr->basedev && !cqr->refers &&
2395 (cqr->status == DASD_CQR_TERMINATED ||
2396 cqr->status == DASD_CQR_NEED_ERP))
2397 return -EAGAIN;
2399 /* normal recovery for basedev IO */
2400 if (__dasd_sleep_on_erp(cqr))
2401 /* handle erp first */
2402 goto retry;
2405 return 0;
2409 * Queue a request to the tail of the device ccw_queue and wait for
2410 * it's completion.
2412 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2414 return _dasd_sleep_on(cqr, 0);
2416 EXPORT_SYMBOL(dasd_sleep_on);
2419 * Start requests from a ccw_queue and wait for their completion.
2421 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2423 return _dasd_sleep_on_queue(ccw_queue, 0);
2425 EXPORT_SYMBOL(dasd_sleep_on_queue);
2428 * Queue a request to the tail of the device ccw_queue and wait
2429 * interruptible for it's completion.
2431 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2433 return _dasd_sleep_on(cqr, 1);
2435 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2438 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2439 * for eckd devices) the currently running request has to be terminated
2440 * and be put back to status queued, before the special request is added
2441 * to the head of the queue. Then the special request is waited on normally.
2443 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2445 struct dasd_ccw_req *cqr;
2446 int rc;
2448 if (list_empty(&device->ccw_queue))
2449 return 0;
2450 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2451 rc = device->discipline->term_IO(cqr);
2452 if (!rc)
2454 * CQR terminated because a more important request is pending.
2455 * Undo decreasing of retry counter because this is
2456 * not an error case.
2458 cqr->retries++;
2459 return rc;
2462 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2464 struct dasd_device *device;
2465 int rc;
2467 device = cqr->startdev;
2468 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2469 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2470 cqr->status = DASD_CQR_FAILED;
2471 cqr->intrc = -EPERM;
2472 return -EIO;
2474 spin_lock_irq(get_ccwdev_lock(device->cdev));
2475 rc = _dasd_term_running_cqr(device);
2476 if (rc) {
2477 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2478 return rc;
2480 cqr->callback = dasd_wakeup_cb;
2481 cqr->callback_data = DASD_SLEEPON_START_TAG;
2482 cqr->status = DASD_CQR_QUEUED;
2484 * add new request as second
2485 * first the terminated cqr needs to be finished
2487 list_add(&cqr->devlist, device->ccw_queue.next);
2489 /* let the bh start the request to keep them in order */
2490 dasd_schedule_device_bh(device);
2492 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2494 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2496 if (cqr->status == DASD_CQR_DONE)
2497 rc = 0;
2498 else if (cqr->intrc)
2499 rc = cqr->intrc;
2500 else
2501 rc = -EIO;
2503 /* kick tasklets */
2504 dasd_schedule_device_bh(device);
2505 if (device->block)
2506 dasd_schedule_block_bh(device->block);
2508 return rc;
2510 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2513 * Cancels a request that was started with dasd_sleep_on_req.
2514 * This is useful to timeout requests. The request will be
2515 * terminated if it is currently in i/o.
2516 * Returns 0 if request termination was successful
2517 * negative error code if termination failed
2518 * Cancellation of a request is an asynchronous operation! The calling
2519 * function has to wait until the request is properly returned via callback.
2521 static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
2523 struct dasd_device *device = cqr->startdev;
2524 int rc = 0;
2526 switch (cqr->status) {
2527 case DASD_CQR_QUEUED:
2528 /* request was not started - just set to cleared */
2529 cqr->status = DASD_CQR_CLEARED;
2530 break;
2531 case DASD_CQR_IN_IO:
2532 /* request in IO - terminate IO and release again */
2533 rc = device->discipline->term_IO(cqr);
2534 if (rc) {
2535 dev_err(&device->cdev->dev,
2536 "Cancelling request %p failed with rc=%d\n",
2537 cqr, rc);
2538 } else {
2539 cqr->stopclk = get_tod_clock();
2541 break;
2542 default: /* already finished or clear pending - do nothing */
2543 break;
2545 dasd_schedule_device_bh(device);
2546 return rc;
2549 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2551 struct dasd_device *device = cqr->startdev;
2552 unsigned long flags;
2553 int rc;
2555 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2556 rc = __dasd_cancel_req(cqr);
2557 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2558 return rc;
2562 * SECTION: Operations of the dasd_block layer.
2566 * Timeout function for dasd_block. This is used when the block layer
2567 * is waiting for something that may not come reliably, (e.g. a state
2568 * change interrupt)
2570 static void dasd_block_timeout(struct timer_list *t)
2572 unsigned long flags;
2573 struct dasd_block *block;
2575 block = from_timer(block, t, timer);
2576 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2577 /* re-activate request queue */
2578 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2579 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2580 dasd_schedule_block_bh(block);
2581 blk_mq_run_hw_queues(block->request_queue, true);
2585 * Setup timeout for a dasd_block in jiffies.
2587 void dasd_block_set_timer(struct dasd_block *block, int expires)
2589 if (expires == 0)
2590 del_timer(&block->timer);
2591 else
2592 mod_timer(&block->timer, jiffies + expires);
2594 EXPORT_SYMBOL(dasd_block_set_timer);
2597 * Clear timeout for a dasd_block.
2599 void dasd_block_clear_timer(struct dasd_block *block)
2601 del_timer(&block->timer);
2603 EXPORT_SYMBOL(dasd_block_clear_timer);
2606 * Process finished error recovery ccw.
2608 static void __dasd_process_erp(struct dasd_device *device,
2609 struct dasd_ccw_req *cqr)
2611 dasd_erp_fn_t erp_fn;
2613 if (cqr->status == DASD_CQR_DONE)
2614 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2615 else
2616 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2617 erp_fn = device->discipline->erp_postaction(cqr);
2618 erp_fn(cqr);
2621 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2623 struct request *req;
2624 blk_status_t error = BLK_STS_OK;
2625 int status;
2627 req = (struct request *) cqr->callback_data;
2628 dasd_profile_end(cqr->block, cqr, req);
2630 status = cqr->block->base->discipline->free_cp(cqr, req);
2631 if (status < 0)
2632 error = errno_to_blk_status(status);
2633 else if (status == 0) {
2634 switch (cqr->intrc) {
2635 case -EPERM:
2636 error = BLK_STS_NEXUS;
2637 break;
2638 case -ENOLINK:
2639 error = BLK_STS_TRANSPORT;
2640 break;
2641 case -ETIMEDOUT:
2642 error = BLK_STS_TIMEOUT;
2643 break;
2644 default:
2645 error = BLK_STS_IOERR;
2646 break;
2651 * We need to take care for ETIMEDOUT errors here since the
2652 * complete callback does not get called in this case.
2653 * Take care of all errors here and avoid additional code to
2654 * transfer the error value to the complete callback.
2656 if (error) {
2657 blk_mq_end_request(req, error);
2658 blk_mq_run_hw_queues(req->q, true);
2659 } else {
2660 blk_mq_complete_request(req);
2665 * Process ccw request queue.
2667 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2668 struct list_head *final_queue)
2670 struct list_head *l, *n;
2671 struct dasd_ccw_req *cqr;
2672 dasd_erp_fn_t erp_fn;
2673 unsigned long flags;
2674 struct dasd_device *base = block->base;
2676 restart:
2677 /* Process request with final status. */
2678 list_for_each_safe(l, n, &block->ccw_queue) {
2679 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2680 if (cqr->status != DASD_CQR_DONE &&
2681 cqr->status != DASD_CQR_FAILED &&
2682 cqr->status != DASD_CQR_NEED_ERP &&
2683 cqr->status != DASD_CQR_TERMINATED)
2684 continue;
2686 if (cqr->status == DASD_CQR_TERMINATED) {
2687 base->discipline->handle_terminated_request(cqr);
2688 goto restart;
2691 /* Process requests that may be recovered */
2692 if (cqr->status == DASD_CQR_NEED_ERP) {
2693 erp_fn = base->discipline->erp_action(cqr);
2694 if (IS_ERR(erp_fn(cqr)))
2695 continue;
2696 goto restart;
2699 /* log sense for fatal error */
2700 if (cqr->status == DASD_CQR_FAILED) {
2701 dasd_log_sense(cqr, &cqr->irb);
2704 /* First of all call extended error reporting. */
2705 if (dasd_eer_enabled(base) &&
2706 cqr->status == DASD_CQR_FAILED) {
2707 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2709 /* restart request */
2710 cqr->status = DASD_CQR_FILLED;
2711 cqr->retries = 255;
2712 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2713 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2714 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2715 flags);
2716 goto restart;
2719 /* Process finished ERP request. */
2720 if (cqr->refers) {
2721 __dasd_process_erp(base, cqr);
2722 goto restart;
2725 /* Rechain finished requests to final queue */
2726 cqr->endclk = get_tod_clock();
2727 list_move_tail(&cqr->blocklist, final_queue);
2731 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2733 dasd_schedule_block_bh(cqr->block);
2736 static void __dasd_block_start_head(struct dasd_block *block)
2738 struct dasd_ccw_req *cqr;
2740 if (list_empty(&block->ccw_queue))
2741 return;
2742 /* We allways begin with the first requests on the queue, as some
2743 * of previously started requests have to be enqueued on a
2744 * dasd_device again for error recovery.
2746 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2747 if (cqr->status != DASD_CQR_FILLED)
2748 continue;
2749 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2750 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2751 cqr->status = DASD_CQR_FAILED;
2752 cqr->intrc = -EPERM;
2753 dasd_schedule_block_bh(block);
2754 continue;
2756 /* Non-temporary stop condition will trigger fail fast */
2757 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2758 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2759 (!dasd_eer_enabled(block->base))) {
2760 cqr->status = DASD_CQR_FAILED;
2761 cqr->intrc = -ENOLINK;
2762 dasd_schedule_block_bh(block);
2763 continue;
2765 /* Don't try to start requests if device is stopped */
2766 if (block->base->stopped)
2767 return;
2769 /* just a fail safe check, should not happen */
2770 if (!cqr->startdev)
2771 cqr->startdev = block->base;
2773 /* make sure that the requests we submit find their way back */
2774 cqr->callback = dasd_return_cqr_cb;
2776 dasd_add_request_tail(cqr);
2781 * Central dasd_block layer routine. Takes requests from the generic
2782 * block layer request queue, creates ccw requests, enqueues them on
2783 * a dasd_device and processes ccw requests that have been returned.
2785 static void dasd_block_tasklet(unsigned long data)
2787 struct dasd_block *block = (struct dasd_block *) data;
2788 struct list_head final_queue;
2789 struct list_head *l, *n;
2790 struct dasd_ccw_req *cqr;
2791 struct dasd_queue *dq;
2793 atomic_set(&block->tasklet_scheduled, 0);
2794 INIT_LIST_HEAD(&final_queue);
2795 spin_lock_irq(&block->queue_lock);
2796 /* Finish off requests on ccw queue */
2797 __dasd_process_block_ccw_queue(block, &final_queue);
2798 spin_unlock_irq(&block->queue_lock);
2800 /* Now call the callback function of requests with final status */
2801 list_for_each_safe(l, n, &final_queue) {
2802 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2803 dq = cqr->dq;
2804 spin_lock_irq(&dq->lock);
2805 list_del_init(&cqr->blocklist);
2806 __dasd_cleanup_cqr(cqr);
2807 spin_unlock_irq(&dq->lock);
2810 spin_lock_irq(&block->queue_lock);
2811 /* Now check if the head of the ccw queue needs to be started. */
2812 __dasd_block_start_head(block);
2813 spin_unlock_irq(&block->queue_lock);
2815 if (waitqueue_active(&shutdown_waitq))
2816 wake_up(&shutdown_waitq);
2817 dasd_put_device(block->base);
2820 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2822 wake_up(&dasd_flush_wq);
2826 * Requeue a request back to the block request queue
2827 * only works for block requests
2829 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2831 struct dasd_block *block = cqr->block;
2832 struct request *req;
2834 if (!block)
2835 return -EINVAL;
2836 spin_lock_irq(&cqr->dq->lock);
2837 req = (struct request *) cqr->callback_data;
2838 blk_mq_requeue_request(req, false);
2839 spin_unlock_irq(&cqr->dq->lock);
2841 return 0;
2845 * Go through all request on the dasd_block request queue, cancel them
2846 * on the respective dasd_device, and return them to the generic
2847 * block layer.
2849 static int dasd_flush_block_queue(struct dasd_block *block)
2851 struct dasd_ccw_req *cqr, *n;
2852 int rc, i;
2853 struct list_head flush_queue;
2854 unsigned long flags;
2856 INIT_LIST_HEAD(&flush_queue);
2857 spin_lock_bh(&block->queue_lock);
2858 rc = 0;
2859 restart:
2860 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2861 /* if this request currently owned by a dasd_device cancel it */
2862 if (cqr->status >= DASD_CQR_QUEUED)
2863 rc = dasd_cancel_req(cqr);
2864 if (rc < 0)
2865 break;
2866 /* Rechain request (including erp chain) so it won't be
2867 * touched by the dasd_block_tasklet anymore.
2868 * Replace the callback so we notice when the request
2869 * is returned from the dasd_device layer.
2871 cqr->callback = _dasd_wake_block_flush_cb;
2872 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2873 list_move_tail(&cqr->blocklist, &flush_queue);
2874 if (i > 1)
2875 /* moved more than one request - need to restart */
2876 goto restart;
2878 spin_unlock_bh(&block->queue_lock);
2879 /* Now call the callback function of flushed requests */
2880 restart_cb:
2881 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2882 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2883 /* Process finished ERP request. */
2884 if (cqr->refers) {
2885 spin_lock_bh(&block->queue_lock);
2886 __dasd_process_erp(block->base, cqr);
2887 spin_unlock_bh(&block->queue_lock);
2888 /* restart list_for_xx loop since dasd_process_erp
2889 * might remove multiple elements */
2890 goto restart_cb;
2892 /* call the callback function */
2893 spin_lock_irqsave(&cqr->dq->lock, flags);
2894 cqr->endclk = get_tod_clock();
2895 list_del_init(&cqr->blocklist);
2896 __dasd_cleanup_cqr(cqr);
2897 spin_unlock_irqrestore(&cqr->dq->lock, flags);
2899 return rc;
2903 * Schedules a call to dasd_tasklet over the device tasklet.
2905 void dasd_schedule_block_bh(struct dasd_block *block)
2907 /* Protect against rescheduling. */
2908 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2909 return;
2910 /* life cycle of block is bound to it's base device */
2911 dasd_get_device(block->base);
2912 tasklet_hi_schedule(&block->tasklet);
2914 EXPORT_SYMBOL(dasd_schedule_block_bh);
2918 * SECTION: external block device operations
2919 * (request queue handling, open, release, etc.)
2923 * Dasd request queue function. Called from ll_rw_blk.c
2925 static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
2926 const struct blk_mq_queue_data *qd)
2928 struct dasd_block *block = hctx->queue->queuedata;
2929 struct dasd_queue *dq = hctx->driver_data;
2930 struct request *req = qd->rq;
2931 struct dasd_device *basedev;
2932 struct dasd_ccw_req *cqr;
2933 blk_status_t rc = BLK_STS_OK;
2935 basedev = block->base;
2936 spin_lock_irq(&dq->lock);
2937 if (basedev->state < DASD_STATE_READY) {
2938 DBF_DEV_EVENT(DBF_ERR, basedev,
2939 "device not ready for request %p", req);
2940 rc = BLK_STS_IOERR;
2941 goto out;
2945 * if device is stopped do not fetch new requests
2946 * except failfast is active which will let requests fail
2947 * immediately in __dasd_block_start_head()
2949 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
2950 DBF_DEV_EVENT(DBF_ERR, basedev,
2951 "device stopped request %p", req);
2952 rc = BLK_STS_RESOURCE;
2953 goto out;
2956 if (basedev->features & DASD_FEATURE_READONLY &&
2957 rq_data_dir(req) == WRITE) {
2958 DBF_DEV_EVENT(DBF_ERR, basedev,
2959 "Rejecting write request %p", req);
2960 rc = BLK_STS_IOERR;
2961 goto out;
2964 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2965 (basedev->features & DASD_FEATURE_FAILFAST ||
2966 blk_noretry_request(req))) {
2967 DBF_DEV_EVENT(DBF_ERR, basedev,
2968 "Rejecting failfast request %p", req);
2969 rc = BLK_STS_IOERR;
2970 goto out;
2973 cqr = basedev->discipline->build_cp(basedev, block, req);
2974 if (IS_ERR(cqr)) {
2975 if (PTR_ERR(cqr) == -EBUSY ||
2976 PTR_ERR(cqr) == -ENOMEM ||
2977 PTR_ERR(cqr) == -EAGAIN) {
2978 rc = BLK_STS_RESOURCE;
2979 goto out;
2981 DBF_DEV_EVENT(DBF_ERR, basedev,
2982 "CCW creation failed (rc=%ld) on request %p",
2983 PTR_ERR(cqr), req);
2984 rc = BLK_STS_IOERR;
2985 goto out;
2988 * Note: callback is set to dasd_return_cqr_cb in
2989 * __dasd_block_start_head to cover erp requests as well
2991 cqr->callback_data = req;
2992 cqr->status = DASD_CQR_FILLED;
2993 cqr->dq = dq;
2995 blk_mq_start_request(req);
2996 spin_lock(&block->queue_lock);
2997 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2998 INIT_LIST_HEAD(&cqr->devlist);
2999 dasd_profile_start(block, cqr, req);
3000 dasd_schedule_block_bh(block);
3001 spin_unlock(&block->queue_lock);
3003 out:
3004 spin_unlock_irq(&dq->lock);
3005 return rc;
3009 * Block timeout callback, called from the block layer
3011 * Return values:
3012 * BLK_EH_RESET_TIMER if the request should be left running
3013 * BLK_EH_DONE if the request is handled or terminated
3014 * by the driver.
3016 enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
3018 struct dasd_block *block = req->q->queuedata;
3019 struct dasd_device *device;
3020 struct dasd_ccw_req *cqr;
3021 unsigned long flags;
3022 int rc = 0;
3024 cqr = blk_mq_rq_to_pdu(req);
3025 if (!cqr)
3026 return BLK_EH_DONE;
3028 spin_lock_irqsave(&cqr->dq->lock, flags);
3029 device = cqr->startdev ? cqr->startdev : block->base;
3030 if (!device->blk_timeout) {
3031 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3032 return BLK_EH_RESET_TIMER;
3034 DBF_DEV_EVENT(DBF_WARNING, device,
3035 " dasd_times_out cqr %p status %x",
3036 cqr, cqr->status);
3038 spin_lock(&block->queue_lock);
3039 spin_lock(get_ccwdev_lock(device->cdev));
3040 cqr->retries = -1;
3041 cqr->intrc = -ETIMEDOUT;
3042 if (cqr->status >= DASD_CQR_QUEUED) {
3043 rc = __dasd_cancel_req(cqr);
3044 } else if (cqr->status == DASD_CQR_FILLED ||
3045 cqr->status == DASD_CQR_NEED_ERP) {
3046 cqr->status = DASD_CQR_TERMINATED;
3047 } else if (cqr->status == DASD_CQR_IN_ERP) {
3048 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3050 list_for_each_entry_safe(searchcqr, nextcqr,
3051 &block->ccw_queue, blocklist) {
3052 tmpcqr = searchcqr;
3053 while (tmpcqr->refers)
3054 tmpcqr = tmpcqr->refers;
3055 if (tmpcqr != cqr)
3056 continue;
3057 /* searchcqr is an ERP request for cqr */
3058 searchcqr->retries = -1;
3059 searchcqr->intrc = -ETIMEDOUT;
3060 if (searchcqr->status >= DASD_CQR_QUEUED) {
3061 rc = __dasd_cancel_req(searchcqr);
3062 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3063 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3064 searchcqr->status = DASD_CQR_TERMINATED;
3065 rc = 0;
3066 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3068 * Shouldn't happen; most recent ERP
3069 * request is at the front of queue
3071 continue;
3073 break;
3076 spin_unlock(get_ccwdev_lock(device->cdev));
3077 dasd_schedule_block_bh(block);
3078 spin_unlock(&block->queue_lock);
3079 spin_unlock_irqrestore(&cqr->dq->lock, flags);
3081 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
3084 static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3085 unsigned int idx)
3087 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3089 if (!dq)
3090 return -ENOMEM;
3092 spin_lock_init(&dq->lock);
3093 hctx->driver_data = dq;
3095 return 0;
3098 static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3100 kfree(hctx->driver_data);
3101 hctx->driver_data = NULL;
3104 static void dasd_request_done(struct request *req)
3106 blk_mq_end_request(req, 0);
3107 blk_mq_run_hw_queues(req->q, true);
3110 static struct blk_mq_ops dasd_mq_ops = {
3111 .queue_rq = do_dasd_request,
3112 .complete = dasd_request_done,
3113 .timeout = dasd_times_out,
3114 .init_hctx = dasd_init_hctx,
3115 .exit_hctx = dasd_exit_hctx,
3119 * Allocate and initialize request queue and default I/O scheduler.
3121 static int dasd_alloc_queue(struct dasd_block *block)
3123 int rc;
3125 block->tag_set.ops = &dasd_mq_ops;
3126 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
3127 block->tag_set.nr_hw_queues = nr_hw_queues;
3128 block->tag_set.queue_depth = queue_depth;
3129 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
3130 block->tag_set.numa_node = NUMA_NO_NODE;
3132 rc = blk_mq_alloc_tag_set(&block->tag_set);
3133 if (rc)
3134 return rc;
3136 block->request_queue = blk_mq_init_queue(&block->tag_set);
3137 if (IS_ERR(block->request_queue))
3138 return PTR_ERR(block->request_queue);
3140 block->request_queue->queuedata = block;
3142 return 0;
3146 * Allocate and initialize request queue.
3148 static void dasd_setup_queue(struct dasd_block *block)
3150 unsigned int logical_block_size = block->bp_block;
3151 struct request_queue *q = block->request_queue;
3152 unsigned int max_bytes, max_discard_sectors;
3153 int max;
3155 if (block->base->features & DASD_FEATURE_USERAW) {
3157 * the max_blocks value for raw_track access is 256
3158 * it is higher than the native ECKD value because we
3159 * only need one ccw per track
3160 * so the max_hw_sectors are
3161 * 2048 x 512B = 1024kB = 16 tracks
3163 max = 2048;
3164 } else {
3165 max = block->base->discipline->max_blocks << block->s2b_shift;
3167 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
3168 q->limits.max_dev_sectors = max;
3169 blk_queue_logical_block_size(q, logical_block_size);
3170 blk_queue_max_hw_sectors(q, max);
3171 blk_queue_max_segments(q, USHRT_MAX);
3172 /* with page sized segments we can translate each segement into
3173 * one idaw/tidaw
3175 blk_queue_max_segment_size(q, PAGE_SIZE);
3176 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
3178 /* Only activate blocklayer discard support for devices that support it */
3179 if (block->base->features & DASD_FEATURE_DISCARD) {
3180 q->limits.discard_granularity = logical_block_size;
3181 q->limits.discard_alignment = PAGE_SIZE;
3183 /* Calculate max_discard_sectors and make it PAGE aligned */
3184 max_bytes = USHRT_MAX * logical_block_size;
3185 max_bytes = ALIGN(max_bytes, PAGE_SIZE) - PAGE_SIZE;
3186 max_discard_sectors = max_bytes / logical_block_size;
3188 blk_queue_max_discard_sectors(q, max_discard_sectors);
3189 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
3190 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
3195 * Deactivate and free request queue.
3197 static void dasd_free_queue(struct dasd_block *block)
3199 if (block->request_queue) {
3200 blk_cleanup_queue(block->request_queue);
3201 blk_mq_free_tag_set(&block->tag_set);
3202 block->request_queue = NULL;
3206 static int dasd_open(struct block_device *bdev, fmode_t mode)
3208 struct dasd_device *base;
3209 int rc;
3211 base = dasd_device_from_gendisk(bdev->bd_disk);
3212 if (!base)
3213 return -ENODEV;
3215 atomic_inc(&base->block->open_count);
3216 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3217 rc = -ENODEV;
3218 goto unlock;
3221 if (!try_module_get(base->discipline->owner)) {
3222 rc = -EINVAL;
3223 goto unlock;
3226 if (dasd_probeonly) {
3227 dev_info(&base->cdev->dev,
3228 "Accessing the DASD failed because it is in "
3229 "probeonly mode\n");
3230 rc = -EPERM;
3231 goto out;
3234 if (base->state <= DASD_STATE_BASIC) {
3235 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3236 " Cannot open unrecognized device");
3237 rc = -ENODEV;
3238 goto out;
3241 if ((mode & FMODE_WRITE) &&
3242 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3243 (base->features & DASD_FEATURE_READONLY))) {
3244 rc = -EROFS;
3245 goto out;
3248 dasd_put_device(base);
3249 return 0;
3251 out:
3252 module_put(base->discipline->owner);
3253 unlock:
3254 atomic_dec(&base->block->open_count);
3255 dasd_put_device(base);
3256 return rc;
3259 static void dasd_release(struct gendisk *disk, fmode_t mode)
3261 struct dasd_device *base = dasd_device_from_gendisk(disk);
3262 if (base) {
3263 atomic_dec(&base->block->open_count);
3264 module_put(base->discipline->owner);
3265 dasd_put_device(base);
3270 * Return disk geometry.
3272 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3274 struct dasd_device *base;
3276 base = dasd_device_from_gendisk(bdev->bd_disk);
3277 if (!base)
3278 return -ENODEV;
3280 if (!base->discipline ||
3281 !base->discipline->fill_geometry) {
3282 dasd_put_device(base);
3283 return -EINVAL;
3285 base->discipline->fill_geometry(base->block, geo);
3286 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3287 dasd_put_device(base);
3288 return 0;
3291 const struct block_device_operations
3292 dasd_device_operations = {
3293 .owner = THIS_MODULE,
3294 .open = dasd_open,
3295 .release = dasd_release,
3296 .ioctl = dasd_ioctl,
3297 .compat_ioctl = dasd_ioctl,
3298 .getgeo = dasd_getgeo,
3301 /*******************************************************************************
3302 * end of block device operations
3305 static void
3306 dasd_exit(void)
3308 #ifdef CONFIG_PROC_FS
3309 dasd_proc_exit();
3310 #endif
3311 dasd_eer_exit();
3312 if (dasd_page_cache != NULL) {
3313 kmem_cache_destroy(dasd_page_cache);
3314 dasd_page_cache = NULL;
3316 dasd_gendisk_exit();
3317 dasd_devmap_exit();
3318 if (dasd_debug_area != NULL) {
3319 debug_unregister(dasd_debug_area);
3320 dasd_debug_area = NULL;
3322 dasd_statistics_removeroot();
3326 * SECTION: common functions for ccw_driver use
3330 * Is the device read-only?
3331 * Note that this function does not report the setting of the
3332 * readonly device attribute, but how it is configured in z/VM.
3334 int dasd_device_is_ro(struct dasd_device *device)
3336 struct ccw_dev_id dev_id;
3337 struct diag210 diag_data;
3338 int rc;
3340 if (!MACHINE_IS_VM)
3341 return 0;
3342 ccw_device_get_id(device->cdev, &dev_id);
3343 memset(&diag_data, 0, sizeof(diag_data));
3344 diag_data.vrdcdvno = dev_id.devno;
3345 diag_data.vrdclen = sizeof(diag_data);
3346 rc = diag210(&diag_data);
3347 if (rc == 0 || rc == 2) {
3348 return diag_data.vrdcvfla & 0x80;
3349 } else {
3350 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3351 dev_id.devno, rc);
3352 return 0;
3355 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3357 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3359 struct ccw_device *cdev = data;
3360 int ret;
3362 ret = ccw_device_set_online(cdev);
3363 if (ret)
3364 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3365 dev_name(&cdev->dev), ret);
3369 * Initial attempt at a probe function. this can be simplified once
3370 * the other detection code is gone.
3372 int dasd_generic_probe(struct ccw_device *cdev,
3373 struct dasd_discipline *discipline)
3375 int ret;
3377 ret = dasd_add_sysfs_files(cdev);
3378 if (ret) {
3379 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3380 "dasd_generic_probe: could not add "
3381 "sysfs entries");
3382 return ret;
3384 cdev->handler = &dasd_int_handler;
3387 * Automatically online either all dasd devices (dasd_autodetect)
3388 * or all devices specified with dasd= parameters during
3389 * initial probe.
3391 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3392 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3393 async_schedule(dasd_generic_auto_online, cdev);
3394 return 0;
3396 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3398 void dasd_generic_free_discipline(struct dasd_device *device)
3400 /* Forget the discipline information. */
3401 if (device->discipline) {
3402 if (device->discipline->uncheck_device)
3403 device->discipline->uncheck_device(device);
3404 module_put(device->discipline->owner);
3405 device->discipline = NULL;
3407 if (device->base_discipline) {
3408 module_put(device->base_discipline->owner);
3409 device->base_discipline = NULL;
3412 EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3415 * This will one day be called from a global not_oper handler.
3416 * It is also used by driver_unregister during module unload.
3418 void dasd_generic_remove(struct ccw_device *cdev)
3420 struct dasd_device *device;
3421 struct dasd_block *block;
3423 cdev->handler = NULL;
3425 device = dasd_device_from_cdev(cdev);
3426 if (IS_ERR(device)) {
3427 dasd_remove_sysfs_files(cdev);
3428 return;
3430 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3431 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3432 /* Already doing offline processing */
3433 dasd_put_device(device);
3434 dasd_remove_sysfs_files(cdev);
3435 return;
3438 * This device is removed unconditionally. Set offline
3439 * flag to prevent dasd_open from opening it while it is
3440 * no quite down yet.
3442 dasd_set_target_state(device, DASD_STATE_NEW);
3443 /* dasd_delete_device destroys the device reference. */
3444 block = device->block;
3445 dasd_delete_device(device);
3447 * life cycle of block is bound to device, so delete it after
3448 * device was safely removed
3450 if (block)
3451 dasd_free_block(block);
3453 dasd_remove_sysfs_files(cdev);
3455 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3458 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3459 * the device is detected for the first time and is supposed to be used
3460 * or the user has started activation through sysfs.
3462 int dasd_generic_set_online(struct ccw_device *cdev,
3463 struct dasd_discipline *base_discipline)
3465 struct dasd_discipline *discipline;
3466 struct dasd_device *device;
3467 int rc;
3469 /* first online clears initial online feature flag */
3470 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3471 device = dasd_create_device(cdev);
3472 if (IS_ERR(device))
3473 return PTR_ERR(device);
3475 discipline = base_discipline;
3476 if (device->features & DASD_FEATURE_USEDIAG) {
3477 if (!dasd_diag_discipline_pointer) {
3478 /* Try to load the required module. */
3479 rc = request_module(DASD_DIAG_MOD);
3480 if (rc) {
3481 pr_warn("%s Setting the DASD online failed "
3482 "because the required module %s "
3483 "could not be loaded (rc=%d)\n",
3484 dev_name(&cdev->dev), DASD_DIAG_MOD,
3485 rc);
3486 dasd_delete_device(device);
3487 return -ENODEV;
3490 /* Module init could have failed, so check again here after
3491 * request_module(). */
3492 if (!dasd_diag_discipline_pointer) {
3493 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3494 dev_name(&cdev->dev));
3495 dasd_delete_device(device);
3496 return -ENODEV;
3498 discipline = dasd_diag_discipline_pointer;
3500 if (!try_module_get(base_discipline->owner)) {
3501 dasd_delete_device(device);
3502 return -EINVAL;
3504 if (!try_module_get(discipline->owner)) {
3505 module_put(base_discipline->owner);
3506 dasd_delete_device(device);
3507 return -EINVAL;
3509 device->base_discipline = base_discipline;
3510 device->discipline = discipline;
3512 /* check_device will allocate block device if necessary */
3513 rc = discipline->check_device(device);
3514 if (rc) {
3515 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3516 dev_name(&cdev->dev), discipline->name, rc);
3517 module_put(discipline->owner);
3518 module_put(base_discipline->owner);
3519 dasd_delete_device(device);
3520 return rc;
3523 dasd_set_target_state(device, DASD_STATE_ONLINE);
3524 if (device->state <= DASD_STATE_KNOWN) {
3525 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3526 dev_name(&cdev->dev));
3527 rc = -ENODEV;
3528 dasd_set_target_state(device, DASD_STATE_NEW);
3529 if (device->block)
3530 dasd_free_block(device->block);
3531 dasd_delete_device(device);
3532 } else
3533 pr_debug("dasd_generic device %s found\n",
3534 dev_name(&cdev->dev));
3536 wait_event(dasd_init_waitq, _wait_for_device(device));
3538 dasd_put_device(device);
3539 return rc;
3541 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3543 int dasd_generic_set_offline(struct ccw_device *cdev)
3545 struct dasd_device *device;
3546 struct dasd_block *block;
3547 int max_count, open_count, rc;
3548 unsigned long flags;
3550 rc = 0;
3551 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3552 device = dasd_device_from_cdev_locked(cdev);
3553 if (IS_ERR(device)) {
3554 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3555 return PTR_ERR(device);
3559 * We must make sure that this device is currently not in use.
3560 * The open_count is increased for every opener, that includes
3561 * the blkdev_get in dasd_scan_partitions. We are only interested
3562 * in the other openers.
3564 if (device->block) {
3565 max_count = device->block->bdev ? 0 : -1;
3566 open_count = atomic_read(&device->block->open_count);
3567 if (open_count > max_count) {
3568 if (open_count > 0)
3569 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3570 dev_name(&cdev->dev), open_count);
3571 else
3572 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3573 dev_name(&cdev->dev));
3574 rc = -EBUSY;
3575 goto out_err;
3580 * Test if the offline processing is already running and exit if so.
3581 * If a safe offline is being processed this could only be a normal
3582 * offline that should be able to overtake the safe offline and
3583 * cancel any I/O we do not want to wait for any longer
3585 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3586 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3587 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3588 &device->flags);
3589 } else {
3590 rc = -EBUSY;
3591 goto out_err;
3594 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3597 * if safe_offline is called set safe_offline_running flag and
3598 * clear safe_offline so that a call to normal offline
3599 * can overrun safe_offline processing
3601 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3602 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3603 /* need to unlock here to wait for outstanding I/O */
3604 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3606 * If we want to set the device safe offline all IO operations
3607 * should be finished before continuing the offline process
3608 * so sync bdev first and then wait for our queues to become
3609 * empty
3611 if (device->block) {
3612 rc = fsync_bdev(device->block->bdev);
3613 if (rc != 0)
3614 goto interrupted;
3616 dasd_schedule_device_bh(device);
3617 rc = wait_event_interruptible(shutdown_waitq,
3618 _wait_for_empty_queues(device));
3619 if (rc != 0)
3620 goto interrupted;
3623 * check if a normal offline process overtook the offline
3624 * processing in this case simply do nothing beside returning
3625 * that we got interrupted
3626 * otherwise mark safe offline as not running any longer and
3627 * continue with normal offline
3629 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3630 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3631 rc = -ERESTARTSYS;
3632 goto out_err;
3634 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3636 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3638 dasd_set_target_state(device, DASD_STATE_NEW);
3639 /* dasd_delete_device destroys the device reference. */
3640 block = device->block;
3641 dasd_delete_device(device);
3643 * life cycle of block is bound to device, so delete it after
3644 * device was safely removed
3646 if (block)
3647 dasd_free_block(block);
3649 return 0;
3651 interrupted:
3652 /* interrupted by signal */
3653 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3654 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3655 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3656 out_err:
3657 dasd_put_device(device);
3658 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3659 return rc;
3661 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3663 int dasd_generic_last_path_gone(struct dasd_device *device)
3665 struct dasd_ccw_req *cqr;
3667 dev_warn(&device->cdev->dev, "No operational channel path is left "
3668 "for the device\n");
3669 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3670 /* First of all call extended error reporting. */
3671 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3673 if (device->state < DASD_STATE_BASIC)
3674 return 0;
3675 /* Device is active. We want to keep it. */
3676 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3677 if ((cqr->status == DASD_CQR_IN_IO) ||
3678 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3679 cqr->status = DASD_CQR_QUEUED;
3680 cqr->retries++;
3682 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3683 dasd_device_clear_timer(device);
3684 dasd_schedule_device_bh(device);
3685 return 1;
3687 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3689 int dasd_generic_path_operational(struct dasd_device *device)
3691 dev_info(&device->cdev->dev, "A channel path to the device has become "
3692 "operational\n");
3693 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3694 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3695 if (device->stopped & DASD_UNRESUMED_PM) {
3696 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3697 dasd_restore_device(device);
3698 return 1;
3700 dasd_schedule_device_bh(device);
3701 if (device->block) {
3702 dasd_schedule_block_bh(device->block);
3703 if (device->block->request_queue)
3704 blk_mq_run_hw_queues(device->block->request_queue,
3705 true);
3708 if (!device->stopped)
3709 wake_up(&generic_waitq);
3711 return 1;
3713 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3715 int dasd_generic_notify(struct ccw_device *cdev, int event)
3717 struct dasd_device *device;
3718 int ret;
3720 device = dasd_device_from_cdev_locked(cdev);
3721 if (IS_ERR(device))
3722 return 0;
3723 ret = 0;
3724 switch (event) {
3725 case CIO_GONE:
3726 case CIO_BOXED:
3727 case CIO_NO_PATH:
3728 dasd_path_no_path(device);
3729 ret = dasd_generic_last_path_gone(device);
3730 break;
3731 case CIO_OPER:
3732 ret = 1;
3733 if (dasd_path_get_opm(device))
3734 ret = dasd_generic_path_operational(device);
3735 break;
3737 dasd_put_device(device);
3738 return ret;
3740 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3742 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3744 struct dasd_device *device;
3745 int chp, oldopm, hpfpm, ifccpm;
3747 device = dasd_device_from_cdev_locked(cdev);
3748 if (IS_ERR(device))
3749 return;
3751 oldopm = dasd_path_get_opm(device);
3752 for (chp = 0; chp < 8; chp++) {
3753 if (path_event[chp] & PE_PATH_GONE) {
3754 dasd_path_notoper(device, chp);
3756 if (path_event[chp] & PE_PATH_AVAILABLE) {
3757 dasd_path_available(device, chp);
3758 dasd_schedule_device_bh(device);
3760 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3761 if (!dasd_path_is_operational(device, chp) &&
3762 !dasd_path_need_verify(device, chp)) {
3764 * we can not establish a pathgroup on an
3765 * unavailable path, so trigger a path
3766 * verification first
3768 dasd_path_available(device, chp);
3769 dasd_schedule_device_bh(device);
3771 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3772 "Pathgroup re-established\n");
3773 if (device->discipline->kick_validate)
3774 device->discipline->kick_validate(device);
3777 hpfpm = dasd_path_get_hpfpm(device);
3778 ifccpm = dasd_path_get_ifccpm(device);
3779 if (!dasd_path_get_opm(device) && hpfpm) {
3781 * device has no operational paths but at least one path is
3782 * disabled due to HPF errors
3783 * disable HPF at all and use the path(s) again
3785 if (device->discipline->disable_hpf)
3786 device->discipline->disable_hpf(device);
3787 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3788 dasd_path_set_tbvpm(device, hpfpm);
3789 dasd_schedule_device_bh(device);
3790 dasd_schedule_requeue(device);
3791 } else if (!dasd_path_get_opm(device) && ifccpm) {
3793 * device has no operational paths but at least one path is
3794 * disabled due to IFCC errors
3795 * trigger path verification on paths with IFCC errors
3797 dasd_path_set_tbvpm(device, ifccpm);
3798 dasd_schedule_device_bh(device);
3800 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
3801 dev_warn(&device->cdev->dev,
3802 "No verified channel paths remain for the device\n");
3803 DBF_DEV_EVENT(DBF_WARNING, device,
3804 "%s", "last verified path gone");
3805 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3806 dasd_device_set_stop_bits(device,
3807 DASD_STOPPED_DC_WAIT);
3809 dasd_put_device(device);
3811 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3813 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3815 if (!dasd_path_get_opm(device) && lpm) {
3816 dasd_path_set_opm(device, lpm);
3817 dasd_generic_path_operational(device);
3818 } else
3819 dasd_path_add_opm(device, lpm);
3820 return 0;
3822 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3825 * clear active requests and requeue them to block layer if possible
3827 static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3829 struct list_head requeue_queue;
3830 struct dasd_ccw_req *cqr, *n;
3831 struct dasd_ccw_req *refers;
3832 int rc;
3834 INIT_LIST_HEAD(&requeue_queue);
3835 spin_lock_irq(get_ccwdev_lock(device->cdev));
3836 rc = 0;
3837 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3838 /* Check status and move request to flush_queue */
3839 if (cqr->status == DASD_CQR_IN_IO) {
3840 rc = device->discipline->term_IO(cqr);
3841 if (rc) {
3842 /* unable to terminate requeust */
3843 dev_err(&device->cdev->dev,
3844 "Unable to terminate request %p "
3845 "on suspend\n", cqr);
3846 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3847 dasd_put_device(device);
3848 return rc;
3851 list_move_tail(&cqr->devlist, &requeue_queue);
3853 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3855 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3856 wait_event(dasd_flush_wq,
3857 (cqr->status != DASD_CQR_CLEAR_PENDING));
3860 * requeue requests to blocklayer will only work
3861 * for block device requests
3863 if (_dasd_requeue_request(cqr))
3864 continue;
3866 /* remove requests from device and block queue */
3867 list_del_init(&cqr->devlist);
3868 while (cqr->refers != NULL) {
3869 refers = cqr->refers;
3870 /* remove the request from the block queue */
3871 list_del(&cqr->blocklist);
3872 /* free the finished erp request */
3873 dasd_free_erp_request(cqr, cqr->memdev);
3874 cqr = refers;
3878 * _dasd_requeue_request already checked for a valid
3879 * blockdevice, no need to check again
3880 * all erp requests (cqr->refers) have a cqr->block
3881 * pointer copy from the original cqr
3883 list_del_init(&cqr->blocklist);
3884 cqr->block->base->discipline->free_cp(
3885 cqr, (struct request *) cqr->callback_data);
3889 * if requests remain then they are internal request
3890 * and go back to the device queue
3892 if (!list_empty(&requeue_queue)) {
3893 /* move freeze_queue to start of the ccw_queue */
3894 spin_lock_irq(get_ccwdev_lock(device->cdev));
3895 list_splice_tail(&requeue_queue, &device->ccw_queue);
3896 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3898 dasd_schedule_device_bh(device);
3899 return rc;
3902 static void do_requeue_requests(struct work_struct *work)
3904 struct dasd_device *device = container_of(work, struct dasd_device,
3905 requeue_requests);
3906 dasd_generic_requeue_all_requests(device);
3907 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
3908 if (device->block)
3909 dasd_schedule_block_bh(device->block);
3910 dasd_put_device(device);
3913 void dasd_schedule_requeue(struct dasd_device *device)
3915 dasd_get_device(device);
3916 /* queue call to dasd_reload_device to the kernel event daemon. */
3917 if (!schedule_work(&device->requeue_requests))
3918 dasd_put_device(device);
3920 EXPORT_SYMBOL(dasd_schedule_requeue);
3922 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3924 struct dasd_device *device = dasd_device_from_cdev(cdev);
3926 if (IS_ERR(device))
3927 return PTR_ERR(device);
3929 /* mark device as suspended */
3930 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3932 if (device->discipline->freeze)
3933 device->discipline->freeze(device);
3935 /* disallow new I/O */
3936 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3938 return dasd_generic_requeue_all_requests(device);
3940 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3942 int dasd_generic_restore_device(struct ccw_device *cdev)
3944 struct dasd_device *device = dasd_device_from_cdev(cdev);
3945 int rc = 0;
3947 if (IS_ERR(device))
3948 return PTR_ERR(device);
3950 /* allow new IO again */
3951 dasd_device_remove_stop_bits(device,
3952 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3954 dasd_schedule_device_bh(device);
3957 * call discipline restore function
3958 * if device is stopped do nothing e.g. for disconnected devices
3960 if (device->discipline->restore && !(device->stopped))
3961 rc = device->discipline->restore(device);
3962 if (rc || device->stopped)
3964 * if the resume failed for the DASD we put it in
3965 * an UNRESUMED stop state
3967 device->stopped |= DASD_UNRESUMED_PM;
3969 if (device->block) {
3970 dasd_schedule_block_bh(device->block);
3971 if (device->block->request_queue)
3972 blk_mq_run_hw_queues(device->block->request_queue,
3973 true);
3976 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3977 dasd_put_device(device);
3978 return 0;
3980 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3982 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3983 void *rdc_buffer,
3984 int rdc_buffer_size,
3985 int magic)
3987 struct dasd_ccw_req *cqr;
3988 struct ccw1 *ccw;
3989 unsigned long *idaw;
3991 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
3992 NULL);
3994 if (IS_ERR(cqr)) {
3995 /* internal error 13 - Allocating the RDC request failed*/
3996 dev_err(&device->cdev->dev,
3997 "An error occurred in the DASD device driver, "
3998 "reason=%s\n", "13");
3999 return cqr;
4002 ccw = cqr->cpaddr;
4003 ccw->cmd_code = CCW_CMD_RDC;
4004 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
4005 idaw = (unsigned long *) (cqr->data);
4006 ccw->cda = (__u32)(addr_t) idaw;
4007 ccw->flags = CCW_FLAG_IDA;
4008 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
4009 } else {
4010 ccw->cda = (__u32)(addr_t) rdc_buffer;
4011 ccw->flags = 0;
4014 ccw->count = rdc_buffer_size;
4015 cqr->startdev = device;
4016 cqr->memdev = device;
4017 cqr->expires = 10*HZ;
4018 cqr->retries = 256;
4019 cqr->buildclk = get_tod_clock();
4020 cqr->status = DASD_CQR_FILLED;
4021 return cqr;
4025 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
4026 void *rdc_buffer, int rdc_buffer_size)
4028 int ret;
4029 struct dasd_ccw_req *cqr;
4031 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
4032 magic);
4033 if (IS_ERR(cqr))
4034 return PTR_ERR(cqr);
4036 ret = dasd_sleep_on(cqr);
4037 dasd_sfree_request(cqr, cqr->memdev);
4038 return ret;
4040 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
4043 * In command mode and transport mode we need to look for sense
4044 * data in different places. The sense data itself is allways
4045 * an array of 32 bytes, so we can unify the sense data access
4046 * for both modes.
4048 char *dasd_get_sense(struct irb *irb)
4050 struct tsb *tsb = NULL;
4051 char *sense = NULL;
4053 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4054 if (irb->scsw.tm.tcw)
4055 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4056 irb->scsw.tm.tcw);
4057 if (tsb && tsb->length == 64 && tsb->flags)
4058 switch (tsb->flags & 0x07) {
4059 case 1: /* tsa_iostat */
4060 sense = tsb->tsa.iostat.sense;
4061 break;
4062 case 2: /* tsa_ddpc */
4063 sense = tsb->tsa.ddpc.sense;
4064 break;
4065 default:
4066 /* currently we don't use interrogate data */
4067 break;
4069 } else if (irb->esw.esw0.erw.cons) {
4070 sense = irb->ecw;
4072 return sense;
4074 EXPORT_SYMBOL_GPL(dasd_get_sense);
4076 void dasd_generic_shutdown(struct ccw_device *cdev)
4078 struct dasd_device *device;
4080 device = dasd_device_from_cdev(cdev);
4081 if (IS_ERR(device))
4082 return;
4084 if (device->block)
4085 dasd_schedule_block_bh(device->block);
4087 dasd_schedule_device_bh(device);
4089 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4091 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4093 static int __init dasd_init(void)
4095 int rc;
4097 init_waitqueue_head(&dasd_init_waitq);
4098 init_waitqueue_head(&dasd_flush_wq);
4099 init_waitqueue_head(&generic_waitq);
4100 init_waitqueue_head(&shutdown_waitq);
4102 /* register 'common' DASD debug area, used for all DBF_XXX calls */
4103 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
4104 if (dasd_debug_area == NULL) {
4105 rc = -ENOMEM;
4106 goto failed;
4108 debug_register_view(dasd_debug_area, &debug_sprintf_view);
4109 debug_set_level(dasd_debug_area, DBF_WARNING);
4111 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4113 dasd_diag_discipline_pointer = NULL;
4115 dasd_statistics_createroot();
4117 rc = dasd_devmap_init();
4118 if (rc)
4119 goto failed;
4120 rc = dasd_gendisk_init();
4121 if (rc)
4122 goto failed;
4123 rc = dasd_parse();
4124 if (rc)
4125 goto failed;
4126 rc = dasd_eer_init();
4127 if (rc)
4128 goto failed;
4129 #ifdef CONFIG_PROC_FS
4130 rc = dasd_proc_init();
4131 if (rc)
4132 goto failed;
4133 #endif
4135 return 0;
4136 failed:
4137 pr_info("The DASD device driver could not be initialized\n");
4138 dasd_exit();
4139 return rc;
4142 module_init(dasd_init);
4143 module_exit(dasd_exit);