blob: 83f80710d555093465a129923049570a6177253a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02007 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Stefan Haberlandfc19f382009-03-26 15:23:49 +010010#define KMSG_COMPONENT "dasd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kmod.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/ctype.h>
17#include <linux/major.h>
18#include <linux/slab.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020020#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010021#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020022#include <linux/debugfs.h>
23#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020024#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010029#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010030#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +020041#define DASD_DIAG_MOD "dasd_diag_mod"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * SECTION: exported variables of dasd.c
45 */
46debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020047EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020048static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020050EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010051void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
54MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020055 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_LICENSE("GPL");
58
59/*
60 * SECTION: prototypes for static functions of dasd.c
61 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010062static int dasd_alloc_queue(struct dasd_block *);
63static void dasd_setup_queue(struct dasd_block *);
64static void dasd_free_queue(struct dasd_block *);
65static void dasd_flush_request_queue(struct dasd_block *);
66static 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 *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010074static void dasd_device_timeout(unsigned long);
75static void dasd_block_timeout(unsigned long);
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);
123 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100124 device->timer.function = dasd_device_timeout;
125 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000126 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200127 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200128 INIT_WORK(&device->reload_device, do_reload_device);
Stefan Haberlanda521b042016-08-08 15:56:54 +0200129 INIT_WORK(&device->requeue_requests, do_requeue_requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 device->state = DASD_STATE_NEW;
131 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100132 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200133 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return device;
135}
136
137/*
138 * Free memory of a device structure.
139 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100140void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800142 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 free_page((unsigned long) device->erp_mem);
144 free_pages((unsigned long) device->ccw_mem, 1);
145 kfree(device);
146}
147
148/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100149 * Allocate memory for a new device structure.
150 */
151struct dasd_block *dasd_alloc_block(void)
152{
153 struct dasd_block *block;
154
155 block = kzalloc(sizeof(*block), GFP_ATOMIC);
156 if (!block)
157 return ERR_PTR(-ENOMEM);
158 /* open_count = 0 means device online but not in use */
159 atomic_set(&block->open_count, -1);
160
161 spin_lock_init(&block->request_queue_lock);
162 atomic_set(&block->tasklet_scheduled, 0);
163 tasklet_init(&block->tasklet,
164 (void (*)(unsigned long)) dasd_block_tasklet,
165 (unsigned long) block);
166 INIT_LIST_HEAD(&block->ccw_queue);
167 spin_lock_init(&block->queue_lock);
168 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100169 block->timer.function = dasd_block_timeout;
170 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200171 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100172
173 return block;
174}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200175EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176
177/*
178 * Free memory of a device structure.
179 */
180void dasd_free_block(struct dasd_block *block)
181{
182 kfree(block);
183}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200184EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100185
186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Make a new device known to the system.
188 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100189static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int rc;
192
193 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200194 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * keep the reference count > 0.
196 */
197 dasd_get_device(device);
198
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100199 if (device->block) {
200 rc = dasd_alloc_queue(device->block);
201 if (rc) {
202 dasd_put_device(device);
203 return rc;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 device->state = DASD_STATE_KNOWN;
207 return 0;
208}
209
210/*
211 * Let the system forget about a device.
212 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100213static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800215 /* Disable extended error reporting for this device. */
216 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->state = DASD_STATE_NEW;
218
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 if (device->block)
220 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Give up reference we took in dasd_state_new_to_known. */
223 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200227static struct dentry *dasd_debugfs_setup(const char *name,
228 struct dentry *base_dentry)
229{
230 struct dentry *pde;
231
232 if (!base_dentry)
233 return NULL;
234 pde = debugfs_create_dir(name, base_dentry);
235 if (!pde || IS_ERR(pde))
236 return NULL;
237 return pde;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Request the irq line for the device.
242 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100243static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200245 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200246 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200249 if (block) {
250 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100251 if (rc)
252 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 block->debugfs_dentry =
254 dasd_debugfs_setup(block->gdp->disk_name,
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&block->profile, block->debugfs_dentry);
257 if (dasd_global_profile_level == DASD_PROFILE_ON)
258 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100259 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200260 device->debugfs_dentry =
261 dasd_debugfs_setup(dev_name(&device->cdev->dev),
262 dasd_debugfs_root_entry);
263 dasd_profile_init(&device->profile, device->debugfs_dentry);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100264 dasd_hosts_init(device->debugfs_dentry, device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100267 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100268 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200270 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
272
273 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200274
275 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * Release the irq line for the device. Terminate any running i/o.
280 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100281static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Horst Hummel8f617012006-08-30 14:33:33 +0200283 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200284
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200285 if (device->discipline->basic_to_known) {
286 rc = device->discipline->basic_to_known(device);
287 if (rc)
288 return rc;
289 }
290
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100291 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200292 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200293 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294 dasd_gendisk_free(device->block);
295 dasd_block_clear_timer(device->block);
296 }
297 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200298 if (rc)
299 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100300 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200301 dasd_profile_exit(&device->profile);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100302 dasd_hosts_exit(device);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200303 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
305 if (device->debug_area != NULL) {
306 debug_unregister(device->debug_area);
307 device->debug_area = NULL;
308 }
309 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
314 * Do the initial analysis. The do_analysis function may return
315 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
316 * until the discipline decides to continue the startup sequence
317 * by calling the function dasd_change_state. The eckd disciplines
318 * uses this to start a ccw that detects the format. The completion
319 * interrupt for this detection ccw uses the kernel event daemon to
320 * trigger the call to dasd_change_state. All this is done in the
321 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800322 * After the analysis ccw is done (do_analysis returned 0) the block
323 * device is setup.
324 * In case the analysis returns an error, the device setup is stopped
325 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100330 struct dasd_block *block;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200331 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800335 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336 if (block) {
337 if (block->base->discipline->do_analysis != NULL)
338 rc = block->base->discipline->do_analysis(block);
339 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200340 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100341 device->state = DASD_STATE_UNFMT;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200342 disk = device->block->gdp;
343 kobject_uevent(&disk_to_dev(disk)->kobj,
344 KOBJ_CHANGE);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200345 goto out;
346 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100347 return rc;
348 }
349 dasd_setup_queue(block);
350 set_capacity(block->gdp,
351 block->blocks << block->s2b_shift);
352 device->state = DASD_STATE_READY;
353 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200354 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100355 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200356 return rc;
357 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100358 } else {
359 device->state = DASD_STATE_READY;
360 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200361out:
362 if (device->discipline->basic_to_ready)
363 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800364 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100367static inline
368int _wait_for_empty_queues(struct dasd_device *device)
369{
370 if (device->block)
371 return list_empty(&device->ccw_queue) &&
372 list_empty(&device->block->ccw_queue);
373 else
374 return list_empty(&device->ccw_queue);
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
378 * Remove device from block device layer. Destroy dirty buffers.
379 * Forget format information. Check if the target level is basic
380 * and if it is create fake disk for formatting.
381 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100382static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Horst Hummel8f617012006-08-30 14:33:33 +0200384 int rc;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100387 if (device->block) {
388 struct dasd_block *block = device->block;
389 rc = dasd_flush_block_queue(block);
390 if (rc) {
391 device->state = DASD_STATE_READY;
392 return rc;
393 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100394 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100395 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100396 block->blocks = 0;
397 block->bp_block = 0;
398 block->s2b_shift = 0;
399 }
Horst Hummel8f617012006-08-30 14:33:33 +0200400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403/*
Horst Hummel90f00942006-03-07 21:55:39 -0800404 * Back to basic.
405 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100406static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800407{
408 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200409 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800410}
411
412/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * Make the device online and schedule the bottom half to start
414 * the requeueing of requests from the linux request queue to the
415 * ccw queue.
416 */
Horst Hummel8f617012006-08-30 14:33:33 +0200417static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418dasd_state_ready_to_online(struct dasd_device * device)
419{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100420 struct gendisk *disk;
421 struct disk_part_iter piter;
422 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100425 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100426 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100427 if ((device->features & DASD_FEATURE_USERAW)) {
428 disk = device->block->gdp;
429 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
430 return 0;
431 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100432 disk = device->block->bdev->bd_disk;
433 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
434 while ((part = disk_part_iter_next(&piter)))
435 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
436 disk_part_iter_exit(&piter);
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
441/*
442 * Stop the requeueing of requests again.
443 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100446 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100447 struct gendisk *disk;
448 struct disk_part_iter piter;
449 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100450
451 if (device->discipline->online_to_ready) {
452 rc = device->discipline->online_to_ready(device);
453 if (rc)
454 return rc;
455 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100458 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100459 disk = device->block->bdev->bd_disk;
460 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
461 while ((part = disk_part_iter_next(&piter)))
462 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
463 disk_part_iter_exit(&piter);
464 }
Horst Hummel8f617012006-08-30 14:33:33 +0200465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/*
469 * Device startup state changes.
470 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100471static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int rc;
474
475 rc = 0;
476 if (device->state == DASD_STATE_NEW &&
477 device->target >= DASD_STATE_KNOWN)
478 rc = dasd_state_new_to_known(device);
479
480 if (!rc &&
481 device->state == DASD_STATE_KNOWN &&
482 device->target >= DASD_STATE_BASIC)
483 rc = dasd_state_known_to_basic(device);
484
485 if (!rc &&
486 device->state == DASD_STATE_BASIC &&
487 device->target >= DASD_STATE_READY)
488 rc = dasd_state_basic_to_ready(device);
489
490 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700491 device->state == DASD_STATE_UNFMT &&
492 device->target > DASD_STATE_UNFMT)
493 rc = -EPERM;
494
495 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 device->state == DASD_STATE_READY &&
497 device->target >= DASD_STATE_ONLINE)
498 rc = dasd_state_ready_to_online(device);
499
500 return rc;
501}
502
503/*
504 * Device shutdown state changes.
505 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100506static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Horst Hummel8f617012006-08-30 14:33:33 +0200508 int rc;
509
510 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (device->state == DASD_STATE_ONLINE &&
512 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200513 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200514
Horst Hummel8f617012006-08-30 14:33:33 +0200515 if (!rc &&
516 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200518 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800519
Horst Hummel8f617012006-08-30 14:33:33 +0200520 if (!rc &&
521 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800522 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200523 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800524
Horst Hummel8f617012006-08-30 14:33:33 +0200525 if (!rc &&
526 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200528 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200529
Horst Hummel8f617012006-08-30 14:33:33 +0200530 if (!rc &&
531 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200533 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Horst Hummel8f617012006-08-30 14:33:33 +0200535 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*
539 * This is the main startup/shutdown routine.
540 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100541static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Sebastian Ott181d9522009-06-22 12:08:21 +0200543 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (device->state == device->target)
546 /* Already where we want to go today... */
547 return;
548 if (device->state < device->target)
549 rc = dasd_increase_state(device);
550 else
551 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200552 if (rc == -EAGAIN)
553 return;
554 if (rc)
555 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200557 /* let user-space know that the device status changed */
558 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200559
560 if (device->state == device->target)
561 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
565 * Kick starter for devices that did not complete the startup/shutdown
566 * procedure or were sleeping because of a pending state.
567 * dasd_kick_device will schedule a call do do_kick_device to the kernel
568 * event daemon.
569 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100570static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Al Viro4927b3f2006-12-06 19:18:20 +0000572 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100573 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100575 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100576 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 dasd_put_device(device);
578}
579
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100580void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 dasd_get_device(device);
583 /* queue call to dasd_kick_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200584 if (!schedule_work(&device->kick_work))
585 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200587EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200590 * dasd_reload_device will schedule a call do do_reload_device to the kernel
591 * event daemon.
592 */
593static void do_reload_device(struct work_struct *work)
594{
595 struct dasd_device *device = container_of(work, struct dasd_device,
596 reload_device);
597 device->discipline->reload(device);
598 dasd_put_device(device);
599}
600
601void dasd_reload_device(struct dasd_device *device)
602{
603 dasd_get_device(device);
604 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200605 if (!schedule_work(&device->reload_device))
606 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200607}
608EXPORT_SYMBOL(dasd_reload_device);
609
610/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200611 * dasd_restore_device will schedule a call do do_restore_device to the kernel
612 * event daemon.
613 */
614static void do_restore_device(struct work_struct *work)
615{
616 struct dasd_device *device = container_of(work, struct dasd_device,
617 restore_device);
618 device->cdev->drv->restore(device->cdev);
619 dasd_put_device(device);
620}
621
622void dasd_restore_device(struct dasd_device *device)
623{
624 dasd_get_device(device);
625 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200626 if (!schedule_work(&device->restore_device))
627 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200628}
629
630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Set the target state for a device and starts the state change.
632 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100633void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200635 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100636 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* If we are in probeonly mode stop at DASD_STATE_READY. */
638 if (dasd_probeonly && target > DASD_STATE_READY)
639 target = DASD_STATE_READY;
640 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100641 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 wake_up(&dasd_init_waitq);
643 device->target = target;
644 }
645 if (device->state != device->target)
646 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100647 mutex_unlock(&device->state_mutex);
648 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200650EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/*
653 * Enable devices with device numbers in [from..to].
654 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100655static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 return (device->state == device->target);
658}
659
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100660void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 dasd_set_target_state(device, DASD_STATE_ONLINE);
663 if (device->state <= DASD_STATE_KNOWN)
664 /* No discipline for device found. */
665 dasd_set_target_state(device, DASD_STATE_NEW);
666 /* Now wait for the devices to come up. */
667 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400668
669 dasd_reload_device(device);
670 if (device->discipline->kick_validate)
671 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200673EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675/*
676 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
677 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200678
679unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100682struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100683 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
684};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200685static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687/*
688 * Add profiling information for cqr before execution.
689 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100690static void dasd_profile_start(struct dasd_block *block,
691 struct dasd_ccw_req *cqr,
692 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct list_head *l;
695 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200696 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* count the length of the chanq for statistics */
699 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200700 if (dasd_global_profile_level || block->profile.data)
701 list_for_each(l, &block->ccw_queue)
702 if (++counter >= 31)
703 break;
704
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100705 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100706 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100707 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200708 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100709 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200710 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100711 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200712
713 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200714 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200715 block->profile.data->dasd_io_nr_req[counter]++;
716 if (rq_data_dir(req) == READ)
717 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200718 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200719 spin_unlock(&block->profile.lock);
720
721 /*
722 * We count the request for the start device, even though it may run on
723 * some other device due to error recovery. This way we make sure that
724 * we count each request only once.
725 */
726 device = cqr->startdev;
727 if (device->profile.data) {
728 counter = 1; /* request is not yet queued on the start device */
729 list_for_each(l, &device->ccw_queue)
730 if (++counter >= 31)
731 break;
732 }
733 spin_lock(&device->profile.lock);
734 if (device->profile.data) {
735 device->profile.data->dasd_io_nr_req[counter]++;
736 if (rq_data_dir(req) == READ)
737 device->profile.data->dasd_read_nr_req[counter]++;
738 }
739 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Add profiling information for cqr after execution.
744 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200745
746#define dasd_profile_counter(value, index) \
747{ \
748 for (index = 0; index < 31 && value >> (2+index); index++) \
749 ; \
750}
751
752static void dasd_profile_end_add_data(struct dasd_profile_info *data,
753 int is_alias,
754 int is_tpm,
755 int is_read,
756 long sectors,
757 int sectors_ind,
758 int tottime_ind,
759 int tottimeps_ind,
760 int strtime_ind,
761 int irqtime_ind,
762 int irqtimeps_ind,
763 int endtime_ind)
764{
765 /* in case of an overflow, reset the whole profile */
766 if (data->dasd_io_reqs == UINT_MAX) {
767 memset(data, 0, sizeof(*data));
768 getnstimeofday(&data->starttod);
769 }
770 data->dasd_io_reqs++;
771 data->dasd_io_sects += sectors;
772 if (is_alias)
773 data->dasd_io_alias++;
774 if (is_tpm)
775 data->dasd_io_tpm++;
776
777 data->dasd_io_secs[sectors_ind]++;
778 data->dasd_io_times[tottime_ind]++;
779 data->dasd_io_timps[tottimeps_ind]++;
780 data->dasd_io_time1[strtime_ind]++;
781 data->dasd_io_time2[irqtime_ind]++;
782 data->dasd_io_time2ps[irqtimeps_ind]++;
783 data->dasd_io_time3[endtime_ind]++;
784
785 if (is_read) {
786 data->dasd_read_reqs++;
787 data->dasd_read_sects += sectors;
788 if (is_alias)
789 data->dasd_read_alias++;
790 if (is_tpm)
791 data->dasd_read_tpm++;
792 data->dasd_read_secs[sectors_ind]++;
793 data->dasd_read_times[tottime_ind]++;
794 data->dasd_read_time1[strtime_ind]++;
795 data->dasd_read_time2[irqtime_ind]++;
796 data->dasd_read_time3[endtime_ind]++;
797 }
798}
799
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100800static void dasd_profile_end(struct dasd_block *block,
801 struct dasd_ccw_req *cqr,
802 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Stefan Haberlandd2907222017-07-14 10:33:18 +0200804 unsigned long strtime, irqtime, endtime, tottime;
805 unsigned long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200806 struct dasd_device *device;
807 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
808 int irqtime_ind, irqtimeps_ind, endtime_ind;
Stefan Haberlandd2907222017-07-14 10:33:18 +0200809 struct dasd_profile_info *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200811 device = cqr->startdev;
812 if (!(dasd_global_profile_level ||
813 block->profile.data ||
814 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return;
816
Tejun Heo83096eb2009-05-07 22:24:39 +0900817 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!cqr->buildclk || !cqr->startclk ||
819 !cqr->stopclk || !cqr->endclk ||
820 !sectors)
821 return;
822
823 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
824 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
825 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
826 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
827 tottimeps = tottime / sectors;
828
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200829 dasd_profile_counter(sectors, sectors_ind);
830 dasd_profile_counter(tottime, tottime_ind);
831 dasd_profile_counter(tottimeps, tottimeps_ind);
832 dasd_profile_counter(strtime, strtime_ind);
833 dasd_profile_counter(irqtime, irqtime_ind);
834 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
835 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100837 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100838 if (dasd_global_profile.data) {
Stefan Haberlandd2907222017-07-14 10:33:18 +0200839 data = dasd_global_profile.data;
840 data->dasd_sum_times += tottime;
841 data->dasd_sum_time_str += strtime;
842 data->dasd_sum_time_irq += irqtime;
843 data->dasd_sum_time_end += endtime;
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100844 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200845 cqr->startdev != block->base,
846 cqr->cpmode == 1,
847 rq_data_dir(req) == READ,
848 sectors, sectors_ind, tottime_ind,
849 tottimeps_ind, strtime_ind,
850 irqtime_ind, irqtimeps_ind,
851 endtime_ind);
852 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100853 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200855 spin_lock(&block->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200856 if (block->profile.data) {
857 data = block->profile.data;
858 data->dasd_sum_times += tottime;
859 data->dasd_sum_time_str += strtime;
860 data->dasd_sum_time_irq += irqtime;
861 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200862 dasd_profile_end_add_data(block->profile.data,
863 cqr->startdev != block->base,
864 cqr->cpmode == 1,
865 rq_data_dir(req) == READ,
866 sectors, sectors_ind, tottime_ind,
867 tottimeps_ind, strtime_ind,
868 irqtime_ind, irqtimeps_ind,
869 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200870 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200871 spin_unlock(&block->profile.lock);
872
873 spin_lock(&device->profile.lock);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200874 if (device->profile.data) {
875 data = device->profile.data;
876 data->dasd_sum_times += tottime;
877 data->dasd_sum_time_str += strtime;
878 data->dasd_sum_time_irq += irqtime;
879 data->dasd_sum_time_end += endtime;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200880 dasd_profile_end_add_data(device->profile.data,
881 cqr->startdev != block->base,
882 cqr->cpmode == 1,
883 rq_data_dir(req) == READ,
884 sectors, sectors_ind, tottime_ind,
885 tottimeps_ind, strtime_ind,
886 irqtime_ind, irqtimeps_ind,
887 endtime_ind);
Stefan Haberlandd2907222017-07-14 10:33:18 +0200888 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200889 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200891
892void dasd_profile_reset(struct dasd_profile *profile)
893{
894 struct dasd_profile_info *data;
895
896 spin_lock_bh(&profile->lock);
897 data = profile->data;
898 if (!data) {
899 spin_unlock_bh(&profile->lock);
900 return;
901 }
902 memset(data, 0, sizeof(*data));
903 getnstimeofday(&data->starttod);
904 spin_unlock_bh(&profile->lock);
905}
906
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200907int dasd_profile_on(struct dasd_profile *profile)
908{
909 struct dasd_profile_info *data;
910
911 data = kzalloc(sizeof(*data), GFP_KERNEL);
912 if (!data)
913 return -ENOMEM;
914 spin_lock_bh(&profile->lock);
915 if (profile->data) {
916 spin_unlock_bh(&profile->lock);
917 kfree(data);
918 return 0;
919 }
920 getnstimeofday(&data->starttod);
921 profile->data = data;
922 spin_unlock_bh(&profile->lock);
923 return 0;
924}
925
926void dasd_profile_off(struct dasd_profile *profile)
927{
928 spin_lock_bh(&profile->lock);
929 kfree(profile->data);
930 profile->data = NULL;
931 spin_unlock_bh(&profile->lock);
932}
933
934char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
935{
936 char *buffer;
937
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200938 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200939 if (buffer == NULL)
940 return ERR_PTR(-ENOMEM);
941 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200942 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200943 return ERR_PTR(-EFAULT);
944 }
945 /* got the string, now strip linefeed. */
946 if (buffer[user_len - 1] == '\n')
947 buffer[user_len - 1] = 0;
948 else
949 buffer[user_len] = 0;
950 return buffer;
951}
952
953static ssize_t dasd_stats_write(struct file *file,
954 const char __user *user_buf,
955 size_t user_len, loff_t *pos)
956{
957 char *buffer, *str;
958 int rc;
959 struct seq_file *m = (struct seq_file *)file->private_data;
960 struct dasd_profile *prof = m->private;
961
962 if (user_len > 65536)
963 user_len = 65536;
964 buffer = dasd_get_user_string(user_buf, user_len);
965 if (IS_ERR(buffer))
966 return PTR_ERR(buffer);
967
968 str = skip_spaces(buffer);
969 rc = user_len;
970 if (strncmp(str, "reset", 5) == 0) {
971 dasd_profile_reset(prof);
972 } else if (strncmp(str, "on", 2) == 0) {
973 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100974 if (rc)
975 goto out;
976 rc = user_len;
977 if (prof == &dasd_global_profile) {
978 dasd_profile_reset(prof);
979 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
980 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200981 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100982 if (prof == &dasd_global_profile)
983 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200984 dasd_profile_off(prof);
985 } else
986 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100987out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200988 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200989 return rc;
990}
991
992static void dasd_stats_array(struct seq_file *m, unsigned int *array)
993{
994 int i;
995
996 for (i = 0; i < 32; i++)
997 seq_printf(m, "%u ", array[i]);
998 seq_putc(m, '\n');
999}
1000
1001static void dasd_stats_seq_print(struct seq_file *m,
1002 struct dasd_profile_info *data)
1003{
1004 seq_printf(m, "start_time %ld.%09ld\n",
1005 data->starttod.tv_sec, data->starttod.tv_nsec);
1006 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
1007 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
1008 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
1009 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Stefan Haberlandd2907222017-07-14 10:33:18 +02001010 seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
1011 data->dasd_sum_times / data->dasd_io_reqs : 0UL);
1012 seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
1013 data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
1014 seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
1015 data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
1016 seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
1017 data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001018 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001019 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001020 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001021 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001022 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001023 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001024 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001025 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001026 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001027 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001028 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001029 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001030 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001031 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001032 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001033 dasd_stats_array(m, data->dasd_io_nr_req);
1034 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1035 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1036 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1037 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001038 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001039 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001040 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001041 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001042 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001043 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001044 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001045 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001046 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001047 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001048 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001049 dasd_stats_array(m, data->dasd_read_nr_req);
1050}
1051
1052static int dasd_stats_show(struct seq_file *m, void *v)
1053{
1054 struct dasd_profile *profile;
1055 struct dasd_profile_info *data;
1056
1057 profile = m->private;
1058 spin_lock_bh(&profile->lock);
1059 data = profile->data;
1060 if (!data) {
1061 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001062 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001063 return 0;
1064 }
1065 dasd_stats_seq_print(m, data);
1066 spin_unlock_bh(&profile->lock);
1067 return 0;
1068}
1069
1070static int dasd_stats_open(struct inode *inode, struct file *file)
1071{
1072 struct dasd_profile *profile = inode->i_private;
1073 return single_open(file, dasd_stats_show, profile);
1074}
1075
1076static const struct file_operations dasd_stats_raw_fops = {
1077 .owner = THIS_MODULE,
1078 .open = dasd_stats_open,
1079 .read = seq_read,
1080 .llseek = seq_lseek,
1081 .release = single_release,
1082 .write = dasd_stats_write,
1083};
1084
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001085static void dasd_profile_init(struct dasd_profile *profile,
1086 struct dentry *base_dentry)
1087{
Al Virof4ae40a62011-07-24 04:33:43 -04001088 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001089 struct dentry *pde;
1090
1091 if (!base_dentry)
1092 return;
1093 profile->dentry = NULL;
1094 profile->data = NULL;
1095 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1096 pde = debugfs_create_file("statistics", mode, base_dentry,
1097 profile, &dasd_stats_raw_fops);
1098 if (pde && !IS_ERR(pde))
1099 profile->dentry = pde;
1100 return;
1101}
1102
1103static void dasd_profile_exit(struct dasd_profile *profile)
1104{
1105 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001106 debugfs_remove(profile->dentry);
1107 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001108}
1109
1110static void dasd_statistics_removeroot(void)
1111{
1112 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001113 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001114 debugfs_remove(dasd_debugfs_global_entry);
1115 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001116}
1117
1118static void dasd_statistics_createroot(void)
1119{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001120 struct dentry *pde;
1121
1122 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001123 pde = debugfs_create_dir("dasd", NULL);
1124 if (!pde || IS_ERR(pde))
1125 goto error;
1126 dasd_debugfs_root_entry = pde;
1127 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1128 if (!pde || IS_ERR(pde))
1129 goto error;
1130 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001131 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001132 return;
1133
1134error:
1135 DBF_EVENT(DBF_ERR, "%s",
1136 "Creation of the dasd debugfs interface failed");
1137 dasd_statistics_removeroot();
1138 return;
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001142#define dasd_profile_start(block, cqr, req) do {} while (0)
1143#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001144
1145static void dasd_statistics_createroot(void)
1146{
1147 return;
1148}
1149
1150static void dasd_statistics_removeroot(void)
1151{
1152 return;
1153}
1154
1155int dasd_stats_generic_show(struct seq_file *m, void *v)
1156{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001157 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001158 return 0;
1159}
1160
1161static void dasd_profile_init(struct dasd_profile *profile,
1162 struct dentry *base_dentry)
1163{
1164 return;
1165}
1166
1167static void dasd_profile_exit(struct dasd_profile *profile)
1168{
1169 return;
1170}
1171
1172int dasd_profile_on(struct dasd_profile *profile)
1173{
1174 return 0;
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177#endif /* CONFIG_DASD_PROFILE */
1178
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001179static int dasd_hosts_show(struct seq_file *m, void *v)
1180{
1181 struct dasd_device *device;
1182 int rc = -EOPNOTSUPP;
1183
1184 device = m->private;
1185 dasd_get_device(device);
1186
1187 if (device->discipline->hosts_print)
1188 rc = device->discipline->hosts_print(device, m);
1189
1190 dasd_put_device(device);
1191 return rc;
1192}
1193
1194static int dasd_hosts_open(struct inode *inode, struct file *file)
1195{
1196 struct dasd_device *device = inode->i_private;
1197
1198 return single_open(file, dasd_hosts_show, device);
1199}
1200
1201static const struct file_operations dasd_hosts_fops = {
1202 .owner = THIS_MODULE,
1203 .open = dasd_hosts_open,
1204 .read = seq_read,
1205 .llseek = seq_lseek,
1206 .release = single_release,
1207};
1208
1209static void dasd_hosts_exit(struct dasd_device *device)
1210{
1211 debugfs_remove(device->hosts_dentry);
1212 device->hosts_dentry = NULL;
1213}
1214
1215static void dasd_hosts_init(struct dentry *base_dentry,
1216 struct dasd_device *device)
1217{
1218 struct dentry *pde;
1219 umode_t mode;
1220
1221 if (!base_dentry)
1222 return;
1223
1224 mode = S_IRUSR | S_IFREG;
1225 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1226 device, &dasd_hosts_fops);
1227 if (pde && !IS_ERR(pde))
1228 device->hosts_dentry = pde;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
1232 * Allocate memory for a channel program with 'cplength' channel
1233 * command words and 'datasize' additional space. There are two
1234 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1235 * memory and 2) dasd_smalloc_request uses the static ccw memory
1236 * that gets allocated for each device.
1237 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001238struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001239 int datasize,
1240 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct dasd_ccw_req *cqr;
1243
1244 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001245 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001246 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001248 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (cqr == NULL)
1250 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 cqr->cpaddr = NULL;
1252 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001253 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 GFP_ATOMIC | GFP_DMA);
1255 if (cqr->cpaddr == NULL) {
1256 kfree(cqr);
1257 return ERR_PTR(-ENOMEM);
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 cqr->data = NULL;
1261 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001262 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001264 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 kfree(cqr);
1266 return ERR_PTR(-ENOMEM);
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001269 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1271 dasd_get_device(device);
1272 return cqr;
1273}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001274EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Stefan Haberland68b781f2009-09-11 10:28:29 +02001276struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001277 int datasize,
1278 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 unsigned long flags;
1281 struct dasd_ccw_req *cqr;
1282 char *data;
1283 int size;
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1286 if (cplength > 0)
1287 size += cplength * sizeof(struct ccw1);
1288 if (datasize > 0)
1289 size += datasize;
1290 spin_lock_irqsave(&device->mem_lock, flags);
1291 cqr = (struct dasd_ccw_req *)
1292 dasd_alloc_chunk(&device->ccw_chunks, size);
1293 spin_unlock_irqrestore(&device->mem_lock, flags);
1294 if (cqr == NULL)
1295 return ERR_PTR(-ENOMEM);
1296 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1297 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1298 cqr->cpaddr = NULL;
1299 if (cplength > 0) {
1300 cqr->cpaddr = (struct ccw1 *) data;
1301 data += cplength*sizeof(struct ccw1);
1302 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1303 }
1304 cqr->data = NULL;
1305 if (datasize > 0) {
1306 cqr->data = data;
1307 memset(cqr->data, 0, datasize);
1308 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001309 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1311 dasd_get_device(device);
1312 return cqr;
1313}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001314EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316/*
1317 * Free memory of a channel program. This function needs to free all the
1318 * idal lists that might have been created by dasd_set_cda and the
1319 * struct dasd_ccw_req itself.
1320 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001321void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 struct ccw1 *ccw;
1324
1325 /* Clear any idals used for the request. */
1326 ccw = cqr->cpaddr;
1327 do {
1328 clear_normalized_cda(ccw);
1329 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
Jesper Juhl17fd6822005-11-07 01:01:30 -08001330 kfree(cqr->cpaddr);
1331 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 kfree(cqr);
1333 dasd_put_device(device);
1334}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001335EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001337void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 unsigned long flags;
1340
1341 spin_lock_irqsave(&device->mem_lock, flags);
1342 dasd_free_chunk(&device->ccw_chunks, cqr);
1343 spin_unlock_irqrestore(&device->mem_lock, flags);
1344 dasd_put_device(device);
1345}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001346EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348/*
1349 * Check discipline magic in cqr.
1350 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001351static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct dasd_device *device;
1354
1355 if (cqr == NULL)
1356 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001357 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001359 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 " dasd_ccw_req 0x%08x magic doesn't match"
1361 " discipline 0x%08x",
1362 cqr->magic,
1363 *(unsigned int *) device->discipline->name);
1364 return -EINVAL;
1365 }
1366 return 0;
1367}
1368
1369/*
1370 * Terminate the current i/o and set the request to clear_pending.
1371 * Timer keeps device runnig.
1372 * ccw_device_clear can fail if the i/o subsystem
1373 * is in a bad mood.
1374 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001375int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct dasd_device *device;
1378 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001379 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* Check the cqr */
1382 rc = dasd_check_cqr(cqr);
1383 if (rc)
1384 return rc;
1385 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001386 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1388 rc = ccw_device_clear(device->cdev, (long) cqr);
1389 switch (rc) {
1390 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001391 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001392 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001393 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 DBF_DEV_EVENT(DBF_DEBUG, device,
1395 "terminate cqr %p successful",
1396 cqr);
1397 break;
1398 case -ENODEV:
1399 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1400 "device gone, retry");
1401 break;
1402 case -EIO:
1403 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1404 "I/O error, retry");
1405 break;
1406 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001407 /*
1408 * device not valid so no I/O could be running
1409 * handle CQR as termination successful
1410 */
1411 cqr->status = DASD_CQR_CLEARED;
1412 cqr->stopclk = get_tod_clock();
1413 cqr->starttime = 0;
1414 /* no retries for invalid devices */
1415 cqr->retries = -1;
1416 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1417 "EINVAL, handle as terminated");
1418 /* fake rc to success */
1419 rc = 0;
1420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 case -EBUSY:
1422 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1423 "device busy, retry later");
1424 break;
1425 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001426 /* internal error 10 - unknown rc*/
1427 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1428 dev_err(&device->cdev->dev, "An error occurred in the "
1429 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 BUG();
1431 break;
1432 }
1433 retries++;
1434 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001435 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return rc;
1437}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001438EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440/*
1441 * Start the i/o. This start_IO can fail if the channel is really busy.
1442 * In that case set up a timer to start the request later.
1443 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001444int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct dasd_device *device;
1447 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001448 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /* Check the cqr */
1451 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001452 if (rc) {
1453 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001455 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001456 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001457 if (((cqr->block &&
1458 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1459 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1460 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1461 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1462 "because of stolen lock", cqr);
1463 cqr->status = DASD_CQR_ERROR;
1464 cqr->intrc = -EPERM;
1465 return -EPERM;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001468 /* internal error 14 - start_IO run out of retries */
1469 sprintf(errorstring, "14 %p", cqr);
1470 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1471 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001472 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EIO;
1474 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001475 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 cqr->starttime = jiffies;
1477 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001478 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001479 cqr->lpm &= dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001480 if (!cqr->lpm)
Stefan Haberlandc9346152016-08-08 15:53:54 +02001481 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001482 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001483 if (cqr->cpmode == 1) {
1484 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1485 (long) cqr, cqr->lpm);
1486 } else {
1487 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1488 (long) cqr, cqr->lpm, 0);
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 switch (rc) {
1491 case 0:
1492 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001495 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 "start_IO: device busy, retry later");
1497 break;
1498 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001499 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 "start_IO: request timeout, retry later");
1501 break;
1502 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001503 /* -EACCES indicates that the request used only a subset of the
1504 * available paths and all these paths are gone. If the lpm of
1505 * this request was only a subset of the opm (e.g. the ppm) then
1506 * we just do a retry with all available paths.
1507 * If we already use the full opm, something is amiss, and we
1508 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001510 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1511 DBF_DEV_EVENT(DBF_WARNING, device,
1512 "start_IO: selected paths gone (%x)",
1513 cqr->lpm);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001514 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1515 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001516 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1517 "start_IO: selected paths gone,"
1518 " retry on all paths");
1519 } else {
1520 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1521 "start_IO: all paths in opm gone,"
1522 " do path verification");
1523 dasd_generic_last_path_gone(device);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001524 dasd_path_no_path(device);
1525 dasd_path_set_tbvpm(device,
1526 ccw_device_get_path_mask(
1527 device->cdev));
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 break;
1530 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001531 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001532 "start_IO: -ENODEV device gone, retry");
1533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001535 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001536 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001538 case -EINVAL:
1539 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001540 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001541 "start_IO: -EINVAL device currently "
1542 "not accessible");
1543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001545 /* internal error 11 - unknown rc */
1546 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1547 dev_err(&device->cdev->dev,
1548 "An error occurred in the DASD device driver, "
1549 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 BUG();
1551 break;
1552 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001553 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return rc;
1555}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001556EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558/*
1559 * Timeout function for dasd devices. This is used for different purposes
1560 * 1) missing interrupt handler for normal operation
1561 * 2) delayed start of request where start_IO failed with -EBUSY
1562 * 3) timeout for missing state change interrupts
1563 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1564 * DASD_CQR_QUEUED for 2) and 3).
1565 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001566static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 unsigned long flags;
1569 struct dasd_device *device;
1570
1571 device = (struct dasd_device *) ptr;
1572 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1573 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001574 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001576 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579/*
1580 * Setup timeout for a device in jiffies.
1581 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001582void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001584 if (expires == 0)
1585 del_timer(&device->timer);
1586 else
1587 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001589EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591/*
1592 * Clear timeout for a device.
1593 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001594void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001596 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001598EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001600static void dasd_handle_killed_request(struct ccw_device *cdev,
1601 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 struct dasd_ccw_req *cqr;
1604 struct dasd_device *device;
1605
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001606 if (!intparm)
1607 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 cqr = (struct dasd_ccw_req *) intparm;
1609 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001610 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1611 "invalid status in handle_killed_request: "
1612 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return;
1614 }
1615
Stefan Haberland589c74d2010-02-26 22:37:47 +01001616 device = dasd_device_from_cdev_locked(cdev);
1617 if (IS_ERR(device)) {
1618 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1619 "unable to get device from cdev");
1620 return;
1621 }
1622
1623 if (!cqr->startdev ||
1624 device != cqr->startdev ||
1625 strncmp(cqr->startdev->discipline->ebcname,
1626 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001627 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1628 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001629 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return;
1631 }
1632
1633 /* Schedule request to be retried. */
1634 cqr->status = DASD_CQR_QUEUED;
1635
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001636 dasd_device_clear_timer(device);
1637 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 dasd_put_device(device);
1639}
1640
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001641void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001643 /* First of all start sense subsystem status request. */
1644 dasd_eer_snss(device);
1645
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001646 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001647 dasd_schedule_device_bh(device);
1648 if (device->block)
1649 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001651EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Stefan Haberlanda521b042016-08-08 15:56:54 +02001653static int dasd_check_hpf_error(struct irb *irb)
1654{
1655 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1656 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1657 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/*
1661 * Interrupt handler for "normal" ssch-io based dasd devices.
1662 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001663void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1664 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct dasd_ccw_req *cqr, *next;
1667 struct dasd_device *device;
Jan Höppner7bf76f012017-08-15 16:40:18 +02001668 unsigned long now;
Jan Höppner8fd57522015-08-19 13:41:20 +02001669 int nrf_suppressed = 0;
1670 int fp_suppressed = 0;
1671 u8 *sense = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001674 cqr = (struct dasd_ccw_req *) intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (IS_ERR(irb)) {
1676 switch (PTR_ERR(irb)) {
1677 case -EIO:
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001678 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001679 device = cqr->startdev;
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001680 cqr->status = DASD_CQR_CLEARED;
1681 dasd_device_clear_timer(device);
1682 wake_up(&dasd_flush_wq);
1683 dasd_schedule_device_bh(device);
1684 return;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 break;
1687 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001688 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1689 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001692 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1693 "unknown error %ld\n", __func__,
1694 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Stefan Weinhuberf16f58432008-05-15 16:52:36 +02001696 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return;
1698 }
1699
Heiko Carstens1aae0562013-01-30 09:49:40 +01001700 now = get_tod_clock();
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001701 /* check for conditions that should be handled immediately */
1702 if (!cqr ||
1703 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1704 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001705 if (cqr)
1706 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001707 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001708 if (IS_ERR(device))
1709 return;
1710 /* ignore unsolicited interrupts for DIAG discipline */
1711 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001715
1716 /*
1717 * In some cases 'File Protected' or 'No Record Found' errors
1718 * might be expected and debug log messages for the
1719 * corresponding interrupts shouldn't be written then.
1720 * Check if either of the according suppress bits is set.
1721 */
1722 sense = dasd_get_sense(irb);
1723 if (sense) {
1724 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1725 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1726 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1727 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1728 }
1729 if (!(fp_suppressed || nrf_suppressed))
1730 device->discipline->dump_sense_dbf(device, irb, "int");
1731
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001732 if (device->features & DASD_FEATURE_ERPLOG)
1733 device->discipline->dump_sense(device, cqr, irb);
1734 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001735 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001737
1738 /* check for for attention message */
1739 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1740 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland22021342017-01-25 14:47:32 +01001741 if (!IS_ERR(device)) {
1742 device->discipline->check_attention(device,
1743 irb->esw.esw1.lpum);
1744 dasd_put_device(device);
1745 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001746 }
1747
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001748 if (!cqr)
1749 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001751 device = (struct dasd_device *) cqr->startdev;
1752 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001754 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1755 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
1757 }
1758
1759 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001761 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 cqr->status = DASD_CQR_CLEARED;
1763 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001764 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001765 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return;
1767 }
1768
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001769 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001771 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1772 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return;
1774 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001775
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001776 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001778 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1779 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001780 /* request was completed successfully */
1781 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 cqr->stopclk = now;
1783 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001784 if (cqr->devlist.next != &device->ccw_queue) {
1785 next = list_entry(cqr->devlist.next,
1786 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001788 } else { /* error */
Stefan Haberlanda521b042016-08-08 15:56:54 +02001789 /* check for HPF error
1790 * call discipline function to requeue all requests
1791 * and disable HPF accordingly
1792 */
1793 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1794 device->discipline->handle_hpf_error)
1795 device->discipline->handle_hpf_error(device, irb);
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001796 /*
1797 * If we don't want complex ERP for this request, then just
1798 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001799 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001800 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001801 cqr->retries > 0) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001802 if (cqr->lpm == dasd_path_get_opm(device))
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001803 DBF_DEV_EVENT(DBF_DEBUG, device,
1804 "default ERP in fastpath "
1805 "(%i retries left)",
1806 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001807 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
Stefan Haberlandc9346152016-08-08 15:53:54 +02001808 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001809 cqr->status = DASD_CQR_QUEUED;
1810 next = cqr;
1811 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001813 }
1814 if (next && (next->status == DASD_CQR_QUEUED) &&
1815 (!device->stopped)) {
1816 if (device->discipline->start_IO(next) == 0)
1817 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001820 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001822 dasd_device_clear_timer(device);
1823 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001825EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001827enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1828{
1829 struct dasd_device *device;
1830
1831 device = dasd_device_from_cdev_locked(cdev);
1832
1833 if (IS_ERR(device))
1834 goto out;
1835 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1836 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001837 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001838 dasd_put_device(device);
1839 goto out;
1840 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001841 if (device->discipline->dump_sense_dbf)
1842 device->discipline->dump_sense_dbf(device, irb, "uc");
1843 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001844 dasd_put_device(device);
1845out:
1846 return UC_TODO_RETRY;
1847}
1848EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001851 * If we have an error on a dasd_block layer request then we cancel
1852 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001854static void __dasd_device_recovery(struct dasd_device *device,
1855 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct list_head *l, *n;
1858 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001861 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001863 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001865
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001866 list_for_each_safe(l, n, &device->ccw_queue) {
1867 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1868 if (cqr->status == DASD_CQR_QUEUED &&
1869 ref_cqr->block == cqr->block) {
1870 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001872 }
1873};
1874
1875/*
1876 * Remove those ccw requests from the queue that need to be returned
1877 * to the upper layer.
1878 */
1879static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1880 struct list_head *final_queue)
1881{
1882 struct list_head *l, *n;
1883 struct dasd_ccw_req *cqr;
1884
1885 /* Process request with final status. */
1886 list_for_each_safe(l, n, &device->ccw_queue) {
1887 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1888
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001889 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001890 if (cqr->status == DASD_CQR_QUEUED ||
1891 cqr->status == DASD_CQR_IN_IO ||
1892 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001893 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001894 if (cqr->status == DASD_CQR_ERROR) {
1895 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001897 /* Rechain finished requests to final queue */
1898 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900}
1901
1902/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001903 * the cqrs from the final queue are returned to the upper layer
1904 * by setting a dasd_block state and calling the callback function
1905 */
1906static void __dasd_device_process_final_queue(struct dasd_device *device,
1907 struct list_head *final_queue)
1908{
1909 struct list_head *l, *n;
1910 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001911 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001912 void (*callback)(struct dasd_ccw_req *, void *data);
1913 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001914 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001915
1916 list_for_each_safe(l, n, final_queue) {
1917 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1918 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001919 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001920 callback = cqr->callback;
1921 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001922 if (block)
1923 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001924 switch (cqr->status) {
1925 case DASD_CQR_SUCCESS:
1926 cqr->status = DASD_CQR_DONE;
1927 break;
1928 case DASD_CQR_ERROR:
1929 cqr->status = DASD_CQR_NEED_ERP;
1930 break;
1931 case DASD_CQR_CLEARED:
1932 cqr->status = DASD_CQR_TERMINATED;
1933 break;
1934 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001935 /* internal error 12 - wrong cqr status*/
1936 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1937 dev_err(&device->cdev->dev,
1938 "An error occurred in the DASD device driver, "
1939 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001940 BUG();
1941 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001942 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001943 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001944 if (block)
1945 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001946 }
1947}
1948
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 * Take a look at the first request on the ccw queue and check
1951 * if it reached its expire time. If so, terminate the IO.
1952 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001953static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 struct dasd_ccw_req *cqr;
1956
1957 if (list_empty(&device->ccw_queue))
1958 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001959 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001960 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1961 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001962 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1963 /*
1964 * IO in safe offline processing should not
1965 * run out of retries
1966 */
1967 cqr->retries++;
1968 }
Horst Hummel29145a62006-12-04 15:40:15 +01001969 if (device->discipline->term_IO(cqr) != 0) {
1970 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001971 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001972 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001973 "ended, retrying in 5 s\n",
1974 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001975 cqr->expires += 5*HZ;
1976 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001977 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001978 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001979 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001980 "remaining\n", cqr, (cqr->expires/HZ),
1981 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 }
1984}
1985
1986/*
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001987 * return 1 when device is not eligible for IO
1988 */
1989static int __dasd_device_is_unusable(struct dasd_device *device,
1990 struct dasd_ccw_req *cqr)
1991{
1992 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1993
Stefan Haberlande8ac0152017-05-18 13:24:45 +02001994 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
1995 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1996 /*
1997 * dasd is being set offline
1998 * but it is no safe offline where we have to allow I/O
1999 */
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002000 return 1;
2001 }
2002 if (device->stopped) {
2003 if (device->stopped & mask) {
2004 /* stopped and CQR will not change that. */
2005 return 1;
2006 }
2007 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2008 /* CQR is not able to change device to
2009 * operational. */
2010 return 1;
2011 }
2012 /* CQR required to get device operational. */
2013 }
2014 return 0;
2015}
2016
2017/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 * Take a look at the first request on the ccw queue and check
2019 * if it needs to be started.
2020 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002021static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct dasd_ccw_req *cqr;
2024 int rc;
2025
2026 if (list_empty(&device->ccw_queue))
2027 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002028 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002029 if (cqr->status != DASD_CQR_QUEUED)
2030 return;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002031 /* if device is not usable return request to upper layer */
2032 if (__dasd_device_is_unusable(device, cqr)) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002033 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 cqr->status = DASD_CQR_CLEARED;
2035 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002036 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002037 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002038
2039 rc = device->discipline->start_IO(cqr);
2040 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002041 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002042 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002043 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002044 } else
2045 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002046 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02002047}
2048
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002049static void __dasd_device_check_path_events(struct dasd_device *device)
2050{
2051 int rc;
2052
Stefan Haberlandc9346152016-08-08 15:53:54 +02002053 if (!dasd_path_get_tbvpm(device))
2054 return;
2055
2056 if (device->stopped &
2057 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2058 return;
2059 rc = device->discipline->verify_path(device,
2060 dasd_path_get_tbvpm(device));
2061 if (rc)
2062 dasd_device_set_timer(device, 50);
2063 else
2064 dasd_path_clear_all_verify(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002065};
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002068 * Go through all request on the dasd_device request queue,
2069 * terminate them on the cdev if necessary, and return them to the
2070 * submitting layer via callback.
2071 * Note:
2072 * Make sure that all 'submitting layers' still exist when
2073 * this function is called!. In other words, when 'device' is a base
2074 * device then all block layer requests must have been removed before
2075 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002077int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002079 struct dasd_ccw_req *cqr, *n;
2080 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 INIT_LIST_HEAD(&flush_queue);
2084 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02002085 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002086 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02002087 /* Check status and move request to flush_queue */
2088 switch (cqr->status) {
2089 case DASD_CQR_IN_IO:
2090 rc = device->discipline->term_IO(cqr);
2091 if (rc) {
2092 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002093 dev_err(&device->cdev->dev,
2094 "Flushing the DASD request queue "
2095 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002096 /* stop flush processing */
2097 goto finished;
2098 }
2099 break;
2100 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002101 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002103 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002104 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002105 break;
2106 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002107 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002108 }
Horst Hummel8f617012006-08-30 14:33:33 +02002109finished:
2110 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002111 /*
2112 * After this point all requests must be in state CLEAR_PENDING,
2113 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2114 * one of the others.
2115 */
2116 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2117 wait_event(dasd_flush_wq,
2118 (cqr->status != DASD_CQR_CLEAR_PENDING));
2119 /*
2120 * Now set each request back to TERMINATED, DONE or NEED_ERP
2121 * and call the callback function of flushed requests
2122 */
2123 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002124 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002126EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128/*
2129 * Acquire the device lock and process queues for the device.
2130 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002131static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 atomic_set (&device->tasklet_scheduled, 0);
2136 INIT_LIST_HEAD(&final_queue);
2137 spin_lock_irq(get_ccwdev_lock(device->cdev));
2138 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002139 __dasd_device_check_expire(device);
2140 /* find final requests on ccw queue */
2141 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002142 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2144 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002145 __dasd_device_process_final_queue(device, &final_queue);
2146 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002148 __dasd_device_start_head(device);
2149 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002150 if (waitqueue_active(&shutdown_waitq))
2151 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 dasd_put_device(device);
2153}
2154
2155/*
2156 * Schedules a call to dasd_tasklet over the device tasklet.
2157 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002158void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002161 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return;
2163 dasd_get_device(device);
2164 tasklet_hi_schedule(&device->tasklet);
2165}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002166EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002168void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2169{
2170 device->stopped |= bits;
2171}
2172EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2173
2174void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2175{
2176 device->stopped &= ~bits;
2177 if (!device->stopped)
2178 wake_up(&generic_waitq);
2179}
2180EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002183 * Queue a request to the head of the device ccw_queue.
2184 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002186void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct dasd_device *device;
2189 unsigned long flags;
2190
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002191 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002193 cqr->status = DASD_CQR_QUEUED;
2194 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002196 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2198}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002199EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002202 * Queue a request to the tail of the device ccw_queue.
2203 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002205void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 struct dasd_device *device;
2208 unsigned long flags;
2209
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002210 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002212 cqr->status = DASD_CQR_QUEUED;
2213 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002215 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2217}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002218EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002221 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002223void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002225 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2226 cqr->callback_data = DASD_SLEEPON_END_TAG;
2227 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2228 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
Stefan Haberland5915a872011-10-30 15:16:57 +01002230EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002232static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 struct dasd_device *device;
2235 int rc;
2236
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002237 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002239 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2241 return rc;
2242}
2243
2244/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002245 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2246 */
2247static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2248{
2249 struct dasd_device *device;
2250 dasd_erp_fn_t erp_fn;
2251
2252 if (cqr->status == DASD_CQR_FILLED)
2253 return 0;
2254 device = cqr->startdev;
2255 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2256 if (cqr->status == DASD_CQR_TERMINATED) {
2257 device->discipline->handle_terminated_request(cqr);
2258 return 1;
2259 }
2260 if (cqr->status == DASD_CQR_NEED_ERP) {
2261 erp_fn = device->discipline->erp_action(cqr);
2262 erp_fn(cqr);
2263 return 1;
2264 }
2265 if (cqr->status == DASD_CQR_FAILED)
2266 dasd_log_sense(cqr, &cqr->irb);
2267 if (cqr->refers) {
2268 __dasd_process_erp(device, cqr);
2269 return 1;
2270 }
2271 }
2272 return 0;
2273}
2274
2275static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2276{
2277 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2278 if (cqr->refers) /* erp is not done yet */
2279 return 1;
2280 return ((cqr->status != DASD_CQR_DONE) &&
2281 (cqr->status != DASD_CQR_FAILED));
2282 } else
2283 return (cqr->status == DASD_CQR_FILLED);
2284}
2285
2286static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2287{
2288 struct dasd_device *device;
2289 int rc;
2290 struct list_head ccw_queue;
2291 struct dasd_ccw_req *cqr;
2292
2293 INIT_LIST_HEAD(&ccw_queue);
2294 maincqr->status = DASD_CQR_FILLED;
2295 device = maincqr->startdev;
2296 list_add(&maincqr->blocklist, &ccw_queue);
2297 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2298 cqr = list_first_entry(&ccw_queue,
2299 struct dasd_ccw_req, blocklist)) {
2300
2301 if (__dasd_sleep_on_erp(cqr))
2302 continue;
2303 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2304 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002305 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2306 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2307 cqr->status = DASD_CQR_FAILED;
2308 cqr->intrc = -EPERM;
2309 continue;
2310 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002311 /* Non-temporary stop condition will trigger fail fast */
2312 if (device->stopped & ~DASD_STOPPED_PENDING &&
2313 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2314 (!dasd_eer_enabled(device))) {
2315 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002316 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002317 continue;
2318 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002319 /*
Stefan Haberlanda9f62732016-09-20 10:29:22 +02002320 * Don't try to start requests if device is in
2321 * offline processing, it might wait forever
2322 */
2323 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2324 cqr->status = DASD_CQR_FAILED;
2325 cqr->intrc = -ENODEV;
2326 continue;
2327 }
2328 /*
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002329 * Don't try to start requests if device is stopped
2330 * except path verification requests
2331 */
2332 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2333 if (interruptible) {
2334 rc = wait_event_interruptible(
2335 generic_waitq, !(device->stopped));
2336 if (rc == -ERESTARTSYS) {
2337 cqr->status = DASD_CQR_FAILED;
2338 maincqr->intrc = rc;
2339 continue;
2340 }
2341 } else
2342 wait_event(generic_waitq, !(device->stopped));
2343 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002344 if (!cqr->callback)
2345 cqr->callback = dasd_wakeup_cb;
2346
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002347 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002348 dasd_add_request_tail(cqr);
2349 if (interruptible) {
2350 rc = wait_event_interruptible(
2351 generic_waitq, _wait_for_wakeup(cqr));
2352 if (rc == -ERESTARTSYS) {
2353 dasd_cancel_req(cqr);
2354 /* wait (non-interruptible) for final status */
2355 wait_event(generic_waitq,
2356 _wait_for_wakeup(cqr));
2357 cqr->status = DASD_CQR_FAILED;
2358 maincqr->intrc = rc;
2359 continue;
2360 }
2361 } else
2362 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2363 }
2364
Heiko Carstens1aae0562013-01-30 09:49:40 +01002365 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002366 if ((maincqr->status != DASD_CQR_DONE) &&
2367 (maincqr->intrc != -ERESTARTSYS))
2368 dasd_log_sense(maincqr, &maincqr->irb);
2369 if (maincqr->status == DASD_CQR_DONE)
2370 rc = 0;
2371 else if (maincqr->intrc)
2372 rc = maincqr->intrc;
2373 else
2374 rc = -EIO;
2375 return rc;
2376}
2377
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002378static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2379{
2380 struct dasd_ccw_req *cqr;
2381
2382 list_for_each_entry(cqr, ccw_queue, blocklist) {
2383 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2384 return 0;
2385 }
2386
2387 return 1;
2388}
2389
2390static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2391{
2392 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002393 struct dasd_ccw_req *cqr, *n;
Jan Höppner8fd57522015-08-19 13:41:20 +02002394 u8 *sense = NULL;
Stefan Haberland362ce842014-10-01 13:04:54 +02002395 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002396
2397retry:
2398 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2399 device = cqr->startdev;
2400 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2401 continue;
2402
2403 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2404 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2405 cqr->status = DASD_CQR_FAILED;
2406 cqr->intrc = -EPERM;
2407 continue;
2408 }
2409 /*Non-temporary stop condition will trigger fail fast*/
2410 if (device->stopped & ~DASD_STOPPED_PENDING &&
2411 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2412 !dasd_eer_enabled(device)) {
2413 cqr->status = DASD_CQR_FAILED;
2414 cqr->intrc = -EAGAIN;
2415 continue;
2416 }
2417
2418 /*Don't try to start requests if device is stopped*/
2419 if (interruptible) {
2420 rc = wait_event_interruptible(
2421 generic_waitq, !device->stopped);
2422 if (rc == -ERESTARTSYS) {
2423 cqr->status = DASD_CQR_FAILED;
2424 cqr->intrc = rc;
2425 continue;
2426 }
2427 } else
2428 wait_event(generic_waitq, !(device->stopped));
2429
2430 if (!cqr->callback)
2431 cqr->callback = dasd_wakeup_cb;
2432 cqr->callback_data = DASD_SLEEPON_START_TAG;
2433 dasd_add_request_tail(cqr);
2434 }
2435
2436 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2437
2438 rc = 0;
2439 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002440 /*
Jan Höppner8fd57522015-08-19 13:41:20 +02002441 * In some cases the 'File Protected' or 'Incorrect Length'
2442 * error might be expected and error recovery would be
2443 * unnecessary in these cases. Check if the according suppress
2444 * bit is set.
2445 */
2446 sense = dasd_get_sense(&cqr->irb);
2447 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2448 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2449 continue;
2450 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2451 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2452 continue;
2453
2454 /*
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002455 * for alias devices simplify error recovery and
2456 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002457 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002458 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002459 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002460 (cqr->status == DASD_CQR_TERMINATED ||
2461 cqr->status == DASD_CQR_NEED_ERP))
2462 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002463
2464 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002465 if (__dasd_sleep_on_erp(cqr))
2466 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002467 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002468 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002469
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002470 return 0;
2471}
2472
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002473/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002474 * Queue a request to the tail of the device ccw_queue and wait for
2475 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002477int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002479 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002481EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002484 * Start requests from a ccw_queue and wait for their completion.
2485 */
2486int dasd_sleep_on_queue(struct list_head *ccw_queue)
2487{
2488 return _dasd_sleep_on_queue(ccw_queue, 0);
2489}
2490EXPORT_SYMBOL(dasd_sleep_on_queue);
2491
2492/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002493 * Queue a request to the tail of the device ccw_queue and wait
2494 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002496int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002498 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002500EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502/*
2503 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2504 * for eckd devices) the currently running request has to be terminated
2505 * and be put back to status queued, before the special request is added
2506 * to the head of the queue. Then the special request is waited on normally.
2507 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002508static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002511 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 if (list_empty(&device->ccw_queue))
2514 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002515 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002516 rc = device->discipline->term_IO(cqr);
2517 if (!rc)
2518 /*
2519 * CQR terminated because a more important request is pending.
2520 * Undo decreasing of retry counter because this is
2521 * not an error case.
2522 */
2523 cqr->retries++;
2524 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002527int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct dasd_device *device;
2530 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002531
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002532 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002533 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2534 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2535 cqr->status = DASD_CQR_FAILED;
2536 cqr->intrc = -EPERM;
2537 return -EIO;
2538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 spin_lock_irq(get_ccwdev_lock(device->cdev));
2540 rc = _dasd_term_running_cqr(device);
2541 if (rc) {
2542 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2543 return rc;
2544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002546 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002548 /*
2549 * add new request as second
2550 * first the terminated cqr needs to be finished
2551 */
2552 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002555 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2558
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002559 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002560
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002561 if (cqr->status == DASD_CQR_DONE)
2562 rc = 0;
2563 else if (cqr->intrc)
2564 rc = cqr->intrc;
2565 else
2566 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002567
2568 /* kick tasklets */
2569 dasd_schedule_device_bh(device);
2570 if (device->block)
2571 dasd_schedule_block_bh(device->block);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return rc;
2574}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002575EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577/*
2578 * Cancels a request that was started with dasd_sleep_on_req.
2579 * This is useful to timeout requests. The request will be
2580 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002581 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002582 * negative error code if termination failed
2583 * Cancellation of a request is an asynchronous operation! The calling
2584 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002586int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002588 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 unsigned long flags;
2590 int rc;
2591
2592 rc = 0;
2593 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2594 switch (cqr->status) {
2595 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002596 /* request was not started - just set to cleared */
2597 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002598 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2599 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 break;
2601 case DASD_CQR_IN_IO:
2602 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002603 rc = device->discipline->term_IO(cqr);
2604 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002605 dev_err(&device->cdev->dev,
2606 "Cancelling request %p failed with rc=%d\n",
2607 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002608 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002609 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002612 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002616 dasd_schedule_device_bh(device);
2617 return rc;
2618}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002619EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002620
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002621/*
2622 * SECTION: Operations of the dasd_block layer.
2623 */
2624
2625/*
2626 * Timeout function for dasd_block. This is used when the block layer
2627 * is waiting for something that may not come reliably, (e.g. a state
2628 * change interrupt)
2629 */
2630static void dasd_block_timeout(unsigned long ptr)
2631{
2632 unsigned long flags;
2633 struct dasd_block *block;
2634
2635 block = (struct dasd_block *) ptr;
2636 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2637 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002638 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002639 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2640 dasd_schedule_block_bh(block);
2641}
2642
2643/*
2644 * Setup timeout for a dasd_block in jiffies.
2645 */
2646void dasd_block_set_timer(struct dasd_block *block, int expires)
2647{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002648 if (expires == 0)
2649 del_timer(&block->timer);
2650 else
2651 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002652}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002653EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002654
2655/*
2656 * Clear timeout for a dasd_block.
2657 */
2658void dasd_block_clear_timer(struct dasd_block *block)
2659{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002660 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002661}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002662EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002663
2664/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002665 * Process finished error recovery ccw.
2666 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002667static void __dasd_process_erp(struct dasd_device *device,
2668 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002669{
2670 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002671
2672 if (cqr->status == DASD_CQR_DONE)
2673 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2674 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002675 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002676 erp_fn = device->discipline->erp_postaction(cqr);
2677 erp_fn(cqr);
2678}
2679
2680/*
2681 * Fetch requests from the block device queue.
2682 */
2683static void __dasd_process_request_queue(struct dasd_block *block)
2684{
2685 struct request_queue *queue;
2686 struct request *req;
2687 struct dasd_ccw_req *cqr;
2688 struct dasd_device *basedev;
2689 unsigned long flags;
2690 queue = block->request_queue;
2691 basedev = block->base;
2692 /* No queue ? Then there is nothing to do. */
2693 if (queue == NULL)
2694 return;
2695
2696 /*
2697 * We requeue request from the block device queue to the ccw
2698 * queue only in two states. In state DASD_STATE_READY the
2699 * partition detection is done and we need to requeue requests
2700 * for that. State DASD_STATE_ONLINE is normal block device
2701 * operation.
2702 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002703 if (basedev->state < DASD_STATE_READY) {
2704 while ((req = blk_fetch_request(block->request_queue)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002705 __blk_end_request_all(req, BLK_STS_IOERR);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002706 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002707 }
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002708
Stefan Haberlandc6fc7b62015-12-22 13:34:38 +01002709 /*
2710 * if device is stopped do not fetch new requests
2711 * except failfast is active which will let requests fail
2712 * immediately in __dasd_block_start_head()
2713 */
2714 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST))
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002715 return;
2716
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002717 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002718 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002719 if (basedev->features & DASD_FEATURE_READONLY &&
2720 rq_data_dir(req) == WRITE) {
2721 DBF_DEV_EVENT(DBF_ERR, basedev,
2722 "Rejecting write request %p",
2723 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002724 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002725 __blk_end_request_all(req, BLK_STS_IOERR);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002726 continue;
2727 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002728 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2729 (basedev->features & DASD_FEATURE_FAILFAST ||
2730 blk_noretry_request(req))) {
2731 DBF_DEV_EVENT(DBF_ERR, basedev,
2732 "Rejecting failfast request %p",
2733 req);
2734 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002735 __blk_end_request_all(req, BLK_STS_TIMEOUT);
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002736 continue;
2737 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002738 cqr = basedev->discipline->build_cp(basedev, block, req);
2739 if (IS_ERR(cqr)) {
2740 if (PTR_ERR(cqr) == -EBUSY)
2741 break; /* normal end condition */
2742 if (PTR_ERR(cqr) == -ENOMEM)
2743 break; /* terminate request queue loop */
2744 if (PTR_ERR(cqr) == -EAGAIN) {
2745 /*
2746 * The current request cannot be build right
2747 * now, we have to try later. If this request
2748 * is the head-of-queue we stop the device
2749 * for 1/2 second.
2750 */
2751 if (!list_empty(&block->ccw_queue))
2752 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002753 spin_lock_irqsave(
2754 get_ccwdev_lock(basedev->cdev), flags);
2755 dasd_device_set_stop_bits(basedev,
2756 DASD_STOPPED_PENDING);
2757 spin_unlock_irqrestore(
2758 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002759 dasd_block_set_timer(block, HZ/2);
2760 break;
2761 }
2762 DBF_DEV_EVENT(DBF_ERR, basedev,
2763 "CCW creation failed (rc=%ld) "
2764 "on request %p",
2765 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002766 blk_start_request(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002767 __blk_end_request_all(req, BLK_STS_IOERR);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002768 continue;
2769 }
2770 /*
2771 * Note: callback is set to dasd_return_cqr_cb in
2772 * __dasd_block_start_head to cover erp requests as well
2773 */
2774 cqr->callback_data = (void *) req;
2775 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002776 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002777 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002778 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002779 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002780 dasd_profile_start(block, cqr, req);
2781 }
2782}
2783
2784static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2785{
2786 struct request *req;
2787 int status;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002788 blk_status_t error = BLK_STS_OK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002789
2790 req = (struct request *) cqr->callback_data;
2791 dasd_profile_end(cqr->block, cqr, req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002792
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002793 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002794 if (status < 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002795 error = errno_to_blk_status(status);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002796 else if (status == 0) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002797 switch (cqr->intrc) {
2798 case -EPERM:
2799 error = BLK_STS_NEXUS;
2800 break;
2801 case -ENOLINK:
2802 error = BLK_STS_TRANSPORT;
2803 break;
2804 case -ETIMEDOUT:
2805 error = BLK_STS_TIMEOUT;
2806 break;
2807 default:
2808 error = BLK_STS_IOERR;
2809 break;
2810 }
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002811 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002812 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002813}
2814
2815/*
2816 * Process ccw request queue.
2817 */
2818static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2819 struct list_head *final_queue)
2820{
2821 struct list_head *l, *n;
2822 struct dasd_ccw_req *cqr;
2823 dasd_erp_fn_t erp_fn;
2824 unsigned long flags;
2825 struct dasd_device *base = block->base;
2826
2827restart:
2828 /* Process request with final status. */
2829 list_for_each_safe(l, n, &block->ccw_queue) {
2830 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2831 if (cqr->status != DASD_CQR_DONE &&
2832 cqr->status != DASD_CQR_FAILED &&
2833 cqr->status != DASD_CQR_NEED_ERP &&
2834 cqr->status != DASD_CQR_TERMINATED)
2835 continue;
2836
2837 if (cqr->status == DASD_CQR_TERMINATED) {
2838 base->discipline->handle_terminated_request(cqr);
2839 goto restart;
2840 }
2841
2842 /* Process requests that may be recovered */
2843 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002844 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002845 if (IS_ERR(erp_fn(cqr)))
2846 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002847 goto restart;
2848 }
2849
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002850 /* log sense for fatal error */
2851 if (cqr->status == DASD_CQR_FAILED) {
2852 dasd_log_sense(cqr, &cqr->irb);
2853 }
2854
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002855 /* First of all call extended error reporting. */
2856 if (dasd_eer_enabled(base) &&
2857 cqr->status == DASD_CQR_FAILED) {
2858 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2859
2860 /* restart request */
2861 cqr->status = DASD_CQR_FILLED;
2862 cqr->retries = 255;
2863 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002864 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002865 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2866 flags);
2867 goto restart;
2868 }
2869
2870 /* Process finished ERP request. */
2871 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002872 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002873 goto restart;
2874 }
2875
2876 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002877 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002878 list_move_tail(&cqr->blocklist, final_queue);
2879 }
2880}
2881
2882static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2883{
2884 dasd_schedule_block_bh(cqr->block);
2885}
2886
2887static void __dasd_block_start_head(struct dasd_block *block)
2888{
2889 struct dasd_ccw_req *cqr;
2890
2891 if (list_empty(&block->ccw_queue))
2892 return;
2893 /* We allways begin with the first requests on the queue, as some
2894 * of previously started requests have to be enqueued on a
2895 * dasd_device again for error recovery.
2896 */
2897 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2898 if (cqr->status != DASD_CQR_FILLED)
2899 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002900 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2901 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2902 cqr->status = DASD_CQR_FAILED;
2903 cqr->intrc = -EPERM;
2904 dasd_schedule_block_bh(block);
2905 continue;
2906 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002907 /* Non-temporary stop condition will trigger fail fast */
2908 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2909 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2910 (!dasd_eer_enabled(block->base))) {
2911 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002912 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002913 dasd_schedule_block_bh(block);
2914 continue;
2915 }
2916 /* Don't try to start requests if device is stopped */
2917 if (block->base->stopped)
2918 return;
2919
2920 /* just a fail safe check, should not happen */
2921 if (!cqr->startdev)
2922 cqr->startdev = block->base;
2923
2924 /* make sure that the requests we submit find their way back */
2925 cqr->callback = dasd_return_cqr_cb;
2926
2927 dasd_add_request_tail(cqr);
2928 }
2929}
2930
2931/*
2932 * Central dasd_block layer routine. Takes requests from the generic
2933 * block layer request queue, creates ccw requests, enqueues them on
2934 * a dasd_device and processes ccw requests that have been returned.
2935 */
2936static void dasd_block_tasklet(struct dasd_block *block)
2937{
2938 struct list_head final_queue;
2939 struct list_head *l, *n;
2940 struct dasd_ccw_req *cqr;
2941
2942 atomic_set(&block->tasklet_scheduled, 0);
2943 INIT_LIST_HEAD(&final_queue);
2944 spin_lock(&block->queue_lock);
2945 /* Finish off requests on ccw queue */
2946 __dasd_process_block_ccw_queue(block, &final_queue);
2947 spin_unlock(&block->queue_lock);
2948 /* Now call the callback function of requests with final status */
2949 spin_lock_irq(&block->request_queue_lock);
2950 list_for_each_safe(l, n, &final_queue) {
2951 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2952 list_del_init(&cqr->blocklist);
2953 __dasd_cleanup_cqr(cqr);
2954 }
2955 spin_lock(&block->queue_lock);
2956 /* Get new request from the block device request queue */
2957 __dasd_process_request_queue(block);
2958 /* Now check if the head of the ccw queue needs to be started. */
2959 __dasd_block_start_head(block);
2960 spin_unlock(&block->queue_lock);
2961 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002962 if (waitqueue_active(&shutdown_waitq))
2963 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002964 dasd_put_device(block->base);
2965}
2966
2967static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2968{
2969 wake_up(&dasd_flush_wq);
2970}
2971
2972/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002973 * Requeue a request back to the block request queue
2974 * only works for block requests
2975 */
2976static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2977{
2978 struct dasd_block *block = cqr->block;
2979 struct request *req;
2980 unsigned long flags;
2981
2982 if (!block)
2983 return -EINVAL;
Stefan Haberlanda521b042016-08-08 15:56:54 +02002984 spin_lock_irqsave(&block->request_queue_lock, flags);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002985 req = (struct request *) cqr->callback_data;
2986 blk_requeue_request(block->request_queue, req);
Stefan Haberlanda521b042016-08-08 15:56:54 +02002987 spin_unlock_irqrestore(&block->request_queue_lock, flags);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002988
2989 return 0;
2990}
2991
2992/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002993 * Go through all request on the dasd_block request queue, cancel them
2994 * on the respective dasd_device, and return them to the generic
2995 * block layer.
2996 */
2997static int dasd_flush_block_queue(struct dasd_block *block)
2998{
2999 struct dasd_ccw_req *cqr, *n;
3000 int rc, i;
3001 struct list_head flush_queue;
3002
3003 INIT_LIST_HEAD(&flush_queue);
3004 spin_lock_bh(&block->queue_lock);
3005 rc = 0;
3006restart:
3007 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
3008 /* if this request currently owned by a dasd_device cancel it */
3009 if (cqr->status >= DASD_CQR_QUEUED)
3010 rc = dasd_cancel_req(cqr);
3011 if (rc < 0)
3012 break;
3013 /* Rechain request (including erp chain) so it won't be
3014 * touched by the dasd_block_tasklet anymore.
3015 * Replace the callback so we notice when the request
3016 * is returned from the dasd_device layer.
3017 */
3018 cqr->callback = _dasd_wake_block_flush_cb;
3019 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
3020 list_move_tail(&cqr->blocklist, &flush_queue);
3021 if (i > 1)
3022 /* moved more than one request - need to restart */
3023 goto restart;
3024 }
3025 spin_unlock_bh(&block->queue_lock);
3026 /* Now call the callback function of flushed requests */
3027restart_cb:
3028 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
3029 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
3030 /* Process finished ERP request. */
3031 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003032 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003033 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003034 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003035 /* restart list_for_xx loop since dasd_process_erp
3036 * might remove multiple elements */
3037 goto restart_cb;
3038 }
3039 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003040 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01003041 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003042 list_del_init(&cqr->blocklist);
3043 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003044 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return rc;
3047}
3048
3049/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003050 * Schedules a call to dasd_tasklet over the device tasklet.
3051 */
3052void dasd_schedule_block_bh(struct dasd_block *block)
3053{
3054 /* Protect against rescheduling. */
3055 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3056 return;
3057 /* life cycle of block is bound to it's base device */
3058 dasd_get_device(block->base);
3059 tasklet_hi_schedule(&block->tasklet);
3060}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003061EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003062
3063
3064/*
3065 * SECTION: external block device operations
3066 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
3068
3069/*
3070 * Dasd request queue function. Called from ll_rw_blk.c
3071 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003072static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003074 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003076 block = queue->queuedata;
3077 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003079 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003081 __dasd_block_start_head(block);
3082 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
3085/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00003086 * Block timeout callback, called from the block layer
3087 *
3088 * request_queue lock is held on entry.
3089 *
3090 * Return values:
3091 * BLK_EH_RESET_TIMER if the request should be left running
3092 * BLK_EH_NOT_HANDLED if the request is handled or terminated
3093 * by the driver.
3094 */
3095enum blk_eh_timer_return dasd_times_out(struct request *req)
3096{
3097 struct dasd_ccw_req *cqr = req->completion_data;
3098 struct dasd_block *block = req->q->queuedata;
3099 struct dasd_device *device;
3100 int rc = 0;
3101
3102 if (!cqr)
3103 return BLK_EH_NOT_HANDLED;
3104
3105 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00003106 if (!device->blk_timeout)
3107 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003108 DBF_DEV_EVENT(DBF_WARNING, device,
3109 " dasd_times_out cqr %p status %x",
3110 cqr, cqr->status);
3111
3112 spin_lock(&block->queue_lock);
3113 spin_lock(get_ccwdev_lock(device->cdev));
3114 cqr->retries = -1;
3115 cqr->intrc = -ETIMEDOUT;
3116 if (cqr->status >= DASD_CQR_QUEUED) {
3117 spin_unlock(get_ccwdev_lock(device->cdev));
3118 rc = dasd_cancel_req(cqr);
3119 } else if (cqr->status == DASD_CQR_FILLED ||
3120 cqr->status == DASD_CQR_NEED_ERP) {
3121 cqr->status = DASD_CQR_TERMINATED;
3122 spin_unlock(get_ccwdev_lock(device->cdev));
3123 } else if (cqr->status == DASD_CQR_IN_ERP) {
3124 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3125
3126 list_for_each_entry_safe(searchcqr, nextcqr,
3127 &block->ccw_queue, blocklist) {
3128 tmpcqr = searchcqr;
3129 while (tmpcqr->refers)
3130 tmpcqr = tmpcqr->refers;
3131 if (tmpcqr != cqr)
3132 continue;
3133 /* searchcqr is an ERP request for cqr */
3134 searchcqr->retries = -1;
3135 searchcqr->intrc = -ETIMEDOUT;
3136 if (searchcqr->status >= DASD_CQR_QUEUED) {
3137 spin_unlock(get_ccwdev_lock(device->cdev));
3138 rc = dasd_cancel_req(searchcqr);
3139 spin_lock(get_ccwdev_lock(device->cdev));
3140 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3141 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3142 searchcqr->status = DASD_CQR_TERMINATED;
3143 rc = 0;
3144 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3145 /*
3146 * Shouldn't happen; most recent ERP
3147 * request is at the front of queue
3148 */
3149 continue;
3150 }
3151 break;
3152 }
3153 spin_unlock(get_ccwdev_lock(device->cdev));
3154 }
3155 dasd_schedule_block_bh(block);
3156 spin_unlock(&block->queue_lock);
3157
3158 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3159}
3160
3161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 * Allocate and initialize request queue and default I/O scheduler.
3163 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003164static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003166 block->request_queue = blk_init_queue(do_dasd_request,
3167 &block->request_queue_lock);
3168 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return -ENOMEM;
3170
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003171 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01003173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
3176/*
3177 * Allocate and initialize request queue.
3178 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003179static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Jan Höppner62ba6f82016-06-30 13:18:16 +02003181 struct request_queue *q = block->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 int max;
3183
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003184 if (block->base->features & DASD_FEATURE_USERAW) {
3185 /*
3186 * the max_blocks value for raw_track access is 256
3187 * it is higher than the native ECKD value because we
3188 * only need one ccw per track
3189 * so the max_hw_sectors are
3190 * 2048 x 512B = 1024kB = 16 tracks
3191 */
3192 max = 2048;
3193 } else {
3194 max = block->base->discipline->max_blocks << block->s2b_shift;
3195 }
Jan Höppner62ba6f82016-06-30 13:18:16 +02003196 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
3197 q->limits.max_dev_sectors = max;
3198 blk_queue_logical_block_size(q, block->bp_block);
3199 blk_queue_max_hw_sectors(q, max);
3200 blk_queue_max_segments(q, USHRT_MAX);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003201 /* with page sized segments we can translate each segement into
3202 * one idaw/tidaw
3203 */
Jan Höppner62ba6f82016-06-30 13:18:16 +02003204 blk_queue_max_segment_size(q, PAGE_SIZE);
3205 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
3208/*
3209 * Deactivate and free request queue.
3210 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003211static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003213 if (block->request_queue) {
3214 blk_cleanup_queue(block->request_queue);
3215 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217}
3218
3219/*
3220 * Flush request on the request queue.
3221 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003222static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 struct request *req;
3225
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003226 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003228
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003229 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003230 while ((req = blk_fetch_request(block->request_queue)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003231 __blk_end_request_all(req, BLK_STS_IOERR);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003232 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Al Viro57a7c0b2008-03-02 10:36:08 -05003235static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003237 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 int rc;
3239
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003240 base = dasd_device_from_gendisk(bdev->bd_disk);
3241 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003242 return -ENODEV;
3243
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003244 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003245 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 rc = -ENODEV;
3247 goto unlock;
3248 }
3249
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003250 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 rc = -EINVAL;
3252 goto unlock;
3253 }
3254
3255 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003256 dev_info(&base->cdev->dev,
3257 "Accessing the DASD failed because it is in "
3258 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 rc = -EPERM;
3260 goto out;
3261 }
3262
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003263 if (base->state <= DASD_STATE_BASIC) {
3264 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 " Cannot open unrecognized device");
3266 rc = -ENODEV;
3267 goto out;
3268 }
3269
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003270 if ((mode & FMODE_WRITE) &&
3271 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3272 (base->features & DASD_FEATURE_READONLY))) {
3273 rc = -EROFS;
3274 goto out;
3275 }
3276
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003277 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279
3280out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003281 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003283 atomic_dec(&base->block->open_count);
3284 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return rc;
3286}
3287
Al Virodb2a1442013-05-05 21:52:57 -04003288static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Al Virodb2a1442013-05-05 21:52:57 -04003290 struct dasd_device *base = dasd_device_from_gendisk(disk);
3291 if (base) {
3292 atomic_dec(&base->block->open_count);
3293 module_put(base->discipline->owner);
3294 dasd_put_device(base);
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003298/*
3299 * Return disk geometry.
3300 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003301static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003302{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003303 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003304
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003305 base = dasd_device_from_gendisk(bdev->bd_disk);
3306 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003307 return -ENODEV;
3308
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003309 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003310 !base->discipline->fill_geometry) {
3311 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003312 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003313 }
3314 base->discipline->fill_geometry(base->block, geo);
3315 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3316 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003317 return 0;
3318}
3319
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003320const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321dasd_device_operations = {
3322 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003323 .open = dasd_open,
3324 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003325 .ioctl = dasd_ioctl,
3326 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003327 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328};
3329
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003330/*******************************************************************************
3331 * end of block device operations
3332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334static void
3335dasd_exit(void)
3336{
3337#ifdef CONFIG_PROC_FS
3338 dasd_proc_exit();
3339#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003340 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003341 if (dasd_page_cache != NULL) {
3342 kmem_cache_destroy(dasd_page_cache);
3343 dasd_page_cache = NULL;
3344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 dasd_gendisk_exit();
3346 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 if (dasd_debug_area != NULL) {
3348 debug_unregister(dasd_debug_area);
3349 dasd_debug_area = NULL;
3350 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003351 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354/*
3355 * SECTION: common functions for ccw_driver use
3356 */
3357
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003358/*
3359 * Is the device read-only?
3360 * Note that this function does not report the setting of the
3361 * readonly device attribute, but how it is configured in z/VM.
3362 */
3363int dasd_device_is_ro(struct dasd_device *device)
3364{
3365 struct ccw_dev_id dev_id;
3366 struct diag210 diag_data;
3367 int rc;
3368
3369 if (!MACHINE_IS_VM)
3370 return 0;
3371 ccw_device_get_id(device->cdev, &dev_id);
3372 memset(&diag_data, 0, sizeof(diag_data));
3373 diag_data.vrdcdvno = dev_id.devno;
3374 diag_data.vrdclen = sizeof(diag_data);
3375 rc = diag210(&diag_data);
3376 if (rc == 0 || rc == 2) {
3377 return diag_data.vrdcvfla & 0x80;
3378 } else {
3379 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3380 dev_id.devno, rc);
3381 return 0;
3382 }
3383}
3384EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3385
Cornelia Huckf3445a12009-04-14 15:36:23 +02003386static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3387{
3388 struct ccw_device *cdev = data;
3389 int ret;
3390
3391 ret = ccw_device_set_online(cdev);
3392 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003393 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3394 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003395}
3396
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003397/*
3398 * Initial attempt at a probe function. this can be simplified once
3399 * the other detection code is gone.
3400 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003401int dasd_generic_probe(struct ccw_device *cdev,
3402 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 int ret;
3405
3406 ret = dasd_add_sysfs_files(cdev);
3407 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003408 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3409 "dasd_generic_probe: could not add "
3410 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
Horst Hummel40545572006-06-29 15:08:18 +02003413 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Horst Hummel40545572006-06-29 15:08:18 +02003415 /*
3416 * Automatically online either all dasd devices (dasd_autodetect)
3417 * or all devices specified with dasd= parameters during
3418 * initial probe.
3419 */
3420 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003421 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003422 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003425EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Stefan Haberlandc020d722016-09-20 10:42:38 +02003427void dasd_generic_free_discipline(struct dasd_device *device)
3428{
3429 /* Forget the discipline information. */
3430 if (device->discipline) {
3431 if (device->discipline->uncheck_device)
3432 device->discipline->uncheck_device(device);
3433 module_put(device->discipline->owner);
3434 device->discipline = NULL;
3435 }
3436 if (device->base_discipline) {
3437 module_put(device->base_discipline->owner);
3438 device->base_discipline = NULL;
3439 }
3440}
3441EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3442
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003443/*
3444 * This will one day be called from a global not_oper handler.
3445 * It is also used by driver_unregister during module unload.
3446 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003447void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003450 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Horst Hummel59afda72005-05-16 21:53:39 -07003452 cdev->handler = NULL;
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003455 if (IS_ERR(device)) {
3456 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003458 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003459 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3460 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 /* Already doing offline processing */
3462 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003463 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return;
3465 }
3466 /*
3467 * This device is removed unconditionally. Set offline
3468 * flag to prevent dasd_open from opening it while it is
3469 * no quite down yet.
3470 */
3471 dasd_set_target_state(device, DASD_STATE_NEW);
3472 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003473 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003475 /*
3476 * life cycle of block is bound to device, so delete it after
3477 * device was safely removed
3478 */
3479 if (block)
3480 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003481
3482 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003484EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003486/*
3487 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003489 * or the user has started activation through sysfs.
3490 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003491int dasd_generic_set_online(struct ccw_device *cdev,
3492 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003494 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003496 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003497
Horst Hummel40545572006-06-29 15:08:18 +02003498 /* first online clears initial online feature flag */
3499 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 device = dasd_create_device(cdev);
3501 if (IS_ERR(device))
3502 return PTR_ERR(device);
3503
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003504 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003505 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003507 /* Try to load the required module. */
3508 rc = request_module(DASD_DIAG_MOD);
3509 if (rc) {
3510 pr_warn("%s Setting the DASD online failed "
3511 "because the required module %s "
3512 "could not be loaded (rc=%d)\n",
3513 dev_name(&cdev->dev), DASD_DIAG_MOD,
3514 rc);
3515 dasd_delete_device(device);
3516 return -ENODEV;
3517 }
3518 }
3519 /* Module init could have failed, so check again here after
3520 * request_module(). */
3521 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003522 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3523 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 dasd_delete_device(device);
3525 return -ENODEV;
3526 }
3527 discipline = dasd_diag_discipline_pointer;
3528 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003529 if (!try_module_get(base_discipline->owner)) {
3530 dasd_delete_device(device);
3531 return -EINVAL;
3532 }
3533 if (!try_module_get(discipline->owner)) {
3534 module_put(base_discipline->owner);
3535 dasd_delete_device(device);
3536 return -EINVAL;
3537 }
3538 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 device->discipline = discipline;
3540
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003541 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 rc = discipline->check_device(device);
3543 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003544 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3545 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003546 module_put(discipline->owner);
3547 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 dasd_delete_device(device);
3549 return rc;
3550 }
3551
3552 dasd_set_target_state(device, DASD_STATE_ONLINE);
3553 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003554 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3555 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 rc = -ENODEV;
3557 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003558 if (device->block)
3559 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 dasd_delete_device(device);
3561 } else
3562 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003563 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003564
3565 wait_event(dasd_init_waitq, _wait_for_device(device));
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 return rc;
3569}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003570EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003572int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
3574 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003575 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003576 int max_count, open_count, rc;
Jan Höppner0f57c972016-10-18 17:54:49 +02003577 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003579 rc = 0;
Jan Höppner0f57c972016-10-18 17:54:49 +02003580 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3581 device = dasd_device_from_cdev_locked(cdev);
3582 if (IS_ERR(device)) {
3583 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return PTR_ERR(device);
Jan Höppner0f57c972016-10-18 17:54:49 +02003585 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 /*
3588 * We must make sure that this device is currently not in use.
3589 * The open_count is increased for every opener, that includes
3590 * the blkdev_get in dasd_scan_partitions. We are only interested
3591 * in the other openers.
3592 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003593 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003594 max_count = device->block->bdev ? 0 : -1;
3595 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003596 if (open_count > max_count) {
3597 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003598 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3599 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003600 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003601 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3602 dev_name(&cdev->dev));
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003603 rc = -EBUSY;
3604 goto out_err;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003607
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003608 /*
3609 * Test if the offline processing is already running and exit if so.
3610 * If a safe offline is being processed this could only be a normal
3611 * offline that should be able to overtake the safe offline and
3612 * cancel any I/O we do not want to wait for any longer
3613 */
3614 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3615 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3616 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3617 &device->flags);
3618 } else {
3619 rc = -EBUSY;
3620 goto out_err;
3621 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003622 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003623 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003624
3625 /*
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003626 * if safe_offline is called set safe_offline_running flag and
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003627 * clear safe_offline so that a call to normal offline
3628 * can overrun safe_offline processing
3629 */
3630 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3631 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003632 /* need to unlock here to wait for outstanding I/O */
3633 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003634 /*
3635 * If we want to set the device safe offline all IO operations
3636 * should be finished before continuing the offline process
3637 * so sync bdev first and then wait for our queues to become
3638 * empty
3639 */
Stefan Haberlandca732e12017-01-25 16:56:41 +01003640 if (device->block) {
3641 rc = fsync_bdev(device->block->bdev);
3642 if (rc != 0)
3643 goto interrupted;
3644 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003645 dasd_schedule_device_bh(device);
3646 rc = wait_event_interruptible(shutdown_waitq,
3647 _wait_for_empty_queues(device));
3648 if (rc != 0)
3649 goto interrupted;
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003650
3651 /*
3652 * check if a normal offline process overtook the offline
3653 * processing in this case simply do nothing beside returning
3654 * that we got interrupted
3655 * otherwise mark safe offline as not running any longer and
3656 * continue with normal offline
3657 */
3658 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3659 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3660 rc = -ERESTARTSYS;
3661 goto out_err;
3662 }
3663 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003664 }
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003665 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 dasd_set_target_state(device, DASD_STATE_NEW);
3668 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003669 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003671 /*
3672 * life cycle of block is bound to device, so delete it after
3673 * device was safely removed
3674 */
3675 if (block)
3676 dasd_free_block(block);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003679
3680interrupted:
3681 /* interrupted by signal */
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003682 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003683 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3684 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003685out_err:
Jan Höppner0f57c972016-10-18 17:54:49 +02003686 dasd_put_device(device);
3687 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003688 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003690EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003692int dasd_generic_last_path_gone(struct dasd_device *device)
3693{
3694 struct dasd_ccw_req *cqr;
3695
3696 dev_warn(&device->cdev->dev, "No operational channel path is left "
3697 "for the device\n");
3698 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3699 /* First of all call extended error reporting. */
3700 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3701
3702 if (device->state < DASD_STATE_BASIC)
3703 return 0;
3704 /* Device is active. We want to keep it. */
3705 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3706 if ((cqr->status == DASD_CQR_IN_IO) ||
3707 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3708 cqr->status = DASD_CQR_QUEUED;
3709 cqr->retries++;
3710 }
3711 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3712 dasd_device_clear_timer(device);
3713 dasd_schedule_device_bh(device);
3714 return 1;
3715}
3716EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3717
3718int dasd_generic_path_operational(struct dasd_device *device)
3719{
3720 dev_info(&device->cdev->dev, "A channel path to the device has become "
3721 "operational\n");
3722 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3723 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3724 if (device->stopped & DASD_UNRESUMED_PM) {
3725 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3726 dasd_restore_device(device);
3727 return 1;
3728 }
3729 dasd_schedule_device_bh(device);
3730 if (device->block)
3731 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003732
3733 if (!device->stopped)
3734 wake_up(&generic_waitq);
3735
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003736 return 1;
3737}
3738EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3739
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003740int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 int ret;
3744
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003745 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 if (IS_ERR(device))
3747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 ret = 0;
3749 switch (event) {
3750 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003751 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 case CIO_NO_PATH:
Stefan Haberlandc9346152016-08-08 15:53:54 +02003753 dasd_path_no_path(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003754 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 break;
3756 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 ret = 1;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003758 if (dasd_path_get_opm(device))
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003759 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 break;
3761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 dasd_put_device(device);
3763 return ret;
3764}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003765EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003767void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3768{
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003769 struct dasd_device *device;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003770 int chp, oldopm, hpfpm, ifccpm;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003771
3772 device = dasd_device_from_cdev_locked(cdev);
3773 if (IS_ERR(device))
3774 return;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003775
3776 oldopm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003777 for (chp = 0; chp < 8; chp++) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003778 if (path_event[chp] & PE_PATH_GONE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003779 dasd_path_notoper(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003780 }
3781 if (path_event[chp] & PE_PATH_AVAILABLE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003782 dasd_path_available(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003783 dasd_schedule_device_bh(device);
3784 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003785 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003786 if (!dasd_path_is_operational(device, chp) &&
3787 !dasd_path_need_verify(device, chp)) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003788 /*
3789 * we can not establish a pathgroup on an
3790 * unavailable path, so trigger a path
3791 * verification first
3792 */
Stefan Haberlandc9346152016-08-08 15:53:54 +02003793 dasd_path_available(device, chp);
3794 dasd_schedule_device_bh(device);
Stefan Haberland12d7b102012-09-11 15:10:58 +02003795 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003796 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3797 "Pathgroup re-established\n");
3798 if (device->discipline->kick_validate)
3799 device->discipline->kick_validate(device);
3800 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003801 }
Stefan Haberlanda521b042016-08-08 15:56:54 +02003802 hpfpm = dasd_path_get_hpfpm(device);
3803 ifccpm = dasd_path_get_ifccpm(device);
3804 if (!dasd_path_get_opm(device) && hpfpm) {
3805 /*
3806 * device has no operational paths but at least one path is
3807 * disabled due to HPF errors
3808 * disable HPF at all and use the path(s) again
3809 */
3810 if (device->discipline->disable_hpf)
3811 device->discipline->disable_hpf(device);
3812 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3813 dasd_path_set_tbvpm(device, hpfpm);
3814 dasd_schedule_device_bh(device);
3815 dasd_schedule_requeue(device);
3816 } else if (!dasd_path_get_opm(device) && ifccpm) {
3817 /*
3818 * device has no operational paths but at least one path is
3819 * disabled due to IFCC errors
3820 * trigger path verification on paths with IFCC errors
3821 */
3822 dasd_path_set_tbvpm(device, ifccpm);
3823 dasd_schedule_device_bh(device);
3824 }
3825 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003826 dev_warn(&device->cdev->dev,
3827 "No verified channel paths remain for the device\n");
3828 DBF_DEV_EVENT(DBF_WARNING, device,
3829 "%s", "last verified path gone");
3830 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3831 dasd_device_set_stop_bits(device,
3832 DASD_STOPPED_DC_WAIT);
3833 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003834 dasd_put_device(device);
3835}
3836EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3837
3838int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3839{
Stefan Haberlandc9346152016-08-08 15:53:54 +02003840 if (!dasd_path_get_opm(device) && lpm) {
3841 dasd_path_set_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003842 dasd_generic_path_operational(device);
3843 } else
Stefan Haberlandc9346152016-08-08 15:53:54 +02003844 dasd_path_add_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003845 return 0;
3846}
3847EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3848
Stefan Haberlanda521b042016-08-08 15:56:54 +02003849/*
3850 * clear active requests and requeue them to block layer if possible
3851 */
3852static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3853{
3854 struct list_head requeue_queue;
3855 struct dasd_ccw_req *cqr, *n;
3856 struct dasd_ccw_req *refers;
3857 int rc;
3858
3859 INIT_LIST_HEAD(&requeue_queue);
3860 spin_lock_irq(get_ccwdev_lock(device->cdev));
3861 rc = 0;
3862 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3863 /* Check status and move request to flush_queue */
3864 if (cqr->status == DASD_CQR_IN_IO) {
3865 rc = device->discipline->term_IO(cqr);
3866 if (rc) {
3867 /* unable to terminate requeust */
3868 dev_err(&device->cdev->dev,
3869 "Unable to terminate request %p "
3870 "on suspend\n", cqr);
3871 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3872 dasd_put_device(device);
3873 return rc;
3874 }
3875 }
3876 list_move_tail(&cqr->devlist, &requeue_queue);
3877 }
3878 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3879
3880 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3881 wait_event(dasd_flush_wq,
3882 (cqr->status != DASD_CQR_CLEAR_PENDING));
3883
3884 /* mark sleepon requests as ended */
3885 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
3886 cqr->callback_data = DASD_SLEEPON_END_TAG;
3887
3888 /* remove requests from device and block queue */
3889 list_del_init(&cqr->devlist);
3890 while (cqr->refers != NULL) {
3891 refers = cqr->refers;
3892 /* remove the request from the block queue */
3893 list_del(&cqr->blocklist);
3894 /* free the finished erp request */
3895 dasd_free_erp_request(cqr, cqr->memdev);
3896 cqr = refers;
3897 }
3898
3899 /*
3900 * requeue requests to blocklayer will only work
3901 * for block device requests
3902 */
3903 if (_dasd_requeue_request(cqr))
3904 continue;
3905
3906 if (cqr->block)
3907 list_del_init(&cqr->blocklist);
3908 cqr->block->base->discipline->free_cp(
3909 cqr, (struct request *) cqr->callback_data);
3910 }
3911
3912 /*
3913 * if requests remain then they are internal request
3914 * and go back to the device queue
3915 */
3916 if (!list_empty(&requeue_queue)) {
3917 /* move freeze_queue to start of the ccw_queue */
3918 spin_lock_irq(get_ccwdev_lock(device->cdev));
3919 list_splice_tail(&requeue_queue, &device->ccw_queue);
3920 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3921 }
3922 /* wake up generic waitqueue for eventually ended sleepon requests */
3923 wake_up(&generic_waitq);
3924 return rc;
3925}
3926
3927static void do_requeue_requests(struct work_struct *work)
3928{
3929 struct dasd_device *device = container_of(work, struct dasd_device,
3930 requeue_requests);
3931 dasd_generic_requeue_all_requests(device);
3932 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
3933 if (device->block)
3934 dasd_schedule_block_bh(device->block);
3935 dasd_put_device(device);
3936}
3937
3938void dasd_schedule_requeue(struct dasd_device *device)
3939{
3940 dasd_get_device(device);
3941 /* queue call to dasd_reload_device to the kernel event daemon. */
3942 if (!schedule_work(&device->requeue_requests))
3943 dasd_put_device(device);
3944}
3945EXPORT_SYMBOL(dasd_schedule_requeue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003946
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003947int dasd_generic_pm_freeze(struct ccw_device *cdev)
3948{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003949 struct dasd_device *device = dasd_device_from_cdev(cdev);
3950
3951 if (IS_ERR(device))
3952 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003953
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003954 /* mark device as suspended */
3955 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3956
Stefan Haberland6f272b92011-01-05 12:48:05 +01003957 if (device->discipline->freeze)
Sebastian Ott4bca6982017-06-26 19:26:55 +02003958 device->discipline->freeze(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003959
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003960 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003961 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003962
Stefan Haberlanda521b042016-08-08 15:56:54 +02003963 return dasd_generic_requeue_all_requests(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003964}
3965EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3966
3967int dasd_generic_restore_device(struct ccw_device *cdev)
3968{
3969 struct dasd_device *device = dasd_device_from_cdev(cdev);
3970 int rc = 0;
3971
3972 if (IS_ERR(device))
3973 return PTR_ERR(device);
3974
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003975 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003976 dasd_device_remove_stop_bits(device,
3977 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003978
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003979 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003980
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003981 /*
3982 * call discipline restore function
3983 * if device is stopped do nothing e.g. for disconnected devices
3984 */
3985 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003986 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003987 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003988 /*
3989 * if the resume failed for the DASD we put it in
3990 * an UNRESUMED stop state
3991 */
3992 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003993
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003994 if (device->block)
3995 dasd_schedule_block_bh(device->block);
3996
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003997 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003998 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003999 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02004000}
4001EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
4002
Heiko Carstens763968e2007-05-10 15:45:46 +02004003static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
4004 void *rdc_buffer,
4005 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02004006 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02004007{
4008 struct dasd_ccw_req *cqr;
4009 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02004010 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02004011
4012 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
4013
4014 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004015 /* internal error 13 - Allocating the RDC request failed*/
4016 dev_err(&device->cdev->dev,
4017 "An error occurred in the DASD device driver, "
4018 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02004019 return cqr;
4020 }
4021
4022 ccw = cqr->cpaddr;
4023 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02004024 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
4025 idaw = (unsigned long *) (cqr->data);
4026 ccw->cda = (__u32)(addr_t) idaw;
4027 ccw->flags = CCW_FLAG_IDA;
4028 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
4029 } else {
4030 ccw->cda = (__u32)(addr_t) rdc_buffer;
4031 ccw->flags = 0;
4032 }
Cornelia Huck17283b52007-05-04 18:47:51 +02004033
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02004034 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004035 cqr->startdev = device;
4036 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02004037 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004038 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01004039 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02004040 cqr->status = DASD_CQR_FILLED;
4041 return cqr;
4042}
4043
4044
Stefan Haberland68b781f2009-09-11 10:28:29 +02004045int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02004046 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02004047{
4048 int ret;
4049 struct dasd_ccw_req *cqr;
4050
Sebastian Ott92636b12009-06-12 10:26:37 +02004051 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02004052 magic);
4053 if (IS_ERR(cqr))
4054 return PTR_ERR(cqr);
4055
4056 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004057 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02004058 return ret;
4059}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02004060EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004061
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01004062/*
4063 * In command mode and transport mode we need to look for sense
4064 * data in different places. The sense data itself is allways
4065 * an array of 32 bytes, so we can unify the sense data access
4066 * for both modes.
4067 */
4068char *dasd_get_sense(struct irb *irb)
4069{
4070 struct tsb *tsb = NULL;
4071 char *sense = NULL;
4072
4073 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4074 if (irb->scsw.tm.tcw)
4075 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4076 irb->scsw.tm.tcw);
4077 if (tsb && tsb->length == 64 && tsb->flags)
4078 switch (tsb->flags & 0x07) {
4079 case 1: /* tsa_iostat */
4080 sense = tsb->tsa.iostat.sense;
4081 break;
4082 case 2: /* tsa_ddpc */
4083 sense = tsb->tsa.ddpc.sense;
4084 break;
4085 default:
4086 /* currently we don't use interrogate data */
4087 break;
4088 }
4089 } else if (irb->esw.esw0.erw.cons) {
4090 sense = irb->ecw;
4091 }
4092 return sense;
4093}
4094EXPORT_SYMBOL_GPL(dasd_get_sense);
4095
Stefan Haberland4679e892012-06-19 17:30:12 +02004096void dasd_generic_shutdown(struct ccw_device *cdev)
4097{
4098 struct dasd_device *device;
4099
4100 device = dasd_device_from_cdev(cdev);
4101 if (IS_ERR(device))
4102 return;
4103
4104 if (device->block)
4105 dasd_schedule_block_bh(device->block);
4106
4107 dasd_schedule_device_bh(device);
4108
4109 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4110}
4111EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4112
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004113static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 int rc;
4116
4117 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02004118 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02004119 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02004120 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01004123 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 if (dasd_debug_area == NULL) {
4125 rc = -ENOMEM;
4126 goto failed;
4127 }
4128 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02004129 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
4131 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4132
4133 dasd_diag_discipline_pointer = NULL;
4134
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02004135 dasd_statistics_createroot();
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 rc = dasd_devmap_init();
4138 if (rc)
4139 goto failed;
4140 rc = dasd_gendisk_init();
4141 if (rc)
4142 goto failed;
4143 rc = dasd_parse();
4144 if (rc)
4145 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004146 rc = dasd_eer_init();
4147 if (rc)
4148 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149#ifdef CONFIG_PROC_FS
4150 rc = dasd_proc_init();
4151 if (rc)
4152 goto failed;
4153#endif
4154
4155 return 0;
4156failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004157 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 dasd_exit();
4159 return rc;
4160}
4161
4162module_init(dasd_init);
4163module_exit(dasd_exit);