blob: 26a51dc4278da64381889e51e50dfd638818018a [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
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100677struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100678 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
679};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200680static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/*
683 * Add profiling information for cqr before execution.
684 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100685static void dasd_profile_start(struct dasd_block *block,
686 struct dasd_ccw_req *cqr,
687 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct list_head *l;
690 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200691 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* count the length of the chanq for statistics */
694 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200695 if (dasd_global_profile_level || block->profile.data)
696 list_for_each(l, &block->ccw_queue)
697 if (++counter >= 31)
698 break;
699
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100700 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100701 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100702 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200703 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100704 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200705 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100706 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200707
708 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200709 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200710 block->profile.data->dasd_io_nr_req[counter]++;
711 if (rq_data_dir(req) == READ)
712 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200713 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200714 spin_unlock(&block->profile.lock);
715
716 /*
717 * We count the request for the start device, even though it may run on
718 * some other device due to error recovery. This way we make sure that
719 * we count each request only once.
720 */
721 device = cqr->startdev;
722 if (device->profile.data) {
723 counter = 1; /* request is not yet queued on the start device */
724 list_for_each(l, &device->ccw_queue)
725 if (++counter >= 31)
726 break;
727 }
728 spin_lock(&device->profile.lock);
729 if (device->profile.data) {
730 device->profile.data->dasd_io_nr_req[counter]++;
731 if (rq_data_dir(req) == READ)
732 device->profile.data->dasd_read_nr_req[counter]++;
733 }
734 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/*
738 * Add profiling information for cqr after execution.
739 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200740
741#define dasd_profile_counter(value, index) \
742{ \
743 for (index = 0; index < 31 && value >> (2+index); index++) \
744 ; \
745}
746
747static void dasd_profile_end_add_data(struct dasd_profile_info *data,
748 int is_alias,
749 int is_tpm,
750 int is_read,
751 long sectors,
752 int sectors_ind,
753 int tottime_ind,
754 int tottimeps_ind,
755 int strtime_ind,
756 int irqtime_ind,
757 int irqtimeps_ind,
758 int endtime_ind)
759{
760 /* in case of an overflow, reset the whole profile */
761 if (data->dasd_io_reqs == UINT_MAX) {
762 memset(data, 0, sizeof(*data));
763 getnstimeofday(&data->starttod);
764 }
765 data->dasd_io_reqs++;
766 data->dasd_io_sects += sectors;
767 if (is_alias)
768 data->dasd_io_alias++;
769 if (is_tpm)
770 data->dasd_io_tpm++;
771
772 data->dasd_io_secs[sectors_ind]++;
773 data->dasd_io_times[tottime_ind]++;
774 data->dasd_io_timps[tottimeps_ind]++;
775 data->dasd_io_time1[strtime_ind]++;
776 data->dasd_io_time2[irqtime_ind]++;
777 data->dasd_io_time2ps[irqtimeps_ind]++;
778 data->dasd_io_time3[endtime_ind]++;
779
780 if (is_read) {
781 data->dasd_read_reqs++;
782 data->dasd_read_sects += sectors;
783 if (is_alias)
784 data->dasd_read_alias++;
785 if (is_tpm)
786 data->dasd_read_tpm++;
787 data->dasd_read_secs[sectors_ind]++;
788 data->dasd_read_times[tottime_ind]++;
789 data->dasd_read_time1[strtime_ind]++;
790 data->dasd_read_time2[irqtime_ind]++;
791 data->dasd_read_time3[endtime_ind]++;
792 }
793}
794
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100795static void dasd_profile_end(struct dasd_block *block,
796 struct dasd_ccw_req *cqr,
797 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 long strtime, irqtime, endtime, tottime; /* in microseconds */
800 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200801 struct dasd_device *device;
802 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
803 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200805 device = cqr->startdev;
806 if (!(dasd_global_profile_level ||
807 block->profile.data ||
808 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return;
810
Tejun Heo83096eb2009-05-07 22:24:39 +0900811 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!cqr->buildclk || !cqr->startclk ||
813 !cqr->stopclk || !cqr->endclk ||
814 !sectors)
815 return;
816
817 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
818 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
819 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
820 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
821 tottimeps = tottime / sectors;
822
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200823 dasd_profile_counter(sectors, sectors_ind);
824 dasd_profile_counter(tottime, tottime_ind);
825 dasd_profile_counter(tottimeps, tottimeps_ind);
826 dasd_profile_counter(strtime, strtime_ind);
827 dasd_profile_counter(irqtime, irqtime_ind);
828 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
829 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100831 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100832 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100833 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200834 cqr->startdev != block->base,
835 cqr->cpmode == 1,
836 rq_data_dir(req) == READ,
837 sectors, sectors_ind, tottime_ind,
838 tottimeps_ind, strtime_ind,
839 irqtime_ind, irqtimeps_ind,
840 endtime_ind);
841 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100842 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200844 spin_lock(&block->profile.lock);
845 if (block->profile.data)
846 dasd_profile_end_add_data(block->profile.data,
847 cqr->startdev != block->base,
848 cqr->cpmode == 1,
849 rq_data_dir(req) == READ,
850 sectors, sectors_ind, tottime_ind,
851 tottimeps_ind, strtime_ind,
852 irqtime_ind, irqtimeps_ind,
853 endtime_ind);
854 spin_unlock(&block->profile.lock);
855
856 spin_lock(&device->profile.lock);
857 if (device->profile.data)
858 dasd_profile_end_add_data(device->profile.data,
859 cqr->startdev != block->base,
860 cqr->cpmode == 1,
861 rq_data_dir(req) == READ,
862 sectors, sectors_ind, tottime_ind,
863 tottimeps_ind, strtime_ind,
864 irqtime_ind, irqtimeps_ind,
865 endtime_ind);
866 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200868
869void dasd_profile_reset(struct dasd_profile *profile)
870{
871 struct dasd_profile_info *data;
872
873 spin_lock_bh(&profile->lock);
874 data = profile->data;
875 if (!data) {
876 spin_unlock_bh(&profile->lock);
877 return;
878 }
879 memset(data, 0, sizeof(*data));
880 getnstimeofday(&data->starttod);
881 spin_unlock_bh(&profile->lock);
882}
883
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200884int dasd_profile_on(struct dasd_profile *profile)
885{
886 struct dasd_profile_info *data;
887
888 data = kzalloc(sizeof(*data), GFP_KERNEL);
889 if (!data)
890 return -ENOMEM;
891 spin_lock_bh(&profile->lock);
892 if (profile->data) {
893 spin_unlock_bh(&profile->lock);
894 kfree(data);
895 return 0;
896 }
897 getnstimeofday(&data->starttod);
898 profile->data = data;
899 spin_unlock_bh(&profile->lock);
900 return 0;
901}
902
903void dasd_profile_off(struct dasd_profile *profile)
904{
905 spin_lock_bh(&profile->lock);
906 kfree(profile->data);
907 profile->data = NULL;
908 spin_unlock_bh(&profile->lock);
909}
910
911char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
912{
913 char *buffer;
914
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200915 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200916 if (buffer == NULL)
917 return ERR_PTR(-ENOMEM);
918 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200919 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200920 return ERR_PTR(-EFAULT);
921 }
922 /* got the string, now strip linefeed. */
923 if (buffer[user_len - 1] == '\n')
924 buffer[user_len - 1] = 0;
925 else
926 buffer[user_len] = 0;
927 return buffer;
928}
929
930static ssize_t dasd_stats_write(struct file *file,
931 const char __user *user_buf,
932 size_t user_len, loff_t *pos)
933{
934 char *buffer, *str;
935 int rc;
936 struct seq_file *m = (struct seq_file *)file->private_data;
937 struct dasd_profile *prof = m->private;
938
939 if (user_len > 65536)
940 user_len = 65536;
941 buffer = dasd_get_user_string(user_buf, user_len);
942 if (IS_ERR(buffer))
943 return PTR_ERR(buffer);
944
945 str = skip_spaces(buffer);
946 rc = user_len;
947 if (strncmp(str, "reset", 5) == 0) {
948 dasd_profile_reset(prof);
949 } else if (strncmp(str, "on", 2) == 0) {
950 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100951 if (rc)
952 goto out;
953 rc = user_len;
954 if (prof == &dasd_global_profile) {
955 dasd_profile_reset(prof);
956 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
957 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200958 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100959 if (prof == &dasd_global_profile)
960 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200961 dasd_profile_off(prof);
962 } else
963 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100964out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200965 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200966 return rc;
967}
968
969static void dasd_stats_array(struct seq_file *m, unsigned int *array)
970{
971 int i;
972
973 for (i = 0; i < 32; i++)
974 seq_printf(m, "%u ", array[i]);
975 seq_putc(m, '\n');
976}
977
978static void dasd_stats_seq_print(struct seq_file *m,
979 struct dasd_profile_info *data)
980{
981 seq_printf(m, "start_time %ld.%09ld\n",
982 data->starttod.tv_sec, data->starttod.tv_nsec);
983 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
984 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
985 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
986 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200987 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200988 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200989 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200990 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200991 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200992 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200993 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200994 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200995 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200996 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200997 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200998 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200999 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001000 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001001 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001002 dasd_stats_array(m, data->dasd_io_nr_req);
1003 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1004 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1005 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1006 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001007 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001008 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001009 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001010 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001011 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001012 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001013 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001014 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001015 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001016 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001017 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001018 dasd_stats_array(m, data->dasd_read_nr_req);
1019}
1020
1021static int dasd_stats_show(struct seq_file *m, void *v)
1022{
1023 struct dasd_profile *profile;
1024 struct dasd_profile_info *data;
1025
1026 profile = m->private;
1027 spin_lock_bh(&profile->lock);
1028 data = profile->data;
1029 if (!data) {
1030 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001031 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001032 return 0;
1033 }
1034 dasd_stats_seq_print(m, data);
1035 spin_unlock_bh(&profile->lock);
1036 return 0;
1037}
1038
1039static int dasd_stats_open(struct inode *inode, struct file *file)
1040{
1041 struct dasd_profile *profile = inode->i_private;
1042 return single_open(file, dasd_stats_show, profile);
1043}
1044
1045static const struct file_operations dasd_stats_raw_fops = {
1046 .owner = THIS_MODULE,
1047 .open = dasd_stats_open,
1048 .read = seq_read,
1049 .llseek = seq_lseek,
1050 .release = single_release,
1051 .write = dasd_stats_write,
1052};
1053
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001054static void dasd_profile_init(struct dasd_profile *profile,
1055 struct dentry *base_dentry)
1056{
Al Virof4ae40a2011-07-24 04:33:43 -04001057 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001058 struct dentry *pde;
1059
1060 if (!base_dentry)
1061 return;
1062 profile->dentry = NULL;
1063 profile->data = NULL;
1064 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1065 pde = debugfs_create_file("statistics", mode, base_dentry,
1066 profile, &dasd_stats_raw_fops);
1067 if (pde && !IS_ERR(pde))
1068 profile->dentry = pde;
1069 return;
1070}
1071
1072static void dasd_profile_exit(struct dasd_profile *profile)
1073{
1074 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001075 debugfs_remove(profile->dentry);
1076 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001077}
1078
1079static void dasd_statistics_removeroot(void)
1080{
1081 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001082 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001083 debugfs_remove(dasd_debugfs_global_entry);
1084 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001085}
1086
1087static void dasd_statistics_createroot(void)
1088{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001089 struct dentry *pde;
1090
1091 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001092 pde = debugfs_create_dir("dasd", NULL);
1093 if (!pde || IS_ERR(pde))
1094 goto error;
1095 dasd_debugfs_root_entry = pde;
1096 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1097 if (!pde || IS_ERR(pde))
1098 goto error;
1099 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001100 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001101 return;
1102
1103error:
1104 DBF_EVENT(DBF_ERR, "%s",
1105 "Creation of the dasd debugfs interface failed");
1106 dasd_statistics_removeroot();
1107 return;
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001111#define dasd_profile_start(block, cqr, req) do {} while (0)
1112#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001113
1114static void dasd_statistics_createroot(void)
1115{
1116 return;
1117}
1118
1119static void dasd_statistics_removeroot(void)
1120{
1121 return;
1122}
1123
1124int dasd_stats_generic_show(struct seq_file *m, void *v)
1125{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001126 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001127 return 0;
1128}
1129
1130static void dasd_profile_init(struct dasd_profile *profile,
1131 struct dentry *base_dentry)
1132{
1133 return;
1134}
1135
1136static void dasd_profile_exit(struct dasd_profile *profile)
1137{
1138 return;
1139}
1140
1141int dasd_profile_on(struct dasd_profile *profile)
1142{
1143 return 0;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#endif /* CONFIG_DASD_PROFILE */
1147
1148/*
1149 * Allocate memory for a channel program with 'cplength' channel
1150 * command words and 'datasize' additional space. There are two
1151 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1152 * memory and 2) dasd_smalloc_request uses the static ccw memory
1153 * that gets allocated for each device.
1154 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001155struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001156 int datasize,
1157 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct dasd_ccw_req *cqr;
1160
1161 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001162 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001163 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001165 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (cqr == NULL)
1167 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 cqr->cpaddr = NULL;
1169 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001170 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 GFP_ATOMIC | GFP_DMA);
1172 if (cqr->cpaddr == NULL) {
1173 kfree(cqr);
1174 return ERR_PTR(-ENOMEM);
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 cqr->data = NULL;
1178 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001179 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001181 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 kfree(cqr);
1183 return ERR_PTR(-ENOMEM);
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001186 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1188 dasd_get_device(device);
1189 return cqr;
1190}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001191EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Stefan Haberland68b781f2009-09-11 10:28:29 +02001193struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001194 int datasize,
1195 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 unsigned long flags;
1198 struct dasd_ccw_req *cqr;
1199 char *data;
1200 int size;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1203 if (cplength > 0)
1204 size += cplength * sizeof(struct ccw1);
1205 if (datasize > 0)
1206 size += datasize;
1207 spin_lock_irqsave(&device->mem_lock, flags);
1208 cqr = (struct dasd_ccw_req *)
1209 dasd_alloc_chunk(&device->ccw_chunks, size);
1210 spin_unlock_irqrestore(&device->mem_lock, flags);
1211 if (cqr == NULL)
1212 return ERR_PTR(-ENOMEM);
1213 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1214 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1215 cqr->cpaddr = NULL;
1216 if (cplength > 0) {
1217 cqr->cpaddr = (struct ccw1 *) data;
1218 data += cplength*sizeof(struct ccw1);
1219 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1220 }
1221 cqr->data = NULL;
1222 if (datasize > 0) {
1223 cqr->data = data;
1224 memset(cqr->data, 0, datasize);
1225 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001226 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1228 dasd_get_device(device);
1229 return cqr;
1230}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001231EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233/*
1234 * Free memory of a channel program. This function needs to free all the
1235 * idal lists that might have been created by dasd_set_cda and the
1236 * struct dasd_ccw_req itself.
1237 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001238void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct ccw1 *ccw;
1241
1242 /* Clear any idals used for the request. */
1243 ccw = cqr->cpaddr;
1244 do {
1245 clear_normalized_cda(ccw);
1246 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
Jesper Juhl17fd6822005-11-07 01:01:30 -08001247 kfree(cqr->cpaddr);
1248 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 kfree(cqr);
1250 dasd_put_device(device);
1251}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001252EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001254void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 unsigned long flags;
1257
1258 spin_lock_irqsave(&device->mem_lock, flags);
1259 dasd_free_chunk(&device->ccw_chunks, cqr);
1260 spin_unlock_irqrestore(&device->mem_lock, flags);
1261 dasd_put_device(device);
1262}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001263EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265/*
1266 * Check discipline magic in cqr.
1267 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001268static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct dasd_device *device;
1271
1272 if (cqr == NULL)
1273 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001274 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001276 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 " dasd_ccw_req 0x%08x magic doesn't match"
1278 " discipline 0x%08x",
1279 cqr->magic,
1280 *(unsigned int *) device->discipline->name);
1281 return -EINVAL;
1282 }
1283 return 0;
1284}
1285
1286/*
1287 * Terminate the current i/o and set the request to clear_pending.
1288 * Timer keeps device runnig.
1289 * ccw_device_clear can fail if the i/o subsystem
1290 * is in a bad mood.
1291 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001292int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct dasd_device *device;
1295 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001296 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Check the cqr */
1299 rc = dasd_check_cqr(cqr);
1300 if (rc)
1301 return rc;
1302 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001303 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1305 rc = ccw_device_clear(device->cdev, (long) cqr);
1306 switch (rc) {
1307 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001308 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001309 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001310 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 DBF_DEV_EVENT(DBF_DEBUG, device,
1312 "terminate cqr %p successful",
1313 cqr);
1314 break;
1315 case -ENODEV:
1316 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1317 "device gone, retry");
1318 break;
1319 case -EIO:
1320 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1321 "I/O error, retry");
1322 break;
1323 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001324 /*
1325 * device not valid so no I/O could be running
1326 * handle CQR as termination successful
1327 */
1328 cqr->status = DASD_CQR_CLEARED;
1329 cqr->stopclk = get_tod_clock();
1330 cqr->starttime = 0;
1331 /* no retries for invalid devices */
1332 cqr->retries = -1;
1333 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1334 "EINVAL, handle as terminated");
1335 /* fake rc to success */
1336 rc = 0;
1337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 case -EBUSY:
1339 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1340 "device busy, retry later");
1341 break;
1342 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001343 /* internal error 10 - unknown rc*/
1344 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1345 dev_err(&device->cdev->dev, "An error occurred in the "
1346 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 BUG();
1348 break;
1349 }
1350 retries++;
1351 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001352 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return rc;
1354}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001355EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357/*
1358 * Start the i/o. This start_IO can fail if the channel is really busy.
1359 * In that case set up a timer to start the request later.
1360 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001361int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct dasd_device *device;
1364 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001365 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /* Check the cqr */
1368 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001369 if (rc) {
1370 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001372 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001373 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001374 if (((cqr->block &&
1375 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1376 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1377 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1378 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1379 "because of stolen lock", cqr);
1380 cqr->status = DASD_CQR_ERROR;
1381 cqr->intrc = -EPERM;
1382 return -EPERM;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001385 /* internal error 14 - start_IO run out of retries */
1386 sprintf(errorstring, "14 %p", cqr);
1387 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1388 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001389 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EIO;
1391 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001392 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 cqr->starttime = jiffies;
1394 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001395 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1396 cqr->lpm &= device->path_data.opm;
1397 if (!cqr->lpm)
1398 cqr->lpm = device->path_data.opm;
1399 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001400 if (cqr->cpmode == 1) {
1401 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1402 (long) cqr, cqr->lpm);
1403 } else {
1404 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1405 (long) cqr, cqr->lpm, 0);
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 switch (rc) {
1408 case 0:
1409 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001412 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 "start_IO: device busy, retry later");
1414 break;
1415 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001416 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 "start_IO: request timeout, retry later");
1418 break;
1419 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001420 /* -EACCES indicates that the request used only a subset of the
1421 * available paths and all these paths are gone. If the lpm of
1422 * this request was only a subset of the opm (e.g. the ppm) then
1423 * we just do a retry with all available paths.
1424 * If we already use the full opm, something is amiss, and we
1425 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001427 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1428 DBF_DEV_EVENT(DBF_WARNING, device,
1429 "start_IO: selected paths gone (%x)",
1430 cqr->lpm);
1431 } else if (cqr->lpm != device->path_data.opm) {
1432 cqr->lpm = device->path_data.opm;
1433 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1434 "start_IO: selected paths gone,"
1435 " retry on all paths");
1436 } else {
1437 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1438 "start_IO: all paths in opm gone,"
1439 " do path verification");
1440 dasd_generic_last_path_gone(device);
1441 device->path_data.opm = 0;
1442 device->path_data.ppm = 0;
1443 device->path_data.npm = 0;
1444 device->path_data.tbvpm =
1445 ccw_device_get_path_mask(device->cdev);
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001449 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001450 "start_IO: -ENODEV device gone, retry");
1451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001453 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001454 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001456 case -EINVAL:
1457 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001459 "start_IO: -EINVAL device currently "
1460 "not accessible");
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001463 /* internal error 11 - unknown rc */
1464 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1465 dev_err(&device->cdev->dev,
1466 "An error occurred in the DASD device driver, "
1467 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 BUG();
1469 break;
1470 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001471 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return rc;
1473}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001474EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476/*
1477 * Timeout function for dasd devices. This is used for different purposes
1478 * 1) missing interrupt handler for normal operation
1479 * 2) delayed start of request where start_IO failed with -EBUSY
1480 * 3) timeout for missing state change interrupts
1481 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1482 * DASD_CQR_QUEUED for 2) and 3).
1483 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001484static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 unsigned long flags;
1487 struct dasd_device *device;
1488
1489 device = (struct dasd_device *) ptr;
1490 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1491 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001492 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001494 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497/*
1498 * Setup timeout for a device in jiffies.
1499 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001500void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001502 if (expires == 0)
1503 del_timer(&device->timer);
1504 else
1505 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001507EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509/*
1510 * Clear timeout for a device.
1511 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001512void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001514 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001516EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518static void dasd_handle_killed_request(struct ccw_device *cdev,
1519 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct dasd_ccw_req *cqr;
1522 struct dasd_device *device;
1523
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001524 if (!intparm)
1525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 cqr = (struct dasd_ccw_req *) intparm;
1527 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001528 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1529 "invalid status in handle_killed_request: "
1530 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return;
1532 }
1533
Stefan Haberland589c74d2010-02-26 22:37:47 +01001534 device = dasd_device_from_cdev_locked(cdev);
1535 if (IS_ERR(device)) {
1536 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1537 "unable to get device from cdev");
1538 return;
1539 }
1540
1541 if (!cqr->startdev ||
1542 device != cqr->startdev ||
1543 strncmp(cqr->startdev->discipline->ebcname,
1544 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001545 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1546 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001547 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return;
1549 }
1550
1551 /* Schedule request to be retried. */
1552 cqr->status = DASD_CQR_QUEUED;
1553
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001554 dasd_device_clear_timer(device);
1555 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 dasd_put_device(device);
1557}
1558
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001559void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001561 /* First of all start sense subsystem status request. */
1562 dasd_eer_snss(device);
1563
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001564 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001565 dasd_schedule_device_bh(device);
1566 if (device->block)
1567 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001569EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571/*
1572 * Interrupt handler for "normal" ssch-io based dasd devices.
1573 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001574void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1575 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct dasd_ccw_req *cqr, *next;
1578 struct dasd_device *device;
1579 unsigned long long now;
1580 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (IS_ERR(irb)) {
1583 switch (PTR_ERR(irb)) {
1584 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 break;
1586 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001587 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1588 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 break;
1590 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001591 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1592 "unknown error %ld\n", __func__,
1593 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001595 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return;
1597 }
1598
Heiko Carstens1aae0562013-01-30 09:49:40 +01001599 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001600 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001601 /* check for conditions that should be handled immediately */
1602 if (!cqr ||
1603 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1604 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001605 if (cqr)
1606 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001607 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001608 if (IS_ERR(device))
1609 return;
1610 /* ignore unsolicited interrupts for DIAG discipline */
1611 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001613 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001615 device->discipline->dump_sense_dbf(device, irb, "int");
1616 if (device->features & DASD_FEATURE_ERPLOG)
1617 device->discipline->dump_sense(device, cqr, irb);
1618 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001619 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001621
1622 /* check for for attention message */
1623 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1624 device = dasd_device_from_cdev_locked(cdev);
1625 device->discipline->check_attention(device, irb->esw.esw1.lpum);
1626 dasd_put_device(device);
1627 }
1628
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001629 if (!cqr)
1630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001632 device = (struct dasd_device *) cqr->startdev;
1633 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001635 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1636 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return;
1638 }
1639
1640 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001641 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001642 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001643 cqr->status = DASD_CQR_CLEARED;
1644 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001645 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001646 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return;
1648 }
1649
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001650 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001652 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1653 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return;
1655 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001656
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001657 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001659 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1660 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001661 /* request was completed successfully */
1662 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 cqr->stopclk = now;
1664 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001665 if (cqr->devlist.next != &device->ccw_queue) {
1666 next = list_entry(cqr->devlist.next,
1667 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001669 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001670 /*
1671 * If we don't want complex ERP for this request, then just
1672 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001674 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001675 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001676 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001677 DBF_DEV_EVENT(DBF_DEBUG, device,
1678 "default ERP in fastpath "
1679 "(%i retries left)",
1680 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001681 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1682 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001683 cqr->status = DASD_CQR_QUEUED;
1684 next = cqr;
1685 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001687 }
1688 if (next && (next->status == DASD_CQR_QUEUED) &&
1689 (!device->stopped)) {
1690 if (device->discipline->start_IO(next) == 0)
1691 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001694 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001696 dasd_device_clear_timer(device);
1697 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001699EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001701enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1702{
1703 struct dasd_device *device;
1704
1705 device = dasd_device_from_cdev_locked(cdev);
1706
1707 if (IS_ERR(device))
1708 goto out;
1709 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1710 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001711 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001712 dasd_put_device(device);
1713 goto out;
1714 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001715 if (device->discipline->dump_sense_dbf)
1716 device->discipline->dump_sense_dbf(device, irb, "uc");
1717 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001718 dasd_put_device(device);
1719out:
1720 return UC_TODO_RETRY;
1721}
1722EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001725 * If we have an error on a dasd_block layer request then we cancel
1726 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001728static void __dasd_device_recovery(struct dasd_device *device,
1729 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct list_head *l, *n;
1732 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001735 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001737 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001739
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 list_for_each_safe(l, n, &device->ccw_queue) {
1741 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1742 if (cqr->status == DASD_CQR_QUEUED &&
1743 ref_cqr->block == cqr->block) {
1744 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001746 }
1747};
1748
1749/*
1750 * Remove those ccw requests from the queue that need to be returned
1751 * to the upper layer.
1752 */
1753static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1754 struct list_head *final_queue)
1755{
1756 struct list_head *l, *n;
1757 struct dasd_ccw_req *cqr;
1758
1759 /* Process request with final status. */
1760 list_for_each_safe(l, n, &device->ccw_queue) {
1761 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1762
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001763 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001764 if (cqr->status == DASD_CQR_QUEUED ||
1765 cqr->status == DASD_CQR_IN_IO ||
1766 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001767 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001768 if (cqr->status == DASD_CQR_ERROR) {
1769 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001771 /* Rechain finished requests to final queue */
1772 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774}
1775
1776/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001777 * the cqrs from the final queue are returned to the upper layer
1778 * by setting a dasd_block state and calling the callback function
1779 */
1780static void __dasd_device_process_final_queue(struct dasd_device *device,
1781 struct list_head *final_queue)
1782{
1783 struct list_head *l, *n;
1784 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001785 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001786 void (*callback)(struct dasd_ccw_req *, void *data);
1787 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001788 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001789
1790 list_for_each_safe(l, n, final_queue) {
1791 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1792 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001793 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001794 callback = cqr->callback;
1795 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001796 if (block)
1797 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001798 switch (cqr->status) {
1799 case DASD_CQR_SUCCESS:
1800 cqr->status = DASD_CQR_DONE;
1801 break;
1802 case DASD_CQR_ERROR:
1803 cqr->status = DASD_CQR_NEED_ERP;
1804 break;
1805 case DASD_CQR_CLEARED:
1806 cqr->status = DASD_CQR_TERMINATED;
1807 break;
1808 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001809 /* internal error 12 - wrong cqr status*/
1810 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1811 dev_err(&device->cdev->dev,
1812 "An error occurred in the DASD device driver, "
1813 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001814 BUG();
1815 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001816 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001817 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001818 if (block)
1819 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001820 }
1821}
1822
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001823/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 * Take a look at the first request on the ccw queue and check
1825 * if it reached its expire time. If so, terminate the IO.
1826 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001827static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 struct dasd_ccw_req *cqr;
1830
1831 if (list_empty(&device->ccw_queue))
1832 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001833 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001834 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1835 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001836 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1837 /*
1838 * IO in safe offline processing should not
1839 * run out of retries
1840 */
1841 cqr->retries++;
1842 }
Horst Hummel29145a62006-12-04 15:40:15 +01001843 if (device->discipline->term_IO(cqr) != 0) {
1844 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001845 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001846 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001847 "ended, retrying in 5 s\n",
1848 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001849 cqr->expires += 5*HZ;
1850 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001851 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001852 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001853 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001854 "remaining\n", cqr, (cqr->expires/HZ),
1855 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 }
1858}
1859
1860/*
1861 * Take a look at the first request on the ccw queue and check
1862 * if it needs to be started.
1863 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001864static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct dasd_ccw_req *cqr;
1867 int rc;
1868
1869 if (list_empty(&device->ccw_queue))
1870 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001871 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001872 if (cqr->status != DASD_CQR_QUEUED)
1873 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001874 /* when device is stopped, return request to previous layer
1875 * exception: only the disconnect or unresumed bits are set and the
1876 * cqr is a path verification request
1877 */
1878 if (device->stopped &&
1879 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1880 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1881 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001882 cqr->status = DASD_CQR_CLEARED;
1883 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001884 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001885 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001886
1887 rc = device->discipline->start_IO(cqr);
1888 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001889 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001890 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001891 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001892 } else
1893 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001894 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001895}
1896
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001897static void __dasd_device_check_path_events(struct dasd_device *device)
1898{
1899 int rc;
1900
1901 if (device->path_data.tbvpm) {
1902 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1903 DASD_UNRESUMED_PM))
1904 return;
1905 rc = device->discipline->verify_path(
1906 device, device->path_data.tbvpm);
1907 if (rc)
1908 dasd_device_set_timer(device, 50);
1909 else
1910 device->path_data.tbvpm = 0;
1911 }
1912};
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001915 * Go through all request on the dasd_device request queue,
1916 * terminate them on the cdev if necessary, and return them to the
1917 * submitting layer via callback.
1918 * Note:
1919 * Make sure that all 'submitting layers' still exist when
1920 * this function is called!. In other words, when 'device' is a base
1921 * device then all block layer requests must have been removed before
1922 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001924int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001926 struct dasd_ccw_req *cqr, *n;
1927 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 INIT_LIST_HEAD(&flush_queue);
1931 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001932 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001933 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001934 /* Check status and move request to flush_queue */
1935 switch (cqr->status) {
1936 case DASD_CQR_IN_IO:
1937 rc = device->discipline->term_IO(cqr);
1938 if (rc) {
1939 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001940 dev_err(&device->cdev->dev,
1941 "Flushing the DASD request queue "
1942 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001943 /* stop flush processing */
1944 goto finished;
1945 }
1946 break;
1947 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001948 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001950 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001951 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001952 break;
1953 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001955 }
Horst Hummel8f617012006-08-30 14:33:33 +02001956finished:
1957 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001958 /*
1959 * After this point all requests must be in state CLEAR_PENDING,
1960 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1961 * one of the others.
1962 */
1963 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1964 wait_event(dasd_flush_wq,
1965 (cqr->status != DASD_CQR_CLEAR_PENDING));
1966 /*
1967 * Now set each request back to TERMINATED, DONE or NEED_ERP
1968 * and call the callback function of flushed requests
1969 */
1970 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001971 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001973EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975/*
1976 * Acquire the device lock and process queues for the device.
1977 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001978static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 atomic_set (&device->tasklet_scheduled, 0);
1983 INIT_LIST_HEAD(&final_queue);
1984 spin_lock_irq(get_ccwdev_lock(device->cdev));
1985 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001986 __dasd_device_check_expire(device);
1987 /* find final requests on ccw queue */
1988 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001989 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1991 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001992 __dasd_device_process_final_queue(device, &final_queue);
1993 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001995 __dasd_device_start_head(device);
1996 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02001997 if (waitqueue_active(&shutdown_waitq))
1998 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 dasd_put_device(device);
2000}
2001
2002/*
2003 * Schedules a call to dasd_tasklet over the device tasklet.
2004 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002005void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002008 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return;
2010 dasd_get_device(device);
2011 tasklet_hi_schedule(&device->tasklet);
2012}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002013EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002015void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2016{
2017 device->stopped |= bits;
2018}
2019EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2020
2021void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2022{
2023 device->stopped &= ~bits;
2024 if (!device->stopped)
2025 wake_up(&generic_waitq);
2026}
2027EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002030 * Queue a request to the head of the device ccw_queue.
2031 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002033void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct dasd_device *device;
2036 unsigned long flags;
2037
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002038 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002040 cqr->status = DASD_CQR_QUEUED;
2041 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002043 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2045}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002046EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002049 * Queue a request to the tail of the device ccw_queue.
2050 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct dasd_device *device;
2055 unsigned long flags;
2056
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002057 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002059 cqr->status = DASD_CQR_QUEUED;
2060 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2064}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002065EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002068 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002070void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002072 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2073 cqr->callback_data = DASD_SLEEPON_END_TAG;
2074 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2075 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
Stefan Haberland5915a872011-10-30 15:16:57 +01002077EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002079static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 struct dasd_device *device;
2082 int rc;
2083
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002086 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2088 return rc;
2089}
2090
2091/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002092 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2093 */
2094static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2095{
2096 struct dasd_device *device;
2097 dasd_erp_fn_t erp_fn;
2098
2099 if (cqr->status == DASD_CQR_FILLED)
2100 return 0;
2101 device = cqr->startdev;
2102 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2103 if (cqr->status == DASD_CQR_TERMINATED) {
2104 device->discipline->handle_terminated_request(cqr);
2105 return 1;
2106 }
2107 if (cqr->status == DASD_CQR_NEED_ERP) {
2108 erp_fn = device->discipline->erp_action(cqr);
2109 erp_fn(cqr);
2110 return 1;
2111 }
2112 if (cqr->status == DASD_CQR_FAILED)
2113 dasd_log_sense(cqr, &cqr->irb);
2114 if (cqr->refers) {
2115 __dasd_process_erp(device, cqr);
2116 return 1;
2117 }
2118 }
2119 return 0;
2120}
2121
2122static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2123{
2124 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2125 if (cqr->refers) /* erp is not done yet */
2126 return 1;
2127 return ((cqr->status != DASD_CQR_DONE) &&
2128 (cqr->status != DASD_CQR_FAILED));
2129 } else
2130 return (cqr->status == DASD_CQR_FILLED);
2131}
2132
2133static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2134{
2135 struct dasd_device *device;
2136 int rc;
2137 struct list_head ccw_queue;
2138 struct dasd_ccw_req *cqr;
2139
2140 INIT_LIST_HEAD(&ccw_queue);
2141 maincqr->status = DASD_CQR_FILLED;
2142 device = maincqr->startdev;
2143 list_add(&maincqr->blocklist, &ccw_queue);
2144 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2145 cqr = list_first_entry(&ccw_queue,
2146 struct dasd_ccw_req, blocklist)) {
2147
2148 if (__dasd_sleep_on_erp(cqr))
2149 continue;
2150 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2151 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002152 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2153 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2154 cqr->status = DASD_CQR_FAILED;
2155 cqr->intrc = -EPERM;
2156 continue;
2157 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002158 /* Non-temporary stop condition will trigger fail fast */
2159 if (device->stopped & ~DASD_STOPPED_PENDING &&
2160 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2161 (!dasd_eer_enabled(device))) {
2162 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002163 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002164 continue;
2165 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002166 /* Don't try to start requests if device is stopped */
2167 if (interruptible) {
2168 rc = wait_event_interruptible(
2169 generic_waitq, !(device->stopped));
2170 if (rc == -ERESTARTSYS) {
2171 cqr->status = DASD_CQR_FAILED;
2172 maincqr->intrc = rc;
2173 continue;
2174 }
2175 } else
2176 wait_event(generic_waitq, !(device->stopped));
2177
Stefan Haberland5915a872011-10-30 15:16:57 +01002178 if (!cqr->callback)
2179 cqr->callback = dasd_wakeup_cb;
2180
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002181 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002182 dasd_add_request_tail(cqr);
2183 if (interruptible) {
2184 rc = wait_event_interruptible(
2185 generic_waitq, _wait_for_wakeup(cqr));
2186 if (rc == -ERESTARTSYS) {
2187 dasd_cancel_req(cqr);
2188 /* wait (non-interruptible) for final status */
2189 wait_event(generic_waitq,
2190 _wait_for_wakeup(cqr));
2191 cqr->status = DASD_CQR_FAILED;
2192 maincqr->intrc = rc;
2193 continue;
2194 }
2195 } else
2196 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2197 }
2198
Heiko Carstens1aae0562013-01-30 09:49:40 +01002199 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002200 if ((maincqr->status != DASD_CQR_DONE) &&
2201 (maincqr->intrc != -ERESTARTSYS))
2202 dasd_log_sense(maincqr, &maincqr->irb);
2203 if (maincqr->status == DASD_CQR_DONE)
2204 rc = 0;
2205 else if (maincqr->intrc)
2206 rc = maincqr->intrc;
2207 else
2208 rc = -EIO;
2209 return rc;
2210}
2211
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002212static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2213{
2214 struct dasd_ccw_req *cqr;
2215
2216 list_for_each_entry(cqr, ccw_queue, blocklist) {
2217 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2218 return 0;
2219 }
2220
2221 return 1;
2222}
2223
2224static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2225{
2226 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002227 struct dasd_ccw_req *cqr, *n;
Stefan Haberland362ce842014-10-01 13:04:54 +02002228 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002229
2230retry:
2231 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2232 device = cqr->startdev;
2233 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2234 continue;
2235
2236 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2237 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2238 cqr->status = DASD_CQR_FAILED;
2239 cqr->intrc = -EPERM;
2240 continue;
2241 }
2242 /*Non-temporary stop condition will trigger fail fast*/
2243 if (device->stopped & ~DASD_STOPPED_PENDING &&
2244 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2245 !dasd_eer_enabled(device)) {
2246 cqr->status = DASD_CQR_FAILED;
2247 cqr->intrc = -EAGAIN;
2248 continue;
2249 }
2250
2251 /*Don't try to start requests if device is stopped*/
2252 if (interruptible) {
2253 rc = wait_event_interruptible(
2254 generic_waitq, !device->stopped);
2255 if (rc == -ERESTARTSYS) {
2256 cqr->status = DASD_CQR_FAILED;
2257 cqr->intrc = rc;
2258 continue;
2259 }
2260 } else
2261 wait_event(generic_waitq, !(device->stopped));
2262
2263 if (!cqr->callback)
2264 cqr->callback = dasd_wakeup_cb;
2265 cqr->callback_data = DASD_SLEEPON_START_TAG;
2266 dasd_add_request_tail(cqr);
2267 }
2268
2269 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2270
2271 rc = 0;
2272 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002273 /*
2274 * for alias devices simplify error recovery and
2275 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002276 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002277 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002278 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002279 (cqr->status == DASD_CQR_TERMINATED ||
2280 cqr->status == DASD_CQR_NEED_ERP))
2281 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002282
2283 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002284 if (__dasd_sleep_on_erp(cqr))
2285 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002286 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002287 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002288
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002289 return 0;
2290}
2291
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002292/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002293 * Queue a request to the tail of the device ccw_queue and wait for
2294 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002296int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002298 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002300EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002303 * Start requests from a ccw_queue and wait for their completion.
2304 */
2305int dasd_sleep_on_queue(struct list_head *ccw_queue)
2306{
2307 return _dasd_sleep_on_queue(ccw_queue, 0);
2308}
2309EXPORT_SYMBOL(dasd_sleep_on_queue);
2310
2311/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002312 * Queue a request to the tail of the device ccw_queue and wait
2313 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002315int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002317 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002319EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321/*
2322 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2323 * for eckd devices) the currently running request has to be terminated
2324 * and be put back to status queued, before the special request is added
2325 * to the head of the queue. Then the special request is waited on normally.
2326 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002327static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002330 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 if (list_empty(&device->ccw_queue))
2333 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002334 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002335 rc = device->discipline->term_IO(cqr);
2336 if (!rc)
2337 /*
2338 * CQR terminated because a more important request is pending.
2339 * Undo decreasing of retry counter because this is
2340 * not an error case.
2341 */
2342 cqr->retries++;
2343 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002346int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 struct dasd_device *device;
2349 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002350
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002351 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002352 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2353 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2354 cqr->status = DASD_CQR_FAILED;
2355 cqr->intrc = -EPERM;
2356 return -EIO;
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 spin_lock_irq(get_ccwdev_lock(device->cdev));
2359 rc = _dasd_term_running_cqr(device);
2360 if (rc) {
2361 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2362 return rc;
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002365 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002367 /*
2368 * add new request as second
2369 * first the terminated cqr needs to be finished
2370 */
2371 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002374 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2377
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002378 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002379
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002380 if (cqr->status == DASD_CQR_DONE)
2381 rc = 0;
2382 else if (cqr->intrc)
2383 rc = cqr->intrc;
2384 else
2385 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002386
2387 /* kick tasklets */
2388 dasd_schedule_device_bh(device);
2389 if (device->block)
2390 dasd_schedule_block_bh(device->block);
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return rc;
2393}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002394EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396/*
2397 * Cancels a request that was started with dasd_sleep_on_req.
2398 * This is useful to timeout requests. The request will be
2399 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002400 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002401 * negative error code if termination failed
2402 * Cancellation of a request is an asynchronous operation! The calling
2403 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002405int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002407 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 unsigned long flags;
2409 int rc;
2410
2411 rc = 0;
2412 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2413 switch (cqr->status) {
2414 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002415 /* request was not started - just set to cleared */
2416 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002417 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2418 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 break;
2420 case DASD_CQR_IN_IO:
2421 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002422 rc = device->discipline->term_IO(cqr);
2423 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002424 dev_err(&device->cdev->dev,
2425 "Cancelling request %p failed with rc=%d\n",
2426 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002427 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002428 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002431 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002435 dasd_schedule_device_bh(device);
2436 return rc;
2437}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002438EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002439
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002440/*
2441 * SECTION: Operations of the dasd_block layer.
2442 */
2443
2444/*
2445 * Timeout function for dasd_block. This is used when the block layer
2446 * is waiting for something that may not come reliably, (e.g. a state
2447 * change interrupt)
2448 */
2449static void dasd_block_timeout(unsigned long ptr)
2450{
2451 unsigned long flags;
2452 struct dasd_block *block;
2453
2454 block = (struct dasd_block *) ptr;
2455 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2456 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002457 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002458 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2459 dasd_schedule_block_bh(block);
2460}
2461
2462/*
2463 * Setup timeout for a dasd_block in jiffies.
2464 */
2465void dasd_block_set_timer(struct dasd_block *block, int expires)
2466{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002467 if (expires == 0)
2468 del_timer(&block->timer);
2469 else
2470 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002471}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002472EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002473
2474/*
2475 * Clear timeout for a dasd_block.
2476 */
2477void dasd_block_clear_timer(struct dasd_block *block)
2478{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002479 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002480}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002481EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482
2483/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002484 * Process finished error recovery ccw.
2485 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002486static void __dasd_process_erp(struct dasd_device *device,
2487 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002488{
2489 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002490
2491 if (cqr->status == DASD_CQR_DONE)
2492 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2493 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002494 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002495 erp_fn = device->discipline->erp_postaction(cqr);
2496 erp_fn(cqr);
2497}
2498
2499/*
2500 * Fetch requests from the block device queue.
2501 */
2502static void __dasd_process_request_queue(struct dasd_block *block)
2503{
2504 struct request_queue *queue;
2505 struct request *req;
2506 struct dasd_ccw_req *cqr;
2507 struct dasd_device *basedev;
2508 unsigned long flags;
2509 queue = block->request_queue;
2510 basedev = block->base;
2511 /* No queue ? Then there is nothing to do. */
2512 if (queue == NULL)
2513 return;
2514
2515 /*
2516 * We requeue request from the block device queue to the ccw
2517 * queue only in two states. In state DASD_STATE_READY the
2518 * partition detection is done and we need to requeue requests
2519 * for that. State DASD_STATE_ONLINE is normal block device
2520 * operation.
2521 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002522 if (basedev->state < DASD_STATE_READY) {
2523 while ((req = blk_fetch_request(block->request_queue)))
2524 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002525 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002526 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002527 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002528 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002529 if (basedev->features & DASD_FEATURE_READONLY &&
2530 rq_data_dir(req) == WRITE) {
2531 DBF_DEV_EVENT(DBF_ERR, basedev,
2532 "Rejecting write request %p",
2533 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002534 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002535 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002536 continue;
2537 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002538 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2539 (basedev->features & DASD_FEATURE_FAILFAST ||
2540 blk_noretry_request(req))) {
2541 DBF_DEV_EVENT(DBF_ERR, basedev,
2542 "Rejecting failfast request %p",
2543 req);
2544 blk_start_request(req);
2545 __blk_end_request_all(req, -ETIMEDOUT);
2546 continue;
2547 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002548 cqr = basedev->discipline->build_cp(basedev, block, req);
2549 if (IS_ERR(cqr)) {
2550 if (PTR_ERR(cqr) == -EBUSY)
2551 break; /* normal end condition */
2552 if (PTR_ERR(cqr) == -ENOMEM)
2553 break; /* terminate request queue loop */
2554 if (PTR_ERR(cqr) == -EAGAIN) {
2555 /*
2556 * The current request cannot be build right
2557 * now, we have to try later. If this request
2558 * is the head-of-queue we stop the device
2559 * for 1/2 second.
2560 */
2561 if (!list_empty(&block->ccw_queue))
2562 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002563 spin_lock_irqsave(
2564 get_ccwdev_lock(basedev->cdev), flags);
2565 dasd_device_set_stop_bits(basedev,
2566 DASD_STOPPED_PENDING);
2567 spin_unlock_irqrestore(
2568 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002569 dasd_block_set_timer(block, HZ/2);
2570 break;
2571 }
2572 DBF_DEV_EVENT(DBF_ERR, basedev,
2573 "CCW creation failed (rc=%ld) "
2574 "on request %p",
2575 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002576 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002577 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002578 continue;
2579 }
2580 /*
2581 * Note: callback is set to dasd_return_cqr_cb in
2582 * __dasd_block_start_head to cover erp requests as well
2583 */
2584 cqr->callback_data = (void *) req;
2585 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002586 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002587 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002588 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002589 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002590 dasd_profile_start(block, cqr, req);
2591 }
2592}
2593
2594static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2595{
2596 struct request *req;
2597 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002598 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002599
2600 req = (struct request *) cqr->callback_data;
2601 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002602 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002603 if (status < 0)
2604 error = status;
2605 else if (status == 0) {
2606 if (cqr->intrc == -EPERM)
2607 error = -EBADE;
2608 else if (cqr->intrc == -ENOLINK ||
2609 cqr->intrc == -ETIMEDOUT)
2610 error = cqr->intrc;
2611 else
2612 error = -EIO;
2613 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002614 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002615}
2616
2617/*
2618 * Process ccw request queue.
2619 */
2620static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2621 struct list_head *final_queue)
2622{
2623 struct list_head *l, *n;
2624 struct dasd_ccw_req *cqr;
2625 dasd_erp_fn_t erp_fn;
2626 unsigned long flags;
2627 struct dasd_device *base = block->base;
2628
2629restart:
2630 /* Process request with final status. */
2631 list_for_each_safe(l, n, &block->ccw_queue) {
2632 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2633 if (cqr->status != DASD_CQR_DONE &&
2634 cqr->status != DASD_CQR_FAILED &&
2635 cqr->status != DASD_CQR_NEED_ERP &&
2636 cqr->status != DASD_CQR_TERMINATED)
2637 continue;
2638
2639 if (cqr->status == DASD_CQR_TERMINATED) {
2640 base->discipline->handle_terminated_request(cqr);
2641 goto restart;
2642 }
2643
2644 /* Process requests that may be recovered */
2645 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002646 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002647 if (IS_ERR(erp_fn(cqr)))
2648 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002649 goto restart;
2650 }
2651
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002652 /* log sense for fatal error */
2653 if (cqr->status == DASD_CQR_FAILED) {
2654 dasd_log_sense(cqr, &cqr->irb);
2655 }
2656
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002657 /* First of all call extended error reporting. */
2658 if (dasd_eer_enabled(base) &&
2659 cqr->status == DASD_CQR_FAILED) {
2660 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2661
2662 /* restart request */
2663 cqr->status = DASD_CQR_FILLED;
2664 cqr->retries = 255;
2665 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002666 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002667 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2668 flags);
2669 goto restart;
2670 }
2671
2672 /* Process finished ERP request. */
2673 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002674 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002675 goto restart;
2676 }
2677
2678 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002679 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002680 list_move_tail(&cqr->blocklist, final_queue);
2681 }
2682}
2683
2684static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2685{
2686 dasd_schedule_block_bh(cqr->block);
2687}
2688
2689static void __dasd_block_start_head(struct dasd_block *block)
2690{
2691 struct dasd_ccw_req *cqr;
2692
2693 if (list_empty(&block->ccw_queue))
2694 return;
2695 /* We allways begin with the first requests on the queue, as some
2696 * of previously started requests have to be enqueued on a
2697 * dasd_device again for error recovery.
2698 */
2699 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2700 if (cqr->status != DASD_CQR_FILLED)
2701 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002702 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2703 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2704 cqr->status = DASD_CQR_FAILED;
2705 cqr->intrc = -EPERM;
2706 dasd_schedule_block_bh(block);
2707 continue;
2708 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002709 /* Non-temporary stop condition will trigger fail fast */
2710 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2711 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2712 (!dasd_eer_enabled(block->base))) {
2713 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002714 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002715 dasd_schedule_block_bh(block);
2716 continue;
2717 }
2718 /* Don't try to start requests if device is stopped */
2719 if (block->base->stopped)
2720 return;
2721
2722 /* just a fail safe check, should not happen */
2723 if (!cqr->startdev)
2724 cqr->startdev = block->base;
2725
2726 /* make sure that the requests we submit find their way back */
2727 cqr->callback = dasd_return_cqr_cb;
2728
2729 dasd_add_request_tail(cqr);
2730 }
2731}
2732
2733/*
2734 * Central dasd_block layer routine. Takes requests from the generic
2735 * block layer request queue, creates ccw requests, enqueues them on
2736 * a dasd_device and processes ccw requests that have been returned.
2737 */
2738static void dasd_block_tasklet(struct dasd_block *block)
2739{
2740 struct list_head final_queue;
2741 struct list_head *l, *n;
2742 struct dasd_ccw_req *cqr;
2743
2744 atomic_set(&block->tasklet_scheduled, 0);
2745 INIT_LIST_HEAD(&final_queue);
2746 spin_lock(&block->queue_lock);
2747 /* Finish off requests on ccw queue */
2748 __dasd_process_block_ccw_queue(block, &final_queue);
2749 spin_unlock(&block->queue_lock);
2750 /* Now call the callback function of requests with final status */
2751 spin_lock_irq(&block->request_queue_lock);
2752 list_for_each_safe(l, n, &final_queue) {
2753 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2754 list_del_init(&cqr->blocklist);
2755 __dasd_cleanup_cqr(cqr);
2756 }
2757 spin_lock(&block->queue_lock);
2758 /* Get new request from the block device request queue */
2759 __dasd_process_request_queue(block);
2760 /* Now check if the head of the ccw queue needs to be started. */
2761 __dasd_block_start_head(block);
2762 spin_unlock(&block->queue_lock);
2763 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002764 if (waitqueue_active(&shutdown_waitq))
2765 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002766 dasd_put_device(block->base);
2767}
2768
2769static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2770{
2771 wake_up(&dasd_flush_wq);
2772}
2773
2774/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002775 * Requeue a request back to the block request queue
2776 * only works for block requests
2777 */
2778static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2779{
2780 struct dasd_block *block = cqr->block;
2781 struct request *req;
2782 unsigned long flags;
2783
2784 if (!block)
2785 return -EINVAL;
2786 spin_lock_irqsave(&block->queue_lock, flags);
2787 req = (struct request *) cqr->callback_data;
2788 blk_requeue_request(block->request_queue, req);
2789 spin_unlock_irqrestore(&block->queue_lock, flags);
2790
2791 return 0;
2792}
2793
2794/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002795 * Go through all request on the dasd_block request queue, cancel them
2796 * on the respective dasd_device, and return them to the generic
2797 * block layer.
2798 */
2799static int dasd_flush_block_queue(struct dasd_block *block)
2800{
2801 struct dasd_ccw_req *cqr, *n;
2802 int rc, i;
2803 struct list_head flush_queue;
2804
2805 INIT_LIST_HEAD(&flush_queue);
2806 spin_lock_bh(&block->queue_lock);
2807 rc = 0;
2808restart:
2809 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2810 /* if this request currently owned by a dasd_device cancel it */
2811 if (cqr->status >= DASD_CQR_QUEUED)
2812 rc = dasd_cancel_req(cqr);
2813 if (rc < 0)
2814 break;
2815 /* Rechain request (including erp chain) so it won't be
2816 * touched by the dasd_block_tasklet anymore.
2817 * Replace the callback so we notice when the request
2818 * is returned from the dasd_device layer.
2819 */
2820 cqr->callback = _dasd_wake_block_flush_cb;
2821 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2822 list_move_tail(&cqr->blocklist, &flush_queue);
2823 if (i > 1)
2824 /* moved more than one request - need to restart */
2825 goto restart;
2826 }
2827 spin_unlock_bh(&block->queue_lock);
2828 /* Now call the callback function of flushed requests */
2829restart_cb:
2830 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2831 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2832 /* Process finished ERP request. */
2833 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002834 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002835 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002836 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002837 /* restart list_for_xx loop since dasd_process_erp
2838 * might remove multiple elements */
2839 goto restart_cb;
2840 }
2841 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002842 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002843 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002844 list_del_init(&cqr->blocklist);
2845 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002846 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return rc;
2849}
2850
2851/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002852 * Schedules a call to dasd_tasklet over the device tasklet.
2853 */
2854void dasd_schedule_block_bh(struct dasd_block *block)
2855{
2856 /* Protect against rescheduling. */
2857 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2858 return;
2859 /* life cycle of block is bound to it's base device */
2860 dasd_get_device(block->base);
2861 tasklet_hi_schedule(&block->tasklet);
2862}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002863EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002864
2865
2866/*
2867 * SECTION: external block device operations
2868 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 */
2870
2871/*
2872 * Dasd request queue function. Called from ll_rw_blk.c
2873 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002874static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002876 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002878 block = queue->queuedata;
2879 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002881 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002883 __dasd_block_start_head(block);
2884 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
2887/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002888 * Block timeout callback, called from the block layer
2889 *
2890 * request_queue lock is held on entry.
2891 *
2892 * Return values:
2893 * BLK_EH_RESET_TIMER if the request should be left running
2894 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2895 * by the driver.
2896 */
2897enum blk_eh_timer_return dasd_times_out(struct request *req)
2898{
2899 struct dasd_ccw_req *cqr = req->completion_data;
2900 struct dasd_block *block = req->q->queuedata;
2901 struct dasd_device *device;
2902 int rc = 0;
2903
2904 if (!cqr)
2905 return BLK_EH_NOT_HANDLED;
2906
2907 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002908 if (!device->blk_timeout)
2909 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002910 DBF_DEV_EVENT(DBF_WARNING, device,
2911 " dasd_times_out cqr %p status %x",
2912 cqr, cqr->status);
2913
2914 spin_lock(&block->queue_lock);
2915 spin_lock(get_ccwdev_lock(device->cdev));
2916 cqr->retries = -1;
2917 cqr->intrc = -ETIMEDOUT;
2918 if (cqr->status >= DASD_CQR_QUEUED) {
2919 spin_unlock(get_ccwdev_lock(device->cdev));
2920 rc = dasd_cancel_req(cqr);
2921 } else if (cqr->status == DASD_CQR_FILLED ||
2922 cqr->status == DASD_CQR_NEED_ERP) {
2923 cqr->status = DASD_CQR_TERMINATED;
2924 spin_unlock(get_ccwdev_lock(device->cdev));
2925 } else if (cqr->status == DASD_CQR_IN_ERP) {
2926 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2927
2928 list_for_each_entry_safe(searchcqr, nextcqr,
2929 &block->ccw_queue, blocklist) {
2930 tmpcqr = searchcqr;
2931 while (tmpcqr->refers)
2932 tmpcqr = tmpcqr->refers;
2933 if (tmpcqr != cqr)
2934 continue;
2935 /* searchcqr is an ERP request for cqr */
2936 searchcqr->retries = -1;
2937 searchcqr->intrc = -ETIMEDOUT;
2938 if (searchcqr->status >= DASD_CQR_QUEUED) {
2939 spin_unlock(get_ccwdev_lock(device->cdev));
2940 rc = dasd_cancel_req(searchcqr);
2941 spin_lock(get_ccwdev_lock(device->cdev));
2942 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2943 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2944 searchcqr->status = DASD_CQR_TERMINATED;
2945 rc = 0;
2946 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2947 /*
2948 * Shouldn't happen; most recent ERP
2949 * request is at the front of queue
2950 */
2951 continue;
2952 }
2953 break;
2954 }
2955 spin_unlock(get_ccwdev_lock(device->cdev));
2956 }
2957 dasd_schedule_block_bh(block);
2958 spin_unlock(&block->queue_lock);
2959
2960 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2961}
2962
2963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 * Allocate and initialize request queue and default I/O scheduler.
2965 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002966static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002968 block->request_queue = blk_init_queue(do_dasd_request,
2969 &block->request_queue_lock);
2970 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return -ENOMEM;
2972
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002973 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01002975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978/*
2979 * Allocate and initialize request queue.
2980 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002981static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 int max;
2984
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002985 if (block->base->features & DASD_FEATURE_USERAW) {
2986 /*
2987 * the max_blocks value for raw_track access is 256
2988 * it is higher than the native ECKD value because we
2989 * only need one ccw per track
2990 * so the max_hw_sectors are
2991 * 2048 x 512B = 1024kB = 16 tracks
2992 */
2993 max = 2048;
2994 } else {
2995 max = block->base->discipline->max_blocks << block->s2b_shift;
2996 }
2997 blk_queue_logical_block_size(block->request_queue,
2998 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002999 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003000 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003001 /* with page sized segments we can translate each segement into
3002 * one idaw/tidaw
3003 */
3004 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3005 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
3008/*
3009 * Deactivate and free request queue.
3010 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003011static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003013 if (block->request_queue) {
3014 blk_cleanup_queue(block->request_queue);
3015 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
3017}
3018
3019/*
3020 * Flush request on the request queue.
3021 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003022static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct request *req;
3025
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003026 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003028
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003029 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003030 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003031 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003032 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Al Viro57a7c0b2008-03-02 10:36:08 -05003035static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003037 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 int rc;
3039
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003040 base = dasd_device_from_gendisk(bdev->bd_disk);
3041 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003042 return -ENODEV;
3043
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003044 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003045 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 rc = -ENODEV;
3047 goto unlock;
3048 }
3049
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003050 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 rc = -EINVAL;
3052 goto unlock;
3053 }
3054
3055 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003056 dev_info(&base->cdev->dev,
3057 "Accessing the DASD failed because it is in "
3058 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 rc = -EPERM;
3060 goto out;
3061 }
3062
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003063 if (base->state <= DASD_STATE_BASIC) {
3064 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 " Cannot open unrecognized device");
3066 rc = -ENODEV;
3067 goto out;
3068 }
3069
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003070 if ((mode & FMODE_WRITE) &&
3071 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3072 (base->features & DASD_FEATURE_READONLY))) {
3073 rc = -EROFS;
3074 goto out;
3075 }
3076
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003077 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return 0;
3079
3080out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003081 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003083 atomic_dec(&base->block->open_count);
3084 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return rc;
3086}
3087
Al Virodb2a1442013-05-05 21:52:57 -04003088static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Al Virodb2a1442013-05-05 21:52:57 -04003090 struct dasd_device *base = dasd_device_from_gendisk(disk);
3091 if (base) {
3092 atomic_dec(&base->block->open_count);
3093 module_put(base->discipline->owner);
3094 dasd_put_device(base);
3095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003098/*
3099 * Return disk geometry.
3100 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003101static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003102{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003103 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003104
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003105 base = dasd_device_from_gendisk(bdev->bd_disk);
3106 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003107 return -ENODEV;
3108
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003109 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003110 !base->discipline->fill_geometry) {
3111 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003112 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003113 }
3114 base->discipline->fill_geometry(base->block, geo);
3115 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3116 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003117 return 0;
3118}
3119
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003120const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121dasd_device_operations = {
3122 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003123 .open = dasd_open,
3124 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003125 .ioctl = dasd_ioctl,
3126 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003127 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128};
3129
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003130/*******************************************************************************
3131 * end of block device operations
3132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134static void
3135dasd_exit(void)
3136{
3137#ifdef CONFIG_PROC_FS
3138 dasd_proc_exit();
3139#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003140 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003141 if (dasd_page_cache != NULL) {
3142 kmem_cache_destroy(dasd_page_cache);
3143 dasd_page_cache = NULL;
3144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 dasd_gendisk_exit();
3146 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (dasd_debug_area != NULL) {
3148 debug_unregister(dasd_debug_area);
3149 dasd_debug_area = NULL;
3150 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003151 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
3154/*
3155 * SECTION: common functions for ccw_driver use
3156 */
3157
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003158/*
3159 * Is the device read-only?
3160 * Note that this function does not report the setting of the
3161 * readonly device attribute, but how it is configured in z/VM.
3162 */
3163int dasd_device_is_ro(struct dasd_device *device)
3164{
3165 struct ccw_dev_id dev_id;
3166 struct diag210 diag_data;
3167 int rc;
3168
3169 if (!MACHINE_IS_VM)
3170 return 0;
3171 ccw_device_get_id(device->cdev, &dev_id);
3172 memset(&diag_data, 0, sizeof(diag_data));
3173 diag_data.vrdcdvno = dev_id.devno;
3174 diag_data.vrdclen = sizeof(diag_data);
3175 rc = diag210(&diag_data);
3176 if (rc == 0 || rc == 2) {
3177 return diag_data.vrdcvfla & 0x80;
3178 } else {
3179 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3180 dev_id.devno, rc);
3181 return 0;
3182 }
3183}
3184EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3185
Cornelia Huckf3445a12009-04-14 15:36:23 +02003186static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3187{
3188 struct ccw_device *cdev = data;
3189 int ret;
3190
3191 ret = ccw_device_set_online(cdev);
3192 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003193 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3194 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003195}
3196
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003197/*
3198 * Initial attempt at a probe function. this can be simplified once
3199 * the other detection code is gone.
3200 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003201int dasd_generic_probe(struct ccw_device *cdev,
3202 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
3204 int ret;
3205
3206 ret = dasd_add_sysfs_files(cdev);
3207 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003208 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3209 "dasd_generic_probe: could not add "
3210 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
Horst Hummel40545572006-06-29 15:08:18 +02003213 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Horst Hummel40545572006-06-29 15:08:18 +02003215 /*
3216 * Automatically online either all dasd devices (dasd_autodetect)
3217 * or all devices specified with dasd= parameters during
3218 * initial probe.
3219 */
3220 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003221 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003222 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003225EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003227/*
3228 * This will one day be called from a global not_oper handler.
3229 * It is also used by driver_unregister during module unload.
3230 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003231void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003234 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Horst Hummel59afda72005-05-16 21:53:39 -07003236 cdev->handler = NULL;
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003239 if (IS_ERR(device)) {
3240 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003242 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003243 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3244 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 /* Already doing offline processing */
3246 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003247 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return;
3249 }
3250 /*
3251 * This device is removed unconditionally. Set offline
3252 * flag to prevent dasd_open from opening it while it is
3253 * no quite down yet.
3254 */
3255 dasd_set_target_state(device, DASD_STATE_NEW);
3256 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003257 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003259 /*
3260 * life cycle of block is bound to device, so delete it after
3261 * device was safely removed
3262 */
3263 if (block)
3264 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003265
3266 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003268EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003270/*
3271 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003273 * or the user has started activation through sysfs.
3274 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003275int dasd_generic_set_online(struct ccw_device *cdev,
3276 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003278 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003280 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003281
Horst Hummel40545572006-06-29 15:08:18 +02003282 /* first online clears initial online feature flag */
3283 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 device = dasd_create_device(cdev);
3285 if (IS_ERR(device))
3286 return PTR_ERR(device);
3287
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003288 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003289 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003291 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3292 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 dasd_delete_device(device);
3294 return -ENODEV;
3295 }
3296 discipline = dasd_diag_discipline_pointer;
3297 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003298 if (!try_module_get(base_discipline->owner)) {
3299 dasd_delete_device(device);
3300 return -EINVAL;
3301 }
3302 if (!try_module_get(discipline->owner)) {
3303 module_put(base_discipline->owner);
3304 dasd_delete_device(device);
3305 return -EINVAL;
3306 }
3307 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 device->discipline = discipline;
3309
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003310 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 rc = discipline->check_device(device);
3312 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003313 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3314 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003315 module_put(discipline->owner);
3316 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 dasd_delete_device(device);
3318 return rc;
3319 }
3320
3321 dasd_set_target_state(device, DASD_STATE_ONLINE);
3322 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003323 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3324 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 rc = -ENODEV;
3326 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003327 if (device->block)
3328 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 dasd_delete_device(device);
3330 } else
3331 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003332 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003333
3334 wait_event(dasd_init_waitq, _wait_for_device(device));
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return rc;
3338}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003339EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003341int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003344 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003345 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003347 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 device = dasd_device_from_cdev(cdev);
3349 if (IS_ERR(device))
3350 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /*
3353 * We must make sure that this device is currently not in use.
3354 * The open_count is increased for every opener, that includes
3355 * the blkdev_get in dasd_scan_partitions. We are only interested
3356 * in the other openers.
3357 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003358 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003359 max_count = device->block->bdev ? 0 : -1;
3360 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003361 if (open_count > max_count) {
3362 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003363 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3364 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003365 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003366 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3367 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003368 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3369 dasd_put_device(device);
3370 return -EBUSY;
3371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003373
3374 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3375 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003376 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003377 * could only be called by normal offline so safe_offline flag
3378 * needs to be removed to run normal offline and kill all I/O
3379 */
3380 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3381 /* Already doing normal offline processing */
3382 dasd_put_device(device);
3383 return -EBUSY;
3384 } else
3385 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3386
3387 } else
3388 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3389 /* Already doing offline processing */
3390 dasd_put_device(device);
3391 return -EBUSY;
3392 }
3393
3394 /*
3395 * if safe_offline called set safe_offline_running flag and
3396 * clear safe_offline so that a call to normal offline
3397 * can overrun safe_offline processing
3398 */
3399 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3400 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3401 /*
3402 * If we want to set the device safe offline all IO operations
3403 * should be finished before continuing the offline process
3404 * so sync bdev first and then wait for our queues to become
3405 * empty
3406 */
3407 /* sync blockdev and partitions */
3408 rc = fsync_bdev(device->block->bdev);
3409 if (rc != 0)
3410 goto interrupted;
3411
3412 /* schedule device tasklet and wait for completion */
3413 dasd_schedule_device_bh(device);
3414 rc = wait_event_interruptible(shutdown_waitq,
3415 _wait_for_empty_queues(device));
3416 if (rc != 0)
3417 goto interrupted;
3418 }
3419
3420 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 dasd_set_target_state(device, DASD_STATE_NEW);
3422 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003423 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003425 /*
3426 * life cycle of block is bound to device, so delete it after
3427 * device was safely removed
3428 */
3429 if (block)
3430 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003432
3433interrupted:
3434 /* interrupted by signal */
3435 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3436 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3437 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3438 dasd_put_device(device);
3439 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003441EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003443int dasd_generic_last_path_gone(struct dasd_device *device)
3444{
3445 struct dasd_ccw_req *cqr;
3446
3447 dev_warn(&device->cdev->dev, "No operational channel path is left "
3448 "for the device\n");
3449 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3450 /* First of all call extended error reporting. */
3451 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3452
3453 if (device->state < DASD_STATE_BASIC)
3454 return 0;
3455 /* Device is active. We want to keep it. */
3456 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3457 if ((cqr->status == DASD_CQR_IN_IO) ||
3458 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3459 cqr->status = DASD_CQR_QUEUED;
3460 cqr->retries++;
3461 }
3462 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3463 dasd_device_clear_timer(device);
3464 dasd_schedule_device_bh(device);
3465 return 1;
3466}
3467EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3468
3469int dasd_generic_path_operational(struct dasd_device *device)
3470{
3471 dev_info(&device->cdev->dev, "A channel path to the device has become "
3472 "operational\n");
3473 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3474 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3475 if (device->stopped & DASD_UNRESUMED_PM) {
3476 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3477 dasd_restore_device(device);
3478 return 1;
3479 }
3480 dasd_schedule_device_bh(device);
3481 if (device->block)
3482 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003483
3484 if (!device->stopped)
3485 wake_up(&generic_waitq);
3486
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003487 return 1;
3488}
3489EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3490
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003491int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 int ret;
3495
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003496 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (IS_ERR(device))
3498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 ret = 0;
3500 switch (event) {
3501 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003502 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003504 device->path_data.opm = 0;
3505 device->path_data.ppm = 0;
3506 device->path_data.npm = 0;
3507 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
3509 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003511 if (device->path_data.opm)
3512 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 break;
3514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 dasd_put_device(device);
3516 return ret;
3517}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003518EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003520void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3521{
3522 int chp;
3523 __u8 oldopm, eventlpm;
3524 struct dasd_device *device;
3525
3526 device = dasd_device_from_cdev_locked(cdev);
3527 if (IS_ERR(device))
3528 return;
3529 for (chp = 0; chp < 8; chp++) {
3530 eventlpm = 0x80 >> chp;
3531 if (path_event[chp] & PE_PATH_GONE) {
3532 oldopm = device->path_data.opm;
3533 device->path_data.opm &= ~eventlpm;
3534 device->path_data.ppm &= ~eventlpm;
3535 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003536 if (oldopm && !device->path_data.opm) {
3537 dev_warn(&device->cdev->dev,
3538 "No verified channel paths remain "
3539 "for the device\n");
3540 DBF_DEV_EVENT(DBF_WARNING, device,
3541 "%s", "last verified path gone");
3542 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3543 dasd_device_set_stop_bits(device,
3544 DASD_STOPPED_DC_WAIT);
3545 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003546 }
3547 if (path_event[chp] & PE_PATH_AVAILABLE) {
3548 device->path_data.opm &= ~eventlpm;
3549 device->path_data.ppm &= ~eventlpm;
3550 device->path_data.npm &= ~eventlpm;
3551 device->path_data.tbvpm |= eventlpm;
3552 dasd_schedule_device_bh(device);
3553 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003554 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003555 if (!(device->path_data.opm & eventlpm) &&
3556 !(device->path_data.tbvpm & eventlpm)) {
3557 /*
3558 * we can not establish a pathgroup on an
3559 * unavailable path, so trigger a path
3560 * verification first
3561 */
3562 device->path_data.tbvpm |= eventlpm;
3563 dasd_schedule_device_bh(device);
3564 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003565 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3566 "Pathgroup re-established\n");
3567 if (device->discipline->kick_validate)
3568 device->discipline->kick_validate(device);
3569 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003570 }
3571 dasd_put_device(device);
3572}
3573EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3574
3575int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3576{
3577 if (!device->path_data.opm && lpm) {
3578 device->path_data.opm = lpm;
3579 dasd_generic_path_operational(device);
3580 } else
3581 device->path_data.opm |= lpm;
3582 return 0;
3583}
3584EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3585
3586
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003587int dasd_generic_pm_freeze(struct ccw_device *cdev)
3588{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003589 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003590 struct list_head freeze_queue;
3591 struct dasd_ccw_req *cqr, *n;
3592 struct dasd_ccw_req *refers;
3593 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003594
3595 if (IS_ERR(device))
3596 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003597
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003598 /* mark device as suspended */
3599 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3600
Stefan Haberland6f272b92011-01-05 12:48:05 +01003601 if (device->discipline->freeze)
3602 rc = device->discipline->freeze(device);
3603
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003604 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003605 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003606
3607 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003608 INIT_LIST_HEAD(&freeze_queue);
3609 spin_lock_irq(get_ccwdev_lock(cdev));
3610 rc = 0;
3611 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3612 /* Check status and move request to flush_queue */
3613 if (cqr->status == DASD_CQR_IN_IO) {
3614 rc = device->discipline->term_IO(cqr);
3615 if (rc) {
3616 /* unable to terminate requeust */
3617 dev_err(&device->cdev->dev,
3618 "Unable to terminate request %p "
3619 "on suspend\n", cqr);
3620 spin_unlock_irq(get_ccwdev_lock(cdev));
3621 dasd_put_device(device);
3622 return rc;
3623 }
3624 }
3625 list_move_tail(&cqr->devlist, &freeze_queue);
3626 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003627 spin_unlock_irq(get_ccwdev_lock(cdev));
3628
3629 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3630 wait_event(dasd_flush_wq,
3631 (cqr->status != DASD_CQR_CLEAR_PENDING));
3632 if (cqr->status == DASD_CQR_CLEARED)
3633 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003634
Stefan Haberlandc5576872013-04-15 16:41:31 +02003635 /* requeue requests to blocklayer will only work for
3636 block device requests */
3637 if (_dasd_requeue_request(cqr))
3638 continue;
3639
3640 /* remove requests from device and block queue */
3641 list_del_init(&cqr->devlist);
3642 while (cqr->refers != NULL) {
3643 refers = cqr->refers;
3644 /* remove the request from the block queue */
3645 list_del(&cqr->blocklist);
3646 /* free the finished erp request */
3647 dasd_free_erp_request(cqr, cqr->memdev);
3648 cqr = refers;
3649 }
3650 if (cqr->block)
3651 list_del_init(&cqr->blocklist);
3652 cqr->block->base->discipline->free_cp(
3653 cqr, (struct request *) cqr->callback_data);
3654 }
3655
3656 /*
3657 * if requests remain then they are internal request
3658 * and go back to the device queue
3659 */
3660 if (!list_empty(&freeze_queue)) {
3661 /* move freeze_queue to start of the ccw_queue */
3662 spin_lock_irq(get_ccwdev_lock(cdev));
3663 list_splice_tail(&freeze_queue, &device->ccw_queue);
3664 spin_unlock_irq(get_ccwdev_lock(cdev));
3665 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003666 dasd_put_device(device);
3667 return rc;
3668}
3669EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3670
3671int dasd_generic_restore_device(struct ccw_device *cdev)
3672{
3673 struct dasd_device *device = dasd_device_from_cdev(cdev);
3674 int rc = 0;
3675
3676 if (IS_ERR(device))
3677 return PTR_ERR(device);
3678
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003679 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003680 dasd_device_remove_stop_bits(device,
3681 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003682
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003683 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003684
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003685 /*
3686 * call discipline restore function
3687 * if device is stopped do nothing e.g. for disconnected devices
3688 */
3689 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003690 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003691 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003692 /*
3693 * if the resume failed for the DASD we put it in
3694 * an UNRESUMED stop state
3695 */
3696 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003697
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003698 if (device->block)
3699 dasd_schedule_block_bh(device->block);
3700
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003701 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003702 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003703 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003704}
3705EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3706
Heiko Carstens763968e2007-05-10 15:45:46 +02003707static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3708 void *rdc_buffer,
3709 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003710 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003711{
3712 struct dasd_ccw_req *cqr;
3713 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003714 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003715
3716 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3717
3718 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003719 /* internal error 13 - Allocating the RDC request failed*/
3720 dev_err(&device->cdev->dev,
3721 "An error occurred in the DASD device driver, "
3722 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003723 return cqr;
3724 }
3725
3726 ccw = cqr->cpaddr;
3727 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003728 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3729 idaw = (unsigned long *) (cqr->data);
3730 ccw->cda = (__u32)(addr_t) idaw;
3731 ccw->flags = CCW_FLAG_IDA;
3732 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3733 } else {
3734 ccw->cda = (__u32)(addr_t) rdc_buffer;
3735 ccw->flags = 0;
3736 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003737
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003738 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003739 cqr->startdev = device;
3740 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003741 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003742 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003743 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003744 cqr->status = DASD_CQR_FILLED;
3745 return cqr;
3746}
3747
3748
Stefan Haberland68b781f2009-09-11 10:28:29 +02003749int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003750 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003751{
3752 int ret;
3753 struct dasd_ccw_req *cqr;
3754
Sebastian Ott92636b12009-06-12 10:26:37 +02003755 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003756 magic);
3757 if (IS_ERR(cqr))
3758 return PTR_ERR(cqr);
3759
3760 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003761 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003762 return ret;
3763}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003764EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003765
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003766/*
3767 * In command mode and transport mode we need to look for sense
3768 * data in different places. The sense data itself is allways
3769 * an array of 32 bytes, so we can unify the sense data access
3770 * for both modes.
3771 */
3772char *dasd_get_sense(struct irb *irb)
3773{
3774 struct tsb *tsb = NULL;
3775 char *sense = NULL;
3776
3777 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3778 if (irb->scsw.tm.tcw)
3779 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3780 irb->scsw.tm.tcw);
3781 if (tsb && tsb->length == 64 && tsb->flags)
3782 switch (tsb->flags & 0x07) {
3783 case 1: /* tsa_iostat */
3784 sense = tsb->tsa.iostat.sense;
3785 break;
3786 case 2: /* tsa_ddpc */
3787 sense = tsb->tsa.ddpc.sense;
3788 break;
3789 default:
3790 /* currently we don't use interrogate data */
3791 break;
3792 }
3793 } else if (irb->esw.esw0.erw.cons) {
3794 sense = irb->ecw;
3795 }
3796 return sense;
3797}
3798EXPORT_SYMBOL_GPL(dasd_get_sense);
3799
Stefan Haberland4679e892012-06-19 17:30:12 +02003800void dasd_generic_shutdown(struct ccw_device *cdev)
3801{
3802 struct dasd_device *device;
3803
3804 device = dasd_device_from_cdev(cdev);
3805 if (IS_ERR(device))
3806 return;
3807
3808 if (device->block)
3809 dasd_schedule_block_bh(device->block);
3810
3811 dasd_schedule_device_bh(device);
3812
3813 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3814}
3815EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3816
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003817static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
3819 int rc;
3820
3821 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003822 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003823 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003824 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003827 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (dasd_debug_area == NULL) {
3829 rc = -ENOMEM;
3830 goto failed;
3831 }
3832 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003833 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3836
3837 dasd_diag_discipline_pointer = NULL;
3838
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003839 dasd_statistics_createroot();
3840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 rc = dasd_devmap_init();
3842 if (rc)
3843 goto failed;
3844 rc = dasd_gendisk_init();
3845 if (rc)
3846 goto failed;
3847 rc = dasd_parse();
3848 if (rc)
3849 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003850 rc = dasd_eer_init();
3851 if (rc)
3852 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853#ifdef CONFIG_PROC_FS
3854 rc = dasd_proc_init();
3855 if (rc)
3856 goto failed;
3857#endif
3858
3859 return 0;
3860failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003861 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 dasd_exit();
3863 return rc;
3864}
3865
3866module_init(dasd_init);
3867module_exit(dasd_exit);