blob: 7c3dddeb781c8ad72202918f96ec22e004434570 [file] [log] [blame]
Greg Kroah-Hartman6a55d2c2017-11-14 18:38:00 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02008 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011#define KMSG_COMPONENT "dasd"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kmod.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/ctype.h>
18#include <linux/major.h>
19#include <linux/slab.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080020#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020021#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010022#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020023#include <linux/debugfs.h>
24#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020025#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/ccwdev.h>
28#include <asm/ebcdic.h>
29#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010030#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010031#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* This is ugly... */
34#define PRINTK_HEADER "dasd:"
35
36#include "dasd_int.h"
37/*
38 * SECTION: Constant definitions to be used within this file
39 */
40#define DASD_CHANQ_MAX_SIZE 4
41
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +020042#define DASD_DIAG_MOD "dasd_diag_mod"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * SECTION: exported variables of dasd.c
46 */
47debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020048EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020049static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020051EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010052void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
55MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020056 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_LICENSE("GPL");
59
60/*
61 * SECTION: prototypes for static functions of dasd.c
62 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010063static int dasd_alloc_queue(struct dasd_block *);
64static void dasd_setup_queue(struct dasd_block *);
65static void dasd_free_queue(struct dasd_block *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010066static int dasd_flush_block_queue(struct dasd_block *);
67static void dasd_device_tasklet(struct dasd_device *);
68static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000069static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020070static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020071static void do_reload_device(struct work_struct *);
Stefan Haberlanda521b042016-08-08 15:56:54 +020072static void do_requeue_requests(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Kees Cooke99e88a2017-10-16 14:43:17 -070074static void dasd_device_timeout(struct timer_list *);
75static void dasd_block_timeout(struct timer_list *);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010076static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020077static void dasd_profile_init(struct dasd_profile *, struct dentry *);
78static void dasd_profile_exit(struct dasd_profile *);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +010079static void dasd_hosts_init(struct dentry *, struct dasd_device *);
80static void dasd_hosts_exit(struct dasd_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * SECTION: Operations on the device structure.
84 */
85static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020086static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020087static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020088static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Allocate memory for a new device structure.
92 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010093struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct dasd_device *device;
96
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010097 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
98 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* Get two pages for normal block device operations. */
102 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100103 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kfree(device);
105 return ERR_PTR(-ENOMEM);
106 }
107 /* Get one page for error recovery. */
108 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100109 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 free_pages((unsigned long) device->ccw_mem, 1);
111 kfree(device);
112 return ERR_PTR(-ENOMEM);
113 }
114
115 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
116 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
117 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100118 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200119 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100120 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 (unsigned long) device);
122 INIT_LIST_HEAD(&device->ccw_queue);
Kees Cooke99e88a2017-10-16 14:43:17 -0700123 timer_setup(&device->timer, dasd_device_timeout, 0);
Al Viro4927b3f2006-12-06 19:18:20 +0000124 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200125 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200126 INIT_WORK(&device->reload_device, do_reload_device);
Stefan Haberlanda521b042016-08-08 15:56:54 +0200127 INIT_WORK(&device->requeue_requests, do_requeue_requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 device->state = DASD_STATE_NEW;
129 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100130 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200131 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return device;
133}
134
135/*
136 * Free memory of a device structure.
137 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100138void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800140 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 free_page((unsigned long) device->erp_mem);
142 free_pages((unsigned long) device->ccw_mem, 1);
143 kfree(device);
144}
145
146/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100147 * Allocate memory for a new device structure.
148 */
149struct dasd_block *dasd_alloc_block(void)
150{
151 struct dasd_block *block;
152
153 block = kzalloc(sizeof(*block), GFP_ATOMIC);
154 if (!block)
155 return ERR_PTR(-ENOMEM);
156 /* open_count = 0 means device online but not in use */
157 atomic_set(&block->open_count, -1);
158
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100159 atomic_set(&block->tasklet_scheduled, 0);
160 tasklet_init(&block->tasklet,
161 (void (*)(unsigned long)) dasd_block_tasklet,
162 (unsigned long) block);
163 INIT_LIST_HEAD(&block->ccw_queue);
164 spin_lock_init(&block->queue_lock);
Kees Cooke99e88a2017-10-16 14:43:17 -0700165 timer_setup(&block->timer, dasd_block_timeout, 0);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200166 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100167
168 return block;
169}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200170EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100171
172/*
173 * Free memory of a device structure.
174 */
175void dasd_free_block(struct dasd_block *block)
176{
177 kfree(block);
178}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200179EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Make a new device known to the system.
183 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100184static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 int rc;
187
188 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200189 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * keep the reference count > 0.
191 */
192 dasd_get_device(device);
193
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100194 if (device->block) {
195 rc = dasd_alloc_queue(device->block);
196 if (rc) {
197 dasd_put_device(device);
198 return rc;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 device->state = DASD_STATE_KNOWN;
202 return 0;
203}
204
205/*
206 * Let the system forget about a device.
207 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100208static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800210 /* Disable extended error reporting for this device. */
211 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 device->state = DASD_STATE_NEW;
213
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100214 if (device->block)
215 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Give up reference we took in dasd_state_new_to_known. */
218 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200222static struct dentry *dasd_debugfs_setup(const char *name,
223 struct dentry *base_dentry)
224{
225 struct dentry *pde;
226
227 if (!base_dentry)
228 return NULL;
229 pde = debugfs_create_dir(name, base_dentry);
230 if (!pde || IS_ERR(pde))
231 return NULL;
232 return pde;
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
236 * Request the irq line for the device.
237 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100238static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200240 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200241 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200244 if (block) {
245 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100246 if (rc)
247 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200248 block->debugfs_dentry =
249 dasd_debugfs_setup(block->gdp->disk_name,
250 dasd_debugfs_root_entry);
251 dasd_profile_init(&block->profile, block->debugfs_dentry);
252 if (dasd_global_profile_level == DASD_PROFILE_ON)
253 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100254 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200255 device->debugfs_dentry =
256 dasd_debugfs_setup(dev_name(&device->cdev->dev),
257 dasd_debugfs_root_entry);
258 dasd_profile_init(&device->profile, device->debugfs_dentry);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100259 dasd_hosts_init(device->debugfs_dentry, device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100262 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100263 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200265 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
267
268 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200269
270 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Release the irq line for the device. Terminate any running i/o.
275 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100276static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Horst Hummel8f617012006-08-30 14:33:33 +0200278 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200279
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200280 if (device->discipline->basic_to_known) {
281 rc = device->discipline->basic_to_known(device);
282 if (rc)
283 return rc;
284 }
285
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100286 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200287 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200288 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289 dasd_gendisk_free(device->block);
290 dasd_block_clear_timer(device->block);
291 }
292 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200293 if (rc)
294 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100295 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200296 dasd_profile_exit(&device->profile);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100297 dasd_hosts_exit(device);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200298 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
300 if (device->debug_area != NULL) {
301 debug_unregister(device->debug_area);
302 device->debug_area = NULL;
303 }
304 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
309 * Do the initial analysis. The do_analysis function may return
310 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
311 * until the discipline decides to continue the startup sequence
312 * by calling the function dasd_change_state. The eckd disciplines
313 * uses this to start a ccw that detects the format. The completion
314 * interrupt for this detection ccw uses the kernel event daemon to
315 * trigger the call to dasd_change_state. All this is done in the
316 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800317 * After the analysis ccw is done (do_analysis returned 0) the block
318 * device is setup.
319 * In case the analysis returns an error, the device setup is stopped
320 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100322static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325 struct dasd_block *block;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200326 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800330 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100331 if (block) {
332 if (block->base->discipline->do_analysis != NULL)
333 rc = block->base->discipline->do_analysis(block);
334 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200335 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336 device->state = DASD_STATE_UNFMT;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200337 disk = device->block->gdp;
338 kobject_uevent(&disk_to_dev(disk)->kobj,
339 KOBJ_CHANGE);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200340 goto out;
341 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100342 return rc;
343 }
344 dasd_setup_queue(block);
345 set_capacity(block->gdp,
346 block->blocks << block->s2b_shift);
347 device->state = DASD_STATE_READY;
348 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200349 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200351 return rc;
352 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353 } else {
354 device->state = DASD_STATE_READY;
355 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200356out:
357 if (device->discipline->basic_to_ready)
358 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800359 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100362static inline
363int _wait_for_empty_queues(struct dasd_device *device)
364{
365 if (device->block)
366 return list_empty(&device->ccw_queue) &&
367 list_empty(&device->block->ccw_queue);
368 else
369 return list_empty(&device->ccw_queue);
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * Remove device from block device layer. Destroy dirty buffers.
374 * Forget format information. Check if the target level is basic
375 * and if it is create fake disk for formatting.
376 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100377static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Horst Hummel8f617012006-08-30 14:33:33 +0200379 int rc;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100382 if (device->block) {
383 struct dasd_block *block = device->block;
384 rc = dasd_flush_block_queue(block);
385 if (rc) {
386 device->state = DASD_STATE_READY;
387 return rc;
388 }
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100389 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100390 block->blocks = 0;
391 block->bp_block = 0;
392 block->s2b_shift = 0;
393 }
Horst Hummel8f617012006-08-30 14:33:33 +0200394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
Horst Hummel90f00942006-03-07 21:55:39 -0800398 * Back to basic.
399 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100400static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800401{
402 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200403 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800404}
405
406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * Make the device online and schedule the bottom half to start
408 * the requeueing of requests from the linux request queue to the
409 * ccw queue.
410 */
Horst Hummel8f617012006-08-30 14:33:33 +0200411static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412dasd_state_ready_to_online(struct dasd_device * device)
413{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100414 struct gendisk *disk;
415 struct disk_part_iter piter;
416 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100419 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100420 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100421 if ((device->features & DASD_FEATURE_USERAW)) {
422 disk = device->block->gdp;
423 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
424 return 0;
425 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100426 disk = device->block->bdev->bd_disk;
427 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
428 while ((part = disk_part_iter_next(&piter)))
429 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
430 disk_part_iter_exit(&piter);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
433}
434
435/*
436 * Stop the requeueing of requests again.
437 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100438static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100440 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100441 struct gendisk *disk;
442 struct disk_part_iter piter;
443 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444
445 if (device->discipline->online_to_ready) {
446 rc = device->discipline->online_to_ready(device);
447 if (rc)
448 return rc;
449 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100452 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100453 disk = device->block->bdev->bd_disk;
454 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
455 while ((part = disk_part_iter_next(&piter)))
456 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
457 disk_part_iter_exit(&piter);
458 }
Horst Hummel8f617012006-08-30 14:33:33 +0200459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
463 * Device startup state changes.
464 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100465static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int rc;
468
469 rc = 0;
470 if (device->state == DASD_STATE_NEW &&
471 device->target >= DASD_STATE_KNOWN)
472 rc = dasd_state_new_to_known(device);
473
474 if (!rc &&
475 device->state == DASD_STATE_KNOWN &&
476 device->target >= DASD_STATE_BASIC)
477 rc = dasd_state_known_to_basic(device);
478
479 if (!rc &&
480 device->state == DASD_STATE_BASIC &&
481 device->target >= DASD_STATE_READY)
482 rc = dasd_state_basic_to_ready(device);
483
484 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700485 device->state == DASD_STATE_UNFMT &&
486 device->target > DASD_STATE_UNFMT)
487 rc = -EPERM;
488
489 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 device->state == DASD_STATE_READY &&
491 device->target >= DASD_STATE_ONLINE)
492 rc = dasd_state_ready_to_online(device);
493
494 return rc;
495}
496
497/*
498 * Device shutdown state changes.
499 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100500static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Horst Hummel8f617012006-08-30 14:33:33 +0200502 int rc;
503
504 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (device->state == DASD_STATE_ONLINE &&
506 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200507 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200508
Horst Hummel8f617012006-08-30 14:33:33 +0200509 if (!rc &&
510 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200512 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800513
Horst Hummel8f617012006-08-30 14:33:33 +0200514 if (!rc &&
515 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800516 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200517 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800518
Horst Hummel8f617012006-08-30 14:33:33 +0200519 if (!rc &&
520 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200522 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200523
Horst Hummel8f617012006-08-30 14:33:33 +0200524 if (!rc &&
525 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200527 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Horst Hummel8f617012006-08-30 14:33:33 +0200529 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532/*
533 * This is the main startup/shutdown routine.
534 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100535static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Sebastian Ott181d9522009-06-22 12:08:21 +0200537 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (device->state == device->target)
540 /* Already where we want to go today... */
541 return;
542 if (device->state < device->target)
543 rc = dasd_increase_state(device);
544 else
545 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200546 if (rc == -EAGAIN)
547 return;
548 if (rc)
549 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200551 /* let user-space know that the device status changed */
552 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200553
554 if (device->state == device->target)
555 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558/*
559 * Kick starter for devices that did not complete the startup/shutdown
560 * procedure or were sleeping because of a pending state.
561 * dasd_kick_device will schedule a call do do_kick_device to the kernel
562 * event daemon.
563 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100564static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Al Viro4927b3f2006-12-06 19:18:20 +0000566 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100567 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100569 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100570 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 dasd_put_device(device);
572}
573
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100574void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 dasd_get_device(device);
577 /* queue call to dasd_kick_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200578 if (!schedule_work(&device->kick_work))
579 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200581EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200584 * dasd_reload_device will schedule a call do do_reload_device to the kernel
585 * event daemon.
586 */
587static void do_reload_device(struct work_struct *work)
588{
589 struct dasd_device *device = container_of(work, struct dasd_device,
590 reload_device);
591 device->discipline->reload(device);
592 dasd_put_device(device);
593}
594
595void dasd_reload_device(struct dasd_device *device)
596{
597 dasd_get_device(device);
598 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200599 if (!schedule_work(&device->reload_device))
600 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200601}
602EXPORT_SYMBOL(dasd_reload_device);
603
604/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200605 * dasd_restore_device will schedule a call do do_restore_device to the kernel
606 * event daemon.
607 */
608static void do_restore_device(struct work_struct *work)
609{
610 struct dasd_device *device = container_of(work, struct dasd_device,
611 restore_device);
612 device->cdev->drv->restore(device->cdev);
613 dasd_put_device(device);
614}
615
616void dasd_restore_device(struct dasd_device *device)
617{
618 dasd_get_device(device);
619 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200620 if (!schedule_work(&device->restore_device))
621 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200622}
623
624/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * Set the target state for a device and starts the state change.
626 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100627void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200629 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100630 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* If we are in probeonly mode stop at DASD_STATE_READY. */
632 if (dasd_probeonly && target > DASD_STATE_READY)
633 target = DASD_STATE_READY;
634 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100635 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 wake_up(&dasd_init_waitq);
637 device->target = target;
638 }
639 if (device->state != device->target)
640 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100641 mutex_unlock(&device->state_mutex);
642 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200644EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646/*
647 * Enable devices with device numbers in [from..to].
648 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100649static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 return (device->state == device->target);
652}
653
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100654void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 dasd_set_target_state(device, DASD_STATE_ONLINE);
657 if (device->state <= DASD_STATE_KNOWN)
658 /* No discipline for device found. */
659 dasd_set_target_state(device, DASD_STATE_NEW);
660 /* Now wait for the devices to come up. */
661 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400662
663 dasd_reload_device(device);
664 if (device->discipline->kick_validate)
665 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200667EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669/*
670 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
671 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200672
673unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100676struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100677 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
678};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200679static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/*
682 * Add profiling information for cqr before execution.
683 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100684static void dasd_profile_start(struct dasd_block *block,
685 struct dasd_ccw_req *cqr,
686 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct list_head *l;
689 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200690 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* count the length of the chanq for statistics */
693 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200694 if (dasd_global_profile_level || block->profile.data)
695 list_for_each(l, &block->ccw_queue)
696 if (++counter >= 31)
697 break;
698
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100699 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100700 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100701 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200702 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100703 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200704 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100705 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200706
707 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200708 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200709 block->profile.data->dasd_io_nr_req[counter]++;
710 if (rq_data_dir(req) == READ)
711 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200712 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200713 spin_unlock(&block->profile.lock);
714
715 /*
716 * We count the request for the start device, even though it may run on
717 * some other device due to error recovery. This way we make sure that
718 * we count each request only once.
719 */
720 device = cqr->startdev;
721 if (device->profile.data) {
722 counter = 1; /* request is not yet queued on the start device */
723 list_for_each(l, &device->ccw_queue)
724 if (++counter >= 31)
725 break;
726 }
727 spin_lock(&device->profile.lock);
728 if (device->profile.data) {
729 device->profile.data->dasd_io_nr_req[counter]++;
730 if (rq_data_dir(req) == READ)
731 device->profile.data->dasd_read_nr_req[counter]++;
732 }
733 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736/*
737 * Add profiling information for cqr after execution.
738 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200739
740#define dasd_profile_counter(value, index) \
741{ \
742 for (index = 0; index < 31 && value >> (2+index); index++) \
743 ; \
744}
745
746static void dasd_profile_end_add_data(struct dasd_profile_info *data,
747 int is_alias,
748 int is_tpm,
749 int is_read,
750 long sectors,
751 int sectors_ind,
752 int tottime_ind,
753 int tottimeps_ind,
754 int strtime_ind,
755 int irqtime_ind,
756 int irqtimeps_ind,
757 int endtime_ind)
758{
759 /* in case of an overflow, reset the whole profile */
760 if (data->dasd_io_reqs == UINT_MAX) {
761 memset(data, 0, sizeof(*data));
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100762 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200763 }
764 data->dasd_io_reqs++;
765 data->dasd_io_sects += sectors;
766 if (is_alias)
767 data->dasd_io_alias++;
768 if (is_tpm)
769 data->dasd_io_tpm++;
770
771 data->dasd_io_secs[sectors_ind]++;
772 data->dasd_io_times[tottime_ind]++;
773 data->dasd_io_timps[tottimeps_ind]++;
774 data->dasd_io_time1[strtime_ind]++;
775 data->dasd_io_time2[irqtime_ind]++;
776 data->dasd_io_time2ps[irqtimeps_ind]++;
777 data->dasd_io_time3[endtime_ind]++;
778
779 if (is_read) {
780 data->dasd_read_reqs++;
781 data->dasd_read_sects += sectors;
782 if (is_alias)
783 data->dasd_read_alias++;
784 if (is_tpm)
785 data->dasd_read_tpm++;
786 data->dasd_read_secs[sectors_ind]++;
787 data->dasd_read_times[tottime_ind]++;
788 data->dasd_read_time1[strtime_ind]++;
789 data->dasd_read_time2[irqtime_ind]++;
790 data->dasd_read_time3[endtime_ind]++;
791 }
792}
793
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100794static void dasd_profile_end(struct dasd_block *block,
795 struct dasd_ccw_req *cqr,
796 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Stefan Haberlandd2907222017-07-14 10:33:18 +0200798 unsigned long strtime, irqtime, endtime, tottime;
799 unsigned long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200800 struct dasd_device *device;
801 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
802 int irqtime_ind, irqtimeps_ind, endtime_ind;
Stefan Haberlandd2907222017-07-14 10:33:18 +0200803 struct dasd_profile_info *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200805 device = cqr->startdev;
806 if (!(dasd_global_profile_level ||
807 block->profile.data ||
808 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return;
810
Tejun Heo83096eb2009-05-07 22:24:39 +0900811 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!cqr->buildclk || !cqr->startclk ||
813 !cqr->stopclk || !cqr->endclk ||
814 !sectors)
815 return;
816
817 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
818 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
819 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
820 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
821 tottimeps = tottime / sectors;
822
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200823 dasd_profile_counter(sectors, sectors_ind);
824 dasd_profile_counter(tottime, tottime_ind);
825 dasd_profile_counter(tottimeps, tottimeps_ind);
826 dasd_profile_counter(strtime, strtime_ind);
827 dasd_profile_counter(irqtime, irqtime_ind);
828 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
829 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100831 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100832 if (dasd_global_profile.data) {
Stefan Haberlandd2907222017-07-14 10:33:18 +0200833 data = dasd_global_profile.data;
834 data->dasd_sum_times += tottime;
835 data->dasd_sum_time_str += strtime;
836 data->dasd_sum_time_irq += irqtime;
837 data->dasd_sum_time_end += endtime;
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100838 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200839 cqr->startdev != block->base,
840 cqr->cpmode == 1,
841 rq_data_dir(req) == READ,
842 sectors, sectors_ind, tottime_ind,
843 tottimeps_ind, strtime_ind,
844 irqtime_ind, irqtimeps_ind,
845 endtime_ind);
846 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100847 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200849 spin_lock(&block->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200850 if (block->profile.data) {
851 data = block->profile.data;
852 data->dasd_sum_times += tottime;
853 data->dasd_sum_time_str += strtime;
854 data->dasd_sum_time_irq += irqtime;
855 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200856 dasd_profile_end_add_data(block->profile.data,
857 cqr->startdev != block->base,
858 cqr->cpmode == 1,
859 rq_data_dir(req) == READ,
860 sectors, sectors_ind, tottime_ind,
861 tottimeps_ind, strtime_ind,
862 irqtime_ind, irqtimeps_ind,
863 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200864 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200865 spin_unlock(&block->profile.lock);
866
867 spin_lock(&device->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200868 if (device->profile.data) {
869 data = device->profile.data;
870 data->dasd_sum_times += tottime;
871 data->dasd_sum_time_str += strtime;
872 data->dasd_sum_time_irq += irqtime;
873 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200874 dasd_profile_end_add_data(device->profile.data,
875 cqr->startdev != block->base,
876 cqr->cpmode == 1,
877 rq_data_dir(req) == READ,
878 sectors, sectors_ind, tottime_ind,
879 tottimeps_ind, strtime_ind,
880 irqtime_ind, irqtimeps_ind,
881 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200882 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200883 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200885
886void dasd_profile_reset(struct dasd_profile *profile)
887{
888 struct dasd_profile_info *data;
889
890 spin_lock_bh(&profile->lock);
891 data = profile->data;
892 if (!data) {
893 spin_unlock_bh(&profile->lock);
894 return;
895 }
896 memset(data, 0, sizeof(*data));
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100897 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200898 spin_unlock_bh(&profile->lock);
899}
900
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200901int dasd_profile_on(struct dasd_profile *profile)
902{
903 struct dasd_profile_info *data;
904
905 data = kzalloc(sizeof(*data), GFP_KERNEL);
906 if (!data)
907 return -ENOMEM;
908 spin_lock_bh(&profile->lock);
909 if (profile->data) {
910 spin_unlock_bh(&profile->lock);
911 kfree(data);
912 return 0;
913 }
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100914 ktime_get_real_ts64(&data->starttod);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200915 profile->data = data;
916 spin_unlock_bh(&profile->lock);
917 return 0;
918}
919
920void dasd_profile_off(struct dasd_profile *profile)
921{
922 spin_lock_bh(&profile->lock);
923 kfree(profile->data);
924 profile->data = NULL;
925 spin_unlock_bh(&profile->lock);
926}
927
928char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
929{
930 char *buffer;
931
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200932 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200933 if (buffer == NULL)
934 return ERR_PTR(-ENOMEM);
935 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200936 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200937 return ERR_PTR(-EFAULT);
938 }
939 /* got the string, now strip linefeed. */
940 if (buffer[user_len - 1] == '\n')
941 buffer[user_len - 1] = 0;
942 else
943 buffer[user_len] = 0;
944 return buffer;
945}
946
947static ssize_t dasd_stats_write(struct file *file,
948 const char __user *user_buf,
949 size_t user_len, loff_t *pos)
950{
951 char *buffer, *str;
952 int rc;
953 struct seq_file *m = (struct seq_file *)file->private_data;
954 struct dasd_profile *prof = m->private;
955
956 if (user_len > 65536)
957 user_len = 65536;
958 buffer = dasd_get_user_string(user_buf, user_len);
959 if (IS_ERR(buffer))
960 return PTR_ERR(buffer);
961
962 str = skip_spaces(buffer);
963 rc = user_len;
964 if (strncmp(str, "reset", 5) == 0) {
965 dasd_profile_reset(prof);
966 } else if (strncmp(str, "on", 2) == 0) {
967 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100968 if (rc)
969 goto out;
970 rc = user_len;
971 if (prof == &dasd_global_profile) {
972 dasd_profile_reset(prof);
973 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
974 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200975 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100976 if (prof == &dasd_global_profile)
977 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200978 dasd_profile_off(prof);
979 } else
980 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100981out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200982 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200983 return rc;
984}
985
986static void dasd_stats_array(struct seq_file *m, unsigned int *array)
987{
988 int i;
989
990 for (i = 0; i < 32; i++)
991 seq_printf(m, "%u ", array[i]);
992 seq_putc(m, '\n');
993}
994
995static void dasd_stats_seq_print(struct seq_file *m,
996 struct dasd_profile_info *data)
997{
Arnd Bergmannbd7a9b32017-11-27 12:46:57 +0100998 seq_printf(m, "start_time %lld.%09ld\n",
999 (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001000 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
1001 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1002 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1003 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Stefan Haberlandd2907222017-07-14 10:33:18 +02001004 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1005 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1006 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1007 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1008 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1009 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1010 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1011 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001012 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001013 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001014 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001015 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001016 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001017 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001018 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001019 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001020 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001021 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001022 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001023 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001024 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001025 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001026 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001027 dasd_stats_array(m, data->dasd_io_nr_req);
1028 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1029 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1030 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1031 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001032 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001033 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001034 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001035 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001036 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001037 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001038 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001039 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001040 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001041 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001042 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001043 dasd_stats_array(m, data->dasd_read_nr_req);
1044}
1045
1046static int dasd_stats_show(struct seq_file *m, void *v)
1047{
1048 struct dasd_profile *profile;
1049 struct dasd_profile_info *data;
1050
1051 profile = m->private;
1052 spin_lock_bh(&profile->lock);
1053 data = profile->data;
1054 if (!data) {
1055 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001056 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001057 return 0;
1058 }
1059 dasd_stats_seq_print(m, data);
1060 spin_unlock_bh(&profile->lock);
1061 return 0;
1062}
1063
1064static int dasd_stats_open(struct inode *inode, struct file *file)
1065{
1066 struct dasd_profile *profile = inode->i_private;
1067 return single_open(file, dasd_stats_show, profile);
1068}
1069
1070static const struct file_operations dasd_stats_raw_fops = {
1071 .owner = THIS_MODULE,
1072 .open = dasd_stats_open,
1073 .read = seq_read,
1074 .llseek = seq_lseek,
1075 .release = single_release,
1076 .write = dasd_stats_write,
1077};
1078
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001079static void dasd_profile_init(struct dasd_profile *profile,
1080 struct dentry *base_dentry)
1081{
Al Virof4ae40a62011-07-24 04:33:43 -04001082 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001083 struct dentry *pde;
1084
1085 if (!base_dentry)
1086 return;
1087 profile->dentry = NULL;
1088 profile->data = NULL;
1089 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1090 pde = debugfs_create_file("statistics", mode, base_dentry,
1091 profile, &dasd_stats_raw_fops);
1092 if (pde && !IS_ERR(pde))
1093 profile->dentry = pde;
1094 return;
1095}
1096
1097static void dasd_profile_exit(struct dasd_profile *profile)
1098{
1099 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001100 debugfs_remove(profile->dentry);
1101 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001102}
1103
1104static void dasd_statistics_removeroot(void)
1105{
1106 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001107 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001108 debugfs_remove(dasd_debugfs_global_entry);
1109 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001110}
1111
1112static void dasd_statistics_createroot(void)
1113{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001114 struct dentry *pde;
1115
1116 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001117 pde = debugfs_create_dir("dasd", NULL);
1118 if (!pde || IS_ERR(pde))
1119 goto error;
1120 dasd_debugfs_root_entry = pde;
1121 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1122 if (!pde || IS_ERR(pde))
1123 goto error;
1124 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001125 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001126 return;
1127
1128error:
1129 DBF_EVENT(DBF_ERR, "%s",
1130 "Creation of the dasd debugfs interface failed");
1131 dasd_statistics_removeroot();
1132 return;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001136#define dasd_profile_start(block, cqr, req) do {} while (0)
1137#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001138
1139static void dasd_statistics_createroot(void)
1140{
1141 return;
1142}
1143
1144static void dasd_statistics_removeroot(void)
1145{
1146 return;
1147}
1148
1149int dasd_stats_generic_show(struct seq_file *m, void *v)
1150{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001151 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001152 return 0;
1153}
1154
1155static void dasd_profile_init(struct dasd_profile *profile,
1156 struct dentry *base_dentry)
1157{
1158 return;
1159}
1160
1161static void dasd_profile_exit(struct dasd_profile *profile)
1162{
1163 return;
1164}
1165
1166int dasd_profile_on(struct dasd_profile *profile)
1167{
1168 return 0;
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#endif /* CONFIG_DASD_PROFILE */
1172
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001173static int dasd_hosts_show(struct seq_file *m, void *v)
1174{
1175 struct dasd_device *device;
1176 int rc = -EOPNOTSUPP;
1177
1178 device = m->private;
1179 dasd_get_device(device);
1180
1181 if (device->discipline->hosts_print)
1182 rc = device->discipline->hosts_print(device, m);
1183
1184 dasd_put_device(device);
1185 return rc;
1186}
1187
1188static int dasd_hosts_open(struct inode *inode, struct file *file)
1189{
1190 struct dasd_device *device = inode->i_private;
1191
1192 return single_open(file, dasd_hosts_show, device);
1193}
1194
1195static const struct file_operations dasd_hosts_fops = {
1196 .owner = THIS_MODULE,
1197 .open = dasd_hosts_open,
1198 .read = seq_read,
1199 .llseek = seq_lseek,
1200 .release = single_release,
1201};
1202
1203static void dasd_hosts_exit(struct dasd_device *device)
1204{
1205 debugfs_remove(device->hosts_dentry);
1206 device->hosts_dentry = NULL;
1207}
1208
1209static void dasd_hosts_init(struct dentry *base_dentry,
1210 struct dasd_device *device)
1211{
1212 struct dentry *pde;
1213 umode_t mode;
1214
1215 if (!base_dentry)
1216 return;
1217
1218 mode = S_IRUSR | S_IFREG;
1219 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1220 device, &dasd_hosts_fops);
1221 if (pde && !IS_ERR(pde))
1222 device->hosts_dentry = pde;
1223}
1224
Sebastian Ottc5205f22018-06-04 19:07:39 +02001225struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
1226 struct dasd_device *device,
1227 struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 unsigned long flags;
Sebastian Ottc5205f22018-06-04 19:07:39 +02001230 char *data, *chunk;
1231 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (cplength > 0)
1234 size += cplength * sizeof(struct ccw1);
1235 if (datasize > 0)
1236 size += datasize;
Sebastian Ottc5205f22018-06-04 19:07:39 +02001237 if (!cqr)
1238 size += (sizeof(*cqr) + 7L) & -8L;
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 spin_lock_irqsave(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001241 data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 spin_unlock_irqrestore(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001243 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return ERR_PTR(-ENOMEM);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001245 if (!cqr) {
1246 cqr = (void *) data;
1247 data += (sizeof(*cqr) + 7L) & -8L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Sebastian Ottc5205f22018-06-04 19:07:39 +02001249 memset(cqr, 0, sizeof(*cqr));
1250 cqr->mem_chunk = chunk;
1251 if (cplength > 0) {
1252 cqr->cpaddr = data;
1253 data += cplength * sizeof(struct ccw1);
1254 memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (datasize > 0) {
1257 cqr->data = data;
1258 memset(cqr->data, 0, datasize);
1259 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001260 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1262 dasd_get_device(device);
1263 return cqr;
1264}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001265EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001267void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 unsigned long flags;
1270
1271 spin_lock_irqsave(&device->mem_lock, flags);
Sebastian Ottc5205f22018-06-04 19:07:39 +02001272 dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_unlock_irqrestore(&device->mem_lock, flags);
1274 dasd_put_device(device);
1275}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001276EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278/*
1279 * Check discipline magic in cqr.
1280 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001281static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct dasd_device *device;
1284
1285 if (cqr == NULL)
1286 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001287 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001289 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 " dasd_ccw_req 0x%08x magic doesn't match"
1291 " discipline 0x%08x",
1292 cqr->magic,
1293 *(unsigned int *) device->discipline->name);
1294 return -EINVAL;
1295 }
1296 return 0;
1297}
1298
1299/*
1300 * Terminate the current i/o and set the request to clear_pending.
1301 * Timer keeps device runnig.
1302 * ccw_device_clear can fail if the i/o subsystem
1303 * is in a bad mood.
1304 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001305int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 struct dasd_device *device;
1308 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001309 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* Check the cqr */
1312 rc = dasd_check_cqr(cqr);
1313 if (rc)
1314 return rc;
1315 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1318 rc = ccw_device_clear(device->cdev, (long) cqr);
1319 switch (rc) {
1320 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001321 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001322 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001323 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 DBF_DEV_EVENT(DBF_DEBUG, device,
1325 "terminate cqr %p successful",
1326 cqr);
1327 break;
1328 case -ENODEV:
1329 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1330 "device gone, retry");
1331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001333 /*
1334 * device not valid so no I/O could be running
1335 * handle CQR as termination successful
1336 */
1337 cqr->status = DASD_CQR_CLEARED;
1338 cqr->stopclk = get_tod_clock();
1339 cqr->starttime = 0;
1340 /* no retries for invalid devices */
1341 cqr->retries = -1;
1342 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1343 "EINVAL, handle as terminated");
1344 /* fake rc to success */
1345 rc = 0;
1346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001348 /* internal error 10 - unknown rc*/
1349 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1350 dev_err(&device->cdev->dev, "An error occurred in the "
1351 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 BUG();
1353 break;
1354 }
1355 retries++;
1356 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001357 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return rc;
1359}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001360EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362/*
1363 * Start the i/o. This start_IO can fail if the channel is really busy.
1364 * In that case set up a timer to start the request later.
1365 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001366int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct dasd_device *device;
1369 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001370 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* Check the cqr */
1373 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001374 if (rc) {
1375 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001377 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001378 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001379 if (((cqr->block &&
1380 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1381 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1382 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1383 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1384 "because of stolen lock", cqr);
1385 cqr->status = DASD_CQR_ERROR;
1386 cqr->intrc = -EPERM;
1387 return -EPERM;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001390 /* internal error 14 - start_IO run out of retries */
1391 sprintf(errorstring, "14 %p", cqr);
1392 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1393 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001394 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EIO;
1396 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001397 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 cqr->starttime = jiffies;
1399 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001400 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001401 cqr->lpm &= dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001402 if (!cqr->lpm)
Stefan Haberlandc9346152016-08-08 15:53:54 +02001403 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001404 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001405 if (cqr->cpmode == 1) {
1406 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1407 (long) cqr, cqr->lpm);
1408 } else {
1409 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1410 (long) cqr, cqr->lpm, 0);
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 switch (rc) {
1413 case 0:
1414 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001417 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 "start_IO: device busy, retry later");
1419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001421 /* -EACCES indicates that the request used only a subset of the
1422 * available paths and all these paths are gone. If the lpm of
1423 * this request was only a subset of the opm (e.g. the ppm) then
1424 * we just do a retry with all available paths.
1425 * If we already use the full opm, something is amiss, and we
1426 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001428 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1429 DBF_DEV_EVENT(DBF_WARNING, device,
1430 "start_IO: selected paths gone (%x)",
1431 cqr->lpm);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001432 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1433 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001434 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1435 "start_IO: selected paths gone,"
1436 " retry on all paths");
1437 } else {
1438 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1439 "start_IO: all paths in opm gone,"
1440 " do path verification");
1441 dasd_generic_last_path_gone(device);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001442 dasd_path_no_path(device);
1443 dasd_path_set_tbvpm(device,
1444 ccw_device_get_path_mask(
1445 device->cdev));
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001449 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001450 "start_IO: -ENODEV device gone, retry");
1451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001453 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001454 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001456 case -EINVAL:
1457 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001459 "start_IO: -EINVAL device currently "
1460 "not accessible");
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001463 /* internal error 11 - unknown rc */
1464 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1465 dev_err(&device->cdev->dev,
1466 "An error occurred in the DASD device driver, "
1467 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 BUG();
1469 break;
1470 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001471 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return rc;
1473}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001474EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476/*
1477 * Timeout function for dasd devices. This is used for different purposes
1478 * 1) missing interrupt handler for normal operation
1479 * 2) delayed start of request where start_IO failed with -EBUSY
1480 * 3) timeout for missing state change interrupts
1481 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1482 * DASD_CQR_QUEUED for 2) and 3).
1483 */
Kees Cooke99e88a2017-10-16 14:43:17 -07001484static void dasd_device_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 unsigned long flags;
1487 struct dasd_device *device;
1488
Kees Cooke99e88a2017-10-16 14:43:17 -07001489 device = from_timer(device, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1491 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001492 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001494 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497/*
1498 * Setup timeout for a device in jiffies.
1499 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001500void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001502 if (expires == 0)
1503 del_timer(&device->timer);
1504 else
1505 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001507EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509/*
1510 * Clear timeout for a device.
1511 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001512void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001514 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001516EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518static void dasd_handle_killed_request(struct ccw_device *cdev,
1519 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct dasd_ccw_req *cqr;
1522 struct dasd_device *device;
1523
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001524 if (!intparm)
1525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 cqr = (struct dasd_ccw_req *) intparm;
1527 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001528 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1529 "invalid status in handle_killed_request: "
1530 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return;
1532 }
1533
Stefan Haberland589c74d2010-02-26 22:37:47 +01001534 device = dasd_device_from_cdev_locked(cdev);
1535 if (IS_ERR(device)) {
1536 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1537 "unable to get device from cdev");
1538 return;
1539 }
1540
1541 if (!cqr->startdev ||
1542 device != cqr->startdev ||
1543 strncmp(cqr->startdev->discipline->ebcname,
1544 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001545 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1546 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001547 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return;
1549 }
1550
1551 /* Schedule request to be retried. */
1552 cqr->status = DASD_CQR_QUEUED;
1553
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001554 dasd_device_clear_timer(device);
1555 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 dasd_put_device(device);
1557}
1558
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001559void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001561 /* First of all start sense subsystem status request. */
1562 dasd_eer_snss(device);
1563
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001564 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001565 dasd_schedule_device_bh(device);
Stefan Haberlande4433432017-05-22 10:59:11 +02001566 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001567 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02001568 if (device->block->request_queue)
1569 blk_mq_run_hw_queues(device->block->request_queue,
1570 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001573EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Stefan Haberlanda521b042016-08-08 15:56:54 +02001575static int dasd_check_hpf_error(struct irb *irb)
1576{
1577 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1578 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1579 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*
1583 * Interrupt handler for "normal" ssch-io based dasd devices.
1584 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001585void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1586 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct dasd_ccw_req *cqr, *next;
1589 struct dasd_device *device;
Jan Höppner7bf76f012017-08-15 16:40:18 +02001590 unsigned long now;
Jan Höppner8fd57522015-08-19 13:41:20 +02001591 int nrf_suppressed = 0;
1592 int fp_suppressed = 0;
1593 u8 *sense = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001596 cqr = (struct dasd_ccw_req *) intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (IS_ERR(irb)) {
1598 switch (PTR_ERR(irb)) {
1599 case -EIO:
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001600 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001601 device = cqr->startdev;
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001602 cqr->status = DASD_CQR_CLEARED;
1603 dasd_device_clear_timer(device);
1604 wake_up(&dasd_flush_wq);
1605 dasd_schedule_device_bh(device);
1606 return;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001610 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1611 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 break;
1613 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001614 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1615 "unknown error %ld\n", __func__,
1616 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001618 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return;
1620 }
1621
Heiko Carstens1aae0562013-01-30 09:49:40 +01001622 now = get_tod_clock();
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001623 /* check for conditions that should be handled immediately */
1624 if (!cqr ||
1625 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1626 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001627 if (cqr)
1628 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001629 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001630 if (IS_ERR(device))
1631 return;
1632 /* ignore unsolicited interrupts for DIAG discipline */
1633 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001635 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001637
1638 /*
1639 * In some cases 'File Protected' or 'No Record Found' errors
1640 * might be expected and debug log messages for the
1641 * corresponding interrupts shouldn't be written then.
1642 * Check if either of the according suppress bits is set.
1643 */
1644 sense = dasd_get_sense(irb);
1645 if (sense) {
1646 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1647 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1648 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1649 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1650 }
1651 if (!(fp_suppressed || nrf_suppressed))
1652 device->discipline->dump_sense_dbf(device, irb, "int");
1653
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001654 if (device->features & DASD_FEATURE_ERPLOG)
1655 device->discipline->dump_sense(device, cqr, irb);
1656 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001657 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001659
1660 /* check for for attention message */
1661 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1662 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland22021342017-01-25 14:47:32 +01001663 if (!IS_ERR(device)) {
1664 device->discipline->check_attention(device,
1665 irb->esw.esw1.lpum);
1666 dasd_put_device(device);
1667 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001668 }
1669
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001670 if (!cqr)
1671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673 device = (struct dasd_device *) cqr->startdev;
1674 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001676 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1677 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return;
1679 }
1680
1681 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001682 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001683 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001684 cqr->status = DASD_CQR_CLEARED;
1685 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001686 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001687 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return;
1689 }
1690
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001691 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001693 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1694 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return;
1696 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001697
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001698 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001700 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1701 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001702 /* request was completed successfully */
1703 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 cqr->stopclk = now;
1705 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001706 if (cqr->devlist.next != &device->ccw_queue) {
1707 next = list_entry(cqr->devlist.next,
1708 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001710 } else { /* error */
Stefan Haberlanda521b042016-08-08 15:56:54 +02001711 /* check for HPF error
1712 * call discipline function to requeue all requests
1713 * and disable HPF accordingly
1714 */
1715 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1716 device->discipline->handle_hpf_error)
1717 device->discipline->handle_hpf_error(device, irb);
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001718 /*
1719 * If we don't want complex ERP for this request, then just
1720 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001721 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001722 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001723 cqr->retries > 0) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001724 if (cqr->lpm == dasd_path_get_opm(device))
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001725 DBF_DEV_EVENT(DBF_DEBUG, device,
1726 "default ERP in fastpath "
1727 "(%i retries left)",
1728 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001729 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
Stefan Haberlandc9346152016-08-08 15:53:54 +02001730 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001731 cqr->status = DASD_CQR_QUEUED;
1732 next = cqr;
1733 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001735 }
1736 if (next && (next->status == DASD_CQR_QUEUED) &&
1737 (!device->stopped)) {
1738 if (device->discipline->start_IO(next) == 0)
1739 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001744 dasd_device_clear_timer(device);
1745 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001747EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001749enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1750{
1751 struct dasd_device *device;
1752
1753 device = dasd_device_from_cdev_locked(cdev);
1754
1755 if (IS_ERR(device))
1756 goto out;
1757 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1758 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001759 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001760 dasd_put_device(device);
1761 goto out;
1762 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001763 if (device->discipline->dump_sense_dbf)
1764 device->discipline->dump_sense_dbf(device, irb, "uc");
1765 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001766 dasd_put_device(device);
1767out:
1768 return UC_TODO_RETRY;
1769}
1770EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001773 * If we have an error on a dasd_block layer request then we cancel
1774 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001776static void __dasd_device_recovery(struct dasd_device *device,
1777 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 struct list_head *l, *n;
1780 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001783 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001785 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001787
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001788 list_for_each_safe(l, n, &device->ccw_queue) {
1789 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1790 if (cqr->status == DASD_CQR_QUEUED &&
1791 ref_cqr->block == cqr->block) {
1792 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001794 }
1795};
1796
1797/*
1798 * Remove those ccw requests from the queue that need to be returned
1799 * to the upper layer.
1800 */
1801static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1802 struct list_head *final_queue)
1803{
1804 struct list_head *l, *n;
1805 struct dasd_ccw_req *cqr;
1806
1807 /* Process request with final status. */
1808 list_for_each_safe(l, n, &device->ccw_queue) {
1809 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1810
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001811 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001812 if (cqr->status == DASD_CQR_QUEUED ||
1813 cqr->status == DASD_CQR_IN_IO ||
1814 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001815 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001816 if (cqr->status == DASD_CQR_ERROR) {
1817 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001819 /* Rechain finished requests to final queue */
1820 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822}
1823
Sebastian Ott5c618c02018-05-24 12:18:58 +02001824static void __dasd_process_cqr(struct dasd_device *device,
1825 struct dasd_ccw_req *cqr)
1826{
1827 char errorstring[ERRORLENGTH];
1828
1829 switch (cqr->status) {
1830 case DASD_CQR_SUCCESS:
1831 cqr->status = DASD_CQR_DONE;
1832 break;
1833 case DASD_CQR_ERROR:
1834 cqr->status = DASD_CQR_NEED_ERP;
1835 break;
1836 case DASD_CQR_CLEARED:
1837 cqr->status = DASD_CQR_TERMINATED;
1838 break;
1839 default:
1840 /* internal error 12 - wrong cqr status*/
1841 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1842 dev_err(&device->cdev->dev,
1843 "An error occurred in the DASD device driver, "
1844 "reason=%s\n", errorstring);
1845 BUG();
1846 }
1847 if (cqr->callback)
1848 cqr->callback(cqr, cqr->callback_data);
1849}
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001852 * the cqrs from the final queue are returned to the upper layer
1853 * by setting a dasd_block state and calling the callback function
1854 */
1855static void __dasd_device_process_final_queue(struct dasd_device *device,
1856 struct list_head *final_queue)
1857{
1858 struct list_head *l, *n;
1859 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001860 struct dasd_block *block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001861
1862 list_for_each_safe(l, n, final_queue) {
1863 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1864 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001865 block = cqr->block;
Sebastian Ott5c618c02018-05-24 12:18:58 +02001866 if (!block) {
1867 __dasd_process_cqr(device, cqr);
1868 } else {
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001869 spin_lock_bh(&block->queue_lock);
Sebastian Ott5c618c02018-05-24 12:18:58 +02001870 __dasd_process_cqr(device, cqr);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001871 spin_unlock_bh(&block->queue_lock);
Sebastian Ott5c618c02018-05-24 12:18:58 +02001872 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001873 }
1874}
1875
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001876/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * Take a look at the first request on the ccw queue and check
1878 * if it reached its expire time. If so, terminate the IO.
1879 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001880static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct dasd_ccw_req *cqr;
1883
1884 if (list_empty(&device->ccw_queue))
1885 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001886 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001887 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1888 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001889 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1890 /*
1891 * IO in safe offline processing should not
1892 * run out of retries
1893 */
1894 cqr->retries++;
1895 }
Horst Hummel29145a62006-12-04 15:40:15 +01001896 if (device->discipline->term_IO(cqr) != 0) {
1897 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001898 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001899 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001900 "ended, retrying in 5 s\n",
1901 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001902 cqr->expires += 5*HZ;
1903 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001904 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001905 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001906 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001907 "remaining\n", cqr, (cqr->expires/HZ),
1908 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 }
1911}
1912
1913/*
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001914 * return 1 when device is not eligible for IO
1915 */
1916static int __dasd_device_is_unusable(struct dasd_device *device,
1917 struct dasd_ccw_req *cqr)
1918{
1919 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1920
Stefan Haberlande8ac0152017-05-18 13:24:45 +02001921 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
1922 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1923 /*
1924 * dasd is being set offline
1925 * but it is no safe offline where we have to allow I/O
1926 */
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001927 return 1;
1928 }
1929 if (device->stopped) {
1930 if (device->stopped & mask) {
1931 /* stopped and CQR will not change that. */
1932 return 1;
1933 }
1934 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1935 /* CQR is not able to change device to
1936 * operational. */
1937 return 1;
1938 }
1939 /* CQR required to get device operational. */
1940 }
1941 return 0;
1942}
1943
1944/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * Take a look at the first request on the ccw queue and check
1946 * if it needs to be started.
1947 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001948static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct dasd_ccw_req *cqr;
1951 int rc;
1952
1953 if (list_empty(&device->ccw_queue))
1954 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001955 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001956 if (cqr->status != DASD_CQR_QUEUED)
1957 return;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001958 /* if device is not usable return request to upper layer */
1959 if (__dasd_device_is_unusable(device, cqr)) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001960 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961 cqr->status = DASD_CQR_CLEARED;
1962 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001963 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001964 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001965
1966 rc = device->discipline->start_IO(cqr);
1967 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001968 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001969 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001970 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001971 } else
1972 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001973 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001974}
1975
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001976static void __dasd_device_check_path_events(struct dasd_device *device)
1977{
1978 int rc;
1979
Stefan Haberlandc9346152016-08-08 15:53:54 +02001980 if (!dasd_path_get_tbvpm(device))
1981 return;
1982
1983 if (device->stopped &
1984 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1985 return;
1986 rc = device->discipline->verify_path(device,
1987 dasd_path_get_tbvpm(device));
1988 if (rc)
1989 dasd_device_set_timer(device, 50);
1990 else
1991 dasd_path_clear_all_verify(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001992};
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001995 * Go through all request on the dasd_device request queue,
1996 * terminate them on the cdev if necessary, and return them to the
1997 * submitting layer via callback.
1998 * Note:
1999 * Make sure that all 'submitting layers' still exist when
2000 * this function is called!. In other words, when 'device' is a base
2001 * device then all block layer requests must have been removed before
2002 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002004int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002006 struct dasd_ccw_req *cqr, *n;
2007 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 INIT_LIST_HEAD(&flush_queue);
2011 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02002012 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002013 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02002014 /* Check status and move request to flush_queue */
2015 switch (cqr->status) {
2016 case DASD_CQR_IN_IO:
2017 rc = device->discipline->term_IO(cqr);
2018 if (rc) {
2019 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002020 dev_err(&device->cdev->dev,
2021 "Flushing the DASD request queue "
2022 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002023 /* stop flush processing */
2024 goto finished;
2025 }
2026 break;
2027 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002028 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002029 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002030 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002031 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002032 break;
2033 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002035 }
Horst Hummel8f617012006-08-30 14:33:33 +02002036finished:
2037 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002038 /*
2039 * After this point all requests must be in state CLEAR_PENDING,
2040 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2041 * one of the others.
2042 */
2043 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2044 wait_event(dasd_flush_wq,
2045 (cqr->status != DASD_CQR_CLEAR_PENDING));
2046 /*
2047 * Now set each request back to TERMINATED, DONE or NEED_ERP
2048 * and call the callback function of flushed requests
2049 */
2050 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002051 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002053EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055/*
2056 * Acquire the device lock and process queues for the device.
2057 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002058static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 atomic_set (&device->tasklet_scheduled, 0);
2063 INIT_LIST_HEAD(&final_queue);
2064 spin_lock_irq(get_ccwdev_lock(device->cdev));
2065 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002066 __dasd_device_check_expire(device);
2067 /* find final requests on ccw queue */
2068 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002069 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2071 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 __dasd_device_process_final_queue(device, &final_queue);
2073 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002075 __dasd_device_start_head(device);
2076 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002077 if (waitqueue_active(&shutdown_waitq))
2078 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 dasd_put_device(device);
2080}
2081
2082/*
2083 * Schedules a call to dasd_tasklet over the device tasklet.
2084 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002085void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002088 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return;
2090 dasd_get_device(device);
2091 tasklet_hi_schedule(&device->tasklet);
2092}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002093EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002095void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2096{
2097 device->stopped |= bits;
2098}
2099EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2100
2101void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2102{
2103 device->stopped &= ~bits;
2104 if (!device->stopped)
2105 wake_up(&generic_waitq);
2106}
2107EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002110 * Queue a request to the head of the device ccw_queue.
2111 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002113void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 struct dasd_device *device;
2116 unsigned long flags;
2117
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002118 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002120 cqr->status = DASD_CQR_QUEUED;
2121 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002123 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2125}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002126EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002129 * Queue a request to the tail of the device ccw_queue.
2130 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002132void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 struct dasd_device *device;
2135 unsigned long flags;
2136
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002137 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002139 cqr->status = DASD_CQR_QUEUED;
2140 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002142 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2144}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002145EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002148 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002150void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002152 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2153 cqr->callback_data = DASD_SLEEPON_END_TAG;
2154 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2155 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
Stefan Haberland5915a872011-10-30 15:16:57 +01002157EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002159static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct dasd_device *device;
2162 int rc;
2163
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002164 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002166 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2168 return rc;
2169}
2170
2171/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002172 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2173 */
2174static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2175{
2176 struct dasd_device *device;
2177 dasd_erp_fn_t erp_fn;
2178
2179 if (cqr->status == DASD_CQR_FILLED)
2180 return 0;
2181 device = cqr->startdev;
2182 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2183 if (cqr->status == DASD_CQR_TERMINATED) {
2184 device->discipline->handle_terminated_request(cqr);
2185 return 1;
2186 }
2187 if (cqr->status == DASD_CQR_NEED_ERP) {
2188 erp_fn = device->discipline->erp_action(cqr);
2189 erp_fn(cqr);
2190 return 1;
2191 }
2192 if (cqr->status == DASD_CQR_FAILED)
2193 dasd_log_sense(cqr, &cqr->irb);
2194 if (cqr->refers) {
2195 __dasd_process_erp(device, cqr);
2196 return 1;
2197 }
2198 }
2199 return 0;
2200}
2201
2202static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2203{
2204 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2205 if (cqr->refers) /* erp is not done yet */
2206 return 1;
2207 return ((cqr->status != DASD_CQR_DONE) &&
2208 (cqr->status != DASD_CQR_FAILED));
2209 } else
2210 return (cqr->status == DASD_CQR_FILLED);
2211}
2212
2213static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2214{
2215 struct dasd_device *device;
2216 int rc;
2217 struct list_head ccw_queue;
2218 struct dasd_ccw_req *cqr;
2219
2220 INIT_LIST_HEAD(&ccw_queue);
2221 maincqr->status = DASD_CQR_FILLED;
2222 device = maincqr->startdev;
2223 list_add(&maincqr->blocklist, &ccw_queue);
2224 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2225 cqr = list_first_entry(&ccw_queue,
2226 struct dasd_ccw_req, blocklist)) {
2227
2228 if (__dasd_sleep_on_erp(cqr))
2229 continue;
2230 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2231 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002232 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2233 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2234 cqr->status = DASD_CQR_FAILED;
2235 cqr->intrc = -EPERM;
2236 continue;
2237 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002238 /* Non-temporary stop condition will trigger fail fast */
2239 if (device->stopped & ~DASD_STOPPED_PENDING &&
2240 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2241 (!dasd_eer_enabled(device))) {
2242 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002243 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002244 continue;
2245 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002246 /*
Stefan Haberlanda9f62732016-09-20 10:29:22 +02002247 * Don't try to start requests if device is in
2248 * offline processing, it might wait forever
2249 */
2250 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2251 cqr->status = DASD_CQR_FAILED;
2252 cqr->intrc = -ENODEV;
2253 continue;
2254 }
2255 /*
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002256 * Don't try to start requests if device is stopped
2257 * except path verification requests
2258 */
2259 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2260 if (interruptible) {
2261 rc = wait_event_interruptible(
2262 generic_waitq, !(device->stopped));
2263 if (rc == -ERESTARTSYS) {
2264 cqr->status = DASD_CQR_FAILED;
2265 maincqr->intrc = rc;
2266 continue;
2267 }
2268 } else
2269 wait_event(generic_waitq, !(device->stopped));
2270 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002271 if (!cqr->callback)
2272 cqr->callback = dasd_wakeup_cb;
2273
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002274 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002275 dasd_add_request_tail(cqr);
2276 if (interruptible) {
2277 rc = wait_event_interruptible(
2278 generic_waitq, _wait_for_wakeup(cqr));
2279 if (rc == -ERESTARTSYS) {
2280 dasd_cancel_req(cqr);
2281 /* wait (non-interruptible) for final status */
2282 wait_event(generic_waitq,
2283 _wait_for_wakeup(cqr));
2284 cqr->status = DASD_CQR_FAILED;
2285 maincqr->intrc = rc;
2286 continue;
2287 }
2288 } else
2289 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2290 }
2291
Heiko Carstens1aae0562013-01-30 09:49:40 +01002292 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002293 if ((maincqr->status != DASD_CQR_DONE) &&
2294 (maincqr->intrc != -ERESTARTSYS))
2295 dasd_log_sense(maincqr, &maincqr->irb);
2296 if (maincqr->status == DASD_CQR_DONE)
2297 rc = 0;
2298 else if (maincqr->intrc)
2299 rc = maincqr->intrc;
2300 else
2301 rc = -EIO;
2302 return rc;
2303}
2304
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002305static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2306{
2307 struct dasd_ccw_req *cqr;
2308
2309 list_for_each_entry(cqr, ccw_queue, blocklist) {
2310 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2311 return 0;
2312 }
2313
2314 return 1;
2315}
2316
2317static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2318{
2319 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002320 struct dasd_ccw_req *cqr, *n;
Jan Höppner8fd57522015-08-19 13:41:20 +02002321 u8 *sense = NULL;
Stefan Haberland362ce842014-10-01 13:04:54 +02002322 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002323
2324retry:
2325 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2326 device = cqr->startdev;
2327 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2328 continue;
2329
2330 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2331 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2332 cqr->status = DASD_CQR_FAILED;
2333 cqr->intrc = -EPERM;
2334 continue;
2335 }
2336 /*Non-temporary stop condition will trigger fail fast*/
2337 if (device->stopped & ~DASD_STOPPED_PENDING &&
2338 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2339 !dasd_eer_enabled(device)) {
2340 cqr->status = DASD_CQR_FAILED;
2341 cqr->intrc = -EAGAIN;
2342 continue;
2343 }
2344
2345 /*Don't try to start requests if device is stopped*/
2346 if (interruptible) {
2347 rc = wait_event_interruptible(
2348 generic_waitq, !device->stopped);
2349 if (rc == -ERESTARTSYS) {
2350 cqr->status = DASD_CQR_FAILED;
2351 cqr->intrc = rc;
2352 continue;
2353 }
2354 } else
2355 wait_event(generic_waitq, !(device->stopped));
2356
2357 if (!cqr->callback)
2358 cqr->callback = dasd_wakeup_cb;
2359 cqr->callback_data = DASD_SLEEPON_START_TAG;
2360 dasd_add_request_tail(cqr);
2361 }
2362
2363 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2364
2365 rc = 0;
2366 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002367 /*
Jan Höppner8fd57522015-08-19 13:41:20 +02002368 * In some cases the 'File Protected' or 'Incorrect Length'
2369 * error might be expected and error recovery would be
2370 * unnecessary in these cases. Check if the according suppress
2371 * bit is set.
2372 */
2373 sense = dasd_get_sense(&cqr->irb);
2374 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2375 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2376 continue;
2377 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2378 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2379 continue;
2380
2381 /*
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002382 * for alias devices simplify error recovery and
2383 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002384 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002385 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002386 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002387 (cqr->status == DASD_CQR_TERMINATED ||
2388 cqr->status == DASD_CQR_NEED_ERP))
2389 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002390
2391 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002392 if (__dasd_sleep_on_erp(cqr))
2393 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002394 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002395 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002396
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002397 return 0;
2398}
2399
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002400/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002401 * Queue a request to the tail of the device ccw_queue and wait for
2402 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002404int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002406 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002408EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002411 * Start requests from a ccw_queue and wait for their completion.
2412 */
2413int dasd_sleep_on_queue(struct list_head *ccw_queue)
2414{
2415 return _dasd_sleep_on_queue(ccw_queue, 0);
2416}
2417EXPORT_SYMBOL(dasd_sleep_on_queue);
2418
2419/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002420 * Queue a request to the tail of the device ccw_queue and wait
2421 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002423int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002425 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002427EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429/*
2430 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2431 * for eckd devices) the currently running request has to be terminated
2432 * and be put back to status queued, before the special request is added
2433 * to the head of the queue. Then the special request is waited on normally.
2434 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002435static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002438 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (list_empty(&device->ccw_queue))
2441 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002442 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002443 rc = device->discipline->term_IO(cqr);
2444 if (!rc)
2445 /*
2446 * CQR terminated because a more important request is pending.
2447 * Undo decreasing of retry counter because this is
2448 * not an error case.
2449 */
2450 cqr->retries++;
2451 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002454int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct dasd_device *device;
2457 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002458
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002459 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002460 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2461 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2462 cqr->status = DASD_CQR_FAILED;
2463 cqr->intrc = -EPERM;
2464 return -EIO;
2465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 spin_lock_irq(get_ccwdev_lock(device->cdev));
2467 rc = _dasd_term_running_cqr(device);
2468 if (rc) {
2469 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2470 return rc;
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002473 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002475 /*
2476 * add new request as second
2477 * first the terminated cqr needs to be finished
2478 */
2479 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2485
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002486 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002487
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002488 if (cqr->status == DASD_CQR_DONE)
2489 rc = 0;
2490 else if (cqr->intrc)
2491 rc = cqr->intrc;
2492 else
2493 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002494
2495 /* kick tasklets */
2496 dasd_schedule_device_bh(device);
2497 if (device->block)
2498 dasd_schedule_block_bh(device->block);
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return rc;
2501}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002502EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504/*
2505 * Cancels a request that was started with dasd_sleep_on_req.
2506 * This is useful to timeout requests. The request will be
2507 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002508 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002509 * negative error code if termination failed
2510 * Cancellation of a request is an asynchronous operation! The calling
2511 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002513static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002515 struct dasd_device *device = cqr->startdev;
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002516 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 switch (cqr->status) {
2519 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002520 /* request was not started - just set to cleared */
2521 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 break;
2523 case DASD_CQR_IN_IO:
2524 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002525 rc = device->discipline->term_IO(cqr);
2526 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002527 dev_err(&device->cdev->dev,
2528 "Cancelling request %p failed with rc=%d\n",
2529 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002530 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002531 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002534 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002537 dasd_schedule_device_bh(device);
2538 return rc;
2539}
Sebastian Ott1bcdb532018-05-16 11:25:21 +02002540
2541int dasd_cancel_req(struct dasd_ccw_req *cqr)
2542{
2543 struct dasd_device *device = cqr->startdev;
2544 unsigned long flags;
2545 int rc;
2546
2547 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2548 rc = __dasd_cancel_req(cqr);
2549 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2550 return rc;
2551}
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002552
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002553/*
2554 * SECTION: Operations of the dasd_block layer.
2555 */
2556
2557/*
2558 * Timeout function for dasd_block. This is used when the block layer
2559 * is waiting for something that may not come reliably, (e.g. a state
2560 * change interrupt)
2561 */
Kees Cooke99e88a2017-10-16 14:43:17 -07002562static void dasd_block_timeout(struct timer_list *t)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002563{
2564 unsigned long flags;
2565 struct dasd_block *block;
2566
Kees Cooke99e88a2017-10-16 14:43:17 -07002567 block = from_timer(block, t, timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002568 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2569 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002570 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002571 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2572 dasd_schedule_block_bh(block);
Stefan Haberlande4433432017-05-22 10:59:11 +02002573 blk_mq_run_hw_queues(block->request_queue, true);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002574}
2575
2576/*
2577 * Setup timeout for a dasd_block in jiffies.
2578 */
2579void dasd_block_set_timer(struct dasd_block *block, int expires)
2580{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002581 if (expires == 0)
2582 del_timer(&block->timer);
2583 else
2584 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002585}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002586EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002587
2588/*
2589 * Clear timeout for a dasd_block.
2590 */
2591void dasd_block_clear_timer(struct dasd_block *block)
2592{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002593 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002594}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002595EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002596
2597/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002598 * Process finished error recovery ccw.
2599 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002600static void __dasd_process_erp(struct dasd_device *device,
2601 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002602{
2603 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002604
2605 if (cqr->status == DASD_CQR_DONE)
2606 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2607 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002608 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002609 erp_fn = device->discipline->erp_postaction(cqr);
2610 erp_fn(cqr);
2611}
2612
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002613static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2614{
2615 struct request *req;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002616 blk_status_t error = BLK_STS_OK;
Stefan Haberlande4433432017-05-22 10:59:11 +02002617 int status;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002618
2619 req = (struct request *) cqr->callback_data;
2620 dasd_profile_end(cqr->block, cqr, req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002621
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002622 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002623 if (status < 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002624 error = errno_to_blk_status(status);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002625 else if (status == 0) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002626 switch (cqr->intrc) {
2627 case -EPERM:
2628 error = BLK_STS_NEXUS;
2629 break;
2630 case -ENOLINK:
2631 error = BLK_STS_TRANSPORT;
2632 break;
2633 case -ETIMEDOUT:
2634 error = BLK_STS_TIMEOUT;
2635 break;
2636 default:
2637 error = BLK_STS_IOERR;
2638 break;
2639 }
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002640 }
Stefan Haberlande4433432017-05-22 10:59:11 +02002641
2642 /*
2643 * We need to take care for ETIMEDOUT errors here since the
2644 * complete callback does not get called in this case.
2645 * Take care of all errors here and avoid additional code to
2646 * transfer the error value to the complete callback.
2647 */
2648 if (error) {
2649 blk_mq_end_request(req, error);
2650 blk_mq_run_hw_queues(req->q, true);
2651 } else {
2652 blk_mq_complete_request(req);
2653 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002654}
2655
2656/*
2657 * Process ccw request queue.
2658 */
2659static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2660 struct list_head *final_queue)
2661{
2662 struct list_head *l, *n;
2663 struct dasd_ccw_req *cqr;
2664 dasd_erp_fn_t erp_fn;
2665 unsigned long flags;
2666 struct dasd_device *base = block->base;
2667
2668restart:
2669 /* Process request with final status. */
2670 list_for_each_safe(l, n, &block->ccw_queue) {
2671 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2672 if (cqr->status != DASD_CQR_DONE &&
2673 cqr->status != DASD_CQR_FAILED &&
2674 cqr->status != DASD_CQR_NEED_ERP &&
2675 cqr->status != DASD_CQR_TERMINATED)
2676 continue;
2677
2678 if (cqr->status == DASD_CQR_TERMINATED) {
2679 base->discipline->handle_terminated_request(cqr);
2680 goto restart;
2681 }
2682
2683 /* Process requests that may be recovered */
2684 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002685 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002686 if (IS_ERR(erp_fn(cqr)))
2687 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002688 goto restart;
2689 }
2690
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002691 /* log sense for fatal error */
2692 if (cqr->status == DASD_CQR_FAILED) {
2693 dasd_log_sense(cqr, &cqr->irb);
2694 }
2695
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002696 /* First of all call extended error reporting. */
2697 if (dasd_eer_enabled(base) &&
2698 cqr->status == DASD_CQR_FAILED) {
2699 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2700
2701 /* restart request */
2702 cqr->status = DASD_CQR_FILLED;
2703 cqr->retries = 255;
2704 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002705 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002706 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2707 flags);
2708 goto restart;
2709 }
2710
2711 /* Process finished ERP request. */
2712 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002713 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002714 goto restart;
2715 }
2716
2717 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002718 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002719 list_move_tail(&cqr->blocklist, final_queue);
2720 }
2721}
2722
2723static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2724{
2725 dasd_schedule_block_bh(cqr->block);
2726}
2727
2728static void __dasd_block_start_head(struct dasd_block *block)
2729{
2730 struct dasd_ccw_req *cqr;
2731
2732 if (list_empty(&block->ccw_queue))
2733 return;
2734 /* We allways begin with the first requests on the queue, as some
2735 * of previously started requests have to be enqueued on a
2736 * dasd_device again for error recovery.
2737 */
2738 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2739 if (cqr->status != DASD_CQR_FILLED)
2740 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002741 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2742 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2743 cqr->status = DASD_CQR_FAILED;
2744 cqr->intrc = -EPERM;
2745 dasd_schedule_block_bh(block);
2746 continue;
2747 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002748 /* Non-temporary stop condition will trigger fail fast */
2749 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2750 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2751 (!dasd_eer_enabled(block->base))) {
2752 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002753 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002754 dasd_schedule_block_bh(block);
2755 continue;
2756 }
2757 /* Don't try to start requests if device is stopped */
2758 if (block->base->stopped)
2759 return;
2760
2761 /* just a fail safe check, should not happen */
2762 if (!cqr->startdev)
2763 cqr->startdev = block->base;
2764
2765 /* make sure that the requests we submit find their way back */
2766 cqr->callback = dasd_return_cqr_cb;
2767
2768 dasd_add_request_tail(cqr);
2769 }
2770}
2771
2772/*
2773 * Central dasd_block layer routine. Takes requests from the generic
2774 * block layer request queue, creates ccw requests, enqueues them on
2775 * a dasd_device and processes ccw requests that have been returned.
2776 */
2777static void dasd_block_tasklet(struct dasd_block *block)
2778{
2779 struct list_head final_queue;
2780 struct list_head *l, *n;
2781 struct dasd_ccw_req *cqr;
Stefan Haberlande4433432017-05-22 10:59:11 +02002782 struct dasd_queue *dq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002783
2784 atomic_set(&block->tasklet_scheduled, 0);
2785 INIT_LIST_HEAD(&final_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02002786 spin_lock_irq(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002787 /* Finish off requests on ccw queue */
2788 __dasd_process_block_ccw_queue(block, &final_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02002789 spin_unlock_irq(&block->queue_lock);
2790
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002791 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002792 list_for_each_safe(l, n, &final_queue) {
2793 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
Stefan Haberlande4433432017-05-22 10:59:11 +02002794 dq = cqr->dq;
2795 spin_lock_irq(&dq->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002796 list_del_init(&cqr->blocklist);
2797 __dasd_cleanup_cqr(cqr);
Stefan Haberlande4433432017-05-22 10:59:11 +02002798 spin_unlock_irq(&dq->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002799 }
Stefan Haberlande4433432017-05-22 10:59:11 +02002800
2801 spin_lock_irq(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002802 /* Now check if the head of the ccw queue needs to be started. */
2803 __dasd_block_start_head(block);
Stefan Haberlande4433432017-05-22 10:59:11 +02002804 spin_unlock_irq(&block->queue_lock);
2805
Stefan Haberland4679e892012-06-19 17:30:12 +02002806 if (waitqueue_active(&shutdown_waitq))
2807 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002808 dasd_put_device(block->base);
2809}
2810
2811static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2812{
2813 wake_up(&dasd_flush_wq);
2814}
2815
2816/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002817 * Requeue a request back to the block request queue
2818 * only works for block requests
2819 */
2820static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2821{
2822 struct dasd_block *block = cqr->block;
2823 struct request *req;
Stefan Haberlandc5576872013-04-15 16:41:31 +02002824
2825 if (!block)
2826 return -EINVAL;
Stefan Haberlande4433432017-05-22 10:59:11 +02002827 spin_lock_irq(&cqr->dq->lock);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002828 req = (struct request *) cqr->callback_data;
Stefan Haberlande4433432017-05-22 10:59:11 +02002829 blk_mq_requeue_request(req, false);
2830 spin_unlock_irq(&cqr->dq->lock);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002831
2832 return 0;
2833}
2834
2835/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002836 * Go through all request on the dasd_block request queue, cancel them
2837 * on the respective dasd_device, and return them to the generic
2838 * block layer.
2839 */
2840static int dasd_flush_block_queue(struct dasd_block *block)
2841{
2842 struct dasd_ccw_req *cqr, *n;
2843 int rc, i;
2844 struct list_head flush_queue;
Stefan Haberlande4433432017-05-22 10:59:11 +02002845 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002846
2847 INIT_LIST_HEAD(&flush_queue);
2848 spin_lock_bh(&block->queue_lock);
2849 rc = 0;
2850restart:
2851 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2852 /* if this request currently owned by a dasd_device cancel it */
2853 if (cqr->status >= DASD_CQR_QUEUED)
2854 rc = dasd_cancel_req(cqr);
2855 if (rc < 0)
2856 break;
2857 /* Rechain request (including erp chain) so it won't be
2858 * touched by the dasd_block_tasklet anymore.
2859 * Replace the callback so we notice when the request
2860 * is returned from the dasd_device layer.
2861 */
2862 cqr->callback = _dasd_wake_block_flush_cb;
2863 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2864 list_move_tail(&cqr->blocklist, &flush_queue);
2865 if (i > 1)
2866 /* moved more than one request - need to restart */
2867 goto restart;
2868 }
2869 spin_unlock_bh(&block->queue_lock);
2870 /* Now call the callback function of flushed requests */
2871restart_cb:
2872 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2873 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2874 /* Process finished ERP request. */
2875 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002876 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002877 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002878 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002879 /* restart list_for_xx loop since dasd_process_erp
2880 * might remove multiple elements */
2881 goto restart_cb;
2882 }
2883 /* call the callback function */
Stefan Haberlande4433432017-05-22 10:59:11 +02002884 spin_lock_irqsave(&cqr->dq->lock, flags);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002885 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002886 list_del_init(&cqr->blocklist);
2887 __dasd_cleanup_cqr(cqr);
Stefan Haberlande4433432017-05-22 10:59:11 +02002888 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return rc;
2891}
2892
2893/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002894 * Schedules a call to dasd_tasklet over the device tasklet.
2895 */
2896void dasd_schedule_block_bh(struct dasd_block *block)
2897{
2898 /* Protect against rescheduling. */
2899 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2900 return;
2901 /* life cycle of block is bound to it's base device */
2902 dasd_get_device(block->base);
2903 tasklet_hi_schedule(&block->tasklet);
2904}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002905EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002906
2907
2908/*
2909 * SECTION: external block device operations
2910 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 */
2912
2913/*
2914 * Dasd request queue function. Called from ll_rw_blk.c
2915 */
Stefan Haberlande4433432017-05-22 10:59:11 +02002916static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
2917 const struct blk_mq_queue_data *qd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Stefan Haberlande4433432017-05-22 10:59:11 +02002919 struct dasd_block *block = hctx->queue->queuedata;
2920 struct dasd_queue *dq = hctx->driver_data;
2921 struct request *req = qd->rq;
2922 struct dasd_device *basedev;
2923 struct dasd_ccw_req *cqr;
2924 blk_status_t rc = BLK_STS_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Stefan Haberlande4433432017-05-22 10:59:11 +02002926 basedev = block->base;
2927 spin_lock_irq(&dq->lock);
2928 if (basedev->state < DASD_STATE_READY) {
2929 DBF_DEV_EVENT(DBF_ERR, basedev,
2930 "device not ready for request %p", req);
2931 rc = BLK_STS_IOERR;
2932 goto out;
2933 }
2934
2935 /*
2936 * if device is stopped do not fetch new requests
2937 * except failfast is active which will let requests fail
2938 * immediately in __dasd_block_start_head()
2939 */
2940 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
2941 DBF_DEV_EVENT(DBF_ERR, basedev,
2942 "device stopped request %p", req);
2943 rc = BLK_STS_RESOURCE;
2944 goto out;
2945 }
2946
2947 if (basedev->features & DASD_FEATURE_READONLY &&
2948 rq_data_dir(req) == WRITE) {
2949 DBF_DEV_EVENT(DBF_ERR, basedev,
2950 "Rejecting write request %p", req);
2951 rc = BLK_STS_IOERR;
2952 goto out;
2953 }
2954
2955 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2956 (basedev->features & DASD_FEATURE_FAILFAST ||
2957 blk_noretry_request(req))) {
2958 DBF_DEV_EVENT(DBF_ERR, basedev,
2959 "Rejecting failfast request %p", req);
2960 rc = BLK_STS_IOERR;
2961 goto out;
2962 }
2963
2964 cqr = basedev->discipline->build_cp(basedev, block, req);
2965 if (IS_ERR(cqr)) {
2966 if (PTR_ERR(cqr) == -EBUSY ||
2967 PTR_ERR(cqr) == -ENOMEM ||
2968 PTR_ERR(cqr) == -EAGAIN) {
2969 rc = BLK_STS_RESOURCE;
2970 goto out;
2971 }
2972 DBF_DEV_EVENT(DBF_ERR, basedev,
2973 "CCW creation failed (rc=%ld) on request %p",
2974 PTR_ERR(cqr), req);
2975 rc = BLK_STS_IOERR;
2976 goto out;
2977 }
2978 /*
2979 * Note: callback is set to dasd_return_cqr_cb in
2980 * __dasd_block_start_head to cover erp requests as well
2981 */
2982 cqr->callback_data = req;
2983 cqr->status = DASD_CQR_FILLED;
2984 cqr->dq = dq;
Sebastian Ottf0f59a22018-05-15 14:05:13 +02002985
Stefan Haberlande4433432017-05-22 10:59:11 +02002986 blk_mq_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002987 spin_lock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02002988 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2989 INIT_LIST_HEAD(&cqr->devlist);
2990 dasd_profile_start(block, cqr, req);
2991 dasd_schedule_block_bh(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002992 spin_unlock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02002993
2994out:
2995 spin_unlock_irq(&dq->lock);
2996 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
2999/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00003000 * Block timeout callback, called from the block layer
3001 *
Hannes Reineckea2ace462013-01-30 09:26:14 +00003002 * Return values:
3003 * BLK_EH_RESET_TIMER if the request should be left running
Christoph Hellwig66005932018-05-29 15:52:29 +02003004 * BLK_EH_DONE if the request is handled or terminated
Hannes Reineckea2ace462013-01-30 09:26:14 +00003005 * by the driver.
3006 */
Stefan Haberlande4433432017-05-22 10:59:11 +02003007enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
Hannes Reineckea2ace462013-01-30 09:26:14 +00003008{
Hannes Reineckea2ace462013-01-30 09:26:14 +00003009 struct dasd_block *block = req->q->queuedata;
3010 struct dasd_device *device;
Sebastian Ottf0f59a22018-05-15 14:05:13 +02003011 struct dasd_ccw_req *cqr;
Stefan Haberlande4433432017-05-22 10:59:11 +02003012 unsigned long flags;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003013 int rc = 0;
3014
Sebastian Ottc5205f22018-06-04 19:07:39 +02003015 cqr = blk_mq_rq_to_pdu(req);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003016 if (!cqr)
Christoph Hellwig66005932018-05-29 15:52:29 +02003017 return BLK_EH_DONE;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003018
Stefan Haberlande4433432017-05-22 10:59:11 +02003019 spin_lock_irqsave(&cqr->dq->lock, flags);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003020 device = cqr->startdev ? cqr->startdev : block->base;
Stefan Haberlande4433432017-05-22 10:59:11 +02003021 if (!device->blk_timeout) {
3022 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00003023 return BLK_EH_RESET_TIMER;
Stefan Haberlande4433432017-05-22 10:59:11 +02003024 }
Hannes Reineckea2ace462013-01-30 09:26:14 +00003025 DBF_DEV_EVENT(DBF_WARNING, device,
3026 " dasd_times_out cqr %p status %x",
3027 cqr, cqr->status);
3028
3029 spin_lock(&block->queue_lock);
3030 spin_lock(get_ccwdev_lock(device->cdev));
3031 cqr->retries = -1;
3032 cqr->intrc = -ETIMEDOUT;
3033 if (cqr->status >= DASD_CQR_QUEUED) {
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003034 rc = __dasd_cancel_req(cqr);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003035 } else if (cqr->status == DASD_CQR_FILLED ||
3036 cqr->status == DASD_CQR_NEED_ERP) {
3037 cqr->status = DASD_CQR_TERMINATED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003038 } else if (cqr->status == DASD_CQR_IN_ERP) {
3039 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3040
3041 list_for_each_entry_safe(searchcqr, nextcqr,
3042 &block->ccw_queue, blocklist) {
3043 tmpcqr = searchcqr;
3044 while (tmpcqr->refers)
3045 tmpcqr = tmpcqr->refers;
3046 if (tmpcqr != cqr)
3047 continue;
3048 /* searchcqr is an ERP request for cqr */
3049 searchcqr->retries = -1;
3050 searchcqr->intrc = -ETIMEDOUT;
3051 if (searchcqr->status >= DASD_CQR_QUEUED) {
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003052 rc = __dasd_cancel_req(searchcqr);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003053 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3054 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3055 searchcqr->status = DASD_CQR_TERMINATED;
3056 rc = 0;
3057 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3058 /*
3059 * Shouldn't happen; most recent ERP
3060 * request is at the front of queue
3061 */
3062 continue;
3063 }
3064 break;
3065 }
Hannes Reineckea2ace462013-01-30 09:26:14 +00003066 }
Sebastian Ott1bcdb532018-05-16 11:25:21 +02003067 spin_unlock(get_ccwdev_lock(device->cdev));
Hannes Reineckea2ace462013-01-30 09:26:14 +00003068 dasd_schedule_block_bh(block);
3069 spin_unlock(&block->queue_lock);
Stefan Haberlande4433432017-05-22 10:59:11 +02003070 spin_unlock_irqrestore(&cqr->dq->lock, flags);
Hannes Reineckea2ace462013-01-30 09:26:14 +00003071
Christoph Hellwig66005932018-05-29 15:52:29 +02003072 return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003073}
3074
Stefan Haberlande4433432017-05-22 10:59:11 +02003075static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
3076 unsigned int idx)
3077{
3078 struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
3079
3080 if (!dq)
3081 return -ENOMEM;
3082
3083 spin_lock_init(&dq->lock);
3084 hctx->driver_data = dq;
3085
3086 return 0;
3087}
3088
3089static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
3090{
3091 kfree(hctx->driver_data);
3092 hctx->driver_data = NULL;
3093}
3094
3095static void dasd_request_done(struct request *req)
3096{
3097 blk_mq_end_request(req, 0);
3098 blk_mq_run_hw_queues(req->q, true);
3099}
3100
3101static struct blk_mq_ops dasd_mq_ops = {
3102 .queue_rq = do_dasd_request,
3103 .complete = dasd_request_done,
3104 .timeout = dasd_times_out,
3105 .init_hctx = dasd_init_hctx,
3106 .exit_hctx = dasd_exit_hctx,
3107};
3108
Hannes Reineckea2ace462013-01-30 09:26:14 +00003109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 * Allocate and initialize request queue and default I/O scheduler.
3111 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003112static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Stefan Haberlande4433432017-05-22 10:59:11 +02003114 int rc;
3115
3116 block->tag_set.ops = &dasd_mq_ops;
Sebastian Ottc5205f22018-06-04 19:07:39 +02003117 block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
Stefan Haberlande4433432017-05-22 10:59:11 +02003118 block->tag_set.nr_hw_queues = DASD_NR_HW_QUEUES;
3119 block->tag_set.queue_depth = DASD_MAX_LCU_DEV * DASD_REQ_PER_DEV;
3120 block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Vasily Gorbikb17e3ab2018-06-24 09:21:59 +02003121 block->tag_set.numa_node = NUMA_NO_NODE;
Stefan Haberlande4433432017-05-22 10:59:11 +02003122
3123 rc = blk_mq_alloc_tag_set(&block->tag_set);
3124 if (rc)
3125 return rc;
3126
3127 block->request_queue = blk_mq_init_queue(&block->tag_set);
3128 if (IS_ERR(block->request_queue))
3129 return PTR_ERR(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003131 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01003133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
3136/*
3137 * Allocate and initialize request queue.
3138 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003139static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Jan Höppner28b841b2016-06-30 13:28:57 +02003141 unsigned int logical_block_size = block->bp_block;
Jan Höppner62ba6f82016-06-30 13:18:16 +02003142 struct request_queue *q = block->request_queue;
Jan Höppner28b841b2016-06-30 13:28:57 +02003143 unsigned int max_bytes, max_discard_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 int max;
3145
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003146 if (block->base->features & DASD_FEATURE_USERAW) {
3147 /*
3148 * the max_blocks value for raw_track access is 256
3149 * it is higher than the native ECKD value because we
3150 * only need one ccw per track
3151 * so the max_hw_sectors are
3152 * 2048 x 512B = 1024kB = 16 tracks
3153 */
3154 max = 2048;
3155 } else {
3156 max = block->base->discipline->max_blocks << block->s2b_shift;
3157 }
Bart Van Assche8b904b52018-03-07 17:10:10 -08003158 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Jan Höppner62ba6f82016-06-30 13:18:16 +02003159 q->limits.max_dev_sectors = max;
Jan Höppner28b841b2016-06-30 13:28:57 +02003160 blk_queue_logical_block_size(q, logical_block_size);
Jan Höppner62ba6f82016-06-30 13:18:16 +02003161 blk_queue_max_hw_sectors(q, max);
3162 blk_queue_max_segments(q, USHRT_MAX);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003163 /* with page sized segments we can translate each segement into
3164 * one idaw/tidaw
3165 */
Jan Höppner62ba6f82016-06-30 13:18:16 +02003166 blk_queue_max_segment_size(q, PAGE_SIZE);
3167 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
Jan Höppner28b841b2016-06-30 13:28:57 +02003168
3169 /* Only activate blocklayer discard support for devices that support it */
3170 if (block->base->features & DASD_FEATURE_DISCARD) {
3171 q->limits.discard_granularity = logical_block_size;
3172 q->limits.discard_alignment = PAGE_SIZE;
3173
3174 /* Calculate max_discard_sectors and make it PAGE aligned */
3175 max_bytes = USHRT_MAX * logical_block_size;
3176 max_bytes = ALIGN(max_bytes, PAGE_SIZE) - PAGE_SIZE;
3177 max_discard_sectors = max_bytes / logical_block_size;
3178
3179 blk_queue_max_discard_sectors(q, max_discard_sectors);
3180 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
Bart Van Assche8b904b52018-03-07 17:10:10 -08003181 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Jan Höppner28b841b2016-06-30 13:28:57 +02003182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
3185/*
3186 * Deactivate and free request queue.
3187 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003188static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003190 if (block->request_queue) {
3191 blk_cleanup_queue(block->request_queue);
Stefan Haberlande4433432017-05-22 10:59:11 +02003192 blk_mq_free_tag_set(&block->tag_set);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003193 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195}
3196
Al Viro57a7c0b2008-03-02 10:36:08 -05003197static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003199 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 int rc;
3201
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003202 base = dasd_device_from_gendisk(bdev->bd_disk);
3203 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003204 return -ENODEV;
3205
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003206 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003207 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 rc = -ENODEV;
3209 goto unlock;
3210 }
3211
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003212 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 rc = -EINVAL;
3214 goto unlock;
3215 }
3216
3217 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003218 dev_info(&base->cdev->dev,
3219 "Accessing the DASD failed because it is in "
3220 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 rc = -EPERM;
3222 goto out;
3223 }
3224
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003225 if (base->state <= DASD_STATE_BASIC) {
3226 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 " Cannot open unrecognized device");
3228 rc = -ENODEV;
3229 goto out;
3230 }
3231
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003232 if ((mode & FMODE_WRITE) &&
3233 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3234 (base->features & DASD_FEATURE_READONLY))) {
3235 rc = -EROFS;
3236 goto out;
3237 }
3238
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003239 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241
3242out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003243 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003245 atomic_dec(&base->block->open_count);
3246 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return rc;
3248}
3249
Al Virodb2a1442013-05-05 21:52:57 -04003250static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Al Virodb2a1442013-05-05 21:52:57 -04003252 struct dasd_device *base = dasd_device_from_gendisk(disk);
3253 if (base) {
3254 atomic_dec(&base->block->open_count);
3255 module_put(base->discipline->owner);
3256 dasd_put_device(base);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003260/*
3261 * Return disk geometry.
3262 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003263static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003264{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003265 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003266
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003267 base = dasd_device_from_gendisk(bdev->bd_disk);
3268 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003269 return -ENODEV;
3270
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003271 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003272 !base->discipline->fill_geometry) {
3273 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003274 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003275 }
3276 base->discipline->fill_geometry(base->block, geo);
3277 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3278 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003279 return 0;
3280}
3281
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003282const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283dasd_device_operations = {
3284 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003285 .open = dasd_open,
3286 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003287 .ioctl = dasd_ioctl,
3288 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003289 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290};
3291
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003292/*******************************************************************************
3293 * end of block device operations
3294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296static void
3297dasd_exit(void)
3298{
3299#ifdef CONFIG_PROC_FS
3300 dasd_proc_exit();
3301#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003302 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003303 if (dasd_page_cache != NULL) {
3304 kmem_cache_destroy(dasd_page_cache);
3305 dasd_page_cache = NULL;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 dasd_gendisk_exit();
3308 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (dasd_debug_area != NULL) {
3310 debug_unregister(dasd_debug_area);
3311 dasd_debug_area = NULL;
3312 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003313 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
3316/*
3317 * SECTION: common functions for ccw_driver use
3318 */
3319
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003320/*
3321 * Is the device read-only?
3322 * Note that this function does not report the setting of the
3323 * readonly device attribute, but how it is configured in z/VM.
3324 */
3325int dasd_device_is_ro(struct dasd_device *device)
3326{
3327 struct ccw_dev_id dev_id;
3328 struct diag210 diag_data;
3329 int rc;
3330
3331 if (!MACHINE_IS_VM)
3332 return 0;
3333 ccw_device_get_id(device->cdev, &dev_id);
3334 memset(&diag_data, 0, sizeof(diag_data));
3335 diag_data.vrdcdvno = dev_id.devno;
3336 diag_data.vrdclen = sizeof(diag_data);
3337 rc = diag210(&diag_data);
3338 if (rc == 0 || rc == 2) {
3339 return diag_data.vrdcvfla & 0x80;
3340 } else {
3341 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3342 dev_id.devno, rc);
3343 return 0;
3344 }
3345}
3346EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3347
Cornelia Huckf3445a12009-04-14 15:36:23 +02003348static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3349{
3350 struct ccw_device *cdev = data;
3351 int ret;
3352
3353 ret = ccw_device_set_online(cdev);
3354 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003355 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3356 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003357}
3358
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003359/*
3360 * Initial attempt at a probe function. this can be simplified once
3361 * the other detection code is gone.
3362 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003363int dasd_generic_probe(struct ccw_device *cdev,
3364 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 int ret;
3367
3368 ret = dasd_add_sysfs_files(cdev);
3369 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003370 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3371 "dasd_generic_probe: could not add "
3372 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Horst Hummel40545572006-06-29 15:08:18 +02003375 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Horst Hummel40545572006-06-29 15:08:18 +02003377 /*
3378 * Automatically online either all dasd devices (dasd_autodetect)
3379 * or all devices specified with dasd= parameters during
3380 * initial probe.
3381 */
3382 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003383 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003384 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003387EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Stefan Haberlandc020d722016-09-20 10:42:38 +02003389void dasd_generic_free_discipline(struct dasd_device *device)
3390{
3391 /* Forget the discipline information. */
3392 if (device->discipline) {
3393 if (device->discipline->uncheck_device)
3394 device->discipline->uncheck_device(device);
3395 module_put(device->discipline->owner);
3396 device->discipline = NULL;
3397 }
3398 if (device->base_discipline) {
3399 module_put(device->base_discipline->owner);
3400 device->base_discipline = NULL;
3401 }
3402}
3403EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3404
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003405/*
3406 * This will one day be called from a global not_oper handler.
3407 * It is also used by driver_unregister during module unload.
3408 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003409void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003412 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Horst Hummel59afda72005-05-16 21:53:39 -07003414 cdev->handler = NULL;
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003417 if (IS_ERR(device)) {
3418 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003420 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003421 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3422 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* Already doing offline processing */
3424 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003425 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return;
3427 }
3428 /*
3429 * This device is removed unconditionally. Set offline
3430 * flag to prevent dasd_open from opening it while it is
3431 * no quite down yet.
3432 */
3433 dasd_set_target_state(device, DASD_STATE_NEW);
3434 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003435 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003437 /*
3438 * life cycle of block is bound to device, so delete it after
3439 * device was safely removed
3440 */
3441 if (block)
3442 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003443
3444 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003446EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003448/*
3449 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003451 * or the user has started activation through sysfs.
3452 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003453int dasd_generic_set_online(struct ccw_device *cdev,
3454 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003456 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003458 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003459
Horst Hummel40545572006-06-29 15:08:18 +02003460 /* first online clears initial online feature flag */
3461 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 device = dasd_create_device(cdev);
3463 if (IS_ERR(device))
3464 return PTR_ERR(device);
3465
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003466 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003467 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003469 /* Try to load the required module. */
3470 rc = request_module(DASD_DIAG_MOD);
3471 if (rc) {
3472 pr_warn("%s Setting the DASD online failed "
3473 "because the required module %s "
3474 "could not be loaded (rc=%d)\n",
3475 dev_name(&cdev->dev), DASD_DIAG_MOD,
3476 rc);
3477 dasd_delete_device(device);
3478 return -ENODEV;
3479 }
3480 }
3481 /* Module init could have failed, so check again here after
3482 * request_module(). */
3483 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003484 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3485 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 dasd_delete_device(device);
3487 return -ENODEV;
3488 }
3489 discipline = dasd_diag_discipline_pointer;
3490 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003491 if (!try_module_get(base_discipline->owner)) {
3492 dasd_delete_device(device);
3493 return -EINVAL;
3494 }
3495 if (!try_module_get(discipline->owner)) {
3496 module_put(base_discipline->owner);
3497 dasd_delete_device(device);
3498 return -EINVAL;
3499 }
3500 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 device->discipline = discipline;
3502
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003503 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 rc = discipline->check_device(device);
3505 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003506 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3507 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003508 module_put(discipline->owner);
3509 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 dasd_delete_device(device);
3511 return rc;
3512 }
3513
3514 dasd_set_target_state(device, DASD_STATE_ONLINE);
3515 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003516 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3517 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 rc = -ENODEV;
3519 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003520 if (device->block)
3521 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 dasd_delete_device(device);
3523 } else
3524 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003525 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003526
3527 wait_event(dasd_init_waitq, _wait_for_device(device));
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return rc;
3531}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003532EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003534int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003537 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003538 int max_count, open_count, rc;
Jan Höppner0f57c972016-10-18 17:54:49 +02003539 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003541 rc = 0;
Jan Höppner0f57c972016-10-18 17:54:49 +02003542 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3543 device = dasd_device_from_cdev_locked(cdev);
3544 if (IS_ERR(device)) {
3545 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return PTR_ERR(device);
Jan Höppner0f57c972016-10-18 17:54:49 +02003547 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 /*
3550 * We must make sure that this device is currently not in use.
3551 * The open_count is increased for every opener, that includes
3552 * the blkdev_get in dasd_scan_partitions. We are only interested
3553 * in the other openers.
3554 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003555 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003556 max_count = device->block->bdev ? 0 : -1;
3557 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003558 if (open_count > max_count) {
3559 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003560 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3561 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003562 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003563 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3564 dev_name(&cdev->dev));
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003565 rc = -EBUSY;
3566 goto out_err;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003569
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003570 /*
3571 * Test if the offline processing is already running and exit if so.
3572 * If a safe offline is being processed this could only be a normal
3573 * offline that should be able to overtake the safe offline and
3574 * cancel any I/O we do not want to wait for any longer
3575 */
3576 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3577 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3578 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3579 &device->flags);
3580 } else {
3581 rc = -EBUSY;
3582 goto out_err;
3583 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003584 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003585 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003586
3587 /*
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003588 * if safe_offline is called set safe_offline_running flag and
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003589 * clear safe_offline so that a call to normal offline
3590 * can overrun safe_offline processing
3591 */
3592 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3593 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003594 /* need to unlock here to wait for outstanding I/O */
3595 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003596 /*
3597 * If we want to set the device safe offline all IO operations
3598 * should be finished before continuing the offline process
3599 * so sync bdev first and then wait for our queues to become
3600 * empty
3601 */
Stefan Haberlandca732e12017-01-25 16:56:41 +01003602 if (device->block) {
3603 rc = fsync_bdev(device->block->bdev);
3604 if (rc != 0)
3605 goto interrupted;
3606 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003607 dasd_schedule_device_bh(device);
3608 rc = wait_event_interruptible(shutdown_waitq,
3609 _wait_for_empty_queues(device));
3610 if (rc != 0)
3611 goto interrupted;
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003612
3613 /*
3614 * check if a normal offline process overtook the offline
3615 * processing in this case simply do nothing beside returning
3616 * that we got interrupted
3617 * otherwise mark safe offline as not running any longer and
3618 * continue with normal offline
3619 */
3620 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3621 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3622 rc = -ERESTARTSYS;
3623 goto out_err;
3624 }
3625 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003626 }
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003627 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 dasd_set_target_state(device, DASD_STATE_NEW);
3630 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003631 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003633 /*
3634 * life cycle of block is bound to device, so delete it after
3635 * device was safely removed
3636 */
3637 if (block)
3638 dasd_free_block(block);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003641
3642interrupted:
3643 /* interrupted by signal */
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003644 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003645 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3646 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003647out_err:
Jan Höppner0f57c972016-10-18 17:54:49 +02003648 dasd_put_device(device);
3649 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003650 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003652EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003654int dasd_generic_last_path_gone(struct dasd_device *device)
3655{
3656 struct dasd_ccw_req *cqr;
3657
3658 dev_warn(&device->cdev->dev, "No operational channel path is left "
3659 "for the device\n");
3660 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3661 /* First of all call extended error reporting. */
3662 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3663
3664 if (device->state < DASD_STATE_BASIC)
3665 return 0;
3666 /* Device is active. We want to keep it. */
3667 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3668 if ((cqr->status == DASD_CQR_IN_IO) ||
3669 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3670 cqr->status = DASD_CQR_QUEUED;
3671 cqr->retries++;
3672 }
3673 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3674 dasd_device_clear_timer(device);
3675 dasd_schedule_device_bh(device);
3676 return 1;
3677}
3678EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3679
3680int dasd_generic_path_operational(struct dasd_device *device)
3681{
3682 dev_info(&device->cdev->dev, "A channel path to the device has become "
3683 "operational\n");
3684 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3685 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3686 if (device->stopped & DASD_UNRESUMED_PM) {
3687 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3688 dasd_restore_device(device);
3689 return 1;
3690 }
3691 dasd_schedule_device_bh(device);
Stefan Haberlande4433432017-05-22 10:59:11 +02003692 if (device->block) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003693 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02003694 if (device->block->request_queue)
3695 blk_mq_run_hw_queues(device->block->request_queue,
3696 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02003697 }
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003698
3699 if (!device->stopped)
3700 wake_up(&generic_waitq);
3701
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003702 return 1;
3703}
3704EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3705
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003706int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
3708 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 int ret;
3710
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003711 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (IS_ERR(device))
3713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 ret = 0;
3715 switch (event) {
3716 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003717 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 case CIO_NO_PATH:
Stefan Haberlandc9346152016-08-08 15:53:54 +02003719 dasd_path_no_path(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003720 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 break;
3722 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 ret = 1;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003724 if (dasd_path_get_opm(device))
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003725 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 break;
3727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 dasd_put_device(device);
3729 return ret;
3730}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003731EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003733void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3734{
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003735 struct dasd_device *device;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003736 int chp, oldopm, hpfpm, ifccpm;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003737
3738 device = dasd_device_from_cdev_locked(cdev);
3739 if (IS_ERR(device))
3740 return;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003741
3742 oldopm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003743 for (chp = 0; chp < 8; chp++) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003744 if (path_event[chp] & PE_PATH_GONE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003745 dasd_path_notoper(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003746 }
3747 if (path_event[chp] & PE_PATH_AVAILABLE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003748 dasd_path_available(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003749 dasd_schedule_device_bh(device);
3750 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003751 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003752 if (!dasd_path_is_operational(device, chp) &&
3753 !dasd_path_need_verify(device, chp)) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003754 /*
3755 * we can not establish a pathgroup on an
3756 * unavailable path, so trigger a path
3757 * verification first
3758 */
Stefan Haberlandc9346152016-08-08 15:53:54 +02003759 dasd_path_available(device, chp);
3760 dasd_schedule_device_bh(device);
Stefan Haberland12d7b102012-09-11 15:10:58 +02003761 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003762 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3763 "Pathgroup re-established\n");
3764 if (device->discipline->kick_validate)
3765 device->discipline->kick_validate(device);
3766 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003767 }
Stefan Haberlanda521b042016-08-08 15:56:54 +02003768 hpfpm = dasd_path_get_hpfpm(device);
3769 ifccpm = dasd_path_get_ifccpm(device);
3770 if (!dasd_path_get_opm(device) && hpfpm) {
3771 /*
3772 * device has no operational paths but at least one path is
3773 * disabled due to HPF errors
3774 * disable HPF at all and use the path(s) again
3775 */
3776 if (device->discipline->disable_hpf)
3777 device->discipline->disable_hpf(device);
3778 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3779 dasd_path_set_tbvpm(device, hpfpm);
3780 dasd_schedule_device_bh(device);
3781 dasd_schedule_requeue(device);
3782 } else if (!dasd_path_get_opm(device) && ifccpm) {
3783 /*
3784 * device has no operational paths but at least one path is
3785 * disabled due to IFCC errors
3786 * trigger path verification on paths with IFCC errors
3787 */
3788 dasd_path_set_tbvpm(device, ifccpm);
3789 dasd_schedule_device_bh(device);
3790 }
3791 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003792 dev_warn(&device->cdev->dev,
3793 "No verified channel paths remain for the device\n");
3794 DBF_DEV_EVENT(DBF_WARNING, device,
3795 "%s", "last verified path gone");
3796 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3797 dasd_device_set_stop_bits(device,
3798 DASD_STOPPED_DC_WAIT);
3799 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003800 dasd_put_device(device);
3801}
3802EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3803
3804int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3805{
Stefan Haberlandc9346152016-08-08 15:53:54 +02003806 if (!dasd_path_get_opm(device) && lpm) {
3807 dasd_path_set_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003808 dasd_generic_path_operational(device);
3809 } else
Stefan Haberlandc9346152016-08-08 15:53:54 +02003810 dasd_path_add_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003811 return 0;
3812}
3813EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3814
Stefan Haberlanda521b042016-08-08 15:56:54 +02003815/*
3816 * clear active requests and requeue them to block layer if possible
3817 */
3818static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3819{
3820 struct list_head requeue_queue;
3821 struct dasd_ccw_req *cqr, *n;
3822 struct dasd_ccw_req *refers;
3823 int rc;
3824
3825 INIT_LIST_HEAD(&requeue_queue);
3826 spin_lock_irq(get_ccwdev_lock(device->cdev));
3827 rc = 0;
3828 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3829 /* Check status and move request to flush_queue */
3830 if (cqr->status == DASD_CQR_IN_IO) {
3831 rc = device->discipline->term_IO(cqr);
3832 if (rc) {
3833 /* unable to terminate requeust */
3834 dev_err(&device->cdev->dev,
3835 "Unable to terminate request %p "
3836 "on suspend\n", cqr);
3837 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3838 dasd_put_device(device);
3839 return rc;
3840 }
3841 }
3842 list_move_tail(&cqr->devlist, &requeue_queue);
3843 }
3844 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3845
3846 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3847 wait_event(dasd_flush_wq,
3848 (cqr->status != DASD_CQR_CLEAR_PENDING));
3849
Stefan Haberland9487cfd2018-02-07 17:39:14 +01003850 /*
3851 * requeue requests to blocklayer will only work
3852 * for block device requests
3853 */
3854 if (_dasd_requeue_request(cqr))
3855 continue;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003856
3857 /* remove requests from device and block queue */
3858 list_del_init(&cqr->devlist);
3859 while (cqr->refers != NULL) {
3860 refers = cqr->refers;
3861 /* remove the request from the block queue */
3862 list_del(&cqr->blocklist);
3863 /* free the finished erp request */
3864 dasd_free_erp_request(cqr, cqr->memdev);
3865 cqr = refers;
3866 }
3867
Stefan Haberlanda1fc8182018-02-19 12:24:39 +01003868 /*
3869 * _dasd_requeue_request already checked for a valid
3870 * blockdevice, no need to check again
3871 * all erp requests (cqr->refers) have a cqr->block
3872 * pointer copy from the original cqr
3873 */
3874 list_del_init(&cqr->blocklist);
Stefan Haberlanda521b042016-08-08 15:56:54 +02003875 cqr->block->base->discipline->free_cp(
3876 cqr, (struct request *) cqr->callback_data);
3877 }
3878
3879 /*
3880 * if requests remain then they are internal request
3881 * and go back to the device queue
3882 */
3883 if (!list_empty(&requeue_queue)) {
3884 /* move freeze_queue to start of the ccw_queue */
3885 spin_lock_irq(get_ccwdev_lock(device->cdev));
3886 list_splice_tail(&requeue_queue, &device->ccw_queue);
3887 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3888 }
Stefan Haberland9487cfd2018-02-07 17:39:14 +01003889 dasd_schedule_device_bh(device);
Stefan Haberlanda521b042016-08-08 15:56:54 +02003890 return rc;
3891}
3892
3893static void do_requeue_requests(struct work_struct *work)
3894{
3895 struct dasd_device *device = container_of(work, struct dasd_device,
3896 requeue_requests);
3897 dasd_generic_requeue_all_requests(device);
3898 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
3899 if (device->block)
3900 dasd_schedule_block_bh(device->block);
3901 dasd_put_device(device);
3902}
3903
3904void dasd_schedule_requeue(struct dasd_device *device)
3905{
3906 dasd_get_device(device);
3907 /* queue call to dasd_reload_device to the kernel event daemon. */
3908 if (!schedule_work(&device->requeue_requests))
3909 dasd_put_device(device);
3910}
3911EXPORT_SYMBOL(dasd_schedule_requeue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003912
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003913int dasd_generic_pm_freeze(struct ccw_device *cdev)
3914{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003915 struct dasd_device *device = dasd_device_from_cdev(cdev);
3916
3917 if (IS_ERR(device))
3918 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003919
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003920 /* mark device as suspended */
3921 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3922
Stefan Haberland6f272b92011-01-05 12:48:05 +01003923 if (device->discipline->freeze)
Sebastian Ott4bca6982017-06-26 19:26:55 +02003924 device->discipline->freeze(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003925
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003926 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003927 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003928
Stefan Haberlanda521b042016-08-08 15:56:54 +02003929 return dasd_generic_requeue_all_requests(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003930}
3931EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3932
3933int dasd_generic_restore_device(struct ccw_device *cdev)
3934{
3935 struct dasd_device *device = dasd_device_from_cdev(cdev);
3936 int rc = 0;
3937
3938 if (IS_ERR(device))
3939 return PTR_ERR(device);
3940
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003941 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003942 dasd_device_remove_stop_bits(device,
3943 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003944
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003945 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003946
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003947 /*
3948 * call discipline restore function
3949 * if device is stopped do nothing e.g. for disconnected devices
3950 */
3951 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003952 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003953 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003954 /*
3955 * if the resume failed for the DASD we put it in
3956 * an UNRESUMED stop state
3957 */
3958 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003959
Stefan Haberlande4433432017-05-22 10:59:11 +02003960 if (device->block) {
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003961 dasd_schedule_block_bh(device->block);
Stefan Haberland673514a2017-09-12 17:22:42 +02003962 if (device->block->request_queue)
3963 blk_mq_run_hw_queues(device->block->request_queue,
3964 true);
Stefan Haberlande4433432017-05-22 10:59:11 +02003965 }
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003966
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003967 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003968 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003969 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003970}
3971EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3972
Heiko Carstens763968e2007-05-10 15:45:46 +02003973static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3974 void *rdc_buffer,
3975 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003976 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003977{
3978 struct dasd_ccw_req *cqr;
3979 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003980 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003981
Sebastian Ottc5205f22018-06-04 19:07:39 +02003982 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
3983 NULL);
Cornelia Huck17283b52007-05-04 18:47:51 +02003984
3985 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003986 /* internal error 13 - Allocating the RDC request failed*/
3987 dev_err(&device->cdev->dev,
3988 "An error occurred in the DASD device driver, "
3989 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003990 return cqr;
3991 }
3992
3993 ccw = cqr->cpaddr;
3994 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003995 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3996 idaw = (unsigned long *) (cqr->data);
3997 ccw->cda = (__u32)(addr_t) idaw;
3998 ccw->flags = CCW_FLAG_IDA;
3999 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
4000 } else {
4001 ccw->cda = (__u32)(addr_t) rdc_buffer;
4002 ccw->flags = 0;
4003 }
Cornelia Huck17283b52007-05-04 18:47:51 +02004004
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02004005 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004006 cqr->startdev = device;
4007 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02004008 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004009 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01004010 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02004011 cqr->status = DASD_CQR_FILLED;
4012 return cqr;
4013}
4014
4015
Stefan Haberland68b781f2009-09-11 10:28:29 +02004016int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02004017 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02004018{
4019 int ret;
4020 struct dasd_ccw_req *cqr;
4021
Sebastian Ott92636b12009-06-12 10:26:37 +02004022 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02004023 magic);
4024 if (IS_ERR(cqr))
4025 return PTR_ERR(cqr);
4026
4027 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004028 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02004029 return ret;
4030}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02004031EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004032
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01004033/*
4034 * In command mode and transport mode we need to look for sense
4035 * data in different places. The sense data itself is allways
4036 * an array of 32 bytes, so we can unify the sense data access
4037 * for both modes.
4038 */
4039char *dasd_get_sense(struct irb *irb)
4040{
4041 struct tsb *tsb = NULL;
4042 char *sense = NULL;
4043
4044 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4045 if (irb->scsw.tm.tcw)
4046 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4047 irb->scsw.tm.tcw);
4048 if (tsb && tsb->length == 64 && tsb->flags)
4049 switch (tsb->flags & 0x07) {
4050 case 1: /* tsa_iostat */
4051 sense = tsb->tsa.iostat.sense;
4052 break;
4053 case 2: /* tsa_ddpc */
4054 sense = tsb->tsa.ddpc.sense;
4055 break;
4056 default:
4057 /* currently we don't use interrogate data */
4058 break;
4059 }
4060 } else if (irb->esw.esw0.erw.cons) {
4061 sense = irb->ecw;
4062 }
4063 return sense;
4064}
4065EXPORT_SYMBOL_GPL(dasd_get_sense);
4066
Stefan Haberland4679e892012-06-19 17:30:12 +02004067void dasd_generic_shutdown(struct ccw_device *cdev)
4068{
4069 struct dasd_device *device;
4070
4071 device = dasd_device_from_cdev(cdev);
4072 if (IS_ERR(device))
4073 return;
4074
4075 if (device->block)
4076 dasd_schedule_block_bh(device->block);
4077
4078 dasd_schedule_device_bh(device);
4079
4080 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4081}
4082EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4083
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004084static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
4086 int rc;
4087
4088 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02004089 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02004090 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02004091 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
4093 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01004094 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (dasd_debug_area == NULL) {
4096 rc = -ENOMEM;
4097 goto failed;
4098 }
4099 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02004100 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4103
4104 dasd_diag_discipline_pointer = NULL;
4105
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02004106 dasd_statistics_createroot();
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 rc = dasd_devmap_init();
4109 if (rc)
4110 goto failed;
4111 rc = dasd_gendisk_init();
4112 if (rc)
4113 goto failed;
4114 rc = dasd_parse();
4115 if (rc)
4116 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004117 rc = dasd_eer_init();
4118 if (rc)
4119 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120#ifdef CONFIG_PROC_FS
4121 rc = dasd_proc_init();
4122 if (rc)
4123 goto failed;
4124#endif
4125
4126 return 0;
4127failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004128 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 dasd_exit();
4130 return rc;
4131}
4132
4133module_init(dasd_init);
4134module_exit(dasd_exit);