blob: 329db997ee6680fc931897b04a01495209df66d2 [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
41/*
42 * SECTION: exported variables of dasd.c
43 */
44debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020045EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020046static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020048EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010049void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
52MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020053 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_LICENSE("GPL");
56
57/*
58 * SECTION: prototypes for static functions of dasd.c
59 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010060static int dasd_alloc_queue(struct dasd_block *);
61static void dasd_setup_queue(struct dasd_block *);
62static void dasd_free_queue(struct dasd_block *);
63static void dasd_flush_request_queue(struct dasd_block *);
64static int dasd_flush_block_queue(struct dasd_block *);
65static void dasd_device_tasklet(struct dasd_device *);
66static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000067static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020068static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020069static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010070static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010071static void dasd_device_timeout(unsigned long);
72static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010073static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020074static void dasd_profile_init(struct dasd_profile *, struct dentry *);
75static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * SECTION: Operations on the device structure.
79 */
80static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020081static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020082static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020083static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Allocate memory for a new device structure.
87 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010088struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct dasd_device *device;
91
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010092 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
93 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* Get two pages for normal block device operations. */
97 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010098 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kfree(device);
100 return ERR_PTR(-ENOMEM);
101 }
102 /* Get one page for error recovery. */
103 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100104 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 free_pages((unsigned long) device->ccw_mem, 1);
106 kfree(device);
107 return ERR_PTR(-ENOMEM);
108 }
109
110 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
111 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
112 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100113 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200114 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100115 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 (unsigned long) device);
117 INIT_LIST_HEAD(&device->ccw_queue);
118 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100119 device->timer.function = dasd_device_timeout;
120 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000121 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200122 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200123 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 device->state = DASD_STATE_NEW;
125 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100126 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200127 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return device;
129}
130
131/*
132 * Free memory of a device structure.
133 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100134void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800136 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 free_page((unsigned long) device->erp_mem);
138 free_pages((unsigned long) device->ccw_mem, 1);
139 kfree(device);
140}
141
142/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100143 * Allocate memory for a new device structure.
144 */
145struct dasd_block *dasd_alloc_block(void)
146{
147 struct dasd_block *block;
148
149 block = kzalloc(sizeof(*block), GFP_ATOMIC);
150 if (!block)
151 return ERR_PTR(-ENOMEM);
152 /* open_count = 0 means device online but not in use */
153 atomic_set(&block->open_count, -1);
154
155 spin_lock_init(&block->request_queue_lock);
156 atomic_set(&block->tasklet_scheduled, 0);
157 tasklet_init(&block->tasklet,
158 (void (*)(unsigned long)) dasd_block_tasklet,
159 (unsigned long) block);
160 INIT_LIST_HEAD(&block->ccw_queue);
161 spin_lock_init(&block->queue_lock);
162 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100163 block->timer.function = dasd_block_timeout;
164 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200165 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100166
167 return block;
168}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200169EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100170
171/*
172 * Free memory of a device structure.
173 */
174void dasd_free_block(struct dasd_block *block)
175{
176 kfree(block);
177}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200178EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Make a new device known to the system.
182 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100183static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int rc;
186
187 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200188 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * keep the reference count > 0.
190 */
191 dasd_get_device(device);
192
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100193 if (device->block) {
194 rc = dasd_alloc_queue(device->block);
195 if (rc) {
196 dasd_put_device(device);
197 return rc;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 device->state = DASD_STATE_KNOWN;
201 return 0;
202}
203
204/*
205 * Let the system forget about a device.
206 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100207static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800209 /* Disable extended error reporting for this device. */
210 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 if (device->discipline) {
213 if (device->discipline->uncheck_device)
214 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800215 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800218 if (device->base_discipline)
219 module_put(device->base_discipline->owner);
220 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 device->state = DASD_STATE_NEW;
222
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100223 if (device->block)
224 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Give up reference we took in dasd_state_new_to_known. */
227 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200231static struct dentry *dasd_debugfs_setup(const char *name,
232 struct dentry *base_dentry)
233{
234 struct dentry *pde;
235
236 if (!base_dentry)
237 return NULL;
238 pde = debugfs_create_dir(name, base_dentry);
239 if (!pde || IS_ERR(pde))
240 return NULL;
241 return pde;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * Request the irq line for the device.
246 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100247static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200249 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200250 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 if (block) {
254 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100255 if (rc)
256 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200257 block->debugfs_dentry =
258 dasd_debugfs_setup(block->gdp->disk_name,
259 dasd_debugfs_root_entry);
260 dasd_profile_init(&block->profile, block->debugfs_dentry);
261 if (dasd_global_profile_level == DASD_PROFILE_ON)
262 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100263 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200264 device->debugfs_dentry =
265 dasd_debugfs_setup(dev_name(&device->cdev->dev),
266 dasd_debugfs_root_entry);
267 dasd_profile_init(&device->profile, device->debugfs_dentry);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100270 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100271 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200273 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
275
276 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200277
278 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * Release the irq line for the device. Terminate any running i/o.
283 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Horst Hummel8f617012006-08-30 14:33:33 +0200286 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200287
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200288 if (device->discipline->basic_to_known) {
289 rc = device->discipline->basic_to_known(device);
290 if (rc)
291 return rc;
292 }
293
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200295 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200296 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100297 dasd_gendisk_free(device->block);
298 dasd_block_clear_timer(device->block);
299 }
300 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200301 if (rc)
302 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100303 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200304 dasd_profile_exit(&device->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200305 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
307 if (device->debug_area != NULL) {
308 debug_unregister(device->debug_area);
309 device->debug_area = NULL;
310 }
311 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * Do the initial analysis. The do_analysis function may return
317 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
318 * until the discipline decides to continue the startup sequence
319 * by calling the function dasd_change_state. The eckd disciplines
320 * uses this to start a ccw that detects the format. The completion
321 * interrupt for this detection ccw uses the kernel event daemon to
322 * trigger the call to dasd_change_state. All this is done in the
323 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800324 * After the analysis ccw is done (do_analysis returned 0) the block
325 * device is setup.
326 * In case the analysis returns an error, the device setup is stopped
327 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100335 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800336 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100337 if (block) {
338 if (block->base->discipline->do_analysis != NULL)
339 rc = block->base->discipline->do_analysis(block);
340 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200341 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100342 device->state = DASD_STATE_UNFMT;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200343 goto out;
344 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 return rc;
346 }
347 dasd_setup_queue(block);
348 set_capacity(block->gdp,
349 block->blocks << block->s2b_shift);
350 device->state = DASD_STATE_READY;
351 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200352 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200354 return rc;
355 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100356 } else {
357 device->state = DASD_STATE_READY;
358 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200359out:
360 if (device->discipline->basic_to_ready)
361 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100365static inline
366int _wait_for_empty_queues(struct dasd_device *device)
367{
368 if (device->block)
369 return list_empty(&device->ccw_queue) &&
370 list_empty(&device->block->ccw_queue);
371 else
372 return list_empty(&device->ccw_queue);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Remove device from block device layer. Destroy dirty buffers.
377 * Forget format information. Check if the target level is basic
378 * and if it is create fake disk for formatting.
379 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Horst Hummel8f617012006-08-30 14:33:33 +0200382 int rc;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100385 if (device->block) {
386 struct dasd_block *block = device->block;
387 rc = dasd_flush_block_queue(block);
388 if (rc) {
389 device->state = DASD_STATE_READY;
390 return rc;
391 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100392 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100393 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100394 block->blocks = 0;
395 block->bp_block = 0;
396 block->s2b_shift = 0;
397 }
Horst Hummel8f617012006-08-30 14:33:33 +0200398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
Horst Hummel90f00942006-03-07 21:55:39 -0800402 * Back to basic.
403 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100404static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800405{
406 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200407 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800408}
409
410/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * Make the device online and schedule the bottom half to start
412 * the requeueing of requests from the linux request queue to the
413 * ccw queue.
414 */
Horst Hummel8f617012006-08-30 14:33:33 +0200415static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416dasd_state_ready_to_online(struct dasd_device * device)
417{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100418 struct gendisk *disk;
419 struct disk_part_iter piter;
420 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100423 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100424 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100425 if ((device->features & DASD_FEATURE_USERAW)) {
426 disk = device->block->gdp;
427 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
428 return 0;
429 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100430 disk = device->block->bdev->bd_disk;
431 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
432 while ((part = disk_part_iter_next(&piter)))
433 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
434 disk_part_iter_exit(&piter);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
439/*
440 * Stop the requeueing of requests again.
441 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100442static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100445 struct gendisk *disk;
446 struct disk_part_iter piter;
447 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100448
449 if (device->discipline->online_to_ready) {
450 rc = device->discipline->online_to_ready(device);
451 if (rc)
452 return rc;
453 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100456 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100457 disk = device->block->bdev->bd_disk;
458 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
459 while ((part = disk_part_iter_next(&piter)))
460 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
461 disk_part_iter_exit(&piter);
462 }
Horst Hummel8f617012006-08-30 14:33:33 +0200463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * Device startup state changes.
468 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100469static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int rc;
472
473 rc = 0;
474 if (device->state == DASD_STATE_NEW &&
475 device->target >= DASD_STATE_KNOWN)
476 rc = dasd_state_new_to_known(device);
477
478 if (!rc &&
479 device->state == DASD_STATE_KNOWN &&
480 device->target >= DASD_STATE_BASIC)
481 rc = dasd_state_known_to_basic(device);
482
483 if (!rc &&
484 device->state == DASD_STATE_BASIC &&
485 device->target >= DASD_STATE_READY)
486 rc = dasd_state_basic_to_ready(device);
487
488 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700489 device->state == DASD_STATE_UNFMT &&
490 device->target > DASD_STATE_UNFMT)
491 rc = -EPERM;
492
493 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 device->state == DASD_STATE_READY &&
495 device->target >= DASD_STATE_ONLINE)
496 rc = dasd_state_ready_to_online(device);
497
498 return rc;
499}
500
501/*
502 * Device shutdown state changes.
503 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100504static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Horst Hummel8f617012006-08-30 14:33:33 +0200506 int rc;
507
508 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (device->state == DASD_STATE_ONLINE &&
510 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200511 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200512
Horst Hummel8f617012006-08-30 14:33:33 +0200513 if (!rc &&
514 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200516 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800517
Horst Hummel8f617012006-08-30 14:33:33 +0200518 if (!rc &&
519 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800520 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200521 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800522
Horst Hummel8f617012006-08-30 14:33:33 +0200523 if (!rc &&
524 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200526 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200527
Horst Hummel8f617012006-08-30 14:33:33 +0200528 if (!rc &&
529 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200531 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Horst Hummel8f617012006-08-30 14:33:33 +0200533 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * This is the main startup/shutdown routine.
538 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100539static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Sebastian Ott181d9522009-06-22 12:08:21 +0200541 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (device->state == device->target)
544 /* Already where we want to go today... */
545 return;
546 if (device->state < device->target)
547 rc = dasd_increase_state(device);
548 else
549 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200550 if (rc == -EAGAIN)
551 return;
552 if (rc)
553 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200555 /* let user-space know that the device status changed */
556 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200557
558 if (device->state == device->target)
559 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Kick starter for devices that did not complete the startup/shutdown
564 * procedure or were sleeping because of a pending state.
565 * dasd_kick_device will schedule a call do do_kick_device to the kernel
566 * event daemon.
567 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Al Viro4927b3f2006-12-06 19:18:20 +0000570 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100571 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100573 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100574 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dasd_put_device(device);
576}
577
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100578void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 dasd_get_device(device);
581 /* queue call to dasd_kick_device to the kernel event daemon. */
582 schedule_work(&device->kick_work);
583}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200584EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200587 * dasd_reload_device will schedule a call do do_reload_device to the kernel
588 * event daemon.
589 */
590static void do_reload_device(struct work_struct *work)
591{
592 struct dasd_device *device = container_of(work, struct dasd_device,
593 reload_device);
594 device->discipline->reload(device);
595 dasd_put_device(device);
596}
597
598void dasd_reload_device(struct dasd_device *device)
599{
600 dasd_get_device(device);
601 /* queue call to dasd_reload_device to the kernel event daemon. */
602 schedule_work(&device->reload_device);
603}
604EXPORT_SYMBOL(dasd_reload_device);
605
606/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200607 * dasd_restore_device will schedule a call do do_restore_device to the kernel
608 * event daemon.
609 */
610static void do_restore_device(struct work_struct *work)
611{
612 struct dasd_device *device = container_of(work, struct dasd_device,
613 restore_device);
614 device->cdev->drv->restore(device->cdev);
615 dasd_put_device(device);
616}
617
618void dasd_restore_device(struct dasd_device *device)
619{
620 dasd_get_device(device);
621 /* queue call to dasd_restore_device to the kernel event daemon. */
622 schedule_work(&device->restore_device);
623}
624
625/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * Set the target state for a device and starts the state change.
627 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100628void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200630 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100631 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* If we are in probeonly mode stop at DASD_STATE_READY. */
633 if (dasd_probeonly && target > DASD_STATE_READY)
634 target = DASD_STATE_READY;
635 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100636 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 wake_up(&dasd_init_waitq);
638 device->target = target;
639 }
640 if (device->state != device->target)
641 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100642 mutex_unlock(&device->state_mutex);
643 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200645EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647/*
648 * Enable devices with device numbers in [from..to].
649 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100650static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 return (device->state == device->target);
653}
654
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100655void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 dasd_set_target_state(device, DASD_STATE_ONLINE);
658 if (device->state <= DASD_STATE_KNOWN)
659 /* No discipline for device found. */
660 dasd_set_target_state(device, DASD_STATE_NEW);
661 /* Now wait for the devices to come up. */
662 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400663
664 dasd_reload_device(device);
665 if (device->discipline->kick_validate)
666 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200668EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/*
671 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
672 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200673
674unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676#ifdef CONFIG_DASD_PROFILE
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200677struct dasd_profile_info dasd_global_profile_data;
678static struct dentry *dasd_global_profile_dentry;
679static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/*
682 * Add profiling information for cqr before execution.
683 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100684static void dasd_profile_start(struct dasd_block *block,
685 struct dasd_ccw_req *cqr,
686 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct list_head *l;
689 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200690 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* count the length of the chanq for statistics */
693 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200694 if (dasd_global_profile_level || block->profile.data)
695 list_for_each(l, &block->ccw_queue)
696 if (++counter >= 31)
697 break;
698
699 if (dasd_global_profile_level) {
700 dasd_global_profile_data.dasd_io_nr_req[counter]++;
701 if (rq_data_dir(req) == READ)
702 dasd_global_profile_data.dasd_read_nr_req[counter]++;
703 }
704
705 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200706 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200707 block->profile.data->dasd_io_nr_req[counter]++;
708 if (rq_data_dir(req) == READ)
709 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200710 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200711 spin_unlock(&block->profile.lock);
712
713 /*
714 * We count the request for the start device, even though it may run on
715 * some other device due to error recovery. This way we make sure that
716 * we count each request only once.
717 */
718 device = cqr->startdev;
719 if (device->profile.data) {
720 counter = 1; /* request is not yet queued on the start device */
721 list_for_each(l, &device->ccw_queue)
722 if (++counter >= 31)
723 break;
724 }
725 spin_lock(&device->profile.lock);
726 if (device->profile.data) {
727 device->profile.data->dasd_io_nr_req[counter]++;
728 if (rq_data_dir(req) == READ)
729 device->profile.data->dasd_read_nr_req[counter]++;
730 }
731 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
735 * Add profiling information for cqr after execution.
736 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200737
738#define dasd_profile_counter(value, index) \
739{ \
740 for (index = 0; index < 31 && value >> (2+index); index++) \
741 ; \
742}
743
744static void dasd_profile_end_add_data(struct dasd_profile_info *data,
745 int is_alias,
746 int is_tpm,
747 int is_read,
748 long sectors,
749 int sectors_ind,
750 int tottime_ind,
751 int tottimeps_ind,
752 int strtime_ind,
753 int irqtime_ind,
754 int irqtimeps_ind,
755 int endtime_ind)
756{
757 /* in case of an overflow, reset the whole profile */
758 if (data->dasd_io_reqs == UINT_MAX) {
759 memset(data, 0, sizeof(*data));
760 getnstimeofday(&data->starttod);
761 }
762 data->dasd_io_reqs++;
763 data->dasd_io_sects += sectors;
764 if (is_alias)
765 data->dasd_io_alias++;
766 if (is_tpm)
767 data->dasd_io_tpm++;
768
769 data->dasd_io_secs[sectors_ind]++;
770 data->dasd_io_times[tottime_ind]++;
771 data->dasd_io_timps[tottimeps_ind]++;
772 data->dasd_io_time1[strtime_ind]++;
773 data->dasd_io_time2[irqtime_ind]++;
774 data->dasd_io_time2ps[irqtimeps_ind]++;
775 data->dasd_io_time3[endtime_ind]++;
776
777 if (is_read) {
778 data->dasd_read_reqs++;
779 data->dasd_read_sects += sectors;
780 if (is_alias)
781 data->dasd_read_alias++;
782 if (is_tpm)
783 data->dasd_read_tpm++;
784 data->dasd_read_secs[sectors_ind]++;
785 data->dasd_read_times[tottime_ind]++;
786 data->dasd_read_time1[strtime_ind]++;
787 data->dasd_read_time2[irqtime_ind]++;
788 data->dasd_read_time3[endtime_ind]++;
789 }
790}
791
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100792static void dasd_profile_end(struct dasd_block *block,
793 struct dasd_ccw_req *cqr,
794 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 long strtime, irqtime, endtime, tottime; /* in microseconds */
797 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200798 struct dasd_device *device;
799 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
800 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200802 device = cqr->startdev;
803 if (!(dasd_global_profile_level ||
804 block->profile.data ||
805 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
807
Tejun Heo83096eb2009-05-07 22:24:39 +0900808 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!cqr->buildclk || !cqr->startclk ||
810 !cqr->stopclk || !cqr->endclk ||
811 !sectors)
812 return;
813
814 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
815 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
816 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
817 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
818 tottimeps = tottime / sectors;
819
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200820 dasd_profile_counter(sectors, sectors_ind);
821 dasd_profile_counter(tottime, tottime_ind);
822 dasd_profile_counter(tottimeps, tottimeps_ind);
823 dasd_profile_counter(strtime, strtime_ind);
824 dasd_profile_counter(irqtime, irqtime_ind);
825 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
826 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200828 if (dasd_global_profile_level) {
829 dasd_profile_end_add_data(&dasd_global_profile_data,
830 cqr->startdev != block->base,
831 cqr->cpmode == 1,
832 rq_data_dir(req) == READ,
833 sectors, sectors_ind, tottime_ind,
834 tottimeps_ind, strtime_ind,
835 irqtime_ind, irqtimeps_ind,
836 endtime_ind);
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200839 spin_lock(&block->profile.lock);
840 if (block->profile.data)
841 dasd_profile_end_add_data(block->profile.data,
842 cqr->startdev != block->base,
843 cqr->cpmode == 1,
844 rq_data_dir(req) == READ,
845 sectors, sectors_ind, tottime_ind,
846 tottimeps_ind, strtime_ind,
847 irqtime_ind, irqtimeps_ind,
848 endtime_ind);
849 spin_unlock(&block->profile.lock);
850
851 spin_lock(&device->profile.lock);
852 if (device->profile.data)
853 dasd_profile_end_add_data(device->profile.data,
854 cqr->startdev != block->base,
855 cqr->cpmode == 1,
856 rq_data_dir(req) == READ,
857 sectors, sectors_ind, tottime_ind,
858 tottimeps_ind, strtime_ind,
859 irqtime_ind, irqtimeps_ind,
860 endtime_ind);
861 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200863
864void dasd_profile_reset(struct dasd_profile *profile)
865{
866 struct dasd_profile_info *data;
867
868 spin_lock_bh(&profile->lock);
869 data = profile->data;
870 if (!data) {
871 spin_unlock_bh(&profile->lock);
872 return;
873 }
874 memset(data, 0, sizeof(*data));
875 getnstimeofday(&data->starttod);
876 spin_unlock_bh(&profile->lock);
877}
878
879void dasd_global_profile_reset(void)
880{
881 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
882 getnstimeofday(&dasd_global_profile_data.starttod);
883}
884
885int dasd_profile_on(struct dasd_profile *profile)
886{
887 struct dasd_profile_info *data;
888
889 data = kzalloc(sizeof(*data), GFP_KERNEL);
890 if (!data)
891 return -ENOMEM;
892 spin_lock_bh(&profile->lock);
893 if (profile->data) {
894 spin_unlock_bh(&profile->lock);
895 kfree(data);
896 return 0;
897 }
898 getnstimeofday(&data->starttod);
899 profile->data = data;
900 spin_unlock_bh(&profile->lock);
901 return 0;
902}
903
904void dasd_profile_off(struct dasd_profile *profile)
905{
906 spin_lock_bh(&profile->lock);
907 kfree(profile->data);
908 profile->data = NULL;
909 spin_unlock_bh(&profile->lock);
910}
911
912char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
913{
914 char *buffer;
915
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200916 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200917 if (buffer == NULL)
918 return ERR_PTR(-ENOMEM);
919 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200920 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200921 return ERR_PTR(-EFAULT);
922 }
923 /* got the string, now strip linefeed. */
924 if (buffer[user_len - 1] == '\n')
925 buffer[user_len - 1] = 0;
926 else
927 buffer[user_len] = 0;
928 return buffer;
929}
930
931static ssize_t dasd_stats_write(struct file *file,
932 const char __user *user_buf,
933 size_t user_len, loff_t *pos)
934{
935 char *buffer, *str;
936 int rc;
937 struct seq_file *m = (struct seq_file *)file->private_data;
938 struct dasd_profile *prof = m->private;
939
940 if (user_len > 65536)
941 user_len = 65536;
942 buffer = dasd_get_user_string(user_buf, user_len);
943 if (IS_ERR(buffer))
944 return PTR_ERR(buffer);
945
946 str = skip_spaces(buffer);
947 rc = user_len;
948 if (strncmp(str, "reset", 5) == 0) {
949 dasd_profile_reset(prof);
950 } else if (strncmp(str, "on", 2) == 0) {
951 rc = dasd_profile_on(prof);
952 if (!rc)
953 rc = user_len;
954 } else if (strncmp(str, "off", 3) == 0) {
955 dasd_profile_off(prof);
956 } else
957 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200958 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200959 return rc;
960}
961
962static void dasd_stats_array(struct seq_file *m, unsigned int *array)
963{
964 int i;
965
966 for (i = 0; i < 32; i++)
967 seq_printf(m, "%u ", array[i]);
968 seq_putc(m, '\n');
969}
970
971static void dasd_stats_seq_print(struct seq_file *m,
972 struct dasd_profile_info *data)
973{
974 seq_printf(m, "start_time %ld.%09ld\n",
975 data->starttod.tv_sec, data->starttod.tv_nsec);
976 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
977 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
978 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
979 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200980 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200981 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200982 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200983 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200984 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200985 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200986 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200987 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200988 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200989 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200990 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200991 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200992 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200993 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200994 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200995 dasd_stats_array(m, data->dasd_io_nr_req);
996 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
997 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
998 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
999 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001000 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001001 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001002 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001003 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001004 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001005 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001006 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001007 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001008 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001009 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001010 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001011 dasd_stats_array(m, data->dasd_read_nr_req);
1012}
1013
1014static int dasd_stats_show(struct seq_file *m, void *v)
1015{
1016 struct dasd_profile *profile;
1017 struct dasd_profile_info *data;
1018
1019 profile = m->private;
1020 spin_lock_bh(&profile->lock);
1021 data = profile->data;
1022 if (!data) {
1023 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001024 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001025 return 0;
1026 }
1027 dasd_stats_seq_print(m, data);
1028 spin_unlock_bh(&profile->lock);
1029 return 0;
1030}
1031
1032static int dasd_stats_open(struct inode *inode, struct file *file)
1033{
1034 struct dasd_profile *profile = inode->i_private;
1035 return single_open(file, dasd_stats_show, profile);
1036}
1037
1038static const struct file_operations dasd_stats_raw_fops = {
1039 .owner = THIS_MODULE,
1040 .open = dasd_stats_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = single_release,
1044 .write = dasd_stats_write,
1045};
1046
1047static ssize_t dasd_stats_global_write(struct file *file,
1048 const char __user *user_buf,
1049 size_t user_len, loff_t *pos)
1050{
1051 char *buffer, *str;
1052 ssize_t rc;
1053
1054 if (user_len > 65536)
1055 user_len = 65536;
1056 buffer = dasd_get_user_string(user_buf, user_len);
1057 if (IS_ERR(buffer))
1058 return PTR_ERR(buffer);
1059 str = skip_spaces(buffer);
1060 rc = user_len;
1061 if (strncmp(str, "reset", 5) == 0) {
1062 dasd_global_profile_reset();
1063 } else if (strncmp(str, "on", 2) == 0) {
1064 dasd_global_profile_reset();
1065 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1066 } else if (strncmp(str, "off", 3) == 0) {
1067 dasd_global_profile_level = DASD_PROFILE_OFF;
1068 } else
1069 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001070 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001071 return rc;
1072}
1073
1074static int dasd_stats_global_show(struct seq_file *m, void *v)
1075{
1076 if (!dasd_global_profile_level) {
Fabian Frederickc794caf2014-06-26 19:41:49 +02001077 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001078 return 0;
1079 }
1080 dasd_stats_seq_print(m, &dasd_global_profile_data);
1081 return 0;
1082}
1083
1084static int dasd_stats_global_open(struct inode *inode, struct file *file)
1085{
1086 return single_open(file, dasd_stats_global_show, NULL);
1087}
1088
1089static const struct file_operations dasd_stats_global_fops = {
1090 .owner = THIS_MODULE,
1091 .open = dasd_stats_global_open,
1092 .read = seq_read,
1093 .llseek = seq_lseek,
1094 .release = single_release,
1095 .write = dasd_stats_global_write,
1096};
1097
1098static void dasd_profile_init(struct dasd_profile *profile,
1099 struct dentry *base_dentry)
1100{
Al Virof4ae40a2011-07-24 04:33:43 -04001101 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001102 struct dentry *pde;
1103
1104 if (!base_dentry)
1105 return;
1106 profile->dentry = NULL;
1107 profile->data = NULL;
1108 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1109 pde = debugfs_create_file("statistics", mode, base_dentry,
1110 profile, &dasd_stats_raw_fops);
1111 if (pde && !IS_ERR(pde))
1112 profile->dentry = pde;
1113 return;
1114}
1115
1116static void dasd_profile_exit(struct dasd_profile *profile)
1117{
1118 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001119 debugfs_remove(profile->dentry);
1120 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001121}
1122
1123static void dasd_statistics_removeroot(void)
1124{
1125 dasd_global_profile_level = DASD_PROFILE_OFF;
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001126 debugfs_remove(dasd_global_profile_dentry);
1127 dasd_global_profile_dentry = NULL;
1128 debugfs_remove(dasd_debugfs_global_entry);
1129 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001130}
1131
1132static void dasd_statistics_createroot(void)
1133{
Al Virof4ae40a2011-07-24 04:33:43 -04001134 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001135 struct dentry *pde;
1136
1137 dasd_debugfs_root_entry = NULL;
1138 dasd_debugfs_global_entry = NULL;
1139 dasd_global_profile_dentry = NULL;
1140 pde = debugfs_create_dir("dasd", NULL);
1141 if (!pde || IS_ERR(pde))
1142 goto error;
1143 dasd_debugfs_root_entry = pde;
1144 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1145 if (!pde || IS_ERR(pde))
1146 goto error;
1147 dasd_debugfs_global_entry = pde;
1148
1149 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1150 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1151 NULL, &dasd_stats_global_fops);
1152 if (!pde || IS_ERR(pde))
1153 goto error;
1154 dasd_global_profile_dentry = pde;
1155 return;
1156
1157error:
1158 DBF_EVENT(DBF_ERR, "%s",
1159 "Creation of the dasd debugfs interface failed");
1160 dasd_statistics_removeroot();
1161 return;
1162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001165#define dasd_profile_start(block, cqr, req) do {} while (0)
1166#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001167
1168static void dasd_statistics_createroot(void)
1169{
1170 return;
1171}
1172
1173static void dasd_statistics_removeroot(void)
1174{
1175 return;
1176}
1177
1178int dasd_stats_generic_show(struct seq_file *m, void *v)
1179{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001180 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001181 return 0;
1182}
1183
1184static void dasd_profile_init(struct dasd_profile *profile,
1185 struct dentry *base_dentry)
1186{
1187 return;
1188}
1189
1190static void dasd_profile_exit(struct dasd_profile *profile)
1191{
1192 return;
1193}
1194
1195int dasd_profile_on(struct dasd_profile *profile)
1196{
1197 return 0;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif /* CONFIG_DASD_PROFILE */
1201
1202/*
1203 * Allocate memory for a channel program with 'cplength' channel
1204 * command words and 'datasize' additional space. There are two
1205 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1206 * memory and 2) dasd_smalloc_request uses the static ccw memory
1207 * that gets allocated for each device.
1208 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001209struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001210 int datasize,
1211 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct dasd_ccw_req *cqr;
1214
1215 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001216 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001217 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001219 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (cqr == NULL)
1221 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 cqr->cpaddr = NULL;
1223 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001224 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 GFP_ATOMIC | GFP_DMA);
1226 if (cqr->cpaddr == NULL) {
1227 kfree(cqr);
1228 return ERR_PTR(-ENOMEM);
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 cqr->data = NULL;
1232 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001233 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001235 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 kfree(cqr);
1237 return ERR_PTR(-ENOMEM);
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001240 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1242 dasd_get_device(device);
1243 return cqr;
1244}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001245EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Stefan Haberland68b781f2009-09-11 10:28:29 +02001247struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001248 int datasize,
1249 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 unsigned long flags;
1252 struct dasd_ccw_req *cqr;
1253 char *data;
1254 int size;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1257 if (cplength > 0)
1258 size += cplength * sizeof(struct ccw1);
1259 if (datasize > 0)
1260 size += datasize;
1261 spin_lock_irqsave(&device->mem_lock, flags);
1262 cqr = (struct dasd_ccw_req *)
1263 dasd_alloc_chunk(&device->ccw_chunks, size);
1264 spin_unlock_irqrestore(&device->mem_lock, flags);
1265 if (cqr == NULL)
1266 return ERR_PTR(-ENOMEM);
1267 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1268 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1269 cqr->cpaddr = NULL;
1270 if (cplength > 0) {
1271 cqr->cpaddr = (struct ccw1 *) data;
1272 data += cplength*sizeof(struct ccw1);
1273 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1274 }
1275 cqr->data = NULL;
1276 if (datasize > 0) {
1277 cqr->data = data;
1278 memset(cqr->data, 0, datasize);
1279 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001280 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1282 dasd_get_device(device);
1283 return cqr;
1284}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001285EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287/*
1288 * Free memory of a channel program. This function needs to free all the
1289 * idal lists that might have been created by dasd_set_cda and the
1290 * struct dasd_ccw_req itself.
1291 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001292void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001294#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct ccw1 *ccw;
1296
1297 /* Clear any idals used for the request. */
1298 ccw = cqr->cpaddr;
1299 do {
1300 clear_normalized_cda(ccw);
1301 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1302#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001303 kfree(cqr->cpaddr);
1304 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 kfree(cqr);
1306 dasd_put_device(device);
1307}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001308EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001310void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 unsigned long flags;
1313
1314 spin_lock_irqsave(&device->mem_lock, flags);
1315 dasd_free_chunk(&device->ccw_chunks, cqr);
1316 spin_unlock_irqrestore(&device->mem_lock, flags);
1317 dasd_put_device(device);
1318}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001319EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321/*
1322 * Check discipline magic in cqr.
1323 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001324static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct dasd_device *device;
1327
1328 if (cqr == NULL)
1329 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001330 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001332 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 " dasd_ccw_req 0x%08x magic doesn't match"
1334 " discipline 0x%08x",
1335 cqr->magic,
1336 *(unsigned int *) device->discipline->name);
1337 return -EINVAL;
1338 }
1339 return 0;
1340}
1341
1342/*
1343 * Terminate the current i/o and set the request to clear_pending.
1344 * Timer keeps device runnig.
1345 * ccw_device_clear can fail if the i/o subsystem
1346 * is in a bad mood.
1347 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001348int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct dasd_device *device;
1351 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001352 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* Check the cqr */
1355 rc = dasd_check_cqr(cqr);
1356 if (rc)
1357 return rc;
1358 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001359 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1361 rc = ccw_device_clear(device->cdev, (long) cqr);
1362 switch (rc) {
1363 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001364 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001365 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001366 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 DBF_DEV_EVENT(DBF_DEBUG, device,
1368 "terminate cqr %p successful",
1369 cqr);
1370 break;
1371 case -ENODEV:
1372 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1373 "device gone, retry");
1374 break;
1375 case -EIO:
1376 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1377 "I/O error, retry");
1378 break;
1379 case -EINVAL:
1380 case -EBUSY:
1381 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1382 "device busy, retry later");
1383 break;
1384 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001385 /* internal error 10 - unknown rc*/
1386 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1387 dev_err(&device->cdev->dev, "An error occurred in the "
1388 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 BUG();
1390 break;
1391 }
1392 retries++;
1393 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001394 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return rc;
1396}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001397EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399/*
1400 * Start the i/o. This start_IO can fail if the channel is really busy.
1401 * In that case set up a timer to start the request later.
1402 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001403int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct dasd_device *device;
1406 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001407 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* Check the cqr */
1410 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001411 if (rc) {
1412 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001414 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001415 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001416 if (((cqr->block &&
1417 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1418 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1419 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1420 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1421 "because of stolen lock", cqr);
1422 cqr->status = DASD_CQR_ERROR;
1423 cqr->intrc = -EPERM;
1424 return -EPERM;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001427 /* internal error 14 - start_IO run out of retries */
1428 sprintf(errorstring, "14 %p", cqr);
1429 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1430 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001431 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -EIO;
1433 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001434 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 cqr->starttime = jiffies;
1436 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001437 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1438 cqr->lpm &= device->path_data.opm;
1439 if (!cqr->lpm)
1440 cqr->lpm = device->path_data.opm;
1441 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001442 if (cqr->cpmode == 1) {
1443 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1444 (long) cqr, cqr->lpm);
1445 } else {
1446 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1447 (long) cqr, cqr->lpm, 0);
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 switch (rc) {
1450 case 0:
1451 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
1453 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001454 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 "start_IO: device busy, retry later");
1456 break;
1457 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 "start_IO: request timeout, retry later");
1460 break;
1461 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001462 /* -EACCES indicates that the request used only a subset of the
1463 * available paths and all these paths are gone. If the lpm of
1464 * this request was only a subset of the opm (e.g. the ppm) then
1465 * we just do a retry with all available paths.
1466 * If we already use the full opm, something is amiss, and we
1467 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001469 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1470 DBF_DEV_EVENT(DBF_WARNING, device,
1471 "start_IO: selected paths gone (%x)",
1472 cqr->lpm);
1473 } else if (cqr->lpm != device->path_data.opm) {
1474 cqr->lpm = device->path_data.opm;
1475 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1476 "start_IO: selected paths gone,"
1477 " retry on all paths");
1478 } else {
1479 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1480 "start_IO: all paths in opm gone,"
1481 " do path verification");
1482 dasd_generic_last_path_gone(device);
1483 device->path_data.opm = 0;
1484 device->path_data.ppm = 0;
1485 device->path_data.npm = 0;
1486 device->path_data.tbvpm =
1487 ccw_device_get_path_mask(device->cdev);
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001491 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001492 "start_IO: -ENODEV device gone, retry");
1493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001495 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001496 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001498 case -EINVAL:
1499 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001500 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001501 "start_IO: -EINVAL device currently "
1502 "not accessible");
1503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001505 /* internal error 11 - unknown rc */
1506 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1507 dev_err(&device->cdev->dev,
1508 "An error occurred in the DASD device driver, "
1509 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 BUG();
1511 break;
1512 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001513 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return rc;
1515}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001516EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518/*
1519 * Timeout function for dasd devices. This is used for different purposes
1520 * 1) missing interrupt handler for normal operation
1521 * 2) delayed start of request where start_IO failed with -EBUSY
1522 * 3) timeout for missing state change interrupts
1523 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1524 * DASD_CQR_QUEUED for 2) and 3).
1525 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001526static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 unsigned long flags;
1529 struct dasd_device *device;
1530
1531 device = (struct dasd_device *) ptr;
1532 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1533 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001534 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001536 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/*
1540 * Setup timeout for a device in jiffies.
1541 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001542void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001544 if (expires == 0)
1545 del_timer(&device->timer);
1546 else
1547 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001549EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551/*
1552 * Clear timeout for a device.
1553 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001554void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001556 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001558EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001560static void dasd_handle_killed_request(struct ccw_device *cdev,
1561 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 struct dasd_ccw_req *cqr;
1564 struct dasd_device *device;
1565
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001566 if (!intparm)
1567 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 cqr = (struct dasd_ccw_req *) intparm;
1569 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001570 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1571 "invalid status in handle_killed_request: "
1572 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return;
1574 }
1575
Stefan Haberland589c74d2010-02-26 22:37:47 +01001576 device = dasd_device_from_cdev_locked(cdev);
1577 if (IS_ERR(device)) {
1578 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1579 "unable to get device from cdev");
1580 return;
1581 }
1582
1583 if (!cqr->startdev ||
1584 device != cqr->startdev ||
1585 strncmp(cqr->startdev->discipline->ebcname,
1586 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001587 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1588 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001589 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return;
1591 }
1592
1593 /* Schedule request to be retried. */
1594 cqr->status = DASD_CQR_QUEUED;
1595
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001596 dasd_device_clear_timer(device);
1597 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 dasd_put_device(device);
1599}
1600
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001601void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001603 /* First of all start sense subsystem status request. */
1604 dasd_eer_snss(device);
1605
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001606 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001607 dasd_schedule_device_bh(device);
1608 if (device->block)
1609 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001611EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613/*
1614 * Interrupt handler for "normal" ssch-io based dasd devices.
1615 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001616void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1617 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct dasd_ccw_req *cqr, *next;
1620 struct dasd_device *device;
1621 unsigned long long now;
1622 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if (IS_ERR(irb)) {
1625 switch (PTR_ERR(irb)) {
1626 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001629 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1630 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001633 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1634 "unknown error %ld\n", __func__,
1635 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001637 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return;
1639 }
1640
Heiko Carstens1aae0562013-01-30 09:49:40 +01001641 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001642 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001643 /* check for conditions that should be handled immediately */
1644 if (!cqr ||
1645 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1646 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001647 if (cqr)
1648 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001649 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001650 if (IS_ERR(device))
1651 return;
1652 /* ignore unsolicited interrupts for DIAG discipline */
1653 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001655 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001657 device->discipline->dump_sense_dbf(device, irb, "int");
1658 if (device->features & DASD_FEATURE_ERPLOG)
1659 device->discipline->dump_sense(device, cqr, irb);
1660 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001661 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001663
1664 /* check for for attention message */
1665 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1666 device = dasd_device_from_cdev_locked(cdev);
1667 device->discipline->check_attention(device, irb->esw.esw1.lpum);
1668 dasd_put_device(device);
1669 }
1670
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001671 if (!cqr)
1672 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001674 device = (struct dasd_device *) cqr->startdev;
1675 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001677 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1678 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return;
1680 }
1681
1682 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001683 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001684 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001685 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02001686 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
1687 cqr->callback_data = DASD_SLEEPON_END_TAG;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001688 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001689 wake_up(&dasd_flush_wq);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02001690 wake_up(&generic_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001691 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return;
1693 }
1694
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001695 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001697 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1698 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return;
1700 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001701
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001702 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001704 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1705 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001706 /* request was completed successfully */
1707 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 cqr->stopclk = now;
1709 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001710 if (cqr->devlist.next != &device->ccw_queue) {
1711 next = list_entry(cqr->devlist.next,
1712 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001714 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001715 /*
1716 * If we don't want complex ERP for this request, then just
1717 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001718 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001719 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001720 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001721 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001722 DBF_DEV_EVENT(DBF_DEBUG, device,
1723 "default ERP in fastpath "
1724 "(%i retries left)",
1725 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001726 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1727 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001728 cqr->status = DASD_CQR_QUEUED;
1729 next = cqr;
1730 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001732 }
1733 if (next && (next->status == DASD_CQR_QUEUED) &&
1734 (!device->stopped)) {
1735 if (device->discipline->start_IO(next) == 0)
1736 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001739 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001741 dasd_device_clear_timer(device);
1742 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001744EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001746enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1747{
1748 struct dasd_device *device;
1749
1750 device = dasd_device_from_cdev_locked(cdev);
1751
1752 if (IS_ERR(device))
1753 goto out;
1754 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1755 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001756 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001757 dasd_put_device(device);
1758 goto out;
1759 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001760 if (device->discipline->dump_sense_dbf)
1761 device->discipline->dump_sense_dbf(device, irb, "uc");
1762 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001763 dasd_put_device(device);
1764out:
1765 return UC_TODO_RETRY;
1766}
1767EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001770 * If we have an error on a dasd_block layer request then we cancel
1771 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001773static void __dasd_device_recovery(struct dasd_device *device,
1774 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct list_head *l, *n;
1777 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001780 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001782 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001784
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001785 list_for_each_safe(l, n, &device->ccw_queue) {
1786 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1787 if (cqr->status == DASD_CQR_QUEUED &&
1788 ref_cqr->block == cqr->block) {
1789 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001791 }
1792};
1793
1794/*
1795 * Remove those ccw requests from the queue that need to be returned
1796 * to the upper layer.
1797 */
1798static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1799 struct list_head *final_queue)
1800{
1801 struct list_head *l, *n;
1802 struct dasd_ccw_req *cqr;
1803
1804 /* Process request with final status. */
1805 list_for_each_safe(l, n, &device->ccw_queue) {
1806 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1807
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001808 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001809 if (cqr->status == DASD_CQR_QUEUED ||
1810 cqr->status == DASD_CQR_IN_IO ||
1811 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001812 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001813 if (cqr->status == DASD_CQR_ERROR) {
1814 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001816 /* Rechain finished requests to final queue */
1817 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819}
1820
1821/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001822 * the cqrs from the final queue are returned to the upper layer
1823 * by setting a dasd_block state and calling the callback function
1824 */
1825static void __dasd_device_process_final_queue(struct dasd_device *device,
1826 struct list_head *final_queue)
1827{
1828 struct list_head *l, *n;
1829 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001830 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001831 void (*callback)(struct dasd_ccw_req *, void *data);
1832 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001833 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001834
1835 list_for_each_safe(l, n, final_queue) {
1836 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1837 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001838 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001839 callback = cqr->callback;
1840 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001841 if (block)
1842 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001843 switch (cqr->status) {
1844 case DASD_CQR_SUCCESS:
1845 cqr->status = DASD_CQR_DONE;
1846 break;
1847 case DASD_CQR_ERROR:
1848 cqr->status = DASD_CQR_NEED_ERP;
1849 break;
1850 case DASD_CQR_CLEARED:
1851 cqr->status = DASD_CQR_TERMINATED;
1852 break;
1853 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001854 /* internal error 12 - wrong cqr status*/
1855 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1856 dev_err(&device->cdev->dev,
1857 "An error occurred in the DASD device driver, "
1858 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001859 BUG();
1860 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001861 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001862 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001863 if (block)
1864 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001865 }
1866}
1867
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001868/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * Take a look at the first request on the ccw queue and check
1870 * if it reached its expire time. If so, terminate the IO.
1871 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001872static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 struct dasd_ccw_req *cqr;
1875
1876 if (list_empty(&device->ccw_queue))
1877 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001878 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001879 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1880 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001881 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1882 /*
1883 * IO in safe offline processing should not
1884 * run out of retries
1885 */
1886 cqr->retries++;
1887 }
Horst Hummel29145a62006-12-04 15:40:15 +01001888 if (device->discipline->term_IO(cqr) != 0) {
1889 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001890 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001891 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001892 "ended, retrying in 5 s\n",
1893 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001894 cqr->expires += 5*HZ;
1895 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001896 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001897 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001898 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001899 "remaining\n", cqr, (cqr->expires/HZ),
1900 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902 }
1903}
1904
1905/*
1906 * Take a look at the first request on the ccw queue and check
1907 * if it needs to be started.
1908 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001909static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 struct dasd_ccw_req *cqr;
1912 int rc;
1913
1914 if (list_empty(&device->ccw_queue))
1915 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001916 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001917 if (cqr->status != DASD_CQR_QUEUED)
1918 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001919 /* when device is stopped, return request to previous layer
1920 * exception: only the disconnect or unresumed bits are set and the
1921 * cqr is a path verification request
1922 */
1923 if (device->stopped &&
1924 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1925 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1926 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001927 cqr->status = DASD_CQR_CLEARED;
1928 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001929 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001930 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001931
1932 rc = device->discipline->start_IO(cqr);
1933 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001934 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001935 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001936 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001937 } else
1938 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001939 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001940}
1941
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001942static void __dasd_device_check_path_events(struct dasd_device *device)
1943{
1944 int rc;
1945
1946 if (device->path_data.tbvpm) {
1947 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1948 DASD_UNRESUMED_PM))
1949 return;
1950 rc = device->discipline->verify_path(
1951 device, device->path_data.tbvpm);
1952 if (rc)
1953 dasd_device_set_timer(device, 50);
1954 else
1955 device->path_data.tbvpm = 0;
1956 }
1957};
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001960 * Go through all request on the dasd_device request queue,
1961 * terminate them on the cdev if necessary, and return them to the
1962 * submitting layer via callback.
1963 * Note:
1964 * Make sure that all 'submitting layers' still exist when
1965 * this function is called!. In other words, when 'device' is a base
1966 * device then all block layer requests must have been removed before
1967 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001969int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001971 struct dasd_ccw_req *cqr, *n;
1972 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 INIT_LIST_HEAD(&flush_queue);
1976 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001977 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001978 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001979 /* Check status and move request to flush_queue */
1980 switch (cqr->status) {
1981 case DASD_CQR_IN_IO:
1982 rc = device->discipline->term_IO(cqr);
1983 if (rc) {
1984 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001985 dev_err(&device->cdev->dev,
1986 "Flushing the DASD request queue "
1987 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001988 /* stop flush processing */
1989 goto finished;
1990 }
1991 break;
1992 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001993 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001994 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001995 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001996 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001997 break;
1998 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001999 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002000 }
Horst Hummel8f617012006-08-30 14:33:33 +02002001finished:
2002 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002003 /*
2004 * After this point all requests must be in state CLEAR_PENDING,
2005 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2006 * one of the others.
2007 */
2008 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2009 wait_event(dasd_flush_wq,
2010 (cqr->status != DASD_CQR_CLEAR_PENDING));
2011 /*
2012 * Now set each request back to TERMINATED, DONE or NEED_ERP
2013 * and call the callback function of flushed requests
2014 */
2015 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002016 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002018EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020/*
2021 * Acquire the device lock and process queues for the device.
2022 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002023static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 atomic_set (&device->tasklet_scheduled, 0);
2028 INIT_LIST_HEAD(&final_queue);
2029 spin_lock_irq(get_ccwdev_lock(device->cdev));
2030 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002031 __dasd_device_check_expire(device);
2032 /* find final requests on ccw queue */
2033 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002034 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2036 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002037 __dasd_device_process_final_queue(device, &final_queue);
2038 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002040 __dasd_device_start_head(device);
2041 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002042 if (waitqueue_active(&shutdown_waitq))
2043 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 dasd_put_device(device);
2045}
2046
2047/*
2048 * Schedules a call to dasd_tasklet over the device tasklet.
2049 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002050void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002053 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return;
2055 dasd_get_device(device);
2056 tasklet_hi_schedule(&device->tasklet);
2057}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002058EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002060void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2061{
2062 device->stopped |= bits;
2063}
2064EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2065
2066void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2067{
2068 device->stopped &= ~bits;
2069 if (!device->stopped)
2070 wake_up(&generic_waitq);
2071}
2072EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002075 * Queue a request to the head of the device ccw_queue.
2076 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002078void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct dasd_device *device;
2081 unsigned long flags;
2082
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002083 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002085 cqr->status = DASD_CQR_QUEUED;
2086 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002088 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2090}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002091EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002094 * Queue a request to the tail of the device ccw_queue.
2095 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002097void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 struct dasd_device *device;
2100 unsigned long flags;
2101
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002104 cqr->status = DASD_CQR_QUEUED;
2105 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002107 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2109}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002110EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002113 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002115void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002117 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2118 cqr->callback_data = DASD_SLEEPON_END_TAG;
2119 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2120 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
Stefan Haberland5915a872011-10-30 15:16:57 +01002122EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002124static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 struct dasd_device *device;
2127 int rc;
2128
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002129 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002131 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2133 return rc;
2134}
2135
2136/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002137 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2138 */
2139static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2140{
2141 struct dasd_device *device;
2142 dasd_erp_fn_t erp_fn;
2143
2144 if (cqr->status == DASD_CQR_FILLED)
2145 return 0;
2146 device = cqr->startdev;
2147 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2148 if (cqr->status == DASD_CQR_TERMINATED) {
2149 device->discipline->handle_terminated_request(cqr);
2150 return 1;
2151 }
2152 if (cqr->status == DASD_CQR_NEED_ERP) {
2153 erp_fn = device->discipline->erp_action(cqr);
2154 erp_fn(cqr);
2155 return 1;
2156 }
2157 if (cqr->status == DASD_CQR_FAILED)
2158 dasd_log_sense(cqr, &cqr->irb);
2159 if (cqr->refers) {
2160 __dasd_process_erp(device, cqr);
2161 return 1;
2162 }
2163 }
2164 return 0;
2165}
2166
2167static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2168{
2169 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2170 if (cqr->refers) /* erp is not done yet */
2171 return 1;
2172 return ((cqr->status != DASD_CQR_DONE) &&
2173 (cqr->status != DASD_CQR_FAILED));
2174 } else
2175 return (cqr->status == DASD_CQR_FILLED);
2176}
2177
2178static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2179{
2180 struct dasd_device *device;
2181 int rc;
2182 struct list_head ccw_queue;
2183 struct dasd_ccw_req *cqr;
2184
2185 INIT_LIST_HEAD(&ccw_queue);
2186 maincqr->status = DASD_CQR_FILLED;
2187 device = maincqr->startdev;
2188 list_add(&maincqr->blocklist, &ccw_queue);
2189 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2190 cqr = list_first_entry(&ccw_queue,
2191 struct dasd_ccw_req, blocklist)) {
2192
2193 if (__dasd_sleep_on_erp(cqr))
2194 continue;
2195 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2196 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002197 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2198 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2199 cqr->status = DASD_CQR_FAILED;
2200 cqr->intrc = -EPERM;
2201 continue;
2202 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002203 /* Non-temporary stop condition will trigger fail fast */
2204 if (device->stopped & ~DASD_STOPPED_PENDING &&
2205 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2206 (!dasd_eer_enabled(device))) {
2207 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002208 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002209 continue;
2210 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002211 /* Don't try to start requests if device is stopped */
2212 if (interruptible) {
2213 rc = wait_event_interruptible(
2214 generic_waitq, !(device->stopped));
2215 if (rc == -ERESTARTSYS) {
2216 cqr->status = DASD_CQR_FAILED;
2217 maincqr->intrc = rc;
2218 continue;
2219 }
2220 } else
2221 wait_event(generic_waitq, !(device->stopped));
2222
Stefan Haberland5915a872011-10-30 15:16:57 +01002223 if (!cqr->callback)
2224 cqr->callback = dasd_wakeup_cb;
2225
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002226 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002227 dasd_add_request_tail(cqr);
2228 if (interruptible) {
2229 rc = wait_event_interruptible(
2230 generic_waitq, _wait_for_wakeup(cqr));
2231 if (rc == -ERESTARTSYS) {
2232 dasd_cancel_req(cqr);
2233 /* wait (non-interruptible) for final status */
2234 wait_event(generic_waitq,
2235 _wait_for_wakeup(cqr));
2236 cqr->status = DASD_CQR_FAILED;
2237 maincqr->intrc = rc;
2238 continue;
2239 }
2240 } else
2241 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2242 }
2243
Heiko Carstens1aae0562013-01-30 09:49:40 +01002244 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002245 if ((maincqr->status != DASD_CQR_DONE) &&
2246 (maincqr->intrc != -ERESTARTSYS))
2247 dasd_log_sense(maincqr, &maincqr->irb);
2248 if (maincqr->status == DASD_CQR_DONE)
2249 rc = 0;
2250 else if (maincqr->intrc)
2251 rc = maincqr->intrc;
2252 else
2253 rc = -EIO;
2254 return rc;
2255}
2256
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002257static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2258{
2259 struct dasd_ccw_req *cqr;
2260
2261 list_for_each_entry(cqr, ccw_queue, blocklist) {
2262 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2263 return 0;
2264 }
2265
2266 return 1;
2267}
2268
2269static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2270{
2271 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002272 struct dasd_ccw_req *cqr, *n;
Stefan Haberland362ce842014-10-01 13:04:54 +02002273 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002274
2275retry:
2276 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2277 device = cqr->startdev;
2278 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2279 continue;
2280
2281 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2282 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2283 cqr->status = DASD_CQR_FAILED;
2284 cqr->intrc = -EPERM;
2285 continue;
2286 }
2287 /*Non-temporary stop condition will trigger fail fast*/
2288 if (device->stopped & ~DASD_STOPPED_PENDING &&
2289 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2290 !dasd_eer_enabled(device)) {
2291 cqr->status = DASD_CQR_FAILED;
2292 cqr->intrc = -EAGAIN;
2293 continue;
2294 }
2295
2296 /*Don't try to start requests if device is stopped*/
2297 if (interruptible) {
2298 rc = wait_event_interruptible(
2299 generic_waitq, !device->stopped);
2300 if (rc == -ERESTARTSYS) {
2301 cqr->status = DASD_CQR_FAILED;
2302 cqr->intrc = rc;
2303 continue;
2304 }
2305 } else
2306 wait_event(generic_waitq, !(device->stopped));
2307
2308 if (!cqr->callback)
2309 cqr->callback = dasd_wakeup_cb;
2310 cqr->callback_data = DASD_SLEEPON_START_TAG;
2311 dasd_add_request_tail(cqr);
2312 }
2313
2314 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2315
2316 rc = 0;
2317 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002318 /*
2319 * for alias devices simplify error recovery and
2320 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002321 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002322 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002323 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002324 (cqr->status == DASD_CQR_TERMINATED ||
2325 cqr->status == DASD_CQR_NEED_ERP))
2326 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002327
2328 /* normal recovery for basedev IO */
2329 if (__dasd_sleep_on_erp(cqr)) {
2330 goto retry;
2331 /* remember that ERP was needed */
2332 rc = 1;
2333 /* skip processing for active cqr */
2334 if (cqr->status != DASD_CQR_TERMINATED &&
2335 cqr->status != DASD_CQR_NEED_ERP)
2336 break;
Stefan Haberland8fa56ae2014-07-18 14:24:20 +02002337 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002338 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002339
2340 /* start ERP requests in upper loop */
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002341 if (rc)
2342 goto retry;
2343
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002344 return 0;
2345}
2346
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002347/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002348 * Queue a request to the tail of the device ccw_queue and wait for
2349 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002351int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002353 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002355EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002358 * Start requests from a ccw_queue and wait for their completion.
2359 */
2360int dasd_sleep_on_queue(struct list_head *ccw_queue)
2361{
2362 return _dasd_sleep_on_queue(ccw_queue, 0);
2363}
2364EXPORT_SYMBOL(dasd_sleep_on_queue);
2365
2366/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002367 * Queue a request to the tail of the device ccw_queue and wait
2368 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002370int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002372 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002374EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376/*
2377 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2378 * for eckd devices) the currently running request has to be terminated
2379 * and be put back to status queued, before the special request is added
2380 * to the head of the queue. Then the special request is waited on normally.
2381 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002382static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002385 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 if (list_empty(&device->ccw_queue))
2388 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002389 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002390 rc = device->discipline->term_IO(cqr);
2391 if (!rc)
2392 /*
2393 * CQR terminated because a more important request is pending.
2394 * Undo decreasing of retry counter because this is
2395 * not an error case.
2396 */
2397 cqr->retries++;
2398 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002401int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct dasd_device *device;
2404 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002405
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002406 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002407 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2408 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2409 cqr->status = DASD_CQR_FAILED;
2410 cqr->intrc = -EPERM;
2411 return -EIO;
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 spin_lock_irq(get_ccwdev_lock(device->cdev));
2414 rc = _dasd_term_running_cqr(device);
2415 if (rc) {
2416 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2417 return rc;
2418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002420 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002422 /*
2423 * add new request as second
2424 * first the terminated cqr needs to be finished
2425 */
2426 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002429 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2432
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002433 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002434
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002435 if (cqr->status == DASD_CQR_DONE)
2436 rc = 0;
2437 else if (cqr->intrc)
2438 rc = cqr->intrc;
2439 else
2440 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002441
2442 /* kick tasklets */
2443 dasd_schedule_device_bh(device);
2444 if (device->block)
2445 dasd_schedule_block_bh(device->block);
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return rc;
2448}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002449EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451/*
2452 * Cancels a request that was started with dasd_sleep_on_req.
2453 * This is useful to timeout requests. The request will be
2454 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002455 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002456 * negative error code if termination failed
2457 * Cancellation of a request is an asynchronous operation! The calling
2458 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002460int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002462 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 unsigned long flags;
2464 int rc;
2465
2466 rc = 0;
2467 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2468 switch (cqr->status) {
2469 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002470 /* request was not started - just set to cleared */
2471 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002472 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2473 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 break;
2475 case DASD_CQR_IN_IO:
2476 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002477 rc = device->discipline->term_IO(cqr);
2478 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002479 dev_err(&device->cdev->dev,
2480 "Cancelling request %p failed with rc=%d\n",
2481 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002483 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002486 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002490 dasd_schedule_device_bh(device);
2491 return rc;
2492}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002493EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002494
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002495/*
2496 * SECTION: Operations of the dasd_block layer.
2497 */
2498
2499/*
2500 * Timeout function for dasd_block. This is used when the block layer
2501 * is waiting for something that may not come reliably, (e.g. a state
2502 * change interrupt)
2503 */
2504static void dasd_block_timeout(unsigned long ptr)
2505{
2506 unsigned long flags;
2507 struct dasd_block *block;
2508
2509 block = (struct dasd_block *) ptr;
2510 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2511 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002512 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002513 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2514 dasd_schedule_block_bh(block);
2515}
2516
2517/*
2518 * Setup timeout for a dasd_block in jiffies.
2519 */
2520void dasd_block_set_timer(struct dasd_block *block, int expires)
2521{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002522 if (expires == 0)
2523 del_timer(&block->timer);
2524 else
2525 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002526}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002527EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002528
2529/*
2530 * Clear timeout for a dasd_block.
2531 */
2532void dasd_block_clear_timer(struct dasd_block *block)
2533{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002534 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002535}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002536EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002537
2538/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002539 * Process finished error recovery ccw.
2540 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002541static void __dasd_process_erp(struct dasd_device *device,
2542 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002543{
2544 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002545
2546 if (cqr->status == DASD_CQR_DONE)
2547 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2548 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002549 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002550 erp_fn = device->discipline->erp_postaction(cqr);
2551 erp_fn(cqr);
2552}
2553
2554/*
2555 * Fetch requests from the block device queue.
2556 */
2557static void __dasd_process_request_queue(struct dasd_block *block)
2558{
2559 struct request_queue *queue;
2560 struct request *req;
2561 struct dasd_ccw_req *cqr;
2562 struct dasd_device *basedev;
2563 unsigned long flags;
2564 queue = block->request_queue;
2565 basedev = block->base;
2566 /* No queue ? Then there is nothing to do. */
2567 if (queue == NULL)
2568 return;
2569
2570 /*
2571 * We requeue request from the block device queue to the ccw
2572 * queue only in two states. In state DASD_STATE_READY the
2573 * partition detection is done and we need to requeue requests
2574 * for that. State DASD_STATE_ONLINE is normal block device
2575 * operation.
2576 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002577 if (basedev->state < DASD_STATE_READY) {
2578 while ((req = blk_fetch_request(block->request_queue)))
2579 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002580 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002581 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002582 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002583 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002584 if (basedev->features & DASD_FEATURE_READONLY &&
2585 rq_data_dir(req) == WRITE) {
2586 DBF_DEV_EVENT(DBF_ERR, basedev,
2587 "Rejecting write request %p",
2588 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002589 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002590 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002591 continue;
2592 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002593 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2594 (basedev->features & DASD_FEATURE_FAILFAST ||
2595 blk_noretry_request(req))) {
2596 DBF_DEV_EVENT(DBF_ERR, basedev,
2597 "Rejecting failfast request %p",
2598 req);
2599 blk_start_request(req);
2600 __blk_end_request_all(req, -ETIMEDOUT);
2601 continue;
2602 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002603 cqr = basedev->discipline->build_cp(basedev, block, req);
2604 if (IS_ERR(cqr)) {
2605 if (PTR_ERR(cqr) == -EBUSY)
2606 break; /* normal end condition */
2607 if (PTR_ERR(cqr) == -ENOMEM)
2608 break; /* terminate request queue loop */
2609 if (PTR_ERR(cqr) == -EAGAIN) {
2610 /*
2611 * The current request cannot be build right
2612 * now, we have to try later. If this request
2613 * is the head-of-queue we stop the device
2614 * for 1/2 second.
2615 */
2616 if (!list_empty(&block->ccw_queue))
2617 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002618 spin_lock_irqsave(
2619 get_ccwdev_lock(basedev->cdev), flags);
2620 dasd_device_set_stop_bits(basedev,
2621 DASD_STOPPED_PENDING);
2622 spin_unlock_irqrestore(
2623 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002624 dasd_block_set_timer(block, HZ/2);
2625 break;
2626 }
2627 DBF_DEV_EVENT(DBF_ERR, basedev,
2628 "CCW creation failed (rc=%ld) "
2629 "on request %p",
2630 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002631 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002632 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002633 continue;
2634 }
2635 /*
2636 * Note: callback is set to dasd_return_cqr_cb in
2637 * __dasd_block_start_head to cover erp requests as well
2638 */
2639 cqr->callback_data = (void *) req;
2640 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002641 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002642 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002643 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002644 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002645 dasd_profile_start(block, cqr, req);
2646 }
2647}
2648
2649static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2650{
2651 struct request *req;
2652 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002653 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002654
2655 req = (struct request *) cqr->callback_data;
2656 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002657 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002658 if (status < 0)
2659 error = status;
2660 else if (status == 0) {
2661 if (cqr->intrc == -EPERM)
2662 error = -EBADE;
2663 else if (cqr->intrc == -ENOLINK ||
2664 cqr->intrc == -ETIMEDOUT)
2665 error = cqr->intrc;
2666 else
2667 error = -EIO;
2668 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002669 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002670}
2671
2672/*
2673 * Process ccw request queue.
2674 */
2675static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2676 struct list_head *final_queue)
2677{
2678 struct list_head *l, *n;
2679 struct dasd_ccw_req *cqr;
2680 dasd_erp_fn_t erp_fn;
2681 unsigned long flags;
2682 struct dasd_device *base = block->base;
2683
2684restart:
2685 /* Process request with final status. */
2686 list_for_each_safe(l, n, &block->ccw_queue) {
2687 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2688 if (cqr->status != DASD_CQR_DONE &&
2689 cqr->status != DASD_CQR_FAILED &&
2690 cqr->status != DASD_CQR_NEED_ERP &&
2691 cqr->status != DASD_CQR_TERMINATED)
2692 continue;
2693
2694 if (cqr->status == DASD_CQR_TERMINATED) {
2695 base->discipline->handle_terminated_request(cqr);
2696 goto restart;
2697 }
2698
2699 /* Process requests that may be recovered */
2700 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002701 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002702 if (IS_ERR(erp_fn(cqr)))
2703 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002704 goto restart;
2705 }
2706
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002707 /* log sense for fatal error */
2708 if (cqr->status == DASD_CQR_FAILED) {
2709 dasd_log_sense(cqr, &cqr->irb);
2710 }
2711
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002712 /* First of all call extended error reporting. */
2713 if (dasd_eer_enabled(base) &&
2714 cqr->status == DASD_CQR_FAILED) {
2715 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2716
2717 /* restart request */
2718 cqr->status = DASD_CQR_FILLED;
2719 cqr->retries = 255;
2720 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002721 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002722 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2723 flags);
2724 goto restart;
2725 }
2726
2727 /* Process finished ERP request. */
2728 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002729 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002730 goto restart;
2731 }
2732
2733 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002734 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002735 list_move_tail(&cqr->blocklist, final_queue);
2736 }
2737}
2738
2739static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2740{
2741 dasd_schedule_block_bh(cqr->block);
2742}
2743
2744static void __dasd_block_start_head(struct dasd_block *block)
2745{
2746 struct dasd_ccw_req *cqr;
2747
2748 if (list_empty(&block->ccw_queue))
2749 return;
2750 /* We allways begin with the first requests on the queue, as some
2751 * of previously started requests have to be enqueued on a
2752 * dasd_device again for error recovery.
2753 */
2754 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2755 if (cqr->status != DASD_CQR_FILLED)
2756 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002757 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2758 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2759 cqr->status = DASD_CQR_FAILED;
2760 cqr->intrc = -EPERM;
2761 dasd_schedule_block_bh(block);
2762 continue;
2763 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002764 /* Non-temporary stop condition will trigger fail fast */
2765 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2766 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2767 (!dasd_eer_enabled(block->base))) {
2768 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002769 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002770 dasd_schedule_block_bh(block);
2771 continue;
2772 }
2773 /* Don't try to start requests if device is stopped */
2774 if (block->base->stopped)
2775 return;
2776
2777 /* just a fail safe check, should not happen */
2778 if (!cqr->startdev)
2779 cqr->startdev = block->base;
2780
2781 /* make sure that the requests we submit find their way back */
2782 cqr->callback = dasd_return_cqr_cb;
2783
2784 dasd_add_request_tail(cqr);
2785 }
2786}
2787
2788/*
2789 * Central dasd_block layer routine. Takes requests from the generic
2790 * block layer request queue, creates ccw requests, enqueues them on
2791 * a dasd_device and processes ccw requests that have been returned.
2792 */
2793static void dasd_block_tasklet(struct dasd_block *block)
2794{
2795 struct list_head final_queue;
2796 struct list_head *l, *n;
2797 struct dasd_ccw_req *cqr;
2798
2799 atomic_set(&block->tasklet_scheduled, 0);
2800 INIT_LIST_HEAD(&final_queue);
2801 spin_lock(&block->queue_lock);
2802 /* Finish off requests on ccw queue */
2803 __dasd_process_block_ccw_queue(block, &final_queue);
2804 spin_unlock(&block->queue_lock);
2805 /* Now call the callback function of requests with final status */
2806 spin_lock_irq(&block->request_queue_lock);
2807 list_for_each_safe(l, n, &final_queue) {
2808 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2809 list_del_init(&cqr->blocklist);
2810 __dasd_cleanup_cqr(cqr);
2811 }
2812 spin_lock(&block->queue_lock);
2813 /* Get new request from the block device request queue */
2814 __dasd_process_request_queue(block);
2815 /* Now check if the head of the ccw queue needs to be started. */
2816 __dasd_block_start_head(block);
2817 spin_unlock(&block->queue_lock);
2818 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002819 if (waitqueue_active(&shutdown_waitq))
2820 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002821 dasd_put_device(block->base);
2822}
2823
2824static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2825{
2826 wake_up(&dasd_flush_wq);
2827}
2828
2829/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002830 * Requeue a request back to the block request queue
2831 * only works for block requests
2832 */
2833static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2834{
2835 struct dasd_block *block = cqr->block;
2836 struct request *req;
2837 unsigned long flags;
2838
2839 if (!block)
2840 return -EINVAL;
2841 spin_lock_irqsave(&block->queue_lock, flags);
2842 req = (struct request *) cqr->callback_data;
2843 blk_requeue_request(block->request_queue, req);
2844 spin_unlock_irqrestore(&block->queue_lock, flags);
2845
2846 return 0;
2847}
2848
2849/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002850 * Go through all request on the dasd_block request queue, cancel them
2851 * on the respective dasd_device, and return them to the generic
2852 * block layer.
2853 */
2854static int dasd_flush_block_queue(struct dasd_block *block)
2855{
2856 struct dasd_ccw_req *cqr, *n;
2857 int rc, i;
2858 struct list_head flush_queue;
2859
2860 INIT_LIST_HEAD(&flush_queue);
2861 spin_lock_bh(&block->queue_lock);
2862 rc = 0;
2863restart:
2864 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2865 /* if this request currently owned by a dasd_device cancel it */
2866 if (cqr->status >= DASD_CQR_QUEUED)
2867 rc = dasd_cancel_req(cqr);
2868 if (rc < 0)
2869 break;
2870 /* Rechain request (including erp chain) so it won't be
2871 * touched by the dasd_block_tasklet anymore.
2872 * Replace the callback so we notice when the request
2873 * is returned from the dasd_device layer.
2874 */
2875 cqr->callback = _dasd_wake_block_flush_cb;
2876 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2877 list_move_tail(&cqr->blocklist, &flush_queue);
2878 if (i > 1)
2879 /* moved more than one request - need to restart */
2880 goto restart;
2881 }
2882 spin_unlock_bh(&block->queue_lock);
2883 /* Now call the callback function of flushed requests */
2884restart_cb:
2885 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2886 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2887 /* Process finished ERP request. */
2888 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002889 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002890 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002891 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002892 /* restart list_for_xx loop since dasd_process_erp
2893 * might remove multiple elements */
2894 goto restart_cb;
2895 }
2896 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002897 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002898 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002899 list_del_init(&cqr->blocklist);
2900 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002901 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return rc;
2904}
2905
2906/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002907 * Schedules a call to dasd_tasklet over the device tasklet.
2908 */
2909void dasd_schedule_block_bh(struct dasd_block *block)
2910{
2911 /* Protect against rescheduling. */
2912 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2913 return;
2914 /* life cycle of block is bound to it's base device */
2915 dasd_get_device(block->base);
2916 tasklet_hi_schedule(&block->tasklet);
2917}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002918EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002919
2920
2921/*
2922 * SECTION: external block device operations
2923 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
2925
2926/*
2927 * Dasd request queue function. Called from ll_rw_blk.c
2928 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002929static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002931 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002933 block = queue->queuedata;
2934 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002936 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002938 __dasd_block_start_head(block);
2939 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
2942/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002943 * Block timeout callback, called from the block layer
2944 *
2945 * request_queue lock is held on entry.
2946 *
2947 * Return values:
2948 * BLK_EH_RESET_TIMER if the request should be left running
2949 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2950 * by the driver.
2951 */
2952enum blk_eh_timer_return dasd_times_out(struct request *req)
2953{
2954 struct dasd_ccw_req *cqr = req->completion_data;
2955 struct dasd_block *block = req->q->queuedata;
2956 struct dasd_device *device;
2957 int rc = 0;
2958
2959 if (!cqr)
2960 return BLK_EH_NOT_HANDLED;
2961
2962 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002963 if (!device->blk_timeout)
2964 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002965 DBF_DEV_EVENT(DBF_WARNING, device,
2966 " dasd_times_out cqr %p status %x",
2967 cqr, cqr->status);
2968
2969 spin_lock(&block->queue_lock);
2970 spin_lock(get_ccwdev_lock(device->cdev));
2971 cqr->retries = -1;
2972 cqr->intrc = -ETIMEDOUT;
2973 if (cqr->status >= DASD_CQR_QUEUED) {
2974 spin_unlock(get_ccwdev_lock(device->cdev));
2975 rc = dasd_cancel_req(cqr);
2976 } else if (cqr->status == DASD_CQR_FILLED ||
2977 cqr->status == DASD_CQR_NEED_ERP) {
2978 cqr->status = DASD_CQR_TERMINATED;
2979 spin_unlock(get_ccwdev_lock(device->cdev));
2980 } else if (cqr->status == DASD_CQR_IN_ERP) {
2981 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2982
2983 list_for_each_entry_safe(searchcqr, nextcqr,
2984 &block->ccw_queue, blocklist) {
2985 tmpcqr = searchcqr;
2986 while (tmpcqr->refers)
2987 tmpcqr = tmpcqr->refers;
2988 if (tmpcqr != cqr)
2989 continue;
2990 /* searchcqr is an ERP request for cqr */
2991 searchcqr->retries = -1;
2992 searchcqr->intrc = -ETIMEDOUT;
2993 if (searchcqr->status >= DASD_CQR_QUEUED) {
2994 spin_unlock(get_ccwdev_lock(device->cdev));
2995 rc = dasd_cancel_req(searchcqr);
2996 spin_lock(get_ccwdev_lock(device->cdev));
2997 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2998 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2999 searchcqr->status = DASD_CQR_TERMINATED;
3000 rc = 0;
3001 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3002 /*
3003 * Shouldn't happen; most recent ERP
3004 * request is at the front of queue
3005 */
3006 continue;
3007 }
3008 break;
3009 }
3010 spin_unlock(get_ccwdev_lock(device->cdev));
3011 }
3012 dasd_schedule_block_bh(block);
3013 spin_unlock(&block->queue_lock);
3014
3015 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3016}
3017
3018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 * Allocate and initialize request queue and default I/O scheduler.
3020 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003021static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 int rc;
3024
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003025 block->request_queue = blk_init_queue(do_dasd_request,
3026 &block->request_queue_lock);
3027 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return -ENOMEM;
3029
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003030 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003032 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02003033 block->request_queue->elevator = NULL;
Heiko Carstensef089942013-10-31 13:24:28 +01003034 mutex_lock(&block->request_queue->sysfs_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003035 rc = elevator_init(block->request_queue, "deadline");
Heiko Carstensef089942013-10-31 13:24:28 +01003036 if (rc)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003037 blk_cleanup_queue(block->request_queue);
Heiko Carstensef089942013-10-31 13:24:28 +01003038 mutex_unlock(&block->request_queue->sysfs_lock);
3039 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042/*
3043 * Allocate and initialize request queue.
3044 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003045static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
3047 int max;
3048
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003049 if (block->base->features & DASD_FEATURE_USERAW) {
3050 /*
3051 * the max_blocks value for raw_track access is 256
3052 * it is higher than the native ECKD value because we
3053 * only need one ccw per track
3054 * so the max_hw_sectors are
3055 * 2048 x 512B = 1024kB = 16 tracks
3056 */
3057 max = 2048;
3058 } else {
3059 max = block->base->discipline->max_blocks << block->s2b_shift;
3060 }
3061 blk_queue_logical_block_size(block->request_queue,
3062 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003063 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003064 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003065 /* with page sized segments we can translate each segement into
3066 * one idaw/tidaw
3067 */
3068 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3069 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072/*
3073 * Deactivate and free request queue.
3074 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003075static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003077 if (block->request_queue) {
3078 blk_cleanup_queue(block->request_queue);
3079 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
3081}
3082
3083/*
3084 * Flush request on the request queue.
3085 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003086static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 struct request *req;
3089
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003090 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003092
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003093 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003094 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003095 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003096 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Al Viro57a7c0b2008-03-02 10:36:08 -05003099static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003101 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 int rc;
3103
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003104 base = dasd_device_from_gendisk(bdev->bd_disk);
3105 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003106 return -ENODEV;
3107
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003108 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003109 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 rc = -ENODEV;
3111 goto unlock;
3112 }
3113
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003114 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 rc = -EINVAL;
3116 goto unlock;
3117 }
3118
3119 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003120 dev_info(&base->cdev->dev,
3121 "Accessing the DASD failed because it is in "
3122 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 rc = -EPERM;
3124 goto out;
3125 }
3126
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003127 if (base->state <= DASD_STATE_BASIC) {
3128 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 " Cannot open unrecognized device");
3130 rc = -ENODEV;
3131 goto out;
3132 }
3133
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003134 if ((mode & FMODE_WRITE) &&
3135 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3136 (base->features & DASD_FEATURE_READONLY))) {
3137 rc = -EROFS;
3138 goto out;
3139 }
3140
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003141 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return 0;
3143
3144out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003145 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003147 atomic_dec(&base->block->open_count);
3148 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return rc;
3150}
3151
Al Virodb2a1442013-05-05 21:52:57 -04003152static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Al Virodb2a1442013-05-05 21:52:57 -04003154 struct dasd_device *base = dasd_device_from_gendisk(disk);
3155 if (base) {
3156 atomic_dec(&base->block->open_count);
3157 module_put(base->discipline->owner);
3158 dasd_put_device(base);
3159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003162/*
3163 * Return disk geometry.
3164 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003165static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003166{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003167 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003168
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003169 base = dasd_device_from_gendisk(bdev->bd_disk);
3170 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003171 return -ENODEV;
3172
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003173 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003174 !base->discipline->fill_geometry) {
3175 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003176 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003177 }
3178 base->discipline->fill_geometry(base->block, geo);
3179 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3180 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003181 return 0;
3182}
3183
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003184const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185dasd_device_operations = {
3186 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003187 .open = dasd_open,
3188 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003189 .ioctl = dasd_ioctl,
3190 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003191 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192};
3193
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003194/*******************************************************************************
3195 * end of block device operations
3196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198static void
3199dasd_exit(void)
3200{
3201#ifdef CONFIG_PROC_FS
3202 dasd_proc_exit();
3203#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003204 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003205 if (dasd_page_cache != NULL) {
3206 kmem_cache_destroy(dasd_page_cache);
3207 dasd_page_cache = NULL;
3208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 dasd_gendisk_exit();
3210 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (dasd_debug_area != NULL) {
3212 debug_unregister(dasd_debug_area);
3213 dasd_debug_area = NULL;
3214 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003215 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
3218/*
3219 * SECTION: common functions for ccw_driver use
3220 */
3221
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003222/*
3223 * Is the device read-only?
3224 * Note that this function does not report the setting of the
3225 * readonly device attribute, but how it is configured in z/VM.
3226 */
3227int dasd_device_is_ro(struct dasd_device *device)
3228{
3229 struct ccw_dev_id dev_id;
3230 struct diag210 diag_data;
3231 int rc;
3232
3233 if (!MACHINE_IS_VM)
3234 return 0;
3235 ccw_device_get_id(device->cdev, &dev_id);
3236 memset(&diag_data, 0, sizeof(diag_data));
3237 diag_data.vrdcdvno = dev_id.devno;
3238 diag_data.vrdclen = sizeof(diag_data);
3239 rc = diag210(&diag_data);
3240 if (rc == 0 || rc == 2) {
3241 return diag_data.vrdcvfla & 0x80;
3242 } else {
3243 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3244 dev_id.devno, rc);
3245 return 0;
3246 }
3247}
3248EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3249
Cornelia Huckf3445a12009-04-14 15:36:23 +02003250static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3251{
3252 struct ccw_device *cdev = data;
3253 int ret;
3254
3255 ret = ccw_device_set_online(cdev);
3256 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003257 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3258 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003259}
3260
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003261/*
3262 * Initial attempt at a probe function. this can be simplified once
3263 * the other detection code is gone.
3264 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003265int dasd_generic_probe(struct ccw_device *cdev,
3266 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
3268 int ret;
3269
3270 ret = dasd_add_sysfs_files(cdev);
3271 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003272 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3273 "dasd_generic_probe: could not add "
3274 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
Horst Hummel40545572006-06-29 15:08:18 +02003277 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Horst Hummel40545572006-06-29 15:08:18 +02003279 /*
3280 * Automatically online either all dasd devices (dasd_autodetect)
3281 * or all devices specified with dasd= parameters during
3282 * initial probe.
3283 */
3284 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003285 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003286 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003289EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003291/*
3292 * This will one day be called from a global not_oper handler.
3293 * It is also used by driver_unregister during module unload.
3294 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003295void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003298 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Horst Hummel59afda72005-05-16 21:53:39 -07003300 cdev->handler = NULL;
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003303 if (IS_ERR(device)) {
3304 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003306 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003307 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3308 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* Already doing offline processing */
3310 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003311 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 return;
3313 }
3314 /*
3315 * This device is removed unconditionally. Set offline
3316 * flag to prevent dasd_open from opening it while it is
3317 * no quite down yet.
3318 */
3319 dasd_set_target_state(device, DASD_STATE_NEW);
3320 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003321 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003323 /*
3324 * life cycle of block is bound to device, so delete it after
3325 * device was safely removed
3326 */
3327 if (block)
3328 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003329
3330 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003332EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003334/*
3335 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003337 * or the user has started activation through sysfs.
3338 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003339int dasd_generic_set_online(struct ccw_device *cdev,
3340 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003342 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003344 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003345
Horst Hummel40545572006-06-29 15:08:18 +02003346 /* first online clears initial online feature flag */
3347 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 device = dasd_create_device(cdev);
3349 if (IS_ERR(device))
3350 return PTR_ERR(device);
3351
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003352 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003353 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003355 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3356 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 dasd_delete_device(device);
3358 return -ENODEV;
3359 }
3360 discipline = dasd_diag_discipline_pointer;
3361 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003362 if (!try_module_get(base_discipline->owner)) {
3363 dasd_delete_device(device);
3364 return -EINVAL;
3365 }
3366 if (!try_module_get(discipline->owner)) {
3367 module_put(base_discipline->owner);
3368 dasd_delete_device(device);
3369 return -EINVAL;
3370 }
3371 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 device->discipline = discipline;
3373
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003374 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 rc = discipline->check_device(device);
3376 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003377 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3378 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003379 module_put(discipline->owner);
3380 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 dasd_delete_device(device);
3382 return rc;
3383 }
3384
3385 dasd_set_target_state(device, DASD_STATE_ONLINE);
3386 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003387 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3388 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 rc = -ENODEV;
3390 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003391 if (device->block)
3392 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 dasd_delete_device(device);
3394 } else
3395 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003396 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003397
3398 wait_event(dasd_init_waitq, _wait_for_device(device));
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return rc;
3402}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003403EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003405int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003408 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003409 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003411 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 device = dasd_device_from_cdev(cdev);
3413 if (IS_ERR(device))
3414 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 /*
3417 * We must make sure that this device is currently not in use.
3418 * The open_count is increased for every opener, that includes
3419 * the blkdev_get in dasd_scan_partitions. We are only interested
3420 * in the other openers.
3421 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003422 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003423 max_count = device->block->bdev ? 0 : -1;
3424 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003425 if (open_count > max_count) {
3426 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003427 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3428 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003429 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003430 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3431 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003432 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3433 dasd_put_device(device);
3434 return -EBUSY;
3435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003437
3438 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3439 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003440 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003441 * could only be called by normal offline so safe_offline flag
3442 * needs to be removed to run normal offline and kill all I/O
3443 */
3444 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3445 /* Already doing normal offline processing */
3446 dasd_put_device(device);
3447 return -EBUSY;
3448 } else
3449 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3450
3451 } else
3452 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3453 /* Already doing offline processing */
3454 dasd_put_device(device);
3455 return -EBUSY;
3456 }
3457
3458 /*
3459 * if safe_offline called set safe_offline_running flag and
3460 * clear safe_offline so that a call to normal offline
3461 * can overrun safe_offline processing
3462 */
3463 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3464 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3465 /*
3466 * If we want to set the device safe offline all IO operations
3467 * should be finished before continuing the offline process
3468 * so sync bdev first and then wait for our queues to become
3469 * empty
3470 */
3471 /* sync blockdev and partitions */
3472 rc = fsync_bdev(device->block->bdev);
3473 if (rc != 0)
3474 goto interrupted;
3475
3476 /* schedule device tasklet and wait for completion */
3477 dasd_schedule_device_bh(device);
3478 rc = wait_event_interruptible(shutdown_waitq,
3479 _wait_for_empty_queues(device));
3480 if (rc != 0)
3481 goto interrupted;
3482 }
3483
3484 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 dasd_set_target_state(device, DASD_STATE_NEW);
3486 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003487 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003489 /*
3490 * life cycle of block is bound to device, so delete it after
3491 * device was safely removed
3492 */
3493 if (block)
3494 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003496
3497interrupted:
3498 /* interrupted by signal */
3499 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3500 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3501 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3502 dasd_put_device(device);
3503 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003505EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003507int dasd_generic_last_path_gone(struct dasd_device *device)
3508{
3509 struct dasd_ccw_req *cqr;
3510
3511 dev_warn(&device->cdev->dev, "No operational channel path is left "
3512 "for the device\n");
3513 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3514 /* First of all call extended error reporting. */
3515 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3516
3517 if (device->state < DASD_STATE_BASIC)
3518 return 0;
3519 /* Device is active. We want to keep it. */
3520 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3521 if ((cqr->status == DASD_CQR_IN_IO) ||
3522 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3523 cqr->status = DASD_CQR_QUEUED;
3524 cqr->retries++;
3525 }
3526 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3527 dasd_device_clear_timer(device);
3528 dasd_schedule_device_bh(device);
3529 return 1;
3530}
3531EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3532
3533int dasd_generic_path_operational(struct dasd_device *device)
3534{
3535 dev_info(&device->cdev->dev, "A channel path to the device has become "
3536 "operational\n");
3537 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3538 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3539 if (device->stopped & DASD_UNRESUMED_PM) {
3540 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3541 dasd_restore_device(device);
3542 return 1;
3543 }
3544 dasd_schedule_device_bh(device);
3545 if (device->block)
3546 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003547
3548 if (!device->stopped)
3549 wake_up(&generic_waitq);
3550
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003551 return 1;
3552}
3553EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3554
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003555int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 int ret;
3559
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003560 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 if (IS_ERR(device))
3562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 ret = 0;
3564 switch (event) {
3565 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003566 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003568 device->path_data.opm = 0;
3569 device->path_data.ppm = 0;
3570 device->path_data.npm = 0;
3571 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 break;
3573 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003575 if (device->path_data.opm)
3576 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 break;
3578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 dasd_put_device(device);
3580 return ret;
3581}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003582EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003584void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3585{
3586 int chp;
3587 __u8 oldopm, eventlpm;
3588 struct dasd_device *device;
3589
3590 device = dasd_device_from_cdev_locked(cdev);
3591 if (IS_ERR(device))
3592 return;
3593 for (chp = 0; chp < 8; chp++) {
3594 eventlpm = 0x80 >> chp;
3595 if (path_event[chp] & PE_PATH_GONE) {
3596 oldopm = device->path_data.opm;
3597 device->path_data.opm &= ~eventlpm;
3598 device->path_data.ppm &= ~eventlpm;
3599 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003600 if (oldopm && !device->path_data.opm) {
3601 dev_warn(&device->cdev->dev,
3602 "No verified channel paths remain "
3603 "for the device\n");
3604 DBF_DEV_EVENT(DBF_WARNING, device,
3605 "%s", "last verified path gone");
3606 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3607 dasd_device_set_stop_bits(device,
3608 DASD_STOPPED_DC_WAIT);
3609 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003610 }
3611 if (path_event[chp] & PE_PATH_AVAILABLE) {
3612 device->path_data.opm &= ~eventlpm;
3613 device->path_data.ppm &= ~eventlpm;
3614 device->path_data.npm &= ~eventlpm;
3615 device->path_data.tbvpm |= eventlpm;
3616 dasd_schedule_device_bh(device);
3617 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003618 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003619 if (!(device->path_data.opm & eventlpm) &&
3620 !(device->path_data.tbvpm & eventlpm)) {
3621 /*
3622 * we can not establish a pathgroup on an
3623 * unavailable path, so trigger a path
3624 * verification first
3625 */
3626 device->path_data.tbvpm |= eventlpm;
3627 dasd_schedule_device_bh(device);
3628 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003629 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3630 "Pathgroup re-established\n");
3631 if (device->discipline->kick_validate)
3632 device->discipline->kick_validate(device);
3633 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003634 }
3635 dasd_put_device(device);
3636}
3637EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3638
3639int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3640{
3641 if (!device->path_data.opm && lpm) {
3642 device->path_data.opm = lpm;
3643 dasd_generic_path_operational(device);
3644 } else
3645 device->path_data.opm |= lpm;
3646 return 0;
3647}
3648EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3649
3650
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003651int dasd_generic_pm_freeze(struct ccw_device *cdev)
3652{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003653 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003654 struct list_head freeze_queue;
3655 struct dasd_ccw_req *cqr, *n;
3656 struct dasd_ccw_req *refers;
3657 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003658
3659 if (IS_ERR(device))
3660 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003661
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003662 /* mark device as suspended */
3663 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3664
Stefan Haberland6f272b92011-01-05 12:48:05 +01003665 if (device->discipline->freeze)
3666 rc = device->discipline->freeze(device);
3667
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003668 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003669 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003670
3671 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003672 INIT_LIST_HEAD(&freeze_queue);
3673 spin_lock_irq(get_ccwdev_lock(cdev));
3674 rc = 0;
3675 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3676 /* Check status and move request to flush_queue */
3677 if (cqr->status == DASD_CQR_IN_IO) {
3678 rc = device->discipline->term_IO(cqr);
3679 if (rc) {
3680 /* unable to terminate requeust */
3681 dev_err(&device->cdev->dev,
3682 "Unable to terminate request %p "
3683 "on suspend\n", cqr);
3684 spin_unlock_irq(get_ccwdev_lock(cdev));
3685 dasd_put_device(device);
3686 return rc;
3687 }
3688 }
3689 list_move_tail(&cqr->devlist, &freeze_queue);
3690 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003691 spin_unlock_irq(get_ccwdev_lock(cdev));
3692
3693 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3694 wait_event(dasd_flush_wq,
3695 (cqr->status != DASD_CQR_CLEAR_PENDING));
3696 if (cqr->status == DASD_CQR_CLEARED)
3697 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003698
Stefan Haberlandc5576872013-04-15 16:41:31 +02003699 /* requeue requests to blocklayer will only work for
3700 block device requests */
3701 if (_dasd_requeue_request(cqr))
3702 continue;
3703
3704 /* remove requests from device and block queue */
3705 list_del_init(&cqr->devlist);
3706 while (cqr->refers != NULL) {
3707 refers = cqr->refers;
3708 /* remove the request from the block queue */
3709 list_del(&cqr->blocklist);
3710 /* free the finished erp request */
3711 dasd_free_erp_request(cqr, cqr->memdev);
3712 cqr = refers;
3713 }
3714 if (cqr->block)
3715 list_del_init(&cqr->blocklist);
3716 cqr->block->base->discipline->free_cp(
3717 cqr, (struct request *) cqr->callback_data);
3718 }
3719
3720 /*
3721 * if requests remain then they are internal request
3722 * and go back to the device queue
3723 */
3724 if (!list_empty(&freeze_queue)) {
3725 /* move freeze_queue to start of the ccw_queue */
3726 spin_lock_irq(get_ccwdev_lock(cdev));
3727 list_splice_tail(&freeze_queue, &device->ccw_queue);
3728 spin_unlock_irq(get_ccwdev_lock(cdev));
3729 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003730 dasd_put_device(device);
3731 return rc;
3732}
3733EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3734
3735int dasd_generic_restore_device(struct ccw_device *cdev)
3736{
3737 struct dasd_device *device = dasd_device_from_cdev(cdev);
3738 int rc = 0;
3739
3740 if (IS_ERR(device))
3741 return PTR_ERR(device);
3742
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003743 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003744 dasd_device_remove_stop_bits(device,
3745 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003746
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003747 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003748
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003749 /*
3750 * call discipline restore function
3751 * if device is stopped do nothing e.g. for disconnected devices
3752 */
3753 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003754 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003755 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003756 /*
3757 * if the resume failed for the DASD we put it in
3758 * an UNRESUMED stop state
3759 */
3760 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003761
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003762 if (device->block)
3763 dasd_schedule_block_bh(device->block);
3764
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003765 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003766 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003767 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003768}
3769EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3770
Heiko Carstens763968e2007-05-10 15:45:46 +02003771static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3772 void *rdc_buffer,
3773 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003774 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003775{
3776 struct dasd_ccw_req *cqr;
3777 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003778 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003779
3780 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3781
3782 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003783 /* internal error 13 - Allocating the RDC request failed*/
3784 dev_err(&device->cdev->dev,
3785 "An error occurred in the DASD device driver, "
3786 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003787 return cqr;
3788 }
3789
3790 ccw = cqr->cpaddr;
3791 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003792 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3793 idaw = (unsigned long *) (cqr->data);
3794 ccw->cda = (__u32)(addr_t) idaw;
3795 ccw->flags = CCW_FLAG_IDA;
3796 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3797 } else {
3798 ccw->cda = (__u32)(addr_t) rdc_buffer;
3799 ccw->flags = 0;
3800 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003801
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003802 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003803 cqr->startdev = device;
3804 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003805 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003806 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003807 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003808 cqr->status = DASD_CQR_FILLED;
3809 return cqr;
3810}
3811
3812
Stefan Haberland68b781f2009-09-11 10:28:29 +02003813int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003814 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003815{
3816 int ret;
3817 struct dasd_ccw_req *cqr;
3818
Sebastian Ott92636b12009-06-12 10:26:37 +02003819 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003820 magic);
3821 if (IS_ERR(cqr))
3822 return PTR_ERR(cqr);
3823
3824 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003825 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003826 return ret;
3827}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003828EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003829
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003830/*
3831 * In command mode and transport mode we need to look for sense
3832 * data in different places. The sense data itself is allways
3833 * an array of 32 bytes, so we can unify the sense data access
3834 * for both modes.
3835 */
3836char *dasd_get_sense(struct irb *irb)
3837{
3838 struct tsb *tsb = NULL;
3839 char *sense = NULL;
3840
3841 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3842 if (irb->scsw.tm.tcw)
3843 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3844 irb->scsw.tm.tcw);
3845 if (tsb && tsb->length == 64 && tsb->flags)
3846 switch (tsb->flags & 0x07) {
3847 case 1: /* tsa_iostat */
3848 sense = tsb->tsa.iostat.sense;
3849 break;
3850 case 2: /* tsa_ddpc */
3851 sense = tsb->tsa.ddpc.sense;
3852 break;
3853 default:
3854 /* currently we don't use interrogate data */
3855 break;
3856 }
3857 } else if (irb->esw.esw0.erw.cons) {
3858 sense = irb->ecw;
3859 }
3860 return sense;
3861}
3862EXPORT_SYMBOL_GPL(dasd_get_sense);
3863
Stefan Haberland4679e892012-06-19 17:30:12 +02003864void dasd_generic_shutdown(struct ccw_device *cdev)
3865{
3866 struct dasd_device *device;
3867
3868 device = dasd_device_from_cdev(cdev);
3869 if (IS_ERR(device))
3870 return;
3871
3872 if (device->block)
3873 dasd_schedule_block_bh(device->block);
3874
3875 dasd_schedule_device_bh(device);
3876
3877 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3878}
3879EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3880
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003881static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
3883 int rc;
3884
3885 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003886 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003887 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003888 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003891 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 if (dasd_debug_area == NULL) {
3893 rc = -ENOMEM;
3894 goto failed;
3895 }
3896 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003897 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
3899 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3900
3901 dasd_diag_discipline_pointer = NULL;
3902
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003903 dasd_statistics_createroot();
3904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 rc = dasd_devmap_init();
3906 if (rc)
3907 goto failed;
3908 rc = dasd_gendisk_init();
3909 if (rc)
3910 goto failed;
3911 rc = dasd_parse();
3912 if (rc)
3913 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003914 rc = dasd_eer_init();
3915 if (rc)
3916 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917#ifdef CONFIG_PROC_FS
3918 rc = dasd_proc_init();
3919 if (rc)
3920 goto failed;
3921#endif
3922
3923 return 0;
3924failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003925 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 dasd_exit();
3927 return rc;
3928}
3929
3930module_init(dasd_init);
3931module_exit(dasd_exit);