blob: a67e8dae73c35bf4cfdf58727b5a562cbafc4c45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02007 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Stefan Haberlandfc19f382009-03-26 15:23:49 +010010#define KMSG_COMPONENT "dasd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kmod.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/ctype.h>
17#include <linux/major.h>
18#include <linux/slab.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020020#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010021#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020022#include <linux/debugfs.h>
23#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020024#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010029#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010030#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
41/*
42 * SECTION: exported variables of dasd.c
43 */
44debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020045EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020046static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020048EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010049void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
52MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020053 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_LICENSE("GPL");
56
57/*
58 * SECTION: prototypes for static functions of dasd.c
59 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010060static int dasd_alloc_queue(struct dasd_block *);
61static void dasd_setup_queue(struct dasd_block *);
62static void dasd_free_queue(struct dasd_block *);
63static void dasd_flush_request_queue(struct dasd_block *);
64static int dasd_flush_block_queue(struct dasd_block *);
65static void dasd_device_tasklet(struct dasd_device *);
66static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000067static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020068static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020069static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010070static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010071static void dasd_device_timeout(unsigned long);
72static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010073static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020074static void dasd_profile_init(struct dasd_profile *, struct dentry *);
75static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * SECTION: Operations on the device structure.
79 */
80static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020081static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020082static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020083static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Allocate memory for a new device structure.
87 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010088struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct dasd_device *device;
91
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010092 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
93 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* Get two pages for normal block device operations. */
97 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010098 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kfree(device);
100 return ERR_PTR(-ENOMEM);
101 }
102 /* Get one page for error recovery. */
103 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100104 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 free_pages((unsigned long) device->ccw_mem, 1);
106 kfree(device);
107 return ERR_PTR(-ENOMEM);
108 }
109
110 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
111 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
112 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100113 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200114 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100115 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 (unsigned long) device);
117 INIT_LIST_HEAD(&device->ccw_queue);
118 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100119 device->timer.function = dasd_device_timeout;
120 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000121 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200122 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200123 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 device->state = DASD_STATE_NEW;
125 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100126 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200127 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return device;
129}
130
131/*
132 * Free memory of a device structure.
133 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100134void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800136 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 free_page((unsigned long) device->erp_mem);
138 free_pages((unsigned long) device->ccw_mem, 1);
139 kfree(device);
140}
141
142/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100143 * Allocate memory for a new device structure.
144 */
145struct dasd_block *dasd_alloc_block(void)
146{
147 struct dasd_block *block;
148
149 block = kzalloc(sizeof(*block), GFP_ATOMIC);
150 if (!block)
151 return ERR_PTR(-ENOMEM);
152 /* open_count = 0 means device online but not in use */
153 atomic_set(&block->open_count, -1);
154
155 spin_lock_init(&block->request_queue_lock);
156 atomic_set(&block->tasklet_scheduled, 0);
157 tasklet_init(&block->tasklet,
158 (void (*)(unsigned long)) dasd_block_tasklet,
159 (unsigned long) block);
160 INIT_LIST_HEAD(&block->ccw_queue);
161 spin_lock_init(&block->queue_lock);
162 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100163 block->timer.function = dasd_block_timeout;
164 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200165 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100166
167 return block;
168}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200169EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100170
171/*
172 * Free memory of a device structure.
173 */
174void dasd_free_block(struct dasd_block *block)
175{
176 kfree(block);
177}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200178EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Make a new device known to the system.
182 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100183static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int rc;
186
187 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200188 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * keep the reference count > 0.
190 */
191 dasd_get_device(device);
192
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100193 if (device->block) {
194 rc = dasd_alloc_queue(device->block);
195 if (rc) {
196 dasd_put_device(device);
197 return rc;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 device->state = DASD_STATE_KNOWN;
201 return 0;
202}
203
204/*
205 * Let the system forget about a device.
206 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100207static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800209 /* Disable extended error reporting for this device. */
210 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 if (device->discipline) {
213 if (device->discipline->uncheck_device)
214 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800215 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800218 if (device->base_discipline)
219 module_put(device->base_discipline->owner);
220 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 device->state = DASD_STATE_NEW;
222
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100223 if (device->block)
224 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Give up reference we took in dasd_state_new_to_known. */
227 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200231static struct dentry *dasd_debugfs_setup(const char *name,
232 struct dentry *base_dentry)
233{
234 struct dentry *pde;
235
236 if (!base_dentry)
237 return NULL;
238 pde = debugfs_create_dir(name, base_dentry);
239 if (!pde || IS_ERR(pde))
240 return NULL;
241 return pde;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * Request the irq line for the device.
246 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100247static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200249 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200250 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 if (block) {
254 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100255 if (rc)
256 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200257 block->debugfs_dentry =
258 dasd_debugfs_setup(block->gdp->disk_name,
259 dasd_debugfs_root_entry);
260 dasd_profile_init(&block->profile, block->debugfs_dentry);
261 if (dasd_global_profile_level == DASD_PROFILE_ON)
262 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100263 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200264 device->debugfs_dentry =
265 dasd_debugfs_setup(dev_name(&device->cdev->dev),
266 dasd_debugfs_root_entry);
267 dasd_profile_init(&device->profile, device->debugfs_dentry);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100270 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100271 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200273 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
275
276 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200277
278 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * Release the irq line for the device. Terminate any running i/o.
283 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Horst Hummel8f617012006-08-30 14:33:33 +0200286 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200287
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200288 if (device->discipline->basic_to_known) {
289 rc = device->discipline->basic_to_known(device);
290 if (rc)
291 return rc;
292 }
293
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200295 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200296 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100297 dasd_gendisk_free(device->block);
298 dasd_block_clear_timer(device->block);
299 }
300 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200301 if (rc)
302 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100303 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200304 dasd_profile_exit(&device->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200305 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
307 if (device->debug_area != NULL) {
308 debug_unregister(device->debug_area);
309 device->debug_area = NULL;
310 }
311 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * Do the initial analysis. The do_analysis function may return
317 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
318 * until the discipline decides to continue the startup sequence
319 * by calling the function dasd_change_state. The eckd disciplines
320 * uses this to start a ccw that detects the format. The completion
321 * interrupt for this detection ccw uses the kernel event daemon to
322 * trigger the call to dasd_change_state. All this is done in the
323 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800324 * After the analysis ccw is done (do_analysis returned 0) the block
325 * device is setup.
326 * In case the analysis returns an error, the device setup is stopped
327 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100335 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800336 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100337 if (block) {
338 if (block->base->discipline->do_analysis != NULL)
339 rc = block->base->discipline->do_analysis(block);
340 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200341 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100342 device->state = DASD_STATE_UNFMT;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200343 goto out;
344 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 return rc;
346 }
347 dasd_setup_queue(block);
348 set_capacity(block->gdp,
349 block->blocks << block->s2b_shift);
350 device->state = DASD_STATE_READY;
351 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200352 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200354 return rc;
355 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100356 } else {
357 device->state = DASD_STATE_READY;
358 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200359out:
360 if (device->discipline->basic_to_ready)
361 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100365static inline
366int _wait_for_empty_queues(struct dasd_device *device)
367{
368 if (device->block)
369 return list_empty(&device->ccw_queue) &&
370 list_empty(&device->block->ccw_queue);
371 else
372 return list_empty(&device->ccw_queue);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Remove device from block device layer. Destroy dirty buffers.
377 * Forget format information. Check if the target level is basic
378 * and if it is create fake disk for formatting.
379 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Horst Hummel8f617012006-08-30 14:33:33 +0200382 int rc;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100385 if (device->block) {
386 struct dasd_block *block = device->block;
387 rc = dasd_flush_block_queue(block);
388 if (rc) {
389 device->state = DASD_STATE_READY;
390 return rc;
391 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100392 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100393 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100394 block->blocks = 0;
395 block->bp_block = 0;
396 block->s2b_shift = 0;
397 }
Horst Hummel8f617012006-08-30 14:33:33 +0200398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
Horst Hummel90f00942006-03-07 21:55:39 -0800402 * Back to basic.
403 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100404static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800405{
406 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200407 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800408}
409
410/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * Make the device online and schedule the bottom half to start
412 * the requeueing of requests from the linux request queue to the
413 * ccw queue.
414 */
Horst Hummel8f617012006-08-30 14:33:33 +0200415static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416dasd_state_ready_to_online(struct dasd_device * device)
417{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100418 struct gendisk *disk;
419 struct disk_part_iter piter;
420 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100423 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100424 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100425 if ((device->features & DASD_FEATURE_USERAW)) {
426 disk = device->block->gdp;
427 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
428 return 0;
429 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100430 disk = device->block->bdev->bd_disk;
431 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
432 while ((part = disk_part_iter_next(&piter)))
433 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
434 disk_part_iter_exit(&piter);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
439/*
440 * Stop the requeueing of requests again.
441 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100442static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100445 struct gendisk *disk;
446 struct disk_part_iter piter;
447 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100448
449 if (device->discipline->online_to_ready) {
450 rc = device->discipline->online_to_ready(device);
451 if (rc)
452 return rc;
453 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100456 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100457 disk = device->block->bdev->bd_disk;
458 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
459 while ((part = disk_part_iter_next(&piter)))
460 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
461 disk_part_iter_exit(&piter);
462 }
Horst Hummel8f617012006-08-30 14:33:33 +0200463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * Device startup state changes.
468 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100469static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int rc;
472
473 rc = 0;
474 if (device->state == DASD_STATE_NEW &&
475 device->target >= DASD_STATE_KNOWN)
476 rc = dasd_state_new_to_known(device);
477
478 if (!rc &&
479 device->state == DASD_STATE_KNOWN &&
480 device->target >= DASD_STATE_BASIC)
481 rc = dasd_state_known_to_basic(device);
482
483 if (!rc &&
484 device->state == DASD_STATE_BASIC &&
485 device->target >= DASD_STATE_READY)
486 rc = dasd_state_basic_to_ready(device);
487
488 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700489 device->state == DASD_STATE_UNFMT &&
490 device->target > DASD_STATE_UNFMT)
491 rc = -EPERM;
492
493 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 device->state == DASD_STATE_READY &&
495 device->target >= DASD_STATE_ONLINE)
496 rc = dasd_state_ready_to_online(device);
497
498 return rc;
499}
500
501/*
502 * Device shutdown state changes.
503 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100504static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Horst Hummel8f617012006-08-30 14:33:33 +0200506 int rc;
507
508 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (device->state == DASD_STATE_ONLINE &&
510 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200511 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200512
Horst Hummel8f617012006-08-30 14:33:33 +0200513 if (!rc &&
514 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200516 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800517
Horst Hummel8f617012006-08-30 14:33:33 +0200518 if (!rc &&
519 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800520 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200521 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800522
Horst Hummel8f617012006-08-30 14:33:33 +0200523 if (!rc &&
524 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200526 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200527
Horst Hummel8f617012006-08-30 14:33:33 +0200528 if (!rc &&
529 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200531 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Horst Hummel8f617012006-08-30 14:33:33 +0200533 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * This is the main startup/shutdown routine.
538 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100539static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Sebastian Ott181d9522009-06-22 12:08:21 +0200541 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (device->state == device->target)
544 /* Already where we want to go today... */
545 return;
546 if (device->state < device->target)
547 rc = dasd_increase_state(device);
548 else
549 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200550 if (rc == -EAGAIN)
551 return;
552 if (rc)
553 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200555 /* let user-space know that the device status changed */
556 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200557
558 if (device->state == device->target)
559 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Kick starter for devices that did not complete the startup/shutdown
564 * procedure or were sleeping because of a pending state.
565 * dasd_kick_device will schedule a call do do_kick_device to the kernel
566 * event daemon.
567 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Al Viro4927b3f2006-12-06 19:18:20 +0000570 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100571 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100573 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100574 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dasd_put_device(device);
576}
577
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100578void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 dasd_get_device(device);
581 /* queue call to dasd_kick_device to the kernel event daemon. */
582 schedule_work(&device->kick_work);
583}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200584EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200587 * dasd_reload_device will schedule a call do do_reload_device to the kernel
588 * event daemon.
589 */
590static void do_reload_device(struct work_struct *work)
591{
592 struct dasd_device *device = container_of(work, struct dasd_device,
593 reload_device);
594 device->discipline->reload(device);
595 dasd_put_device(device);
596}
597
598void dasd_reload_device(struct dasd_device *device)
599{
600 dasd_get_device(device);
601 /* queue call to dasd_reload_device to the kernel event daemon. */
602 schedule_work(&device->reload_device);
603}
604EXPORT_SYMBOL(dasd_reload_device);
605
606/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200607 * dasd_restore_device will schedule a call do do_restore_device to the kernel
608 * event daemon.
609 */
610static void do_restore_device(struct work_struct *work)
611{
612 struct dasd_device *device = container_of(work, struct dasd_device,
613 restore_device);
614 device->cdev->drv->restore(device->cdev);
615 dasd_put_device(device);
616}
617
618void dasd_restore_device(struct dasd_device *device)
619{
620 dasd_get_device(device);
621 /* queue call to dasd_restore_device to the kernel event daemon. */
622 schedule_work(&device->restore_device);
623}
624
625/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * Set the target state for a device and starts the state change.
627 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100628void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200630 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100631 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* If we are in probeonly mode stop at DASD_STATE_READY. */
633 if (dasd_probeonly && target > DASD_STATE_READY)
634 target = DASD_STATE_READY;
635 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100636 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 wake_up(&dasd_init_waitq);
638 device->target = target;
639 }
640 if (device->state != device->target)
641 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100642 mutex_unlock(&device->state_mutex);
643 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200645EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647/*
648 * Enable devices with device numbers in [from..to].
649 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100650static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 return (device->state == device->target);
653}
654
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100655void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 dasd_set_target_state(device, DASD_STATE_ONLINE);
658 if (device->state <= DASD_STATE_KNOWN)
659 /* No discipline for device found. */
660 dasd_set_target_state(device, DASD_STATE_NEW);
661 /* Now wait for the devices to come up. */
662 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400663
664 dasd_reload_device(device);
665 if (device->discipline->kick_validate)
666 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200668EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/*
671 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
672 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200673
674unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100677static struct dasd_profile_info dasd_global_profile_data;
678struct dasd_profile dasd_global_profile = {
679 .dentry = NULL,
680 .data = &dasd_global_profile_data,
681 .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);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200704 if (dasd_global_profile_level) {
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);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200835 if (dasd_global_profile_level) {
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
887void dasd_global_profile_reset(void)
888{
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100889 dasd_profile_reset(&dasd_global_profile);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200890}
891
892int dasd_profile_on(struct dasd_profile *profile)
893{
894 struct dasd_profile_info *data;
895
896 data = kzalloc(sizeof(*data), GFP_KERNEL);
897 if (!data)
898 return -ENOMEM;
899 spin_lock_bh(&profile->lock);
900 if (profile->data) {
901 spin_unlock_bh(&profile->lock);
902 kfree(data);
903 return 0;
904 }
905 getnstimeofday(&data->starttod);
906 profile->data = data;
907 spin_unlock_bh(&profile->lock);
908 return 0;
909}
910
911void dasd_profile_off(struct dasd_profile *profile)
912{
913 spin_lock_bh(&profile->lock);
914 kfree(profile->data);
915 profile->data = NULL;
916 spin_unlock_bh(&profile->lock);
917}
918
919char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
920{
921 char *buffer;
922
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200923 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200924 if (buffer == NULL)
925 return ERR_PTR(-ENOMEM);
926 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200927 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200928 return ERR_PTR(-EFAULT);
929 }
930 /* got the string, now strip linefeed. */
931 if (buffer[user_len - 1] == '\n')
932 buffer[user_len - 1] = 0;
933 else
934 buffer[user_len] = 0;
935 return buffer;
936}
937
938static ssize_t dasd_stats_write(struct file *file,
939 const char __user *user_buf,
940 size_t user_len, loff_t *pos)
941{
942 char *buffer, *str;
943 int rc;
944 struct seq_file *m = (struct seq_file *)file->private_data;
945 struct dasd_profile *prof = m->private;
946
947 if (user_len > 65536)
948 user_len = 65536;
949 buffer = dasd_get_user_string(user_buf, user_len);
950 if (IS_ERR(buffer))
951 return PTR_ERR(buffer);
952
953 str = skip_spaces(buffer);
954 rc = user_len;
955 if (strncmp(str, "reset", 5) == 0) {
956 dasd_profile_reset(prof);
957 } else if (strncmp(str, "on", 2) == 0) {
958 rc = dasd_profile_on(prof);
959 if (!rc)
960 rc = user_len;
961 } else if (strncmp(str, "off", 3) == 0) {
962 dasd_profile_off(prof);
963 } else
964 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200965 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200966 return rc;
967}
968
969static void dasd_stats_array(struct seq_file *m, unsigned int *array)
970{
971 int i;
972
973 for (i = 0; i < 32; i++)
974 seq_printf(m, "%u ", array[i]);
975 seq_putc(m, '\n');
976}
977
978static void dasd_stats_seq_print(struct seq_file *m,
979 struct dasd_profile_info *data)
980{
981 seq_printf(m, "start_time %ld.%09ld\n",
982 data->starttod.tv_sec, data->starttod.tv_nsec);
983 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
984 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
985 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
986 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200987 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200988 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200989 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200990 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200991 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200992 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200993 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200994 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200995 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200996 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200997 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200998 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200999 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001000 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001001 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001002 dasd_stats_array(m, data->dasd_io_nr_req);
1003 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1004 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1005 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1006 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001007 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001008 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001009 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001010 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001011 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001012 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001013 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001014 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001015 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001016 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001017 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001018 dasd_stats_array(m, data->dasd_read_nr_req);
1019}
1020
1021static int dasd_stats_show(struct seq_file *m, void *v)
1022{
1023 struct dasd_profile *profile;
1024 struct dasd_profile_info *data;
1025
1026 profile = m->private;
1027 spin_lock_bh(&profile->lock);
1028 data = profile->data;
1029 if (!data) {
1030 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001031 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001032 return 0;
1033 }
1034 dasd_stats_seq_print(m, data);
1035 spin_unlock_bh(&profile->lock);
1036 return 0;
1037}
1038
1039static int dasd_stats_open(struct inode *inode, struct file *file)
1040{
1041 struct dasd_profile *profile = inode->i_private;
1042 return single_open(file, dasd_stats_show, profile);
1043}
1044
1045static const struct file_operations dasd_stats_raw_fops = {
1046 .owner = THIS_MODULE,
1047 .open = dasd_stats_open,
1048 .read = seq_read,
1049 .llseek = seq_lseek,
1050 .release = single_release,
1051 .write = dasd_stats_write,
1052};
1053
1054static ssize_t dasd_stats_global_write(struct file *file,
1055 const char __user *user_buf,
1056 size_t user_len, loff_t *pos)
1057{
1058 char *buffer, *str;
1059 ssize_t rc;
1060
1061 if (user_len > 65536)
1062 user_len = 65536;
1063 buffer = dasd_get_user_string(user_buf, user_len);
1064 if (IS_ERR(buffer))
1065 return PTR_ERR(buffer);
1066 str = skip_spaces(buffer);
1067 rc = user_len;
1068 if (strncmp(str, "reset", 5) == 0) {
1069 dasd_global_profile_reset();
1070 } else if (strncmp(str, "on", 2) == 0) {
1071 dasd_global_profile_reset();
1072 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1073 } else if (strncmp(str, "off", 3) == 0) {
1074 dasd_global_profile_level = DASD_PROFILE_OFF;
1075 } else
1076 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001077 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001078 return rc;
1079}
1080
1081static int dasd_stats_global_show(struct seq_file *m, void *v)
1082{
1083 if (!dasd_global_profile_level) {
Fabian Frederickc794caf2014-06-26 19:41:49 +02001084 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001085 return 0;
1086 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +01001087 spin_lock_bh(&dasd_global_profile.lock);
1088 dasd_stats_seq_print(m, dasd_global_profile.data);
1089 spin_unlock_bh(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001090 return 0;
1091}
1092
1093static int dasd_stats_global_open(struct inode *inode, struct file *file)
1094{
1095 return single_open(file, dasd_stats_global_show, NULL);
1096}
1097
1098static const struct file_operations dasd_stats_global_fops = {
1099 .owner = THIS_MODULE,
1100 .open = dasd_stats_global_open,
1101 .read = seq_read,
1102 .llseek = seq_lseek,
1103 .release = single_release,
1104 .write = dasd_stats_global_write,
1105};
1106
1107static void dasd_profile_init(struct dasd_profile *profile,
1108 struct dentry *base_dentry)
1109{
Al Virof4ae40a2011-07-24 04:33:43 -04001110 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001111 struct dentry *pde;
1112
1113 if (!base_dentry)
1114 return;
1115 profile->dentry = NULL;
1116 profile->data = NULL;
1117 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1118 pde = debugfs_create_file("statistics", mode, base_dentry,
1119 profile, &dasd_stats_raw_fops);
1120 if (pde && !IS_ERR(pde))
1121 profile->dentry = pde;
1122 return;
1123}
1124
1125static void dasd_profile_exit(struct dasd_profile *profile)
1126{
1127 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001128 debugfs_remove(profile->dentry);
1129 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001130}
1131
1132static void dasd_statistics_removeroot(void)
1133{
1134 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott8ea55c92015-01-28 18:44:17 +01001135 debugfs_remove(dasd_global_profile.dentry);
1136 dasd_global_profile.dentry = NULL;
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001137 debugfs_remove(dasd_debugfs_global_entry);
1138 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001139}
1140
1141static void dasd_statistics_createroot(void)
1142{
Al Virof4ae40a2011-07-24 04:33:43 -04001143 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001144 struct dentry *pde;
1145
1146 dasd_debugfs_root_entry = NULL;
1147 dasd_debugfs_global_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001148 pde = debugfs_create_dir("dasd", NULL);
1149 if (!pde || IS_ERR(pde))
1150 goto error;
1151 dasd_debugfs_root_entry = pde;
1152 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1153 if (!pde || IS_ERR(pde))
1154 goto error;
1155 dasd_debugfs_global_entry = pde;
1156
1157 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1158 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1159 NULL, &dasd_stats_global_fops);
1160 if (!pde || IS_ERR(pde))
1161 goto error;
Sebastian Ott8ea55c92015-01-28 18:44:17 +01001162 dasd_global_profile.dentry = pde;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001163 return;
1164
1165error:
1166 DBF_EVENT(DBF_ERR, "%s",
1167 "Creation of the dasd debugfs interface failed");
1168 dasd_statistics_removeroot();
1169 return;
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001173#define dasd_profile_start(block, cqr, req) do {} while (0)
1174#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001175
1176static void dasd_statistics_createroot(void)
1177{
1178 return;
1179}
1180
1181static void dasd_statistics_removeroot(void)
1182{
1183 return;
1184}
1185
1186int dasd_stats_generic_show(struct seq_file *m, void *v)
1187{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001188 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001189 return 0;
1190}
1191
1192static void dasd_profile_init(struct dasd_profile *profile,
1193 struct dentry *base_dentry)
1194{
1195 return;
1196}
1197
1198static void dasd_profile_exit(struct dasd_profile *profile)
1199{
1200 return;
1201}
1202
1203int dasd_profile_on(struct dasd_profile *profile)
1204{
1205 return 0;
1206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208#endif /* CONFIG_DASD_PROFILE */
1209
1210/*
1211 * Allocate memory for a channel program with 'cplength' channel
1212 * command words and 'datasize' additional space. There are two
1213 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1214 * memory and 2) dasd_smalloc_request uses the static ccw memory
1215 * that gets allocated for each device.
1216 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001217struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001218 int datasize,
1219 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct dasd_ccw_req *cqr;
1222
1223 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001224 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001225 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001227 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (cqr == NULL)
1229 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 cqr->cpaddr = NULL;
1231 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001232 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 GFP_ATOMIC | GFP_DMA);
1234 if (cqr->cpaddr == NULL) {
1235 kfree(cqr);
1236 return ERR_PTR(-ENOMEM);
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 cqr->data = NULL;
1240 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001241 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001243 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 kfree(cqr);
1245 return ERR_PTR(-ENOMEM);
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001248 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1250 dasd_get_device(device);
1251 return cqr;
1252}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001253EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Stefan Haberland68b781f2009-09-11 10:28:29 +02001255struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001256 int datasize,
1257 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 unsigned long flags;
1260 struct dasd_ccw_req *cqr;
1261 char *data;
1262 int size;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1265 if (cplength > 0)
1266 size += cplength * sizeof(struct ccw1);
1267 if (datasize > 0)
1268 size += datasize;
1269 spin_lock_irqsave(&device->mem_lock, flags);
1270 cqr = (struct dasd_ccw_req *)
1271 dasd_alloc_chunk(&device->ccw_chunks, size);
1272 spin_unlock_irqrestore(&device->mem_lock, flags);
1273 if (cqr == NULL)
1274 return ERR_PTR(-ENOMEM);
1275 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1276 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1277 cqr->cpaddr = NULL;
1278 if (cplength > 0) {
1279 cqr->cpaddr = (struct ccw1 *) data;
1280 data += cplength*sizeof(struct ccw1);
1281 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1282 }
1283 cqr->data = NULL;
1284 if (datasize > 0) {
1285 cqr->data = data;
1286 memset(cqr->data, 0, datasize);
1287 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001288 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1290 dasd_get_device(device);
1291 return cqr;
1292}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001293EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295/*
1296 * Free memory of a channel program. This function needs to free all the
1297 * idal lists that might have been created by dasd_set_cda and the
1298 * struct dasd_ccw_req itself.
1299 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001300void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001302#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct ccw1 *ccw;
1304
1305 /* Clear any idals used for the request. */
1306 ccw = cqr->cpaddr;
1307 do {
1308 clear_normalized_cda(ccw);
1309 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1310#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001311 kfree(cqr->cpaddr);
1312 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 kfree(cqr);
1314 dasd_put_device(device);
1315}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001316EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001318void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 unsigned long flags;
1321
1322 spin_lock_irqsave(&device->mem_lock, flags);
1323 dasd_free_chunk(&device->ccw_chunks, cqr);
1324 spin_unlock_irqrestore(&device->mem_lock, flags);
1325 dasd_put_device(device);
1326}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001327EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329/*
1330 * Check discipline magic in cqr.
1331 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001332static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct dasd_device *device;
1335
1336 if (cqr == NULL)
1337 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001338 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001340 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 " dasd_ccw_req 0x%08x magic doesn't match"
1342 " discipline 0x%08x",
1343 cqr->magic,
1344 *(unsigned int *) device->discipline->name);
1345 return -EINVAL;
1346 }
1347 return 0;
1348}
1349
1350/*
1351 * Terminate the current i/o and set the request to clear_pending.
1352 * Timer keeps device runnig.
1353 * ccw_device_clear can fail if the i/o subsystem
1354 * is in a bad mood.
1355 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001356int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct dasd_device *device;
1359 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001360 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* Check the cqr */
1363 rc = dasd_check_cqr(cqr);
1364 if (rc)
1365 return rc;
1366 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001367 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1369 rc = ccw_device_clear(device->cdev, (long) cqr);
1370 switch (rc) {
1371 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001372 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001373 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001374 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 DBF_DEV_EVENT(DBF_DEBUG, device,
1376 "terminate cqr %p successful",
1377 cqr);
1378 break;
1379 case -ENODEV:
1380 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1381 "device gone, retry");
1382 break;
1383 case -EIO:
1384 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1385 "I/O error, retry");
1386 break;
1387 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001388 /*
1389 * device not valid so no I/O could be running
1390 * handle CQR as termination successful
1391 */
1392 cqr->status = DASD_CQR_CLEARED;
1393 cqr->stopclk = get_tod_clock();
1394 cqr->starttime = 0;
1395 /* no retries for invalid devices */
1396 cqr->retries = -1;
1397 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1398 "EINVAL, handle as terminated");
1399 /* fake rc to success */
1400 rc = 0;
1401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 case -EBUSY:
1403 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1404 "device busy, retry later");
1405 break;
1406 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001407 /* internal error 10 - unknown rc*/
1408 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1409 dev_err(&device->cdev->dev, "An error occurred in the "
1410 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 BUG();
1412 break;
1413 }
1414 retries++;
1415 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001416 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return rc;
1418}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001419EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/*
1422 * Start the i/o. This start_IO can fail if the channel is really busy.
1423 * In that case set up a timer to start the request later.
1424 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001425int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct dasd_device *device;
1428 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001429 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* Check the cqr */
1432 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001433 if (rc) {
1434 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001436 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001437 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001438 if (((cqr->block &&
1439 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1440 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1441 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1442 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1443 "because of stolen lock", cqr);
1444 cqr->status = DASD_CQR_ERROR;
1445 cqr->intrc = -EPERM;
1446 return -EPERM;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001449 /* internal error 14 - start_IO run out of retries */
1450 sprintf(errorstring, "14 %p", cqr);
1451 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1452 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001453 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EIO;
1455 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001456 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 cqr->starttime = jiffies;
1458 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001459 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1460 cqr->lpm &= device->path_data.opm;
1461 if (!cqr->lpm)
1462 cqr->lpm = device->path_data.opm;
1463 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001464 if (cqr->cpmode == 1) {
1465 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1466 (long) cqr, cqr->lpm);
1467 } else {
1468 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1469 (long) cqr, cqr->lpm, 0);
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 switch (rc) {
1472 case 0:
1473 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001476 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 "start_IO: device busy, retry later");
1478 break;
1479 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001480 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 "start_IO: request timeout, retry later");
1482 break;
1483 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001484 /* -EACCES indicates that the request used only a subset of the
1485 * available paths and all these paths are gone. If the lpm of
1486 * this request was only a subset of the opm (e.g. the ppm) then
1487 * we just do a retry with all available paths.
1488 * If we already use the full opm, something is amiss, and we
1489 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001491 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1492 DBF_DEV_EVENT(DBF_WARNING, device,
1493 "start_IO: selected paths gone (%x)",
1494 cqr->lpm);
1495 } else if (cqr->lpm != device->path_data.opm) {
1496 cqr->lpm = device->path_data.opm;
1497 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1498 "start_IO: selected paths gone,"
1499 " retry on all paths");
1500 } else {
1501 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1502 "start_IO: all paths in opm gone,"
1503 " do path verification");
1504 dasd_generic_last_path_gone(device);
1505 device->path_data.opm = 0;
1506 device->path_data.ppm = 0;
1507 device->path_data.npm = 0;
1508 device->path_data.tbvpm =
1509 ccw_device_get_path_mask(device->cdev);
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001513 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001514 "start_IO: -ENODEV device gone, retry");
1515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001517 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001518 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001520 case -EINVAL:
1521 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001522 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001523 "start_IO: -EINVAL device currently "
1524 "not accessible");
1525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001527 /* internal error 11 - unknown rc */
1528 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1529 dev_err(&device->cdev->dev,
1530 "An error occurred in the DASD device driver, "
1531 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 BUG();
1533 break;
1534 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001535 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return rc;
1537}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001538EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/*
1541 * Timeout function for dasd devices. This is used for different purposes
1542 * 1) missing interrupt handler for normal operation
1543 * 2) delayed start of request where start_IO failed with -EBUSY
1544 * 3) timeout for missing state change interrupts
1545 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1546 * DASD_CQR_QUEUED for 2) and 3).
1547 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001548static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 unsigned long flags;
1551 struct dasd_device *device;
1552
1553 device = (struct dasd_device *) ptr;
1554 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1555 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001556 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001558 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561/*
1562 * Setup timeout for a device in jiffies.
1563 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001564void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001566 if (expires == 0)
1567 del_timer(&device->timer);
1568 else
1569 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001571EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573/*
1574 * Clear timeout for a device.
1575 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001576void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001578 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001580EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001582static void dasd_handle_killed_request(struct ccw_device *cdev,
1583 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct dasd_ccw_req *cqr;
1586 struct dasd_device *device;
1587
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001588 if (!intparm)
1589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 cqr = (struct dasd_ccw_req *) intparm;
1591 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001592 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1593 "invalid status in handle_killed_request: "
1594 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return;
1596 }
1597
Stefan Haberland589c74d2010-02-26 22:37:47 +01001598 device = dasd_device_from_cdev_locked(cdev);
1599 if (IS_ERR(device)) {
1600 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1601 "unable to get device from cdev");
1602 return;
1603 }
1604
1605 if (!cqr->startdev ||
1606 device != cqr->startdev ||
1607 strncmp(cqr->startdev->discipline->ebcname,
1608 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001609 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1610 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001611 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return;
1613 }
1614
1615 /* Schedule request to be retried. */
1616 cqr->status = DASD_CQR_QUEUED;
1617
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001618 dasd_device_clear_timer(device);
1619 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 dasd_put_device(device);
1621}
1622
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001623void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001625 /* First of all start sense subsystem status request. */
1626 dasd_eer_snss(device);
1627
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001628 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001629 dasd_schedule_device_bh(device);
1630 if (device->block)
1631 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001633EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635/*
1636 * Interrupt handler for "normal" ssch-io based dasd devices.
1637 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001638void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1639 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct dasd_ccw_req *cqr, *next;
1642 struct dasd_device *device;
1643 unsigned long long now;
1644 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (IS_ERR(irb)) {
1647 switch (PTR_ERR(irb)) {
1648 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001651 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1652 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001655 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1656 "unknown error %ld\n", __func__,
1657 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001659 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return;
1661 }
1662
Heiko Carstens1aae0562013-01-30 09:49:40 +01001663 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001664 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001665 /* check for conditions that should be handled immediately */
1666 if (!cqr ||
1667 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1668 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001669 if (cqr)
1670 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001671 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001672 if (IS_ERR(device))
1673 return;
1674 /* ignore unsolicited interrupts for DIAG discipline */
1675 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001677 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001679 device->discipline->dump_sense_dbf(device, irb, "int");
1680 if (device->features & DASD_FEATURE_ERPLOG)
1681 device->discipline->dump_sense(device, cqr, irb);
1682 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001683 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001685
1686 /* check for for attention message */
1687 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1688 device = dasd_device_from_cdev_locked(cdev);
1689 device->discipline->check_attention(device, irb->esw.esw1.lpum);
1690 dasd_put_device(device);
1691 }
1692
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001693 if (!cqr)
1694 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001696 device = (struct dasd_device *) cqr->startdev;
1697 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001699 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1700 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return;
1702 }
1703
1704 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001705 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001706 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001707 cqr->status = DASD_CQR_CLEARED;
1708 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001709 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001710 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return;
1712 }
1713
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001714 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001716 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1717 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return;
1719 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001720
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001721 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001723 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1724 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001725 /* request was completed successfully */
1726 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 cqr->stopclk = now;
1728 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001729 if (cqr->devlist.next != &device->ccw_queue) {
1730 next = list_entry(cqr->devlist.next,
1731 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001733 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001734 /*
1735 * If we don't want complex ERP for this request, then just
1736 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001737 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001738 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001739 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001740 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001741 DBF_DEV_EVENT(DBF_DEBUG, device,
1742 "default ERP in fastpath "
1743 "(%i retries left)",
1744 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001745 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1746 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001747 cqr->status = DASD_CQR_QUEUED;
1748 next = cqr;
1749 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001751 }
1752 if (next && (next->status == DASD_CQR_QUEUED) &&
1753 (!device->stopped)) {
1754 if (device->discipline->start_IO(next) == 0)
1755 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001758 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 dasd_device_clear_timer(device);
1761 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001763EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001765enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1766{
1767 struct dasd_device *device;
1768
1769 device = dasd_device_from_cdev_locked(cdev);
1770
1771 if (IS_ERR(device))
1772 goto out;
1773 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1774 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001775 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001776 dasd_put_device(device);
1777 goto out;
1778 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001779 if (device->discipline->dump_sense_dbf)
1780 device->discipline->dump_sense_dbf(device, irb, "uc");
1781 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001782 dasd_put_device(device);
1783out:
1784 return UC_TODO_RETRY;
1785}
1786EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001789 * If we have an error on a dasd_block layer request then we cancel
1790 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001792static void __dasd_device_recovery(struct dasd_device *device,
1793 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct list_head *l, *n;
1796 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001799 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001801 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001803
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001804 list_for_each_safe(l, n, &device->ccw_queue) {
1805 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1806 if (cqr->status == DASD_CQR_QUEUED &&
1807 ref_cqr->block == cqr->block) {
1808 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001810 }
1811};
1812
1813/*
1814 * Remove those ccw requests from the queue that need to be returned
1815 * to the upper layer.
1816 */
1817static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1818 struct list_head *final_queue)
1819{
1820 struct list_head *l, *n;
1821 struct dasd_ccw_req *cqr;
1822
1823 /* Process request with final status. */
1824 list_for_each_safe(l, n, &device->ccw_queue) {
1825 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1826
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001827 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001828 if (cqr->status == DASD_CQR_QUEUED ||
1829 cqr->status == DASD_CQR_IN_IO ||
1830 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001831 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001832 if (cqr->status == DASD_CQR_ERROR) {
1833 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001835 /* Rechain finished requests to final queue */
1836 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838}
1839
1840/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001841 * the cqrs from the final queue are returned to the upper layer
1842 * by setting a dasd_block state and calling the callback function
1843 */
1844static void __dasd_device_process_final_queue(struct dasd_device *device,
1845 struct list_head *final_queue)
1846{
1847 struct list_head *l, *n;
1848 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001849 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001850 void (*callback)(struct dasd_ccw_req *, void *data);
1851 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001852 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001853
1854 list_for_each_safe(l, n, final_queue) {
1855 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1856 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001857 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001858 callback = cqr->callback;
1859 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001860 if (block)
1861 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001862 switch (cqr->status) {
1863 case DASD_CQR_SUCCESS:
1864 cqr->status = DASD_CQR_DONE;
1865 break;
1866 case DASD_CQR_ERROR:
1867 cqr->status = DASD_CQR_NEED_ERP;
1868 break;
1869 case DASD_CQR_CLEARED:
1870 cqr->status = DASD_CQR_TERMINATED;
1871 break;
1872 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001873 /* internal error 12 - wrong cqr status*/
1874 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1875 dev_err(&device->cdev->dev,
1876 "An error occurred in the DASD device driver, "
1877 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001878 BUG();
1879 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001880 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001881 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001882 if (block)
1883 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001884 }
1885}
1886
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001887/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 * Take a look at the first request on the ccw queue and check
1889 * if it reached its expire time. If so, terminate the IO.
1890 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001891static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct dasd_ccw_req *cqr;
1894
1895 if (list_empty(&device->ccw_queue))
1896 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001897 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001898 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1899 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001900 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1901 /*
1902 * IO in safe offline processing should not
1903 * run out of retries
1904 */
1905 cqr->retries++;
1906 }
Horst Hummel29145a62006-12-04 15:40:15 +01001907 if (device->discipline->term_IO(cqr) != 0) {
1908 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001909 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001910 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001911 "ended, retrying in 5 s\n",
1912 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001913 cqr->expires += 5*HZ;
1914 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001915 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001916 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001917 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001918 "remaining\n", cqr, (cqr->expires/HZ),
1919 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 }
1922}
1923
1924/*
1925 * Take a look at the first request on the ccw queue and check
1926 * if it needs to be started.
1927 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001928static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct dasd_ccw_req *cqr;
1931 int rc;
1932
1933 if (list_empty(&device->ccw_queue))
1934 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001935 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001936 if (cqr->status != DASD_CQR_QUEUED)
1937 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001938 /* when device is stopped, return request to previous layer
1939 * exception: only the disconnect or unresumed bits are set and the
1940 * cqr is a path verification request
1941 */
1942 if (device->stopped &&
1943 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1944 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1945 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001946 cqr->status = DASD_CQR_CLEARED;
1947 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001948 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001949 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001950
1951 rc = device->discipline->start_IO(cqr);
1952 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001953 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001954 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001955 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001956 } else
1957 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001958 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001959}
1960
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001961static void __dasd_device_check_path_events(struct dasd_device *device)
1962{
1963 int rc;
1964
1965 if (device->path_data.tbvpm) {
1966 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1967 DASD_UNRESUMED_PM))
1968 return;
1969 rc = device->discipline->verify_path(
1970 device, device->path_data.tbvpm);
1971 if (rc)
1972 dasd_device_set_timer(device, 50);
1973 else
1974 device->path_data.tbvpm = 0;
1975 }
1976};
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001979 * Go through all request on the dasd_device request queue,
1980 * terminate them on the cdev if necessary, and return them to the
1981 * submitting layer via callback.
1982 * Note:
1983 * Make sure that all 'submitting layers' still exist when
1984 * this function is called!. In other words, when 'device' is a base
1985 * device then all block layer requests must have been removed before
1986 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001988int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001990 struct dasd_ccw_req *cqr, *n;
1991 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 INIT_LIST_HEAD(&flush_queue);
1995 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001996 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001997 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001998 /* Check status and move request to flush_queue */
1999 switch (cqr->status) {
2000 case DASD_CQR_IN_IO:
2001 rc = device->discipline->term_IO(cqr);
2002 if (rc) {
2003 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002004 dev_err(&device->cdev->dev,
2005 "Flushing the DASD request queue "
2006 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002007 /* stop flush processing */
2008 goto finished;
2009 }
2010 break;
2011 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002012 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002013 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002014 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002015 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002016 break;
2017 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002018 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002019 }
Horst Hummel8f617012006-08-30 14:33:33 +02002020finished:
2021 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002022 /*
2023 * After this point all requests must be in state CLEAR_PENDING,
2024 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2025 * one of the others.
2026 */
2027 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2028 wait_event(dasd_flush_wq,
2029 (cqr->status != DASD_CQR_CLEAR_PENDING));
2030 /*
2031 * Now set each request back to TERMINATED, DONE or NEED_ERP
2032 * and call the callback function of flushed requests
2033 */
2034 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002035 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002037EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039/*
2040 * Acquire the device lock and process queues for the device.
2041 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002042static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 atomic_set (&device->tasklet_scheduled, 0);
2047 INIT_LIST_HEAD(&final_queue);
2048 spin_lock_irq(get_ccwdev_lock(device->cdev));
2049 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002050 __dasd_device_check_expire(device);
2051 /* find final requests on ccw queue */
2052 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002053 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2055 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002056 __dasd_device_process_final_queue(device, &final_queue);
2057 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002059 __dasd_device_start_head(device);
2060 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002061 if (waitqueue_active(&shutdown_waitq))
2062 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 dasd_put_device(device);
2064}
2065
2066/*
2067 * Schedules a call to dasd_tasklet over the device tasklet.
2068 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002069void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002072 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return;
2074 dasd_get_device(device);
2075 tasklet_hi_schedule(&device->tasklet);
2076}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002077EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002079void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2080{
2081 device->stopped |= bits;
2082}
2083EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2084
2085void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2086{
2087 device->stopped &= ~bits;
2088 if (!device->stopped)
2089 wake_up(&generic_waitq);
2090}
2091EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002094 * Queue a request to the head of the device ccw_queue.
2095 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002097void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 struct dasd_device *device;
2100 unsigned long flags;
2101
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002104 cqr->status = DASD_CQR_QUEUED;
2105 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002107 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2109}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002110EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002113 * Queue a request to the tail of the device ccw_queue.
2114 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002116void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 struct dasd_device *device;
2119 unsigned long flags;
2120
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002121 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002123 cqr->status = DASD_CQR_QUEUED;
2124 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002126 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2128}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002129EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002132 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002134void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002136 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2137 cqr->callback_data = DASD_SLEEPON_END_TAG;
2138 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2139 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
Stefan Haberland5915a872011-10-30 15:16:57 +01002141EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002143static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct dasd_device *device;
2146 int rc;
2147
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002148 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002150 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2152 return rc;
2153}
2154
2155/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002156 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2157 */
2158static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2159{
2160 struct dasd_device *device;
2161 dasd_erp_fn_t erp_fn;
2162
2163 if (cqr->status == DASD_CQR_FILLED)
2164 return 0;
2165 device = cqr->startdev;
2166 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2167 if (cqr->status == DASD_CQR_TERMINATED) {
2168 device->discipline->handle_terminated_request(cqr);
2169 return 1;
2170 }
2171 if (cqr->status == DASD_CQR_NEED_ERP) {
2172 erp_fn = device->discipline->erp_action(cqr);
2173 erp_fn(cqr);
2174 return 1;
2175 }
2176 if (cqr->status == DASD_CQR_FAILED)
2177 dasd_log_sense(cqr, &cqr->irb);
2178 if (cqr->refers) {
2179 __dasd_process_erp(device, cqr);
2180 return 1;
2181 }
2182 }
2183 return 0;
2184}
2185
2186static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2187{
2188 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2189 if (cqr->refers) /* erp is not done yet */
2190 return 1;
2191 return ((cqr->status != DASD_CQR_DONE) &&
2192 (cqr->status != DASD_CQR_FAILED));
2193 } else
2194 return (cqr->status == DASD_CQR_FILLED);
2195}
2196
2197static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2198{
2199 struct dasd_device *device;
2200 int rc;
2201 struct list_head ccw_queue;
2202 struct dasd_ccw_req *cqr;
2203
2204 INIT_LIST_HEAD(&ccw_queue);
2205 maincqr->status = DASD_CQR_FILLED;
2206 device = maincqr->startdev;
2207 list_add(&maincqr->blocklist, &ccw_queue);
2208 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2209 cqr = list_first_entry(&ccw_queue,
2210 struct dasd_ccw_req, blocklist)) {
2211
2212 if (__dasd_sleep_on_erp(cqr))
2213 continue;
2214 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2215 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002216 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2217 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2218 cqr->status = DASD_CQR_FAILED;
2219 cqr->intrc = -EPERM;
2220 continue;
2221 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002222 /* Non-temporary stop condition will trigger fail fast */
2223 if (device->stopped & ~DASD_STOPPED_PENDING &&
2224 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2225 (!dasd_eer_enabled(device))) {
2226 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002227 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002228 continue;
2229 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002230 /* Don't try to start requests if device is stopped */
2231 if (interruptible) {
2232 rc = wait_event_interruptible(
2233 generic_waitq, !(device->stopped));
2234 if (rc == -ERESTARTSYS) {
2235 cqr->status = DASD_CQR_FAILED;
2236 maincqr->intrc = rc;
2237 continue;
2238 }
2239 } else
2240 wait_event(generic_waitq, !(device->stopped));
2241
Stefan Haberland5915a872011-10-30 15:16:57 +01002242 if (!cqr->callback)
2243 cqr->callback = dasd_wakeup_cb;
2244
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002245 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002246 dasd_add_request_tail(cqr);
2247 if (interruptible) {
2248 rc = wait_event_interruptible(
2249 generic_waitq, _wait_for_wakeup(cqr));
2250 if (rc == -ERESTARTSYS) {
2251 dasd_cancel_req(cqr);
2252 /* wait (non-interruptible) for final status */
2253 wait_event(generic_waitq,
2254 _wait_for_wakeup(cqr));
2255 cqr->status = DASD_CQR_FAILED;
2256 maincqr->intrc = rc;
2257 continue;
2258 }
2259 } else
2260 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2261 }
2262
Heiko Carstens1aae0562013-01-30 09:49:40 +01002263 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002264 if ((maincqr->status != DASD_CQR_DONE) &&
2265 (maincqr->intrc != -ERESTARTSYS))
2266 dasd_log_sense(maincqr, &maincqr->irb);
2267 if (maincqr->status == DASD_CQR_DONE)
2268 rc = 0;
2269 else if (maincqr->intrc)
2270 rc = maincqr->intrc;
2271 else
2272 rc = -EIO;
2273 return rc;
2274}
2275
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002276static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2277{
2278 struct dasd_ccw_req *cqr;
2279
2280 list_for_each_entry(cqr, ccw_queue, blocklist) {
2281 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2282 return 0;
2283 }
2284
2285 return 1;
2286}
2287
2288static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2289{
2290 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002291 struct dasd_ccw_req *cqr, *n;
Stefan Haberland362ce842014-10-01 13:04:54 +02002292 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002293
2294retry:
2295 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2296 device = cqr->startdev;
2297 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2298 continue;
2299
2300 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2301 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2302 cqr->status = DASD_CQR_FAILED;
2303 cqr->intrc = -EPERM;
2304 continue;
2305 }
2306 /*Non-temporary stop condition will trigger fail fast*/
2307 if (device->stopped & ~DASD_STOPPED_PENDING &&
2308 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2309 !dasd_eer_enabled(device)) {
2310 cqr->status = DASD_CQR_FAILED;
2311 cqr->intrc = -EAGAIN;
2312 continue;
2313 }
2314
2315 /*Don't try to start requests if device is stopped*/
2316 if (interruptible) {
2317 rc = wait_event_interruptible(
2318 generic_waitq, !device->stopped);
2319 if (rc == -ERESTARTSYS) {
2320 cqr->status = DASD_CQR_FAILED;
2321 cqr->intrc = rc;
2322 continue;
2323 }
2324 } else
2325 wait_event(generic_waitq, !(device->stopped));
2326
2327 if (!cqr->callback)
2328 cqr->callback = dasd_wakeup_cb;
2329 cqr->callback_data = DASD_SLEEPON_START_TAG;
2330 dasd_add_request_tail(cqr);
2331 }
2332
2333 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2334
2335 rc = 0;
2336 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002337 /*
2338 * for alias devices simplify error recovery and
2339 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002340 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002341 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002342 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002343 (cqr->status == DASD_CQR_TERMINATED ||
2344 cqr->status == DASD_CQR_NEED_ERP))
2345 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002346
2347 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002348 if (__dasd_sleep_on_erp(cqr))
2349 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002350 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002351 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002352
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002353 return 0;
2354}
2355
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002356/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002357 * Queue a request to the tail of the device ccw_queue and wait for
2358 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002360int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002362 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002364EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002367 * Start requests from a ccw_queue and wait for their completion.
2368 */
2369int dasd_sleep_on_queue(struct list_head *ccw_queue)
2370{
2371 return _dasd_sleep_on_queue(ccw_queue, 0);
2372}
2373EXPORT_SYMBOL(dasd_sleep_on_queue);
2374
2375/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002376 * Queue a request to the tail of the device ccw_queue and wait
2377 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002379int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002381 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002383EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385/*
2386 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2387 * for eckd devices) the currently running request has to be terminated
2388 * and be put back to status queued, before the special request is added
2389 * to the head of the queue. Then the special request is waited on normally.
2390 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002391static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002394 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 if (list_empty(&device->ccw_queue))
2397 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002398 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002399 rc = device->discipline->term_IO(cqr);
2400 if (!rc)
2401 /*
2402 * CQR terminated because a more important request is pending.
2403 * Undo decreasing of retry counter because this is
2404 * not an error case.
2405 */
2406 cqr->retries++;
2407 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002410int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct dasd_device *device;
2413 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002414
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002415 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002416 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2417 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2418 cqr->status = DASD_CQR_FAILED;
2419 cqr->intrc = -EPERM;
2420 return -EIO;
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 spin_lock_irq(get_ccwdev_lock(device->cdev));
2423 rc = _dasd_term_running_cqr(device);
2424 if (rc) {
2425 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2426 return rc;
2427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002429 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002431 /*
2432 * add new request as second
2433 * first the terminated cqr needs to be finished
2434 */
2435 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002438 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2441
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002442 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002443
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002444 if (cqr->status == DASD_CQR_DONE)
2445 rc = 0;
2446 else if (cqr->intrc)
2447 rc = cqr->intrc;
2448 else
2449 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002450
2451 /* kick tasklets */
2452 dasd_schedule_device_bh(device);
2453 if (device->block)
2454 dasd_schedule_block_bh(device->block);
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return rc;
2457}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002458EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460/*
2461 * Cancels a request that was started with dasd_sleep_on_req.
2462 * This is useful to timeout requests. The request will be
2463 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002464 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002465 * negative error code if termination failed
2466 * Cancellation of a request is an asynchronous operation! The calling
2467 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002469int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002471 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 unsigned long flags;
2473 int rc;
2474
2475 rc = 0;
2476 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2477 switch (cqr->status) {
2478 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002479 /* request was not started - just set to cleared */
2480 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002481 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2482 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 break;
2484 case DASD_CQR_IN_IO:
2485 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002486 rc = device->discipline->term_IO(cqr);
2487 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002488 dev_err(&device->cdev->dev,
2489 "Cancelling request %p failed with rc=%d\n",
2490 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002491 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002492 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002495 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002499 dasd_schedule_device_bh(device);
2500 return rc;
2501}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002502EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002503
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002504/*
2505 * SECTION: Operations of the dasd_block layer.
2506 */
2507
2508/*
2509 * Timeout function for dasd_block. This is used when the block layer
2510 * is waiting for something that may not come reliably, (e.g. a state
2511 * change interrupt)
2512 */
2513static void dasd_block_timeout(unsigned long ptr)
2514{
2515 unsigned long flags;
2516 struct dasd_block *block;
2517
2518 block = (struct dasd_block *) ptr;
2519 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2520 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002521 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002522 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2523 dasd_schedule_block_bh(block);
2524}
2525
2526/*
2527 * Setup timeout for a dasd_block in jiffies.
2528 */
2529void dasd_block_set_timer(struct dasd_block *block, int expires)
2530{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002531 if (expires == 0)
2532 del_timer(&block->timer);
2533 else
2534 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002535}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002536EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002537
2538/*
2539 * Clear timeout for a dasd_block.
2540 */
2541void dasd_block_clear_timer(struct dasd_block *block)
2542{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002543 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002544}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002545EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002546
2547/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002548 * Process finished error recovery ccw.
2549 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002550static void __dasd_process_erp(struct dasd_device *device,
2551 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002552{
2553 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002554
2555 if (cqr->status == DASD_CQR_DONE)
2556 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2557 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002558 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002559 erp_fn = device->discipline->erp_postaction(cqr);
2560 erp_fn(cqr);
2561}
2562
2563/*
2564 * Fetch requests from the block device queue.
2565 */
2566static void __dasd_process_request_queue(struct dasd_block *block)
2567{
2568 struct request_queue *queue;
2569 struct request *req;
2570 struct dasd_ccw_req *cqr;
2571 struct dasd_device *basedev;
2572 unsigned long flags;
2573 queue = block->request_queue;
2574 basedev = block->base;
2575 /* No queue ? Then there is nothing to do. */
2576 if (queue == NULL)
2577 return;
2578
2579 /*
2580 * We requeue request from the block device queue to the ccw
2581 * queue only in two states. In state DASD_STATE_READY the
2582 * partition detection is done and we need to requeue requests
2583 * for that. State DASD_STATE_ONLINE is normal block device
2584 * operation.
2585 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002586 if (basedev->state < DASD_STATE_READY) {
2587 while ((req = blk_fetch_request(block->request_queue)))
2588 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002589 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002590 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002591 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002592 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002593 if (basedev->features & DASD_FEATURE_READONLY &&
2594 rq_data_dir(req) == WRITE) {
2595 DBF_DEV_EVENT(DBF_ERR, basedev,
2596 "Rejecting write request %p",
2597 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002598 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002599 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002600 continue;
2601 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002602 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2603 (basedev->features & DASD_FEATURE_FAILFAST ||
2604 blk_noretry_request(req))) {
2605 DBF_DEV_EVENT(DBF_ERR, basedev,
2606 "Rejecting failfast request %p",
2607 req);
2608 blk_start_request(req);
2609 __blk_end_request_all(req, -ETIMEDOUT);
2610 continue;
2611 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002612 cqr = basedev->discipline->build_cp(basedev, block, req);
2613 if (IS_ERR(cqr)) {
2614 if (PTR_ERR(cqr) == -EBUSY)
2615 break; /* normal end condition */
2616 if (PTR_ERR(cqr) == -ENOMEM)
2617 break; /* terminate request queue loop */
2618 if (PTR_ERR(cqr) == -EAGAIN) {
2619 /*
2620 * The current request cannot be build right
2621 * now, we have to try later. If this request
2622 * is the head-of-queue we stop the device
2623 * for 1/2 second.
2624 */
2625 if (!list_empty(&block->ccw_queue))
2626 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002627 spin_lock_irqsave(
2628 get_ccwdev_lock(basedev->cdev), flags);
2629 dasd_device_set_stop_bits(basedev,
2630 DASD_STOPPED_PENDING);
2631 spin_unlock_irqrestore(
2632 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002633 dasd_block_set_timer(block, HZ/2);
2634 break;
2635 }
2636 DBF_DEV_EVENT(DBF_ERR, basedev,
2637 "CCW creation failed (rc=%ld) "
2638 "on request %p",
2639 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002640 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002641 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002642 continue;
2643 }
2644 /*
2645 * Note: callback is set to dasd_return_cqr_cb in
2646 * __dasd_block_start_head to cover erp requests as well
2647 */
2648 cqr->callback_data = (void *) req;
2649 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002650 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002651 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002652 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002653 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002654 dasd_profile_start(block, cqr, req);
2655 }
2656}
2657
2658static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2659{
2660 struct request *req;
2661 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002662 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002663
2664 req = (struct request *) cqr->callback_data;
2665 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002666 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002667 if (status < 0)
2668 error = status;
2669 else if (status == 0) {
2670 if (cqr->intrc == -EPERM)
2671 error = -EBADE;
2672 else if (cqr->intrc == -ENOLINK ||
2673 cqr->intrc == -ETIMEDOUT)
2674 error = cqr->intrc;
2675 else
2676 error = -EIO;
2677 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002678 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002679}
2680
2681/*
2682 * Process ccw request queue.
2683 */
2684static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2685 struct list_head *final_queue)
2686{
2687 struct list_head *l, *n;
2688 struct dasd_ccw_req *cqr;
2689 dasd_erp_fn_t erp_fn;
2690 unsigned long flags;
2691 struct dasd_device *base = block->base;
2692
2693restart:
2694 /* Process request with final status. */
2695 list_for_each_safe(l, n, &block->ccw_queue) {
2696 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2697 if (cqr->status != DASD_CQR_DONE &&
2698 cqr->status != DASD_CQR_FAILED &&
2699 cqr->status != DASD_CQR_NEED_ERP &&
2700 cqr->status != DASD_CQR_TERMINATED)
2701 continue;
2702
2703 if (cqr->status == DASD_CQR_TERMINATED) {
2704 base->discipline->handle_terminated_request(cqr);
2705 goto restart;
2706 }
2707
2708 /* Process requests that may be recovered */
2709 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002710 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002711 if (IS_ERR(erp_fn(cqr)))
2712 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002713 goto restart;
2714 }
2715
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002716 /* log sense for fatal error */
2717 if (cqr->status == DASD_CQR_FAILED) {
2718 dasd_log_sense(cqr, &cqr->irb);
2719 }
2720
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002721 /* First of all call extended error reporting. */
2722 if (dasd_eer_enabled(base) &&
2723 cqr->status == DASD_CQR_FAILED) {
2724 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2725
2726 /* restart request */
2727 cqr->status = DASD_CQR_FILLED;
2728 cqr->retries = 255;
2729 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002730 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002731 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2732 flags);
2733 goto restart;
2734 }
2735
2736 /* Process finished ERP request. */
2737 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002738 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002739 goto restart;
2740 }
2741
2742 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002743 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002744 list_move_tail(&cqr->blocklist, final_queue);
2745 }
2746}
2747
2748static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2749{
2750 dasd_schedule_block_bh(cqr->block);
2751}
2752
2753static void __dasd_block_start_head(struct dasd_block *block)
2754{
2755 struct dasd_ccw_req *cqr;
2756
2757 if (list_empty(&block->ccw_queue))
2758 return;
2759 /* We allways begin with the first requests on the queue, as some
2760 * of previously started requests have to be enqueued on a
2761 * dasd_device again for error recovery.
2762 */
2763 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2764 if (cqr->status != DASD_CQR_FILLED)
2765 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002766 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2767 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2768 cqr->status = DASD_CQR_FAILED;
2769 cqr->intrc = -EPERM;
2770 dasd_schedule_block_bh(block);
2771 continue;
2772 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002773 /* Non-temporary stop condition will trigger fail fast */
2774 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2775 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2776 (!dasd_eer_enabled(block->base))) {
2777 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002778 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002779 dasd_schedule_block_bh(block);
2780 continue;
2781 }
2782 /* Don't try to start requests if device is stopped */
2783 if (block->base->stopped)
2784 return;
2785
2786 /* just a fail safe check, should not happen */
2787 if (!cqr->startdev)
2788 cqr->startdev = block->base;
2789
2790 /* make sure that the requests we submit find their way back */
2791 cqr->callback = dasd_return_cqr_cb;
2792
2793 dasd_add_request_tail(cqr);
2794 }
2795}
2796
2797/*
2798 * Central dasd_block layer routine. Takes requests from the generic
2799 * block layer request queue, creates ccw requests, enqueues them on
2800 * a dasd_device and processes ccw requests that have been returned.
2801 */
2802static void dasd_block_tasklet(struct dasd_block *block)
2803{
2804 struct list_head final_queue;
2805 struct list_head *l, *n;
2806 struct dasd_ccw_req *cqr;
2807
2808 atomic_set(&block->tasklet_scheduled, 0);
2809 INIT_LIST_HEAD(&final_queue);
2810 spin_lock(&block->queue_lock);
2811 /* Finish off requests on ccw queue */
2812 __dasd_process_block_ccw_queue(block, &final_queue);
2813 spin_unlock(&block->queue_lock);
2814 /* Now call the callback function of requests with final status */
2815 spin_lock_irq(&block->request_queue_lock);
2816 list_for_each_safe(l, n, &final_queue) {
2817 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2818 list_del_init(&cqr->blocklist);
2819 __dasd_cleanup_cqr(cqr);
2820 }
2821 spin_lock(&block->queue_lock);
2822 /* Get new request from the block device request queue */
2823 __dasd_process_request_queue(block);
2824 /* Now check if the head of the ccw queue needs to be started. */
2825 __dasd_block_start_head(block);
2826 spin_unlock(&block->queue_lock);
2827 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002828 if (waitqueue_active(&shutdown_waitq))
2829 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002830 dasd_put_device(block->base);
2831}
2832
2833static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2834{
2835 wake_up(&dasd_flush_wq);
2836}
2837
2838/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002839 * Requeue a request back to the block request queue
2840 * only works for block requests
2841 */
2842static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2843{
2844 struct dasd_block *block = cqr->block;
2845 struct request *req;
2846 unsigned long flags;
2847
2848 if (!block)
2849 return -EINVAL;
2850 spin_lock_irqsave(&block->queue_lock, flags);
2851 req = (struct request *) cqr->callback_data;
2852 blk_requeue_request(block->request_queue, req);
2853 spin_unlock_irqrestore(&block->queue_lock, flags);
2854
2855 return 0;
2856}
2857
2858/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002859 * Go through all request on the dasd_block request queue, cancel them
2860 * on the respective dasd_device, and return them to the generic
2861 * block layer.
2862 */
2863static int dasd_flush_block_queue(struct dasd_block *block)
2864{
2865 struct dasd_ccw_req *cqr, *n;
2866 int rc, i;
2867 struct list_head flush_queue;
2868
2869 INIT_LIST_HEAD(&flush_queue);
2870 spin_lock_bh(&block->queue_lock);
2871 rc = 0;
2872restart:
2873 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2874 /* if this request currently owned by a dasd_device cancel it */
2875 if (cqr->status >= DASD_CQR_QUEUED)
2876 rc = dasd_cancel_req(cqr);
2877 if (rc < 0)
2878 break;
2879 /* Rechain request (including erp chain) so it won't be
2880 * touched by the dasd_block_tasklet anymore.
2881 * Replace the callback so we notice when the request
2882 * is returned from the dasd_device layer.
2883 */
2884 cqr->callback = _dasd_wake_block_flush_cb;
2885 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2886 list_move_tail(&cqr->blocklist, &flush_queue);
2887 if (i > 1)
2888 /* moved more than one request - need to restart */
2889 goto restart;
2890 }
2891 spin_unlock_bh(&block->queue_lock);
2892 /* Now call the callback function of flushed requests */
2893restart_cb:
2894 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2895 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2896 /* Process finished ERP request. */
2897 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002898 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002899 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002900 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002901 /* restart list_for_xx loop since dasd_process_erp
2902 * might remove multiple elements */
2903 goto restart_cb;
2904 }
2905 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002906 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002907 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002908 list_del_init(&cqr->blocklist);
2909 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002910 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return rc;
2913}
2914
2915/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002916 * Schedules a call to dasd_tasklet over the device tasklet.
2917 */
2918void dasd_schedule_block_bh(struct dasd_block *block)
2919{
2920 /* Protect against rescheduling. */
2921 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2922 return;
2923 /* life cycle of block is bound to it's base device */
2924 dasd_get_device(block->base);
2925 tasklet_hi_schedule(&block->tasklet);
2926}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002927EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002928
2929
2930/*
2931 * SECTION: external block device operations
2932 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 */
2934
2935/*
2936 * Dasd request queue function. Called from ll_rw_blk.c
2937 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002938static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002940 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002942 block = queue->queuedata;
2943 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002945 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002947 __dasd_block_start_head(block);
2948 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
2951/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002952 * Block timeout callback, called from the block layer
2953 *
2954 * request_queue lock is held on entry.
2955 *
2956 * Return values:
2957 * BLK_EH_RESET_TIMER if the request should be left running
2958 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2959 * by the driver.
2960 */
2961enum blk_eh_timer_return dasd_times_out(struct request *req)
2962{
2963 struct dasd_ccw_req *cqr = req->completion_data;
2964 struct dasd_block *block = req->q->queuedata;
2965 struct dasd_device *device;
2966 int rc = 0;
2967
2968 if (!cqr)
2969 return BLK_EH_NOT_HANDLED;
2970
2971 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002972 if (!device->blk_timeout)
2973 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002974 DBF_DEV_EVENT(DBF_WARNING, device,
2975 " dasd_times_out cqr %p status %x",
2976 cqr, cqr->status);
2977
2978 spin_lock(&block->queue_lock);
2979 spin_lock(get_ccwdev_lock(device->cdev));
2980 cqr->retries = -1;
2981 cqr->intrc = -ETIMEDOUT;
2982 if (cqr->status >= DASD_CQR_QUEUED) {
2983 spin_unlock(get_ccwdev_lock(device->cdev));
2984 rc = dasd_cancel_req(cqr);
2985 } else if (cqr->status == DASD_CQR_FILLED ||
2986 cqr->status == DASD_CQR_NEED_ERP) {
2987 cqr->status = DASD_CQR_TERMINATED;
2988 spin_unlock(get_ccwdev_lock(device->cdev));
2989 } else if (cqr->status == DASD_CQR_IN_ERP) {
2990 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2991
2992 list_for_each_entry_safe(searchcqr, nextcqr,
2993 &block->ccw_queue, blocklist) {
2994 tmpcqr = searchcqr;
2995 while (tmpcqr->refers)
2996 tmpcqr = tmpcqr->refers;
2997 if (tmpcqr != cqr)
2998 continue;
2999 /* searchcqr is an ERP request for cqr */
3000 searchcqr->retries = -1;
3001 searchcqr->intrc = -ETIMEDOUT;
3002 if (searchcqr->status >= DASD_CQR_QUEUED) {
3003 spin_unlock(get_ccwdev_lock(device->cdev));
3004 rc = dasd_cancel_req(searchcqr);
3005 spin_lock(get_ccwdev_lock(device->cdev));
3006 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3007 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3008 searchcqr->status = DASD_CQR_TERMINATED;
3009 rc = 0;
3010 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3011 /*
3012 * Shouldn't happen; most recent ERP
3013 * request is at the front of queue
3014 */
3015 continue;
3016 }
3017 break;
3018 }
3019 spin_unlock(get_ccwdev_lock(device->cdev));
3020 }
3021 dasd_schedule_block_bh(block);
3022 spin_unlock(&block->queue_lock);
3023
3024 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3025}
3026
3027/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 * Allocate and initialize request queue and default I/O scheduler.
3029 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003030static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 int rc;
3033
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003034 block->request_queue = blk_init_queue(do_dasd_request,
3035 &block->request_queue_lock);
3036 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return -ENOMEM;
3038
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003039 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003041 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02003042 block->request_queue->elevator = NULL;
Heiko Carstensef089942013-10-31 13:24:28 +01003043 mutex_lock(&block->request_queue->sysfs_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003044 rc = elevator_init(block->request_queue, "deadline");
Heiko Carstensef089942013-10-31 13:24:28 +01003045 if (rc)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003046 blk_cleanup_queue(block->request_queue);
Heiko Carstensef089942013-10-31 13:24:28 +01003047 mutex_unlock(&block->request_queue->sysfs_lock);
3048 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
3051/*
3052 * Allocate and initialize request queue.
3053 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003054static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 int max;
3057
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003058 if (block->base->features & DASD_FEATURE_USERAW) {
3059 /*
3060 * the max_blocks value for raw_track access is 256
3061 * it is higher than the native ECKD value because we
3062 * only need one ccw per track
3063 * so the max_hw_sectors are
3064 * 2048 x 512B = 1024kB = 16 tracks
3065 */
3066 max = 2048;
3067 } else {
3068 max = block->base->discipline->max_blocks << block->s2b_shift;
3069 }
3070 blk_queue_logical_block_size(block->request_queue,
3071 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003072 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003073 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003074 /* with page sized segments we can translate each segement into
3075 * one idaw/tidaw
3076 */
3077 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3078 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081/*
3082 * Deactivate and free request queue.
3083 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003084static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003086 if (block->request_queue) {
3087 blk_cleanup_queue(block->request_queue);
3088 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090}
3091
3092/*
3093 * Flush request on the request queue.
3094 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003095static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 struct request *req;
3098
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003099 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003101
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003102 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003103 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003104 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003105 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Al Viro57a7c0b2008-03-02 10:36:08 -05003108static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003110 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 int rc;
3112
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003113 base = dasd_device_from_gendisk(bdev->bd_disk);
3114 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003115 return -ENODEV;
3116
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003117 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003118 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 rc = -ENODEV;
3120 goto unlock;
3121 }
3122
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003123 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 rc = -EINVAL;
3125 goto unlock;
3126 }
3127
3128 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003129 dev_info(&base->cdev->dev,
3130 "Accessing the DASD failed because it is in "
3131 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 rc = -EPERM;
3133 goto out;
3134 }
3135
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003136 if (base->state <= DASD_STATE_BASIC) {
3137 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 " Cannot open unrecognized device");
3139 rc = -ENODEV;
3140 goto out;
3141 }
3142
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003143 if ((mode & FMODE_WRITE) &&
3144 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3145 (base->features & DASD_FEATURE_READONLY))) {
3146 rc = -EROFS;
3147 goto out;
3148 }
3149
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003150 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return 0;
3152
3153out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003154 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003156 atomic_dec(&base->block->open_count);
3157 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 return rc;
3159}
3160
Al Virodb2a1442013-05-05 21:52:57 -04003161static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Al Virodb2a1442013-05-05 21:52:57 -04003163 struct dasd_device *base = dasd_device_from_gendisk(disk);
3164 if (base) {
3165 atomic_dec(&base->block->open_count);
3166 module_put(base->discipline->owner);
3167 dasd_put_device(base);
3168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003171/*
3172 * Return disk geometry.
3173 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003174static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003175{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003176 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003177
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003178 base = dasd_device_from_gendisk(bdev->bd_disk);
3179 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003180 return -ENODEV;
3181
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003182 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003183 !base->discipline->fill_geometry) {
3184 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003185 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003186 }
3187 base->discipline->fill_geometry(base->block, geo);
3188 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3189 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003190 return 0;
3191}
3192
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003193const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194dasd_device_operations = {
3195 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003196 .open = dasd_open,
3197 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003198 .ioctl = dasd_ioctl,
3199 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003200 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201};
3202
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003203/*******************************************************************************
3204 * end of block device operations
3205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207static void
3208dasd_exit(void)
3209{
3210#ifdef CONFIG_PROC_FS
3211 dasd_proc_exit();
3212#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003213 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003214 if (dasd_page_cache != NULL) {
3215 kmem_cache_destroy(dasd_page_cache);
3216 dasd_page_cache = NULL;
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 dasd_gendisk_exit();
3219 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (dasd_debug_area != NULL) {
3221 debug_unregister(dasd_debug_area);
3222 dasd_debug_area = NULL;
3223 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003224 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
3227/*
3228 * SECTION: common functions for ccw_driver use
3229 */
3230
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003231/*
3232 * Is the device read-only?
3233 * Note that this function does not report the setting of the
3234 * readonly device attribute, but how it is configured in z/VM.
3235 */
3236int dasd_device_is_ro(struct dasd_device *device)
3237{
3238 struct ccw_dev_id dev_id;
3239 struct diag210 diag_data;
3240 int rc;
3241
3242 if (!MACHINE_IS_VM)
3243 return 0;
3244 ccw_device_get_id(device->cdev, &dev_id);
3245 memset(&diag_data, 0, sizeof(diag_data));
3246 diag_data.vrdcdvno = dev_id.devno;
3247 diag_data.vrdclen = sizeof(diag_data);
3248 rc = diag210(&diag_data);
3249 if (rc == 0 || rc == 2) {
3250 return diag_data.vrdcvfla & 0x80;
3251 } else {
3252 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3253 dev_id.devno, rc);
3254 return 0;
3255 }
3256}
3257EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3258
Cornelia Huckf3445a12009-04-14 15:36:23 +02003259static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3260{
3261 struct ccw_device *cdev = data;
3262 int ret;
3263
3264 ret = ccw_device_set_online(cdev);
3265 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003266 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3267 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003268}
3269
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003270/*
3271 * Initial attempt at a probe function. this can be simplified once
3272 * the other detection code is gone.
3273 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003274int dasd_generic_probe(struct ccw_device *cdev,
3275 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 int ret;
3278
3279 ret = dasd_add_sysfs_files(cdev);
3280 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003281 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3282 "dasd_generic_probe: could not add "
3283 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
Horst Hummel40545572006-06-29 15:08:18 +02003286 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Horst Hummel40545572006-06-29 15:08:18 +02003288 /*
3289 * Automatically online either all dasd devices (dasd_autodetect)
3290 * or all devices specified with dasd= parameters during
3291 * initial probe.
3292 */
3293 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003294 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003295 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003298EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003300/*
3301 * This will one day be called from a global not_oper handler.
3302 * It is also used by driver_unregister during module unload.
3303 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003304void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003307 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Horst Hummel59afda72005-05-16 21:53:39 -07003309 cdev->handler = NULL;
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003312 if (IS_ERR(device)) {
3313 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003315 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003316 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3317 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /* Already doing offline processing */
3319 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003320 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return;
3322 }
3323 /*
3324 * This device is removed unconditionally. Set offline
3325 * flag to prevent dasd_open from opening it while it is
3326 * no quite down yet.
3327 */
3328 dasd_set_target_state(device, DASD_STATE_NEW);
3329 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003330 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003332 /*
3333 * life cycle of block is bound to device, so delete it after
3334 * device was safely removed
3335 */
3336 if (block)
3337 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003338
3339 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003341EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003343/*
3344 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003346 * or the user has started activation through sysfs.
3347 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003348int dasd_generic_set_online(struct ccw_device *cdev,
3349 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003351 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003353 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003354
Horst Hummel40545572006-06-29 15:08:18 +02003355 /* first online clears initial online feature flag */
3356 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 device = dasd_create_device(cdev);
3358 if (IS_ERR(device))
3359 return PTR_ERR(device);
3360
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003361 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003362 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003364 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3365 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 dasd_delete_device(device);
3367 return -ENODEV;
3368 }
3369 discipline = dasd_diag_discipline_pointer;
3370 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003371 if (!try_module_get(base_discipline->owner)) {
3372 dasd_delete_device(device);
3373 return -EINVAL;
3374 }
3375 if (!try_module_get(discipline->owner)) {
3376 module_put(base_discipline->owner);
3377 dasd_delete_device(device);
3378 return -EINVAL;
3379 }
3380 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 device->discipline = discipline;
3382
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003383 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 rc = discipline->check_device(device);
3385 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003386 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3387 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003388 module_put(discipline->owner);
3389 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 dasd_delete_device(device);
3391 return rc;
3392 }
3393
3394 dasd_set_target_state(device, DASD_STATE_ONLINE);
3395 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003396 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3397 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 rc = -ENODEV;
3399 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003400 if (device->block)
3401 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 dasd_delete_device(device);
3403 } else
3404 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003405 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003406
3407 wait_event(dasd_init_waitq, _wait_for_device(device));
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return rc;
3411}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003412EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003414int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
3416 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003417 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003418 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003420 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 device = dasd_device_from_cdev(cdev);
3422 if (IS_ERR(device))
3423 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /*
3426 * We must make sure that this device is currently not in use.
3427 * The open_count is increased for every opener, that includes
3428 * the blkdev_get in dasd_scan_partitions. We are only interested
3429 * in the other openers.
3430 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003431 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003432 max_count = device->block->bdev ? 0 : -1;
3433 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003434 if (open_count > max_count) {
3435 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003436 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3437 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003438 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003439 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3440 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003441 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3442 dasd_put_device(device);
3443 return -EBUSY;
3444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003446
3447 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3448 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003449 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003450 * could only be called by normal offline so safe_offline flag
3451 * needs to be removed to run normal offline and kill all I/O
3452 */
3453 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3454 /* Already doing normal offline processing */
3455 dasd_put_device(device);
3456 return -EBUSY;
3457 } else
3458 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3459
3460 } else
3461 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3462 /* Already doing offline processing */
3463 dasd_put_device(device);
3464 return -EBUSY;
3465 }
3466
3467 /*
3468 * if safe_offline called set safe_offline_running flag and
3469 * clear safe_offline so that a call to normal offline
3470 * can overrun safe_offline processing
3471 */
3472 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3473 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3474 /*
3475 * If we want to set the device safe offline all IO operations
3476 * should be finished before continuing the offline process
3477 * so sync bdev first and then wait for our queues to become
3478 * empty
3479 */
3480 /* sync blockdev and partitions */
3481 rc = fsync_bdev(device->block->bdev);
3482 if (rc != 0)
3483 goto interrupted;
3484
3485 /* schedule device tasklet and wait for completion */
3486 dasd_schedule_device_bh(device);
3487 rc = wait_event_interruptible(shutdown_waitq,
3488 _wait_for_empty_queues(device));
3489 if (rc != 0)
3490 goto interrupted;
3491 }
3492
3493 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 dasd_set_target_state(device, DASD_STATE_NEW);
3495 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003496 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003498 /*
3499 * life cycle of block is bound to device, so delete it after
3500 * device was safely removed
3501 */
3502 if (block)
3503 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003505
3506interrupted:
3507 /* interrupted by signal */
3508 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3509 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3510 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3511 dasd_put_device(device);
3512 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003514EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003516int dasd_generic_last_path_gone(struct dasd_device *device)
3517{
3518 struct dasd_ccw_req *cqr;
3519
3520 dev_warn(&device->cdev->dev, "No operational channel path is left "
3521 "for the device\n");
3522 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3523 /* First of all call extended error reporting. */
3524 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3525
3526 if (device->state < DASD_STATE_BASIC)
3527 return 0;
3528 /* Device is active. We want to keep it. */
3529 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3530 if ((cqr->status == DASD_CQR_IN_IO) ||
3531 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3532 cqr->status = DASD_CQR_QUEUED;
3533 cqr->retries++;
3534 }
3535 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3536 dasd_device_clear_timer(device);
3537 dasd_schedule_device_bh(device);
3538 return 1;
3539}
3540EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3541
3542int dasd_generic_path_operational(struct dasd_device *device)
3543{
3544 dev_info(&device->cdev->dev, "A channel path to the device has become "
3545 "operational\n");
3546 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3547 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3548 if (device->stopped & DASD_UNRESUMED_PM) {
3549 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3550 dasd_restore_device(device);
3551 return 1;
3552 }
3553 dasd_schedule_device_bh(device);
3554 if (device->block)
3555 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003556
3557 if (!device->stopped)
3558 wake_up(&generic_waitq);
3559
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003560 return 1;
3561}
3562EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3563
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003564int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
3566 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 int ret;
3568
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003569 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (IS_ERR(device))
3571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 ret = 0;
3573 switch (event) {
3574 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003575 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003577 device->path_data.opm = 0;
3578 device->path_data.ppm = 0;
3579 device->path_data.npm = 0;
3580 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 break;
3582 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003584 if (device->path_data.opm)
3585 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 break;
3587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 dasd_put_device(device);
3589 return ret;
3590}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003591EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003593void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3594{
3595 int chp;
3596 __u8 oldopm, eventlpm;
3597 struct dasd_device *device;
3598
3599 device = dasd_device_from_cdev_locked(cdev);
3600 if (IS_ERR(device))
3601 return;
3602 for (chp = 0; chp < 8; chp++) {
3603 eventlpm = 0x80 >> chp;
3604 if (path_event[chp] & PE_PATH_GONE) {
3605 oldopm = device->path_data.opm;
3606 device->path_data.opm &= ~eventlpm;
3607 device->path_data.ppm &= ~eventlpm;
3608 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003609 if (oldopm && !device->path_data.opm) {
3610 dev_warn(&device->cdev->dev,
3611 "No verified channel paths remain "
3612 "for the device\n");
3613 DBF_DEV_EVENT(DBF_WARNING, device,
3614 "%s", "last verified path gone");
3615 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3616 dasd_device_set_stop_bits(device,
3617 DASD_STOPPED_DC_WAIT);
3618 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003619 }
3620 if (path_event[chp] & PE_PATH_AVAILABLE) {
3621 device->path_data.opm &= ~eventlpm;
3622 device->path_data.ppm &= ~eventlpm;
3623 device->path_data.npm &= ~eventlpm;
3624 device->path_data.tbvpm |= eventlpm;
3625 dasd_schedule_device_bh(device);
3626 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003627 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003628 if (!(device->path_data.opm & eventlpm) &&
3629 !(device->path_data.tbvpm & eventlpm)) {
3630 /*
3631 * we can not establish a pathgroup on an
3632 * unavailable path, so trigger a path
3633 * verification first
3634 */
3635 device->path_data.tbvpm |= eventlpm;
3636 dasd_schedule_device_bh(device);
3637 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003638 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3639 "Pathgroup re-established\n");
3640 if (device->discipline->kick_validate)
3641 device->discipline->kick_validate(device);
3642 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003643 }
3644 dasd_put_device(device);
3645}
3646EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3647
3648int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3649{
3650 if (!device->path_data.opm && lpm) {
3651 device->path_data.opm = lpm;
3652 dasd_generic_path_operational(device);
3653 } else
3654 device->path_data.opm |= lpm;
3655 return 0;
3656}
3657EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3658
3659
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003660int dasd_generic_pm_freeze(struct ccw_device *cdev)
3661{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003662 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003663 struct list_head freeze_queue;
3664 struct dasd_ccw_req *cqr, *n;
3665 struct dasd_ccw_req *refers;
3666 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003667
3668 if (IS_ERR(device))
3669 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003670
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003671 /* mark device as suspended */
3672 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3673
Stefan Haberland6f272b92011-01-05 12:48:05 +01003674 if (device->discipline->freeze)
3675 rc = device->discipline->freeze(device);
3676
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003677 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003678 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003679
3680 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003681 INIT_LIST_HEAD(&freeze_queue);
3682 spin_lock_irq(get_ccwdev_lock(cdev));
3683 rc = 0;
3684 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3685 /* Check status and move request to flush_queue */
3686 if (cqr->status == DASD_CQR_IN_IO) {
3687 rc = device->discipline->term_IO(cqr);
3688 if (rc) {
3689 /* unable to terminate requeust */
3690 dev_err(&device->cdev->dev,
3691 "Unable to terminate request %p "
3692 "on suspend\n", cqr);
3693 spin_unlock_irq(get_ccwdev_lock(cdev));
3694 dasd_put_device(device);
3695 return rc;
3696 }
3697 }
3698 list_move_tail(&cqr->devlist, &freeze_queue);
3699 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003700 spin_unlock_irq(get_ccwdev_lock(cdev));
3701
3702 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3703 wait_event(dasd_flush_wq,
3704 (cqr->status != DASD_CQR_CLEAR_PENDING));
3705 if (cqr->status == DASD_CQR_CLEARED)
3706 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003707
Stefan Haberlandc5576872013-04-15 16:41:31 +02003708 /* requeue requests to blocklayer will only work for
3709 block device requests */
3710 if (_dasd_requeue_request(cqr))
3711 continue;
3712
3713 /* remove requests from device and block queue */
3714 list_del_init(&cqr->devlist);
3715 while (cqr->refers != NULL) {
3716 refers = cqr->refers;
3717 /* remove the request from the block queue */
3718 list_del(&cqr->blocklist);
3719 /* free the finished erp request */
3720 dasd_free_erp_request(cqr, cqr->memdev);
3721 cqr = refers;
3722 }
3723 if (cqr->block)
3724 list_del_init(&cqr->blocklist);
3725 cqr->block->base->discipline->free_cp(
3726 cqr, (struct request *) cqr->callback_data);
3727 }
3728
3729 /*
3730 * if requests remain then they are internal request
3731 * and go back to the device queue
3732 */
3733 if (!list_empty(&freeze_queue)) {
3734 /* move freeze_queue to start of the ccw_queue */
3735 spin_lock_irq(get_ccwdev_lock(cdev));
3736 list_splice_tail(&freeze_queue, &device->ccw_queue);
3737 spin_unlock_irq(get_ccwdev_lock(cdev));
3738 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003739 dasd_put_device(device);
3740 return rc;
3741}
3742EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3743
3744int dasd_generic_restore_device(struct ccw_device *cdev)
3745{
3746 struct dasd_device *device = dasd_device_from_cdev(cdev);
3747 int rc = 0;
3748
3749 if (IS_ERR(device))
3750 return PTR_ERR(device);
3751
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003752 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003753 dasd_device_remove_stop_bits(device,
3754 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003755
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003756 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003757
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003758 /*
3759 * call discipline restore function
3760 * if device is stopped do nothing e.g. for disconnected devices
3761 */
3762 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003763 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003764 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003765 /*
3766 * if the resume failed for the DASD we put it in
3767 * an UNRESUMED stop state
3768 */
3769 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003770
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003771 if (device->block)
3772 dasd_schedule_block_bh(device->block);
3773
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003774 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003775 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003776 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003777}
3778EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3779
Heiko Carstens763968e2007-05-10 15:45:46 +02003780static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3781 void *rdc_buffer,
3782 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003783 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003784{
3785 struct dasd_ccw_req *cqr;
3786 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003787 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003788
3789 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3790
3791 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003792 /* internal error 13 - Allocating the RDC request failed*/
3793 dev_err(&device->cdev->dev,
3794 "An error occurred in the DASD device driver, "
3795 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003796 return cqr;
3797 }
3798
3799 ccw = cqr->cpaddr;
3800 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003801 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3802 idaw = (unsigned long *) (cqr->data);
3803 ccw->cda = (__u32)(addr_t) idaw;
3804 ccw->flags = CCW_FLAG_IDA;
3805 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3806 } else {
3807 ccw->cda = (__u32)(addr_t) rdc_buffer;
3808 ccw->flags = 0;
3809 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003810
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003811 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003812 cqr->startdev = device;
3813 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003814 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003815 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003816 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003817 cqr->status = DASD_CQR_FILLED;
3818 return cqr;
3819}
3820
3821
Stefan Haberland68b781f2009-09-11 10:28:29 +02003822int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003823 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003824{
3825 int ret;
3826 struct dasd_ccw_req *cqr;
3827
Sebastian Ott92636b12009-06-12 10:26:37 +02003828 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003829 magic);
3830 if (IS_ERR(cqr))
3831 return PTR_ERR(cqr);
3832
3833 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003834 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003835 return ret;
3836}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003837EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003838
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003839/*
3840 * In command mode and transport mode we need to look for sense
3841 * data in different places. The sense data itself is allways
3842 * an array of 32 bytes, so we can unify the sense data access
3843 * for both modes.
3844 */
3845char *dasd_get_sense(struct irb *irb)
3846{
3847 struct tsb *tsb = NULL;
3848 char *sense = NULL;
3849
3850 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3851 if (irb->scsw.tm.tcw)
3852 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3853 irb->scsw.tm.tcw);
3854 if (tsb && tsb->length == 64 && tsb->flags)
3855 switch (tsb->flags & 0x07) {
3856 case 1: /* tsa_iostat */
3857 sense = tsb->tsa.iostat.sense;
3858 break;
3859 case 2: /* tsa_ddpc */
3860 sense = tsb->tsa.ddpc.sense;
3861 break;
3862 default:
3863 /* currently we don't use interrogate data */
3864 break;
3865 }
3866 } else if (irb->esw.esw0.erw.cons) {
3867 sense = irb->ecw;
3868 }
3869 return sense;
3870}
3871EXPORT_SYMBOL_GPL(dasd_get_sense);
3872
Stefan Haberland4679e892012-06-19 17:30:12 +02003873void dasd_generic_shutdown(struct ccw_device *cdev)
3874{
3875 struct dasd_device *device;
3876
3877 device = dasd_device_from_cdev(cdev);
3878 if (IS_ERR(device))
3879 return;
3880
3881 if (device->block)
3882 dasd_schedule_block_bh(device->block);
3883
3884 dasd_schedule_device_bh(device);
3885
3886 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3887}
3888EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3889
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003890static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
3892 int rc;
3893
3894 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003895 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003896 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003897 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
3899 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003900 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (dasd_debug_area == NULL) {
3902 rc = -ENOMEM;
3903 goto failed;
3904 }
3905 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003906 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3909
3910 dasd_diag_discipline_pointer = NULL;
3911
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003912 dasd_statistics_createroot();
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 rc = dasd_devmap_init();
3915 if (rc)
3916 goto failed;
3917 rc = dasd_gendisk_init();
3918 if (rc)
3919 goto failed;
3920 rc = dasd_parse();
3921 if (rc)
3922 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003923 rc = dasd_eer_init();
3924 if (rc)
3925 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926#ifdef CONFIG_PROC_FS
3927 rc = dasd_proc_init();
3928 if (rc)
3929 goto failed;
3930#endif
3931
3932 return 0;
3933failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003934 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 dasd_exit();
3936 return rc;
3937}
3938
3939module_init(dasd_init);
3940module_exit(dasd_exit);