blob: f1b7fdc58a5f366ffe054576e8755851f60b2beb [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
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +020041#define DASD_SLEEPON_START_TAG (void *) 1
42#define DASD_SLEEPON_END_TAG (void *) 2
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * SECTION: exported variables of dasd.c
46 */
47debug_info_t *dasd_debug_area;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020048static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010050void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
53MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020054 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_LICENSE("GPL");
57
58/*
59 * SECTION: prototypes for static functions of dasd.c
60 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010061static int dasd_alloc_queue(struct dasd_block *);
62static void dasd_setup_queue(struct dasd_block *);
63static void dasd_free_queue(struct dasd_block *);
64static void dasd_flush_request_queue(struct dasd_block *);
65static int dasd_flush_block_queue(struct dasd_block *);
66static void dasd_device_tasklet(struct dasd_device *);
67static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000068static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020069static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020070static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010071static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010072static void dasd_device_timeout(unsigned long);
73static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010074static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020075static void dasd_profile_init(struct dasd_profile *, struct dentry *);
76static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * SECTION: Operations on the device structure.
80 */
81static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020082static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020083static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020084static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Allocate memory for a new device structure.
88 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010089struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct dasd_device *device;
92
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010093 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
94 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /* Get two pages for normal block device operations. */
98 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010099 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree(device);
101 return ERR_PTR(-ENOMEM);
102 }
103 /* Get one page for error recovery. */
104 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100105 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 free_pages((unsigned long) device->ccw_mem, 1);
107 kfree(device);
108 return ERR_PTR(-ENOMEM);
109 }
110
111 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
112 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
113 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100114 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200115 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100116 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 (unsigned long) device);
118 INIT_LIST_HEAD(&device->ccw_queue);
119 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100120 device->timer.function = dasd_device_timeout;
121 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000122 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200123 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200124 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 device->state = DASD_STATE_NEW;
126 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100127 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200128 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return device;
130}
131
132/*
133 * Free memory of a device structure.
134 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100135void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800137 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 free_page((unsigned long) device->erp_mem);
139 free_pages((unsigned long) device->ccw_mem, 1);
140 kfree(device);
141}
142
143/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100144 * Allocate memory for a new device structure.
145 */
146struct dasd_block *dasd_alloc_block(void)
147{
148 struct dasd_block *block;
149
150 block = kzalloc(sizeof(*block), GFP_ATOMIC);
151 if (!block)
152 return ERR_PTR(-ENOMEM);
153 /* open_count = 0 means device online but not in use */
154 atomic_set(&block->open_count, -1);
155
156 spin_lock_init(&block->request_queue_lock);
157 atomic_set(&block->tasklet_scheduled, 0);
158 tasklet_init(&block->tasklet,
159 (void (*)(unsigned long)) dasd_block_tasklet,
160 (unsigned long) block);
161 INIT_LIST_HEAD(&block->ccw_queue);
162 spin_lock_init(&block->queue_lock);
163 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100164 block->timer.function = dasd_block_timeout;
165 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200166 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100167
168 return block;
169}
170
171/*
172 * Free memory of a device structure.
173 */
174void dasd_free_block(struct dasd_block *block)
175{
176 kfree(block);
177}
178
179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Make a new device known to the system.
181 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100182static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int rc;
185
186 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200187 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * keep the reference count > 0.
189 */
190 dasd_get_device(device);
191
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100192 if (device->block) {
193 rc = dasd_alloc_queue(device->block);
194 if (rc) {
195 dasd_put_device(device);
196 return rc;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 device->state = DASD_STATE_KNOWN;
200 return 0;
201}
202
203/*
204 * Let the system forget about a device.
205 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800208 /* Disable extended error reporting for this device. */
209 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100211 if (device->discipline) {
212 if (device->discipline->uncheck_device)
213 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800214 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800217 if (device->base_discipline)
218 module_put(device->base_discipline->owner);
219 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 device->state = DASD_STATE_NEW;
221
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100222 if (device->block)
223 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* Give up reference we took in dasd_state_new_to_known. */
226 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200230static struct dentry *dasd_debugfs_setup(const char *name,
231 struct dentry *base_dentry)
232{
233 struct dentry *pde;
234
235 if (!base_dentry)
236 return NULL;
237 pde = debugfs_create_dir(name, base_dentry);
238 if (!pde || IS_ERR(pde))
239 return NULL;
240 return pde;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
244 * Request the irq line for the device.
245 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100246static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200248 struct dasd_block *block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int rc;
250
251 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200252 if (block) {
253 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100254 if (rc)
255 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200256 block->debugfs_dentry =
257 dasd_debugfs_setup(block->gdp->disk_name,
258 dasd_debugfs_root_entry);
259 dasd_profile_init(&block->profile, block->debugfs_dentry);
260 if (dasd_global_profile_level == DASD_PROFILE_ON)
261 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100262 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200263 device->debugfs_dentry =
264 dasd_debugfs_setup(dev_name(&device->cdev->dev),
265 dasd_debugfs_root_entry);
266 dasd_profile_init(&device->profile, device->debugfs_dentry);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100269 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100270 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200272 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
274
275 device->state = DASD_STATE_BASIC;
276 return 0;
277}
278
279/*
280 * Release the irq line for the device. Terminate any running i/o.
281 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100282static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Horst Hummel8f617012006-08-30 14:33:33 +0200284 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100285 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200286 dasd_profile_exit(&device->block->profile);
287 if (device->block->debugfs_dentry)
288 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289 dasd_gendisk_free(device->block);
290 dasd_block_clear_timer(device->block);
291 }
292 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200293 if (rc)
294 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100295 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200296 dasd_profile_exit(&device->profile);
297 if (device->debugfs_dentry)
298 debugfs_remove(device->debugfs_dentry);
Horst Hummel8f617012006-08-30 14:33:33 +0200299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
301 if (device->debug_area != NULL) {
302 debug_unregister(device->debug_area);
303 device->debug_area = NULL;
304 }
305 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
310 * Do the initial analysis. The do_analysis function may return
311 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
312 * until the discipline decides to continue the startup sequence
313 * by calling the function dasd_change_state. The eckd disciplines
314 * uses this to start a ccw that detects the format. The completion
315 * interrupt for this detection ccw uses the kernel event daemon to
316 * trigger the call to dasd_change_state. All this is done in the
317 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800318 * After the analysis ccw is done (do_analysis returned 0) the block
319 * device is setup.
320 * In case the analysis returns an error, the device setup is stopped
321 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100323static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100326 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800330 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100331 if (block) {
332 if (block->base->discipline->do_analysis != NULL)
333 rc = block->base->discipline->do_analysis(block);
334 if (rc) {
335 if (rc != -EAGAIN)
336 device->state = DASD_STATE_UNFMT;
337 return rc;
338 }
339 dasd_setup_queue(block);
340 set_capacity(block->gdp,
341 block->blocks << block->s2b_shift);
342 device->state = DASD_STATE_READY;
343 rc = dasd_scan_partitions(block);
344 if (rc)
345 device->state = DASD_STATE_BASIC;
346 } else {
347 device->state = DASD_STATE_READY;
348 }
Horst Hummel90f00942006-03-07 21:55:39 -0800349 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100352static inline
353int _wait_for_empty_queues(struct dasd_device *device)
354{
355 if (device->block)
356 return list_empty(&device->ccw_queue) &&
357 list_empty(&device->block->ccw_queue);
358 else
359 return list_empty(&device->ccw_queue);
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/*
363 * Remove device from block device layer. Destroy dirty buffers.
364 * Forget format information. Check if the target level is basic
365 * and if it is create fake disk for formatting.
366 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100367static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Horst Hummel8f617012006-08-30 14:33:33 +0200369 int rc;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100372 if (device->block) {
373 struct dasd_block *block = device->block;
374 rc = dasd_flush_block_queue(block);
375 if (rc) {
376 device->state = DASD_STATE_READY;
377 return rc;
378 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100379 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100380 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100381 block->blocks = 0;
382 block->bp_block = 0;
383 block->s2b_shift = 0;
384 }
Horst Hummel8f617012006-08-30 14:33:33 +0200385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388/*
Horst Hummel90f00942006-03-07 21:55:39 -0800389 * Back to basic.
390 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100391static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800392{
393 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200394 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800395}
396
397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * Make the device online and schedule the bottom half to start
399 * the requeueing of requests from the linux request queue to the
400 * ccw queue.
401 */
Horst Hummel8f617012006-08-30 14:33:33 +0200402static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403dasd_state_ready_to_online(struct dasd_device * device)
404{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100405 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100406 struct gendisk *disk;
407 struct disk_part_iter piter;
408 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100409
410 if (device->discipline->ready_to_online) {
411 rc = device->discipline->ready_to_online(device);
412 if (rc)
413 return rc;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100416 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100417 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100418 if ((device->features & DASD_FEATURE_USERAW)) {
419 disk = device->block->gdp;
420 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
421 return 0;
422 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100423 disk = device->block->bdev->bd_disk;
424 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
425 while ((part = disk_part_iter_next(&piter)))
426 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
427 disk_part_iter_exit(&piter);
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
432/*
433 * Stop the requeueing of requests again.
434 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100435static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100437 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100438 struct gendisk *disk;
439 struct disk_part_iter piter;
440 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100441
442 if (device->discipline->online_to_ready) {
443 rc = device->discipline->online_to_ready(device);
444 if (rc)
445 return rc;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100448 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100449 disk = device->block->bdev->bd_disk;
450 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
451 while ((part = disk_part_iter_next(&piter)))
452 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
453 disk_part_iter_exit(&piter);
454 }
Horst Hummel8f617012006-08-30 14:33:33 +0200455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458/*
459 * Device startup state changes.
460 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100461static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 int rc;
464
465 rc = 0;
466 if (device->state == DASD_STATE_NEW &&
467 device->target >= DASD_STATE_KNOWN)
468 rc = dasd_state_new_to_known(device);
469
470 if (!rc &&
471 device->state == DASD_STATE_KNOWN &&
472 device->target >= DASD_STATE_BASIC)
473 rc = dasd_state_known_to_basic(device);
474
475 if (!rc &&
476 device->state == DASD_STATE_BASIC &&
477 device->target >= DASD_STATE_READY)
478 rc = dasd_state_basic_to_ready(device);
479
480 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700481 device->state == DASD_STATE_UNFMT &&
482 device->target > DASD_STATE_UNFMT)
483 rc = -EPERM;
484
485 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 device->state == DASD_STATE_READY &&
487 device->target >= DASD_STATE_ONLINE)
488 rc = dasd_state_ready_to_online(device);
489
490 return rc;
491}
492
493/*
494 * Device shutdown state changes.
495 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100496static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Horst Hummel8f617012006-08-30 14:33:33 +0200498 int rc;
499
500 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (device->state == DASD_STATE_ONLINE &&
502 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200503 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200504
Horst Hummel8f617012006-08-30 14:33:33 +0200505 if (!rc &&
506 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200508 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800509
Horst Hummel8f617012006-08-30 14:33:33 +0200510 if (!rc &&
511 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800512 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200513 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800514
Horst Hummel8f617012006-08-30 14:33:33 +0200515 if (!rc &&
516 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200518 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200519
Horst Hummel8f617012006-08-30 14:33:33 +0200520 if (!rc &&
521 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200523 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Horst Hummel8f617012006-08-30 14:33:33 +0200525 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528/*
529 * This is the main startup/shutdown routine.
530 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100531static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Sebastian Ott181d9522009-06-22 12:08:21 +0200533 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (device->state == device->target)
536 /* Already where we want to go today... */
537 return;
538 if (device->state < device->target)
539 rc = dasd_increase_state(device);
540 else
541 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200542 if (rc == -EAGAIN)
543 return;
544 if (rc)
545 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200547 /* let user-space know that the device status changed */
548 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200549
550 if (device->state == device->target)
551 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
555 * Kick starter for devices that did not complete the startup/shutdown
556 * procedure or were sleeping because of a pending state.
557 * dasd_kick_device will schedule a call do do_kick_device to the kernel
558 * event daemon.
559 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100560static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Al Viro4927b3f2006-12-06 19:18:20 +0000562 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100563 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100565 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100566 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dasd_put_device(device);
568}
569
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100570void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 dasd_get_device(device);
573 /* queue call to dasd_kick_device to the kernel event daemon. */
574 schedule_work(&device->kick_work);
575}
576
577/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200578 * dasd_reload_device will schedule a call do do_reload_device to the kernel
579 * event daemon.
580 */
581static void do_reload_device(struct work_struct *work)
582{
583 struct dasd_device *device = container_of(work, struct dasd_device,
584 reload_device);
585 device->discipline->reload(device);
586 dasd_put_device(device);
587}
588
589void dasd_reload_device(struct dasd_device *device)
590{
591 dasd_get_device(device);
592 /* queue call to dasd_reload_device to the kernel event daemon. */
593 schedule_work(&device->reload_device);
594}
595EXPORT_SYMBOL(dasd_reload_device);
596
597/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200598 * dasd_restore_device will schedule a call do do_restore_device to the kernel
599 * event daemon.
600 */
601static void do_restore_device(struct work_struct *work)
602{
603 struct dasd_device *device = container_of(work, struct dasd_device,
604 restore_device);
605 device->cdev->drv->restore(device->cdev);
606 dasd_put_device(device);
607}
608
609void dasd_restore_device(struct dasd_device *device)
610{
611 dasd_get_device(device);
612 /* queue call to dasd_restore_device to the kernel event daemon. */
613 schedule_work(&device->restore_device);
614}
615
616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * Set the target state for a device and starts the state change.
618 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100619void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200621 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100622 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* If we are in probeonly mode stop at DASD_STATE_READY. */
624 if (dasd_probeonly && target > DASD_STATE_READY)
625 target = DASD_STATE_READY;
626 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100627 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 wake_up(&dasd_init_waitq);
629 device->target = target;
630 }
631 if (device->state != device->target)
632 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100633 mutex_unlock(&device->state_mutex);
634 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/*
638 * Enable devices with device numbers in [from..to].
639 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100640static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 return (device->state == device->target);
643}
644
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100645void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 dasd_set_target_state(device, DASD_STATE_ONLINE);
648 if (device->state <= DASD_STATE_KNOWN)
649 /* No discipline for device found. */
650 dasd_set_target_state(device, DASD_STATE_NEW);
651 /* Now wait for the devices to come up. */
652 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400653
654 dasd_reload_device(device);
655 if (device->discipline->kick_validate)
656 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659/*
660 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
661 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200662
663unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#ifdef CONFIG_DASD_PROFILE
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200666struct dasd_profile_info dasd_global_profile_data;
667static struct dentry *dasd_global_profile_dentry;
668static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/*
671 * Add profiling information for cqr before execution.
672 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100673static void dasd_profile_start(struct dasd_block *block,
674 struct dasd_ccw_req *cqr,
675 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct list_head *l;
678 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200679 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /* count the length of the chanq for statistics */
682 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200683 if (dasd_global_profile_level || block->profile.data)
684 list_for_each(l, &block->ccw_queue)
685 if (++counter >= 31)
686 break;
687
688 if (dasd_global_profile_level) {
689 dasd_global_profile_data.dasd_io_nr_req[counter]++;
690 if (rq_data_dir(req) == READ)
691 dasd_global_profile_data.dasd_read_nr_req[counter]++;
692 }
693
694 spin_lock(&block->profile.lock);
695 if (block->profile.data)
696 block->profile.data->dasd_io_nr_req[counter]++;
697 if (rq_data_dir(req) == READ)
698 block->profile.data->dasd_read_nr_req[counter]++;
699 spin_unlock(&block->profile.lock);
700
701 /*
702 * We count the request for the start device, even though it may run on
703 * some other device due to error recovery. This way we make sure that
704 * we count each request only once.
705 */
706 device = cqr->startdev;
707 if (device->profile.data) {
708 counter = 1; /* request is not yet queued on the start device */
709 list_for_each(l, &device->ccw_queue)
710 if (++counter >= 31)
711 break;
712 }
713 spin_lock(&device->profile.lock);
714 if (device->profile.data) {
715 device->profile.data->dasd_io_nr_req[counter]++;
716 if (rq_data_dir(req) == READ)
717 device->profile.data->dasd_read_nr_req[counter]++;
718 }
719 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
723 * Add profiling information for cqr after execution.
724 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200725
726#define dasd_profile_counter(value, index) \
727{ \
728 for (index = 0; index < 31 && value >> (2+index); index++) \
729 ; \
730}
731
732static void dasd_profile_end_add_data(struct dasd_profile_info *data,
733 int is_alias,
734 int is_tpm,
735 int is_read,
736 long sectors,
737 int sectors_ind,
738 int tottime_ind,
739 int tottimeps_ind,
740 int strtime_ind,
741 int irqtime_ind,
742 int irqtimeps_ind,
743 int endtime_ind)
744{
745 /* in case of an overflow, reset the whole profile */
746 if (data->dasd_io_reqs == UINT_MAX) {
747 memset(data, 0, sizeof(*data));
748 getnstimeofday(&data->starttod);
749 }
750 data->dasd_io_reqs++;
751 data->dasd_io_sects += sectors;
752 if (is_alias)
753 data->dasd_io_alias++;
754 if (is_tpm)
755 data->dasd_io_tpm++;
756
757 data->dasd_io_secs[sectors_ind]++;
758 data->dasd_io_times[tottime_ind]++;
759 data->dasd_io_timps[tottimeps_ind]++;
760 data->dasd_io_time1[strtime_ind]++;
761 data->dasd_io_time2[irqtime_ind]++;
762 data->dasd_io_time2ps[irqtimeps_ind]++;
763 data->dasd_io_time3[endtime_ind]++;
764
765 if (is_read) {
766 data->dasd_read_reqs++;
767 data->dasd_read_sects += sectors;
768 if (is_alias)
769 data->dasd_read_alias++;
770 if (is_tpm)
771 data->dasd_read_tpm++;
772 data->dasd_read_secs[sectors_ind]++;
773 data->dasd_read_times[tottime_ind]++;
774 data->dasd_read_time1[strtime_ind]++;
775 data->dasd_read_time2[irqtime_ind]++;
776 data->dasd_read_time3[endtime_ind]++;
777 }
778}
779
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100780static void dasd_profile_end(struct dasd_block *block,
781 struct dasd_ccw_req *cqr,
782 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 long strtime, irqtime, endtime, tottime; /* in microseconds */
785 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200786 struct dasd_device *device;
787 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
788 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200790 device = cqr->startdev;
791 if (!(dasd_global_profile_level ||
792 block->profile.data ||
793 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
795
Tejun Heo83096eb2009-05-07 22:24:39 +0900796 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!cqr->buildclk || !cqr->startclk ||
798 !cqr->stopclk || !cqr->endclk ||
799 !sectors)
800 return;
801
802 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
803 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
804 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
805 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
806 tottimeps = tottime / sectors;
807
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200808 dasd_profile_counter(sectors, sectors_ind);
809 dasd_profile_counter(tottime, tottime_ind);
810 dasd_profile_counter(tottimeps, tottimeps_ind);
811 dasd_profile_counter(strtime, strtime_ind);
812 dasd_profile_counter(irqtime, irqtime_ind);
813 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
814 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200816 if (dasd_global_profile_level) {
817 dasd_profile_end_add_data(&dasd_global_profile_data,
818 cqr->startdev != block->base,
819 cqr->cpmode == 1,
820 rq_data_dir(req) == READ,
821 sectors, sectors_ind, tottime_ind,
822 tottimeps_ind, strtime_ind,
823 irqtime_ind, irqtimeps_ind,
824 endtime_ind);
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200827 spin_lock(&block->profile.lock);
828 if (block->profile.data)
829 dasd_profile_end_add_data(block->profile.data,
830 cqr->startdev != block->base,
831 cqr->cpmode == 1,
832 rq_data_dir(req) == READ,
833 sectors, sectors_ind, tottime_ind,
834 tottimeps_ind, strtime_ind,
835 irqtime_ind, irqtimeps_ind,
836 endtime_ind);
837 spin_unlock(&block->profile.lock);
838
839 spin_lock(&device->profile.lock);
840 if (device->profile.data)
841 dasd_profile_end_add_data(device->profile.data,
842 cqr->startdev != block->base,
843 cqr->cpmode == 1,
844 rq_data_dir(req) == READ,
845 sectors, sectors_ind, tottime_ind,
846 tottimeps_ind, strtime_ind,
847 irqtime_ind, irqtimeps_ind,
848 endtime_ind);
849 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200851
852void dasd_profile_reset(struct dasd_profile *profile)
853{
854 struct dasd_profile_info *data;
855
856 spin_lock_bh(&profile->lock);
857 data = profile->data;
858 if (!data) {
859 spin_unlock_bh(&profile->lock);
860 return;
861 }
862 memset(data, 0, sizeof(*data));
863 getnstimeofday(&data->starttod);
864 spin_unlock_bh(&profile->lock);
865}
866
867void dasd_global_profile_reset(void)
868{
869 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
870 getnstimeofday(&dasd_global_profile_data.starttod);
871}
872
873int dasd_profile_on(struct dasd_profile *profile)
874{
875 struct dasd_profile_info *data;
876
877 data = kzalloc(sizeof(*data), GFP_KERNEL);
878 if (!data)
879 return -ENOMEM;
880 spin_lock_bh(&profile->lock);
881 if (profile->data) {
882 spin_unlock_bh(&profile->lock);
883 kfree(data);
884 return 0;
885 }
886 getnstimeofday(&data->starttod);
887 profile->data = data;
888 spin_unlock_bh(&profile->lock);
889 return 0;
890}
891
892void dasd_profile_off(struct dasd_profile *profile)
893{
894 spin_lock_bh(&profile->lock);
895 kfree(profile->data);
896 profile->data = NULL;
897 spin_unlock_bh(&profile->lock);
898}
899
900char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
901{
902 char *buffer;
903
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200904 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200905 if (buffer == NULL)
906 return ERR_PTR(-ENOMEM);
907 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200908 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200909 return ERR_PTR(-EFAULT);
910 }
911 /* got the string, now strip linefeed. */
912 if (buffer[user_len - 1] == '\n')
913 buffer[user_len - 1] = 0;
914 else
915 buffer[user_len] = 0;
916 return buffer;
917}
918
919static ssize_t dasd_stats_write(struct file *file,
920 const char __user *user_buf,
921 size_t user_len, loff_t *pos)
922{
923 char *buffer, *str;
924 int rc;
925 struct seq_file *m = (struct seq_file *)file->private_data;
926 struct dasd_profile *prof = m->private;
927
928 if (user_len > 65536)
929 user_len = 65536;
930 buffer = dasd_get_user_string(user_buf, user_len);
931 if (IS_ERR(buffer))
932 return PTR_ERR(buffer);
933
934 str = skip_spaces(buffer);
935 rc = user_len;
936 if (strncmp(str, "reset", 5) == 0) {
937 dasd_profile_reset(prof);
938 } else if (strncmp(str, "on", 2) == 0) {
939 rc = dasd_profile_on(prof);
940 if (!rc)
941 rc = user_len;
942 } else if (strncmp(str, "off", 3) == 0) {
943 dasd_profile_off(prof);
944 } else
945 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200946 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200947 return rc;
948}
949
950static void dasd_stats_array(struct seq_file *m, unsigned int *array)
951{
952 int i;
953
954 for (i = 0; i < 32; i++)
955 seq_printf(m, "%u ", array[i]);
956 seq_putc(m, '\n');
957}
958
959static void dasd_stats_seq_print(struct seq_file *m,
960 struct dasd_profile_info *data)
961{
962 seq_printf(m, "start_time %ld.%09ld\n",
963 data->starttod.tv_sec, data->starttod.tv_nsec);
964 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
965 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
966 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
967 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
968 seq_printf(m, "histogram_sectors ");
969 dasd_stats_array(m, data->dasd_io_secs);
970 seq_printf(m, "histogram_io_times ");
971 dasd_stats_array(m, data->dasd_io_times);
972 seq_printf(m, "histogram_io_times_weighted ");
973 dasd_stats_array(m, data->dasd_io_timps);
974 seq_printf(m, "histogram_time_build_to_ssch ");
975 dasd_stats_array(m, data->dasd_io_time1);
976 seq_printf(m, "histogram_time_ssch_to_irq ");
977 dasd_stats_array(m, data->dasd_io_time2);
978 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
979 dasd_stats_array(m, data->dasd_io_time2ps);
980 seq_printf(m, "histogram_time_irq_to_end ");
981 dasd_stats_array(m, data->dasd_io_time3);
982 seq_printf(m, "histogram_ccw_queue_length ");
983 dasd_stats_array(m, data->dasd_io_nr_req);
984 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
985 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
986 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
987 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
988 seq_printf(m, "histogram_read_sectors ");
989 dasd_stats_array(m, data->dasd_read_secs);
990 seq_printf(m, "histogram_read_times ");
991 dasd_stats_array(m, data->dasd_read_times);
992 seq_printf(m, "histogram_read_time_build_to_ssch ");
993 dasd_stats_array(m, data->dasd_read_time1);
994 seq_printf(m, "histogram_read_time_ssch_to_irq ");
995 dasd_stats_array(m, data->dasd_read_time2);
996 seq_printf(m, "histogram_read_time_irq_to_end ");
997 dasd_stats_array(m, data->dasd_read_time3);
998 seq_printf(m, "histogram_read_ccw_queue_length ");
999 dasd_stats_array(m, data->dasd_read_nr_req);
1000}
1001
1002static int dasd_stats_show(struct seq_file *m, void *v)
1003{
1004 struct dasd_profile *profile;
1005 struct dasd_profile_info *data;
1006
1007 profile = m->private;
1008 spin_lock_bh(&profile->lock);
1009 data = profile->data;
1010 if (!data) {
1011 spin_unlock_bh(&profile->lock);
1012 seq_printf(m, "disabled\n");
1013 return 0;
1014 }
1015 dasd_stats_seq_print(m, data);
1016 spin_unlock_bh(&profile->lock);
1017 return 0;
1018}
1019
1020static int dasd_stats_open(struct inode *inode, struct file *file)
1021{
1022 struct dasd_profile *profile = inode->i_private;
1023 return single_open(file, dasd_stats_show, profile);
1024}
1025
1026static const struct file_operations dasd_stats_raw_fops = {
1027 .owner = THIS_MODULE,
1028 .open = dasd_stats_open,
1029 .read = seq_read,
1030 .llseek = seq_lseek,
1031 .release = single_release,
1032 .write = dasd_stats_write,
1033};
1034
1035static ssize_t dasd_stats_global_write(struct file *file,
1036 const char __user *user_buf,
1037 size_t user_len, loff_t *pos)
1038{
1039 char *buffer, *str;
1040 ssize_t rc;
1041
1042 if (user_len > 65536)
1043 user_len = 65536;
1044 buffer = dasd_get_user_string(user_buf, user_len);
1045 if (IS_ERR(buffer))
1046 return PTR_ERR(buffer);
1047 str = skip_spaces(buffer);
1048 rc = user_len;
1049 if (strncmp(str, "reset", 5) == 0) {
1050 dasd_global_profile_reset();
1051 } else if (strncmp(str, "on", 2) == 0) {
1052 dasd_global_profile_reset();
1053 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1054 } else if (strncmp(str, "off", 3) == 0) {
1055 dasd_global_profile_level = DASD_PROFILE_OFF;
1056 } else
1057 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001058 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001059 return rc;
1060}
1061
1062static int dasd_stats_global_show(struct seq_file *m, void *v)
1063{
1064 if (!dasd_global_profile_level) {
1065 seq_printf(m, "disabled\n");
1066 return 0;
1067 }
1068 dasd_stats_seq_print(m, &dasd_global_profile_data);
1069 return 0;
1070}
1071
1072static int dasd_stats_global_open(struct inode *inode, struct file *file)
1073{
1074 return single_open(file, dasd_stats_global_show, NULL);
1075}
1076
1077static const struct file_operations dasd_stats_global_fops = {
1078 .owner = THIS_MODULE,
1079 .open = dasd_stats_global_open,
1080 .read = seq_read,
1081 .llseek = seq_lseek,
1082 .release = single_release,
1083 .write = dasd_stats_global_write,
1084};
1085
1086static void dasd_profile_init(struct dasd_profile *profile,
1087 struct dentry *base_dentry)
1088{
Al Virof4ae40a2011-07-24 04:33:43 -04001089 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001090 struct dentry *pde;
1091
1092 if (!base_dentry)
1093 return;
1094 profile->dentry = NULL;
1095 profile->data = NULL;
1096 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1097 pde = debugfs_create_file("statistics", mode, base_dentry,
1098 profile, &dasd_stats_raw_fops);
1099 if (pde && !IS_ERR(pde))
1100 profile->dentry = pde;
1101 return;
1102}
1103
1104static void dasd_profile_exit(struct dasd_profile *profile)
1105{
1106 dasd_profile_off(profile);
1107 if (profile->dentry) {
1108 debugfs_remove(profile->dentry);
1109 profile->dentry = NULL;
1110 }
1111}
1112
1113static void dasd_statistics_removeroot(void)
1114{
1115 dasd_global_profile_level = DASD_PROFILE_OFF;
1116 if (dasd_global_profile_dentry) {
1117 debugfs_remove(dasd_global_profile_dentry);
1118 dasd_global_profile_dentry = NULL;
1119 }
1120 if (dasd_debugfs_global_entry)
1121 debugfs_remove(dasd_debugfs_global_entry);
1122 if (dasd_debugfs_root_entry)
1123 debugfs_remove(dasd_debugfs_root_entry);
1124}
1125
1126static void dasd_statistics_createroot(void)
1127{
Al Virof4ae40a2011-07-24 04:33:43 -04001128 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001129 struct dentry *pde;
1130
1131 dasd_debugfs_root_entry = NULL;
1132 dasd_debugfs_global_entry = NULL;
1133 dasd_global_profile_dentry = NULL;
1134 pde = debugfs_create_dir("dasd", NULL);
1135 if (!pde || IS_ERR(pde))
1136 goto error;
1137 dasd_debugfs_root_entry = pde;
1138 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1139 if (!pde || IS_ERR(pde))
1140 goto error;
1141 dasd_debugfs_global_entry = pde;
1142
1143 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1144 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1145 NULL, &dasd_stats_global_fops);
1146 if (!pde || IS_ERR(pde))
1147 goto error;
1148 dasd_global_profile_dentry = pde;
1149 return;
1150
1151error:
1152 DBF_EVENT(DBF_ERR, "%s",
1153 "Creation of the dasd debugfs interface failed");
1154 dasd_statistics_removeroot();
1155 return;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001159#define dasd_profile_start(block, cqr, req) do {} while (0)
1160#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001161
1162static void dasd_statistics_createroot(void)
1163{
1164 return;
1165}
1166
1167static void dasd_statistics_removeroot(void)
1168{
1169 return;
1170}
1171
1172int dasd_stats_generic_show(struct seq_file *m, void *v)
1173{
1174 seq_printf(m, "Statistics are not activated in this kernel\n");
1175 return 0;
1176}
1177
1178static void dasd_profile_init(struct dasd_profile *profile,
1179 struct dentry *base_dentry)
1180{
1181 return;
1182}
1183
1184static void dasd_profile_exit(struct dasd_profile *profile)
1185{
1186 return;
1187}
1188
1189int dasd_profile_on(struct dasd_profile *profile)
1190{
1191 return 0;
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#endif /* CONFIG_DASD_PROFILE */
1195
1196/*
1197 * Allocate memory for a channel program with 'cplength' channel
1198 * command words and 'datasize' additional space. There are two
1199 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1200 * memory and 2) dasd_smalloc_request uses the static ccw memory
1201 * that gets allocated for each device.
1202 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001203struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001204 int datasize,
1205 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct dasd_ccw_req *cqr;
1208
1209 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001210 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001211 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001213 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (cqr == NULL)
1215 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 cqr->cpaddr = NULL;
1217 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001218 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 GFP_ATOMIC | GFP_DMA);
1220 if (cqr->cpaddr == NULL) {
1221 kfree(cqr);
1222 return ERR_PTR(-ENOMEM);
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 cqr->data = NULL;
1226 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001227 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001229 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 kfree(cqr);
1231 return ERR_PTR(-ENOMEM);
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001234 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1236 dasd_get_device(device);
1237 return cqr;
1238}
1239
Stefan Haberland68b781f2009-09-11 10:28:29 +02001240struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001241 int datasize,
1242 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 unsigned long flags;
1245 struct dasd_ccw_req *cqr;
1246 char *data;
1247 int size;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1250 if (cplength > 0)
1251 size += cplength * sizeof(struct ccw1);
1252 if (datasize > 0)
1253 size += datasize;
1254 spin_lock_irqsave(&device->mem_lock, flags);
1255 cqr = (struct dasd_ccw_req *)
1256 dasd_alloc_chunk(&device->ccw_chunks, size);
1257 spin_unlock_irqrestore(&device->mem_lock, flags);
1258 if (cqr == NULL)
1259 return ERR_PTR(-ENOMEM);
1260 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1261 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1262 cqr->cpaddr = NULL;
1263 if (cplength > 0) {
1264 cqr->cpaddr = (struct ccw1 *) data;
1265 data += cplength*sizeof(struct ccw1);
1266 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1267 }
1268 cqr->data = NULL;
1269 if (datasize > 0) {
1270 cqr->data = data;
1271 memset(cqr->data, 0, datasize);
1272 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001273 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1275 dasd_get_device(device);
1276 return cqr;
1277}
1278
1279/*
1280 * Free memory of a channel program. This function needs to free all the
1281 * idal lists that might have been created by dasd_set_cda and the
1282 * struct dasd_ccw_req itself.
1283 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001284void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001286#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct ccw1 *ccw;
1288
1289 /* Clear any idals used for the request. */
1290 ccw = cqr->cpaddr;
1291 do {
1292 clear_normalized_cda(ccw);
1293 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1294#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001295 kfree(cqr->cpaddr);
1296 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 kfree(cqr);
1298 dasd_put_device(device);
1299}
1300
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001301void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 unsigned long flags;
1304
1305 spin_lock_irqsave(&device->mem_lock, flags);
1306 dasd_free_chunk(&device->ccw_chunks, cqr);
1307 spin_unlock_irqrestore(&device->mem_lock, flags);
1308 dasd_put_device(device);
1309}
1310
1311/*
1312 * Check discipline magic in cqr.
1313 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001314static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct dasd_device *device;
1317
1318 if (cqr == NULL)
1319 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001320 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001322 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 " dasd_ccw_req 0x%08x magic doesn't match"
1324 " discipline 0x%08x",
1325 cqr->magic,
1326 *(unsigned int *) device->discipline->name);
1327 return -EINVAL;
1328 }
1329 return 0;
1330}
1331
1332/*
1333 * Terminate the current i/o and set the request to clear_pending.
1334 * Timer keeps device runnig.
1335 * ccw_device_clear can fail if the i/o subsystem
1336 * is in a bad mood.
1337 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001338int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct dasd_device *device;
1341 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001342 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 /* Check the cqr */
1345 rc = dasd_check_cqr(cqr);
1346 if (rc)
1347 return rc;
1348 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001349 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1351 rc = ccw_device_clear(device->cdev, (long) cqr);
1352 switch (rc) {
1353 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001354 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001355 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001356 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 DBF_DEV_EVENT(DBF_DEBUG, device,
1358 "terminate cqr %p successful",
1359 cqr);
1360 break;
1361 case -ENODEV:
1362 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1363 "device gone, retry");
1364 break;
1365 case -EIO:
1366 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1367 "I/O error, retry");
1368 break;
1369 case -EINVAL:
1370 case -EBUSY:
1371 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1372 "device busy, retry later");
1373 break;
1374 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001375 /* internal error 10 - unknown rc*/
1376 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1377 dev_err(&device->cdev->dev, "An error occurred in the "
1378 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 BUG();
1380 break;
1381 }
1382 retries++;
1383 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001384 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return rc;
1386}
1387
1388/*
1389 * Start the i/o. This start_IO can fail if the channel is really busy.
1390 * In that case set up a timer to start the request later.
1391 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct dasd_device *device;
1395 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001396 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /* Check the cqr */
1399 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001400 if (rc) {
1401 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001403 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001404 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001405 if (((cqr->block &&
1406 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1407 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1408 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1409 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1410 "because of stolen lock", cqr);
1411 cqr->status = DASD_CQR_ERROR;
1412 cqr->intrc = -EPERM;
1413 return -EPERM;
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001416 /* internal error 14 - start_IO run out of retries */
1417 sprintf(errorstring, "14 %p", cqr);
1418 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1419 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001420 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -EIO;
1422 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001423 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 cqr->starttime = jiffies;
1425 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001426 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1427 cqr->lpm &= device->path_data.opm;
1428 if (!cqr->lpm)
1429 cqr->lpm = device->path_data.opm;
1430 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001431 if (cqr->cpmode == 1) {
1432 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1433 (long) cqr, cqr->lpm);
1434 } else {
1435 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1436 (long) cqr, cqr->lpm, 0);
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 switch (rc) {
1439 case 0:
1440 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 break;
1442 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001443 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 "start_IO: device busy, retry later");
1445 break;
1446 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001447 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 "start_IO: request timeout, retry later");
1449 break;
1450 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001451 /* -EACCES indicates that the request used only a subset of the
1452 * available paths and all these paths are gone. If the lpm of
1453 * this request was only a subset of the opm (e.g. the ppm) then
1454 * we just do a retry with all available paths.
1455 * If we already use the full opm, something is amiss, and we
1456 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1459 DBF_DEV_EVENT(DBF_WARNING, device,
1460 "start_IO: selected paths gone (%x)",
1461 cqr->lpm);
1462 } else if (cqr->lpm != device->path_data.opm) {
1463 cqr->lpm = device->path_data.opm;
1464 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1465 "start_IO: selected paths gone,"
1466 " retry on all paths");
1467 } else {
1468 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1469 "start_IO: all paths in opm gone,"
1470 " do path verification");
1471 dasd_generic_last_path_gone(device);
1472 device->path_data.opm = 0;
1473 device->path_data.ppm = 0;
1474 device->path_data.npm = 0;
1475 device->path_data.tbvpm =
1476 ccw_device_get_path_mask(device->cdev);
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 break;
1479 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001480 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001481 "start_IO: -ENODEV device gone, retry");
1482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001484 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001485 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001487 case -EINVAL:
1488 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001489 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001490 "start_IO: -EINVAL device currently "
1491 "not accessible");
1492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001494 /* internal error 11 - unknown rc */
1495 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1496 dev_err(&device->cdev->dev,
1497 "An error occurred in the DASD device driver, "
1498 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 BUG();
1500 break;
1501 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001502 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return rc;
1504}
1505
1506/*
1507 * Timeout function for dasd devices. This is used for different purposes
1508 * 1) missing interrupt handler for normal operation
1509 * 2) delayed start of request where start_IO failed with -EBUSY
1510 * 3) timeout for missing state change interrupts
1511 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1512 * DASD_CQR_QUEUED for 2) and 3).
1513 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001514static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 unsigned long flags;
1517 struct dasd_device *device;
1518
1519 device = (struct dasd_device *) ptr;
1520 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1521 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001522 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001524 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
1528 * Setup timeout for a device in jiffies.
1529 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001530void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001532 if (expires == 0)
1533 del_timer(&device->timer);
1534 else
1535 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538/*
1539 * Clear timeout for a device.
1540 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001541void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001543 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001546static void dasd_handle_killed_request(struct ccw_device *cdev,
1547 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct dasd_ccw_req *cqr;
1550 struct dasd_device *device;
1551
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001552 if (!intparm)
1553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 cqr = (struct dasd_ccw_req *) intparm;
1555 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001556 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1557 "invalid status in handle_killed_request: "
1558 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return;
1560 }
1561
Stefan Haberland589c74d2010-02-26 22:37:47 +01001562 device = dasd_device_from_cdev_locked(cdev);
1563 if (IS_ERR(device)) {
1564 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1565 "unable to get device from cdev");
1566 return;
1567 }
1568
1569 if (!cqr->startdev ||
1570 device != cqr->startdev ||
1571 strncmp(cqr->startdev->discipline->ebcname,
1572 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001573 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1574 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001575 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return;
1577 }
1578
1579 /* Schedule request to be retried. */
1580 cqr->status = DASD_CQR_QUEUED;
1581
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001582 dasd_device_clear_timer(device);
1583 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 dasd_put_device(device);
1585}
1586
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001587void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001589 /* First of all start sense subsystem status request. */
1590 dasd_eer_snss(device);
1591
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001592 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001593 dasd_schedule_device_bh(device);
1594 if (device->block)
1595 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598/*
1599 * Interrupt handler for "normal" ssch-io based dasd devices.
1600 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001601void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1602 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 struct dasd_ccw_req *cqr, *next;
1605 struct dasd_device *device;
1606 unsigned long long now;
1607 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 if (IS_ERR(irb)) {
1610 switch (PTR_ERR(irb)) {
1611 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 break;
1613 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001614 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1615 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
1617 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001618 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1619 "unknown error %ld\n", __func__,
1620 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001622 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return;
1624 }
1625
Heiko Carstens1aae0562013-01-30 09:49:40 +01001626 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001627 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001628 /* check for conditions that should be handled immediately */
1629 if (!cqr ||
1630 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1631 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001632 if (cqr)
1633 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001634 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001635 if (IS_ERR(device))
1636 return;
1637 /* ignore unsolicited interrupts for DIAG discipline */
1638 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001642 device->discipline->dump_sense_dbf(device, irb, "int");
1643 if (device->features & DASD_FEATURE_ERPLOG)
1644 device->discipline->dump_sense(device, cqr, irb);
1645 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001646 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001648 if (!cqr)
1649 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001651 device = (struct dasd_device *) cqr->startdev;
1652 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001654 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1655 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return;
1657 }
1658
1659 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001660 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001661 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001662 cqr->status = DASD_CQR_CLEARED;
1663 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001664 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001665 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return;
1667 }
1668
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001669 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001671 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1672 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return;
1674 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001675
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001676 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001678 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1679 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001680 /* request was completed successfully */
1681 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 cqr->stopclk = now;
1683 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001684 if (cqr->devlist.next != &device->ccw_queue) {
1685 next = list_entry(cqr->devlist.next,
1686 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001688 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001689 /*
1690 * If we don't want complex ERP for this request, then just
1691 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001692 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001693 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001694 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001695 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001696 DBF_DEV_EVENT(DBF_DEBUG, device,
1697 "default ERP in fastpath "
1698 "(%i retries left)",
1699 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001700 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1701 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001702 cqr->status = DASD_CQR_QUEUED;
1703 next = cqr;
1704 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001706 }
1707 if (next && (next->status == DASD_CQR_QUEUED) &&
1708 (!device->stopped)) {
1709 if (device->discipline->start_IO(next) == 0)
1710 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001713 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001715 dasd_device_clear_timer(device);
1716 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001719enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1720{
1721 struct dasd_device *device;
1722
1723 device = dasd_device_from_cdev_locked(cdev);
1724
1725 if (IS_ERR(device))
1726 goto out;
1727 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1728 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001729 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001730 dasd_put_device(device);
1731 goto out;
1732 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001733 if (device->discipline->dump_sense_dbf)
1734 device->discipline->dump_sense_dbf(device, irb, "uc");
1735 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001736 dasd_put_device(device);
1737out:
1738 return UC_TODO_RETRY;
1739}
1740EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001743 * If we have an error on a dasd_block layer request then we cancel
1744 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001746static void __dasd_device_recovery(struct dasd_device *device,
1747 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 struct list_head *l, *n;
1750 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001753 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001755 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001757
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001758 list_for_each_safe(l, n, &device->ccw_queue) {
1759 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1760 if (cqr->status == DASD_CQR_QUEUED &&
1761 ref_cqr->block == cqr->block) {
1762 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001764 }
1765};
1766
1767/*
1768 * Remove those ccw requests from the queue that need to be returned
1769 * to the upper layer.
1770 */
1771static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1772 struct list_head *final_queue)
1773{
1774 struct list_head *l, *n;
1775 struct dasd_ccw_req *cqr;
1776
1777 /* Process request with final status. */
1778 list_for_each_safe(l, n, &device->ccw_queue) {
1779 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1780
1781 /* Stop list processing at the first non-final request. */
1782 if (cqr->status == DASD_CQR_QUEUED ||
1783 cqr->status == DASD_CQR_IN_IO ||
1784 cqr->status == DASD_CQR_CLEAR_PENDING)
1785 break;
1786 if (cqr->status == DASD_CQR_ERROR) {
1787 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001789 /* Rechain finished requests to final queue */
1790 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792}
1793
1794/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001795 * the cqrs from the final queue are returned to the upper layer
1796 * by setting a dasd_block state and calling the callback function
1797 */
1798static void __dasd_device_process_final_queue(struct dasd_device *device,
1799 struct list_head *final_queue)
1800{
1801 struct list_head *l, *n;
1802 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001803 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001804 void (*callback)(struct dasd_ccw_req *, void *data);
1805 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001806 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001807
1808 list_for_each_safe(l, n, final_queue) {
1809 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1810 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001811 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001812 callback = cqr->callback;
1813 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001814 if (block)
1815 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001816 switch (cqr->status) {
1817 case DASD_CQR_SUCCESS:
1818 cqr->status = DASD_CQR_DONE;
1819 break;
1820 case DASD_CQR_ERROR:
1821 cqr->status = DASD_CQR_NEED_ERP;
1822 break;
1823 case DASD_CQR_CLEARED:
1824 cqr->status = DASD_CQR_TERMINATED;
1825 break;
1826 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001827 /* internal error 12 - wrong cqr status*/
1828 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1829 dev_err(&device->cdev->dev,
1830 "An error occurred in the DASD device driver, "
1831 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001832 BUG();
1833 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001834 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001835 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001836 if (block)
1837 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001838 }
1839}
1840
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001841/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 * Take a look at the first request on the ccw queue and check
1843 * if it reached its expire time. If so, terminate the IO.
1844 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001845static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct dasd_ccw_req *cqr;
1848
1849 if (list_empty(&device->ccw_queue))
1850 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001851 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001852 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1853 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001854 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1855 /*
1856 * IO in safe offline processing should not
1857 * run out of retries
1858 */
1859 cqr->retries++;
1860 }
Horst Hummel29145a62006-12-04 15:40:15 +01001861 if (device->discipline->term_IO(cqr) != 0) {
1862 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001863 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001864 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001865 "ended, retrying in 5 s\n",
1866 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001867 cqr->expires += 5*HZ;
1868 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001869 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001870 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001871 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001872 "remaining\n", cqr, (cqr->expires/HZ),
1873 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 }
1876}
1877
1878/*
1879 * Take a look at the first request on the ccw queue and check
1880 * if it needs to be started.
1881 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001882static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 struct dasd_ccw_req *cqr;
1885 int rc;
1886
1887 if (list_empty(&device->ccw_queue))
1888 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001889 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001890 if (cqr->status != DASD_CQR_QUEUED)
1891 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001892 /* when device is stopped, return request to previous layer
1893 * exception: only the disconnect or unresumed bits are set and the
1894 * cqr is a path verification request
1895 */
1896 if (device->stopped &&
1897 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1898 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1899 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001900 cqr->status = DASD_CQR_CLEARED;
1901 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001902 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001903 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001904
1905 rc = device->discipline->start_IO(cqr);
1906 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001907 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001908 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001909 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001910 } else
1911 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001912 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001913}
1914
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001915static void __dasd_device_check_path_events(struct dasd_device *device)
1916{
1917 int rc;
1918
1919 if (device->path_data.tbvpm) {
1920 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1921 DASD_UNRESUMED_PM))
1922 return;
1923 rc = device->discipline->verify_path(
1924 device, device->path_data.tbvpm);
1925 if (rc)
1926 dasd_device_set_timer(device, 50);
1927 else
1928 device->path_data.tbvpm = 0;
1929 }
1930};
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001933 * Go through all request on the dasd_device request queue,
1934 * terminate them on the cdev if necessary, and return them to the
1935 * submitting layer via callback.
1936 * Note:
1937 * Make sure that all 'submitting layers' still exist when
1938 * this function is called!. In other words, when 'device' is a base
1939 * device then all block layer requests must have been removed before
1940 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001942int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001944 struct dasd_ccw_req *cqr, *n;
1945 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 INIT_LIST_HEAD(&flush_queue);
1949 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001950 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001951 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001952 /* Check status and move request to flush_queue */
1953 switch (cqr->status) {
1954 case DASD_CQR_IN_IO:
1955 rc = device->discipline->term_IO(cqr);
1956 if (rc) {
1957 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001958 dev_err(&device->cdev->dev,
1959 "Flushing the DASD request queue "
1960 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001961 /* stop flush processing */
1962 goto finished;
1963 }
1964 break;
1965 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001966 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001967 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001968 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001969 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001970 break;
1971 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001972 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001973 }
Horst Hummel8f617012006-08-30 14:33:33 +02001974finished:
1975 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001976 /*
1977 * After this point all requests must be in state CLEAR_PENDING,
1978 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1979 * one of the others.
1980 */
1981 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1982 wait_event(dasd_flush_wq,
1983 (cqr->status != DASD_CQR_CLEAR_PENDING));
1984 /*
1985 * Now set each request back to TERMINATED, DONE or NEED_ERP
1986 * and call the callback function of flushed requests
1987 */
1988 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001989 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
1992/*
1993 * Acquire the device lock and process queues for the device.
1994 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001995static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 atomic_set (&device->tasklet_scheduled, 0);
2000 INIT_LIST_HEAD(&final_queue);
2001 spin_lock_irq(get_ccwdev_lock(device->cdev));
2002 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002003 __dasd_device_check_expire(device);
2004 /* find final requests on ccw queue */
2005 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002006 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2008 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002009 __dasd_device_process_final_queue(device, &final_queue);
2010 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002012 __dasd_device_start_head(device);
2013 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002014 if (waitqueue_active(&shutdown_waitq))
2015 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 dasd_put_device(device);
2017}
2018
2019/*
2020 * Schedules a call to dasd_tasklet over the device tasklet.
2021 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002022void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002025 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return;
2027 dasd_get_device(device);
2028 tasklet_hi_schedule(&device->tasklet);
2029}
2030
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002031void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2032{
2033 device->stopped |= bits;
2034}
2035EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2036
2037void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2038{
2039 device->stopped &= ~bits;
2040 if (!device->stopped)
2041 wake_up(&generic_waitq);
2042}
2043EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002046 * Queue a request to the head of the device ccw_queue.
2047 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002049void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct dasd_device *device;
2052 unsigned long flags;
2053
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002054 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002056 cqr->status = DASD_CQR_QUEUED;
2057 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002059 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2061}
2062
2063/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002064 * Queue a request to the tail of the device ccw_queue.
2065 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002067void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct dasd_device *device;
2070 unsigned long flags;
2071
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002074 cqr->status = DASD_CQR_QUEUED;
2075 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002077 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2079}
2080
2081/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002082 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002084void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002086 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2087 cqr->callback_data = DASD_SLEEPON_END_TAG;
2088 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2089 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
Stefan Haberland5915a872011-10-30 15:16:57 +01002091EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002093static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct dasd_device *device;
2096 int rc;
2097
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002098 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002100 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2102 return rc;
2103}
2104
2105/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002106 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2107 */
2108static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2109{
2110 struct dasd_device *device;
2111 dasd_erp_fn_t erp_fn;
2112
2113 if (cqr->status == DASD_CQR_FILLED)
2114 return 0;
2115 device = cqr->startdev;
2116 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2117 if (cqr->status == DASD_CQR_TERMINATED) {
2118 device->discipline->handle_terminated_request(cqr);
2119 return 1;
2120 }
2121 if (cqr->status == DASD_CQR_NEED_ERP) {
2122 erp_fn = device->discipline->erp_action(cqr);
2123 erp_fn(cqr);
2124 return 1;
2125 }
2126 if (cqr->status == DASD_CQR_FAILED)
2127 dasd_log_sense(cqr, &cqr->irb);
2128 if (cqr->refers) {
2129 __dasd_process_erp(device, cqr);
2130 return 1;
2131 }
2132 }
2133 return 0;
2134}
2135
2136static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2137{
2138 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2139 if (cqr->refers) /* erp is not done yet */
2140 return 1;
2141 return ((cqr->status != DASD_CQR_DONE) &&
2142 (cqr->status != DASD_CQR_FAILED));
2143 } else
2144 return (cqr->status == DASD_CQR_FILLED);
2145}
2146
2147static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2148{
2149 struct dasd_device *device;
2150 int rc;
2151 struct list_head ccw_queue;
2152 struct dasd_ccw_req *cqr;
2153
2154 INIT_LIST_HEAD(&ccw_queue);
2155 maincqr->status = DASD_CQR_FILLED;
2156 device = maincqr->startdev;
2157 list_add(&maincqr->blocklist, &ccw_queue);
2158 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2159 cqr = list_first_entry(&ccw_queue,
2160 struct dasd_ccw_req, blocklist)) {
2161
2162 if (__dasd_sleep_on_erp(cqr))
2163 continue;
2164 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2165 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002166 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2167 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2168 cqr->status = DASD_CQR_FAILED;
2169 cqr->intrc = -EPERM;
2170 continue;
2171 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002172 /* Non-temporary stop condition will trigger fail fast */
2173 if (device->stopped & ~DASD_STOPPED_PENDING &&
2174 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2175 (!dasd_eer_enabled(device))) {
2176 cqr->status = DASD_CQR_FAILED;
Stefan Haberland12d7b102012-09-11 15:10:58 +02002177 cqr->intrc = -EAGAIN;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002178 continue;
2179 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002180 /* Don't try to start requests if device is stopped */
2181 if (interruptible) {
2182 rc = wait_event_interruptible(
2183 generic_waitq, !(device->stopped));
2184 if (rc == -ERESTARTSYS) {
2185 cqr->status = DASD_CQR_FAILED;
2186 maincqr->intrc = rc;
2187 continue;
2188 }
2189 } else
2190 wait_event(generic_waitq, !(device->stopped));
2191
Stefan Haberland5915a872011-10-30 15:16:57 +01002192 if (!cqr->callback)
2193 cqr->callback = dasd_wakeup_cb;
2194
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002195 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002196 dasd_add_request_tail(cqr);
2197 if (interruptible) {
2198 rc = wait_event_interruptible(
2199 generic_waitq, _wait_for_wakeup(cqr));
2200 if (rc == -ERESTARTSYS) {
2201 dasd_cancel_req(cqr);
2202 /* wait (non-interruptible) for final status */
2203 wait_event(generic_waitq,
2204 _wait_for_wakeup(cqr));
2205 cqr->status = DASD_CQR_FAILED;
2206 maincqr->intrc = rc;
2207 continue;
2208 }
2209 } else
2210 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2211 }
2212
Heiko Carstens1aae0562013-01-30 09:49:40 +01002213 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002214 if ((maincqr->status != DASD_CQR_DONE) &&
2215 (maincqr->intrc != -ERESTARTSYS))
2216 dasd_log_sense(maincqr, &maincqr->irb);
2217 if (maincqr->status == DASD_CQR_DONE)
2218 rc = 0;
2219 else if (maincqr->intrc)
2220 rc = maincqr->intrc;
2221 else
2222 rc = -EIO;
2223 return rc;
2224}
2225
2226/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002227 * Queue a request to the tail of the device ccw_queue and wait for
2228 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002230int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002232 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002236 * Queue a request to the tail of the device ccw_queue and wait
2237 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002239int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002241 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
2244/*
2245 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2246 * for eckd devices) the currently running request has to be terminated
2247 * and be put back to status queued, before the special request is added
2248 * to the head of the queue. Then the special request is waited on normally.
2249 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002250static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002253 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 if (list_empty(&device->ccw_queue))
2256 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002257 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002258 rc = device->discipline->term_IO(cqr);
2259 if (!rc)
2260 /*
2261 * CQR terminated because a more important request is pending.
2262 * Undo decreasing of retry counter because this is
2263 * not an error case.
2264 */
2265 cqr->retries++;
2266 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002269int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 struct dasd_device *device;
2272 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002273
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002274 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002275 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2276 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2277 cqr->status = DASD_CQR_FAILED;
2278 cqr->intrc = -EPERM;
2279 return -EIO;
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 spin_lock_irq(get_ccwdev_lock(device->cdev));
2282 rc = _dasd_term_running_cqr(device);
2283 if (rc) {
2284 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2285 return rc;
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002288 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002290 /*
2291 * add new request as second
2292 * first the terminated cqr needs to be finished
2293 */
2294 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002297 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2300
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002301 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002302
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002303 if (cqr->status == DASD_CQR_DONE)
2304 rc = 0;
2305 else if (cqr->intrc)
2306 rc = cqr->intrc;
2307 else
2308 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return rc;
2310}
2311
2312/*
2313 * Cancels a request that was started with dasd_sleep_on_req.
2314 * This is useful to timeout requests. The request will be
2315 * terminated if it is currently in i/o.
2316 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002317 * 0 if there was no need to terminate the request (not started yet)
2318 * negative error code if termination failed
2319 * Cancellation of a request is an asynchronous operation! The calling
2320 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002322int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002324 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 unsigned long flags;
2326 int rc;
2327
2328 rc = 0;
2329 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2330 switch (cqr->status) {
2331 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002332 /* request was not started - just set to cleared */
2333 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 break;
2335 case DASD_CQR_IN_IO:
2336 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002337 rc = device->discipline->term_IO(cqr);
2338 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002339 dev_err(&device->cdev->dev,
2340 "Cancelling request %p failed with rc=%d\n",
2341 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002342 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002343 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002346 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002350 dasd_schedule_device_bh(device);
2351 return rc;
2352}
2353
2354
2355/*
2356 * SECTION: Operations of the dasd_block layer.
2357 */
2358
2359/*
2360 * Timeout function for dasd_block. This is used when the block layer
2361 * is waiting for something that may not come reliably, (e.g. a state
2362 * change interrupt)
2363 */
2364static void dasd_block_timeout(unsigned long ptr)
2365{
2366 unsigned long flags;
2367 struct dasd_block *block;
2368
2369 block = (struct dasd_block *) ptr;
2370 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2371 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002372 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002373 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2374 dasd_schedule_block_bh(block);
2375}
2376
2377/*
2378 * Setup timeout for a dasd_block in jiffies.
2379 */
2380void dasd_block_set_timer(struct dasd_block *block, int expires)
2381{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002382 if (expires == 0)
2383 del_timer(&block->timer);
2384 else
2385 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002386}
2387
2388/*
2389 * Clear timeout for a dasd_block.
2390 */
2391void dasd_block_clear_timer(struct dasd_block *block)
2392{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002393 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002394}
2395
2396/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002397 * Process finished error recovery ccw.
2398 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002399static void __dasd_process_erp(struct dasd_device *device,
2400 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002401{
2402 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002403
2404 if (cqr->status == DASD_CQR_DONE)
2405 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2406 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002407 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002408 erp_fn = device->discipline->erp_postaction(cqr);
2409 erp_fn(cqr);
2410}
2411
2412/*
2413 * Fetch requests from the block device queue.
2414 */
2415static void __dasd_process_request_queue(struct dasd_block *block)
2416{
2417 struct request_queue *queue;
2418 struct request *req;
2419 struct dasd_ccw_req *cqr;
2420 struct dasd_device *basedev;
2421 unsigned long flags;
2422 queue = block->request_queue;
2423 basedev = block->base;
2424 /* No queue ? Then there is nothing to do. */
2425 if (queue == NULL)
2426 return;
2427
2428 /*
2429 * We requeue request from the block device queue to the ccw
2430 * queue only in two states. In state DASD_STATE_READY the
2431 * partition detection is done and we need to requeue requests
2432 * for that. State DASD_STATE_ONLINE is normal block device
2433 * operation.
2434 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002435 if (basedev->state < DASD_STATE_READY) {
2436 while ((req = blk_fetch_request(block->request_queue)))
2437 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002438 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002439 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002440 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002441 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002442 if (basedev->features & DASD_FEATURE_READONLY &&
2443 rq_data_dir(req) == WRITE) {
2444 DBF_DEV_EVENT(DBF_ERR, basedev,
2445 "Rejecting write request %p",
2446 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002447 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002448 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002449 continue;
2450 }
2451 cqr = basedev->discipline->build_cp(basedev, block, req);
2452 if (IS_ERR(cqr)) {
2453 if (PTR_ERR(cqr) == -EBUSY)
2454 break; /* normal end condition */
2455 if (PTR_ERR(cqr) == -ENOMEM)
2456 break; /* terminate request queue loop */
2457 if (PTR_ERR(cqr) == -EAGAIN) {
2458 /*
2459 * The current request cannot be build right
2460 * now, we have to try later. If this request
2461 * is the head-of-queue we stop the device
2462 * for 1/2 second.
2463 */
2464 if (!list_empty(&block->ccw_queue))
2465 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002466 spin_lock_irqsave(
2467 get_ccwdev_lock(basedev->cdev), flags);
2468 dasd_device_set_stop_bits(basedev,
2469 DASD_STOPPED_PENDING);
2470 spin_unlock_irqrestore(
2471 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002472 dasd_block_set_timer(block, HZ/2);
2473 break;
2474 }
2475 DBF_DEV_EVENT(DBF_ERR, basedev,
2476 "CCW creation failed (rc=%ld) "
2477 "on request %p",
2478 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002479 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002480 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002481 continue;
2482 }
2483 /*
2484 * Note: callback is set to dasd_return_cqr_cb in
2485 * __dasd_block_start_head to cover erp requests as well
2486 */
2487 cqr->callback_data = (void *) req;
2488 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002489 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002490 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2491 dasd_profile_start(block, cqr, req);
2492 }
2493}
2494
2495static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2496{
2497 struct request *req;
2498 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002499 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002500
2501 req = (struct request *) cqr->callback_data;
2502 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002503 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002504 if (status <= 0)
2505 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09002506 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002507}
2508
2509/*
2510 * Process ccw request queue.
2511 */
2512static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2513 struct list_head *final_queue)
2514{
2515 struct list_head *l, *n;
2516 struct dasd_ccw_req *cqr;
2517 dasd_erp_fn_t erp_fn;
2518 unsigned long flags;
2519 struct dasd_device *base = block->base;
2520
2521restart:
2522 /* Process request with final status. */
2523 list_for_each_safe(l, n, &block->ccw_queue) {
2524 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2525 if (cqr->status != DASD_CQR_DONE &&
2526 cqr->status != DASD_CQR_FAILED &&
2527 cqr->status != DASD_CQR_NEED_ERP &&
2528 cqr->status != DASD_CQR_TERMINATED)
2529 continue;
2530
2531 if (cqr->status == DASD_CQR_TERMINATED) {
2532 base->discipline->handle_terminated_request(cqr);
2533 goto restart;
2534 }
2535
2536 /* Process requests that may be recovered */
2537 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002538 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002539 if (IS_ERR(erp_fn(cqr)))
2540 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002541 goto restart;
2542 }
2543
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002544 /* log sense for fatal error */
2545 if (cqr->status == DASD_CQR_FAILED) {
2546 dasd_log_sense(cqr, &cqr->irb);
2547 }
2548
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002549 /* First of all call extended error reporting. */
2550 if (dasd_eer_enabled(base) &&
2551 cqr->status == DASD_CQR_FAILED) {
2552 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2553
2554 /* restart request */
2555 cqr->status = DASD_CQR_FILLED;
2556 cqr->retries = 255;
2557 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002558 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002559 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2560 flags);
2561 goto restart;
2562 }
2563
2564 /* Process finished ERP request. */
2565 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002566 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002567 goto restart;
2568 }
2569
2570 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002571 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002572 list_move_tail(&cqr->blocklist, final_queue);
2573 }
2574}
2575
2576static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2577{
2578 dasd_schedule_block_bh(cqr->block);
2579}
2580
2581static void __dasd_block_start_head(struct dasd_block *block)
2582{
2583 struct dasd_ccw_req *cqr;
2584
2585 if (list_empty(&block->ccw_queue))
2586 return;
2587 /* We allways begin with the first requests on the queue, as some
2588 * of previously started requests have to be enqueued on a
2589 * dasd_device again for error recovery.
2590 */
2591 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2592 if (cqr->status != DASD_CQR_FILLED)
2593 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002594 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2595 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2596 cqr->status = DASD_CQR_FAILED;
2597 cqr->intrc = -EPERM;
2598 dasd_schedule_block_bh(block);
2599 continue;
2600 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002601 /* Non-temporary stop condition will trigger fail fast */
2602 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2603 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2604 (!dasd_eer_enabled(block->base))) {
2605 cqr->status = DASD_CQR_FAILED;
2606 dasd_schedule_block_bh(block);
2607 continue;
2608 }
2609 /* Don't try to start requests if device is stopped */
2610 if (block->base->stopped)
2611 return;
2612
2613 /* just a fail safe check, should not happen */
2614 if (!cqr->startdev)
2615 cqr->startdev = block->base;
2616
2617 /* make sure that the requests we submit find their way back */
2618 cqr->callback = dasd_return_cqr_cb;
2619
2620 dasd_add_request_tail(cqr);
2621 }
2622}
2623
2624/*
2625 * Central dasd_block layer routine. Takes requests from the generic
2626 * block layer request queue, creates ccw requests, enqueues them on
2627 * a dasd_device and processes ccw requests that have been returned.
2628 */
2629static void dasd_block_tasklet(struct dasd_block *block)
2630{
2631 struct list_head final_queue;
2632 struct list_head *l, *n;
2633 struct dasd_ccw_req *cqr;
2634
2635 atomic_set(&block->tasklet_scheduled, 0);
2636 INIT_LIST_HEAD(&final_queue);
2637 spin_lock(&block->queue_lock);
2638 /* Finish off requests on ccw queue */
2639 __dasd_process_block_ccw_queue(block, &final_queue);
2640 spin_unlock(&block->queue_lock);
2641 /* Now call the callback function of requests with final status */
2642 spin_lock_irq(&block->request_queue_lock);
2643 list_for_each_safe(l, n, &final_queue) {
2644 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2645 list_del_init(&cqr->blocklist);
2646 __dasd_cleanup_cqr(cqr);
2647 }
2648 spin_lock(&block->queue_lock);
2649 /* Get new request from the block device request queue */
2650 __dasd_process_request_queue(block);
2651 /* Now check if the head of the ccw queue needs to be started. */
2652 __dasd_block_start_head(block);
2653 spin_unlock(&block->queue_lock);
2654 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002655 if (waitqueue_active(&shutdown_waitq))
2656 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002657 dasd_put_device(block->base);
2658}
2659
2660static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2661{
2662 wake_up(&dasd_flush_wq);
2663}
2664
2665/*
2666 * Go through all request on the dasd_block request queue, cancel them
2667 * on the respective dasd_device, and return them to the generic
2668 * block layer.
2669 */
2670static int dasd_flush_block_queue(struct dasd_block *block)
2671{
2672 struct dasd_ccw_req *cqr, *n;
2673 int rc, i;
2674 struct list_head flush_queue;
2675
2676 INIT_LIST_HEAD(&flush_queue);
2677 spin_lock_bh(&block->queue_lock);
2678 rc = 0;
2679restart:
2680 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2681 /* if this request currently owned by a dasd_device cancel it */
2682 if (cqr->status >= DASD_CQR_QUEUED)
2683 rc = dasd_cancel_req(cqr);
2684 if (rc < 0)
2685 break;
2686 /* Rechain request (including erp chain) so it won't be
2687 * touched by the dasd_block_tasklet anymore.
2688 * Replace the callback so we notice when the request
2689 * is returned from the dasd_device layer.
2690 */
2691 cqr->callback = _dasd_wake_block_flush_cb;
2692 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2693 list_move_tail(&cqr->blocklist, &flush_queue);
2694 if (i > 1)
2695 /* moved more than one request - need to restart */
2696 goto restart;
2697 }
2698 spin_unlock_bh(&block->queue_lock);
2699 /* Now call the callback function of flushed requests */
2700restart_cb:
2701 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2702 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2703 /* Process finished ERP request. */
2704 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002705 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002706 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002707 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002708 /* restart list_for_xx loop since dasd_process_erp
2709 * might remove multiple elements */
2710 goto restart_cb;
2711 }
2712 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002713 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002714 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002715 list_del_init(&cqr->blocklist);
2716 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002717 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return rc;
2720}
2721
2722/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002723 * Schedules a call to dasd_tasklet over the device tasklet.
2724 */
2725void dasd_schedule_block_bh(struct dasd_block *block)
2726{
2727 /* Protect against rescheduling. */
2728 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2729 return;
2730 /* life cycle of block is bound to it's base device */
2731 dasd_get_device(block->base);
2732 tasklet_hi_schedule(&block->tasklet);
2733}
2734
2735
2736/*
2737 * SECTION: external block device operations
2738 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 */
2740
2741/*
2742 * Dasd request queue function. Called from ll_rw_blk.c
2743 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002744static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002746 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002748 block = queue->queuedata;
2749 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002751 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002753 __dasd_block_start_head(block);
2754 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
2757/*
2758 * Allocate and initialize request queue and default I/O scheduler.
2759 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002760static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 int rc;
2763
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002764 block->request_queue = blk_init_queue(do_dasd_request,
2765 &block->request_queue_lock);
2766 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return -ENOMEM;
2768
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002769 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002771 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002772 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002773 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002775 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return rc;
2777 }
2778 return 0;
2779}
2780
2781/*
2782 * Allocate and initialize request queue.
2783 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002784static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
2786 int max;
2787
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002788 if (block->base->features & DASD_FEATURE_USERAW) {
2789 /*
2790 * the max_blocks value for raw_track access is 256
2791 * it is higher than the native ECKD value because we
2792 * only need one ccw per track
2793 * so the max_hw_sectors are
2794 * 2048 x 512B = 1024kB = 16 tracks
2795 */
2796 max = 2048;
2797 } else {
2798 max = block->base->discipline->max_blocks << block->s2b_shift;
2799 }
2800 blk_queue_logical_block_size(block->request_queue,
2801 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002802 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002803 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002804 /* with page sized segments we can translate each segement into
2805 * one idaw/tidaw
2806 */
2807 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2808 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
2811/*
2812 * Deactivate and free request queue.
2813 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002814static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002816 if (block->request_queue) {
2817 blk_cleanup_queue(block->request_queue);
2818 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820}
2821
2822/*
2823 * Flush request on the request queue.
2824 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002825static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct request *req;
2828
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002829 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002831
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002832 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002833 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002834 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002835 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Al Viro57a7c0b2008-03-02 10:36:08 -05002838static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Stefan Haberland9eb25122010-02-26 22:37:46 +01002840 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 int rc;
2842
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002843 base = dasd_device_from_gendisk(bdev->bd_disk);
2844 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01002845 return -ENODEV;
2846
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002847 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002848 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 rc = -ENODEV;
2850 goto unlock;
2851 }
2852
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002853 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 rc = -EINVAL;
2855 goto unlock;
2856 }
2857
2858 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002859 dev_info(&base->cdev->dev,
2860 "Accessing the DASD failed because it is in "
2861 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 rc = -EPERM;
2863 goto out;
2864 }
2865
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002866 if (base->state <= DASD_STATE_BASIC) {
2867 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 " Cannot open unrecognized device");
2869 rc = -ENODEV;
2870 goto out;
2871 }
2872
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002873 if ((mode & FMODE_WRITE) &&
2874 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2875 (base->features & DASD_FEATURE_READONLY))) {
2876 rc = -EROFS;
2877 goto out;
2878 }
2879
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002880 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return 0;
2882
2883out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002884 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002886 atomic_dec(&base->block->open_count);
2887 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return rc;
2889}
2890
Al Viro57a7c0b2008-03-02 10:36:08 -05002891static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002893 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002895 base = dasd_device_from_gendisk(disk);
2896 if (!base)
2897 return -ENODEV;
2898
2899 atomic_dec(&base->block->open_count);
2900 module_put(base->discipline->owner);
2901 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return 0;
2903}
2904
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002905/*
2906 * Return disk geometry.
2907 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002908static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002909{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002910 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002911
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002912 base = dasd_device_from_gendisk(bdev->bd_disk);
2913 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002914 return -ENODEV;
2915
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002916 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002917 !base->discipline->fill_geometry) {
2918 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002919 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002920 }
2921 base->discipline->fill_geometry(base->block, geo);
2922 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
2923 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002924 return 0;
2925}
2926
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002927const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928dasd_device_operations = {
2929 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002930 .open = dasd_open,
2931 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002932 .ioctl = dasd_ioctl,
2933 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002934 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935};
2936
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002937/*******************************************************************************
2938 * end of block device operations
2939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941static void
2942dasd_exit(void)
2943{
2944#ifdef CONFIG_PROC_FS
2945 dasd_proc_exit();
2946#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002947 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002948 if (dasd_page_cache != NULL) {
2949 kmem_cache_destroy(dasd_page_cache);
2950 dasd_page_cache = NULL;
2951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 dasd_gendisk_exit();
2953 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (dasd_debug_area != NULL) {
2955 debug_unregister(dasd_debug_area);
2956 dasd_debug_area = NULL;
2957 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02002958 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
2961/*
2962 * SECTION: common functions for ccw_driver use
2963 */
2964
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002965/*
2966 * Is the device read-only?
2967 * Note that this function does not report the setting of the
2968 * readonly device attribute, but how it is configured in z/VM.
2969 */
2970int dasd_device_is_ro(struct dasd_device *device)
2971{
2972 struct ccw_dev_id dev_id;
2973 struct diag210 diag_data;
2974 int rc;
2975
2976 if (!MACHINE_IS_VM)
2977 return 0;
2978 ccw_device_get_id(device->cdev, &dev_id);
2979 memset(&diag_data, 0, sizeof(diag_data));
2980 diag_data.vrdcdvno = dev_id.devno;
2981 diag_data.vrdclen = sizeof(diag_data);
2982 rc = diag210(&diag_data);
2983 if (rc == 0 || rc == 2) {
2984 return diag_data.vrdcvfla & 0x80;
2985 } else {
2986 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2987 dev_id.devno, rc);
2988 return 0;
2989 }
2990}
2991EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2992
Cornelia Huckf3445a12009-04-14 15:36:23 +02002993static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2994{
2995 struct ccw_device *cdev = data;
2996 int ret;
2997
2998 ret = ccw_device_set_online(cdev);
2999 if (ret)
3000 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3001 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003002}
3003
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003004/*
3005 * Initial attempt at a probe function. this can be simplified once
3006 * the other detection code is gone.
3007 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003008int dasd_generic_probe(struct ccw_device *cdev,
3009 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 int ret;
3012
3013 ret = dasd_add_sysfs_files(cdev);
3014 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003015 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3016 "dasd_generic_probe: could not add "
3017 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Horst Hummel40545572006-06-29 15:08:18 +02003020 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Horst Hummel40545572006-06-29 15:08:18 +02003022 /*
3023 * Automatically online either all dasd devices (dasd_autodetect)
3024 * or all devices specified with dasd= parameters during
3025 * initial probe.
3026 */
3027 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003028 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003029 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003033/*
3034 * This will one day be called from a global not_oper handler.
3035 * It is also used by driver_unregister during module unload.
3036 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003037void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003040 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Horst Hummel59afda72005-05-16 21:53:39 -07003042 cdev->handler = NULL;
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003045 if (IS_ERR(device)) {
3046 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003048 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003049 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3050 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /* Already doing offline processing */
3052 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003053 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return;
3055 }
3056 /*
3057 * This device is removed unconditionally. Set offline
3058 * flag to prevent dasd_open from opening it while it is
3059 * no quite down yet.
3060 */
3061 dasd_set_target_state(device, DASD_STATE_NEW);
3062 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003063 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003065 /*
3066 * life cycle of block is bound to device, so delete it after
3067 * device was safely removed
3068 */
3069 if (block)
3070 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003071
3072 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003075/*
3076 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003078 * or the user has started activation through sysfs.
3079 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003080int dasd_generic_set_online(struct ccw_device *cdev,
3081 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003083 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003085 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003086
Horst Hummel40545572006-06-29 15:08:18 +02003087 /* first online clears initial online feature flag */
3088 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 device = dasd_create_device(cdev);
3090 if (IS_ERR(device))
3091 return PTR_ERR(device);
3092
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003093 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003094 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003096 pr_warning("%s Setting the DASD online failed because "
3097 "of missing DIAG discipline\n",
3098 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 dasd_delete_device(device);
3100 return -ENODEV;
3101 }
3102 discipline = dasd_diag_discipline_pointer;
3103 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003104 if (!try_module_get(base_discipline->owner)) {
3105 dasd_delete_device(device);
3106 return -EINVAL;
3107 }
3108 if (!try_module_get(discipline->owner)) {
3109 module_put(base_discipline->owner);
3110 dasd_delete_device(device);
3111 return -EINVAL;
3112 }
3113 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 device->discipline = discipline;
3115
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003116 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 rc = discipline->check_device(device);
3118 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003119 pr_warning("%s Setting the DASD online with discipline %s "
3120 "failed with rc=%i\n",
3121 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003122 module_put(discipline->owner);
3123 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 dasd_delete_device(device);
3125 return rc;
3126 }
3127
3128 dasd_set_target_state(device, DASD_STATE_ONLINE);
3129 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003130 pr_warning("%s Setting the DASD online failed because of a "
3131 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 rc = -ENODEV;
3133 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003134 if (device->block)
3135 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 dasd_delete_device(device);
3137 } else
3138 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003139 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003140
3141 wait_event(dasd_init_waitq, _wait_for_device(device));
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return rc;
3145}
3146
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003147int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
3149 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003150 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003151 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003153 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 device = dasd_device_from_cdev(cdev);
3155 if (IS_ERR(device))
3156 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 /*
3159 * We must make sure that this device is currently not in use.
3160 * The open_count is increased for every opener, that includes
3161 * the blkdev_get in dasd_scan_partitions. We are only interested
3162 * in the other openers.
3163 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003164 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003165 max_count = device->block->bdev ? 0 : -1;
3166 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003167 if (open_count > max_count) {
3168 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003169 pr_warning("%s: The DASD cannot be set offline "
3170 "with open count %i\n",
3171 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003172 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003173 pr_warning("%s: The DASD cannot be set offline "
3174 "while it is in use\n",
3175 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003176 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3177 dasd_put_device(device);
3178 return -EBUSY;
3179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003181
3182 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3183 /*
3184 * safe offline allready running
3185 * could only be called by normal offline so safe_offline flag
3186 * needs to be removed to run normal offline and kill all I/O
3187 */
3188 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3189 /* Already doing normal offline processing */
3190 dasd_put_device(device);
3191 return -EBUSY;
3192 } else
3193 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3194
3195 } else
3196 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3197 /* Already doing offline processing */
3198 dasd_put_device(device);
3199 return -EBUSY;
3200 }
3201
3202 /*
3203 * if safe_offline called set safe_offline_running flag and
3204 * clear safe_offline so that a call to normal offline
3205 * can overrun safe_offline processing
3206 */
3207 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3208 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3209 /*
3210 * If we want to set the device safe offline all IO operations
3211 * should be finished before continuing the offline process
3212 * so sync bdev first and then wait for our queues to become
3213 * empty
3214 */
3215 /* sync blockdev and partitions */
3216 rc = fsync_bdev(device->block->bdev);
3217 if (rc != 0)
3218 goto interrupted;
3219
3220 /* schedule device tasklet and wait for completion */
3221 dasd_schedule_device_bh(device);
3222 rc = wait_event_interruptible(shutdown_waitq,
3223 _wait_for_empty_queues(device));
3224 if (rc != 0)
3225 goto interrupted;
3226 }
3227
3228 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 dasd_set_target_state(device, DASD_STATE_NEW);
3230 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003231 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003233 /*
3234 * life cycle of block is bound to device, so delete it after
3235 * device was safely removed
3236 */
3237 if (block)
3238 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003240
3241interrupted:
3242 /* interrupted by signal */
3243 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3244 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3245 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3246 dasd_put_device(device);
3247 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003250int dasd_generic_last_path_gone(struct dasd_device *device)
3251{
3252 struct dasd_ccw_req *cqr;
3253
3254 dev_warn(&device->cdev->dev, "No operational channel path is left "
3255 "for the device\n");
3256 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3257 /* First of all call extended error reporting. */
3258 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3259
3260 if (device->state < DASD_STATE_BASIC)
3261 return 0;
3262 /* Device is active. We want to keep it. */
3263 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3264 if ((cqr->status == DASD_CQR_IN_IO) ||
3265 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3266 cqr->status = DASD_CQR_QUEUED;
3267 cqr->retries++;
3268 }
3269 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3270 dasd_device_clear_timer(device);
3271 dasd_schedule_device_bh(device);
3272 return 1;
3273}
3274EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3275
3276int dasd_generic_path_operational(struct dasd_device *device)
3277{
3278 dev_info(&device->cdev->dev, "A channel path to the device has become "
3279 "operational\n");
3280 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3281 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3282 if (device->stopped & DASD_UNRESUMED_PM) {
3283 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3284 dasd_restore_device(device);
3285 return 1;
3286 }
3287 dasd_schedule_device_bh(device);
3288 if (device->block)
3289 dasd_schedule_block_bh(device->block);
3290 return 1;
3291}
3292EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3293
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003294int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
3296 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 int ret;
3298
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003299 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (IS_ERR(device))
3301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 ret = 0;
3303 switch (event) {
3304 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003305 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003307 device->path_data.opm = 0;
3308 device->path_data.ppm = 0;
3309 device->path_data.npm = 0;
3310 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 break;
3312 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003314 if (device->path_data.opm)
3315 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 break;
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 dasd_put_device(device);
3319 return ret;
3320}
3321
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003322void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3323{
3324 int chp;
3325 __u8 oldopm, eventlpm;
3326 struct dasd_device *device;
3327
3328 device = dasd_device_from_cdev_locked(cdev);
3329 if (IS_ERR(device))
3330 return;
3331 for (chp = 0; chp < 8; chp++) {
3332 eventlpm = 0x80 >> chp;
3333 if (path_event[chp] & PE_PATH_GONE) {
3334 oldopm = device->path_data.opm;
3335 device->path_data.opm &= ~eventlpm;
3336 device->path_data.ppm &= ~eventlpm;
3337 device->path_data.npm &= ~eventlpm;
3338 if (oldopm && !device->path_data.opm)
3339 dasd_generic_last_path_gone(device);
3340 }
3341 if (path_event[chp] & PE_PATH_AVAILABLE) {
3342 device->path_data.opm &= ~eventlpm;
3343 device->path_data.ppm &= ~eventlpm;
3344 device->path_data.npm &= ~eventlpm;
3345 device->path_data.tbvpm |= eventlpm;
3346 dasd_schedule_device_bh(device);
3347 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003348 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003349 if (!(device->path_data.opm & eventlpm) &&
3350 !(device->path_data.tbvpm & eventlpm)) {
3351 /*
3352 * we can not establish a pathgroup on an
3353 * unavailable path, so trigger a path
3354 * verification first
3355 */
3356 device->path_data.tbvpm |= eventlpm;
3357 dasd_schedule_device_bh(device);
3358 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003359 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3360 "Pathgroup re-established\n");
3361 if (device->discipline->kick_validate)
3362 device->discipline->kick_validate(device);
3363 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003364 }
3365 dasd_put_device(device);
3366}
3367EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3368
3369int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3370{
3371 if (!device->path_data.opm && lpm) {
3372 device->path_data.opm = lpm;
3373 dasd_generic_path_operational(device);
3374 } else
3375 device->path_data.opm |= lpm;
3376 return 0;
3377}
3378EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3379
3380
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003381int dasd_generic_pm_freeze(struct ccw_device *cdev)
3382{
3383 struct dasd_ccw_req *cqr, *n;
3384 int rc;
3385 struct list_head freeze_queue;
3386 struct dasd_device *device = dasd_device_from_cdev(cdev);
3387
3388 if (IS_ERR(device))
3389 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003390
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003391 /* mark device as suspended */
3392 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3393
Stefan Haberland6f272b92011-01-05 12:48:05 +01003394 if (device->discipline->freeze)
3395 rc = device->discipline->freeze(device);
3396
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003397 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003398 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003399 /* clear active requests */
3400 INIT_LIST_HEAD(&freeze_queue);
3401 spin_lock_irq(get_ccwdev_lock(cdev));
3402 rc = 0;
3403 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3404 /* Check status and move request to flush_queue */
3405 if (cqr->status == DASD_CQR_IN_IO) {
3406 rc = device->discipline->term_IO(cqr);
3407 if (rc) {
3408 /* unable to terminate requeust */
3409 dev_err(&device->cdev->dev,
3410 "Unable to terminate request %p "
3411 "on suspend\n", cqr);
3412 spin_unlock_irq(get_ccwdev_lock(cdev));
3413 dasd_put_device(device);
3414 return rc;
3415 }
3416 }
3417 list_move_tail(&cqr->devlist, &freeze_queue);
3418 }
3419
3420 spin_unlock_irq(get_ccwdev_lock(cdev));
3421
3422 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3423 wait_event(dasd_flush_wq,
3424 (cqr->status != DASD_CQR_CLEAR_PENDING));
3425 if (cqr->status == DASD_CQR_CLEARED)
3426 cqr->status = DASD_CQR_QUEUED;
3427 }
3428 /* move freeze_queue to start of the ccw_queue */
3429 spin_lock_irq(get_ccwdev_lock(cdev));
3430 list_splice_tail(&freeze_queue, &device->ccw_queue);
3431 spin_unlock_irq(get_ccwdev_lock(cdev));
3432
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003433 dasd_put_device(device);
3434 return rc;
3435}
3436EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3437
3438int dasd_generic_restore_device(struct ccw_device *cdev)
3439{
3440 struct dasd_device *device = dasd_device_from_cdev(cdev);
3441 int rc = 0;
3442
3443 if (IS_ERR(device))
3444 return PTR_ERR(device);
3445
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003446 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003447 dasd_device_remove_stop_bits(device,
3448 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003449
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003450 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003451
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003452 /*
3453 * call discipline restore function
3454 * if device is stopped do nothing e.g. for disconnected devices
3455 */
3456 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003457 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003458 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003459 /*
3460 * if the resume failed for the DASD we put it in
3461 * an UNRESUMED stop state
3462 */
3463 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003464
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003465 if (device->block)
3466 dasd_schedule_block_bh(device->block);
3467
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003468 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003469 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003470 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003471}
3472EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3473
Heiko Carstens763968e2007-05-10 15:45:46 +02003474static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3475 void *rdc_buffer,
3476 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003477 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003478{
3479 struct dasd_ccw_req *cqr;
3480 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003481 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003482
3483 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3484
3485 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003486 /* internal error 13 - Allocating the RDC request failed*/
3487 dev_err(&device->cdev->dev,
3488 "An error occurred in the DASD device driver, "
3489 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003490 return cqr;
3491 }
3492
3493 ccw = cqr->cpaddr;
3494 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003495 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3496 idaw = (unsigned long *) (cqr->data);
3497 ccw->cda = (__u32)(addr_t) idaw;
3498 ccw->flags = CCW_FLAG_IDA;
3499 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3500 } else {
3501 ccw->cda = (__u32)(addr_t) rdc_buffer;
3502 ccw->flags = 0;
3503 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003504
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003505 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003506 cqr->startdev = device;
3507 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003508 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003509 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003510 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003511 cqr->status = DASD_CQR_FILLED;
3512 return cqr;
3513}
3514
3515
Stefan Haberland68b781f2009-09-11 10:28:29 +02003516int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003517 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003518{
3519 int ret;
3520 struct dasd_ccw_req *cqr;
3521
Sebastian Ott92636b12009-06-12 10:26:37 +02003522 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003523 magic);
3524 if (IS_ERR(cqr))
3525 return PTR_ERR(cqr);
3526
3527 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003528 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003529 return ret;
3530}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003531EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003532
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003533/*
3534 * In command mode and transport mode we need to look for sense
3535 * data in different places. The sense data itself is allways
3536 * an array of 32 bytes, so we can unify the sense data access
3537 * for both modes.
3538 */
3539char *dasd_get_sense(struct irb *irb)
3540{
3541 struct tsb *tsb = NULL;
3542 char *sense = NULL;
3543
3544 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3545 if (irb->scsw.tm.tcw)
3546 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3547 irb->scsw.tm.tcw);
3548 if (tsb && tsb->length == 64 && tsb->flags)
3549 switch (tsb->flags & 0x07) {
3550 case 1: /* tsa_iostat */
3551 sense = tsb->tsa.iostat.sense;
3552 break;
3553 case 2: /* tsa_ddpc */
3554 sense = tsb->tsa.ddpc.sense;
3555 break;
3556 default:
3557 /* currently we don't use interrogate data */
3558 break;
3559 }
3560 } else if (irb->esw.esw0.erw.cons) {
3561 sense = irb->ecw;
3562 }
3563 return sense;
3564}
3565EXPORT_SYMBOL_GPL(dasd_get_sense);
3566
Stefan Haberland4679e892012-06-19 17:30:12 +02003567void dasd_generic_shutdown(struct ccw_device *cdev)
3568{
3569 struct dasd_device *device;
3570
3571 device = dasd_device_from_cdev(cdev);
3572 if (IS_ERR(device))
3573 return;
3574
3575 if (device->block)
3576 dasd_schedule_block_bh(device->block);
3577
3578 dasd_schedule_device_bh(device);
3579
3580 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3581}
3582EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3583
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003584static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 int rc;
3587
3588 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003589 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003590 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003591 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
3593 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003594 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (dasd_debug_area == NULL) {
3596 rc = -ENOMEM;
3597 goto failed;
3598 }
3599 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003600 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3603
3604 dasd_diag_discipline_pointer = NULL;
3605
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003606 dasd_statistics_createroot();
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 rc = dasd_devmap_init();
3609 if (rc)
3610 goto failed;
3611 rc = dasd_gendisk_init();
3612 if (rc)
3613 goto failed;
3614 rc = dasd_parse();
3615 if (rc)
3616 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003617 rc = dasd_eer_init();
3618 if (rc)
3619 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620#ifdef CONFIG_PROC_FS
3621 rc = dasd_proc_init();
3622 if (rc)
3623 goto failed;
3624#endif
3625
3626 return 0;
3627failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003628 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 dasd_exit();
3630 return rc;
3631}
3632
3633module_init(dasd_init);
3634module_exit(dasd_exit);
3635
3636EXPORT_SYMBOL(dasd_debug_area);
3637EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3638
3639EXPORT_SYMBOL(dasd_add_request_head);
3640EXPORT_SYMBOL(dasd_add_request_tail);
3641EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003642EXPORT_SYMBOL(dasd_device_clear_timer);
3643EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644EXPORT_SYMBOL(dasd_enable_device);
3645EXPORT_SYMBOL(dasd_int_handler);
3646EXPORT_SYMBOL(dasd_kfree_request);
3647EXPORT_SYMBOL(dasd_kick_device);
3648EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003649EXPORT_SYMBOL(dasd_schedule_device_bh);
3650EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003652EXPORT_SYMBOL(dasd_device_set_timer);
3653EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654EXPORT_SYMBOL(dasd_sfree_request);
3655EXPORT_SYMBOL(dasd_sleep_on);
3656EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3657EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3658EXPORT_SYMBOL(dasd_smalloc_request);
3659EXPORT_SYMBOL(dasd_start_IO);
3660EXPORT_SYMBOL(dasd_term_IO);
3661
3662EXPORT_SYMBOL_GPL(dasd_generic_probe);
3663EXPORT_SYMBOL_GPL(dasd_generic_remove);
3664EXPORT_SYMBOL_GPL(dasd_generic_notify);
3665EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3666EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003667EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3668EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3669EXPORT_SYMBOL_GPL(dasd_alloc_block);
3670EXPORT_SYMBOL_GPL(dasd_free_block);