blob: 5645eb7cc39576000592b7e5b8ef7d841900bc3c [file] [log] [blame]
Greg Kroah-Hartman6a55d2c2017-11-14 18:38:00 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02008 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011#define KMSG_COMPONENT "dasd"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080020#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020021#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010022#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020023#include <linux/debugfs.h>
24#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020025#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/ccwdev.h>
28#include <asm/ebcdic.h>
29#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010030#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010031#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* This is ugly... */
34#define PRINTK_HEADER "dasd:"
35
36#include "dasd_int.h"
37/*
38 * SECTION: Constant definitions to be used within this file
39 */
40#define DASD_CHANQ_MAX_SIZE 4
41
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +020042#define DASD_DIAG_MOD "dasd_diag_mod"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * SECTION: exported variables of dasd.c
46 */
47debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020048EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020049static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020051EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010052void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
55MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020056 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_LICENSE("GPL");
59
60/*
61 * SECTION: prototypes for static functions of dasd.c
62 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010063static int dasd_alloc_queue(struct dasd_block *);
64static void dasd_setup_queue(struct dasd_block *);
65static void dasd_free_queue(struct dasd_block *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010066static int dasd_flush_block_queue(struct dasd_block *);
Sebastian Ottd2f2df62018-07-03 13:06:45 +020067static void dasd_device_tasklet(unsigned long);
68static void dasd_block_tasklet(unsigned long);
Al Viro4927b3f2006-12-06 19:18:20 +000069static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020070static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020071static void do_reload_device(struct work_struct *);
Stefan Haberlanda521b042016-08-08 15:56:54 +020072static void do_requeue_requests(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Kees Cooke99e88a2017-10-16 14:43:17 -070074static void dasd_device_timeout(struct timer_list *);
75static void dasd_block_timeout(struct timer_list *);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010076static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020077static void dasd_profile_init(struct dasd_profile *, struct dentry *);
78static void dasd_profile_exit(struct dasd_profile *);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +010079static void dasd_hosts_init(struct dentry *, struct dasd_device *);
80static void dasd_hosts_exit(struct dasd_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * SECTION: Operations on the device structure.
84 */
85static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020086static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020087static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020088static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Allocate memory for a new device structure.
92 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010093struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct dasd_device *device;
96
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010097 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
98 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* Get two pages for normal block device operations. */
102 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100103 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kfree(device);
105 return ERR_PTR(-ENOMEM);
106 }
107 /* Get one page for error recovery. */
108 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100109 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 free_pages((unsigned long) device->ccw_mem, 1);
111 kfree(device);
112 return ERR_PTR(-ENOMEM);
113 }
114
115 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
116 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
117 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100118 atomic_set(&device->tasklet_scheduled, 0);
Sebastian Ottd2f2df62018-07-03 13:06:45 +0200119 tasklet_init(&device->tasklet, dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 (unsigned long) device);
121 INIT_LIST_HEAD(&device->ccw_queue);
Kees Cooke99e88a2017-10-16 14:43:17 -0700122 timer_setup(&device->timer, dasd_device_timeout, 0);
Al Viro4927b3f2006-12-06 19:18:20 +0000123 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200124 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200125 INIT_WORK(&device->reload_device, do_reload_device);
Stefan Haberlanda521b042016-08-08 15:56:54 +0200126 INIT_WORK(&device->requeue_requests, do_requeue_requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 device->state = DASD_STATE_NEW;
128 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100129 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200130 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return device;
132}
133
134/*
135 * Free memory of a device structure.
136 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100137void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800139 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 free_page((unsigned long) device->erp_mem);
141 free_pages((unsigned long) device->ccw_mem, 1);
142 kfree(device);
143}
144
145/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100146 * Allocate memory for a new device structure.
147 */
148struct dasd_block *dasd_alloc_block(void)
149{
150 struct dasd_block *block;
151
152 block = kzalloc(sizeof(*block), GFP_ATOMIC);
153 if (!block)
154 return ERR_PTR(-ENOMEM);
155 /* open_count = 0 means device online but not in use */
156 atomic_set(&block->open_count, -1);
157
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100158 atomic_set(&block->tasklet_scheduled, 0);
Sebastian Ottd2f2df62018-07-03 13:06:45 +0200159 tasklet_init(&block->tasklet, dasd_block_tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100160 (unsigned long) block);
161 INIT_LIST_HEAD(&block->ccw_queue);
162 spin_lock_init(&block->queue_lock);
Kees Cooke99e88a2017-10-16 14:43:17 -0700163 timer_setup(&block->timer, dasd_block_timeout, 0);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200164 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100165
166 return block;
167}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200168EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100169
170/*
171 * Free memory of a device structure.
172 */
173void dasd_free_block(struct dasd_block *block)
174{
175 kfree(block);
176}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200177EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100178
179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Make a new device known to the system.
181 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100182static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int rc;
185
186 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200187 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * keep the reference count > 0.
189 */
190 dasd_get_device(device);
191
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100192 if (device->block) {
193 rc = dasd_alloc_queue(device->block);
194 if (rc) {
195 dasd_put_device(device);
196 return rc;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 device->state = DASD_STATE_KNOWN;
200 return 0;
201}
202
203/*
204 * Let the system forget about a device.
205 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800208 /* Disable extended error reporting for this device. */
209 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 device->state = DASD_STATE_NEW;
211
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 if (device->block)
213 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Give up reference we took in dasd_state_new_to_known. */
216 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200220static struct dentry *dasd_debugfs_setup(const char *name,
221 struct dentry *base_dentry)
222{
223 struct dentry *pde;
224
225 if (!base_dentry)
226 return NULL;
227 pde = debugfs_create_dir(name, base_dentry);
228 if (!pde || IS_ERR(pde))
229 return NULL;
230 return pde;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
234 * Request the irq line for the device.
235 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100236static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200238 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200239 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200242 if (block) {
243 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100244 if (rc)
245 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200246 block->debugfs_dentry =
247 dasd_debugfs_setup(block->gdp->disk_name,
248 dasd_debugfs_root_entry);
249 dasd_profile_init(&block->profile, block->debugfs_dentry);
250 if (dasd_global_profile_level == DASD_PROFILE_ON)
251 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100252 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 device->debugfs_dentry =
254 dasd_debugfs_setup(dev_name(&device->cdev->dev),
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&device->profile, device->debugfs_dentry);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100257 dasd_hosts_init(device->debugfs_dentry, device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100260 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100261 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200263 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
265
266 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200267
268 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/*
272 * Release the irq line for the device. Terminate any running i/o.
273 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100274static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Horst Hummel8f617012006-08-30 14:33:33 +0200276 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200277
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200278 if (device->discipline->basic_to_known) {
279 rc = device->discipline->basic_to_known(device);
280 if (rc)
281 return rc;
282 }
283
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200285 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200286 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100287 dasd_gendisk_free(device->block);
288 dasd_block_clear_timer(device->block);
289 }
290 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200291 if (rc)
292 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100293 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200294 dasd_profile_exit(&device->profile);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100295 dasd_hosts_exit(device);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200296 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
298 if (device->debug_area != NULL) {
299 debug_unregister(device->debug_area);
300 device->debug_area = NULL;
301 }
302 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/*
307 * Do the initial analysis. The do_analysis function may return
308 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
309 * until the discipline decides to continue the startup sequence
310 * by calling the function dasd_change_state. The eckd disciplines
311 * uses this to start a ccw that detects the format. The completion
312 * interrupt for this detection ccw uses the kernel event daemon to
313 * trigger the call to dasd_change_state. All this is done in the
314 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800315 * After the analysis ccw is done (do_analysis returned 0) the block
316 * device is setup.
317 * In case the analysis returns an error, the device setup is stopped
318 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100320static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100323 struct dasd_block *block;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200324 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800328 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329 if (block) {
330 if (block->base->discipline->do_analysis != NULL)
331 rc = block->base->discipline->do_analysis(block);
332 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200333 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 device->state = DASD_STATE_UNFMT;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200335 disk = device->block->gdp;
336 kobject_uevent(&disk_to_dev(disk)->kobj,
337 KOBJ_CHANGE);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200338 goto out;
339 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100340 return rc;
341 }
342 dasd_setup_queue(block);
343 set_capacity(block->gdp,
344 block->blocks << block->s2b_shift);
345 device->state = DASD_STATE_READY;
346 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200347 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100348 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200349 return rc;
350 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100351 } else {
352 device->state = DASD_STATE_READY;
353 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200354out:
355 if (device->discipline->basic_to_ready)
356 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800357 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100360static inline
361int _wait_for_empty_queues(struct dasd_device *device)
362{
363 if (device->block)
364 return list_empty(&device->ccw_queue) &&
365 list_empty(&device->block->ccw_queue);
366 else
367 return list_empty(&device->ccw_queue);
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*
371 * Remove device from block device layer. Destroy dirty buffers.
372 * Forget format information. Check if the target level is basic
373 * and if it is create fake disk for formatting.
374 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100375static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Horst Hummel8f617012006-08-30 14:33:33 +0200377 int rc;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380 if (device->block) {
381 struct dasd_block *block = device->block;
382 rc = dasd_flush_block_queue(block);
383 if (rc) {
384 device->state = DASD_STATE_READY;
385 return rc;
386 }
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100387 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100388 block->blocks = 0;
389 block->bp_block = 0;
390 block->s2b_shift = 0;
391 }
Horst Hummel8f617012006-08-30 14:33:33 +0200392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
Horst Hummel90f00942006-03-07 21:55:39 -0800396 * Back to basic.
397 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100398static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800399{
400 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200401 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800402}
403
404/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * Make the device online and schedule the bottom half to start
406 * the requeueing of requests from the linux request queue to the
407 * ccw queue.
408 */
Horst Hummel8f617012006-08-30 14:33:33 +0200409static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410dasd_state_ready_to_online(struct dasd_device * device)
411{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100412 struct gendisk *disk;
413 struct disk_part_iter piter;
414 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100417 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100418 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100419 if ((device->features & DASD_FEATURE_USERAW)) {
420 disk = device->block->gdp;
421 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
422 return 0;
423 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100424 disk = device->block->bdev->bd_disk;
425 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
426 while ((part = disk_part_iter_next(&piter)))
427 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
428 disk_part_iter_exit(&piter);
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433/*
434 * Stop the requeueing of requests again.
435 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100436static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100438 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100439 struct gendisk *disk;
440 struct disk_part_iter piter;
441 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100442
443 if (device->discipline->online_to_ready) {
444 rc = device->discipline->online_to_ready(device);
445 if (rc)
446 return rc;
447 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100450 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100451 disk = device->block->bdev->bd_disk;
452 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
453 while ((part = disk_part_iter_next(&piter)))
454 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
455 disk_part_iter_exit(&piter);
456 }
Horst Hummel8f617012006-08-30 14:33:33 +0200457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Device startup state changes.
462 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100463static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int rc;
466
467 rc = 0;
468 if (device->state == DASD_STATE_NEW &&
469 device->target >= DASD_STATE_KNOWN)
470 rc = dasd_state_new_to_known(device);
471
472 if (!rc &&
473 device->state == DASD_STATE_KNOWN &&
474 device->target >= DASD_STATE_BASIC)
475 rc = dasd_state_known_to_basic(device);
476
477 if (!rc &&
478 device->state == DASD_STATE_BASIC &&
479 device->target >= DASD_STATE_READY)
480 rc = dasd_state_basic_to_ready(device);
481
482 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700483 device->state == DASD_STATE_UNFMT &&
484 device->target > DASD_STATE_UNFMT)
485 rc = -EPERM;
486
487 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 device->state == DASD_STATE_READY &&
489 device->target >= DASD_STATE_ONLINE)
490 rc = dasd_state_ready_to_online(device);
491
492 return rc;
493}
494
495/*
496 * Device shutdown state changes.
497 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100498static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Horst Hummel8f617012006-08-30 14:33:33 +0200500 int rc;
501
502 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (device->state == DASD_STATE_ONLINE &&
504 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200505 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200506
Horst Hummel8f617012006-08-30 14:33:33 +0200507 if (!rc &&
508 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200510 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800511
Horst Hummel8f617012006-08-30 14:33:33 +0200512 if (!rc &&
513 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800514 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200515 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800516
Horst Hummel8f617012006-08-30 14:33:33 +0200517 if (!rc &&
518 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200520 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200521
Horst Hummel8f617012006-08-30 14:33:33 +0200522 if (!rc &&
523 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200525 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Horst Hummel8f617012006-08-30 14:33:33 +0200527 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
531 * This is the main startup/shutdown routine.
532 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100533static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Sebastian Ott181d9522009-06-22 12:08:21 +0200535 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (device->state == device->target)
538 /* Already where we want to go today... */
539 return;
540 if (device->state < device->target)
541 rc = dasd_increase_state(device);
542 else
543 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200544 if (rc == -EAGAIN)
545 return;
546 if (rc)
547 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200549 /* let user-space know that the device status changed */
550 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200551
552 if (device->state == device->target)
553 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556/*
557 * Kick starter for devices that did not complete the startup/shutdown
558 * procedure or were sleeping because of a pending state.
559 * dasd_kick_device will schedule a call do do_kick_device to the kernel
560 * event daemon.
561 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100562static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Al Viro4927b3f2006-12-06 19:18:20 +0000564 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100565 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100567 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 dasd_put_device(device);
570}
571
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100572void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 dasd_get_device(device);
575 /* queue call to dasd_kick_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200576 if (!schedule_work(&device->kick_work))
577 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200579EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200582 * dasd_reload_device will schedule a call do do_reload_device to the kernel
583 * event daemon.
584 */
585static void do_reload_device(struct work_struct *work)
586{
587 struct dasd_device *device = container_of(work, struct dasd_device,
588 reload_device);
589 device->discipline->reload(device);
590 dasd_put_device(device);
591}
592
593void dasd_reload_device(struct dasd_device *device)
594{
595 dasd_get_device(device);
596 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200597 if (!schedule_work(&device->reload_device))
598 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200599}
600EXPORT_SYMBOL(dasd_reload_device);
601
602/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200603 * dasd_restore_device will schedule a call do do_restore_device to the kernel
604 * event daemon.
605 */
606static void do_restore_device(struct work_struct *work)
607{
608 struct dasd_device *device = container_of(work, struct dasd_device,
609 restore_device);
610 device->cdev->drv->restore(device->cdev);
611 dasd_put_device(device);
612}
613
614void dasd_restore_device(struct dasd_device *device)
615{
616 dasd_get_device(device);
617 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200618 if (!schedule_work(&device->restore_device))
619 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200620}
621
622/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * Set the target state for a device and starts the state change.
624 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100625void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200627 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100628 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* If we are in probeonly mode stop at DASD_STATE_READY. */
630 if (dasd_probeonly && target > DASD_STATE_READY)
631 target = DASD_STATE_READY;
632 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100633 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 wake_up(&dasd_init_waitq);
635 device->target = target;
636 }
637 if (device->state != device->target)
638 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100639 mutex_unlock(&device->state_mutex);
640 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200642EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/*
645 * Enable devices with device numbers in [from..to].
646 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100647static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 return (device->state == device->target);
650}
651
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100652void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 dasd_set_target_state(device, DASD_STATE_ONLINE);
655 if (device->state <= DASD_STATE_KNOWN)
656 /* No discipline for device found. */
657 dasd_set_target_state(device, DASD_STATE_NEW);
658 /* Now wait for the devices to come up. */
659 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400660
661 dasd_reload_device(device);
662 if (device->discipline->kick_validate)
663 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200665EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/*
668 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
669 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200670
671unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100674struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100675 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
676};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200677static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/*
680 * Add profiling information for cqr before execution.
681 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100682static void dasd_profile_start(struct dasd_block *block,
683 struct dasd_ccw_req *cqr,
684 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct list_head *l;
687 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200688 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* count the length of the chanq for statistics */
691 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200692 if (dasd_global_profile_level || block->profile.data)
693 list_for_each(l, &block->ccw_queue)
694 if (++counter >= 31)
695 break;
696
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100697 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100698 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100699 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200700 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100701 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200702 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100703 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200704
705 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200706 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200707 block->profile.data->dasd_io_nr_req[counter]++;
708 if (rq_data_dir(req) == READ)
709 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200710 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200711 spin_unlock(&block->profile.lock);
712
713 /*
714 * We count the request for the start device, even though it may run on
715 * some other device due to error recovery. This way we make sure that
716 * we count each request only once.
717 */
718 device = cqr->startdev;
719 if (device->profile.data) {
720 counter = 1; /* request is not yet queued on the start device */
721 list_for_each(l, &device->ccw_queue)
722 if (++counter >= 31)
723 break;
724 }
725 spin_lock(&device->profile.lock);
726 if (device->profile.data) {
727 device->profile.data->dasd_io_nr_req[counter]++;
728 if (rq_data_dir(req) == READ)
729 device->profile.data->dasd_read_nr_req[counter]++;
730 }
731 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
735 * Add profiling information for cqr after execution.
736 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200737
738#define dasd_profile_counter(value, index) \
739{ \
740 for (index = 0; index < 31 && value >> (2+index); index++) \
741 ; \
742}
743
744static void dasd_profile_end_add_data(struct dasd_profile_info *data,
745 int is_alias,
746 int is_tpm,
747 int is_read,
748 long sectors,
749 int sectors_ind,
750 int tottime_ind,
751 int tottimeps_ind,
752 int strtime_ind,
753 int irqtime_ind,
754 int irqtimeps_ind,
755 int endtime_ind)
756{
757 /* in case of an overflow, reset the whole profile */
758 if (data->dasd_io_reqs == UINT_MAX) {
759 memset(data, 0, sizeof(*data));
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100760 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200761 }
762 data->dasd_io_reqs++;
763 data->dasd_io_sects += sectors;
764 if (is_alias)
765 data->dasd_io_alias++;
766 if (is_tpm)
767 data->dasd_io_tpm++;
768
769 data->dasd_io_secs[sectors_ind]++;
770 data->dasd_io_times[tottime_ind]++;
771 data->dasd_io_timps[tottimeps_ind]++;
772 data->dasd_io_time1[strtime_ind]++;
773 data->dasd_io_time2[irqtime_ind]++;
774 data->dasd_io_time2ps[irqtimeps_ind]++;
775 data->dasd_io_time3[endtime_ind]++;
776
777 if (is_read) {
778 data->dasd_read_reqs++;
779 data->dasd_read_sects += sectors;
780 if (is_alias)
781 data->dasd_read_alias++;
782 if (is_tpm)
783 data->dasd_read_tpm++;
784 data->dasd_read_secs[sectors_ind]++;
785 data->dasd_read_times[tottime_ind]++;
786 data->dasd_read_time1[strtime_ind]++;
787 data->dasd_read_time2[irqtime_ind]++;
788 data->dasd_read_time3[endtime_ind]++;
789 }
790}
791
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100792static void dasd_profile_end(struct dasd_block *block,
793 struct dasd_ccw_req *cqr,
794 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Stefan Haberlandd2907222017-07-14 10:33:18 +0200796 unsigned long strtime, irqtime, endtime, tottime;
797 unsigned long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200798 struct dasd_device *device;
799 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
800 int irqtime_ind, irqtimeps_ind, endtime_ind;
Stefan Haberlandd2907222017-07-14 10:33:18 +0200801 struct dasd_profile_info *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200803 device = cqr->startdev;
804 if (!(dasd_global_profile_level ||
805 block->profile.data ||
806 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return;
808
Tejun Heo83096eb2009-05-07 22:24:39 +0900809 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!cqr->buildclk || !cqr->startclk ||
811 !cqr->stopclk || !cqr->endclk ||
812 !sectors)
813 return;
814
815 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
816 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
817 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
818 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
819 tottimeps = tottime / sectors;
820
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200821 dasd_profile_counter(sectors, sectors_ind);
822 dasd_profile_counter(tottime, tottime_ind);
823 dasd_profile_counter(tottimeps, tottimeps_ind);
824 dasd_profile_counter(strtime, strtime_ind);
825 dasd_profile_counter(irqtime, irqtime_ind);
826 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
827 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100829 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100830 if (dasd_global_profile.data) {
Stefan Haberlandd2907222017-07-14 10:33:18 +0200831 data = dasd_global_profile.data;
832 data->dasd_sum_times += tottime;
833 data->dasd_sum_time_str += strtime;
834 data->dasd_sum_time_irq += irqtime;
835 data->dasd_sum_time_end += endtime;
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100836 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200837 cqr->startdev != block->base,
838 cqr->cpmode == 1,
839 rq_data_dir(req) == READ,
840 sectors, sectors_ind, tottime_ind,
841 tottimeps_ind, strtime_ind,
842 irqtime_ind, irqtimeps_ind,
843 endtime_ind);
844 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100845 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200847 spin_lock(&block->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200848 if (block->profile.data) {
849 data = block->profile.data;
850 data->dasd_sum_times += tottime;
851 data->dasd_sum_time_str += strtime;
852 data->dasd_sum_time_irq += irqtime;
853 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200854 dasd_profile_end_add_data(block->profile.data,
855 cqr->startdev != block->base,
856 cqr->cpmode == 1,
857 rq_data_dir(req) == READ,
858 sectors, sectors_ind, tottime_ind,
859 tottimeps_ind, strtime_ind,
860 irqtime_ind, irqtimeps_ind,
861 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200862 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200863 spin_unlock(&block->profile.lock);
864
865 spin_lock(&device->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200866 if (device->profile.data) {
867 data = device->profile.data;
868 data->dasd_sum_times += tottime;
869 data->dasd_sum_time_str += strtime;
870 data->dasd_sum_time_irq += irqtime;
871 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200872 dasd_profile_end_add_data(device->profile.data,
873 cqr->startdev != block->base,
874 cqr->cpmode == 1,
875 rq_data_dir(req) == READ,
876 sectors, sectors_ind, tottime_ind,
877 tottimeps_ind, strtime_ind,
878 irqtime_ind, irqtimeps_ind,
879 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200880 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200881 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200883
884void dasd_profile_reset(struct dasd_profile *profile)
885{
886 struct dasd_profile_info *data;
887
888 spin_lock_bh(&profile->lock);
889 data = profile->data;
890 if (!data) {
891 spin_unlock_bh(&profile->lock);
892 return;
893 }
894 memset(data, 0, sizeof(*data));
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100895 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200896 spin_unlock_bh(&profile->lock);
897}
898
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200899int dasd_profile_on(struct dasd_profile *profile)
900{
901 struct dasd_profile_info *data;
902
903 data = kzalloc(sizeof(*data), GFP_KERNEL);
904 if (!data)
905 return -ENOMEM;
906 spin_lock_bh(&profile->lock);
907 if (profile->data) {
908 spin_unlock_bh(&profile->lock);
909 kfree(data);
910 return 0;
911 }
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100912 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200913 profile->data = data;
914 spin_unlock_bh(&profile->lock);
915 return 0;
916}
917
918void dasd_profile_off(struct dasd_profile *profile)
919{
920 spin_lock_bh(&profile->lock);
921 kfree(profile->data);
922 profile->data = NULL;
923 spin_unlock_bh(&profile->lock);
924}
925
926char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
927{
928 char *buffer;
929
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200930 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200931 if (buffer == NULL)
932 return ERR_PTR(-ENOMEM);
933 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200934 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200935 return ERR_PTR(-EFAULT);
936 }
937 /* got the string, now strip linefeed. */
938 if (buffer[user_len - 1] == '\n')
939 buffer[user_len - 1] = 0;
940 else
941 buffer[user_len] = 0;
942 return buffer;
943}
944
945static ssize_t dasd_stats_write(struct file *file,
946 const char __user *user_buf,
947 size_t user_len, loff_t *pos)
948{
949 char *buffer, *str;
950 int rc;
951 struct seq_file *m = (struct seq_file *)file->private_data;
952 struct dasd_profile *prof = m->private;
953
954 if (user_len > 65536)
955 user_len = 65536;
956 buffer = dasd_get_user_string(user_buf, user_len);
957 if (IS_ERR(buffer))
958 return PTR_ERR(buffer);
959
960 str = skip_spaces(buffer);
961 rc = user_len;
962 if (strncmp(str, "reset", 5) == 0) {
963 dasd_profile_reset(prof);
964 } else if (strncmp(str, "on", 2) == 0) {
965 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100966 if (rc)
967 goto out;
968 rc = user_len;
969 if (prof == &dasd_global_profile) {
970 dasd_profile_reset(prof);
971 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
972 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200973 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100974 if (prof == &dasd_global_profile)
975 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200976 dasd_profile_off(prof);
977 } else
978 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100979out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200980 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200981 return rc;
982}
983
984static void dasd_stats_array(struct seq_file *m, unsigned int *array)
985{
986 int i;
987
988 for (i = 0; i < 32; i++)
989 seq_printf(m, "%u ", array[i]);
990 seq_putc(m, '\n');
991}
992
993static void dasd_stats_seq_print(struct seq_file *m,
994 struct dasd_profile_info *data)
995{
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100996 seq_printf(m, "start_time %lld.%09ld\n",
997 (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200998 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
999 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1000 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1001 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Stefan Haberlandd2907222017-07-14 10:33:18 +02001002 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1003 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1004 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1005 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1006 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1007 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1008 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1009 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001010 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001011 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001012 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001013 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001014 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001015 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001016 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001017 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001018 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001019 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001020 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001021 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001022 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001023 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001024 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001025 dasd_stats_array(m, data->dasd_io_nr_req);
1026 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1027 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1028 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1029 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001030 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001031 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001032 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001033 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001034 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001035 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001036 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001037 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001038 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001039 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001040 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001041 dasd_stats_array(m, data->dasd_read_nr_req);
1042}
1043
1044static int dasd_stats_show(struct seq_file *m, void *v)
1045{
1046 struct dasd_profile *profile;
1047 struct dasd_profile_info *data;
1048
1049 profile = m->private;
1050 spin_lock_bh(&profile->lock);
1051 data = profile->data;
1052 if (!data) {
1053 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001054 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001055 return 0;
1056 }
1057 dasd_stats_seq_print(m, data);
1058 spin_unlock_bh(&profile->lock);
1059 return 0;
1060}
1061
1062static int dasd_stats_open(struct inode *inode, struct file *file)
1063{
1064 struct dasd_profile *profile = inode->i_private;
1065 return single_open(file, dasd_stats_show, profile);
1066}
1067
1068static const struct file_operations dasd_stats_raw_fops = {
1069 .owner = THIS_MODULE,
1070 .open = dasd_stats_open,
1071 .read = seq_read,
1072 .llseek = seq_lseek,
1073 .release = single_release,
1074 .write = dasd_stats_write,
1075};
1076
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001077static void dasd_profile_init(struct dasd_profile *profile,
1078 struct dentry *base_dentry)
1079{
Al Virof4ae40a62011-07-24 04:33:43 -04001080 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001081 struct dentry *pde;
1082
1083 if (!base_dentry)
1084 return;
1085 profile->dentry = NULL;
1086 profile->data = NULL;
1087 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1088 pde = debugfs_create_file("statistics", mode, base_dentry,
1089 profile, &dasd_stats_raw_fops);
1090 if (pde && !IS_ERR(pde))
1091 profile->dentry = pde;
1092 return;
1093}
1094
1095static void dasd_profile_exit(struct dasd_profile *profile)
1096{
1097 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001098 debugfs_remove(profile->dentry);
1099 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001100}
1101
1102static void dasd_statistics_removeroot(void)
1103{
1104 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001105 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001106 debugfs_remove(dasd_debugfs_global_entry);
1107 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001108}
1109
1110static void dasd_statistics_createroot(void)
1111{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001112 struct dentry *pde;
1113
1114 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001115 pde = debugfs_create_dir("dasd", NULL);
1116 if (!pde || IS_ERR(pde))
1117 goto error;
1118 dasd_debugfs_root_entry = pde;
1119 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1120 if (!pde || IS_ERR(pde))
1121 goto error;
1122 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001123 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001124 return;
1125
1126error:
1127 DBF_EVENT(DBF_ERR, "%s",
1128 "Creation of the dasd debugfs interface failed");
1129 dasd_statistics_removeroot();
1130 return;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001134#define dasd_profile_start(block, cqr, req) do {} while (0)
1135#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001136
1137static void dasd_statistics_createroot(void)
1138{
1139 return;
1140}
1141
1142static void dasd_statistics_removeroot(void)
1143{
1144 return;
1145}
1146
1147int dasd_stats_generic_show(struct seq_file *m, void *v)
1148{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001149 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001150 return 0;
1151}
1152
1153static void dasd_profile_init(struct dasd_profile *profile,
1154 struct dentry *base_dentry)
1155{
1156 return;
1157}
1158
1159static void dasd_profile_exit(struct dasd_profile *profile)
1160{
1161 return;
1162}
1163
1164int dasd_profile_on(struct dasd_profile *profile)
1165{
1166 return 0;
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#endif /* CONFIG_DASD_PROFILE */
1170
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001171static int dasd_hosts_show(struct seq_file *m, void *v)
1172{
1173 struct dasd_device *device;
1174 int rc = -EOPNOTSUPP;
1175
1176 device = m->private;
1177 dasd_get_device(device);
1178
1179 if (device->discipline->hosts_print)
1180 rc = device->discipline->hosts_print(device, m);
1181
1182 dasd_put_device(device);
1183 return rc;
1184}
1185
1186static int dasd_hosts_open(struct inode *inode, struct file *file)
1187{
1188 struct dasd_device *device = inode->i_private;
1189
1190 return single_open(file, dasd_hosts_show, device);
1191}
1192
1193static const struct file_operations dasd_hosts_fops = {
1194 .owner = THIS_MODULE,
1195 .open = dasd_hosts_open,
1196 .read = seq_read,
1197 .llseek = seq_lseek,
1198 .release = single_release,
1199};
1200
1201static void dasd_hosts_exit(struct dasd_device *device)
1202{
1203 debugfs_remove(device->hosts_dentry);
1204 device->hosts_dentry = NULL;
1205}
1206
1207static void dasd_hosts_init(struct dentry *base_dentry,
1208 struct dasd_device *device)
1209{
1210 struct dentry *pde;
1211 umode_t mode;
1212
1213 if (!base_dentry)
1214 return;
1215
1216 mode = S_IRUSR | S_IFREG;
1217 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1218 device, &dasd_hosts_fops);
1219 if (pde && !IS_ERR(pde))
1220 device->hosts_dentry = pde;
1221}
1222
Sebastian Ottc5205f22018-06-04 19:07:39 +02001223struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
1224 struct dasd_device *device,
1225 struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 unsigned long flags;
Sebastian Ottc5205f22018-06-04 19:07:39 +02001228 char *data, *chunk;
1229 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (cplength > 0)
1232 size += cplength * sizeof(struct ccw1);
1233 if (datasize > 0)
1234 size += datasize;
Sebastian Ottc5205f22018-06-04 19:07:39 +02001235 if (!cqr)
1236 size += (sizeof(*cqr) + 7L) & -8L;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 spin_lock_irqsave(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001239 data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 spin_unlock_irqrestore(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001241 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return ERR_PTR(-ENOMEM);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001243 if (!cqr) {
1244 cqr = (void *) data;
1245 data += (sizeof(*cqr) + 7L) & -8L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Sebastian Ottc5205f22018-06-04 19:07:39 +02001247 memset(cqr, 0, sizeof(*cqr));
1248 cqr->mem_chunk = chunk;
1249 if (cplength > 0) {
1250 cqr->cpaddr = data;
1251 data += cplength * sizeof(struct ccw1);
1252 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (datasize > 0) {
1255 cqr->data = data;
1256 memset(cqr->data, 0, datasize);
1257 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001258 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1260 dasd_get_device(device);
1261 return cqr;
1262}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001263EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001265void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 unsigned long flags;
1268
1269 spin_lock_irqsave(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001270 dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 spin_unlock_irqrestore(&device->mem_lock, flags);
1272 dasd_put_device(device);
1273}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001274EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276/*
1277 * Check discipline magic in cqr.
1278 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001279static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct dasd_device *device;
1282
1283 if (cqr == NULL)
1284 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001285 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001287 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 " dasd_ccw_req 0x%08x magic doesn't match"
1289 " discipline 0x%08x",
1290 cqr->magic,
1291 *(unsigned int *) device->discipline->name);
1292 return -EINVAL;
1293 }
1294 return 0;
1295}
1296
1297/*
1298 * Terminate the current i/o and set the request to clear_pending.
1299 * Timer keeps device runnig.
1300 * ccw_device_clear can fail if the i/o subsystem
1301 * is in a bad mood.
1302 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001303int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct dasd_device *device;
1306 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001307 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /* Check the cqr */
1310 rc = dasd_check_cqr(cqr);
1311 if (rc)
1312 return rc;
1313 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001314 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1316 rc = ccw_device_clear(device->cdev, (long) cqr);
1317 switch (rc) {
1318 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001319 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001320 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001321 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 DBF_DEV_EVENT(DBF_DEBUG, device,
1323 "terminate cqr %p successful",
1324 cqr);
1325 break;
1326 case -ENODEV:
1327 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1328 "device gone, retry");
1329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001331 /*
1332 * device not valid so no I/O could be running
1333 * handle CQR as termination successful
1334 */
1335 cqr->status = DASD_CQR_CLEARED;
1336 cqr->stopclk = get_tod_clock();
1337 cqr->starttime = 0;
1338 /* no retries for invalid devices */
1339 cqr->retries = -1;
1340 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1341 "EINVAL, handle as terminated");
1342 /* fake rc to success */
1343 rc = 0;
1344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001346 /* internal error 10 - unknown rc*/
1347 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1348 dev_err(&device->cdev->dev, "An error occurred in the "
1349 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 BUG();
1351 break;
1352 }
1353 retries++;
1354 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001355 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return rc;
1357}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001358EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360/*
1361 * Start the i/o. This start_IO can fail if the channel is really busy.
1362 * In that case set up a timer to start the request later.
1363 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001364int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct dasd_device *device;
1367 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001368 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* Check the cqr */
1371 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001372 if (rc) {
1373 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001375 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001376 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001377 if (((cqr->block &&
1378 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1379 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1380 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1381 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1382 "because of stolen lock", cqr);
1383 cqr->status = DASD_CQR_ERROR;
1384 cqr->intrc = -EPERM;
1385 return -EPERM;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001388 /* internal error 14 - start_IO run out of retries */
1389 sprintf(errorstring, "14 %p", cqr);
1390 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1391 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EIO;
1394 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001395 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 cqr->starttime = jiffies;
1397 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001398 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001399 cqr->lpm &= dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001400 if (!cqr->lpm)
Stefan Haberlandc9346152016-08-08 15:53:54 +02001401 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001402 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001403 if (cqr->cpmode == 1) {
1404 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1405 (long) cqr, cqr->lpm);
1406 } else {
1407 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1408 (long) cqr, cqr->lpm, 0);
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 switch (rc) {
1411 case 0:
1412 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001415 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 "start_IO: device busy, retry later");
1417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001419 /* -EACCES indicates that the request used only a subset of the
1420 * available paths and all these paths are gone. If the lpm of
1421 * this request was only a subset of the opm (e.g. the ppm) then
1422 * we just do a retry with all available paths.
1423 * If we already use the full opm, something is amiss, and we
1424 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001426 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1427 DBF_DEV_EVENT(DBF_WARNING, device,
1428 "start_IO: selected paths gone (%x)",
1429 cqr->lpm);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001430 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1431 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001432 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1433 "start_IO: selected paths gone,"
1434 " retry on all paths");
1435 } else {
1436 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1437 "start_IO: all paths in opm gone,"
1438 " do path verification");
1439 dasd_generic_last_path_gone(device);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001440 dasd_path_no_path(device);
1441 dasd_path_set_tbvpm(device,
1442 ccw_device_get_path_mask(
1443 device->cdev));
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001447 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001448 "start_IO: -ENODEV device gone, retry");
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001451 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001452 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001454 case -EINVAL:
1455 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001456 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001457 "start_IO: -EINVAL device currently "
1458 "not accessible");
1459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001461 /* internal error 11 - unknown rc */
1462 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1463 dev_err(&device->cdev->dev,
1464 "An error occurred in the DASD device driver, "
1465 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 BUG();
1467 break;
1468 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001469 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return rc;
1471}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001472EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474/*
1475 * Timeout function for dasd devices. This is used for different purposes
1476 * 1) missing interrupt handler for normal operation
1477 * 2) delayed start of request where start_IO failed with -EBUSY
1478 * 3) timeout for missing state change interrupts
1479 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1480 * DASD_CQR_QUEUED for 2) and 3).
1481 */
Kees Cooke99e88a2017-10-16 14:43:17 -07001482static void dasd_device_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 unsigned long flags;
1485 struct dasd_device *device;
1486
Kees Cooke99e88a2017-10-16 14:43:17 -07001487 device = from_timer(device, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1489 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001490 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001492 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495/*
1496 * Setup timeout for a device in jiffies.
1497 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001498void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001500 if (expires == 0)
1501 del_timer(&device->timer);
1502 else
1503 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001505EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507/*
1508 * Clear timeout for a device.
1509 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001510void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001512 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001514EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001516static void dasd_handle_killed_request(struct ccw_device *cdev,
1517 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 struct dasd_ccw_req *cqr;
1520 struct dasd_device *device;
1521
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001522 if (!intparm)
1523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 cqr = (struct dasd_ccw_req *) intparm;
1525 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001526 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1527 "invalid status in handle_killed_request: "
1528 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return;
1530 }
1531
Stefan Haberland589c74d2010-02-26 22:37:47 +01001532 device = dasd_device_from_cdev_locked(cdev);
1533 if (IS_ERR(device)) {
1534 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1535 "unable to get device from cdev");
1536 return;
1537 }
1538
1539 if (!cqr->startdev ||
1540 device != cqr->startdev ||
1541 strncmp(cqr->startdev->discipline->ebcname,
1542 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001543 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1544 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001545 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return;
1547 }
1548
1549 /* Schedule request to be retried. */
1550 cqr->status = DASD_CQR_QUEUED;
1551
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001552 dasd_device_clear_timer(device);
1553 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 dasd_put_device(device);
1555}
1556
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001557void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001559 /* First of all start sense subsystem status request. */
1560 dasd_eer_snss(device);
1561
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001562 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001563 dasd_schedule_device_bh(device);
Stefan Haberlande4433432017-05-22 10:59:11 +02001564 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001565 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02001566 if (device->block->request_queue)
1567 blk_mq_run_hw_queues(device->block->request_queue,
1568 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001571EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Stefan Haberlanda521b042016-08-08 15:56:54 +02001573static int dasd_check_hpf_error(struct irb *irb)
1574{
1575 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1576 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1577 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/*
1581 * Interrupt handler for "normal" ssch-io based dasd devices.
1582 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001583void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1584 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct dasd_ccw_req *cqr, *next;
1587 struct dasd_device *device;
Jan Höppner7bf76f012017-08-15 16:40:18 +02001588 unsigned long now;
Jan Höppner8fd57522015-08-19 13:41:20 +02001589 int nrf_suppressed = 0;
1590 int fp_suppressed = 0;
1591 u8 *sense = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001594 cqr = (struct dasd_ccw_req *) intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (IS_ERR(irb)) {
1596 switch (PTR_ERR(irb)) {
1597 case -EIO:
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001598 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001599 device = cqr->startdev;
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001600 cqr->status = DASD_CQR_CLEARED;
1601 dasd_device_clear_timer(device);
1602 wake_up(&dasd_flush_wq);
1603 dasd_schedule_device_bh(device);
1604 return;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001608 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1609 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 break;
1611 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001612 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1613 "unknown error %ld\n", __func__,
1614 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001616 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return;
1618 }
1619
Heiko Carstens1aae0562013-01-30 09:49:40 +01001620 now = get_tod_clock();
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001621 /* check for conditions that should be handled immediately */
1622 if (!cqr ||
1623 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1624 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001625 if (cqr)
1626 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001627 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001628 if (IS_ERR(device))
1629 return;
1630 /* ignore unsolicited interrupts for DIAG discipline */
1631 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001635
1636 /*
1637 * In some cases 'File Protected' or 'No Record Found' errors
1638 * might be expected and debug log messages for the
1639 * corresponding interrupts shouldn't be written then.
1640 * Check if either of the according suppress bits is set.
1641 */
1642 sense = dasd_get_sense(irb);
1643 if (sense) {
1644 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1645 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1646 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1647 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1648 }
1649 if (!(fp_suppressed || nrf_suppressed))
1650 device->discipline->dump_sense_dbf(device, irb, "int");
1651
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001652 if (device->features & DASD_FEATURE_ERPLOG)
1653 device->discipline->dump_sense(device, cqr, irb);
1654 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001655 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001657
1658 /* check for for attention message */
1659 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1660 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland22021342017-01-25 14:47:32 +01001661 if (!IS_ERR(device)) {
1662 device->discipline->check_attention(device,
1663 irb->esw.esw1.lpum);
1664 dasd_put_device(device);
1665 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001666 }
1667
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001668 if (!cqr)
1669 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001671 device = (struct dasd_device *) cqr->startdev;
1672 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001674 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1675 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return;
1677 }
1678
1679 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001680 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001681 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001682 cqr->status = DASD_CQR_CLEARED;
1683 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001684 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001685 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return;
1687 }
1688
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001689 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001691 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1692 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return;
1694 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001695
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001696 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001698 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1699 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001700 /* request was completed successfully */
1701 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 cqr->stopclk = now;
1703 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001704 if (cqr->devlist.next != &device->ccw_queue) {
1705 next = list_entry(cqr->devlist.next,
1706 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001708 } else { /* error */
Stefan Haberlanda521b042016-08-08 15:56:54 +02001709 /* check for HPF error
1710 * call discipline function to requeue all requests
1711 * and disable HPF accordingly
1712 */
1713 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1714 device->discipline->handle_hpf_error)
1715 device->discipline->handle_hpf_error(device, irb);
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001716 /*
1717 * If we don't want complex ERP for this request, then just
1718 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001719 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001720 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001721 cqr->retries > 0) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001722 if (cqr->lpm == dasd_path_get_opm(device))
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001723 DBF_DEV_EVENT(DBF_DEBUG, device,
1724 "default ERP in fastpath "
1725 "(%i retries left)",
1726 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001727 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
Stefan Haberlandc9346152016-08-08 15:53:54 +02001728 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001729 cqr->status = DASD_CQR_QUEUED;
1730 next = cqr;
1731 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001733 }
1734 if (next && (next->status == DASD_CQR_QUEUED) &&
1735 (!device->stopped)) {
1736 if (device->discipline->start_IO(next) == 0)
1737 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 dasd_device_clear_timer(device);
1743 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001745EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001747enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1748{
1749 struct dasd_device *device;
1750
1751 device = dasd_device_from_cdev_locked(cdev);
1752
1753 if (IS_ERR(device))
1754 goto out;
1755 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1756 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001757 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001758 dasd_put_device(device);
1759 goto out;
1760 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001761 if (device->discipline->dump_sense_dbf)
1762 device->discipline->dump_sense_dbf(device, irb, "uc");
1763 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001764 dasd_put_device(device);
1765out:
1766 return UC_TODO_RETRY;
1767}
1768EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001771 * If we have an error on a dasd_block layer request then we cancel
1772 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001774static void __dasd_device_recovery(struct dasd_device *device,
1775 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct list_head *l, *n;
1778 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001781 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001783 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001785
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001786 list_for_each_safe(l, n, &device->ccw_queue) {
1787 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1788 if (cqr->status == DASD_CQR_QUEUED &&
1789 ref_cqr->block == cqr->block) {
1790 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001792 }
1793};
1794
1795/*
1796 * Remove those ccw requests from the queue that need to be returned
1797 * to the upper layer.
1798 */
1799static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1800 struct list_head *final_queue)
1801{
1802 struct list_head *l, *n;
1803 struct dasd_ccw_req *cqr;
1804
1805 /* Process request with final status. */
1806 list_for_each_safe(l, n, &device->ccw_queue) {
1807 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1808
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001809 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001810 if (cqr->status == DASD_CQR_QUEUED ||
1811 cqr->status == DASD_CQR_IN_IO ||
1812 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001813 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001814 if (cqr->status == DASD_CQR_ERROR) {
1815 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001817 /* Rechain finished requests to final queue */
1818 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820}
1821
Sebastian Ott5c618c02018-05-24 12:18:58 +02001822static void __dasd_process_cqr(struct dasd_device *device,
1823 struct dasd_ccw_req *cqr)
1824{
1825 char errorstring[ERRORLENGTH];
1826
1827 switch (cqr->status) {
1828 case DASD_CQR_SUCCESS:
1829 cqr->status = DASD_CQR_DONE;
1830 break;
1831 case DASD_CQR_ERROR:
1832 cqr->status = DASD_CQR_NEED_ERP;
1833 break;
1834 case DASD_CQR_CLEARED:
1835 cqr->status = DASD_CQR_TERMINATED;
1836 break;
1837 default:
1838 /* internal error 12 - wrong cqr status*/
1839 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1840 dev_err(&device->cdev->dev,
1841 "An error occurred in the DASD device driver, "
1842 "reason=%s\n", errorstring);
1843 BUG();
1844 }
1845 if (cqr->callback)
1846 cqr->callback(cqr, cqr->callback_data);
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001850 * the cqrs from the final queue are returned to the upper layer
1851 * by setting a dasd_block state and calling the callback function
1852 */
1853static void __dasd_device_process_final_queue(struct dasd_device *device,
1854 struct list_head *final_queue)
1855{
1856 struct list_head *l, *n;
1857 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001858 struct dasd_block *block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001859
1860 list_for_each_safe(l, n, final_queue) {
1861 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1862 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001863 block = cqr->block;
Sebastian Ott5c618c02018-05-24 12:18:58 +02001864 if (!block) {
1865 __dasd_process_cqr(device, cqr);
1866 } else {
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001867 spin_lock_bh(&block->queue_lock);
Sebastian Ott5c618c02018-05-24 12:18:58 +02001868 __dasd_process_cqr(device, cqr);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001869 spin_unlock_bh(&block->queue_lock);
Sebastian Ott5c618c02018-05-24 12:18:58 +02001870 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001871 }
1872}
1873
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001874/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * Take a look at the first request on the ccw queue and check
1876 * if it reached its expire time. If so, terminate the IO.
1877 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001878static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 struct dasd_ccw_req *cqr;
1881
1882 if (list_empty(&device->ccw_queue))
1883 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001884 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001885 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1886 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001887 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1888 /*
1889 * IO in safe offline processing should not
1890 * run out of retries
1891 */
1892 cqr->retries++;
1893 }
Horst Hummel29145a62006-12-04 15:40:15 +01001894 if (device->discipline->term_IO(cqr) != 0) {
1895 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001896 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001897 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001898 "ended, retrying in 5 s\n",
1899 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001900 cqr->expires += 5*HZ;
1901 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001902 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001903 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001904 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001905 "remaining\n", cqr, (cqr->expires/HZ),
1906 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908 }
1909}
1910
1911/*
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001912 * return 1 when device is not eligible for IO
1913 */
1914static int __dasd_device_is_unusable(struct dasd_device *device,
1915 struct dasd_ccw_req *cqr)
1916{
1917 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1918
Stefan Haberlande8ac0152017-05-18 13:24:45 +02001919 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
1920 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1921 /*
1922 * dasd is being set offline
1923 * but it is no safe offline where we have to allow I/O
1924 */
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001925 return 1;
1926 }
1927 if (device->stopped) {
1928 if (device->stopped & mask) {
1929 /* stopped and CQR will not change that. */
1930 return 1;
1931 }
1932 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1933 /* CQR is not able to change device to
1934 * operational. */
1935 return 1;
1936 }
1937 /* CQR required to get device operational. */
1938 }
1939 return 0;
1940}
1941
1942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * Take a look at the first request on the ccw queue and check
1944 * if it needs to be started.
1945 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001946static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct dasd_ccw_req *cqr;
1949 int rc;
1950
1951 if (list_empty(&device->ccw_queue))
1952 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001953 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001954 if (cqr->status != DASD_CQR_QUEUED)
1955 return;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001956 /* if device is not usable return request to upper layer */
1957 if (__dasd_device_is_unusable(device, cqr)) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001958 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001959 cqr->status = DASD_CQR_CLEARED;
1960 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001961 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001962 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001963
1964 rc = device->discipline->start_IO(cqr);
1965 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001966 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001967 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001968 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001969 } else
1970 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001971 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001972}
1973
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001974static void __dasd_device_check_path_events(struct dasd_device *device)
1975{
1976 int rc;
1977
Stefan Haberlandc9346152016-08-08 15:53:54 +02001978 if (!dasd_path_get_tbvpm(device))
1979 return;
1980
1981 if (device->stopped &
1982 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1983 return;
1984 rc = device->discipline->verify_path(device,
1985 dasd_path_get_tbvpm(device));
1986 if (rc)
1987 dasd_device_set_timer(device, 50);
1988 else
1989 dasd_path_clear_all_verify(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001990};
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001993 * Go through all request on the dasd_device request queue,
1994 * terminate them on the cdev if necessary, and return them to the
1995 * submitting layer via callback.
1996 * Note:
1997 * Make sure that all 'submitting layers' still exist when
1998 * this function is called!. In other words, when 'device' is a base
1999 * device then all block layer requests must have been removed before
2000 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002002int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002004 struct dasd_ccw_req *cqr, *n;
2005 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 INIT_LIST_HEAD(&flush_queue);
2009 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02002010 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002011 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02002012 /* Check status and move request to flush_queue */
2013 switch (cqr->status) {
2014 case DASD_CQR_IN_IO:
2015 rc = device->discipline->term_IO(cqr);
2016 if (rc) {
2017 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002018 dev_err(&device->cdev->dev,
2019 "Flushing the DASD request queue "
2020 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002021 /* stop flush processing */
2022 goto finished;
2023 }
2024 break;
2025 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002026 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002027 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002028 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002029 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002030 break;
2031 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002032 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002033 }
Horst Hummel8f617012006-08-30 14:33:33 +02002034finished:
2035 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002036 /*
2037 * After this point all requests must be in state CLEAR_PENDING,
2038 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2039 * one of the others.
2040 */
2041 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2042 wait_event(dasd_flush_wq,
2043 (cqr->status != DASD_CQR_CLEAR_PENDING));
2044 /*
2045 * Now set each request back to TERMINATED, DONE or NEED_ERP
2046 * and call the callback function of flushed requests
2047 */
2048 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002049 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002051EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053/*
2054 * Acquire the device lock and process queues for the device.
2055 */
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002056static void dasd_device_tasklet(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002058 struct dasd_device *device = (struct dasd_device *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 atomic_set (&device->tasklet_scheduled, 0);
2062 INIT_LIST_HEAD(&final_queue);
2063 spin_lock_irq(get_ccwdev_lock(device->cdev));
2064 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002065 __dasd_device_check_expire(device);
2066 /* find final requests on ccw queue */
2067 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002068 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2070 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002071 __dasd_device_process_final_queue(device, &final_queue);
2072 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002074 __dasd_device_start_head(device);
2075 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002076 if (waitqueue_active(&shutdown_waitq))
2077 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 dasd_put_device(device);
2079}
2080
2081/*
2082 * Schedules a call to dasd_tasklet over the device tasklet.
2083 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002087 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return;
2089 dasd_get_device(device);
2090 tasklet_hi_schedule(&device->tasklet);
2091}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002092EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002094void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2095{
2096 device->stopped |= bits;
2097}
2098EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2099
2100void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2101{
2102 device->stopped &= ~bits;
2103 if (!device->stopped)
2104 wake_up(&generic_waitq);
2105}
2106EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002109 * Queue a request to the head of the device ccw_queue.
2110 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002112void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 struct dasd_device *device;
2115 unsigned long flags;
2116
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002117 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002119 cqr->status = DASD_CQR_QUEUED;
2120 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002122 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2124}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002125EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002128 * Queue a request to the tail of the device ccw_queue.
2129 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002131void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 struct dasd_device *device;
2134 unsigned long flags;
2135
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002136 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002138 cqr->status = DASD_CQR_QUEUED;
2139 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002141 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2143}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002144EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002147 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002149void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002151 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2152 cqr->callback_data = DASD_SLEEPON_END_TAG;
2153 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2154 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
Stefan Haberland5915a872011-10-30 15:16:57 +01002156EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002158static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 struct dasd_device *device;
2161 int rc;
2162
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002163 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002165 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2167 return rc;
2168}
2169
2170/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002171 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2172 */
2173static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2174{
2175 struct dasd_device *device;
2176 dasd_erp_fn_t erp_fn;
2177
2178 if (cqr->status == DASD_CQR_FILLED)
2179 return 0;
2180 device = cqr->startdev;
2181 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2182 if (cqr->status == DASD_CQR_TERMINATED) {
2183 device->discipline->handle_terminated_request(cqr);
2184 return 1;
2185 }
2186 if (cqr->status == DASD_CQR_NEED_ERP) {
2187 erp_fn = device->discipline->erp_action(cqr);
2188 erp_fn(cqr);
2189 return 1;
2190 }
2191 if (cqr->status == DASD_CQR_FAILED)
2192 dasd_log_sense(cqr, &cqr->irb);
2193 if (cqr->refers) {
2194 __dasd_process_erp(device, cqr);
2195 return 1;
2196 }
2197 }
2198 return 0;
2199}
2200
2201static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2202{
2203 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2204 if (cqr->refers) /* erp is not done yet */
2205 return 1;
2206 return ((cqr->status != DASD_CQR_DONE) &&
2207 (cqr->status != DASD_CQR_FAILED));
2208 } else
2209 return (cqr->status == DASD_CQR_FILLED);
2210}
2211
2212static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2213{
2214 struct dasd_device *device;
2215 int rc;
2216 struct list_head ccw_queue;
2217 struct dasd_ccw_req *cqr;
2218
2219 INIT_LIST_HEAD(&ccw_queue);
2220 maincqr->status = DASD_CQR_FILLED;
2221 device = maincqr->startdev;
2222 list_add(&maincqr->blocklist, &ccw_queue);
2223 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2224 cqr = list_first_entry(&ccw_queue,
2225 struct dasd_ccw_req, blocklist)) {
2226
2227 if (__dasd_sleep_on_erp(cqr))
2228 continue;
2229 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2230 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002231 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2232 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2233 cqr->status = DASD_CQR_FAILED;
2234 cqr->intrc = -EPERM;
2235 continue;
2236 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002237 /* Non-temporary stop condition will trigger fail fast */
2238 if (device->stopped & ~DASD_STOPPED_PENDING &&
2239 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2240 (!dasd_eer_enabled(device))) {
2241 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002242 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002243 continue;
2244 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002245 /*
Stefan Haberlanda9f62732016-09-20 10:29:22 +02002246 * Don't try to start requests if device is in
2247 * offline processing, it might wait forever
2248 */
2249 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2250 cqr->status = DASD_CQR_FAILED;
2251 cqr->intrc = -ENODEV;
2252 continue;
2253 }
2254 /*
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002255 * Don't try to start requests if device is stopped
2256 * except path verification requests
2257 */
2258 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2259 if (interruptible) {
2260 rc = wait_event_interruptible(
2261 generic_waitq, !(device->stopped));
2262 if (rc == -ERESTARTSYS) {
2263 cqr->status = DASD_CQR_FAILED;
2264 maincqr->intrc = rc;
2265 continue;
2266 }
2267 } else
2268 wait_event(generic_waitq, !(device->stopped));
2269 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002270 if (!cqr->callback)
2271 cqr->callback = dasd_wakeup_cb;
2272
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002273 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002274 dasd_add_request_tail(cqr);
2275 if (interruptible) {
2276 rc = wait_event_interruptible(
2277 generic_waitq, _wait_for_wakeup(cqr));
2278 if (rc == -ERESTARTSYS) {
2279 dasd_cancel_req(cqr);
2280 /* wait (non-interruptible) for final status */
2281 wait_event(generic_waitq,
2282 _wait_for_wakeup(cqr));
2283 cqr->status = DASD_CQR_FAILED;
2284 maincqr->intrc = rc;
2285 continue;
2286 }
2287 } else
2288 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2289 }
2290
Heiko Carstens1aae0562013-01-30 09:49:40 +01002291 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002292 if ((maincqr->status != DASD_CQR_DONE) &&
2293 (maincqr->intrc != -ERESTARTSYS))
2294 dasd_log_sense(maincqr, &maincqr->irb);
2295 if (maincqr->status == DASD_CQR_DONE)
2296 rc = 0;
2297 else if (maincqr->intrc)
2298 rc = maincqr->intrc;
2299 else
2300 rc = -EIO;
2301 return rc;
2302}
2303
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002304static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2305{
2306 struct dasd_ccw_req *cqr;
2307
2308 list_for_each_entry(cqr, ccw_queue, blocklist) {
2309 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2310 return 0;
2311 }
2312
2313 return 1;
2314}
2315
2316static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2317{
2318 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002319 struct dasd_ccw_req *cqr, *n;
Jan Höppner8fd57522015-08-19 13:41:20 +02002320 u8 *sense = NULL;
Stefan Haberland362ce842014-10-01 13:04:54 +02002321 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002322
2323retry:
2324 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2325 device = cqr->startdev;
2326 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2327 continue;
2328
2329 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2330 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2331 cqr->status = DASD_CQR_FAILED;
2332 cqr->intrc = -EPERM;
2333 continue;
2334 }
2335 /*Non-temporary stop condition will trigger fail fast*/
2336 if (device->stopped & ~DASD_STOPPED_PENDING &&
2337 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2338 !dasd_eer_enabled(device)) {
2339 cqr->status = DASD_CQR_FAILED;
2340 cqr->intrc = -EAGAIN;
2341 continue;
2342 }
2343
2344 /*Don't try to start requests if device is stopped*/
2345 if (interruptible) {
2346 rc = wait_event_interruptible(
2347 generic_waitq, !device->stopped);
2348 if (rc == -ERESTARTSYS) {
2349 cqr->status = DASD_CQR_FAILED;
2350 cqr->intrc = rc;
2351 continue;
2352 }
2353 } else
2354 wait_event(generic_waitq, !(device->stopped));
2355
2356 if (!cqr->callback)
2357 cqr->callback = dasd_wakeup_cb;
2358 cqr->callback_data = DASD_SLEEPON_START_TAG;
2359 dasd_add_request_tail(cqr);
2360 }
2361
2362 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2363
2364 rc = 0;
2365 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002366 /*
Jan Höppner8fd57522015-08-19 13:41:20 +02002367 * In some cases the 'File Protected' or 'Incorrect Length'
2368 * error might be expected and error recovery would be
2369 * unnecessary in these cases. Check if the according suppress
2370 * bit is set.
2371 */
2372 sense = dasd_get_sense(&cqr->irb);
2373 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2374 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2375 continue;
2376 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2377 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2378 continue;
2379
2380 /*
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002381 * for alias devices simplify error recovery and
2382 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002383 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002384 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002385 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002386 (cqr->status == DASD_CQR_TERMINATED ||
2387 cqr->status == DASD_CQR_NEED_ERP))
2388 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002389
2390 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002391 if (__dasd_sleep_on_erp(cqr))
2392 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002393 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002394 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002395
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002396 return 0;
2397}
2398
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002399/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002400 * Queue a request to the tail of the device ccw_queue and wait for
2401 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002403int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002405 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002407EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002410 * Start requests from a ccw_queue and wait for their completion.
2411 */
2412int dasd_sleep_on_queue(struct list_head *ccw_queue)
2413{
2414 return _dasd_sleep_on_queue(ccw_queue, 0);
2415}
2416EXPORT_SYMBOL(dasd_sleep_on_queue);
2417
2418/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002419 * Queue a request to the tail of the device ccw_queue and wait
2420 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002422int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002424 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002426EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428/*
2429 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2430 * for eckd devices) the currently running request has to be terminated
2431 * and be put back to status queued, before the special request is added
2432 * to the head of the queue. Then the special request is waited on normally.
2433 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002434static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002437 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (list_empty(&device->ccw_queue))
2440 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002441 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002442 rc = device->discipline->term_IO(cqr);
2443 if (!rc)
2444 /*
2445 * CQR terminated because a more important request is pending.
2446 * Undo decreasing of retry counter because this is
2447 * not an error case.
2448 */
2449 cqr->retries++;
2450 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002453int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct dasd_device *device;
2456 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002457
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002458 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002459 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2460 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2461 cqr->status = DASD_CQR_FAILED;
2462 cqr->intrc = -EPERM;
2463 return -EIO;
2464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 spin_lock_irq(get_ccwdev_lock(device->cdev));
2466 rc = _dasd_term_running_cqr(device);
2467 if (rc) {
2468 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2469 return rc;
2470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002472 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002474 /*
2475 * add new request as second
2476 * first the terminated cqr needs to be finished
2477 */
2478 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002481 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2484
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002485 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002486
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002487 if (cqr->status == DASD_CQR_DONE)
2488 rc = 0;
2489 else if (cqr->intrc)
2490 rc = cqr->intrc;
2491 else
2492 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002493
2494 /* kick tasklets */
2495 dasd_schedule_device_bh(device);
2496 if (device->block)
2497 dasd_schedule_block_bh(device->block);
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return rc;
2500}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002501EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503/*
2504 * Cancels a request that was started with dasd_sleep_on_req.
2505 * This is useful to timeout requests. The request will be
2506 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002507 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002508 * negative error code if termination failed
2509 * Cancellation of a request is an asynchronous operation! The calling
2510 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002512static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002514 struct dasd_device *device = cqr->startdev;
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002515 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 switch (cqr->status) {
2518 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002519 /* request was not started - just set to cleared */
2520 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 break;
2522 case DASD_CQR_IN_IO:
2523 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002524 rc = device->discipline->term_IO(cqr);
2525 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002526 dev_err(&device->cdev->dev,
2527 "Cancelling request %p failed with rc=%d\n",
2528 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002529 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002530 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002533 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002536 dasd_schedule_device_bh(device);
2537 return rc;
2538}
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002539
2540int dasd_cancel_req(struct dasd_ccw_req *cqr)
2541{
2542 struct dasd_device *device = cqr->startdev;
2543 unsigned long flags;
2544 int rc;
2545
2546 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2547 rc = __dasd_cancel_req(cqr);
2548 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2549 return rc;
2550}
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002551
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002552/*
2553 * SECTION: Operations of the dasd_block layer.
2554 */
2555
2556/*
2557 * Timeout function for dasd_block. This is used when the block layer
2558 * is waiting for something that may not come reliably, (e.g. a state
2559 * change interrupt)
2560 */
Kees Cooke99e88a2017-10-16 14:43:17 -07002561static void dasd_block_timeout(struct timer_list *t)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002562{
2563 unsigned long flags;
2564 struct dasd_block *block;
2565
Kees Cooke99e88a2017-10-16 14:43:17 -07002566 block = from_timer(block, t, timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002567 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2568 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002569 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002570 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2571 dasd_schedule_block_bh(block);
Stefan Haberlande4433432017-05-22 10:59:11 +02002572 blk_mq_run_hw_queues(block->request_queue, true);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002573}
2574
2575/*
2576 * Setup timeout for a dasd_block in jiffies.
2577 */
2578void dasd_block_set_timer(struct dasd_block *block, int expires)
2579{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002580 if (expires == 0)
2581 del_timer(&block->timer);
2582 else
2583 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002584}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002585EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002586
2587/*
2588 * Clear timeout for a dasd_block.
2589 */
2590void dasd_block_clear_timer(struct dasd_block *block)
2591{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002592 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002593}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002594EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002595
2596/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002597 * Process finished error recovery ccw.
2598 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002599static void __dasd_process_erp(struct dasd_device *device,
2600 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002601{
2602 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002603
2604 if (cqr->status == DASD_CQR_DONE)
2605 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2606 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002607 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002608 erp_fn = device->discipline->erp_postaction(cqr);
2609 erp_fn(cqr);
2610}
2611
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002612static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2613{
2614 struct request *req;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002615 blk_status_t error = BLK_STS_OK;
Stefan Haberlande4433432017-05-22 10:59:11 +02002616 int status;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002617
2618 req = (struct request *) cqr->callback_data;
2619 dasd_profile_end(cqr->block, cqr, req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002620
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002621 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002622 if (status < 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002623 error = errno_to_blk_status(status);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002624 else if (status == 0) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002625 switch (cqr->intrc) {
2626 case -EPERM:
2627 error = BLK_STS_NEXUS;
2628 break;
2629 case -ENOLINK:
2630 error = BLK_STS_TRANSPORT;
2631 break;
2632 case -ETIMEDOUT:
2633 error = BLK_STS_TIMEOUT;
2634 break;
2635 default:
2636 error = BLK_STS_IOERR;
2637 break;
2638 }
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002639 }
Stefan Haberlande4433432017-05-22 10:59:11 +02002640
2641 /*
2642 * We need to take care for ETIMEDOUT errors here since the
2643 * complete callback does not get called in this case.
2644 * Take care of all errors here and avoid additional code to
2645 * transfer the error value to the complete callback.
2646 */
2647 if (error) {
2648 blk_mq_end_request(req, error);
2649 blk_mq_run_hw_queues(req->q, true);
2650 } else {
2651 blk_mq_complete_request(req);
2652 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002653}
2654
2655/*
2656 * Process ccw request queue.
2657 */
2658static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2659 struct list_head *final_queue)
2660{
2661 struct list_head *l, *n;
2662 struct dasd_ccw_req *cqr;
2663 dasd_erp_fn_t erp_fn;
2664 unsigned long flags;
2665 struct dasd_device *base = block->base;
2666
2667restart:
2668 /* Process request with final status. */
2669 list_for_each_safe(l, n, &block->ccw_queue) {
2670 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2671 if (cqr->status != DASD_CQR_DONE &&
2672 cqr->status != DASD_CQR_FAILED &&
2673 cqr->status != DASD_CQR_NEED_ERP &&
2674 cqr->status != DASD_CQR_TERMINATED)
2675 continue;
2676
2677 if (cqr->status == DASD_CQR_TERMINATED) {
2678 base->discipline->handle_terminated_request(cqr);
2679 goto restart;
2680 }
2681
2682 /* Process requests that may be recovered */
2683 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002684 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002685 if (IS_ERR(erp_fn(cqr)))
2686 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002687 goto restart;
2688 }
2689
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002690 /* log sense for fatal error */
2691 if (cqr->status == DASD_CQR_FAILED) {
2692 dasd_log_sense(cqr, &cqr->irb);
2693 }
2694
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002695 /* First of all call extended error reporting. */
2696 if (dasd_eer_enabled(base) &&
2697 cqr->status == DASD_CQR_FAILED) {
2698 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2699
2700 /* restart request */
2701 cqr->status = DASD_CQR_FILLED;
2702 cqr->retries = 255;
2703 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002704 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002705 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2706 flags);
2707 goto restart;
2708 }
2709
2710 /* Process finished ERP request. */
2711 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002712 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002713 goto restart;
2714 }
2715
2716 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002717 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002718 list_move_tail(&cqr->blocklist, final_queue);
2719 }
2720}
2721
2722static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2723{
2724 dasd_schedule_block_bh(cqr->block);
2725}
2726
2727static void __dasd_block_start_head(struct dasd_block *block)
2728{
2729 struct dasd_ccw_req *cqr;
2730
2731 if (list_empty(&block->ccw_queue))
2732 return;
2733 /* We allways begin with the first requests on the queue, as some
2734 * of previously started requests have to be enqueued on a
2735 * dasd_device again for error recovery.
2736 */
2737 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2738 if (cqr->status != DASD_CQR_FILLED)
2739 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002740 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2741 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2742 cqr->status = DASD_CQR_FAILED;
2743 cqr->intrc = -EPERM;
2744 dasd_schedule_block_bh(block);
2745 continue;
2746 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002747 /* Non-temporary stop condition will trigger fail fast */
2748 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2749 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2750 (!dasd_eer_enabled(block->base))) {
2751 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002752 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002753 dasd_schedule_block_bh(block);
2754 continue;
2755 }
2756 /* Don't try to start requests if device is stopped */
2757 if (block->base->stopped)
2758 return;
2759
2760 /* just a fail safe check, should not happen */
2761 if (!cqr->startdev)
2762 cqr->startdev = block->base;
2763
2764 /* make sure that the requests we submit find their way back */
2765 cqr->callback = dasd_return_cqr_cb;
2766
2767 dasd_add_request_tail(cqr);
2768 }
2769}
2770
2771/*
2772 * Central dasd_block layer routine. Takes requests from the generic
2773 * block layer request queue, creates ccw requests, enqueues them on
2774 * a dasd_device and processes ccw requests that have been returned.
2775 */
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002776static void dasd_block_tasklet(unsigned long data)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002777{
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002778 struct dasd_block *block = (struct dasd_block *) data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002779 struct list_head final_queue;
2780 struct list_head *l, *n;
2781 struct dasd_ccw_req *cqr;
Stefan Haberlande4433432017-05-22 10:59:11 +02002782 struct dasd_queue *dq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002783
2784 atomic_set(&block->tasklet_scheduled, 0);
2785 INIT_LIST_HEAD(&final_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02002786 spin_lock_irq(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002787 /* Finish off requests on ccw queue */
2788 __dasd_process_block_ccw_queue(block, &final_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02002789 spin_unlock_irq(&block->queue_lock);
2790
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002791 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002792 list_for_each_safe(l, n, &final_queue) {
2793 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
Stefan Haberlande4433432017-05-22 10:59:11 +02002794 dq = cqr->dq;
2795 spin_lock_irq(&dq->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002796 list_del_init(&cqr->blocklist);
2797 __dasd_cleanup_cqr(cqr);
Stefan Haberlande4433432017-05-22 10:59:11 +02002798 spin_unlock_irq(&dq->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002799 }
Stefan Haberlande4433432017-05-22 10:59:11 +02002800
2801 spin_lock_irq(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002802 /* Now check if the head of the ccw queue needs to be started. */
2803 __dasd_block_start_head(block);
Stefan Haberlande4433432017-05-22 10:59:11 +02002804 spin_unlock_irq(&block->queue_lock);
2805
Stefan Haberland4679e892012-06-19 17:30:12 +02002806 if (waitqueue_active(&shutdown_waitq))
2807 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002808 dasd_put_device(block->base);
2809}
2810
2811static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2812{
2813 wake_up(&dasd_flush_wq);
2814}
2815
2816/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002817 * Requeue a request back to the block request queue
2818 * only works for block requests
2819 */
2820static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2821{
2822 struct dasd_block *block = cqr->block;
2823 struct request *req;
Stefan Haberlandc5576872013-04-15 16:41:31 +02002824
2825 if (!block)
2826 return -EINVAL;
Stefan Haberlande4433432017-05-22 10:59:11 +02002827 spin_lock_irq(&cqr->dq->lock);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002828 req = (struct request *) cqr->callback_data;
Stefan Haberlande4433432017-05-22 10:59:11 +02002829 blk_mq_requeue_request(req, false);
2830 spin_unlock_irq(&cqr->dq->lock);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002831
2832 return 0;
2833}
2834
2835/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002836 * Go through all request on the dasd_block request queue, cancel them
2837 * on the respective dasd_device, and return them to the generic
2838 * block layer.
2839 */
2840static int dasd_flush_block_queue(struct dasd_block *block)
2841{
2842 struct dasd_ccw_req *cqr, *n;
2843 int rc, i;
2844 struct list_head flush_queue;
Stefan Haberlande4433432017-05-22 10:59:11 +02002845 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002846
2847 INIT_LIST_HEAD(&flush_queue);
2848 spin_lock_bh(&block->queue_lock);
2849 rc = 0;
2850restart:
2851 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2852 /* if this request currently owned by a dasd_device cancel it */
2853 if (cqr->status >= DASD_CQR_QUEUED)
2854 rc = dasd_cancel_req(cqr);
2855 if (rc < 0)
2856 break;
2857 /* Rechain request (including erp chain) so it won't be
2858 * touched by the dasd_block_tasklet anymore.
2859 * Replace the callback so we notice when the request
2860 * is returned from the dasd_device layer.
2861 */
2862 cqr->callback = _dasd_wake_block_flush_cb;
2863 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2864 list_move_tail(&cqr->blocklist, &flush_queue);
2865 if (i > 1)
2866 /* moved more than one request - need to restart */
2867 goto restart;
2868 }
2869 spin_unlock_bh(&block->queue_lock);
2870 /* Now call the callback function of flushed requests */
2871restart_cb:
2872 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2873 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2874 /* Process finished ERP request. */
2875 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002876 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002877 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002878 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002879 /* restart list_for_xx loop since dasd_process_erp
2880 * might remove multiple elements */
2881 goto restart_cb;
2882 }
2883 /* call the callback function */
Stefan Haberlande4433432017-05-22 10:59:11 +02002884 spin_lock_irqsave(&cqr->dq->lock, flags);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002885 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002886 list_del_init(&cqr->blocklist);
2887 __dasd_cleanup_cqr(cqr);
Stefan Haberlande4433432017-05-22 10:59:11 +02002888 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return rc;
2891}
2892
2893/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002894 * Schedules a call to dasd_tasklet over the device tasklet.
2895 */
2896void dasd_schedule_block_bh(struct dasd_block *block)
2897{
2898 /* Protect against rescheduling. */
2899 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2900 return;
2901 /* life cycle of block is bound to it's base device */
2902 dasd_get_device(block->base);
2903 tasklet_hi_schedule(&block->tasklet);
2904}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002905EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002906
2907
2908/*
2909 * SECTION: external block device operations
2910 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 */
2912
2913/*
2914 * Dasd request queue function. Called from ll_rw_blk.c
2915 */
Stefan Haberlande4433432017-05-22 10:59:11 +02002916static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
2917 const struct blk_mq_queue_data *qd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Stefan Haberlande4433432017-05-22 10:59:11 +02002919 struct dasd_block *block = hctx->queue->queuedata;
2920 struct dasd_queue *dq = hctx->driver_data;
2921 struct request *req = qd->rq;
2922 struct dasd_device *basedev;
2923 struct dasd_ccw_req *cqr;
2924 blk_status_t rc = BLK_STS_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Stefan Haberlande4433432017-05-22 10:59:11 +02002926 basedev = block->base;
2927 spin_lock_irq(&dq->lock);
2928 if (basedev->state < DASD_STATE_READY) {
2929 DBF_DEV_EVENT(DBF_ERR, basedev,
2930 "device not ready for request %p", req);
2931 rc = BLK_STS_IOERR;
2932 goto out;
2933 }
2934
2935 /*
2936 * if device is stopped do not fetch new requests
2937 * except failfast is active which will let requests fail
2938 * immediately in __dasd_block_start_head()
2939 */
2940 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
2941 DBF_DEV_EVENT(DBF_ERR, basedev,
2942 "device stopped request %p", req);
2943 rc = BLK_STS_RESOURCE;
2944 goto out;
2945 }
2946
2947 if (basedev->features & DASD_FEATURE_READONLY &&
2948 rq_data_dir(req) == WRITE) {
2949 DBF_DEV_EVENT(DBF_ERR, basedev,
2950 "Rejecting write request %p", req);
2951 rc = BLK_STS_IOERR;
2952 goto out;
2953 }
2954
2955 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2956 (basedev->features & DASD_FEATURE_FAILFAST ||
2957 blk_noretry_request(req))) {
2958 DBF_DEV_EVENT(DBF_ERR, basedev,
2959 "Rejecting failfast request %p", req);
2960 rc = BLK_STS_IOERR;
2961 goto out;
2962 }
2963
2964 cqr = basedev->discipline->build_cp(basedev, block, req);
2965 if (IS_ERR(cqr)) {
2966 if (PTR_ERR(cqr) == -EBUSY ||
2967 PTR_ERR(cqr) == -ENOMEM ||
2968 PTR_ERR(cqr) == -EAGAIN) {
2969 rc = BLK_STS_RESOURCE;
2970 goto out;
2971 }
2972 DBF_DEV_EVENT(DBF_ERR, basedev,
2973 "CCW creation failed (rc=%ld) on request %p",
2974 PTR_ERR(cqr), req);
2975 rc = BLK_STS_IOERR;
2976 goto out;
2977 }
2978 /*
2979 * Note: callback is set to dasd_return_cqr_cb in
2980 * __dasd_block_start_head to cover erp requests as well
2981 */
2982 cqr->callback_data = req;
2983 cqr->status = DASD_CQR_FILLED;
2984 cqr->dq = dq;
Sebastian Ottf0f59a22018-05-15 14:05:13 +02002985
Stefan Haberlande4433432017-05-22 10:59:11 +02002986 blk_mq_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002987 spin_lock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02002988 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2989 INIT_LIST_HEAD(&cqr->devlist);
2990 dasd_profile_start(block, cqr, req);
2991 dasd_schedule_block_bh(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002992 spin_unlock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02002993
2994out:
2995 spin_unlock_irq(&dq->lock);
2996 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
2999/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00003000 * Block timeout callback, called from the block layer
3001 *
Hannes Reineckea2ace462013-01-30 09:26:14 +00003002 * Return values:
3003 * BLK_EH_RESET_TIMER if the request should be left running
Christoph Hellwig66005932018-05-29 15:52:29 +02003004 * BLK_EH_DONE if the request is handled or terminated
Hannes Reineckea2ace462013-01-30 09:26:14 +00003005 * by the driver.
3006 */
Stefan Haberlande4433432017-05-22 10:59:11 +02003007enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
Hannes Reineckea2ace462013-01-30 09:26:14 +00003008{
Hannes Reineckea2ace462013-01-30 09:26:14 +00003009 struct dasd_block *block = req->q->queuedata;
3010 struct dasd_device *device;
Sebastian Ottf0f59a22018-05-15 14:05:13 +02003011 struct dasd_ccw_req *cqr;
Stefan Haberlande4433432017-05-22 10:59:11 +02003012 unsigned long flags;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003013 int rc = 0;
3014
Sebastian Ottc5205f22018-06-04 19:07:39 +02003015 cqr = blk_mq_rq_to_pdu(req);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003016 if (!cqr)
Christoph Hellwig66005932018-05-29 15:52:29 +02003017 return BLK_EH_DONE;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003018
Stefan Haberlande4433432017-05-22 10:59:11 +02003019 spin_lock_irqsave(&cqr->dq->lock, flags);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003020 device = cqr->startdev ? cqr->startdev : block->base;
Stefan Haberlande4433432017-05-22 10:59:11 +02003021 if (!device->blk_timeout) {
3022 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00003023 return BLK_EH_RESET_TIMER;
Stefan Haberlande4433432017-05-22 10:59:11 +02003024 }
Hannes Reineckea2ace462013-01-30 09:26:14 +00003025 DBF_DEV_EVENT(DBF_WARNING, device,
3026 " dasd_times_out cqr %p status %x",
3027 cqr, cqr->status);
3028
3029 spin_lock(&block->queue_lock);
3030 spin_lock(get_ccwdev_lock(device->cdev));
3031 cqr->retries = -1;
3032 cqr->intrc = -ETIMEDOUT;
3033 if (cqr->status >= DASD_CQR_QUEUED) {
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003034 rc = __dasd_cancel_req(cqr);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003035 } else if (cqr->status == DASD_CQR_FILLED ||
3036 cqr->status == DASD_CQR_NEED_ERP) {
3037 cqr->status = DASD_CQR_TERMINATED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003038 } else if (cqr->status == DASD_CQR_IN_ERP) {
3039 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3040
3041 list_for_each_entry_safe(searchcqr, nextcqr,
3042 &block->ccw_queue, blocklist) {
3043 tmpcqr = searchcqr;
3044 while (tmpcqr->refers)
3045 tmpcqr = tmpcqr->refers;
3046 if (tmpcqr != cqr)
3047 continue;
3048 /* searchcqr is an ERP request for cqr */
3049 searchcqr->retries = -1;
3050 searchcqr->intrc = -ETIMEDOUT;
3051 if (searchcqr->status >= DASD_CQR_QUEUED) {
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003052 rc = __dasd_cancel_req(searchcqr);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003053 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3054 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3055 searchcqr->status = DASD_CQR_TERMINATED;
3056 rc = 0;
3057 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3058 /*
3059 * Shouldn't happen; most recent ERP
3060 * request is at the front of queue
3061 */
3062 continue;
3063 }
3064 break;
3065 }
Hannes Reineckea2ace462013-01-30 09:26:14 +00003066 }
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003067 spin_unlock(get_ccwdev_lock(device->cdev));
Hannes Reineckea2ace462013-01-30 09:26:14 +00003068 dasd_schedule_block_bh(block);
3069 spin_unlock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02003070 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003071
Christoph Hellwig66005932018-05-29 15:52:29 +02003072 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003073}
3074
Stefan Haberlande4433432017-05-22 10:59:11 +02003075static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3076 unsigned int idx)
3077{
3078 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3079
3080 if (!dq)
3081 return -ENOMEM;
3082
3083 spin_lock_init(&dq->lock);
3084 hctx->driver_data = dq;
3085
3086 return 0;
3087}
3088
3089static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3090{
3091 kfree(hctx->driver_data);
3092 hctx->driver_data = NULL;
3093}
3094
3095static void dasd_request_done(struct request *req)
3096{
3097 blk_mq_end_request(req, 0);
3098 blk_mq_run_hw_queues(req->q, true);
3099}
3100
3101static struct blk_mq_ops dasd_mq_ops = {
3102 .queue_rq = do_dasd_request,
3103 .complete = dasd_request_done,
3104 .timeout = dasd_times_out,
3105 .init_hctx = dasd_init_hctx,
3106 .exit_hctx = dasd_exit_hctx,
3107};
3108
Hannes Reineckea2ace462013-01-30 09:26:14 +00003109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 * Allocate and initialize request queue and default I/O scheduler.
3111 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003112static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Stefan Haberlande4433432017-05-22 10:59:11 +02003114 int rc;
3115
3116 block->tag_set.ops = &dasd_mq_ops;
Sebastian Ottc5205f22018-06-04 19:07:39 +02003117 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
Stefan Haberlande4433432017-05-22 10:59:11 +02003118 block->tag_set.nr_hw_queues = DASD_NR_HW_QUEUES;
3119 block->tag_set.queue_depth = DASD_MAX_LCU_DEV * DASD_REQ_PER_DEV;
3120 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Vasily Gorbikb17e3ab2018-06-24 09:21:59 +02003121 block->tag_set.numa_node = NUMA_NO_NODE;
Stefan Haberlande4433432017-05-22 10:59:11 +02003122
3123 rc = blk_mq_alloc_tag_set(&block->tag_set);
3124 if (rc)
3125 return rc;
3126
3127 block->request_queue = blk_mq_init_queue(&block->tag_set);
3128 if (IS_ERR(block->request_queue))
3129 return PTR_ERR(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003131 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01003133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
3136/*
3137 * Allocate and initialize request queue.
3138 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003139static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Jan Höppner28b841b2016-06-30 13:28:57 +02003141 unsigned int logical_block_size = block->bp_block;
Jan Höppner62ba6f82016-06-30 13:18:16 +02003142 struct request_queue *q = block->request_queue;
Jan Höppner28b841b2016-06-30 13:28:57 +02003143 unsigned int max_bytes, max_discard_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 int max;
3145
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003146 if (block->base->features & DASD_FEATURE_USERAW) {
3147 /*
3148 * the max_blocks value for raw_track access is 256
3149 * it is higher than the native ECKD value because we
3150 * only need one ccw per track
3151 * so the max_hw_sectors are
3152 * 2048 x 512B = 1024kB = 16 tracks
3153 */
3154 max = 2048;
3155 } else {
3156 max = block->base->discipline->max_blocks << block->s2b_shift;
3157 }
Bart Van Assche8b904b52018-03-07 17:10:10 -08003158 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Jan Höppner62ba6f82016-06-30 13:18:16 +02003159 q->limits.max_dev_sectors = max;
Jan Höppner28b841b2016-06-30 13:28:57 +02003160 blk_queue_logical_block_size(q, logical_block_size);
Jan Höppner62ba6f82016-06-30 13:18:16 +02003161 blk_queue_max_hw_sectors(q, max);
3162 blk_queue_max_segments(q, USHRT_MAX);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003163 /* with page sized segments we can translate each segement into
3164 * one idaw/tidaw
3165 */
Jan Höppner62ba6f82016-06-30 13:18:16 +02003166 blk_queue_max_segment_size(q, PAGE_SIZE);
3167 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
Jan Höppner28b841b2016-06-30 13:28:57 +02003168
3169 /* Only activate blocklayer discard support for devices that support it */
3170 if (block->base->features & DASD_FEATURE_DISCARD) {
3171 q->limits.discard_granularity = logical_block_size;
3172 q->limits.discard_alignment = PAGE_SIZE;
3173
3174 /* Calculate max_discard_sectors and make it PAGE aligned */
3175 max_bytes = USHRT_MAX * logical_block_size;
3176 max_bytes = ALIGN(max_bytes, PAGE_SIZE) - PAGE_SIZE;
3177 max_discard_sectors = max_bytes / logical_block_size;
3178
3179 blk_queue_max_discard_sectors(q, max_discard_sectors);
3180 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
Bart Van Assche8b904b52018-03-07 17:10:10 -08003181 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Jan Höppner28b841b2016-06-30 13:28:57 +02003182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
3185/*
3186 * Deactivate and free request queue.
3187 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003188static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003190 if (block->request_queue) {
3191 blk_cleanup_queue(block->request_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02003192 blk_mq_free_tag_set(&block->tag_set);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003193 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195}
3196
Al Viro57a7c0b2008-03-02 10:36:08 -05003197static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003199 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 int rc;
3201
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003202 base = dasd_device_from_gendisk(bdev->bd_disk);
3203 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003204 return -ENODEV;
3205
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003206 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003207 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 rc = -ENODEV;
3209 goto unlock;
3210 }
3211
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003212 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 rc = -EINVAL;
3214 goto unlock;
3215 }
3216
3217 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003218 dev_info(&base->cdev->dev,
3219 "Accessing the DASD failed because it is in "
3220 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 rc = -EPERM;
3222 goto out;
3223 }
3224
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003225 if (base->state <= DASD_STATE_BASIC) {
3226 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 " Cannot open unrecognized device");
3228 rc = -ENODEV;
3229 goto out;
3230 }
3231
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003232 if ((mode & FMODE_WRITE) &&
3233 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3234 (base->features & DASD_FEATURE_READONLY))) {
3235 rc = -EROFS;
3236 goto out;
3237 }
3238
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003239 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241
3242out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003243 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003245 atomic_dec(&base->block->open_count);
3246 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return rc;
3248}
3249
Al Virodb2a1442013-05-05 21:52:57 -04003250static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Al Virodb2a1442013-05-05 21:52:57 -04003252 struct dasd_device *base = dasd_device_from_gendisk(disk);
3253 if (base) {
3254 atomic_dec(&base->block->open_count);
3255 module_put(base->discipline->owner);
3256 dasd_put_device(base);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003260/*
3261 * Return disk geometry.
3262 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003263static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003264{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003265 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003266
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003267 base = dasd_device_from_gendisk(bdev->bd_disk);
3268 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003269 return -ENODEV;
3270
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003271 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003272 !base->discipline->fill_geometry) {
3273 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003274 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003275 }
3276 base->discipline->fill_geometry(base->block, geo);
3277 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3278 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003279 return 0;
3280}
3281
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003282const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283dasd_device_operations = {
3284 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003285 .open = dasd_open,
3286 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003287 .ioctl = dasd_ioctl,
3288 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003289 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290};
3291
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003292/*******************************************************************************
3293 * end of block device operations
3294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296static void
3297dasd_exit(void)
3298{
3299#ifdef CONFIG_PROC_FS
3300 dasd_proc_exit();
3301#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003302 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003303 if (dasd_page_cache != NULL) {
3304 kmem_cache_destroy(dasd_page_cache);
3305 dasd_page_cache = NULL;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 dasd_gendisk_exit();
3308 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (dasd_debug_area != NULL) {
3310 debug_unregister(dasd_debug_area);
3311 dasd_debug_area = NULL;
3312 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003313 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
3316/*
3317 * SECTION: common functions for ccw_driver use
3318 */
3319
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003320/*
3321 * Is the device read-only?
3322 * Note that this function does not report the setting of the
3323 * readonly device attribute, but how it is configured in z/VM.
3324 */
3325int dasd_device_is_ro(struct dasd_device *device)
3326{
3327 struct ccw_dev_id dev_id;
3328 struct diag210 diag_data;
3329 int rc;
3330
3331 if (!MACHINE_IS_VM)
3332 return 0;
3333 ccw_device_get_id(device->cdev, &dev_id);
3334 memset(&diag_data, 0, sizeof(diag_data));
3335 diag_data.vrdcdvno = dev_id.devno;
3336 diag_data.vrdclen = sizeof(diag_data);
3337 rc = diag210(&diag_data);
3338 if (rc == 0 || rc == 2) {
3339 return diag_data.vrdcvfla & 0x80;
3340 } else {
3341 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3342 dev_id.devno, rc);
3343 return 0;
3344 }
3345}
3346EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3347
Cornelia Huckf3445a12009-04-14 15:36:23 +02003348static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3349{
3350 struct ccw_device *cdev = data;
3351 int ret;
3352
3353 ret = ccw_device_set_online(cdev);
3354 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003355 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3356 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003357}
3358
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003359/*
3360 * Initial attempt at a probe function. this can be simplified once
3361 * the other detection code is gone.
3362 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003363int dasd_generic_probe(struct ccw_device *cdev,
3364 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 int ret;
3367
3368 ret = dasd_add_sysfs_files(cdev);
3369 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003370 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3371 "dasd_generic_probe: could not add "
3372 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Horst Hummel40545572006-06-29 15:08:18 +02003375 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Horst Hummel40545572006-06-29 15:08:18 +02003377 /*
3378 * Automatically online either all dasd devices (dasd_autodetect)
3379 * or all devices specified with dasd= parameters during
3380 * initial probe.
3381 */
3382 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003383 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003384 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003387EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Stefan Haberlandc020d722016-09-20 10:42:38 +02003389void dasd_generic_free_discipline(struct dasd_device *device)
3390{
3391 /* Forget the discipline information. */
3392 if (device->discipline) {
3393 if (device->discipline->uncheck_device)
3394 device->discipline->uncheck_device(device);
3395 module_put(device->discipline->owner);
3396 device->discipline = NULL;
3397 }
3398 if (device->base_discipline) {
3399 module_put(device->base_discipline->owner);
3400 device->base_discipline = NULL;
3401 }
3402}
3403EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3404
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003405/*
3406 * This will one day be called from a global not_oper handler.
3407 * It is also used by driver_unregister during module unload.
3408 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003409void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003412 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Horst Hummel59afda72005-05-16 21:53:39 -07003414 cdev->handler = NULL;
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003417 if (IS_ERR(device)) {
3418 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003420 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003421 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3422 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* Already doing offline processing */
3424 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003425 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return;
3427 }
3428 /*
3429 * This device is removed unconditionally. Set offline
3430 * flag to prevent dasd_open from opening it while it is
3431 * no quite down yet.
3432 */
3433 dasd_set_target_state(device, DASD_STATE_NEW);
3434 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003435 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003437 /*
3438 * life cycle of block is bound to device, so delete it after
3439 * device was safely removed
3440 */
3441 if (block)
3442 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003443
3444 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003446EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003448/*
3449 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003451 * or the user has started activation through sysfs.
3452 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003453int dasd_generic_set_online(struct ccw_device *cdev,
3454 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003456 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003458 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003459
Horst Hummel40545572006-06-29 15:08:18 +02003460 /* first online clears initial online feature flag */
3461 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 device = dasd_create_device(cdev);
3463 if (IS_ERR(device))
3464 return PTR_ERR(device);
3465
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003466 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003467 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003469 /* Try to load the required module. */
3470 rc = request_module(DASD_DIAG_MOD);
3471 if (rc) {
3472 pr_warn("%s Setting the DASD online failed "
3473 "because the required module %s "
3474 "could not be loaded (rc=%d)\n",
3475 dev_name(&cdev->dev), DASD_DIAG_MOD,
3476 rc);
3477 dasd_delete_device(device);
3478 return -ENODEV;
3479 }
3480 }
3481 /* Module init could have failed, so check again here after
3482 * request_module(). */
3483 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003484 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3485 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 dasd_delete_device(device);
3487 return -ENODEV;
3488 }
3489 discipline = dasd_diag_discipline_pointer;
3490 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003491 if (!try_module_get(base_discipline->owner)) {
3492 dasd_delete_device(device);
3493 return -EINVAL;
3494 }
3495 if (!try_module_get(discipline->owner)) {
3496 module_put(base_discipline->owner);
3497 dasd_delete_device(device);
3498 return -EINVAL;
3499 }
3500 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 device->discipline = discipline;
3502
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003503 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 rc = discipline->check_device(device);
3505 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003506 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3507 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003508 module_put(discipline->owner);
3509 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 dasd_delete_device(device);
3511 return rc;
3512 }
3513
3514 dasd_set_target_state(device, DASD_STATE_ONLINE);
3515 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003516 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3517 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 rc = -ENODEV;
3519 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003520 if (device->block)
3521 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 dasd_delete_device(device);
3523 } else
3524 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003525 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003526
3527 wait_event(dasd_init_waitq, _wait_for_device(device));
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return rc;
3531}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003532EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003534int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003537 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003538 int max_count, open_count, rc;
Jan Höppner0f57c972016-10-18 17:54:49 +02003539 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003541 rc = 0;
Jan Höppner0f57c972016-10-18 17:54:49 +02003542 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3543 device = dasd_device_from_cdev_locked(cdev);
3544 if (IS_ERR(device)) {
3545 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return PTR_ERR(device);
Jan Höppner0f57c972016-10-18 17:54:49 +02003547 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 /*
3550 * We must make sure that this device is currently not in use.
3551 * The open_count is increased for every opener, that includes
3552 * the blkdev_get in dasd_scan_partitions. We are only interested
3553 * in the other openers.
3554 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003555 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003556 max_count = device->block->bdev ? 0 : -1;
3557 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003558 if (open_count > max_count) {
3559 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003560 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3561 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003562 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003563 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3564 dev_name(&cdev->dev));
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003565 rc = -EBUSY;
3566 goto out_err;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003569
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003570 /*
3571 * Test if the offline processing is already running and exit if so.
3572 * If a safe offline is being processed this could only be a normal
3573 * offline that should be able to overtake the safe offline and
3574 * cancel any I/O we do not want to wait for any longer
3575 */
3576 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3577 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3578 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3579 &device->flags);
3580 } else {
3581 rc = -EBUSY;
3582 goto out_err;
3583 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003584 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003585 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003586
3587 /*
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003588 * if safe_offline is called set safe_offline_running flag and
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003589 * clear safe_offline so that a call to normal offline
3590 * can overrun safe_offline processing
3591 */
3592 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3593 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003594 /* need to unlock here to wait for outstanding I/O */
3595 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003596 /*
3597 * If we want to set the device safe offline all IO operations
3598 * should be finished before continuing the offline process
3599 * so sync bdev first and then wait for our queues to become
3600 * empty
3601 */
Stefan Haberlandca732e12017-01-25 16:56:41 +01003602 if (device->block) {
3603 rc = fsync_bdev(device->block->bdev);
3604 if (rc != 0)
3605 goto interrupted;
3606 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003607 dasd_schedule_device_bh(device);
3608 rc = wait_event_interruptible(shutdown_waitq,
3609 _wait_for_empty_queues(device));
3610 if (rc != 0)
3611 goto interrupted;
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003612
3613 /*
3614 * check if a normal offline process overtook the offline
3615 * processing in this case simply do nothing beside returning
3616 * that we got interrupted
3617 * otherwise mark safe offline as not running any longer and
3618 * continue with normal offline
3619 */
3620 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3621 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3622 rc = -ERESTARTSYS;
3623 goto out_err;
3624 }
3625 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003626 }
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003627 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 dasd_set_target_state(device, DASD_STATE_NEW);
3630 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003631 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003633 /*
3634 * life cycle of block is bound to device, so delete it after
3635 * device was safely removed
3636 */
3637 if (block)
3638 dasd_free_block(block);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003641
3642interrupted:
3643 /* interrupted by signal */
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003644 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003645 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3646 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003647out_err:
Jan Höppner0f57c972016-10-18 17:54:49 +02003648 dasd_put_device(device);
3649 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003650 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003652EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003654int dasd_generic_last_path_gone(struct dasd_device *device)
3655{
3656 struct dasd_ccw_req *cqr;
3657
3658 dev_warn(&device->cdev->dev, "No operational channel path is left "
3659 "for the device\n");
3660 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3661 /* First of all call extended error reporting. */
3662 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3663
3664 if (device->state < DASD_STATE_BASIC)
3665 return 0;
3666 /* Device is active. We want to keep it. */
3667 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3668 if ((cqr->status == DASD_CQR_IN_IO) ||
3669 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3670 cqr->status = DASD_CQR_QUEUED;
3671 cqr->retries++;
3672 }
3673 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3674 dasd_device_clear_timer(device);
3675 dasd_schedule_device_bh(device);
3676 return 1;
3677}
3678EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3679
3680int dasd_generic_path_operational(struct dasd_device *device)
3681{
3682 dev_info(&device->cdev->dev, "A channel path to the device has become "
3683 "operational\n");
3684 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3685 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3686 if (device->stopped & DASD_UNRESUMED_PM) {
3687 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3688 dasd_restore_device(device);
3689 return 1;
3690 }
3691 dasd_schedule_device_bh(device);
Stefan Haberlande4433432017-05-22 10:59:11 +02003692 if (device->block) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003693 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02003694 if (device->block->request_queue)
3695 blk_mq_run_hw_queues(device->block->request_queue,
3696 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02003697 }
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003698
3699 if (!device->stopped)
3700 wake_up(&generic_waitq);
3701
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003702 return 1;
3703}
3704EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3705
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003706int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
3708 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 int ret;
3710
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003711 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (IS_ERR(device))
3713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 ret = 0;
3715 switch (event) {
3716 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003717 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 case CIO_NO_PATH:
Stefan Haberlandc9346152016-08-08 15:53:54 +02003719 dasd_path_no_path(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003720 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 break;
3722 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 ret = 1;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003724 if (dasd_path_get_opm(device))
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003725 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 break;
3727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 dasd_put_device(device);
3729 return ret;
3730}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003731EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003733void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3734{
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003735 struct dasd_device *device;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003736 int chp, oldopm, hpfpm, ifccpm;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003737
3738 device = dasd_device_from_cdev_locked(cdev);
3739 if (IS_ERR(device))
3740 return;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003741
3742 oldopm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003743 for (chp = 0; chp < 8; chp++) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003744 if (path_event[chp] & PE_PATH_GONE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003745 dasd_path_notoper(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003746 }
3747 if (path_event[chp] & PE_PATH_AVAILABLE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003748 dasd_path_available(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003749 dasd_schedule_device_bh(device);
3750 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003751 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003752 if (!dasd_path_is_operational(device, chp) &&
3753 !dasd_path_need_verify(device, chp)) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003754 /*
3755 * we can not establish a pathgroup on an
3756 * unavailable path, so trigger a path
3757 * verification first
3758 */
Stefan Haberlandc9346152016-08-08 15:53:54 +02003759 dasd_path_available(device, chp);
3760 dasd_schedule_device_bh(device);
Stefan Haberland12d7b102012-09-11 15:10:58 +02003761 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003762 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3763 "Pathgroup re-established\n");
3764 if (device->discipline->kick_validate)
3765 device->discipline->kick_validate(device);
3766 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003767 }
Stefan Haberlanda521b042016-08-08 15:56:54 +02003768 hpfpm = dasd_path_get_hpfpm(device);
3769 ifccpm = dasd_path_get_ifccpm(device);
3770 if (!dasd_path_get_opm(device) && hpfpm) {
3771 /*
3772 * device has no operational paths but at least one path is
3773 * disabled due to HPF errors
3774 * disable HPF at all and use the path(s) again
3775 */
3776 if (device->discipline->disable_hpf)
3777 device->discipline->disable_hpf(device);
3778 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3779 dasd_path_set_tbvpm(device, hpfpm);
3780 dasd_schedule_device_bh(device);
3781 dasd_schedule_requeue(device);
3782 } else if (!dasd_path_get_opm(device) && ifccpm) {
3783 /*
3784 * device has no operational paths but at least one path is
3785 * disabled due to IFCC errors
3786 * trigger path verification on paths with IFCC errors
3787 */
3788 dasd_path_set_tbvpm(device, ifccpm);
3789 dasd_schedule_device_bh(device);
3790 }
3791 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003792 dev_warn(&device->cdev->dev,
3793 "No verified channel paths remain for the device\n");
3794 DBF_DEV_EVENT(DBF_WARNING, device,
3795 "%s", "last verified path gone");
3796 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3797 dasd_device_set_stop_bits(device,
3798 DASD_STOPPED_DC_WAIT);
3799 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003800 dasd_put_device(device);
3801}
3802EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3803
3804int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3805{
Stefan Haberlandc9346152016-08-08 15:53:54 +02003806 if (!dasd_path_get_opm(device) && lpm) {
3807 dasd_path_set_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003808 dasd_generic_path_operational(device);
3809 } else
Stefan Haberlandc9346152016-08-08 15:53:54 +02003810 dasd_path_add_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003811 return 0;
3812}
3813EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3814
Stefan Haberlanda521b042016-08-08 15:56:54 +02003815/*
3816 * clear active requests and requeue them to block layer if possible
3817 */
3818static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3819{
3820 struct list_head requeue_queue;
3821 struct dasd_ccw_req *cqr, *n;
3822 struct dasd_ccw_req *refers;
3823 int rc;
3824
3825 INIT_LIST_HEAD(&requeue_queue);
3826 spin_lock_irq(get_ccwdev_lock(device->cdev));
3827 rc = 0;
3828 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3829 /* Check status and move request to flush_queue */
3830 if (cqr->status == DASD_CQR_IN_IO) {
3831 rc = device->discipline->term_IO(cqr);
3832 if (rc) {
3833 /* unable to terminate requeust */
3834 dev_err(&device->cdev->dev,
3835 "Unable to terminate request %p "
3836 "on suspend\n", cqr);
3837 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3838 dasd_put_device(device);
3839 return rc;
3840 }
3841 }
3842 list_move_tail(&cqr->devlist, &requeue_queue);
3843 }
3844 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3845
3846 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3847 wait_event(dasd_flush_wq,
3848 (cqr->status != DASD_CQR_CLEAR_PENDING));
3849
Stefan Haberland9487cfd2018-02-07 17:39:14 +01003850 /*
3851 * requeue requests to blocklayer will only work
3852 * for block device requests
3853 */
3854 if (_dasd_requeue_request(cqr))
3855 continue;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003856
3857 /* remove requests from device and block queue */
3858 list_del_init(&cqr->devlist);
3859 while (cqr->refers != NULL) {
3860 refers = cqr->refers;
3861 /* remove the request from the block queue */
3862 list_del(&cqr->blocklist);
3863 /* free the finished erp request */
3864 dasd_free_erp_request(cqr, cqr->memdev);
3865 cqr = refers;
3866 }
3867
Stefan Haberlanda1fc8182018-02-19 12:24:39 +01003868 /*
3869 * _dasd_requeue_request already checked for a valid
3870 * blockdevice, no need to check again
3871 * all erp requests (cqr->refers) have a cqr->block
3872 * pointer copy from the original cqr
3873 */
3874 list_del_init(&cqr->blocklist);
Stefan Haberlanda521b042016-08-08 15:56:54 +02003875 cqr->block->base->discipline->free_cp(
3876 cqr, (struct request *) cqr->callback_data);
3877 }
3878
3879 /*
3880 * if requests remain then they are internal request
3881 * and go back to the device queue
3882 */
3883 if (!list_empty(&requeue_queue)) {
3884 /* move freeze_queue to start of the ccw_queue */
3885 spin_lock_irq(get_ccwdev_lock(device->cdev));
3886 list_splice_tail(&requeue_queue, &device->ccw_queue);
3887 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3888 }
Stefan Haberland9487cfd2018-02-07 17:39:14 +01003889 dasd_schedule_device_bh(device);
Stefan Haberlanda521b042016-08-08 15:56:54 +02003890 return rc;
3891}
3892
3893static void do_requeue_requests(struct work_struct *work)
3894{
3895 struct dasd_device *device = container_of(work, struct dasd_device,
3896 requeue_requests);
3897 dasd_generic_requeue_all_requests(device);
3898 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
3899 if (device->block)
3900 dasd_schedule_block_bh(device->block);
3901 dasd_put_device(device);
3902}
3903
3904void dasd_schedule_requeue(struct dasd_device *device)
3905{
3906 dasd_get_device(device);
3907 /* queue call to dasd_reload_device to the kernel event daemon. */
3908 if (!schedule_work(&device->requeue_requests))
3909 dasd_put_device(device);
3910}
3911EXPORT_SYMBOL(dasd_schedule_requeue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003912
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003913int dasd_generic_pm_freeze(struct ccw_device *cdev)
3914{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003915 struct dasd_device *device = dasd_device_from_cdev(cdev);
3916
3917 if (IS_ERR(device))
3918 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003919
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003920 /* mark device as suspended */
3921 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3922
Stefan Haberland6f272b92011-01-05 12:48:05 +01003923 if (device->discipline->freeze)
Sebastian Ott4bca6982017-06-26 19:26:55 +02003924 device->discipline->freeze(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003925
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003926 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003927 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003928
Stefan Haberlanda521b042016-08-08 15:56:54 +02003929 return dasd_generic_requeue_all_requests(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003930}
3931EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3932
3933int dasd_generic_restore_device(struct ccw_device *cdev)
3934{
3935 struct dasd_device *device = dasd_device_from_cdev(cdev);
3936 int rc = 0;
3937
3938 if (IS_ERR(device))
3939 return PTR_ERR(device);
3940
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003941 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003942 dasd_device_remove_stop_bits(device,
3943 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003944
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003945 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003946
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003947 /*
3948 * call discipline restore function
3949 * if device is stopped do nothing e.g. for disconnected devices
3950 */
3951 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003952 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003953 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003954 /*
3955 * if the resume failed for the DASD we put it in
3956 * an UNRESUMED stop state
3957 */
3958 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003959
Stefan Haberlande4433432017-05-22 10:59:11 +02003960 if (device->block) {
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003961 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02003962 if (device->block->request_queue)
3963 blk_mq_run_hw_queues(device->block->request_queue,
3964 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02003965 }
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003966
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003967 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003968 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003969 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003970}
3971EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3972
Heiko Carstens763968e2007-05-10 15:45:46 +02003973static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3974 void *rdc_buffer,
3975 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003976 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003977{
3978 struct dasd_ccw_req *cqr;
3979 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003980 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003981
Sebastian Ottc5205f22018-06-04 19:07:39 +02003982 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
3983 NULL);
Cornelia Huck17283b52007-05-04 18:47:51 +02003984
3985 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003986 /* internal error 13 - Allocating the RDC request failed*/
3987 dev_err(&device->cdev->dev,
3988 "An error occurred in the DASD device driver, "
3989 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003990 return cqr;
3991 }
3992
3993 ccw = cqr->cpaddr;
3994 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003995 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3996 idaw = (unsigned long *) (cqr->data);
3997 ccw->cda = (__u32)(addr_t) idaw;
3998 ccw->flags = CCW_FLAG_IDA;
3999 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
4000 } else {
4001 ccw->cda = (__u32)(addr_t) rdc_buffer;
4002 ccw->flags = 0;
4003 }
Cornelia Huck17283b52007-05-04 18:47:51 +02004004
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02004005 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004006 cqr->startdev = device;
4007 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02004008 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004009 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01004010 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02004011 cqr->status = DASD_CQR_FILLED;
4012 return cqr;
4013}
4014
4015
Stefan Haberland68b781f2009-09-11 10:28:29 +02004016int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02004017 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02004018{
4019 int ret;
4020 struct dasd_ccw_req *cqr;
4021
Sebastian Ott92636b12009-06-12 10:26:37 +02004022 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02004023 magic);
4024 if (IS_ERR(cqr))
4025 return PTR_ERR(cqr);
4026
4027 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004028 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02004029 return ret;
4030}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02004031EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004032
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01004033/*
4034 * In command mode and transport mode we need to look for sense
4035 * data in different places. The sense data itself is allways
4036 * an array of 32 bytes, so we can unify the sense data access
4037 * for both modes.
4038 */
4039char *dasd_get_sense(struct irb *irb)
4040{
4041 struct tsb *tsb = NULL;
4042 char *sense = NULL;
4043
4044 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4045 if (irb->scsw.tm.tcw)
4046 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4047 irb->scsw.tm.tcw);
4048 if (tsb && tsb->length == 64 && tsb->flags)
4049 switch (tsb->flags & 0x07) {
4050 case 1: /* tsa_iostat */
4051 sense = tsb->tsa.iostat.sense;
4052 break;
4053 case 2: /* tsa_ddpc */
4054 sense = tsb->tsa.ddpc.sense;
4055 break;
4056 default:
4057 /* currently we don't use interrogate data */
4058 break;
4059 }
4060 } else if (irb->esw.esw0.erw.cons) {
4061 sense = irb->ecw;
4062 }
4063 return sense;
4064}
4065EXPORT_SYMBOL_GPL(dasd_get_sense);
4066
Stefan Haberland4679e892012-06-19 17:30:12 +02004067void dasd_generic_shutdown(struct ccw_device *cdev)
4068{
4069 struct dasd_device *device;
4070
4071 device = dasd_device_from_cdev(cdev);
4072 if (IS_ERR(device))
4073 return;
4074
4075 if (device->block)
4076 dasd_schedule_block_bh(device->block);
4077
4078 dasd_schedule_device_bh(device);
4079
4080 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4081}
4082EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4083
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004084static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
4086 int rc;
4087
4088 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02004089 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02004090 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02004091 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
4093 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01004094 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (dasd_debug_area == NULL) {
4096 rc = -ENOMEM;
4097 goto failed;
4098 }
4099 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02004100 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4103
4104 dasd_diag_discipline_pointer = NULL;
4105
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02004106 dasd_statistics_createroot();
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 rc = dasd_devmap_init();
4109 if (rc)
4110 goto failed;
4111 rc = dasd_gendisk_init();
4112 if (rc)
4113 goto failed;
4114 rc = dasd_parse();
4115 if (rc)
4116 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004117 rc = dasd_eer_init();
4118 if (rc)
4119 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120#ifdef CONFIG_PROC_FS
4121 rc = dasd_proc_init();
4122 if (rc)
4123 goto failed;
4124#endif
4125
4126 return 0;
4127failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004128 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 dasd_exit();
4130 return rc;
4131}
4132
4133module_init(dasd_init);
4134module_exit(dasd_exit);