blob: 6cca72782af6a004c02734f2d7d9b21c34770167 [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
Stefan Haberland3284da32018-06-12 16:47:10 +020044static unsigned int queue_depth = 32;
45static unsigned int nr_hw_queues = 4;
46
47module_param(queue_depth, uint, 0444);
48MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
49
50module_param(nr_hw_queues, uint, 0444);
51MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * SECTION: exported variables of dasd.c
55 */
56debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020057EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020058static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020060EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010061void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
64MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020065 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067MODULE_LICENSE("GPL");
68
69/*
70 * SECTION: prototypes for static functions of dasd.c
71 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010072static int dasd_alloc_queue(struct dasd_block *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073static void dasd_free_queue(struct dasd_block *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010074static int dasd_flush_block_queue(struct dasd_block *);
Sebastian Ottd2f2df62018-07-03 13:06:45 +020075static void dasd_device_tasklet(unsigned long);
76static void dasd_block_tasklet(unsigned long);
Al Viro4927b3f2006-12-06 19:18:20 +000077static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020078static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020079static void do_reload_device(struct work_struct *);
Stefan Haberlanda521b042016-08-08 15:56:54 +020080static void do_requeue_requests(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010081static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Kees Cooke99e88a2017-10-16 14:43:17 -070082static void dasd_device_timeout(struct timer_list *);
83static void dasd_block_timeout(struct timer_list *);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010084static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020085static void dasd_profile_init(struct dasd_profile *, struct dentry *);
86static void dasd_profile_exit(struct dasd_profile *);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +010087static void dasd_hosts_init(struct dentry *, struct dasd_device *);
88static void dasd_hosts_exit(struct dasd_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * SECTION: Operations on the device structure.
92 */
93static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020094static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020095static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020096static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * Allocate memory for a new device structure.
100 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100101struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct dasd_device *device;
104
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100105 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
106 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* Get two pages for normal block device operations. */
110 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100111 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 kfree(device);
113 return ERR_PTR(-ENOMEM);
114 }
115 /* Get one page for error recovery. */
116 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100117 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 free_pages((unsigned long) device->ccw_mem, 1);
119 kfree(device);
120 return ERR_PTR(-ENOMEM);
121 }
Jan Höppner5e2b17e2018-04-27 16:51:22 +0200122 /* Get two pages for ese format. */
123 device->ese_mem = (void *)__get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
124 if (!device->ese_mem) {
125 free_page((unsigned long) device->erp_mem);
126 free_pages((unsigned long) device->ccw_mem, 1);
127 kfree(device);
128 return ERR_PTR(-ENOMEM);
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
132 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
Jan Höppner5e2b17e2018-04-27 16:51:22 +0200133 dasd_init_chunklist(&device->ese_chunks, device->ese_mem, PAGE_SIZE * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100135 atomic_set(&device->tasklet_scheduled, 0);
Sebastian Ottd2f2df62018-07-03 13:06:45 +0200136 tasklet_init(&device->tasklet, dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 (unsigned long) device);
138 INIT_LIST_HEAD(&device->ccw_queue);
Kees Cooke99e88a2017-10-16 14:43:17 -0700139 timer_setup(&device->timer, dasd_device_timeout, 0);
Al Viro4927b3f2006-12-06 19:18:20 +0000140 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200141 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200142 INIT_WORK(&device->reload_device, do_reload_device);
Stefan Haberlanda521b042016-08-08 15:56:54 +0200143 INIT_WORK(&device->requeue_requests, do_requeue_requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 device->state = DASD_STATE_NEW;
145 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100146 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200147 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return device;
149}
150
151/*
152 * Free memory of a device structure.
153 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100154void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800156 kfree(device->private);
Jan Höppner5e2b17e2018-04-27 16:51:22 +0200157 free_pages((unsigned long) device->ese_mem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 free_page((unsigned long) device->erp_mem);
159 free_pages((unsigned long) device->ccw_mem, 1);
160 kfree(device);
161}
162
163/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100164 * Allocate memory for a new device structure.
165 */
166struct dasd_block *dasd_alloc_block(void)
167{
168 struct dasd_block *block;
169
170 block = kzalloc(sizeof(*block), GFP_ATOMIC);
171 if (!block)
172 return ERR_PTR(-ENOMEM);
173 /* open_count = 0 means device online but not in use */
174 atomic_set(&block->open_count, -1);
175
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176 atomic_set(&block->tasklet_scheduled, 0);
Sebastian Ottd2f2df62018-07-03 13:06:45 +0200177 tasklet_init(&block->tasklet, dasd_block_tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100178 (unsigned long) block);
179 INIT_LIST_HEAD(&block->ccw_queue);
180 spin_lock_init(&block->queue_lock);
Kees Cooke99e88a2017-10-16 14:43:17 -0700181 timer_setup(&block->timer, dasd_block_timeout, 0);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200182 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100183
184 return block;
185}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200186EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100187
188/*
189 * Free memory of a device structure.
190 */
191void dasd_free_block(struct dasd_block *block)
192{
193 kfree(block);
194}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200195EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100196
197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * Make a new device known to the system.
199 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100200static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int rc;
203
204 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200205 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * keep the reference count > 0.
207 */
208 dasd_get_device(device);
209
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100210 if (device->block) {
211 rc = dasd_alloc_queue(device->block);
212 if (rc) {
213 dasd_put_device(device);
214 return rc;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->state = DASD_STATE_KNOWN;
218 return 0;
219}
220
221/*
222 * Let the system forget about a device.
223 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100224static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800226 /* Disable extended error reporting for this device. */
227 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 device->state = DASD_STATE_NEW;
229
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100230 if (device->block)
231 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* Give up reference we took in dasd_state_new_to_known. */
234 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200238static struct dentry *dasd_debugfs_setup(const char *name,
239 struct dentry *base_dentry)
240{
241 struct dentry *pde;
242
243 if (!base_dentry)
244 return NULL;
245 pde = debugfs_create_dir(name, base_dentry);
246 if (!pde || IS_ERR(pde))
247 return NULL;
248 return pde;
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Request the irq line for the device.
253 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100254static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200256 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200257 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200260 if (block) {
261 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100262 if (rc)
263 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200264 block->debugfs_dentry =
265 dasd_debugfs_setup(block->gdp->disk_name,
266 dasd_debugfs_root_entry);
267 dasd_profile_init(&block->profile, block->debugfs_dentry);
268 if (dasd_global_profile_level == DASD_PROFILE_ON)
269 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100270 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200271 device->debugfs_dentry =
272 dasd_debugfs_setup(dev_name(&device->cdev->dev),
273 dasd_debugfs_root_entry);
274 dasd_profile_init(&device->profile, device->debugfs_dentry);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100275 dasd_hosts_init(device->debugfs_dentry, device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100278 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200281 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
283
284 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200285
286 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * Release the irq line for the device. Terminate any running i/o.
291 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100292static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Horst Hummel8f617012006-08-30 14:33:33 +0200294 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200295
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200296 if (device->discipline->basic_to_known) {
297 rc = device->discipline->basic_to_known(device);
298 if (rc)
299 return rc;
300 }
301
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100302 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200303 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200304 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100305 dasd_gendisk_free(device->block);
306 dasd_block_clear_timer(device->block);
307 }
308 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200309 if (rc)
310 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100311 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200312 dasd_profile_exit(&device->profile);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100313 dasd_hosts_exit(device);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200314 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
316 if (device->debug_area != NULL) {
317 debug_unregister(device->debug_area);
318 device->debug_area = NULL;
319 }
320 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * Do the initial analysis. The do_analysis function may return
326 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
327 * until the discipline decides to continue the startup sequence
328 * by calling the function dasd_change_state. The eckd disciplines
329 * uses this to start a ccw that detects the format. The completion
330 * interrupt for this detection ccw uses the kernel event daemon to
331 * trigger the call to dasd_change_state. All this is done in the
332 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800333 * After the analysis ccw is done (do_analysis returned 0) the block
334 * device is setup.
335 * In case the analysis returns an error, the device setup is stopped
336 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100338static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100341 struct dasd_block *block;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200342 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800346 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100347 if (block) {
348 if (block->base->discipline->do_analysis != NULL)
349 rc = block->base->discipline->do_analysis(block);
350 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200351 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100352 device->state = DASD_STATE_UNFMT;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200353 disk = device->block->gdp;
354 kobject_uevent(&disk_to_dev(disk)->kobj,
355 KOBJ_CHANGE);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200356 goto out;
357 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100358 return rc;
359 }
Jan Höppnera0610a82018-04-27 16:55:27 +0200360 if (device->discipline->setup_blk_queue)
361 device->discipline->setup_blk_queue(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100362 set_capacity(block->gdp,
363 block->blocks << block->s2b_shift);
364 device->state = DASD_STATE_READY;
365 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200366 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100367 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200368 return rc;
369 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100370 } else {
371 device->state = DASD_STATE_READY;
372 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200373out:
374 if (device->discipline->basic_to_ready)
375 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800376 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100379static inline
380int _wait_for_empty_queues(struct dasd_device *device)
381{
382 if (device->block)
383 return list_empty(&device->ccw_queue) &&
384 list_empty(&device->block->ccw_queue);
385 else
386 return list_empty(&device->ccw_queue);
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * Remove device from block device layer. Destroy dirty buffers.
391 * Forget format information. Check if the target level is basic
392 * and if it is create fake disk for formatting.
393 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100394static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Horst Hummel8f617012006-08-30 14:33:33 +0200396 int rc;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100399 if (device->block) {
400 struct dasd_block *block = device->block;
401 rc = dasd_flush_block_queue(block);
402 if (rc) {
403 device->state = DASD_STATE_READY;
404 return rc;
405 }
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100406 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100407 block->blocks = 0;
408 block->bp_block = 0;
409 block->s2b_shift = 0;
410 }
Horst Hummel8f617012006-08-30 14:33:33 +0200411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/*
Horst Hummel90f00942006-03-07 21:55:39 -0800415 * Back to basic.
416 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100417static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800418{
419 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200420 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800421}
422
423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * Make the device online and schedule the bottom half to start
425 * the requeueing of requests from the linux request queue to the
426 * ccw queue.
427 */
Horst Hummel8f617012006-08-30 14:33:33 +0200428static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429dasd_state_ready_to_online(struct dasd_device * device)
430{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100431 struct gendisk *disk;
432 struct disk_part_iter piter;
433 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100436 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100437 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100438 if ((device->features & DASD_FEATURE_USERAW)) {
439 disk = device->block->gdp;
440 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
441 return 0;
442 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100443 disk = device->block->bdev->bd_disk;
444 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
445 while ((part = disk_part_iter_next(&piter)))
446 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
447 disk_part_iter_exit(&piter);
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450}
451
452/*
453 * Stop the requeueing of requests again.
454 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100455static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100457 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100458 struct gendisk *disk;
459 struct disk_part_iter piter;
460 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100461
462 if (device->discipline->online_to_ready) {
463 rc = device->discipline->online_to_ready(device);
464 if (rc)
465 return rc;
466 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100469 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100470 disk = device->block->bdev->bd_disk;
471 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
472 while ((part = disk_part_iter_next(&piter)))
473 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
474 disk_part_iter_exit(&piter);
475 }
Horst Hummel8f617012006-08-30 14:33:33 +0200476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/*
480 * Device startup state changes.
481 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100482static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 int rc;
485
486 rc = 0;
487 if (device->state == DASD_STATE_NEW &&
488 device->target >= DASD_STATE_KNOWN)
489 rc = dasd_state_new_to_known(device);
490
491 if (!rc &&
492 device->state == DASD_STATE_KNOWN &&
493 device->target >= DASD_STATE_BASIC)
494 rc = dasd_state_known_to_basic(device);
495
496 if (!rc &&
497 device->state == DASD_STATE_BASIC &&
498 device->target >= DASD_STATE_READY)
499 rc = dasd_state_basic_to_ready(device);
500
501 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700502 device->state == DASD_STATE_UNFMT &&
503 device->target > DASD_STATE_UNFMT)
504 rc = -EPERM;
505
506 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 device->state == DASD_STATE_READY &&
508 device->target >= DASD_STATE_ONLINE)
509 rc = dasd_state_ready_to_online(device);
510
511 return rc;
512}
513
514/*
515 * Device shutdown state changes.
516 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100517static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Horst Hummel8f617012006-08-30 14:33:33 +0200519 int rc;
520
521 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (device->state == DASD_STATE_ONLINE &&
523 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200524 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200525
Horst Hummel8f617012006-08-30 14:33:33 +0200526 if (!rc &&
527 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200529 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800530
Horst Hummel8f617012006-08-30 14:33:33 +0200531 if (!rc &&
532 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800533 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200534 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800535
Horst Hummel8f617012006-08-30 14:33:33 +0200536 if (!rc &&
537 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200539 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200540
Horst Hummel8f617012006-08-30 14:33:33 +0200541 if (!rc &&
542 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200544 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Horst Hummel8f617012006-08-30 14:33:33 +0200546 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/*
550 * This is the main startup/shutdown routine.
551 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100552static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Sebastian Ott181d9522009-06-22 12:08:21 +0200554 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (device->state == device->target)
557 /* Already where we want to go today... */
558 return;
559 if (device->state < device->target)
560 rc = dasd_increase_state(device);
561 else
562 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200563 if (rc == -EAGAIN)
564 return;
565 if (rc)
566 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200568 /* let user-space know that the device status changed */
569 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200570
571 if (device->state == device->target)
572 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/*
576 * Kick starter for devices that did not complete the startup/shutdown
577 * procedure or were sleeping because of a pending state.
578 * dasd_kick_device will schedule a call do do_kick_device to the kernel
579 * event daemon.
580 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100581static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Al Viro4927b3f2006-12-06 19:18:20 +0000583 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100584 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100586 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100587 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 dasd_put_device(device);
589}
590
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100591void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 dasd_get_device(device);
594 /* queue call to dasd_kick_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200595 if (!schedule_work(&device->kick_work))
596 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200598EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200601 * dasd_reload_device will schedule a call do do_reload_device to the kernel
602 * event daemon.
603 */
604static void do_reload_device(struct work_struct *work)
605{
606 struct dasd_device *device = container_of(work, struct dasd_device,
607 reload_device);
608 device->discipline->reload(device);
609 dasd_put_device(device);
610}
611
612void dasd_reload_device(struct dasd_device *device)
613{
614 dasd_get_device(device);
615 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200616 if (!schedule_work(&device->reload_device))
617 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200618}
619EXPORT_SYMBOL(dasd_reload_device);
620
621/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200622 * dasd_restore_device will schedule a call do do_restore_device to the kernel
623 * event daemon.
624 */
625static void do_restore_device(struct work_struct *work)
626{
627 struct dasd_device *device = container_of(work, struct dasd_device,
628 restore_device);
629 device->cdev->drv->restore(device->cdev);
630 dasd_put_device(device);
631}
632
633void dasd_restore_device(struct dasd_device *device)
634{
635 dasd_get_device(device);
636 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200637 if (!schedule_work(&device->restore_device))
638 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200639}
640
641/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * Set the target state for a device and starts the state change.
643 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100644void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200646 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100647 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* If we are in probeonly mode stop at DASD_STATE_READY. */
649 if (dasd_probeonly && target > DASD_STATE_READY)
650 target = DASD_STATE_READY;
651 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100652 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 wake_up(&dasd_init_waitq);
654 device->target = target;
655 }
656 if (device->state != device->target)
657 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100658 mutex_unlock(&device->state_mutex);
659 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200661EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663/*
664 * Enable devices with device numbers in [from..to].
665 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100666static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 return (device->state == device->target);
669}
670
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100671void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 dasd_set_target_state(device, DASD_STATE_ONLINE);
674 if (device->state <= DASD_STATE_KNOWN)
675 /* No discipline for device found. */
676 dasd_set_target_state(device, DASD_STATE_NEW);
677 /* Now wait for the devices to come up. */
678 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400679
680 dasd_reload_device(device);
681 if (device->discipline->kick_validate)
682 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200684EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686/*
687 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
688 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200689
690unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100693struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100694 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
695};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200696static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/*
699 * Add profiling information for cqr before execution.
700 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100701static void dasd_profile_start(struct dasd_block *block,
702 struct dasd_ccw_req *cqr,
703 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct list_head *l;
706 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200707 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* count the length of the chanq for statistics */
710 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200711 if (dasd_global_profile_level || block->profile.data)
712 list_for_each(l, &block->ccw_queue)
713 if (++counter >= 31)
714 break;
715
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100716 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100717 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100718 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200719 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100720 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200721 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100722 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200723
724 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200725 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200726 block->profile.data->dasd_io_nr_req[counter]++;
727 if (rq_data_dir(req) == READ)
728 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200729 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200730 spin_unlock(&block->profile.lock);
731
732 /*
733 * We count the request for the start device, even though it may run on
734 * some other device due to error recovery. This way we make sure that
735 * we count each request only once.
736 */
737 device = cqr->startdev;
738 if (device->profile.data) {
739 counter = 1; /* request is not yet queued on the start device */
740 list_for_each(l, &device->ccw_queue)
741 if (++counter >= 31)
742 break;
743 }
744 spin_lock(&device->profile.lock);
745 if (device->profile.data) {
746 device->profile.data->dasd_io_nr_req[counter]++;
747 if (rq_data_dir(req) == READ)
748 device->profile.data->dasd_read_nr_req[counter]++;
749 }
750 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753/*
754 * Add profiling information for cqr after execution.
755 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200756
757#define dasd_profile_counter(value, index) \
758{ \
759 for (index = 0; index < 31 && value >> (2+index); index++) \
760 ; \
761}
762
763static void dasd_profile_end_add_data(struct dasd_profile_info *data,
764 int is_alias,
765 int is_tpm,
766 int is_read,
767 long sectors,
768 int sectors_ind,
769 int tottime_ind,
770 int tottimeps_ind,
771 int strtime_ind,
772 int irqtime_ind,
773 int irqtimeps_ind,
774 int endtime_ind)
775{
776 /* in case of an overflow, reset the whole profile */
777 if (data->dasd_io_reqs == UINT_MAX) {
778 memset(data, 0, sizeof(*data));
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100779 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200780 }
781 data->dasd_io_reqs++;
782 data->dasd_io_sects += sectors;
783 if (is_alias)
784 data->dasd_io_alias++;
785 if (is_tpm)
786 data->dasd_io_tpm++;
787
788 data->dasd_io_secs[sectors_ind]++;
789 data->dasd_io_times[tottime_ind]++;
790 data->dasd_io_timps[tottimeps_ind]++;
791 data->dasd_io_time1[strtime_ind]++;
792 data->dasd_io_time2[irqtime_ind]++;
793 data->dasd_io_time2ps[irqtimeps_ind]++;
794 data->dasd_io_time3[endtime_ind]++;
795
796 if (is_read) {
797 data->dasd_read_reqs++;
798 data->dasd_read_sects += sectors;
799 if (is_alias)
800 data->dasd_read_alias++;
801 if (is_tpm)
802 data->dasd_read_tpm++;
803 data->dasd_read_secs[sectors_ind]++;
804 data->dasd_read_times[tottime_ind]++;
805 data->dasd_read_time1[strtime_ind]++;
806 data->dasd_read_time2[irqtime_ind]++;
807 data->dasd_read_time3[endtime_ind]++;
808 }
809}
810
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100811static void dasd_profile_end(struct dasd_block *block,
812 struct dasd_ccw_req *cqr,
813 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Stefan Haberlandd2907222017-07-14 10:33:18 +0200815 unsigned long strtime, irqtime, endtime, tottime;
816 unsigned long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200817 struct dasd_device *device;
818 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
819 int irqtime_ind, irqtimeps_ind, endtime_ind;
Stefan Haberlandd2907222017-07-14 10:33:18 +0200820 struct dasd_profile_info *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200822 device = cqr->startdev;
823 if (!(dasd_global_profile_level ||
824 block->profile.data ||
825 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827
Tejun Heo83096eb2009-05-07 22:24:39 +0900828 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!cqr->buildclk || !cqr->startclk ||
830 !cqr->stopclk || !cqr->endclk ||
831 !sectors)
832 return;
833
834 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
835 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
836 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
837 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
838 tottimeps = tottime / sectors;
839
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200840 dasd_profile_counter(sectors, sectors_ind);
841 dasd_profile_counter(tottime, tottime_ind);
842 dasd_profile_counter(tottimeps, tottimeps_ind);
843 dasd_profile_counter(strtime, strtime_ind);
844 dasd_profile_counter(irqtime, irqtime_ind);
845 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
846 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100848 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100849 if (dasd_global_profile.data) {
Stefan Haberlandd2907222017-07-14 10:33:18 +0200850 data = dasd_global_profile.data;
851 data->dasd_sum_times += tottime;
852 data->dasd_sum_time_str += strtime;
853 data->dasd_sum_time_irq += irqtime;
854 data->dasd_sum_time_end += endtime;
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100855 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200856 cqr->startdev != block->base,
857 cqr->cpmode == 1,
858 rq_data_dir(req) == READ,
859 sectors, sectors_ind, tottime_ind,
860 tottimeps_ind, strtime_ind,
861 irqtime_ind, irqtimeps_ind,
862 endtime_ind);
863 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100864 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200866 spin_lock(&block->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200867 if (block->profile.data) {
868 data = block->profile.data;
869 data->dasd_sum_times += tottime;
870 data->dasd_sum_time_str += strtime;
871 data->dasd_sum_time_irq += irqtime;
872 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200873 dasd_profile_end_add_data(block->profile.data,
874 cqr->startdev != block->base,
875 cqr->cpmode == 1,
876 rq_data_dir(req) == READ,
877 sectors, sectors_ind, tottime_ind,
878 tottimeps_ind, strtime_ind,
879 irqtime_ind, irqtimeps_ind,
880 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200881 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200882 spin_unlock(&block->profile.lock);
883
884 spin_lock(&device->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200885 if (device->profile.data) {
886 data = device->profile.data;
887 data->dasd_sum_times += tottime;
888 data->dasd_sum_time_str += strtime;
889 data->dasd_sum_time_irq += irqtime;
890 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200891 dasd_profile_end_add_data(device->profile.data,
892 cqr->startdev != block->base,
893 cqr->cpmode == 1,
894 rq_data_dir(req) == READ,
895 sectors, sectors_ind, tottime_ind,
896 tottimeps_ind, strtime_ind,
897 irqtime_ind, irqtimeps_ind,
898 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200899 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200900 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200902
903void dasd_profile_reset(struct dasd_profile *profile)
904{
905 struct dasd_profile_info *data;
906
907 spin_lock_bh(&profile->lock);
908 data = profile->data;
909 if (!data) {
910 spin_unlock_bh(&profile->lock);
911 return;
912 }
913 memset(data, 0, sizeof(*data));
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100914 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200915 spin_unlock_bh(&profile->lock);
916}
917
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200918int dasd_profile_on(struct dasd_profile *profile)
919{
920 struct dasd_profile_info *data;
921
922 data = kzalloc(sizeof(*data), GFP_KERNEL);
923 if (!data)
924 return -ENOMEM;
925 spin_lock_bh(&profile->lock);
926 if (profile->data) {
927 spin_unlock_bh(&profile->lock);
928 kfree(data);
929 return 0;
930 }
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100931 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200932 profile->data = data;
933 spin_unlock_bh(&profile->lock);
934 return 0;
935}
936
937void dasd_profile_off(struct dasd_profile *profile)
938{
939 spin_lock_bh(&profile->lock);
940 kfree(profile->data);
941 profile->data = NULL;
942 spin_unlock_bh(&profile->lock);
943}
944
945char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
946{
947 char *buffer;
948
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200949 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200950 if (buffer == NULL)
951 return ERR_PTR(-ENOMEM);
952 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200953 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200954 return ERR_PTR(-EFAULT);
955 }
956 /* got the string, now strip linefeed. */
957 if (buffer[user_len - 1] == '\n')
958 buffer[user_len - 1] = 0;
959 else
960 buffer[user_len] = 0;
961 return buffer;
962}
963
964static ssize_t dasd_stats_write(struct file *file,
965 const char __user *user_buf,
966 size_t user_len, loff_t *pos)
967{
968 char *buffer, *str;
969 int rc;
970 struct seq_file *m = (struct seq_file *)file->private_data;
971 struct dasd_profile *prof = m->private;
972
973 if (user_len > 65536)
974 user_len = 65536;
975 buffer = dasd_get_user_string(user_buf, user_len);
976 if (IS_ERR(buffer))
977 return PTR_ERR(buffer);
978
979 str = skip_spaces(buffer);
980 rc = user_len;
981 if (strncmp(str, "reset", 5) == 0) {
982 dasd_profile_reset(prof);
983 } else if (strncmp(str, "on", 2) == 0) {
984 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100985 if (rc)
986 goto out;
987 rc = user_len;
988 if (prof == &dasd_global_profile) {
989 dasd_profile_reset(prof);
990 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
991 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200992 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100993 if (prof == &dasd_global_profile)
994 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200995 dasd_profile_off(prof);
996 } else
997 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100998out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200999 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001000 return rc;
1001}
1002
1003static void dasd_stats_array(struct seq_file *m, unsigned int *array)
1004{
1005 int i;
1006
1007 for (i = 0; i < 32; i++)
1008 seq_printf(m, "%u ", array[i]);
1009 seq_putc(m, '\n');
1010}
1011
1012static void dasd_stats_seq_print(struct seq_file *m,
1013 struct dasd_profile_info *data)
1014{
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +01001015 seq_printf(m, "start_time %lld.%09ld\n",
1016 (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001017 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
1018 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1019 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1020 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Stefan Haberlandd2907222017-07-14 10:33:18 +02001021 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1022 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1023 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1024 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1025 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1026 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1027 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1028 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001029 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001030 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001031 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001032 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001033 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001034 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001035 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001036 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001037 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001038 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001039 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001040 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001041 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001042 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001043 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001044 dasd_stats_array(m, data->dasd_io_nr_req);
1045 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1046 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1047 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1048 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001049 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001050 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001051 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001052 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001053 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001054 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001055 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001056 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001057 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001058 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001059 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001060 dasd_stats_array(m, data->dasd_read_nr_req);
1061}
1062
1063static int dasd_stats_show(struct seq_file *m, void *v)
1064{
1065 struct dasd_profile *profile;
1066 struct dasd_profile_info *data;
1067
1068 profile = m->private;
1069 spin_lock_bh(&profile->lock);
1070 data = profile->data;
1071 if (!data) {
1072 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001073 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001074 return 0;
1075 }
1076 dasd_stats_seq_print(m, data);
1077 spin_unlock_bh(&profile->lock);
1078 return 0;
1079}
1080
1081static int dasd_stats_open(struct inode *inode, struct file *file)
1082{
1083 struct dasd_profile *profile = inode->i_private;
1084 return single_open(file, dasd_stats_show, profile);
1085}
1086
1087static const struct file_operations dasd_stats_raw_fops = {
1088 .owner = THIS_MODULE,
1089 .open = dasd_stats_open,
1090 .read = seq_read,
1091 .llseek = seq_lseek,
1092 .release = single_release,
1093 .write = dasd_stats_write,
1094};
1095
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001096static void dasd_profile_init(struct dasd_profile *profile,
1097 struct dentry *base_dentry)
1098{
Al Virof4ae40a62011-07-24 04:33:43 -04001099 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001100 struct dentry *pde;
1101
1102 if (!base_dentry)
1103 return;
1104 profile->dentry = NULL;
1105 profile->data = NULL;
1106 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1107 pde = debugfs_create_file("statistics", mode, base_dentry,
1108 profile, &dasd_stats_raw_fops);
1109 if (pde && !IS_ERR(pde))
1110 profile->dentry = pde;
1111 return;
1112}
1113
1114static void dasd_profile_exit(struct dasd_profile *profile)
1115{
1116 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001117 debugfs_remove(profile->dentry);
1118 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001119}
1120
1121static void dasd_statistics_removeroot(void)
1122{
1123 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001124 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001125 debugfs_remove(dasd_debugfs_global_entry);
1126 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001127}
1128
1129static void dasd_statistics_createroot(void)
1130{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001131 struct dentry *pde;
1132
1133 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001134 pde = debugfs_create_dir("dasd", NULL);
1135 if (!pde || IS_ERR(pde))
1136 goto error;
1137 dasd_debugfs_root_entry = pde;
1138 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1139 if (!pde || IS_ERR(pde))
1140 goto error;
1141 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001142 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001143 return;
1144
1145error:
1146 DBF_EVENT(DBF_ERR, "%s",
1147 "Creation of the dasd debugfs interface failed");
1148 dasd_statistics_removeroot();
1149 return;
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001153#define dasd_profile_start(block, cqr, req) do {} while (0)
1154#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001155
1156static void dasd_statistics_createroot(void)
1157{
1158 return;
1159}
1160
1161static void dasd_statistics_removeroot(void)
1162{
1163 return;
1164}
1165
1166int dasd_stats_generic_show(struct seq_file *m, void *v)
1167{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001168 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001169 return 0;
1170}
1171
1172static void dasd_profile_init(struct dasd_profile *profile,
1173 struct dentry *base_dentry)
1174{
1175 return;
1176}
1177
1178static void dasd_profile_exit(struct dasd_profile *profile)
1179{
1180 return;
1181}
1182
1183int dasd_profile_on(struct dasd_profile *profile)
1184{
1185 return 0;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188#endif /* CONFIG_DASD_PROFILE */
1189
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001190static int dasd_hosts_show(struct seq_file *m, void *v)
1191{
1192 struct dasd_device *device;
1193 int rc = -EOPNOTSUPP;
1194
1195 device = m->private;
1196 dasd_get_device(device);
1197
1198 if (device->discipline->hosts_print)
1199 rc = device->discipline->hosts_print(device, m);
1200
1201 dasd_put_device(device);
1202 return rc;
1203}
1204
Yangtao Lica92b932018-12-01 22:28:11 -05001205DEFINE_SHOW_ATTRIBUTE(dasd_hosts);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001206
1207static void dasd_hosts_exit(struct dasd_device *device)
1208{
1209 debugfs_remove(device->hosts_dentry);
1210 device->hosts_dentry = NULL;
1211}
1212
1213static void dasd_hosts_init(struct dentry *base_dentry,
1214 struct dasd_device *device)
1215{
1216 struct dentry *pde;
1217 umode_t mode;
1218
1219 if (!base_dentry)
1220 return;
1221
1222 mode = S_IRUSR | S_IFREG;
1223 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1224 device, &dasd_hosts_fops);
1225 if (pde && !IS_ERR(pde))
1226 device->hosts_dentry = pde;
1227}
1228
Sebastian Ottc5205f22018-06-04 19:07:39 +02001229struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
1230 struct dasd_device *device,
1231 struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 unsigned long flags;
Sebastian Ottc5205f22018-06-04 19:07:39 +02001234 char *data, *chunk;
1235 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (cplength > 0)
1238 size += cplength * sizeof(struct ccw1);
1239 if (datasize > 0)
1240 size += datasize;
Sebastian Ottc5205f22018-06-04 19:07:39 +02001241 if (!cqr)
1242 size += (sizeof(*cqr) + 7L) & -8L;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 spin_lock_irqsave(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001245 data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 spin_unlock_irqrestore(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001247 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return ERR_PTR(-ENOMEM);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001249 if (!cqr) {
1250 cqr = (void *) data;
1251 data += (sizeof(*cqr) + 7L) & -8L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Sebastian Ottc5205f22018-06-04 19:07:39 +02001253 memset(cqr, 0, sizeof(*cqr));
1254 cqr->mem_chunk = chunk;
1255 if (cplength > 0) {
1256 cqr->cpaddr = data;
1257 data += cplength * sizeof(struct ccw1);
1258 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (datasize > 0) {
1261 cqr->data = data;
1262 memset(cqr->data, 0, datasize);
1263 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001264 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1266 dasd_get_device(device);
1267 return cqr;
1268}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001269EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jan Höppner5e2b17e2018-04-27 16:51:22 +02001271struct dasd_ccw_req *dasd_fmalloc_request(int magic, int cplength,
1272 int datasize,
1273 struct dasd_device *device)
1274{
1275 struct dasd_ccw_req *cqr;
1276 unsigned long flags;
1277 int size, cqr_size;
1278 char *data;
1279
1280 cqr_size = (sizeof(*cqr) + 7L) & -8L;
1281 size = cqr_size;
1282 if (cplength > 0)
1283 size += cplength * sizeof(struct ccw1);
1284 if (datasize > 0)
1285 size += datasize;
1286
1287 spin_lock_irqsave(&device->mem_lock, flags);
1288 cqr = dasd_alloc_chunk(&device->ese_chunks, size);
1289 spin_unlock_irqrestore(&device->mem_lock, flags);
1290 if (!cqr)
1291 return ERR_PTR(-ENOMEM);
1292 memset(cqr, 0, sizeof(*cqr));
1293 data = (char *)cqr + cqr_size;
1294 cqr->cpaddr = NULL;
1295 if (cplength > 0) {
1296 cqr->cpaddr = data;
1297 data += cplength * sizeof(struct ccw1);
1298 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1299 }
1300 cqr->data = NULL;
1301 if (datasize > 0) {
1302 cqr->data = data;
1303 memset(cqr->data, 0, datasize);
1304 }
1305
1306 cqr->magic = magic;
1307 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1308 dasd_get_device(device);
1309
1310 return cqr;
1311}
1312EXPORT_SYMBOL(dasd_fmalloc_request);
1313
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001314void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 unsigned long flags;
1317
1318 spin_lock_irqsave(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001319 dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 spin_unlock_irqrestore(&device->mem_lock, flags);
1321 dasd_put_device(device);
1322}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001323EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Jan Höppner5e2b17e2018-04-27 16:51:22 +02001325void dasd_ffree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1326{
1327 unsigned long flags;
1328
1329 spin_lock_irqsave(&device->mem_lock, flags);
1330 dasd_free_chunk(&device->ese_chunks, cqr);
1331 spin_unlock_irqrestore(&device->mem_lock, flags);
1332 dasd_put_device(device);
1333}
1334EXPORT_SYMBOL(dasd_ffree_request);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/*
1337 * Check discipline magic in cqr.
1338 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001339static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct dasd_device *device;
1342
1343 if (cqr == NULL)
1344 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001345 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001347 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 " dasd_ccw_req 0x%08x magic doesn't match"
1349 " discipline 0x%08x",
1350 cqr->magic,
1351 *(unsigned int *) device->discipline->name);
1352 return -EINVAL;
1353 }
1354 return 0;
1355}
1356
1357/*
1358 * Terminate the current i/o and set the request to clear_pending.
1359 * Timer keeps device runnig.
1360 * ccw_device_clear can fail if the i/o subsystem
1361 * is in a bad mood.
1362 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001363int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct dasd_device *device;
1366 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001367 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* Check the cqr */
1370 rc = dasd_check_cqr(cqr);
1371 if (rc)
1372 return rc;
1373 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001374 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1376 rc = ccw_device_clear(device->cdev, (long) cqr);
1377 switch (rc) {
1378 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001379 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001380 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001381 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 DBF_DEV_EVENT(DBF_DEBUG, device,
1383 "terminate cqr %p successful",
1384 cqr);
1385 break;
1386 case -ENODEV:
1387 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1388 "device gone, retry");
1389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001391 /*
1392 * device not valid so no I/O could be running
1393 * handle CQR as termination successful
1394 */
1395 cqr->status = DASD_CQR_CLEARED;
1396 cqr->stopclk = get_tod_clock();
1397 cqr->starttime = 0;
1398 /* no retries for invalid devices */
1399 cqr->retries = -1;
1400 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1401 "EINVAL, handle as terminated");
1402 /* fake rc to success */
1403 rc = 0;
1404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001406 /* internal error 10 - unknown rc*/
1407 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1408 dev_err(&device->cdev->dev, "An error occurred in the "
1409 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 BUG();
1411 break;
1412 }
1413 retries++;
1414 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001415 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return rc;
1417}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001418EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420/*
1421 * Start the i/o. This start_IO can fail if the channel is really busy.
1422 * In that case set up a timer to start the request later.
1423 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001424int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 struct dasd_device *device;
1427 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001428 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 /* Check the cqr */
1431 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001432 if (rc) {
1433 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001435 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001437 if (((cqr->block &&
1438 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1439 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1440 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1441 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1442 "because of stolen lock", cqr);
1443 cqr->status = DASD_CQR_ERROR;
1444 cqr->intrc = -EPERM;
1445 return -EPERM;
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001448 /* internal error 14 - start_IO run out of retries */
1449 sprintf(errorstring, "14 %p", cqr);
1450 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1451 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001452 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EIO;
1454 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001455 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 cqr->starttime = jiffies;
1457 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001459 cqr->lpm &= dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001460 if (!cqr->lpm)
Stefan Haberlandc9346152016-08-08 15:53:54 +02001461 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001462 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001463 if (cqr->cpmode == 1) {
1464 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1465 (long) cqr, cqr->lpm);
1466 } else {
1467 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1468 (long) cqr, cqr->lpm, 0);
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 switch (rc) {
1471 case 0:
1472 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
1474 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001475 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 "start_IO: device busy, retry later");
1477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001479 /* -EACCES indicates that the request used only a subset of the
1480 * available paths and all these paths are gone. If the lpm of
1481 * this request was only a subset of the opm (e.g. the ppm) then
1482 * we just do a retry with all available paths.
1483 * If we already use the full opm, something is amiss, and we
1484 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001486 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1487 DBF_DEV_EVENT(DBF_WARNING, device,
1488 "start_IO: selected paths gone (%x)",
1489 cqr->lpm);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001490 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1491 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001492 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1493 "start_IO: selected paths gone,"
1494 " retry on all paths");
1495 } else {
1496 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1497 "start_IO: all paths in opm gone,"
1498 " do path verification");
1499 dasd_generic_last_path_gone(device);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001500 dasd_path_no_path(device);
1501 dasd_path_set_tbvpm(device,
1502 ccw_device_get_path_mask(
1503 device->cdev));
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
1506 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001507 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001508 "start_IO: -ENODEV device gone, retry");
1509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001511 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001512 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001514 case -EINVAL:
1515 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001516 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001517 "start_IO: -EINVAL device currently "
1518 "not accessible");
1519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001521 /* internal error 11 - unknown rc */
1522 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1523 dev_err(&device->cdev->dev,
1524 "An error occurred in the DASD device driver, "
1525 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 BUG();
1527 break;
1528 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001529 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return rc;
1531}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001532EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534/*
1535 * Timeout function for dasd devices. This is used for different purposes
1536 * 1) missing interrupt handler for normal operation
1537 * 2) delayed start of request where start_IO failed with -EBUSY
1538 * 3) timeout for missing state change interrupts
1539 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1540 * DASD_CQR_QUEUED for 2) and 3).
1541 */
Kees Cooke99e88a2017-10-16 14:43:17 -07001542static void dasd_device_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 unsigned long flags;
1545 struct dasd_device *device;
1546
Kees Cooke99e88a2017-10-16 14:43:17 -07001547 device = from_timer(device, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1549 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001550 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001552 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555/*
1556 * Setup timeout for a device in jiffies.
1557 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001558void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001560 if (expires == 0)
1561 del_timer(&device->timer);
1562 else
1563 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001565EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567/*
1568 * Clear timeout for a device.
1569 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001570void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001572 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001574EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001576static void dasd_handle_killed_request(struct ccw_device *cdev,
1577 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct dasd_ccw_req *cqr;
1580 struct dasd_device *device;
1581
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001582 if (!intparm)
1583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 cqr = (struct dasd_ccw_req *) intparm;
1585 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001586 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1587 "invalid status in handle_killed_request: "
1588 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return;
1590 }
1591
Stefan Haberland589c74d2010-02-26 22:37:47 +01001592 device = dasd_device_from_cdev_locked(cdev);
1593 if (IS_ERR(device)) {
1594 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1595 "unable to get device from cdev");
1596 return;
1597 }
1598
1599 if (!cqr->startdev ||
1600 device != cqr->startdev ||
1601 strncmp(cqr->startdev->discipline->ebcname,
1602 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001603 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1604 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001605 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return;
1607 }
1608
1609 /* Schedule request to be retried. */
1610 cqr->status = DASD_CQR_QUEUED;
1611
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001612 dasd_device_clear_timer(device);
1613 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 dasd_put_device(device);
1615}
1616
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001617void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001619 /* First of all start sense subsystem status request. */
1620 dasd_eer_snss(device);
1621
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001622 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001623 dasd_schedule_device_bh(device);
Stefan Haberlande4433432017-05-22 10:59:11 +02001624 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001625 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02001626 if (device->block->request_queue)
1627 blk_mq_run_hw_queues(device->block->request_queue,
1628 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001631EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Stefan Haberlanda521b042016-08-08 15:56:54 +02001633static int dasd_check_hpf_error(struct irb *irb)
1634{
1635 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1636 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1637 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1638}
1639
Jan Höppner5e2b17e2018-04-27 16:51:22 +02001640static int dasd_ese_needs_format(struct dasd_block *block, struct irb *irb)
1641{
1642 struct dasd_device *device = NULL;
1643 u8 *sense = NULL;
1644
1645 if (!block)
1646 return 0;
1647 device = block->base;
1648 if (!device || !device->discipline->is_ese)
1649 return 0;
1650 if (!device->discipline->is_ese(device))
1651 return 0;
1652
1653 sense = dasd_get_sense(irb);
1654 if (!sense)
1655 return 0;
1656
1657 return !!(sense[1] & SNS1_NO_REC_FOUND) ||
1658 !!(sense[1] & SNS1_FILE_PROTECTED) ||
1659 scsw_cstat(&irb->scsw) == SCHN_STAT_INCORR_LEN;
1660}
1661
Jan Höppner9e12e542018-07-03 10:56:51 +02001662static int dasd_ese_oos_cond(u8 *sense)
1663{
1664 return sense[0] & SNS0_EQUIPMENT_CHECK &&
1665 sense[1] & SNS1_PERM_ERR &&
1666 sense[1] & SNS1_WRITE_INHIBITED &&
1667 sense[25] == 0x01;
1668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670/*
1671 * Interrupt handler for "normal" ssch-io based dasd devices.
1672 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1674 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Jan Höppner5e2b17e2018-04-27 16:51:22 +02001676 struct dasd_ccw_req *cqr, *next, *fcqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct dasd_device *device;
Jan Höppner7bf76f012017-08-15 16:40:18 +02001678 unsigned long now;
Jan Höppner8fd57522015-08-19 13:41:20 +02001679 int nrf_suppressed = 0;
1680 int fp_suppressed = 0;
1681 u8 *sense = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001684 cqr = (struct dasd_ccw_req *) intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (IS_ERR(irb)) {
1686 switch (PTR_ERR(irb)) {
1687 case -EIO:
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001688 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001689 device = cqr->startdev;
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001690 cqr->status = DASD_CQR_CLEARED;
1691 dasd_device_clear_timer(device);
1692 wake_up(&dasd_flush_wq);
1693 dasd_schedule_device_bh(device);
1694 return;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 break;
1697 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001698 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1699 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 break;
1701 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001702 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1703 "unknown error %ld\n", __func__,
1704 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001706 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return;
1708 }
1709
Heiko Carstens1aae0562013-01-30 09:49:40 +01001710 now = get_tod_clock();
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001711 /* check for conditions that should be handled immediately */
1712 if (!cqr ||
1713 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1714 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001715 if (cqr)
1716 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001717 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001718 if (IS_ERR(device))
1719 return;
1720 /* ignore unsolicited interrupts for DIAG discipline */
1721 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001725
1726 /*
1727 * In some cases 'File Protected' or 'No Record Found' errors
1728 * might be expected and debug log messages for the
1729 * corresponding interrupts shouldn't be written then.
1730 * Check if either of the according suppress bits is set.
1731 */
1732 sense = dasd_get_sense(irb);
1733 if (sense) {
1734 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1735 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1736 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1737 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
Jan Höppner9e12e542018-07-03 10:56:51 +02001738
1739 /*
1740 * Extent pool probably out-of-space.
1741 * Stop device and check exhaust level.
1742 */
1743 if (dasd_ese_oos_cond(sense)) {
1744 dasd_generic_space_exhaust(device, cqr);
1745 device->discipline->ext_pool_exhaust(device, cqr);
1746 dasd_put_device(device);
1747 return;
1748 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001749 }
1750 if (!(fp_suppressed || nrf_suppressed))
1751 device->discipline->dump_sense_dbf(device, irb, "int");
1752
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001753 if (device->features & DASD_FEATURE_ERPLOG)
1754 device->discipline->dump_sense(device, cqr, irb);
1755 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001756 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001758
1759 /* check for for attention message */
1760 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1761 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland22021342017-01-25 14:47:32 +01001762 if (!IS_ERR(device)) {
1763 device->discipline->check_attention(device,
1764 irb->esw.esw1.lpum);
1765 dasd_put_device(device);
1766 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001767 }
1768
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001769 if (!cqr)
1770 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001772 device = (struct dasd_device *) cqr->startdev;
1773 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001775 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1776 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return;
1778 }
1779
Jan Höppner5e2b17e2018-04-27 16:51:22 +02001780 if (dasd_ese_needs_format(cqr->block, irb)) {
1781 if (rq_data_dir((struct request *)cqr->callback_data) == READ) {
1782 device->discipline->ese_read(cqr);
1783 cqr->status = DASD_CQR_SUCCESS;
1784 cqr->stopclk = now;
1785 dasd_device_clear_timer(device);
1786 dasd_schedule_device_bh(device);
1787 return;
1788 }
1789 fcqr = device->discipline->ese_format(device, cqr);
1790 if (IS_ERR(fcqr)) {
1791 /*
1792 * If we can't format now, let the request go
1793 * one extra round. Maybe we can format later.
1794 */
1795 cqr->status = DASD_CQR_QUEUED;
1796 } else {
1797 fcqr->status = DASD_CQR_QUEUED;
1798 cqr->status = DASD_CQR_QUEUED;
1799 list_add(&fcqr->devlist, &device->ccw_queue);
1800 dasd_schedule_device_bh(device);
1801 return;
1802 }
1803 }
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001806 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001807 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001808 cqr->status = DASD_CQR_CLEARED;
1809 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001810 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001811 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return;
1813 }
1814
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001815 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001817 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1818 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return;
1820 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001821
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001822 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001824 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1825 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001826 /* request was completed successfully */
1827 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 cqr->stopclk = now;
1829 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001830 if (cqr->devlist.next != &device->ccw_queue) {
1831 next = list_entry(cqr->devlist.next,
1832 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001834 } else { /* error */
Stefan Haberlanda521b042016-08-08 15:56:54 +02001835 /* check for HPF error
1836 * call discipline function to requeue all requests
1837 * and disable HPF accordingly
1838 */
1839 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1840 device->discipline->handle_hpf_error)
1841 device->discipline->handle_hpf_error(device, irb);
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001842 /*
1843 * If we don't want complex ERP for this request, then just
1844 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001845 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001846 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001847 cqr->retries > 0) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001848 if (cqr->lpm == dasd_path_get_opm(device))
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001849 DBF_DEV_EVENT(DBF_DEBUG, device,
1850 "default ERP in fastpath "
1851 "(%i retries left)",
1852 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001853 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
Stefan Haberlandc9346152016-08-08 15:53:54 +02001854 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001855 cqr->status = DASD_CQR_QUEUED;
1856 next = cqr;
1857 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001859 }
1860 if (next && (next->status == DASD_CQR_QUEUED) &&
1861 (!device->stopped)) {
1862 if (device->discipline->start_IO(next) == 0)
1863 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001866 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001868 dasd_device_clear_timer(device);
1869 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001871EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001873enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1874{
1875 struct dasd_device *device;
1876
1877 device = dasd_device_from_cdev_locked(cdev);
1878
1879 if (IS_ERR(device))
1880 goto out;
1881 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1882 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001883 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001884 dasd_put_device(device);
1885 goto out;
1886 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001887 if (device->discipline->dump_sense_dbf)
1888 device->discipline->dump_sense_dbf(device, irb, "uc");
1889 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001890 dasd_put_device(device);
1891out:
1892 return UC_TODO_RETRY;
1893}
1894EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001897 * If we have an error on a dasd_block layer request then we cancel
1898 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001900static void __dasd_device_recovery(struct dasd_device *device,
1901 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct list_head *l, *n;
1904 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001907 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001909 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001911
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001912 list_for_each_safe(l, n, &device->ccw_queue) {
1913 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1914 if (cqr->status == DASD_CQR_QUEUED &&
1915 ref_cqr->block == cqr->block) {
1916 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001918 }
1919};
1920
1921/*
1922 * Remove those ccw requests from the queue that need to be returned
1923 * to the upper layer.
1924 */
1925static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1926 struct list_head *final_queue)
1927{
1928 struct list_head *l, *n;
1929 struct dasd_ccw_req *cqr;
1930
1931 /* Process request with final status. */
1932 list_for_each_safe(l, n, &device->ccw_queue) {
1933 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1934
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001935 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001936 if (cqr->status == DASD_CQR_QUEUED ||
1937 cqr->status == DASD_CQR_IN_IO ||
1938 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001939 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001940 if (cqr->status == DASD_CQR_ERROR) {
1941 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001943 /* Rechain finished requests to final queue */
1944 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946}
1947
Sebastian Ott5c618c02018-05-24 12:18:58 +02001948static void __dasd_process_cqr(struct dasd_device *device,
1949 struct dasd_ccw_req *cqr)
1950{
1951 char errorstring[ERRORLENGTH];
1952
1953 switch (cqr->status) {
1954 case DASD_CQR_SUCCESS:
1955 cqr->status = DASD_CQR_DONE;
1956 break;
1957 case DASD_CQR_ERROR:
1958 cqr->status = DASD_CQR_NEED_ERP;
1959 break;
1960 case DASD_CQR_CLEARED:
1961 cqr->status = DASD_CQR_TERMINATED;
1962 break;
1963 default:
1964 /* internal error 12 - wrong cqr status*/
1965 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1966 dev_err(&device->cdev->dev,
1967 "An error occurred in the DASD device driver, "
1968 "reason=%s\n", errorstring);
1969 BUG();
1970 }
1971 if (cqr->callback)
1972 cqr->callback(cqr, cqr->callback_data);
1973}
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001976 * the cqrs from the final queue are returned to the upper layer
1977 * by setting a dasd_block state and calling the callback function
1978 */
1979static void __dasd_device_process_final_queue(struct dasd_device *device,
1980 struct list_head *final_queue)
1981{
1982 struct list_head *l, *n;
1983 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001984 struct dasd_block *block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001985
1986 list_for_each_safe(l, n, final_queue) {
1987 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1988 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001989 block = cqr->block;
Sebastian Ott5c618c02018-05-24 12:18:58 +02001990 if (!block) {
1991 __dasd_process_cqr(device, cqr);
1992 } else {
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001993 spin_lock_bh(&block->queue_lock);
Sebastian Ott5c618c02018-05-24 12:18:58 +02001994 __dasd_process_cqr(device, cqr);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001995 spin_unlock_bh(&block->queue_lock);
Sebastian Ott5c618c02018-05-24 12:18:58 +02001996 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001997 }
1998}
1999
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002000/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * Take a look at the first request on the ccw queue and check
2002 * if it reached its expire time. If so, terminate the IO.
2003 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002004static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct dasd_ccw_req *cqr;
2007
2008 if (list_empty(&device->ccw_queue))
2009 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002010 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01002011 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
2012 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01002013 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2014 /*
2015 * IO in safe offline processing should not
2016 * run out of retries
2017 */
2018 cqr->retries++;
2019 }
Horst Hummel29145a62006-12-04 15:40:15 +01002020 if (device->discipline->term_IO(cqr) != 0) {
2021 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002022 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02002023 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002024 "ended, retrying in 5 s\n",
2025 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01002026 cqr->expires += 5*HZ;
2027 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01002028 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002029 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02002030 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002031 "remaining\n", cqr, (cqr->expires/HZ),
2032 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034 }
2035}
2036
2037/*
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002038 * return 1 when device is not eligible for IO
2039 */
2040static int __dasd_device_is_unusable(struct dasd_device *device,
2041 struct dasd_ccw_req *cqr)
2042{
Jan Höppner9e12e542018-07-03 10:56:51 +02002043 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM | DASD_STOPPED_NOSPC);
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002044
Stefan Haberlande8ac0152017-05-18 13:24:45 +02002045 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
2046 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
2047 /*
2048 * dasd is being set offline
2049 * but it is no safe offline where we have to allow I/O
2050 */
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002051 return 1;
2052 }
2053 if (device->stopped) {
2054 if (device->stopped & mask) {
2055 /* stopped and CQR will not change that. */
2056 return 1;
2057 }
2058 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2059 /* CQR is not able to change device to
2060 * operational. */
2061 return 1;
2062 }
2063 /* CQR required to get device operational. */
2064 }
2065 return 0;
2066}
2067
2068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Take a look at the first request on the ccw queue and check
2070 * if it needs to be started.
2071 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 struct dasd_ccw_req *cqr;
2075 int rc;
2076
2077 if (list_empty(&device->ccw_queue))
2078 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002079 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002080 if (cqr->status != DASD_CQR_QUEUED)
2081 return;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002082 /* if device is not usable return request to upper layer */
2083 if (__dasd_device_is_unusable(device, cqr)) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002084 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002085 cqr->status = DASD_CQR_CLEARED;
2086 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002087 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002088 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002089
2090 rc = device->discipline->start_IO(cqr);
2091 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002092 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002093 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002094 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002095 } else
2096 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002097 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02002098}
2099
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002100static void __dasd_device_check_path_events(struct dasd_device *device)
2101{
2102 int rc;
2103
Stefan Haberlandc9346152016-08-08 15:53:54 +02002104 if (!dasd_path_get_tbvpm(device))
2105 return;
2106
2107 if (device->stopped &
2108 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2109 return;
2110 rc = device->discipline->verify_path(device,
2111 dasd_path_get_tbvpm(device));
2112 if (rc)
2113 dasd_device_set_timer(device, 50);
2114 else
2115 dasd_path_clear_all_verify(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002116};
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002119 * Go through all request on the dasd_device request queue,
2120 * terminate them on the cdev if necessary, and return them to the
2121 * submitting layer via callback.
2122 * Note:
2123 * Make sure that all 'submitting layers' still exist when
2124 * this function is called!. In other words, when 'device' is a base
2125 * device then all block layer requests must have been removed before
2126 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002128int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002130 struct dasd_ccw_req *cqr, *n;
2131 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 INIT_LIST_HEAD(&flush_queue);
2135 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02002136 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002137 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02002138 /* Check status and move request to flush_queue */
2139 switch (cqr->status) {
2140 case DASD_CQR_IN_IO:
2141 rc = device->discipline->term_IO(cqr);
2142 if (rc) {
2143 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002144 dev_err(&device->cdev->dev,
2145 "Flushing the DASD request queue "
2146 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002147 /* stop flush processing */
2148 goto finished;
2149 }
2150 break;
2151 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002152 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002153 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002154 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002155 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002156 break;
2157 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002158 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002159 }
Horst Hummel8f617012006-08-30 14:33:33 +02002160finished:
2161 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002162 /*
2163 * After this point all requests must be in state CLEAR_PENDING,
2164 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2165 * one of the others.
2166 */
2167 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2168 wait_event(dasd_flush_wq,
2169 (cqr->status != DASD_CQR_CLEAR_PENDING));
2170 /*
2171 * Now set each request back to TERMINATED, DONE or NEED_ERP
2172 * and call the callback function of flushed requests
2173 */
2174 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002175 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002177EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179/*
2180 * Acquire the device lock and process queues for the device.
2181 */
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002182static void dasd_device_tasklet(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002184 struct dasd_device *device = (struct dasd_device *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 atomic_set (&device->tasklet_scheduled, 0);
2188 INIT_LIST_HEAD(&final_queue);
2189 spin_lock_irq(get_ccwdev_lock(device->cdev));
2190 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002191 __dasd_device_check_expire(device);
2192 /* find final requests on ccw queue */
2193 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002194 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2196 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002197 __dasd_device_process_final_queue(device, &final_queue);
2198 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002200 __dasd_device_start_head(device);
2201 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002202 if (waitqueue_active(&shutdown_waitq))
2203 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 dasd_put_device(device);
2205}
2206
2207/*
2208 * Schedules a call to dasd_tasklet over the device tasklet.
2209 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002210void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002213 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 return;
2215 dasd_get_device(device);
2216 tasklet_hi_schedule(&device->tasklet);
2217}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002218EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002220void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2221{
2222 device->stopped |= bits;
2223}
2224EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2225
2226void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2227{
2228 device->stopped &= ~bits;
2229 if (!device->stopped)
2230 wake_up(&generic_waitq);
2231}
2232EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002235 * Queue a request to the head of the device ccw_queue.
2236 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002238void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 struct dasd_device *device;
2241 unsigned long flags;
2242
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002243 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002245 cqr->status = DASD_CQR_QUEUED;
2246 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002248 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2250}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002251EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002254 * Queue a request to the tail of the device ccw_queue.
2255 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002257void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 struct dasd_device *device;
2260 unsigned long flags;
2261
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002262 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002264 cqr->status = DASD_CQR_QUEUED;
2265 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002267 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2269}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002270EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002273 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002275void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002277 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2278 cqr->callback_data = DASD_SLEEPON_END_TAG;
2279 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2280 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
Stefan Haberland5915a872011-10-30 15:16:57 +01002282EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002284static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 struct dasd_device *device;
2287 int rc;
2288
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002289 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002291 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2293 return rc;
2294}
2295
2296/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002297 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2298 */
2299static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2300{
2301 struct dasd_device *device;
2302 dasd_erp_fn_t erp_fn;
2303
2304 if (cqr->status == DASD_CQR_FILLED)
2305 return 0;
2306 device = cqr->startdev;
2307 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2308 if (cqr->status == DASD_CQR_TERMINATED) {
2309 device->discipline->handle_terminated_request(cqr);
2310 return 1;
2311 }
2312 if (cqr->status == DASD_CQR_NEED_ERP) {
2313 erp_fn = device->discipline->erp_action(cqr);
2314 erp_fn(cqr);
2315 return 1;
2316 }
2317 if (cqr->status == DASD_CQR_FAILED)
2318 dasd_log_sense(cqr, &cqr->irb);
2319 if (cqr->refers) {
2320 __dasd_process_erp(device, cqr);
2321 return 1;
2322 }
2323 }
2324 return 0;
2325}
2326
2327static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2328{
2329 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2330 if (cqr->refers) /* erp is not done yet */
2331 return 1;
2332 return ((cqr->status != DASD_CQR_DONE) &&
2333 (cqr->status != DASD_CQR_FAILED));
2334 } else
2335 return (cqr->status == DASD_CQR_FILLED);
2336}
2337
2338static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2339{
2340 struct dasd_device *device;
2341 int rc;
2342 struct list_head ccw_queue;
2343 struct dasd_ccw_req *cqr;
2344
2345 INIT_LIST_HEAD(&ccw_queue);
2346 maincqr->status = DASD_CQR_FILLED;
2347 device = maincqr->startdev;
2348 list_add(&maincqr->blocklist, &ccw_queue);
2349 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2350 cqr = list_first_entry(&ccw_queue,
2351 struct dasd_ccw_req, blocklist)) {
2352
2353 if (__dasd_sleep_on_erp(cqr))
2354 continue;
2355 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2356 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002357 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2358 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2359 cqr->status = DASD_CQR_FAILED;
2360 cqr->intrc = -EPERM;
2361 continue;
2362 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002363 /* Non-temporary stop condition will trigger fail fast */
2364 if (device->stopped & ~DASD_STOPPED_PENDING &&
2365 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2366 (!dasd_eer_enabled(device))) {
2367 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002368 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002369 continue;
2370 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002371 /*
Stefan Haberlanda9f62732016-09-20 10:29:22 +02002372 * Don't try to start requests if device is in
2373 * offline processing, it might wait forever
2374 */
2375 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2376 cqr->status = DASD_CQR_FAILED;
2377 cqr->intrc = -ENODEV;
2378 continue;
2379 }
2380 /*
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002381 * Don't try to start requests if device is stopped
2382 * except path verification requests
2383 */
2384 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2385 if (interruptible) {
2386 rc = wait_event_interruptible(
2387 generic_waitq, !(device->stopped));
2388 if (rc == -ERESTARTSYS) {
2389 cqr->status = DASD_CQR_FAILED;
2390 maincqr->intrc = rc;
2391 continue;
2392 }
2393 } else
2394 wait_event(generic_waitq, !(device->stopped));
2395 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002396 if (!cqr->callback)
2397 cqr->callback = dasd_wakeup_cb;
2398
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002399 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002400 dasd_add_request_tail(cqr);
2401 if (interruptible) {
2402 rc = wait_event_interruptible(
2403 generic_waitq, _wait_for_wakeup(cqr));
2404 if (rc == -ERESTARTSYS) {
2405 dasd_cancel_req(cqr);
2406 /* wait (non-interruptible) for final status */
2407 wait_event(generic_waitq,
2408 _wait_for_wakeup(cqr));
2409 cqr->status = DASD_CQR_FAILED;
2410 maincqr->intrc = rc;
2411 continue;
2412 }
2413 } else
2414 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2415 }
2416
Heiko Carstens1aae0562013-01-30 09:49:40 +01002417 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002418 if ((maincqr->status != DASD_CQR_DONE) &&
2419 (maincqr->intrc != -ERESTARTSYS))
2420 dasd_log_sense(maincqr, &maincqr->irb);
2421 if (maincqr->status == DASD_CQR_DONE)
2422 rc = 0;
2423 else if (maincqr->intrc)
2424 rc = maincqr->intrc;
2425 else
2426 rc = -EIO;
2427 return rc;
2428}
2429
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002430static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2431{
2432 struct dasd_ccw_req *cqr;
2433
2434 list_for_each_entry(cqr, ccw_queue, blocklist) {
2435 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2436 return 0;
2437 }
2438
2439 return 1;
2440}
2441
2442static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2443{
2444 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002445 struct dasd_ccw_req *cqr, *n;
Jan Höppner8fd57522015-08-19 13:41:20 +02002446 u8 *sense = NULL;
Stefan Haberland362ce842014-10-01 13:04:54 +02002447 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002448
2449retry:
2450 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2451 device = cqr->startdev;
2452 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2453 continue;
2454
2455 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2456 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2457 cqr->status = DASD_CQR_FAILED;
2458 cqr->intrc = -EPERM;
2459 continue;
2460 }
2461 /*Non-temporary stop condition will trigger fail fast*/
2462 if (device->stopped & ~DASD_STOPPED_PENDING &&
2463 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2464 !dasd_eer_enabled(device)) {
2465 cqr->status = DASD_CQR_FAILED;
2466 cqr->intrc = -EAGAIN;
2467 continue;
2468 }
2469
2470 /*Don't try to start requests if device is stopped*/
2471 if (interruptible) {
2472 rc = wait_event_interruptible(
2473 generic_waitq, !device->stopped);
2474 if (rc == -ERESTARTSYS) {
2475 cqr->status = DASD_CQR_FAILED;
2476 cqr->intrc = rc;
2477 continue;
2478 }
2479 } else
2480 wait_event(generic_waitq, !(device->stopped));
2481
2482 if (!cqr->callback)
2483 cqr->callback = dasd_wakeup_cb;
2484 cqr->callback_data = DASD_SLEEPON_START_TAG;
2485 dasd_add_request_tail(cqr);
2486 }
2487
2488 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2489
2490 rc = 0;
2491 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002492 /*
Jan Höppner8fd57522015-08-19 13:41:20 +02002493 * In some cases the 'File Protected' or 'Incorrect Length'
2494 * error might be expected and error recovery would be
2495 * unnecessary in these cases. Check if the according suppress
2496 * bit is set.
2497 */
2498 sense = dasd_get_sense(&cqr->irb);
2499 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2500 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2501 continue;
2502 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2503 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2504 continue;
2505
2506 /*
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002507 * for alias devices simplify error recovery and
2508 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002509 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002510 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002511 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002512 (cqr->status == DASD_CQR_TERMINATED ||
2513 cqr->status == DASD_CQR_NEED_ERP))
2514 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002515
2516 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002517 if (__dasd_sleep_on_erp(cqr))
2518 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002519 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002520 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002521
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002522 return 0;
2523}
2524
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002525/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002526 * Queue a request to the tail of the device ccw_queue and wait for
2527 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002529int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002531 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002533EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002536 * Start requests from a ccw_queue and wait for their completion.
2537 */
2538int dasd_sleep_on_queue(struct list_head *ccw_queue)
2539{
2540 return _dasd_sleep_on_queue(ccw_queue, 0);
2541}
2542EXPORT_SYMBOL(dasd_sleep_on_queue);
2543
2544/*
Jan Höppnerbcf36762019-06-27 14:33:17 +02002545 * Start requests from a ccw_queue and wait interruptible for their completion.
2546 */
2547int dasd_sleep_on_queue_interruptible(struct list_head *ccw_queue)
2548{
2549 return _dasd_sleep_on_queue(ccw_queue, 1);
2550}
2551EXPORT_SYMBOL(dasd_sleep_on_queue_interruptible);
2552
2553/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002554 * Queue a request to the tail of the device ccw_queue and wait
2555 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002557int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002559 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002561EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563/*
2564 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2565 * for eckd devices) the currently running request has to be terminated
2566 * and be put back to status queued, before the special request is added
2567 * to the head of the queue. Then the special request is waited on normally.
2568 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002569static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
2571 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002572 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 if (list_empty(&device->ccw_queue))
2575 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002576 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002577 rc = device->discipline->term_IO(cqr);
2578 if (!rc)
2579 /*
2580 * CQR terminated because a more important request is pending.
2581 * Undo decreasing of retry counter because this is
2582 * not an error case.
2583 */
2584 cqr->retries++;
2585 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002588int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 struct dasd_device *device;
2591 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002592
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002593 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002594 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2595 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2596 cqr->status = DASD_CQR_FAILED;
2597 cqr->intrc = -EPERM;
2598 return -EIO;
2599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 spin_lock_irq(get_ccwdev_lock(device->cdev));
2601 rc = _dasd_term_running_cqr(device);
2602 if (rc) {
2603 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2604 return rc;
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002607 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002609 /*
2610 * add new request as second
2611 * first the terminated cqr needs to be finished
2612 */
2613 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002616 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2619
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002620 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002621
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002622 if (cqr->status == DASD_CQR_DONE)
2623 rc = 0;
2624 else if (cqr->intrc)
2625 rc = cqr->intrc;
2626 else
2627 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002628
2629 /* kick tasklets */
2630 dasd_schedule_device_bh(device);
2631 if (device->block)
2632 dasd_schedule_block_bh(device->block);
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return rc;
2635}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002636EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638/*
2639 * Cancels a request that was started with dasd_sleep_on_req.
2640 * This is useful to timeout requests. The request will be
2641 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002642 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002643 * negative error code if termination failed
2644 * Cancellation of a request is an asynchronous operation! The calling
2645 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 */
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002647static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002649 struct dasd_device *device = cqr->startdev;
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002650 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 switch (cqr->status) {
2653 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002654 /* request was not started - just set to cleared */
2655 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 break;
2657 case DASD_CQR_IN_IO:
2658 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002659 rc = device->discipline->term_IO(cqr);
2660 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002661 dev_err(&device->cdev->dev,
2662 "Cancelling request %p failed with rc=%d\n",
2663 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002664 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002665 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002668 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002671 dasd_schedule_device_bh(device);
2672 return rc;
2673}
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002674
2675int dasd_cancel_req(struct dasd_ccw_req *cqr)
2676{
2677 struct dasd_device *device = cqr->startdev;
2678 unsigned long flags;
2679 int rc;
2680
2681 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2682 rc = __dasd_cancel_req(cqr);
2683 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2684 return rc;
2685}
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002686
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002687/*
2688 * SECTION: Operations of the dasd_block layer.
2689 */
2690
2691/*
2692 * Timeout function for dasd_block. This is used when the block layer
2693 * is waiting for something that may not come reliably, (e.g. a state
2694 * change interrupt)
2695 */
Kees Cooke99e88a2017-10-16 14:43:17 -07002696static void dasd_block_timeout(struct timer_list *t)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002697{
2698 unsigned long flags;
2699 struct dasd_block *block;
2700
Kees Cooke99e88a2017-10-16 14:43:17 -07002701 block = from_timer(block, t, timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002702 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2703 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002704 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002705 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2706 dasd_schedule_block_bh(block);
Stefan Haberlande4433432017-05-22 10:59:11 +02002707 blk_mq_run_hw_queues(block->request_queue, true);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002708}
2709
2710/*
2711 * Setup timeout for a dasd_block in jiffies.
2712 */
2713void dasd_block_set_timer(struct dasd_block *block, int expires)
2714{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002715 if (expires == 0)
2716 del_timer(&block->timer);
2717 else
2718 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002719}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002720EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002721
2722/*
2723 * Clear timeout for a dasd_block.
2724 */
2725void dasd_block_clear_timer(struct dasd_block *block)
2726{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002727 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002728}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002729EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002730
2731/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002732 * Process finished error recovery ccw.
2733 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002734static void __dasd_process_erp(struct dasd_device *device,
2735 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002736{
2737 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002738
2739 if (cqr->status == DASD_CQR_DONE)
2740 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2741 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002742 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002743 erp_fn = device->discipline->erp_postaction(cqr);
2744 erp_fn(cqr);
2745}
2746
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002747static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2748{
2749 struct request *req;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002750 blk_status_t error = BLK_STS_OK;
Stefan Haberlande4433432017-05-22 10:59:11 +02002751 int status;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002752
2753 req = (struct request *) cqr->callback_data;
2754 dasd_profile_end(cqr->block, cqr, req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002755
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002756 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002757 if (status < 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002758 error = errno_to_blk_status(status);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002759 else if (status == 0) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002760 switch (cqr->intrc) {
2761 case -EPERM:
2762 error = BLK_STS_NEXUS;
2763 break;
2764 case -ENOLINK:
2765 error = BLK_STS_TRANSPORT;
2766 break;
2767 case -ETIMEDOUT:
2768 error = BLK_STS_TIMEOUT;
2769 break;
2770 default:
2771 error = BLK_STS_IOERR;
2772 break;
2773 }
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002774 }
Stefan Haberlande4433432017-05-22 10:59:11 +02002775
2776 /*
2777 * We need to take care for ETIMEDOUT errors here since the
2778 * complete callback does not get called in this case.
2779 * Take care of all errors here and avoid additional code to
2780 * transfer the error value to the complete callback.
2781 */
2782 if (error) {
2783 blk_mq_end_request(req, error);
2784 blk_mq_run_hw_queues(req->q, true);
2785 } else {
2786 blk_mq_complete_request(req);
2787 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002788}
2789
2790/*
2791 * Process ccw request queue.
2792 */
2793static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2794 struct list_head *final_queue)
2795{
2796 struct list_head *l, *n;
2797 struct dasd_ccw_req *cqr;
2798 dasd_erp_fn_t erp_fn;
2799 unsigned long flags;
2800 struct dasd_device *base = block->base;
2801
2802restart:
2803 /* Process request with final status. */
2804 list_for_each_safe(l, n, &block->ccw_queue) {
2805 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2806 if (cqr->status != DASD_CQR_DONE &&
2807 cqr->status != DASD_CQR_FAILED &&
2808 cqr->status != DASD_CQR_NEED_ERP &&
2809 cqr->status != DASD_CQR_TERMINATED)
2810 continue;
2811
2812 if (cqr->status == DASD_CQR_TERMINATED) {
2813 base->discipline->handle_terminated_request(cqr);
2814 goto restart;
2815 }
2816
2817 /* Process requests that may be recovered */
2818 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002819 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002820 if (IS_ERR(erp_fn(cqr)))
2821 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002822 goto restart;
2823 }
2824
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002825 /* log sense for fatal error */
2826 if (cqr->status == DASD_CQR_FAILED) {
2827 dasd_log_sense(cqr, &cqr->irb);
2828 }
2829
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002830 /* First of all call extended error reporting. */
2831 if (dasd_eer_enabled(base) &&
2832 cqr->status == DASD_CQR_FAILED) {
2833 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2834
2835 /* restart request */
2836 cqr->status = DASD_CQR_FILLED;
2837 cqr->retries = 255;
2838 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002839 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002840 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2841 flags);
2842 goto restart;
2843 }
2844
2845 /* Process finished ERP request. */
2846 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002847 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002848 goto restart;
2849 }
2850
2851 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002852 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002853 list_move_tail(&cqr->blocklist, final_queue);
2854 }
2855}
2856
2857static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2858{
2859 dasd_schedule_block_bh(cqr->block);
2860}
2861
2862static void __dasd_block_start_head(struct dasd_block *block)
2863{
2864 struct dasd_ccw_req *cqr;
2865
2866 if (list_empty(&block->ccw_queue))
2867 return;
2868 /* We allways begin with the first requests on the queue, as some
2869 * of previously started requests have to be enqueued on a
2870 * dasd_device again for error recovery.
2871 */
2872 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2873 if (cqr->status != DASD_CQR_FILLED)
2874 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002875 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2876 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2877 cqr->status = DASD_CQR_FAILED;
2878 cqr->intrc = -EPERM;
2879 dasd_schedule_block_bh(block);
2880 continue;
2881 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002882 /* Non-temporary stop condition will trigger fail fast */
2883 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2884 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2885 (!dasd_eer_enabled(block->base))) {
2886 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002887 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002888 dasd_schedule_block_bh(block);
2889 continue;
2890 }
2891 /* Don't try to start requests if device is stopped */
2892 if (block->base->stopped)
2893 return;
2894
2895 /* just a fail safe check, should not happen */
2896 if (!cqr->startdev)
2897 cqr->startdev = block->base;
2898
2899 /* make sure that the requests we submit find their way back */
2900 cqr->callback = dasd_return_cqr_cb;
2901
2902 dasd_add_request_tail(cqr);
2903 }
2904}
2905
2906/*
2907 * Central dasd_block layer routine. Takes requests from the generic
2908 * block layer request queue, creates ccw requests, enqueues them on
2909 * a dasd_device and processes ccw requests that have been returned.
2910 */
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002911static void dasd_block_tasklet(unsigned long data)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002912{
Sebastian Ottd2f2df62018-07-03 13:06:45 +02002913 struct dasd_block *block = (struct dasd_block *) data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002914 struct list_head final_queue;
2915 struct list_head *l, *n;
2916 struct dasd_ccw_req *cqr;
Stefan Haberlande4433432017-05-22 10:59:11 +02002917 struct dasd_queue *dq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002918
2919 atomic_set(&block->tasklet_scheduled, 0);
2920 INIT_LIST_HEAD(&final_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02002921 spin_lock_irq(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002922 /* Finish off requests on ccw queue */
2923 __dasd_process_block_ccw_queue(block, &final_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02002924 spin_unlock_irq(&block->queue_lock);
2925
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002926 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002927 list_for_each_safe(l, n, &final_queue) {
2928 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
Stefan Haberlande4433432017-05-22 10:59:11 +02002929 dq = cqr->dq;
2930 spin_lock_irq(&dq->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002931 list_del_init(&cqr->blocklist);
2932 __dasd_cleanup_cqr(cqr);
Stefan Haberlande4433432017-05-22 10:59:11 +02002933 spin_unlock_irq(&dq->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002934 }
Stefan Haberlande4433432017-05-22 10:59:11 +02002935
2936 spin_lock_irq(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002937 /* Now check if the head of the ccw queue needs to be started. */
2938 __dasd_block_start_head(block);
Stefan Haberlande4433432017-05-22 10:59:11 +02002939 spin_unlock_irq(&block->queue_lock);
2940
Stefan Haberland4679e892012-06-19 17:30:12 +02002941 if (waitqueue_active(&shutdown_waitq))
2942 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002943 dasd_put_device(block->base);
2944}
2945
2946static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2947{
2948 wake_up(&dasd_flush_wq);
2949}
2950
2951/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002952 * Requeue a request back to the block request queue
2953 * only works for block requests
2954 */
2955static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2956{
2957 struct dasd_block *block = cqr->block;
2958 struct request *req;
Stefan Haberlandc5576872013-04-15 16:41:31 +02002959
2960 if (!block)
2961 return -EINVAL;
Stefan Haberlande4433432017-05-22 10:59:11 +02002962 spin_lock_irq(&cqr->dq->lock);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002963 req = (struct request *) cqr->callback_data;
Stefan Haberlande4433432017-05-22 10:59:11 +02002964 blk_mq_requeue_request(req, false);
2965 spin_unlock_irq(&cqr->dq->lock);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002966
2967 return 0;
2968}
2969
2970/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002971 * Go through all request on the dasd_block request queue, cancel them
2972 * on the respective dasd_device, and return them to the generic
2973 * block layer.
2974 */
2975static int dasd_flush_block_queue(struct dasd_block *block)
2976{
2977 struct dasd_ccw_req *cqr, *n;
2978 int rc, i;
2979 struct list_head flush_queue;
Stefan Haberlande4433432017-05-22 10:59:11 +02002980 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002981
2982 INIT_LIST_HEAD(&flush_queue);
2983 spin_lock_bh(&block->queue_lock);
2984 rc = 0;
2985restart:
2986 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2987 /* if this request currently owned by a dasd_device cancel it */
2988 if (cqr->status >= DASD_CQR_QUEUED)
2989 rc = dasd_cancel_req(cqr);
2990 if (rc < 0)
2991 break;
2992 /* Rechain request (including erp chain) so it won't be
2993 * touched by the dasd_block_tasklet anymore.
2994 * Replace the callback so we notice when the request
2995 * is returned from the dasd_device layer.
2996 */
2997 cqr->callback = _dasd_wake_block_flush_cb;
2998 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2999 list_move_tail(&cqr->blocklist, &flush_queue);
3000 if (i > 1)
3001 /* moved more than one request - need to restart */
3002 goto restart;
3003 }
3004 spin_unlock_bh(&block->queue_lock);
3005 /* Now call the callback function of flushed requests */
3006restart_cb:
3007 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
3008 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
3009 /* Process finished ERP request. */
3010 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003011 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003012 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003013 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003014 /* restart list_for_xx loop since dasd_process_erp
3015 * might remove multiple elements */
3016 goto restart_cb;
3017 }
3018 /* call the callback function */
Stefan Haberlande4433432017-05-22 10:59:11 +02003019 spin_lock_irqsave(&cqr->dq->lock, flags);
Heiko Carstens1aae0562013-01-30 09:49:40 +01003020 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003021 list_del_init(&cqr->blocklist);
3022 __dasd_cleanup_cqr(cqr);
Stefan Haberlande4433432017-05-22 10:59:11 +02003023 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return rc;
3026}
3027
3028/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003029 * Schedules a call to dasd_tasklet over the device tasklet.
3030 */
3031void dasd_schedule_block_bh(struct dasd_block *block)
3032{
3033 /* Protect against rescheduling. */
3034 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3035 return;
3036 /* life cycle of block is bound to it's base device */
3037 dasd_get_device(block->base);
3038 tasklet_hi_schedule(&block->tasklet);
3039}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003040EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003041
3042
3043/*
3044 * SECTION: external block device operations
3045 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 */
3047
3048/*
3049 * Dasd request queue function. Called from ll_rw_blk.c
3050 */
Stefan Haberlande4433432017-05-22 10:59:11 +02003051static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
3052 const struct blk_mq_queue_data *qd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Stefan Haberlande4433432017-05-22 10:59:11 +02003054 struct dasd_block *block = hctx->queue->queuedata;
3055 struct dasd_queue *dq = hctx->driver_data;
3056 struct request *req = qd->rq;
3057 struct dasd_device *basedev;
3058 struct dasd_ccw_req *cqr;
3059 blk_status_t rc = BLK_STS_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Stefan Haberlande4433432017-05-22 10:59:11 +02003061 basedev = block->base;
3062 spin_lock_irq(&dq->lock);
3063 if (basedev->state < DASD_STATE_READY) {
3064 DBF_DEV_EVENT(DBF_ERR, basedev,
3065 "device not ready for request %p", req);
3066 rc = BLK_STS_IOERR;
3067 goto out;
3068 }
3069
3070 /*
3071 * if device is stopped do not fetch new requests
3072 * except failfast is active which will let requests fail
3073 * immediately in __dasd_block_start_head()
3074 */
3075 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
3076 DBF_DEV_EVENT(DBF_ERR, basedev,
3077 "device stopped request %p", req);
3078 rc = BLK_STS_RESOURCE;
3079 goto out;
3080 }
3081
3082 if (basedev->features & DASD_FEATURE_READONLY &&
3083 rq_data_dir(req) == WRITE) {
3084 DBF_DEV_EVENT(DBF_ERR, basedev,
3085 "Rejecting write request %p", req);
3086 rc = BLK_STS_IOERR;
3087 goto out;
3088 }
3089
3090 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
3091 (basedev->features & DASD_FEATURE_FAILFAST ||
3092 blk_noretry_request(req))) {
3093 DBF_DEV_EVENT(DBF_ERR, basedev,
3094 "Rejecting failfast request %p", req);
3095 rc = BLK_STS_IOERR;
3096 goto out;
3097 }
3098
3099 cqr = basedev->discipline->build_cp(basedev, block, req);
3100 if (IS_ERR(cqr)) {
3101 if (PTR_ERR(cqr) == -EBUSY ||
3102 PTR_ERR(cqr) == -ENOMEM ||
3103 PTR_ERR(cqr) == -EAGAIN) {
3104 rc = BLK_STS_RESOURCE;
3105 goto out;
3106 }
3107 DBF_DEV_EVENT(DBF_ERR, basedev,
3108 "CCW creation failed (rc=%ld) on request %p",
3109 PTR_ERR(cqr), req);
3110 rc = BLK_STS_IOERR;
3111 goto out;
3112 }
3113 /*
3114 * Note: callback is set to dasd_return_cqr_cb in
3115 * __dasd_block_start_head to cover erp requests as well
3116 */
3117 cqr->callback_data = req;
3118 cqr->status = DASD_CQR_FILLED;
3119 cqr->dq = dq;
Sebastian Ottf0f59a22018-05-15 14:05:13 +02003120
Stefan Haberlande4433432017-05-22 10:59:11 +02003121 blk_mq_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003122 spin_lock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02003123 list_add_tail(&cqr->blocklist, &block->ccw_queue);
3124 INIT_LIST_HEAD(&cqr->devlist);
3125 dasd_profile_start(block, cqr, req);
3126 dasd_schedule_block_bh(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003127 spin_unlock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02003128
3129out:
3130 spin_unlock_irq(&dq->lock);
3131 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
3134/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00003135 * Block timeout callback, called from the block layer
3136 *
Hannes Reineckea2ace462013-01-30 09:26:14 +00003137 * Return values:
3138 * BLK_EH_RESET_TIMER if the request should be left running
Christoph Hellwig66005932018-05-29 15:52:29 +02003139 * BLK_EH_DONE if the request is handled or terminated
Hannes Reineckea2ace462013-01-30 09:26:14 +00003140 * by the driver.
3141 */
Stefan Haberlande4433432017-05-22 10:59:11 +02003142enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
Hannes Reineckea2ace462013-01-30 09:26:14 +00003143{
Hannes Reineckea2ace462013-01-30 09:26:14 +00003144 struct dasd_block *block = req->q->queuedata;
3145 struct dasd_device *device;
Sebastian Ottf0f59a22018-05-15 14:05:13 +02003146 struct dasd_ccw_req *cqr;
Stefan Haberlande4433432017-05-22 10:59:11 +02003147 unsigned long flags;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003148 int rc = 0;
3149
Sebastian Ottc5205f22018-06-04 19:07:39 +02003150 cqr = blk_mq_rq_to_pdu(req);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003151 if (!cqr)
Christoph Hellwig66005932018-05-29 15:52:29 +02003152 return BLK_EH_DONE;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003153
Stefan Haberlande4433432017-05-22 10:59:11 +02003154 spin_lock_irqsave(&cqr->dq->lock, flags);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003155 device = cqr->startdev ? cqr->startdev : block->base;
Stefan Haberlande4433432017-05-22 10:59:11 +02003156 if (!device->blk_timeout) {
3157 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00003158 return BLK_EH_RESET_TIMER;
Stefan Haberlande4433432017-05-22 10:59:11 +02003159 }
Hannes Reineckea2ace462013-01-30 09:26:14 +00003160 DBF_DEV_EVENT(DBF_WARNING, device,
3161 " dasd_times_out cqr %p status %x",
3162 cqr, cqr->status);
3163
3164 spin_lock(&block->queue_lock);
3165 spin_lock(get_ccwdev_lock(device->cdev));
3166 cqr->retries = -1;
3167 cqr->intrc = -ETIMEDOUT;
3168 if (cqr->status >= DASD_CQR_QUEUED) {
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003169 rc = __dasd_cancel_req(cqr);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003170 } else if (cqr->status == DASD_CQR_FILLED ||
3171 cqr->status == DASD_CQR_NEED_ERP) {
3172 cqr->status = DASD_CQR_TERMINATED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003173 } else if (cqr->status == DASD_CQR_IN_ERP) {
3174 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3175
3176 list_for_each_entry_safe(searchcqr, nextcqr,
3177 &block->ccw_queue, blocklist) {
3178 tmpcqr = searchcqr;
3179 while (tmpcqr->refers)
3180 tmpcqr = tmpcqr->refers;
3181 if (tmpcqr != cqr)
3182 continue;
3183 /* searchcqr is an ERP request for cqr */
3184 searchcqr->retries = -1;
3185 searchcqr->intrc = -ETIMEDOUT;
3186 if (searchcqr->status >= DASD_CQR_QUEUED) {
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003187 rc = __dasd_cancel_req(searchcqr);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003188 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3189 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3190 searchcqr->status = DASD_CQR_TERMINATED;
3191 rc = 0;
3192 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3193 /*
3194 * Shouldn't happen; most recent ERP
3195 * request is at the front of queue
3196 */
3197 continue;
3198 }
3199 break;
3200 }
Hannes Reineckea2ace462013-01-30 09:26:14 +00003201 }
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003202 spin_unlock(get_ccwdev_lock(device->cdev));
Hannes Reineckea2ace462013-01-30 09:26:14 +00003203 dasd_schedule_block_bh(block);
3204 spin_unlock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02003205 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003206
Christoph Hellwig66005932018-05-29 15:52:29 +02003207 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003208}
3209
Stefan Haberlande4433432017-05-22 10:59:11 +02003210static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3211 unsigned int idx)
3212{
3213 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3214
3215 if (!dq)
3216 return -ENOMEM;
3217
3218 spin_lock_init(&dq->lock);
3219 hctx->driver_data = dq;
3220
3221 return 0;
3222}
3223
3224static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3225{
3226 kfree(hctx->driver_data);
3227 hctx->driver_data = NULL;
3228}
3229
3230static void dasd_request_done(struct request *req)
3231{
3232 blk_mq_end_request(req, 0);
3233 blk_mq_run_hw_queues(req->q, true);
3234}
3235
3236static struct blk_mq_ops dasd_mq_ops = {
3237 .queue_rq = do_dasd_request,
3238 .complete = dasd_request_done,
3239 .timeout = dasd_times_out,
3240 .init_hctx = dasd_init_hctx,
3241 .exit_hctx = dasd_exit_hctx,
3242};
3243
Hannes Reineckea2ace462013-01-30 09:26:14 +00003244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 * Allocate and initialize request queue and default I/O scheduler.
3246 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003247static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Stefan Haberlande4433432017-05-22 10:59:11 +02003249 int rc;
3250
3251 block->tag_set.ops = &dasd_mq_ops;
Sebastian Ottc5205f22018-06-04 19:07:39 +02003252 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
Stefan Haberland3284da32018-06-12 16:47:10 +02003253 block->tag_set.nr_hw_queues = nr_hw_queues;
3254 block->tag_set.queue_depth = queue_depth;
Stefan Haberlande4433432017-05-22 10:59:11 +02003255 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Vasily Gorbikb17e3ab2018-06-24 09:21:59 +02003256 block->tag_set.numa_node = NUMA_NO_NODE;
Stefan Haberlande4433432017-05-22 10:59:11 +02003257
3258 rc = blk_mq_alloc_tag_set(&block->tag_set);
3259 if (rc)
3260 return rc;
3261
3262 block->request_queue = blk_mq_init_queue(&block->tag_set);
3263 if (IS_ERR(block->request_queue))
3264 return PTR_ERR(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003266 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01003268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
3271/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 * Deactivate and free request queue.
3273 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003274static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003276 if (block->request_queue) {
3277 blk_cleanup_queue(block->request_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02003278 blk_mq_free_tag_set(&block->tag_set);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003279 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281}
3282
Al Viro57a7c0b2008-03-02 10:36:08 -05003283static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003285 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 int rc;
3287
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003288 base = dasd_device_from_gendisk(bdev->bd_disk);
3289 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003290 return -ENODEV;
3291
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003292 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003293 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 rc = -ENODEV;
3295 goto unlock;
3296 }
3297
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003298 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 rc = -EINVAL;
3300 goto unlock;
3301 }
3302
3303 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003304 dev_info(&base->cdev->dev,
3305 "Accessing the DASD failed because it is in "
3306 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 rc = -EPERM;
3308 goto out;
3309 }
3310
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003311 if (base->state <= DASD_STATE_BASIC) {
3312 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 " Cannot open unrecognized device");
3314 rc = -ENODEV;
3315 goto out;
3316 }
3317
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003318 if ((mode & FMODE_WRITE) &&
3319 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3320 (base->features & DASD_FEATURE_READONLY))) {
3321 rc = -EROFS;
3322 goto out;
3323 }
3324
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003325 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return 0;
3327
3328out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003329 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003331 atomic_dec(&base->block->open_count);
3332 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return rc;
3334}
3335
Al Virodb2a1442013-05-05 21:52:57 -04003336static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Al Virodb2a1442013-05-05 21:52:57 -04003338 struct dasd_device *base = dasd_device_from_gendisk(disk);
3339 if (base) {
3340 atomic_dec(&base->block->open_count);
3341 module_put(base->discipline->owner);
3342 dasd_put_device(base);
3343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003346/*
3347 * Return disk geometry.
3348 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003349static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003350{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003351 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003352
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003353 base = dasd_device_from_gendisk(bdev->bd_disk);
3354 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003355 return -ENODEV;
3356
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003357 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003358 !base->discipline->fill_geometry) {
3359 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003360 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003361 }
3362 base->discipline->fill_geometry(base->block, geo);
3363 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3364 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003365 return 0;
3366}
3367
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003368const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369dasd_device_operations = {
3370 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003371 .open = dasd_open,
3372 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003373 .ioctl = dasd_ioctl,
3374 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003375 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376};
3377
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003378/*******************************************************************************
3379 * end of block device operations
3380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382static void
3383dasd_exit(void)
3384{
3385#ifdef CONFIG_PROC_FS
3386 dasd_proc_exit();
3387#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003388 dasd_eer_exit();
Chengguang Xu734781a2018-08-28 07:51:30 +08003389 kmem_cache_destroy(dasd_page_cache);
3390 dasd_page_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 dasd_gendisk_exit();
3392 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 if (dasd_debug_area != NULL) {
3394 debug_unregister(dasd_debug_area);
3395 dasd_debug_area = NULL;
3396 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003397 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
3400/*
3401 * SECTION: common functions for ccw_driver use
3402 */
3403
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003404/*
3405 * Is the device read-only?
3406 * Note that this function does not report the setting of the
3407 * readonly device attribute, but how it is configured in z/VM.
3408 */
3409int dasd_device_is_ro(struct dasd_device *device)
3410{
3411 struct ccw_dev_id dev_id;
3412 struct diag210 diag_data;
3413 int rc;
3414
3415 if (!MACHINE_IS_VM)
3416 return 0;
3417 ccw_device_get_id(device->cdev, &dev_id);
3418 memset(&diag_data, 0, sizeof(diag_data));
3419 diag_data.vrdcdvno = dev_id.devno;
3420 diag_data.vrdclen = sizeof(diag_data);
3421 rc = diag210(&diag_data);
3422 if (rc == 0 || rc == 2) {
3423 return diag_data.vrdcvfla & 0x80;
3424 } else {
3425 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3426 dev_id.devno, rc);
3427 return 0;
3428 }
3429}
3430EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3431
Cornelia Huckf3445a12009-04-14 15:36:23 +02003432static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3433{
3434 struct ccw_device *cdev = data;
3435 int ret;
3436
3437 ret = ccw_device_set_online(cdev);
3438 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003439 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3440 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003441}
3442
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003443/*
3444 * Initial attempt at a probe function. this can be simplified once
3445 * the other detection code is gone.
3446 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003447int dasd_generic_probe(struct ccw_device *cdev,
3448 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
3450 int ret;
3451
3452 ret = dasd_add_sysfs_files(cdev);
3453 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003454 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3455 "dasd_generic_probe: could not add "
3456 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
Horst Hummel40545572006-06-29 15:08:18 +02003459 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Horst Hummel40545572006-06-29 15:08:18 +02003461 /*
3462 * Automatically online either all dasd devices (dasd_autodetect)
3463 * or all devices specified with dasd= parameters during
3464 * initial probe.
3465 */
3466 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003467 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003468 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003471EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Stefan Haberlandc020d722016-09-20 10:42:38 +02003473void dasd_generic_free_discipline(struct dasd_device *device)
3474{
3475 /* Forget the discipline information. */
3476 if (device->discipline) {
3477 if (device->discipline->uncheck_device)
3478 device->discipline->uncheck_device(device);
3479 module_put(device->discipline->owner);
3480 device->discipline = NULL;
3481 }
3482 if (device->base_discipline) {
3483 module_put(device->base_discipline->owner);
3484 device->base_discipline = NULL;
3485 }
3486}
3487EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3488
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003489/*
3490 * This will one day be called from a global not_oper handler.
3491 * It is also used by driver_unregister during module unload.
3492 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003493void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
3495 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003496 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Horst Hummel59afda72005-05-16 21:53:39 -07003498 cdev->handler = NULL;
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003501 if (IS_ERR(device)) {
3502 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003504 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003505 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3506 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 /* Already doing offline processing */
3508 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003509 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return;
3511 }
3512 /*
3513 * This device is removed unconditionally. Set offline
3514 * flag to prevent dasd_open from opening it while it is
3515 * no quite down yet.
3516 */
3517 dasd_set_target_state(device, DASD_STATE_NEW);
3518 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003519 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003521 /*
3522 * life cycle of block is bound to device, so delete it after
3523 * device was safely removed
3524 */
3525 if (block)
3526 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003527
3528 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003530EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003532/*
3533 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003535 * or the user has started activation through sysfs.
3536 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003537int dasd_generic_set_online(struct ccw_device *cdev,
3538 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003540 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003542 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003543
Horst Hummel40545572006-06-29 15:08:18 +02003544 /* first online clears initial online feature flag */
3545 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 device = dasd_create_device(cdev);
3547 if (IS_ERR(device))
3548 return PTR_ERR(device);
3549
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003550 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003551 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003553 /* Try to load the required module. */
3554 rc = request_module(DASD_DIAG_MOD);
3555 if (rc) {
3556 pr_warn("%s Setting the DASD online failed "
3557 "because the required module %s "
3558 "could not be loaded (rc=%d)\n",
3559 dev_name(&cdev->dev), DASD_DIAG_MOD,
3560 rc);
3561 dasd_delete_device(device);
3562 return -ENODEV;
3563 }
3564 }
3565 /* Module init could have failed, so check again here after
3566 * request_module(). */
3567 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003568 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3569 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 dasd_delete_device(device);
3571 return -ENODEV;
3572 }
3573 discipline = dasd_diag_discipline_pointer;
3574 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003575 if (!try_module_get(base_discipline->owner)) {
3576 dasd_delete_device(device);
3577 return -EINVAL;
3578 }
3579 if (!try_module_get(discipline->owner)) {
3580 module_put(base_discipline->owner);
3581 dasd_delete_device(device);
3582 return -EINVAL;
3583 }
3584 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 device->discipline = discipline;
3586
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003587 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 rc = discipline->check_device(device);
3589 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003590 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3591 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003592 module_put(discipline->owner);
3593 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 dasd_delete_device(device);
3595 return rc;
3596 }
3597
3598 dasd_set_target_state(device, DASD_STATE_ONLINE);
3599 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003600 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3601 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 rc = -ENODEV;
3603 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003604 if (device->block)
3605 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 dasd_delete_device(device);
3607 } else
3608 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003609 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003610
3611 wait_event(dasd_init_waitq, _wait_for_device(device));
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return rc;
3615}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003616EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003618int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003621 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003622 int max_count, open_count, rc;
Jan Höppner0f57c972016-10-18 17:54:49 +02003623 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003625 rc = 0;
Jan Höppner0f57c972016-10-18 17:54:49 +02003626 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3627 device = dasd_device_from_cdev_locked(cdev);
3628 if (IS_ERR(device)) {
3629 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return PTR_ERR(device);
Jan Höppner0f57c972016-10-18 17:54:49 +02003631 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /*
3634 * We must make sure that this device is currently not in use.
3635 * The open_count is increased for every opener, that includes
3636 * the blkdev_get in dasd_scan_partitions. We are only interested
3637 * in the other openers.
3638 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003639 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003640 max_count = device->block->bdev ? 0 : -1;
3641 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003642 if (open_count > max_count) {
3643 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003644 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3645 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003646 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003647 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3648 dev_name(&cdev->dev));
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003649 rc = -EBUSY;
3650 goto out_err;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003653
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003654 /*
3655 * Test if the offline processing is already running and exit if so.
3656 * If a safe offline is being processed this could only be a normal
3657 * offline that should be able to overtake the safe offline and
3658 * cancel any I/O we do not want to wait for any longer
3659 */
3660 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3661 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3662 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3663 &device->flags);
3664 } else {
3665 rc = -EBUSY;
3666 goto out_err;
3667 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003668 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003669 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003670
3671 /*
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003672 * if safe_offline is called set safe_offline_running flag and
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003673 * clear safe_offline so that a call to normal offline
3674 * can overrun safe_offline processing
3675 */
3676 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3677 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003678 /* need to unlock here to wait for outstanding I/O */
3679 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003680 /*
3681 * If we want to set the device safe offline all IO operations
3682 * should be finished before continuing the offline process
3683 * so sync bdev first and then wait for our queues to become
3684 * empty
3685 */
Stefan Haberlandca732e12017-01-25 16:56:41 +01003686 if (device->block) {
3687 rc = fsync_bdev(device->block->bdev);
3688 if (rc != 0)
3689 goto interrupted;
3690 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003691 dasd_schedule_device_bh(device);
3692 rc = wait_event_interruptible(shutdown_waitq,
3693 _wait_for_empty_queues(device));
3694 if (rc != 0)
3695 goto interrupted;
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003696
3697 /*
3698 * check if a normal offline process overtook the offline
3699 * processing in this case simply do nothing beside returning
3700 * that we got interrupted
3701 * otherwise mark safe offline as not running any longer and
3702 * continue with normal offline
3703 */
3704 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3705 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3706 rc = -ERESTARTSYS;
3707 goto out_err;
3708 }
3709 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003710 }
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003711 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 dasd_set_target_state(device, DASD_STATE_NEW);
3714 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003715 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003717 /*
3718 * life cycle of block is bound to device, so delete it after
3719 * device was safely removed
3720 */
3721 if (block)
3722 dasd_free_block(block);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003725
3726interrupted:
3727 /* interrupted by signal */
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003728 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003729 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3730 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003731out_err:
Jan Höppner0f57c972016-10-18 17:54:49 +02003732 dasd_put_device(device);
3733 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003734 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003736EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003738int dasd_generic_last_path_gone(struct dasd_device *device)
3739{
3740 struct dasd_ccw_req *cqr;
3741
3742 dev_warn(&device->cdev->dev, "No operational channel path is left "
3743 "for the device\n");
3744 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3745 /* First of all call extended error reporting. */
3746 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3747
3748 if (device->state < DASD_STATE_BASIC)
3749 return 0;
3750 /* Device is active. We want to keep it. */
3751 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3752 if ((cqr->status == DASD_CQR_IN_IO) ||
3753 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3754 cqr->status = DASD_CQR_QUEUED;
3755 cqr->retries++;
3756 }
3757 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3758 dasd_device_clear_timer(device);
3759 dasd_schedule_device_bh(device);
3760 return 1;
3761}
3762EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3763
3764int dasd_generic_path_operational(struct dasd_device *device)
3765{
3766 dev_info(&device->cdev->dev, "A channel path to the device has become "
3767 "operational\n");
3768 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3769 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3770 if (device->stopped & DASD_UNRESUMED_PM) {
3771 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3772 dasd_restore_device(device);
3773 return 1;
3774 }
3775 dasd_schedule_device_bh(device);
Stefan Haberlande4433432017-05-22 10:59:11 +02003776 if (device->block) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003777 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02003778 if (device->block->request_queue)
3779 blk_mq_run_hw_queues(device->block->request_queue,
3780 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02003781 }
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003782
3783 if (!device->stopped)
3784 wake_up(&generic_waitq);
3785
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003786 return 1;
3787}
3788EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3789
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003790int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 int ret;
3794
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003795 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 if (IS_ERR(device))
3797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 ret = 0;
3799 switch (event) {
3800 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003801 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 case CIO_NO_PATH:
Stefan Haberlandc9346152016-08-08 15:53:54 +02003803 dasd_path_no_path(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003804 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 break;
3806 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 ret = 1;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003808 if (dasd_path_get_opm(device))
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003809 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 break;
3811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 dasd_put_device(device);
3813 return ret;
3814}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003815EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003817void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3818{
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003819 struct dasd_device *device;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003820 int chp, oldopm, hpfpm, ifccpm;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003821
3822 device = dasd_device_from_cdev_locked(cdev);
3823 if (IS_ERR(device))
3824 return;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003825
3826 oldopm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003827 for (chp = 0; chp < 8; chp++) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003828 if (path_event[chp] & PE_PATH_GONE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003829 dasd_path_notoper(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003830 }
3831 if (path_event[chp] & PE_PATH_AVAILABLE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003832 dasd_path_available(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003833 dasd_schedule_device_bh(device);
3834 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003835 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003836 if (!dasd_path_is_operational(device, chp) &&
3837 !dasd_path_need_verify(device, chp)) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003838 /*
3839 * we can not establish a pathgroup on an
3840 * unavailable path, so trigger a path
3841 * verification first
3842 */
Stefan Haberlandc9346152016-08-08 15:53:54 +02003843 dasd_path_available(device, chp);
3844 dasd_schedule_device_bh(device);
Stefan Haberland12d7b102012-09-11 15:10:58 +02003845 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003846 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3847 "Pathgroup re-established\n");
3848 if (device->discipline->kick_validate)
3849 device->discipline->kick_validate(device);
3850 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003851 }
Stefan Haberlanda521b042016-08-08 15:56:54 +02003852 hpfpm = dasd_path_get_hpfpm(device);
3853 ifccpm = dasd_path_get_ifccpm(device);
3854 if (!dasd_path_get_opm(device) && hpfpm) {
3855 /*
3856 * device has no operational paths but at least one path is
3857 * disabled due to HPF errors
3858 * disable HPF at all and use the path(s) again
3859 */
3860 if (device->discipline->disable_hpf)
3861 device->discipline->disable_hpf(device);
3862 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3863 dasd_path_set_tbvpm(device, hpfpm);
3864 dasd_schedule_device_bh(device);
3865 dasd_schedule_requeue(device);
3866 } else if (!dasd_path_get_opm(device) && ifccpm) {
3867 /*
3868 * device has no operational paths but at least one path is
3869 * disabled due to IFCC errors
3870 * trigger path verification on paths with IFCC errors
3871 */
3872 dasd_path_set_tbvpm(device, ifccpm);
3873 dasd_schedule_device_bh(device);
3874 }
3875 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003876 dev_warn(&device->cdev->dev,
3877 "No verified channel paths remain for the device\n");
3878 DBF_DEV_EVENT(DBF_WARNING, device,
3879 "%s", "last verified path gone");
3880 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3881 dasd_device_set_stop_bits(device,
3882 DASD_STOPPED_DC_WAIT);
3883 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003884 dasd_put_device(device);
3885}
3886EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3887
3888int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3889{
Stefan Haberlandc9346152016-08-08 15:53:54 +02003890 if (!dasd_path_get_opm(device) && lpm) {
3891 dasd_path_set_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003892 dasd_generic_path_operational(device);
3893 } else
Stefan Haberlandc9346152016-08-08 15:53:54 +02003894 dasd_path_add_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003895 return 0;
3896}
3897EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3898
Jan Höppner9e12e542018-07-03 10:56:51 +02003899void dasd_generic_space_exhaust(struct dasd_device *device,
3900 struct dasd_ccw_req *cqr)
3901{
3902 dasd_eer_write(device, NULL, DASD_EER_NOSPC);
3903
3904 if (device->state < DASD_STATE_BASIC)
3905 return;
3906
3907 if (cqr->status == DASD_CQR_IN_IO ||
3908 cqr->status == DASD_CQR_CLEAR_PENDING) {
3909 cqr->status = DASD_CQR_QUEUED;
3910 cqr->retries++;
3911 }
3912 dasd_device_set_stop_bits(device, DASD_STOPPED_NOSPC);
3913 dasd_device_clear_timer(device);
3914 dasd_schedule_device_bh(device);
3915}
3916EXPORT_SYMBOL_GPL(dasd_generic_space_exhaust);
3917
3918void dasd_generic_space_avail(struct dasd_device *device)
3919{
3920 dev_info(&device->cdev->dev, "Extent pool space is available\n");
3921 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "space available");
3922
3923 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOSPC);
3924 dasd_schedule_device_bh(device);
3925
3926 if (device->block) {
3927 dasd_schedule_block_bh(device->block);
3928 if (device->block->request_queue)
3929 blk_mq_run_hw_queues(device->block->request_queue, true);
3930 }
3931 if (!device->stopped)
3932 wake_up(&generic_waitq);
3933}
3934EXPORT_SYMBOL_GPL(dasd_generic_space_avail);
3935
Stefan Haberlanda521b042016-08-08 15:56:54 +02003936/*
3937 * clear active requests and requeue them to block layer if possible
3938 */
3939static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3940{
3941 struct list_head requeue_queue;
3942 struct dasd_ccw_req *cqr, *n;
3943 struct dasd_ccw_req *refers;
3944 int rc;
3945
3946 INIT_LIST_HEAD(&requeue_queue);
3947 spin_lock_irq(get_ccwdev_lock(device->cdev));
3948 rc = 0;
3949 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3950 /* Check status and move request to flush_queue */
3951 if (cqr->status == DASD_CQR_IN_IO) {
3952 rc = device->discipline->term_IO(cqr);
3953 if (rc) {
3954 /* unable to terminate requeust */
3955 dev_err(&device->cdev->dev,
3956 "Unable to terminate request %p "
3957 "on suspend\n", cqr);
3958 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3959 dasd_put_device(device);
3960 return rc;
3961 }
3962 }
3963 list_move_tail(&cqr->devlist, &requeue_queue);
3964 }
3965 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3966
3967 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3968 wait_event(dasd_flush_wq,
3969 (cqr->status != DASD_CQR_CLEAR_PENDING));
3970
Stefan Haberland9487cfd2018-02-07 17:39:14 +01003971 /*
3972 * requeue requests to blocklayer will only work
3973 * for block device requests
3974 */
3975 if (_dasd_requeue_request(cqr))
3976 continue;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003977
3978 /* remove requests from device and block queue */
3979 list_del_init(&cqr->devlist);
3980 while (cqr->refers != NULL) {
3981 refers = cqr->refers;
3982 /* remove the request from the block queue */
3983 list_del(&cqr->blocklist);
3984 /* free the finished erp request */
3985 dasd_free_erp_request(cqr, cqr->memdev);
3986 cqr = refers;
3987 }
3988
Stefan Haberlanda1fc8182018-02-19 12:24:39 +01003989 /*
3990 * _dasd_requeue_request already checked for a valid
3991 * blockdevice, no need to check again
3992 * all erp requests (cqr->refers) have a cqr->block
3993 * pointer copy from the original cqr
3994 */
3995 list_del_init(&cqr->blocklist);
Stefan Haberlanda521b042016-08-08 15:56:54 +02003996 cqr->block->base->discipline->free_cp(
3997 cqr, (struct request *) cqr->callback_data);
3998 }
3999
4000 /*
4001 * if requests remain then they are internal request
4002 * and go back to the device queue
4003 */
4004 if (!list_empty(&requeue_queue)) {
4005 /* move freeze_queue to start of the ccw_queue */
4006 spin_lock_irq(get_ccwdev_lock(device->cdev));
4007 list_splice_tail(&requeue_queue, &device->ccw_queue);
4008 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4009 }
Stefan Haberland9487cfd2018-02-07 17:39:14 +01004010 dasd_schedule_device_bh(device);
Stefan Haberlanda521b042016-08-08 15:56:54 +02004011 return rc;
4012}
4013
4014static void do_requeue_requests(struct work_struct *work)
4015{
4016 struct dasd_device *device = container_of(work, struct dasd_device,
4017 requeue_requests);
4018 dasd_generic_requeue_all_requests(device);
4019 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
4020 if (device->block)
4021 dasd_schedule_block_bh(device->block);
4022 dasd_put_device(device);
4023}
4024
4025void dasd_schedule_requeue(struct dasd_device *device)
4026{
4027 dasd_get_device(device);
4028 /* queue call to dasd_reload_device to the kernel event daemon. */
4029 if (!schedule_work(&device->requeue_requests))
4030 dasd_put_device(device);
4031}
4032EXPORT_SYMBOL(dasd_schedule_requeue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01004033
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004034int dasd_generic_pm_freeze(struct ccw_device *cdev)
4035{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004036 struct dasd_device *device = dasd_device_from_cdev(cdev);
4037
4038 if (IS_ERR(device))
4039 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01004040
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01004041 /* mark device as suspended */
4042 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
4043
Stefan Haberland6f272b92011-01-05 12:48:05 +01004044 if (device->discipline->freeze)
Sebastian Ott4bca6982017-06-26 19:26:55 +02004045 device->discipline->freeze(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01004046
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004047 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004048 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02004049
Stefan Haberlanda521b042016-08-08 15:56:54 +02004050 return dasd_generic_requeue_all_requests(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004051}
4052EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
4053
4054int dasd_generic_restore_device(struct ccw_device *cdev)
4055{
4056 struct dasd_device *device = dasd_device_from_cdev(cdev);
4057 int rc = 0;
4058
4059 if (IS_ERR(device))
4060 return PTR_ERR(device);
4061
Stefan Haberlande6125fb2009-06-22 12:08:17 +02004062 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004063 dasd_device_remove_stop_bits(device,
4064 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02004065
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004066 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004067
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004068 /*
4069 * call discipline restore function
4070 * if device is stopped do nothing e.g. for disconnected devices
4071 */
4072 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004073 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004074 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02004075 /*
4076 * if the resume failed for the DASD we put it in
4077 * an UNRESUMED stop state
4078 */
4079 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004080
Stefan Haberlande4433432017-05-22 10:59:11 +02004081 if (device->block) {
Stefan Haberland6fca97a2009-10-06 10:34:15 +02004082 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02004083 if (device->block->request_queue)
4084 blk_mq_run_hw_queues(device->block->request_queue,
4085 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02004086 }
Stefan Haberland6fca97a2009-10-06 10:34:15 +02004087
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01004088 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004089 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02004090 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004091}
4092EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
4093
Heiko Carstens763968e2007-05-10 15:45:46 +02004094static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
Heiko Carstens763968e2007-05-10 15:45:46 +02004095 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02004096 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02004097{
4098 struct dasd_ccw_req *cqr;
4099 struct ccw1 *ccw;
4100
Sebastian Ottc5205f22018-06-04 19:07:39 +02004101 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
4102 NULL);
Cornelia Huck17283b52007-05-04 18:47:51 +02004103
4104 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004105 /* internal error 13 - Allocating the RDC request failed*/
4106 dev_err(&device->cdev->dev,
4107 "An error occurred in the DASD device driver, "
4108 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02004109 return cqr;
4110 }
4111
4112 ccw = cqr->cpaddr;
4113 ccw->cmd_code = CCW_CMD_RDC;
Martin Schwidefsky9fe567d2019-03-01 16:03:47 +01004114 ccw->cda = (__u32)(addr_t) cqr->data;
4115 ccw->flags = 0;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02004116 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004117 cqr->startdev = device;
4118 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02004119 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004120 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01004121 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02004122 cqr->status = DASD_CQR_FILLED;
4123 return cqr;
4124}
4125
4126
Stefan Haberland68b781f2009-09-11 10:28:29 +02004127int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02004128 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02004129{
4130 int ret;
4131 struct dasd_ccw_req *cqr;
4132
Martin Schwidefsky9fe567d2019-03-01 16:03:47 +01004133 cqr = dasd_generic_build_rdc(device, rdc_buffer_size, magic);
Cornelia Huck17283b52007-05-04 18:47:51 +02004134 if (IS_ERR(cqr))
4135 return PTR_ERR(cqr);
4136
4137 ret = dasd_sleep_on(cqr);
Martin Schwidefsky9fe567d2019-03-01 16:03:47 +01004138 if (ret == 0)
4139 memcpy(rdc_buffer, cqr->data, rdc_buffer_size);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004140 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02004141 return ret;
4142}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02004143EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004144
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01004145/*
4146 * In command mode and transport mode we need to look for sense
4147 * data in different places. The sense data itself is allways
4148 * an array of 32 bytes, so we can unify the sense data access
4149 * for both modes.
4150 */
4151char *dasd_get_sense(struct irb *irb)
4152{
4153 struct tsb *tsb = NULL;
4154 char *sense = NULL;
4155
4156 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4157 if (irb->scsw.tm.tcw)
4158 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4159 irb->scsw.tm.tcw);
4160 if (tsb && tsb->length == 64 && tsb->flags)
4161 switch (tsb->flags & 0x07) {
4162 case 1: /* tsa_iostat */
4163 sense = tsb->tsa.iostat.sense;
4164 break;
4165 case 2: /* tsa_ddpc */
4166 sense = tsb->tsa.ddpc.sense;
4167 break;
4168 default:
4169 /* currently we don't use interrogate data */
4170 break;
4171 }
4172 } else if (irb->esw.esw0.erw.cons) {
4173 sense = irb->ecw;
4174 }
4175 return sense;
4176}
4177EXPORT_SYMBOL_GPL(dasd_get_sense);
4178
Stefan Haberland4679e892012-06-19 17:30:12 +02004179void dasd_generic_shutdown(struct ccw_device *cdev)
4180{
4181 struct dasd_device *device;
4182
4183 device = dasd_device_from_cdev(cdev);
4184 if (IS_ERR(device))
4185 return;
4186
4187 if (device->block)
4188 dasd_schedule_block_bh(device->block);
4189
4190 dasd_schedule_device_bh(device);
4191
4192 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4193}
4194EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4195
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004196static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
4198 int rc;
4199
4200 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02004201 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02004202 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02004203 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01004206 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (dasd_debug_area == NULL) {
4208 rc = -ENOMEM;
4209 goto failed;
4210 }
4211 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02004212 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4215
4216 dasd_diag_discipline_pointer = NULL;
4217
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02004218 dasd_statistics_createroot();
4219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 rc = dasd_devmap_init();
4221 if (rc)
4222 goto failed;
4223 rc = dasd_gendisk_init();
4224 if (rc)
4225 goto failed;
4226 rc = dasd_parse();
4227 if (rc)
4228 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004229 rc = dasd_eer_init();
4230 if (rc)
4231 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232#ifdef CONFIG_PROC_FS
4233 rc = dasd_proc_init();
4234 if (rc)
4235 goto failed;
4236#endif
4237
4238 return 0;
4239failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004240 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 dasd_exit();
4242 return rc;
4243}
4244
4245module_init(dasd_init);
4246module_exit(dasd_exit);