blob: 57fd66357b959fadaccfa3ecf269dda848bc7166 [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. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200582 if (!schedule_work(&device->kick_work))
583 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200585EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200588 * dasd_reload_device will schedule a call do do_reload_device to the kernel
589 * event daemon.
590 */
591static void do_reload_device(struct work_struct *work)
592{
593 struct dasd_device *device = container_of(work, struct dasd_device,
594 reload_device);
595 device->discipline->reload(device);
596 dasd_put_device(device);
597}
598
599void dasd_reload_device(struct dasd_device *device)
600{
601 dasd_get_device(device);
602 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200603 if (!schedule_work(&device->reload_device))
604 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200605}
606EXPORT_SYMBOL(dasd_reload_device);
607
608/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200609 * dasd_restore_device will schedule a call do do_restore_device to the kernel
610 * event daemon.
611 */
612static void do_restore_device(struct work_struct *work)
613{
614 struct dasd_device *device = container_of(work, struct dasd_device,
615 restore_device);
616 device->cdev->drv->restore(device->cdev);
617 dasd_put_device(device);
618}
619
620void dasd_restore_device(struct dasd_device *device)
621{
622 dasd_get_device(device);
623 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200624 if (!schedule_work(&device->restore_device))
625 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200626}
627
628/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * Set the target state for a device and starts the state change.
630 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100631void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200633 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100634 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* If we are in probeonly mode stop at DASD_STATE_READY. */
636 if (dasd_probeonly && target > DASD_STATE_READY)
637 target = DASD_STATE_READY;
638 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100639 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 wake_up(&dasd_init_waitq);
641 device->target = target;
642 }
643 if (device->state != device->target)
644 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100645 mutex_unlock(&device->state_mutex);
646 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200648EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/*
651 * Enable devices with device numbers in [from..to].
652 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100653static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 return (device->state == device->target);
656}
657
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100658void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 dasd_set_target_state(device, DASD_STATE_ONLINE);
661 if (device->state <= DASD_STATE_KNOWN)
662 /* No discipline for device found. */
663 dasd_set_target_state(device, DASD_STATE_NEW);
664 /* Now wait for the devices to come up. */
665 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400666
667 dasd_reload_device(device);
668 if (device->discipline->kick_validate)
669 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200671EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/*
674 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
675 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200676
677unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100680struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100681 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
682};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200683static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/*
686 * Add profiling information for cqr before execution.
687 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100688static void dasd_profile_start(struct dasd_block *block,
689 struct dasd_ccw_req *cqr,
690 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct list_head *l;
693 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200694 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* count the length of the chanq for statistics */
697 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200698 if (dasd_global_profile_level || block->profile.data)
699 list_for_each(l, &block->ccw_queue)
700 if (++counter >= 31)
701 break;
702
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100703 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100704 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100705 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200706 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100707 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200708 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100709 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200710
711 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200712 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200713 block->profile.data->dasd_io_nr_req[counter]++;
714 if (rq_data_dir(req) == READ)
715 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200716 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200717 spin_unlock(&block->profile.lock);
718
719 /*
720 * We count the request for the start device, even though it may run on
721 * some other device due to error recovery. This way we make sure that
722 * we count each request only once.
723 */
724 device = cqr->startdev;
725 if (device->profile.data) {
726 counter = 1; /* request is not yet queued on the start device */
727 list_for_each(l, &device->ccw_queue)
728 if (++counter >= 31)
729 break;
730 }
731 spin_lock(&device->profile.lock);
732 if (device->profile.data) {
733 device->profile.data->dasd_io_nr_req[counter]++;
734 if (rq_data_dir(req) == READ)
735 device->profile.data->dasd_read_nr_req[counter]++;
736 }
737 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740/*
741 * Add profiling information for cqr after execution.
742 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200743
744#define dasd_profile_counter(value, index) \
745{ \
746 for (index = 0; index < 31 && value >> (2+index); index++) \
747 ; \
748}
749
750static void dasd_profile_end_add_data(struct dasd_profile_info *data,
751 int is_alias,
752 int is_tpm,
753 int is_read,
754 long sectors,
755 int sectors_ind,
756 int tottime_ind,
757 int tottimeps_ind,
758 int strtime_ind,
759 int irqtime_ind,
760 int irqtimeps_ind,
761 int endtime_ind)
762{
763 /* in case of an overflow, reset the whole profile */
764 if (data->dasd_io_reqs == UINT_MAX) {
765 memset(data, 0, sizeof(*data));
766 getnstimeofday(&data->starttod);
767 }
768 data->dasd_io_reqs++;
769 data->dasd_io_sects += sectors;
770 if (is_alias)
771 data->dasd_io_alias++;
772 if (is_tpm)
773 data->dasd_io_tpm++;
774
775 data->dasd_io_secs[sectors_ind]++;
776 data->dasd_io_times[tottime_ind]++;
777 data->dasd_io_timps[tottimeps_ind]++;
778 data->dasd_io_time1[strtime_ind]++;
779 data->dasd_io_time2[irqtime_ind]++;
780 data->dasd_io_time2ps[irqtimeps_ind]++;
781 data->dasd_io_time3[endtime_ind]++;
782
783 if (is_read) {
784 data->dasd_read_reqs++;
785 data->dasd_read_sects += sectors;
786 if (is_alias)
787 data->dasd_read_alias++;
788 if (is_tpm)
789 data->dasd_read_tpm++;
790 data->dasd_read_secs[sectors_ind]++;
791 data->dasd_read_times[tottime_ind]++;
792 data->dasd_read_time1[strtime_ind]++;
793 data->dasd_read_time2[irqtime_ind]++;
794 data->dasd_read_time3[endtime_ind]++;
795 }
796}
797
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100798static void dasd_profile_end(struct dasd_block *block,
799 struct dasd_ccw_req *cqr,
800 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 long strtime, irqtime, endtime, tottime; /* in microseconds */
803 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200804 struct dasd_device *device;
805 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
806 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200808 device = cqr->startdev;
809 if (!(dasd_global_profile_level ||
810 block->profile.data ||
811 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return;
813
Tejun Heo83096eb2009-05-07 22:24:39 +0900814 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!cqr->buildclk || !cqr->startclk ||
816 !cqr->stopclk || !cqr->endclk ||
817 !sectors)
818 return;
819
820 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
821 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
822 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
823 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
824 tottimeps = tottime / sectors;
825
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200826 dasd_profile_counter(sectors, sectors_ind);
827 dasd_profile_counter(tottime, tottime_ind);
828 dasd_profile_counter(tottimeps, tottimeps_ind);
829 dasd_profile_counter(strtime, strtime_ind);
830 dasd_profile_counter(irqtime, irqtime_ind);
831 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
832 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100834 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100835 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100836 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200837 cqr->startdev != block->base,
838 cqr->cpmode == 1,
839 rq_data_dir(req) == READ,
840 sectors, sectors_ind, tottime_ind,
841 tottimeps_ind, strtime_ind,
842 irqtime_ind, irqtimeps_ind,
843 endtime_ind);
844 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100845 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200847 spin_lock(&block->profile.lock);
848 if (block->profile.data)
849 dasd_profile_end_add_data(block->profile.data,
850 cqr->startdev != block->base,
851 cqr->cpmode == 1,
852 rq_data_dir(req) == READ,
853 sectors, sectors_ind, tottime_ind,
854 tottimeps_ind, strtime_ind,
855 irqtime_ind, irqtimeps_ind,
856 endtime_ind);
857 spin_unlock(&block->profile.lock);
858
859 spin_lock(&device->profile.lock);
860 if (device->profile.data)
861 dasd_profile_end_add_data(device->profile.data,
862 cqr->startdev != block->base,
863 cqr->cpmode == 1,
864 rq_data_dir(req) == READ,
865 sectors, sectors_ind, tottime_ind,
866 tottimeps_ind, strtime_ind,
867 irqtime_ind, irqtimeps_ind,
868 endtime_ind);
869 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200871
872void dasd_profile_reset(struct dasd_profile *profile)
873{
874 struct dasd_profile_info *data;
875
876 spin_lock_bh(&profile->lock);
877 data = profile->data;
878 if (!data) {
879 spin_unlock_bh(&profile->lock);
880 return;
881 }
882 memset(data, 0, sizeof(*data));
883 getnstimeofday(&data->starttod);
884 spin_unlock_bh(&profile->lock);
885}
886
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200887int dasd_profile_on(struct dasd_profile *profile)
888{
889 struct dasd_profile_info *data;
890
891 data = kzalloc(sizeof(*data), GFP_KERNEL);
892 if (!data)
893 return -ENOMEM;
894 spin_lock_bh(&profile->lock);
895 if (profile->data) {
896 spin_unlock_bh(&profile->lock);
897 kfree(data);
898 return 0;
899 }
900 getnstimeofday(&data->starttod);
901 profile->data = data;
902 spin_unlock_bh(&profile->lock);
903 return 0;
904}
905
906void dasd_profile_off(struct dasd_profile *profile)
907{
908 spin_lock_bh(&profile->lock);
909 kfree(profile->data);
910 profile->data = NULL;
911 spin_unlock_bh(&profile->lock);
912}
913
914char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
915{
916 char *buffer;
917
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200918 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200919 if (buffer == NULL)
920 return ERR_PTR(-ENOMEM);
921 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200922 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200923 return ERR_PTR(-EFAULT);
924 }
925 /* got the string, now strip linefeed. */
926 if (buffer[user_len - 1] == '\n')
927 buffer[user_len - 1] = 0;
928 else
929 buffer[user_len] = 0;
930 return buffer;
931}
932
933static ssize_t dasd_stats_write(struct file *file,
934 const char __user *user_buf,
935 size_t user_len, loff_t *pos)
936{
937 char *buffer, *str;
938 int rc;
939 struct seq_file *m = (struct seq_file *)file->private_data;
940 struct dasd_profile *prof = m->private;
941
942 if (user_len > 65536)
943 user_len = 65536;
944 buffer = dasd_get_user_string(user_buf, user_len);
945 if (IS_ERR(buffer))
946 return PTR_ERR(buffer);
947
948 str = skip_spaces(buffer);
949 rc = user_len;
950 if (strncmp(str, "reset", 5) == 0) {
951 dasd_profile_reset(prof);
952 } else if (strncmp(str, "on", 2) == 0) {
953 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100954 if (rc)
955 goto out;
956 rc = user_len;
957 if (prof == &dasd_global_profile) {
958 dasd_profile_reset(prof);
959 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
960 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200961 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100962 if (prof == &dasd_global_profile)
963 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200964 dasd_profile_off(prof);
965 } else
966 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100967out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200968 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200969 return rc;
970}
971
972static void dasd_stats_array(struct seq_file *m, unsigned int *array)
973{
974 int i;
975
976 for (i = 0; i < 32; i++)
977 seq_printf(m, "%u ", array[i]);
978 seq_putc(m, '\n');
979}
980
981static void dasd_stats_seq_print(struct seq_file *m,
982 struct dasd_profile_info *data)
983{
984 seq_printf(m, "start_time %ld.%09ld\n",
985 data->starttod.tv_sec, data->starttod.tv_nsec);
986 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
987 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
988 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
989 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200990 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200991 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200992 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200993 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200994 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200995 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200996 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200997 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200998 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200999 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001000 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001001 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001002 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001003 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001004 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001005 dasd_stats_array(m, data->dasd_io_nr_req);
1006 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1007 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1008 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1009 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001010 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001011 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001012 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001013 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001014 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001015 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001016 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001017 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001018 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001019 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001020 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001021 dasd_stats_array(m, data->dasd_read_nr_req);
1022}
1023
1024static int dasd_stats_show(struct seq_file *m, void *v)
1025{
1026 struct dasd_profile *profile;
1027 struct dasd_profile_info *data;
1028
1029 profile = m->private;
1030 spin_lock_bh(&profile->lock);
1031 data = profile->data;
1032 if (!data) {
1033 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001034 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001035 return 0;
1036 }
1037 dasd_stats_seq_print(m, data);
1038 spin_unlock_bh(&profile->lock);
1039 return 0;
1040}
1041
1042static int dasd_stats_open(struct inode *inode, struct file *file)
1043{
1044 struct dasd_profile *profile = inode->i_private;
1045 return single_open(file, dasd_stats_show, profile);
1046}
1047
1048static const struct file_operations dasd_stats_raw_fops = {
1049 .owner = THIS_MODULE,
1050 .open = dasd_stats_open,
1051 .read = seq_read,
1052 .llseek = seq_lseek,
1053 .release = single_release,
1054 .write = dasd_stats_write,
1055};
1056
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001057static void dasd_profile_init(struct dasd_profile *profile,
1058 struct dentry *base_dentry)
1059{
Al Virof4ae40a2011-07-24 04:33:43 -04001060 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001061 struct dentry *pde;
1062
1063 if (!base_dentry)
1064 return;
1065 profile->dentry = NULL;
1066 profile->data = NULL;
1067 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1068 pde = debugfs_create_file("statistics", mode, base_dentry,
1069 profile, &dasd_stats_raw_fops);
1070 if (pde && !IS_ERR(pde))
1071 profile->dentry = pde;
1072 return;
1073}
1074
1075static void dasd_profile_exit(struct dasd_profile *profile)
1076{
1077 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001078 debugfs_remove(profile->dentry);
1079 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001080}
1081
1082static void dasd_statistics_removeroot(void)
1083{
1084 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001085 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001086 debugfs_remove(dasd_debugfs_global_entry);
1087 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001088}
1089
1090static void dasd_statistics_createroot(void)
1091{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001092 struct dentry *pde;
1093
1094 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001095 pde = debugfs_create_dir("dasd", NULL);
1096 if (!pde || IS_ERR(pde))
1097 goto error;
1098 dasd_debugfs_root_entry = pde;
1099 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1100 if (!pde || IS_ERR(pde))
1101 goto error;
1102 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001103 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001104 return;
1105
1106error:
1107 DBF_EVENT(DBF_ERR, "%s",
1108 "Creation of the dasd debugfs interface failed");
1109 dasd_statistics_removeroot();
1110 return;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001114#define dasd_profile_start(block, cqr, req) do {} while (0)
1115#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001116
1117static void dasd_statistics_createroot(void)
1118{
1119 return;
1120}
1121
1122static void dasd_statistics_removeroot(void)
1123{
1124 return;
1125}
1126
1127int dasd_stats_generic_show(struct seq_file *m, void *v)
1128{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001129 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001130 return 0;
1131}
1132
1133static void dasd_profile_init(struct dasd_profile *profile,
1134 struct dentry *base_dentry)
1135{
1136 return;
1137}
1138
1139static void dasd_profile_exit(struct dasd_profile *profile)
1140{
1141 return;
1142}
1143
1144int dasd_profile_on(struct dasd_profile *profile)
1145{
1146 return 0;
1147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149#endif /* CONFIG_DASD_PROFILE */
1150
1151/*
1152 * Allocate memory for a channel program with 'cplength' channel
1153 * command words and 'datasize' additional space. There are two
1154 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1155 * memory and 2) dasd_smalloc_request uses the static ccw memory
1156 * that gets allocated for each device.
1157 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001158struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001159 int datasize,
1160 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct dasd_ccw_req *cqr;
1163
1164 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001165 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001166 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001168 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (cqr == NULL)
1170 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 cqr->cpaddr = NULL;
1172 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001173 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 GFP_ATOMIC | GFP_DMA);
1175 if (cqr->cpaddr == NULL) {
1176 kfree(cqr);
1177 return ERR_PTR(-ENOMEM);
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 cqr->data = NULL;
1181 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001182 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001184 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 kfree(cqr);
1186 return ERR_PTR(-ENOMEM);
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001189 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1191 dasd_get_device(device);
1192 return cqr;
1193}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001194EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Stefan Haberland68b781f2009-09-11 10:28:29 +02001196struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001197 int datasize,
1198 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 unsigned long flags;
1201 struct dasd_ccw_req *cqr;
1202 char *data;
1203 int size;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1206 if (cplength > 0)
1207 size += cplength * sizeof(struct ccw1);
1208 if (datasize > 0)
1209 size += datasize;
1210 spin_lock_irqsave(&device->mem_lock, flags);
1211 cqr = (struct dasd_ccw_req *)
1212 dasd_alloc_chunk(&device->ccw_chunks, size);
1213 spin_unlock_irqrestore(&device->mem_lock, flags);
1214 if (cqr == NULL)
1215 return ERR_PTR(-ENOMEM);
1216 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1217 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1218 cqr->cpaddr = NULL;
1219 if (cplength > 0) {
1220 cqr->cpaddr = (struct ccw1 *) data;
1221 data += cplength*sizeof(struct ccw1);
1222 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1223 }
1224 cqr->data = NULL;
1225 if (datasize > 0) {
1226 cqr->data = data;
1227 memset(cqr->data, 0, datasize);
1228 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001229 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1231 dasd_get_device(device);
1232 return cqr;
1233}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001234EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236/*
1237 * Free memory of a channel program. This function needs to free all the
1238 * idal lists that might have been created by dasd_set_cda and the
1239 * struct dasd_ccw_req itself.
1240 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001241void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct ccw1 *ccw;
1244
1245 /* Clear any idals used for the request. */
1246 ccw = cqr->cpaddr;
1247 do {
1248 clear_normalized_cda(ccw);
1249 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
Jesper Juhl17fd6822005-11-07 01:01:30 -08001250 kfree(cqr->cpaddr);
1251 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 kfree(cqr);
1253 dasd_put_device(device);
1254}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001255EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001257void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 unsigned long flags;
1260
1261 spin_lock_irqsave(&device->mem_lock, flags);
1262 dasd_free_chunk(&device->ccw_chunks, cqr);
1263 spin_unlock_irqrestore(&device->mem_lock, flags);
1264 dasd_put_device(device);
1265}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001266EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268/*
1269 * Check discipline magic in cqr.
1270 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001271static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct dasd_device *device;
1274
1275 if (cqr == NULL)
1276 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001277 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001279 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 " dasd_ccw_req 0x%08x magic doesn't match"
1281 " discipline 0x%08x",
1282 cqr->magic,
1283 *(unsigned int *) device->discipline->name);
1284 return -EINVAL;
1285 }
1286 return 0;
1287}
1288
1289/*
1290 * Terminate the current i/o and set the request to clear_pending.
1291 * Timer keeps device runnig.
1292 * ccw_device_clear can fail if the i/o subsystem
1293 * is in a bad mood.
1294 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001295int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct dasd_device *device;
1298 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001299 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /* Check the cqr */
1302 rc = dasd_check_cqr(cqr);
1303 if (rc)
1304 return rc;
1305 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001306 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1308 rc = ccw_device_clear(device->cdev, (long) cqr);
1309 switch (rc) {
1310 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001311 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001312 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001313 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 DBF_DEV_EVENT(DBF_DEBUG, device,
1315 "terminate cqr %p successful",
1316 cqr);
1317 break;
1318 case -ENODEV:
1319 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1320 "device gone, retry");
1321 break;
1322 case -EIO:
1323 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1324 "I/O error, retry");
1325 break;
1326 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001327 /*
1328 * device not valid so no I/O could be running
1329 * handle CQR as termination successful
1330 */
1331 cqr->status = DASD_CQR_CLEARED;
1332 cqr->stopclk = get_tod_clock();
1333 cqr->starttime = 0;
1334 /* no retries for invalid devices */
1335 cqr->retries = -1;
1336 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1337 "EINVAL, handle as terminated");
1338 /* fake rc to success */
1339 rc = 0;
1340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 case -EBUSY:
1342 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1343 "device busy, retry later");
1344 break;
1345 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001346 /* internal error 10 - unknown rc*/
1347 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1348 dev_err(&device->cdev->dev, "An error occurred in the "
1349 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 BUG();
1351 break;
1352 }
1353 retries++;
1354 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001355 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return rc;
1357}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001358EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360/*
1361 * Start the i/o. This start_IO can fail if the channel is really busy.
1362 * In that case set up a timer to start the request later.
1363 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001364int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct dasd_device *device;
1367 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001368 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* Check the cqr */
1371 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001372 if (rc) {
1373 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001375 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001376 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001377 if (((cqr->block &&
1378 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1379 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1380 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1381 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1382 "because of stolen lock", cqr);
1383 cqr->status = DASD_CQR_ERROR;
1384 cqr->intrc = -EPERM;
1385 return -EPERM;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001388 /* internal error 14 - start_IO run out of retries */
1389 sprintf(errorstring, "14 %p", cqr);
1390 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1391 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EIO;
1394 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001395 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 cqr->starttime = jiffies;
1397 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001398 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1399 cqr->lpm &= device->path_data.opm;
1400 if (!cqr->lpm)
1401 cqr->lpm = device->path_data.opm;
1402 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001403 if (cqr->cpmode == 1) {
1404 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1405 (long) cqr, cqr->lpm);
1406 } else {
1407 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1408 (long) cqr, cqr->lpm, 0);
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 switch (rc) {
1411 case 0:
1412 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001415 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 "start_IO: device busy, retry later");
1417 break;
1418 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001419 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 "start_IO: request timeout, retry later");
1421 break;
1422 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001423 /* -EACCES indicates that the request used only a subset of the
1424 * available paths and all these paths are gone. If the lpm of
1425 * this request was only a subset of the opm (e.g. the ppm) then
1426 * we just do a retry with all available paths.
1427 * If we already use the full opm, something is amiss, and we
1428 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001430 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1431 DBF_DEV_EVENT(DBF_WARNING, device,
1432 "start_IO: selected paths gone (%x)",
1433 cqr->lpm);
1434 } else if (cqr->lpm != device->path_data.opm) {
1435 cqr->lpm = device->path_data.opm;
1436 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1437 "start_IO: selected paths gone,"
1438 " retry on all paths");
1439 } else {
1440 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1441 "start_IO: all paths in opm gone,"
1442 " do path verification");
1443 dasd_generic_last_path_gone(device);
1444 device->path_data.opm = 0;
1445 device->path_data.ppm = 0;
1446 device->path_data.npm = 0;
1447 device->path_data.tbvpm =
1448 ccw_device_get_path_mask(device->cdev);
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
1451 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001452 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001453 "start_IO: -ENODEV device gone, retry");
1454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001456 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001457 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001459 case -EINVAL:
1460 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001461 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001462 "start_IO: -EINVAL device currently "
1463 "not accessible");
1464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001466 /* internal error 11 - unknown rc */
1467 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1468 dev_err(&device->cdev->dev,
1469 "An error occurred in the DASD device driver, "
1470 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 BUG();
1472 break;
1473 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001474 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return rc;
1476}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001477EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479/*
1480 * Timeout function for dasd devices. This is used for different purposes
1481 * 1) missing interrupt handler for normal operation
1482 * 2) delayed start of request where start_IO failed with -EBUSY
1483 * 3) timeout for missing state change interrupts
1484 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1485 * DASD_CQR_QUEUED for 2) and 3).
1486 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001487static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 unsigned long flags;
1490 struct dasd_device *device;
1491
1492 device = (struct dasd_device *) ptr;
1493 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1494 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001495 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001497 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/*
1501 * Setup timeout for a device in jiffies.
1502 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001503void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001505 if (expires == 0)
1506 del_timer(&device->timer);
1507 else
1508 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001510EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512/*
1513 * Clear timeout for a device.
1514 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001515void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001517 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001519EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001521static void dasd_handle_killed_request(struct ccw_device *cdev,
1522 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct dasd_ccw_req *cqr;
1525 struct dasd_device *device;
1526
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001527 if (!intparm)
1528 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 cqr = (struct dasd_ccw_req *) intparm;
1530 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001531 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1532 "invalid status in handle_killed_request: "
1533 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return;
1535 }
1536
Stefan Haberland589c74d2010-02-26 22:37:47 +01001537 device = dasd_device_from_cdev_locked(cdev);
1538 if (IS_ERR(device)) {
1539 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1540 "unable to get device from cdev");
1541 return;
1542 }
1543
1544 if (!cqr->startdev ||
1545 device != cqr->startdev ||
1546 strncmp(cqr->startdev->discipline->ebcname,
1547 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001548 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1549 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001550 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return;
1552 }
1553
1554 /* Schedule request to be retried. */
1555 cqr->status = DASD_CQR_QUEUED;
1556
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001557 dasd_device_clear_timer(device);
1558 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 dasd_put_device(device);
1560}
1561
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001562void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001564 /* First of all start sense subsystem status request. */
1565 dasd_eer_snss(device);
1566
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001567 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001568 dasd_schedule_device_bh(device);
1569 if (device->block)
1570 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001572EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574/*
1575 * Interrupt handler for "normal" ssch-io based dasd devices.
1576 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001577void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1578 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 struct dasd_ccw_req *cqr, *next;
1581 struct dasd_device *device;
1582 unsigned long long now;
1583 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (IS_ERR(irb)) {
1586 switch (PTR_ERR(irb)) {
1587 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
1589 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001590 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1591 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 break;
1593 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001594 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1595 "unknown error %ld\n", __func__,
1596 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001598 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return;
1600 }
1601
Heiko Carstens1aae0562013-01-30 09:49:40 +01001602 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001603 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001604 /* check for conditions that should be handled immediately */
1605 if (!cqr ||
1606 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1607 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001608 if (cqr)
1609 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001610 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001611 if (IS_ERR(device))
1612 return;
1613 /* ignore unsolicited interrupts for DIAG discipline */
1614 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001618 device->discipline->dump_sense_dbf(device, irb, "int");
1619 if (device->features & DASD_FEATURE_ERPLOG)
1620 device->discipline->dump_sense(device, cqr, irb);
1621 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001622 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001624
1625 /* check for for attention message */
1626 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1627 device = dasd_device_from_cdev_locked(cdev);
1628 device->discipline->check_attention(device, irb->esw.esw1.lpum);
1629 dasd_put_device(device);
1630 }
1631
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001632 if (!cqr)
1633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001635 device = (struct dasd_device *) cqr->startdev;
1636 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001638 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1639 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return;
1641 }
1642
1643 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001644 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001645 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001646 cqr->status = DASD_CQR_CLEARED;
1647 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001648 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001649 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return;
1651 }
1652
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001653 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001655 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1656 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return;
1658 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001659
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001660 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001662 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1663 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001664 /* request was completed successfully */
1665 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 cqr->stopclk = now;
1667 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001668 if (cqr->devlist.next != &device->ccw_queue) {
1669 next = list_entry(cqr->devlist.next,
1670 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001672 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001673 /*
1674 * If we don't want complex ERP for this request, then just
1675 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001676 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001677 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001678 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001679 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001680 DBF_DEV_EVENT(DBF_DEBUG, device,
1681 "default ERP in fastpath "
1682 "(%i retries left)",
1683 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001684 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1685 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001686 cqr->status = DASD_CQR_QUEUED;
1687 next = cqr;
1688 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001690 }
1691 if (next && (next->status == DASD_CQR_QUEUED) &&
1692 (!device->stopped)) {
1693 if (device->discipline->start_IO(next) == 0)
1694 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001697 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001699 dasd_device_clear_timer(device);
1700 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001702EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001704enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1705{
1706 struct dasd_device *device;
1707
1708 device = dasd_device_from_cdev_locked(cdev);
1709
1710 if (IS_ERR(device))
1711 goto out;
1712 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1713 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001714 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001715 dasd_put_device(device);
1716 goto out;
1717 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001718 if (device->discipline->dump_sense_dbf)
1719 device->discipline->dump_sense_dbf(device, irb, "uc");
1720 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001721 dasd_put_device(device);
1722out:
1723 return UC_TODO_RETRY;
1724}
1725EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001728 * If we have an error on a dasd_block layer request then we cancel
1729 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001731static void __dasd_device_recovery(struct dasd_device *device,
1732 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct list_head *l, *n;
1735 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001738 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001742
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001743 list_for_each_safe(l, n, &device->ccw_queue) {
1744 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1745 if (cqr->status == DASD_CQR_QUEUED &&
1746 ref_cqr->block == cqr->block) {
1747 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001749 }
1750};
1751
1752/*
1753 * Remove those ccw requests from the queue that need to be returned
1754 * to the upper layer.
1755 */
1756static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1757 struct list_head *final_queue)
1758{
1759 struct list_head *l, *n;
1760 struct dasd_ccw_req *cqr;
1761
1762 /* Process request with final status. */
1763 list_for_each_safe(l, n, &device->ccw_queue) {
1764 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1765
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001766 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001767 if (cqr->status == DASD_CQR_QUEUED ||
1768 cqr->status == DASD_CQR_IN_IO ||
1769 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001770 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001771 if (cqr->status == DASD_CQR_ERROR) {
1772 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001774 /* Rechain finished requests to final queue */
1775 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777}
1778
1779/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001780 * the cqrs from the final queue are returned to the upper layer
1781 * by setting a dasd_block state and calling the callback function
1782 */
1783static void __dasd_device_process_final_queue(struct dasd_device *device,
1784 struct list_head *final_queue)
1785{
1786 struct list_head *l, *n;
1787 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001788 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001789 void (*callback)(struct dasd_ccw_req *, void *data);
1790 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001791 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001792
1793 list_for_each_safe(l, n, final_queue) {
1794 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1795 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001796 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001797 callback = cqr->callback;
1798 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001799 if (block)
1800 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001801 switch (cqr->status) {
1802 case DASD_CQR_SUCCESS:
1803 cqr->status = DASD_CQR_DONE;
1804 break;
1805 case DASD_CQR_ERROR:
1806 cqr->status = DASD_CQR_NEED_ERP;
1807 break;
1808 case DASD_CQR_CLEARED:
1809 cqr->status = DASD_CQR_TERMINATED;
1810 break;
1811 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001812 /* internal error 12 - wrong cqr status*/
1813 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1814 dev_err(&device->cdev->dev,
1815 "An error occurred in the DASD device driver, "
1816 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001817 BUG();
1818 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001819 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001820 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001821 if (block)
1822 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001823 }
1824}
1825
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001826/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * Take a look at the first request on the ccw queue and check
1828 * if it reached its expire time. If so, terminate the IO.
1829 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001830static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct dasd_ccw_req *cqr;
1833
1834 if (list_empty(&device->ccw_queue))
1835 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001836 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001837 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1838 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001839 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1840 /*
1841 * IO in safe offline processing should not
1842 * run out of retries
1843 */
1844 cqr->retries++;
1845 }
Horst Hummel29145a62006-12-04 15:40:15 +01001846 if (device->discipline->term_IO(cqr) != 0) {
1847 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001848 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001849 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001850 "ended, retrying in 5 s\n",
1851 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001852 cqr->expires += 5*HZ;
1853 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001854 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001855 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001856 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001857 "remaining\n", cqr, (cqr->expires/HZ),
1858 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 }
1861}
1862
1863/*
1864 * Take a look at the first request on the ccw queue and check
1865 * if it needs to be started.
1866 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001867static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 struct dasd_ccw_req *cqr;
1870 int rc;
1871
1872 if (list_empty(&device->ccw_queue))
1873 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001874 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001875 if (cqr->status != DASD_CQR_QUEUED)
1876 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001877 /* when device is stopped, return request to previous layer
1878 * exception: only the disconnect or unresumed bits are set and the
1879 * cqr is a path verification request
1880 */
1881 if (device->stopped &&
1882 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1883 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1884 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001885 cqr->status = DASD_CQR_CLEARED;
1886 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001887 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001888 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001889
1890 rc = device->discipline->start_IO(cqr);
1891 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001892 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001893 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001894 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001895 } else
1896 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001897 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001898}
1899
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001900static void __dasd_device_check_path_events(struct dasd_device *device)
1901{
1902 int rc;
1903
1904 if (device->path_data.tbvpm) {
1905 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1906 DASD_UNRESUMED_PM))
1907 return;
1908 rc = device->discipline->verify_path(
1909 device, device->path_data.tbvpm);
1910 if (rc)
1911 dasd_device_set_timer(device, 50);
1912 else
1913 device->path_data.tbvpm = 0;
1914 }
1915};
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001918 * Go through all request on the dasd_device request queue,
1919 * terminate them on the cdev if necessary, and return them to the
1920 * submitting layer via callback.
1921 * Note:
1922 * Make sure that all 'submitting layers' still exist when
1923 * this function is called!. In other words, when 'device' is a base
1924 * device then all block layer requests must have been removed before
1925 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001927int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001929 struct dasd_ccw_req *cqr, *n;
1930 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 INIT_LIST_HEAD(&flush_queue);
1934 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001935 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001936 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001937 /* Check status and move request to flush_queue */
1938 switch (cqr->status) {
1939 case DASD_CQR_IN_IO:
1940 rc = device->discipline->term_IO(cqr);
1941 if (rc) {
1942 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001943 dev_err(&device->cdev->dev,
1944 "Flushing the DASD request queue "
1945 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001946 /* stop flush processing */
1947 goto finished;
1948 }
1949 break;
1950 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001951 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001952 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001953 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001955 break;
1956 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001957 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001958 }
Horst Hummel8f617012006-08-30 14:33:33 +02001959finished:
1960 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961 /*
1962 * After this point all requests must be in state CLEAR_PENDING,
1963 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1964 * one of the others.
1965 */
1966 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1967 wait_event(dasd_flush_wq,
1968 (cqr->status != DASD_CQR_CLEAR_PENDING));
1969 /*
1970 * Now set each request back to TERMINATED, DONE or NEED_ERP
1971 * and call the callback function of flushed requests
1972 */
1973 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001974 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001976EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978/*
1979 * Acquire the device lock and process queues for the device.
1980 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001981static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 atomic_set (&device->tasklet_scheduled, 0);
1986 INIT_LIST_HEAD(&final_queue);
1987 spin_lock_irq(get_ccwdev_lock(device->cdev));
1988 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001989 __dasd_device_check_expire(device);
1990 /* find final requests on ccw queue */
1991 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001992 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1994 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001995 __dasd_device_process_final_queue(device, &final_queue);
1996 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001998 __dasd_device_start_head(device);
1999 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002000 if (waitqueue_active(&shutdown_waitq))
2001 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 dasd_put_device(device);
2003}
2004
2005/*
2006 * Schedules a call to dasd_tasklet over the device tasklet.
2007 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002008void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002011 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return;
2013 dasd_get_device(device);
2014 tasklet_hi_schedule(&device->tasklet);
2015}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002016EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002018void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2019{
2020 device->stopped |= bits;
2021}
2022EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2023
2024void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2025{
2026 device->stopped &= ~bits;
2027 if (!device->stopped)
2028 wake_up(&generic_waitq);
2029}
2030EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002033 * Queue a request to the head of the device ccw_queue.
2034 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002036void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 struct dasd_device *device;
2039 unsigned long flags;
2040
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002041 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002043 cqr->status = DASD_CQR_QUEUED;
2044 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002046 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2048}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002049EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052 * Queue a request to the tail of the device ccw_queue.
2053 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002055void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 struct dasd_device *device;
2058 unsigned long flags;
2059
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002060 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 cqr->status = DASD_CQR_QUEUED;
2063 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002065 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2067}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002068EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002071 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002073void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002075 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2076 cqr->callback_data = DASD_SLEEPON_END_TAG;
2077 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2078 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
Stefan Haberland5915a872011-10-30 15:16:57 +01002080EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002082static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct dasd_device *device;
2085 int rc;
2086
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002087 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002089 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2091 return rc;
2092}
2093
2094/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002095 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2096 */
2097static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2098{
2099 struct dasd_device *device;
2100 dasd_erp_fn_t erp_fn;
2101
2102 if (cqr->status == DASD_CQR_FILLED)
2103 return 0;
2104 device = cqr->startdev;
2105 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2106 if (cqr->status == DASD_CQR_TERMINATED) {
2107 device->discipline->handle_terminated_request(cqr);
2108 return 1;
2109 }
2110 if (cqr->status == DASD_CQR_NEED_ERP) {
2111 erp_fn = device->discipline->erp_action(cqr);
2112 erp_fn(cqr);
2113 return 1;
2114 }
2115 if (cqr->status == DASD_CQR_FAILED)
2116 dasd_log_sense(cqr, &cqr->irb);
2117 if (cqr->refers) {
2118 __dasd_process_erp(device, cqr);
2119 return 1;
2120 }
2121 }
2122 return 0;
2123}
2124
2125static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2126{
2127 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2128 if (cqr->refers) /* erp is not done yet */
2129 return 1;
2130 return ((cqr->status != DASD_CQR_DONE) &&
2131 (cqr->status != DASD_CQR_FAILED));
2132 } else
2133 return (cqr->status == DASD_CQR_FILLED);
2134}
2135
2136static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2137{
2138 struct dasd_device *device;
2139 int rc;
2140 struct list_head ccw_queue;
2141 struct dasd_ccw_req *cqr;
2142
2143 INIT_LIST_HEAD(&ccw_queue);
2144 maincqr->status = DASD_CQR_FILLED;
2145 device = maincqr->startdev;
2146 list_add(&maincqr->blocklist, &ccw_queue);
2147 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2148 cqr = list_first_entry(&ccw_queue,
2149 struct dasd_ccw_req, blocklist)) {
2150
2151 if (__dasd_sleep_on_erp(cqr))
2152 continue;
2153 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2154 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002155 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2156 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2157 cqr->status = DASD_CQR_FAILED;
2158 cqr->intrc = -EPERM;
2159 continue;
2160 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002161 /* Non-temporary stop condition will trigger fail fast */
2162 if (device->stopped & ~DASD_STOPPED_PENDING &&
2163 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2164 (!dasd_eer_enabled(device))) {
2165 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002166 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002167 continue;
2168 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002169 /*
2170 * Don't try to start requests if device is stopped
2171 * except path verification requests
2172 */
2173 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2174 if (interruptible) {
2175 rc = wait_event_interruptible(
2176 generic_waitq, !(device->stopped));
2177 if (rc == -ERESTARTSYS) {
2178 cqr->status = DASD_CQR_FAILED;
2179 maincqr->intrc = rc;
2180 continue;
2181 }
2182 } else
2183 wait_event(generic_waitq, !(device->stopped));
2184 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002185 if (!cqr->callback)
2186 cqr->callback = dasd_wakeup_cb;
2187
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002188 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002189 dasd_add_request_tail(cqr);
2190 if (interruptible) {
2191 rc = wait_event_interruptible(
2192 generic_waitq, _wait_for_wakeup(cqr));
2193 if (rc == -ERESTARTSYS) {
2194 dasd_cancel_req(cqr);
2195 /* wait (non-interruptible) for final status */
2196 wait_event(generic_waitq,
2197 _wait_for_wakeup(cqr));
2198 cqr->status = DASD_CQR_FAILED;
2199 maincqr->intrc = rc;
2200 continue;
2201 }
2202 } else
2203 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2204 }
2205
Heiko Carstens1aae0562013-01-30 09:49:40 +01002206 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002207 if ((maincqr->status != DASD_CQR_DONE) &&
2208 (maincqr->intrc != -ERESTARTSYS))
2209 dasd_log_sense(maincqr, &maincqr->irb);
2210 if (maincqr->status == DASD_CQR_DONE)
2211 rc = 0;
2212 else if (maincqr->intrc)
2213 rc = maincqr->intrc;
2214 else
2215 rc = -EIO;
2216 return rc;
2217}
2218
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002219static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2220{
2221 struct dasd_ccw_req *cqr;
2222
2223 list_for_each_entry(cqr, ccw_queue, blocklist) {
2224 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2225 return 0;
2226 }
2227
2228 return 1;
2229}
2230
2231static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2232{
2233 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002234 struct dasd_ccw_req *cqr, *n;
Stefan Haberland362ce842014-10-01 13:04:54 +02002235 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002236
2237retry:
2238 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2239 device = cqr->startdev;
2240 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2241 continue;
2242
2243 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2244 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2245 cqr->status = DASD_CQR_FAILED;
2246 cqr->intrc = -EPERM;
2247 continue;
2248 }
2249 /*Non-temporary stop condition will trigger fail fast*/
2250 if (device->stopped & ~DASD_STOPPED_PENDING &&
2251 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2252 !dasd_eer_enabled(device)) {
2253 cqr->status = DASD_CQR_FAILED;
2254 cqr->intrc = -EAGAIN;
2255 continue;
2256 }
2257
2258 /*Don't try to start requests if device is stopped*/
2259 if (interruptible) {
2260 rc = wait_event_interruptible(
2261 generic_waitq, !device->stopped);
2262 if (rc == -ERESTARTSYS) {
2263 cqr->status = DASD_CQR_FAILED;
2264 cqr->intrc = rc;
2265 continue;
2266 }
2267 } else
2268 wait_event(generic_waitq, !(device->stopped));
2269
2270 if (!cqr->callback)
2271 cqr->callback = dasd_wakeup_cb;
2272 cqr->callback_data = DASD_SLEEPON_START_TAG;
2273 dasd_add_request_tail(cqr);
2274 }
2275
2276 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2277
2278 rc = 0;
2279 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002280 /*
2281 * for alias devices simplify error recovery and
2282 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002283 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002284 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002285 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002286 (cqr->status == DASD_CQR_TERMINATED ||
2287 cqr->status == DASD_CQR_NEED_ERP))
2288 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002289
2290 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002291 if (__dasd_sleep_on_erp(cqr))
2292 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002293 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002294 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002295
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002296 return 0;
2297}
2298
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002299/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002300 * Queue a request to the tail of the device ccw_queue and wait for
2301 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002303int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002305 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002307EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002310 * Start requests from a ccw_queue and wait for their completion.
2311 */
2312int dasd_sleep_on_queue(struct list_head *ccw_queue)
2313{
2314 return _dasd_sleep_on_queue(ccw_queue, 0);
2315}
2316EXPORT_SYMBOL(dasd_sleep_on_queue);
2317
2318/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002319 * Queue a request to the tail of the device ccw_queue and wait
2320 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002322int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002324 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002326EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328/*
2329 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2330 * for eckd devices) the currently running request has to be terminated
2331 * and be put back to status queued, before the special request is added
2332 * to the head of the queue. Then the special request is waited on normally.
2333 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002334static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002337 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 if (list_empty(&device->ccw_queue))
2340 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002341 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002342 rc = device->discipline->term_IO(cqr);
2343 if (!rc)
2344 /*
2345 * CQR terminated because a more important request is pending.
2346 * Undo decreasing of retry counter because this is
2347 * not an error case.
2348 */
2349 cqr->retries++;
2350 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002353int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct dasd_device *device;
2356 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002357
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002358 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002359 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2360 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2361 cqr->status = DASD_CQR_FAILED;
2362 cqr->intrc = -EPERM;
2363 return -EIO;
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 spin_lock_irq(get_ccwdev_lock(device->cdev));
2366 rc = _dasd_term_running_cqr(device);
2367 if (rc) {
2368 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2369 return rc;
2370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002372 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002374 /*
2375 * add new request as second
2376 * first the terminated cqr needs to be finished
2377 */
2378 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002381 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2384
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002385 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002386
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002387 if (cqr->status == DASD_CQR_DONE)
2388 rc = 0;
2389 else if (cqr->intrc)
2390 rc = cqr->intrc;
2391 else
2392 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002393
2394 /* kick tasklets */
2395 dasd_schedule_device_bh(device);
2396 if (device->block)
2397 dasd_schedule_block_bh(device->block);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return rc;
2400}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002401EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403/*
2404 * Cancels a request that was started with dasd_sleep_on_req.
2405 * This is useful to timeout requests. The request will be
2406 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002407 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002408 * negative error code if termination failed
2409 * Cancellation of a request is an asynchronous operation! The calling
2410 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002412int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002414 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 unsigned long flags;
2416 int rc;
2417
2418 rc = 0;
2419 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2420 switch (cqr->status) {
2421 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002422 /* request was not started - just set to cleared */
2423 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002424 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2425 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 break;
2427 case DASD_CQR_IN_IO:
2428 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002429 rc = device->discipline->term_IO(cqr);
2430 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002431 dev_err(&device->cdev->dev,
2432 "Cancelling request %p failed with rc=%d\n",
2433 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002434 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002435 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002438 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002442 dasd_schedule_device_bh(device);
2443 return rc;
2444}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002445EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002446
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002447/*
2448 * SECTION: Operations of the dasd_block layer.
2449 */
2450
2451/*
2452 * Timeout function for dasd_block. This is used when the block layer
2453 * is waiting for something that may not come reliably, (e.g. a state
2454 * change interrupt)
2455 */
2456static void dasd_block_timeout(unsigned long ptr)
2457{
2458 unsigned long flags;
2459 struct dasd_block *block;
2460
2461 block = (struct dasd_block *) ptr;
2462 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2463 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002464 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002465 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2466 dasd_schedule_block_bh(block);
2467}
2468
2469/*
2470 * Setup timeout for a dasd_block in jiffies.
2471 */
2472void dasd_block_set_timer(struct dasd_block *block, int expires)
2473{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002474 if (expires == 0)
2475 del_timer(&block->timer);
2476 else
2477 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002478}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002479EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002480
2481/*
2482 * Clear timeout for a dasd_block.
2483 */
2484void dasd_block_clear_timer(struct dasd_block *block)
2485{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002486 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002487}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002488EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002489
2490/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002491 * Process finished error recovery ccw.
2492 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002493static void __dasd_process_erp(struct dasd_device *device,
2494 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002495{
2496 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002497
2498 if (cqr->status == DASD_CQR_DONE)
2499 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2500 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002501 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002502 erp_fn = device->discipline->erp_postaction(cqr);
2503 erp_fn(cqr);
2504}
2505
2506/*
2507 * Fetch requests from the block device queue.
2508 */
2509static void __dasd_process_request_queue(struct dasd_block *block)
2510{
2511 struct request_queue *queue;
2512 struct request *req;
2513 struct dasd_ccw_req *cqr;
2514 struct dasd_device *basedev;
2515 unsigned long flags;
2516 queue = block->request_queue;
2517 basedev = block->base;
2518 /* No queue ? Then there is nothing to do. */
2519 if (queue == NULL)
2520 return;
2521
2522 /*
2523 * We requeue request from the block device queue to the ccw
2524 * queue only in two states. In state DASD_STATE_READY the
2525 * partition detection is done and we need to requeue requests
2526 * for that. State DASD_STATE_ONLINE is normal block device
2527 * operation.
2528 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002529 if (basedev->state < DASD_STATE_READY) {
2530 while ((req = blk_fetch_request(block->request_queue)))
2531 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002532 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002533 }
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002534
2535 /* if device ist stopped do not fetch new requests */
2536 if (basedev->stopped)
2537 return;
2538
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002539 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002540 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002541 if (basedev->features & DASD_FEATURE_READONLY &&
2542 rq_data_dir(req) == WRITE) {
2543 DBF_DEV_EVENT(DBF_ERR, basedev,
2544 "Rejecting write request %p",
2545 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002546 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002547 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002548 continue;
2549 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002550 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2551 (basedev->features & DASD_FEATURE_FAILFAST ||
2552 blk_noretry_request(req))) {
2553 DBF_DEV_EVENT(DBF_ERR, basedev,
2554 "Rejecting failfast request %p",
2555 req);
2556 blk_start_request(req);
2557 __blk_end_request_all(req, -ETIMEDOUT);
2558 continue;
2559 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002560 cqr = basedev->discipline->build_cp(basedev, block, req);
2561 if (IS_ERR(cqr)) {
2562 if (PTR_ERR(cqr) == -EBUSY)
2563 break; /* normal end condition */
2564 if (PTR_ERR(cqr) == -ENOMEM)
2565 break; /* terminate request queue loop */
2566 if (PTR_ERR(cqr) == -EAGAIN) {
2567 /*
2568 * The current request cannot be build right
2569 * now, we have to try later. If this request
2570 * is the head-of-queue we stop the device
2571 * for 1/2 second.
2572 */
2573 if (!list_empty(&block->ccw_queue))
2574 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002575 spin_lock_irqsave(
2576 get_ccwdev_lock(basedev->cdev), flags);
2577 dasd_device_set_stop_bits(basedev,
2578 DASD_STOPPED_PENDING);
2579 spin_unlock_irqrestore(
2580 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002581 dasd_block_set_timer(block, HZ/2);
2582 break;
2583 }
2584 DBF_DEV_EVENT(DBF_ERR, basedev,
2585 "CCW creation failed (rc=%ld) "
2586 "on request %p",
2587 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002588 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002589 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002590 continue;
2591 }
2592 /*
2593 * Note: callback is set to dasd_return_cqr_cb in
2594 * __dasd_block_start_head to cover erp requests as well
2595 */
2596 cqr->callback_data = (void *) req;
2597 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002598 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002599 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002600 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002601 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002602 dasd_profile_start(block, cqr, req);
2603 }
2604}
2605
2606static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2607{
2608 struct request *req;
2609 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002610 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002611
2612 req = (struct request *) cqr->callback_data;
2613 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002614 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002615 if (status < 0)
2616 error = status;
2617 else if (status == 0) {
2618 if (cqr->intrc == -EPERM)
2619 error = -EBADE;
2620 else if (cqr->intrc == -ENOLINK ||
2621 cqr->intrc == -ETIMEDOUT)
2622 error = cqr->intrc;
2623 else
2624 error = -EIO;
2625 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002626 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002627}
2628
2629/*
2630 * Process ccw request queue.
2631 */
2632static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2633 struct list_head *final_queue)
2634{
2635 struct list_head *l, *n;
2636 struct dasd_ccw_req *cqr;
2637 dasd_erp_fn_t erp_fn;
2638 unsigned long flags;
2639 struct dasd_device *base = block->base;
2640
2641restart:
2642 /* Process request with final status. */
2643 list_for_each_safe(l, n, &block->ccw_queue) {
2644 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2645 if (cqr->status != DASD_CQR_DONE &&
2646 cqr->status != DASD_CQR_FAILED &&
2647 cqr->status != DASD_CQR_NEED_ERP &&
2648 cqr->status != DASD_CQR_TERMINATED)
2649 continue;
2650
2651 if (cqr->status == DASD_CQR_TERMINATED) {
2652 base->discipline->handle_terminated_request(cqr);
2653 goto restart;
2654 }
2655
2656 /* Process requests that may be recovered */
2657 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002658 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002659 if (IS_ERR(erp_fn(cqr)))
2660 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002661 goto restart;
2662 }
2663
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002664 /* log sense for fatal error */
2665 if (cqr->status == DASD_CQR_FAILED) {
2666 dasd_log_sense(cqr, &cqr->irb);
2667 }
2668
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002669 /* First of all call extended error reporting. */
2670 if (dasd_eer_enabled(base) &&
2671 cqr->status == DASD_CQR_FAILED) {
2672 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2673
2674 /* restart request */
2675 cqr->status = DASD_CQR_FILLED;
2676 cqr->retries = 255;
2677 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002678 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002679 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2680 flags);
2681 goto restart;
2682 }
2683
2684 /* Process finished ERP request. */
2685 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002686 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002687 goto restart;
2688 }
2689
2690 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002691 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002692 list_move_tail(&cqr->blocklist, final_queue);
2693 }
2694}
2695
2696static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2697{
2698 dasd_schedule_block_bh(cqr->block);
2699}
2700
2701static void __dasd_block_start_head(struct dasd_block *block)
2702{
2703 struct dasd_ccw_req *cqr;
2704
2705 if (list_empty(&block->ccw_queue))
2706 return;
2707 /* We allways begin with the first requests on the queue, as some
2708 * of previously started requests have to be enqueued on a
2709 * dasd_device again for error recovery.
2710 */
2711 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2712 if (cqr->status != DASD_CQR_FILLED)
2713 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002714 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2715 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2716 cqr->status = DASD_CQR_FAILED;
2717 cqr->intrc = -EPERM;
2718 dasd_schedule_block_bh(block);
2719 continue;
2720 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002721 /* Non-temporary stop condition will trigger fail fast */
2722 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2723 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2724 (!dasd_eer_enabled(block->base))) {
2725 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002726 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002727 dasd_schedule_block_bh(block);
2728 continue;
2729 }
2730 /* Don't try to start requests if device is stopped */
2731 if (block->base->stopped)
2732 return;
2733
2734 /* just a fail safe check, should not happen */
2735 if (!cqr->startdev)
2736 cqr->startdev = block->base;
2737
2738 /* make sure that the requests we submit find their way back */
2739 cqr->callback = dasd_return_cqr_cb;
2740
2741 dasd_add_request_tail(cqr);
2742 }
2743}
2744
2745/*
2746 * Central dasd_block layer routine. Takes requests from the generic
2747 * block layer request queue, creates ccw requests, enqueues them on
2748 * a dasd_device and processes ccw requests that have been returned.
2749 */
2750static void dasd_block_tasklet(struct dasd_block *block)
2751{
2752 struct list_head final_queue;
2753 struct list_head *l, *n;
2754 struct dasd_ccw_req *cqr;
2755
2756 atomic_set(&block->tasklet_scheduled, 0);
2757 INIT_LIST_HEAD(&final_queue);
2758 spin_lock(&block->queue_lock);
2759 /* Finish off requests on ccw queue */
2760 __dasd_process_block_ccw_queue(block, &final_queue);
2761 spin_unlock(&block->queue_lock);
2762 /* Now call the callback function of requests with final status */
2763 spin_lock_irq(&block->request_queue_lock);
2764 list_for_each_safe(l, n, &final_queue) {
2765 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2766 list_del_init(&cqr->blocklist);
2767 __dasd_cleanup_cqr(cqr);
2768 }
2769 spin_lock(&block->queue_lock);
2770 /* Get new request from the block device request queue */
2771 __dasd_process_request_queue(block);
2772 /* Now check if the head of the ccw queue needs to be started. */
2773 __dasd_block_start_head(block);
2774 spin_unlock(&block->queue_lock);
2775 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002776 if (waitqueue_active(&shutdown_waitq))
2777 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002778 dasd_put_device(block->base);
2779}
2780
2781static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2782{
2783 wake_up(&dasd_flush_wq);
2784}
2785
2786/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002787 * Requeue a request back to the block request queue
2788 * only works for block requests
2789 */
2790static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2791{
2792 struct dasd_block *block = cqr->block;
2793 struct request *req;
2794 unsigned long flags;
2795
2796 if (!block)
2797 return -EINVAL;
2798 spin_lock_irqsave(&block->queue_lock, flags);
2799 req = (struct request *) cqr->callback_data;
2800 blk_requeue_request(block->request_queue, req);
2801 spin_unlock_irqrestore(&block->queue_lock, flags);
2802
2803 return 0;
2804}
2805
2806/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002807 * Go through all request on the dasd_block request queue, cancel them
2808 * on the respective dasd_device, and return them to the generic
2809 * block layer.
2810 */
2811static int dasd_flush_block_queue(struct dasd_block *block)
2812{
2813 struct dasd_ccw_req *cqr, *n;
2814 int rc, i;
2815 struct list_head flush_queue;
2816
2817 INIT_LIST_HEAD(&flush_queue);
2818 spin_lock_bh(&block->queue_lock);
2819 rc = 0;
2820restart:
2821 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2822 /* if this request currently owned by a dasd_device cancel it */
2823 if (cqr->status >= DASD_CQR_QUEUED)
2824 rc = dasd_cancel_req(cqr);
2825 if (rc < 0)
2826 break;
2827 /* Rechain request (including erp chain) so it won't be
2828 * touched by the dasd_block_tasklet anymore.
2829 * Replace the callback so we notice when the request
2830 * is returned from the dasd_device layer.
2831 */
2832 cqr->callback = _dasd_wake_block_flush_cb;
2833 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2834 list_move_tail(&cqr->blocklist, &flush_queue);
2835 if (i > 1)
2836 /* moved more than one request - need to restart */
2837 goto restart;
2838 }
2839 spin_unlock_bh(&block->queue_lock);
2840 /* Now call the callback function of flushed requests */
2841restart_cb:
2842 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2843 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2844 /* Process finished ERP request. */
2845 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002846 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002847 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002848 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002849 /* restart list_for_xx loop since dasd_process_erp
2850 * might remove multiple elements */
2851 goto restart_cb;
2852 }
2853 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002854 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002855 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002856 list_del_init(&cqr->blocklist);
2857 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002858 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return rc;
2861}
2862
2863/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002864 * Schedules a call to dasd_tasklet over the device tasklet.
2865 */
2866void dasd_schedule_block_bh(struct dasd_block *block)
2867{
2868 /* Protect against rescheduling. */
2869 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2870 return;
2871 /* life cycle of block is bound to it's base device */
2872 dasd_get_device(block->base);
2873 tasklet_hi_schedule(&block->tasklet);
2874}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002875EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002876
2877
2878/*
2879 * SECTION: external block device operations
2880 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 */
2882
2883/*
2884 * Dasd request queue function. Called from ll_rw_blk.c
2885 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002886static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002888 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002890 block = queue->queuedata;
2891 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002893 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002895 __dasd_block_start_head(block);
2896 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002900 * Block timeout callback, called from the block layer
2901 *
2902 * request_queue lock is held on entry.
2903 *
2904 * Return values:
2905 * BLK_EH_RESET_TIMER if the request should be left running
2906 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2907 * by the driver.
2908 */
2909enum blk_eh_timer_return dasd_times_out(struct request *req)
2910{
2911 struct dasd_ccw_req *cqr = req->completion_data;
2912 struct dasd_block *block = req->q->queuedata;
2913 struct dasd_device *device;
2914 int rc = 0;
2915
2916 if (!cqr)
2917 return BLK_EH_NOT_HANDLED;
2918
2919 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002920 if (!device->blk_timeout)
2921 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002922 DBF_DEV_EVENT(DBF_WARNING, device,
2923 " dasd_times_out cqr %p status %x",
2924 cqr, cqr->status);
2925
2926 spin_lock(&block->queue_lock);
2927 spin_lock(get_ccwdev_lock(device->cdev));
2928 cqr->retries = -1;
2929 cqr->intrc = -ETIMEDOUT;
2930 if (cqr->status >= DASD_CQR_QUEUED) {
2931 spin_unlock(get_ccwdev_lock(device->cdev));
2932 rc = dasd_cancel_req(cqr);
2933 } else if (cqr->status == DASD_CQR_FILLED ||
2934 cqr->status == DASD_CQR_NEED_ERP) {
2935 cqr->status = DASD_CQR_TERMINATED;
2936 spin_unlock(get_ccwdev_lock(device->cdev));
2937 } else if (cqr->status == DASD_CQR_IN_ERP) {
2938 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2939
2940 list_for_each_entry_safe(searchcqr, nextcqr,
2941 &block->ccw_queue, blocklist) {
2942 tmpcqr = searchcqr;
2943 while (tmpcqr->refers)
2944 tmpcqr = tmpcqr->refers;
2945 if (tmpcqr != cqr)
2946 continue;
2947 /* searchcqr is an ERP request for cqr */
2948 searchcqr->retries = -1;
2949 searchcqr->intrc = -ETIMEDOUT;
2950 if (searchcqr->status >= DASD_CQR_QUEUED) {
2951 spin_unlock(get_ccwdev_lock(device->cdev));
2952 rc = dasd_cancel_req(searchcqr);
2953 spin_lock(get_ccwdev_lock(device->cdev));
2954 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2955 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2956 searchcqr->status = DASD_CQR_TERMINATED;
2957 rc = 0;
2958 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2959 /*
2960 * Shouldn't happen; most recent ERP
2961 * request is at the front of queue
2962 */
2963 continue;
2964 }
2965 break;
2966 }
2967 spin_unlock(get_ccwdev_lock(device->cdev));
2968 }
2969 dasd_schedule_block_bh(block);
2970 spin_unlock(&block->queue_lock);
2971
2972 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2973}
2974
2975/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 * Allocate and initialize request queue and default I/O scheduler.
2977 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002978static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002980 block->request_queue = blk_init_queue(do_dasd_request,
2981 &block->request_queue_lock);
2982 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return -ENOMEM;
2984
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002985 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01002987 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
2990/*
2991 * Allocate and initialize request queue.
2992 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002993static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 int max;
2996
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002997 if (block->base->features & DASD_FEATURE_USERAW) {
2998 /*
2999 * the max_blocks value for raw_track access is 256
3000 * it is higher than the native ECKD value because we
3001 * only need one ccw per track
3002 * so the max_hw_sectors are
3003 * 2048 x 512B = 1024kB = 16 tracks
3004 */
3005 max = 2048;
3006 } else {
3007 max = block->base->discipline->max_blocks << block->s2b_shift;
3008 }
3009 blk_queue_logical_block_size(block->request_queue,
3010 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003011 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003012 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003013 /* with page sized segments we can translate each segement into
3014 * one idaw/tidaw
3015 */
3016 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3017 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020/*
3021 * Deactivate and free request queue.
3022 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003023static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003025 if (block->request_queue) {
3026 blk_cleanup_queue(block->request_queue);
3027 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029}
3030
3031/*
3032 * Flush request on the request queue.
3033 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003034static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct request *req;
3037
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003038 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003040
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003041 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003042 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003043 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003044 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Al Viro57a7c0b2008-03-02 10:36:08 -05003047static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003049 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 int rc;
3051
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003052 base = dasd_device_from_gendisk(bdev->bd_disk);
3053 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003054 return -ENODEV;
3055
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003056 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003057 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 rc = -ENODEV;
3059 goto unlock;
3060 }
3061
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003062 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 rc = -EINVAL;
3064 goto unlock;
3065 }
3066
3067 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003068 dev_info(&base->cdev->dev,
3069 "Accessing the DASD failed because it is in "
3070 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 rc = -EPERM;
3072 goto out;
3073 }
3074
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003075 if (base->state <= DASD_STATE_BASIC) {
3076 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 " Cannot open unrecognized device");
3078 rc = -ENODEV;
3079 goto out;
3080 }
3081
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003082 if ((mode & FMODE_WRITE) &&
3083 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3084 (base->features & DASD_FEATURE_READONLY))) {
3085 rc = -EROFS;
3086 goto out;
3087 }
3088
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003089 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return 0;
3091
3092out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003093 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003095 atomic_dec(&base->block->open_count);
3096 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return rc;
3098}
3099
Al Virodb2a1442013-05-05 21:52:57 -04003100static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Al Virodb2a1442013-05-05 21:52:57 -04003102 struct dasd_device *base = dasd_device_from_gendisk(disk);
3103 if (base) {
3104 atomic_dec(&base->block->open_count);
3105 module_put(base->discipline->owner);
3106 dasd_put_device(base);
3107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003110/*
3111 * Return disk geometry.
3112 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003113static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003114{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003115 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003116
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003117 base = dasd_device_from_gendisk(bdev->bd_disk);
3118 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003119 return -ENODEV;
3120
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003121 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003122 !base->discipline->fill_geometry) {
3123 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003124 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003125 }
3126 base->discipline->fill_geometry(base->block, geo);
3127 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3128 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003129 return 0;
3130}
3131
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003132const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133dasd_device_operations = {
3134 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003135 .open = dasd_open,
3136 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003137 .ioctl = dasd_ioctl,
3138 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003139 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140};
3141
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003142/*******************************************************************************
3143 * end of block device operations
3144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
3146static void
3147dasd_exit(void)
3148{
3149#ifdef CONFIG_PROC_FS
3150 dasd_proc_exit();
3151#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003152 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003153 if (dasd_page_cache != NULL) {
3154 kmem_cache_destroy(dasd_page_cache);
3155 dasd_page_cache = NULL;
3156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 dasd_gendisk_exit();
3158 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (dasd_debug_area != NULL) {
3160 debug_unregister(dasd_debug_area);
3161 dasd_debug_area = NULL;
3162 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003163 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
3166/*
3167 * SECTION: common functions for ccw_driver use
3168 */
3169
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003170/*
3171 * Is the device read-only?
3172 * Note that this function does not report the setting of the
3173 * readonly device attribute, but how it is configured in z/VM.
3174 */
3175int dasd_device_is_ro(struct dasd_device *device)
3176{
3177 struct ccw_dev_id dev_id;
3178 struct diag210 diag_data;
3179 int rc;
3180
3181 if (!MACHINE_IS_VM)
3182 return 0;
3183 ccw_device_get_id(device->cdev, &dev_id);
3184 memset(&diag_data, 0, sizeof(diag_data));
3185 diag_data.vrdcdvno = dev_id.devno;
3186 diag_data.vrdclen = sizeof(diag_data);
3187 rc = diag210(&diag_data);
3188 if (rc == 0 || rc == 2) {
3189 return diag_data.vrdcvfla & 0x80;
3190 } else {
3191 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3192 dev_id.devno, rc);
3193 return 0;
3194 }
3195}
3196EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3197
Cornelia Huckf3445a12009-04-14 15:36:23 +02003198static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3199{
3200 struct ccw_device *cdev = data;
3201 int ret;
3202
3203 ret = ccw_device_set_online(cdev);
3204 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003205 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3206 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003207}
3208
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003209/*
3210 * Initial attempt at a probe function. this can be simplified once
3211 * the other detection code is gone.
3212 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003213int dasd_generic_probe(struct ccw_device *cdev,
3214 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 int ret;
3217
3218 ret = dasd_add_sysfs_files(cdev);
3219 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003220 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3221 "dasd_generic_probe: could not add "
3222 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
Horst Hummel40545572006-06-29 15:08:18 +02003225 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Horst Hummel40545572006-06-29 15:08:18 +02003227 /*
3228 * Automatically online either all dasd devices (dasd_autodetect)
3229 * or all devices specified with dasd= parameters during
3230 * initial probe.
3231 */
3232 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003233 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003234 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003237EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003239/*
3240 * This will one day be called from a global not_oper handler.
3241 * It is also used by driver_unregister during module unload.
3242 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003243void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
3245 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003246 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Horst Hummel59afda72005-05-16 21:53:39 -07003248 cdev->handler = NULL;
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003251 if (IS_ERR(device)) {
3252 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003254 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003255 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3256 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 /* Already doing offline processing */
3258 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003259 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return;
3261 }
3262 /*
3263 * This device is removed unconditionally. Set offline
3264 * flag to prevent dasd_open from opening it while it is
3265 * no quite down yet.
3266 */
3267 dasd_set_target_state(device, DASD_STATE_NEW);
3268 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003269 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003271 /*
3272 * life cycle of block is bound to device, so delete it after
3273 * device was safely removed
3274 */
3275 if (block)
3276 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003277
3278 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003280EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003282/*
3283 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003285 * or the user has started activation through sysfs.
3286 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003287int dasd_generic_set_online(struct ccw_device *cdev,
3288 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003290 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003292 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003293
Horst Hummel40545572006-06-29 15:08:18 +02003294 /* first online clears initial online feature flag */
3295 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 device = dasd_create_device(cdev);
3297 if (IS_ERR(device))
3298 return PTR_ERR(device);
3299
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003300 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003301 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003303 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3304 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 dasd_delete_device(device);
3306 return -ENODEV;
3307 }
3308 discipline = dasd_diag_discipline_pointer;
3309 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003310 if (!try_module_get(base_discipline->owner)) {
3311 dasd_delete_device(device);
3312 return -EINVAL;
3313 }
3314 if (!try_module_get(discipline->owner)) {
3315 module_put(base_discipline->owner);
3316 dasd_delete_device(device);
3317 return -EINVAL;
3318 }
3319 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 device->discipline = discipline;
3321
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003322 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 rc = discipline->check_device(device);
3324 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003325 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3326 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003327 module_put(discipline->owner);
3328 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 dasd_delete_device(device);
3330 return rc;
3331 }
3332
3333 dasd_set_target_state(device, DASD_STATE_ONLINE);
3334 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003335 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3336 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 rc = -ENODEV;
3338 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003339 if (device->block)
3340 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 dasd_delete_device(device);
3342 } else
3343 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003344 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003345
3346 wait_event(dasd_init_waitq, _wait_for_device(device));
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return rc;
3350}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003351EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003353int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003356 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003357 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003359 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 device = dasd_device_from_cdev(cdev);
3361 if (IS_ERR(device))
3362 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /*
3365 * We must make sure that this device is currently not in use.
3366 * The open_count is increased for every opener, that includes
3367 * the blkdev_get in dasd_scan_partitions. We are only interested
3368 * in the other openers.
3369 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003370 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003371 max_count = device->block->bdev ? 0 : -1;
3372 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003373 if (open_count > max_count) {
3374 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003375 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3376 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003377 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003378 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3379 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003380 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3381 dasd_put_device(device);
3382 return -EBUSY;
3383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003385
3386 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3387 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003388 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003389 * could only be called by normal offline so safe_offline flag
3390 * needs to be removed to run normal offline and kill all I/O
3391 */
3392 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3393 /* Already doing normal offline processing */
3394 dasd_put_device(device);
3395 return -EBUSY;
3396 } else
3397 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3398
3399 } else
3400 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3401 /* Already doing offline processing */
3402 dasd_put_device(device);
3403 return -EBUSY;
3404 }
3405
3406 /*
3407 * if safe_offline called set safe_offline_running flag and
3408 * clear safe_offline so that a call to normal offline
3409 * can overrun safe_offline processing
3410 */
3411 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3412 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3413 /*
3414 * If we want to set the device safe offline all IO operations
3415 * should be finished before continuing the offline process
3416 * so sync bdev first and then wait for our queues to become
3417 * empty
3418 */
3419 /* sync blockdev and partitions */
3420 rc = fsync_bdev(device->block->bdev);
3421 if (rc != 0)
3422 goto interrupted;
3423
3424 /* schedule device tasklet and wait for completion */
3425 dasd_schedule_device_bh(device);
3426 rc = wait_event_interruptible(shutdown_waitq,
3427 _wait_for_empty_queues(device));
3428 if (rc != 0)
3429 goto interrupted;
3430 }
3431
3432 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 dasd_set_target_state(device, DASD_STATE_NEW);
3434 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003435 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003437 /*
3438 * life cycle of block is bound to device, so delete it after
3439 * device was safely removed
3440 */
3441 if (block)
3442 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003444
3445interrupted:
3446 /* interrupted by signal */
3447 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3448 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3449 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3450 dasd_put_device(device);
3451 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003453EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003455int dasd_generic_last_path_gone(struct dasd_device *device)
3456{
3457 struct dasd_ccw_req *cqr;
3458
3459 dev_warn(&device->cdev->dev, "No operational channel path is left "
3460 "for the device\n");
3461 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3462 /* First of all call extended error reporting. */
3463 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3464
3465 if (device->state < DASD_STATE_BASIC)
3466 return 0;
3467 /* Device is active. We want to keep it. */
3468 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3469 if ((cqr->status == DASD_CQR_IN_IO) ||
3470 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3471 cqr->status = DASD_CQR_QUEUED;
3472 cqr->retries++;
3473 }
3474 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3475 dasd_device_clear_timer(device);
3476 dasd_schedule_device_bh(device);
3477 return 1;
3478}
3479EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3480
3481int dasd_generic_path_operational(struct dasd_device *device)
3482{
3483 dev_info(&device->cdev->dev, "A channel path to the device has become "
3484 "operational\n");
3485 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3486 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3487 if (device->stopped & DASD_UNRESUMED_PM) {
3488 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3489 dasd_restore_device(device);
3490 return 1;
3491 }
3492 dasd_schedule_device_bh(device);
3493 if (device->block)
3494 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003495
3496 if (!device->stopped)
3497 wake_up(&generic_waitq);
3498
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003499 return 1;
3500}
3501EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3502
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003503int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
3505 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 int ret;
3507
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003508 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 if (IS_ERR(device))
3510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 ret = 0;
3512 switch (event) {
3513 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003514 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003516 device->path_data.opm = 0;
3517 device->path_data.ppm = 0;
3518 device->path_data.npm = 0;
3519 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 break;
3521 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003523 if (device->path_data.opm)
3524 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 break;
3526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 dasd_put_device(device);
3528 return ret;
3529}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003530EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003532void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3533{
3534 int chp;
3535 __u8 oldopm, eventlpm;
3536 struct dasd_device *device;
3537
3538 device = dasd_device_from_cdev_locked(cdev);
3539 if (IS_ERR(device))
3540 return;
3541 for (chp = 0; chp < 8; chp++) {
3542 eventlpm = 0x80 >> chp;
3543 if (path_event[chp] & PE_PATH_GONE) {
3544 oldopm = device->path_data.opm;
3545 device->path_data.opm &= ~eventlpm;
3546 device->path_data.ppm &= ~eventlpm;
3547 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003548 if (oldopm && !device->path_data.opm) {
3549 dev_warn(&device->cdev->dev,
3550 "No verified channel paths remain "
3551 "for the device\n");
3552 DBF_DEV_EVENT(DBF_WARNING, device,
3553 "%s", "last verified path gone");
3554 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3555 dasd_device_set_stop_bits(device,
3556 DASD_STOPPED_DC_WAIT);
3557 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003558 }
3559 if (path_event[chp] & PE_PATH_AVAILABLE) {
3560 device->path_data.opm &= ~eventlpm;
3561 device->path_data.ppm &= ~eventlpm;
3562 device->path_data.npm &= ~eventlpm;
3563 device->path_data.tbvpm |= eventlpm;
3564 dasd_schedule_device_bh(device);
3565 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003566 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003567 if (!(device->path_data.opm & eventlpm) &&
3568 !(device->path_data.tbvpm & eventlpm)) {
3569 /*
3570 * we can not establish a pathgroup on an
3571 * unavailable path, so trigger a path
3572 * verification first
3573 */
3574 device->path_data.tbvpm |= eventlpm;
3575 dasd_schedule_device_bh(device);
3576 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003577 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3578 "Pathgroup re-established\n");
3579 if (device->discipline->kick_validate)
3580 device->discipline->kick_validate(device);
3581 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003582 }
3583 dasd_put_device(device);
3584}
3585EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3586
3587int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3588{
3589 if (!device->path_data.opm && lpm) {
3590 device->path_data.opm = lpm;
3591 dasd_generic_path_operational(device);
3592 } else
3593 device->path_data.opm |= lpm;
3594 return 0;
3595}
3596EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3597
3598
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003599int dasd_generic_pm_freeze(struct ccw_device *cdev)
3600{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003601 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003602 struct list_head freeze_queue;
3603 struct dasd_ccw_req *cqr, *n;
3604 struct dasd_ccw_req *refers;
3605 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003606
3607 if (IS_ERR(device))
3608 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003609
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003610 /* mark device as suspended */
3611 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3612
Stefan Haberland6f272b92011-01-05 12:48:05 +01003613 if (device->discipline->freeze)
3614 rc = device->discipline->freeze(device);
3615
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003616 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003617 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003618
3619 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003620 INIT_LIST_HEAD(&freeze_queue);
3621 spin_lock_irq(get_ccwdev_lock(cdev));
3622 rc = 0;
3623 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3624 /* Check status and move request to flush_queue */
3625 if (cqr->status == DASD_CQR_IN_IO) {
3626 rc = device->discipline->term_IO(cqr);
3627 if (rc) {
3628 /* unable to terminate requeust */
3629 dev_err(&device->cdev->dev,
3630 "Unable to terminate request %p "
3631 "on suspend\n", cqr);
3632 spin_unlock_irq(get_ccwdev_lock(cdev));
3633 dasd_put_device(device);
3634 return rc;
3635 }
3636 }
3637 list_move_tail(&cqr->devlist, &freeze_queue);
3638 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003639 spin_unlock_irq(get_ccwdev_lock(cdev));
3640
3641 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3642 wait_event(dasd_flush_wq,
3643 (cqr->status != DASD_CQR_CLEAR_PENDING));
3644 if (cqr->status == DASD_CQR_CLEARED)
3645 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003646
Stefan Haberlandc5576872013-04-15 16:41:31 +02003647 /* requeue requests to blocklayer will only work for
3648 block device requests */
3649 if (_dasd_requeue_request(cqr))
3650 continue;
3651
3652 /* remove requests from device and block queue */
3653 list_del_init(&cqr->devlist);
3654 while (cqr->refers != NULL) {
3655 refers = cqr->refers;
3656 /* remove the request from the block queue */
3657 list_del(&cqr->blocklist);
3658 /* free the finished erp request */
3659 dasd_free_erp_request(cqr, cqr->memdev);
3660 cqr = refers;
3661 }
3662 if (cqr->block)
3663 list_del_init(&cqr->blocklist);
3664 cqr->block->base->discipline->free_cp(
3665 cqr, (struct request *) cqr->callback_data);
3666 }
3667
3668 /*
3669 * if requests remain then they are internal request
3670 * and go back to the device queue
3671 */
3672 if (!list_empty(&freeze_queue)) {
3673 /* move freeze_queue to start of the ccw_queue */
3674 spin_lock_irq(get_ccwdev_lock(cdev));
3675 list_splice_tail(&freeze_queue, &device->ccw_queue);
3676 spin_unlock_irq(get_ccwdev_lock(cdev));
3677 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003678 dasd_put_device(device);
3679 return rc;
3680}
3681EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3682
3683int dasd_generic_restore_device(struct ccw_device *cdev)
3684{
3685 struct dasd_device *device = dasd_device_from_cdev(cdev);
3686 int rc = 0;
3687
3688 if (IS_ERR(device))
3689 return PTR_ERR(device);
3690
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003691 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003692 dasd_device_remove_stop_bits(device,
3693 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003694
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003695 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003696
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003697 /*
3698 * call discipline restore function
3699 * if device is stopped do nothing e.g. for disconnected devices
3700 */
3701 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003702 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003703 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003704 /*
3705 * if the resume failed for the DASD we put it in
3706 * an UNRESUMED stop state
3707 */
3708 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003709
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003710 if (device->block)
3711 dasd_schedule_block_bh(device->block);
3712
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003713 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003714 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003715 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003716}
3717EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3718
Heiko Carstens763968e2007-05-10 15:45:46 +02003719static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3720 void *rdc_buffer,
3721 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003722 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003723{
3724 struct dasd_ccw_req *cqr;
3725 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003726 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003727
3728 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3729
3730 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003731 /* internal error 13 - Allocating the RDC request failed*/
3732 dev_err(&device->cdev->dev,
3733 "An error occurred in the DASD device driver, "
3734 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003735 return cqr;
3736 }
3737
3738 ccw = cqr->cpaddr;
3739 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003740 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3741 idaw = (unsigned long *) (cqr->data);
3742 ccw->cda = (__u32)(addr_t) idaw;
3743 ccw->flags = CCW_FLAG_IDA;
3744 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3745 } else {
3746 ccw->cda = (__u32)(addr_t) rdc_buffer;
3747 ccw->flags = 0;
3748 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003749
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003750 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003751 cqr->startdev = device;
3752 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003753 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003754 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003755 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003756 cqr->status = DASD_CQR_FILLED;
3757 return cqr;
3758}
3759
3760
Stefan Haberland68b781f2009-09-11 10:28:29 +02003761int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003762 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003763{
3764 int ret;
3765 struct dasd_ccw_req *cqr;
3766
Sebastian Ott92636b12009-06-12 10:26:37 +02003767 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003768 magic);
3769 if (IS_ERR(cqr))
3770 return PTR_ERR(cqr);
3771
3772 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003773 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003774 return ret;
3775}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003776EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003777
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003778/*
3779 * In command mode and transport mode we need to look for sense
3780 * data in different places. The sense data itself is allways
3781 * an array of 32 bytes, so we can unify the sense data access
3782 * for both modes.
3783 */
3784char *dasd_get_sense(struct irb *irb)
3785{
3786 struct tsb *tsb = NULL;
3787 char *sense = NULL;
3788
3789 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3790 if (irb->scsw.tm.tcw)
3791 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3792 irb->scsw.tm.tcw);
3793 if (tsb && tsb->length == 64 && tsb->flags)
3794 switch (tsb->flags & 0x07) {
3795 case 1: /* tsa_iostat */
3796 sense = tsb->tsa.iostat.sense;
3797 break;
3798 case 2: /* tsa_ddpc */
3799 sense = tsb->tsa.ddpc.sense;
3800 break;
3801 default:
3802 /* currently we don't use interrogate data */
3803 break;
3804 }
3805 } else if (irb->esw.esw0.erw.cons) {
3806 sense = irb->ecw;
3807 }
3808 return sense;
3809}
3810EXPORT_SYMBOL_GPL(dasd_get_sense);
3811
Stefan Haberland4679e892012-06-19 17:30:12 +02003812void dasd_generic_shutdown(struct ccw_device *cdev)
3813{
3814 struct dasd_device *device;
3815
3816 device = dasd_device_from_cdev(cdev);
3817 if (IS_ERR(device))
3818 return;
3819
3820 if (device->block)
3821 dasd_schedule_block_bh(device->block);
3822
3823 dasd_schedule_device_bh(device);
3824
3825 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3826}
3827EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3828
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003829static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
3831 int rc;
3832
3833 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003834 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003835 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003836 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003839 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 if (dasd_debug_area == NULL) {
3841 rc = -ENOMEM;
3842 goto failed;
3843 }
3844 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003845 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
3847 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3848
3849 dasd_diag_discipline_pointer = NULL;
3850
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003851 dasd_statistics_createroot();
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 rc = dasd_devmap_init();
3854 if (rc)
3855 goto failed;
3856 rc = dasd_gendisk_init();
3857 if (rc)
3858 goto failed;
3859 rc = dasd_parse();
3860 if (rc)
3861 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003862 rc = dasd_eer_init();
3863 if (rc)
3864 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865#ifdef CONFIG_PROC_FS
3866 rc = dasd_proc_init();
3867 if (rc)
3868 goto failed;
3869#endif
3870
3871 return 0;
3872failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003873 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 dasd_exit();
3875 return rc;
3876}
3877
3878module_init(dasd_init);
3879module_exit(dasd_exit);