blob: ce2a780a9ea83f69312e08ac890f7f83752340fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File...........: linux/drivers/s390/block/dasd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02008 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011#define KMSG_COMPONENT "dasd"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kmod.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/ctype.h>
18#include <linux/major.h>
19#include <linux/slab.h>
20#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080021#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020022#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010023#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020024#include <linux/debugfs.h>
25#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020026#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/ccwdev.h>
29#include <asm/ebcdic.h>
30#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010031#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010032#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* This is ugly... */
35#define PRINTK_HEADER "dasd:"
36
37#include "dasd_int.h"
38/*
39 * SECTION: Constant definitions to be used within this file
40 */
41#define DASD_CHANQ_MAX_SIZE 4
42
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +020043#define DASD_SLEEPON_START_TAG (void *) 1
44#define DASD_SLEEPON_END_TAG (void *) 2
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * SECTION: exported variables of dasd.c
48 */
49debug_info_t *dasd_debug_area;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020050static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010052void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
55MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
56 " Copyright 2000 IBM Corporation");
57MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_LICENSE("GPL");
59
60/*
61 * SECTION: prototypes for static functions of dasd.c
62 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010063static int dasd_alloc_queue(struct dasd_block *);
64static void dasd_setup_queue(struct dasd_block *);
65static void dasd_free_queue(struct dasd_block *);
66static void dasd_flush_request_queue(struct dasd_block *);
67static int dasd_flush_block_queue(struct dasd_block *);
68static void dasd_device_tasklet(struct dasd_device *);
69static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000070static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020071static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020072static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010074static void dasd_device_timeout(unsigned long);
75static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010076static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020077static void dasd_profile_init(struct dasd_profile *, struct dentry *);
78static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * SECTION: Operations on the device structure.
82 */
83static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020084static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020085static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Allocate memory for a new device structure.
89 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010090struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct dasd_device *device;
93
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010094 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
95 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Get two pages for normal block device operations. */
99 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100100 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 kfree(device);
102 return ERR_PTR(-ENOMEM);
103 }
104 /* Get one page for error recovery. */
105 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100106 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 free_pages((unsigned long) device->ccw_mem, 1);
108 kfree(device);
109 return ERR_PTR(-ENOMEM);
110 }
111
112 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
113 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
114 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100115 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200116 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100117 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 (unsigned long) device);
119 INIT_LIST_HEAD(&device->ccw_queue);
120 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100121 device->timer.function = dasd_device_timeout;
122 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000123 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200124 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200125 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 device->state = DASD_STATE_NEW;
127 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100128 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200129 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return device;
131}
132
133/*
134 * Free memory of a device structure.
135 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100136void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800138 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 free_page((unsigned long) device->erp_mem);
140 free_pages((unsigned long) device->ccw_mem, 1);
141 kfree(device);
142}
143
144/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100145 * Allocate memory for a new device structure.
146 */
147struct dasd_block *dasd_alloc_block(void)
148{
149 struct dasd_block *block;
150
151 block = kzalloc(sizeof(*block), GFP_ATOMIC);
152 if (!block)
153 return ERR_PTR(-ENOMEM);
154 /* open_count = 0 means device online but not in use */
155 atomic_set(&block->open_count, -1);
156
157 spin_lock_init(&block->request_queue_lock);
158 atomic_set(&block->tasklet_scheduled, 0);
159 tasklet_init(&block->tasklet,
160 (void (*)(unsigned long)) dasd_block_tasklet,
161 (unsigned long) block);
162 INIT_LIST_HEAD(&block->ccw_queue);
163 spin_lock_init(&block->queue_lock);
164 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100165 block->timer.function = dasd_block_timeout;
166 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200167 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100168
169 return block;
170}
171
172/*
173 * Free memory of a device structure.
174 */
175void dasd_free_block(struct dasd_block *block)
176{
177 kfree(block);
178}
179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Make a new device known to the system.
182 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100183static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int rc;
186
187 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200188 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * keep the reference count > 0.
190 */
191 dasd_get_device(device);
192
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100193 if (device->block) {
194 rc = dasd_alloc_queue(device->block);
195 if (rc) {
196 dasd_put_device(device);
197 return rc;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 device->state = DASD_STATE_KNOWN;
201 return 0;
202}
203
204/*
205 * Let the system forget about a device.
206 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100207static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800209 /* Disable extended error reporting for this device. */
210 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 if (device->discipline) {
213 if (device->discipline->uncheck_device)
214 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800215 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800218 if (device->base_discipline)
219 module_put(device->base_discipline->owner);
220 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 device->state = DASD_STATE_NEW;
222
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100223 if (device->block)
224 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Give up reference we took in dasd_state_new_to_known. */
227 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200231static struct dentry *dasd_debugfs_setup(const char *name,
232 struct dentry *base_dentry)
233{
234 struct dentry *pde;
235
236 if (!base_dentry)
237 return NULL;
238 pde = debugfs_create_dir(name, base_dentry);
239 if (!pde || IS_ERR(pde))
240 return NULL;
241 return pde;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * Request the irq line for the device.
246 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100247static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200249 struct dasd_block *block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int rc;
251
252 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 if (block) {
254 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100255 if (rc)
256 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200257 block->debugfs_dentry =
258 dasd_debugfs_setup(block->gdp->disk_name,
259 dasd_debugfs_root_entry);
260 dasd_profile_init(&block->profile, block->debugfs_dentry);
261 if (dasd_global_profile_level == DASD_PROFILE_ON)
262 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100263 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200264 device->debugfs_dentry =
265 dasd_debugfs_setup(dev_name(&device->cdev->dev),
266 dasd_debugfs_root_entry);
267 dasd_profile_init(&device->profile, device->debugfs_dentry);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100270 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100271 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200273 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
275
276 device->state = DASD_STATE_BASIC;
277 return 0;
278}
279
280/*
281 * Release the irq line for the device. Terminate any running i/o.
282 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100283static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Horst Hummel8f617012006-08-30 14:33:33 +0200285 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100286 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200287 dasd_profile_exit(&device->block->profile);
288 if (device->block->debugfs_dentry)
289 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100290 dasd_gendisk_free(device->block);
291 dasd_block_clear_timer(device->block);
292 }
293 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200294 if (rc)
295 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100296 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200297 dasd_profile_exit(&device->profile);
298 if (device->debugfs_dentry)
299 debugfs_remove(device->debugfs_dentry);
Horst Hummel8f617012006-08-30 14:33:33 +0200300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
302 if (device->debug_area != NULL) {
303 debug_unregister(device->debug_area);
304 device->debug_area = NULL;
305 }
306 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/*
311 * Do the initial analysis. The do_analysis function may return
312 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
313 * until the discipline decides to continue the startup sequence
314 * by calling the function dasd_change_state. The eckd disciplines
315 * uses this to start a ccw that detects the format. The completion
316 * interrupt for this detection ccw uses the kernel event daemon to
317 * trigger the call to dasd_change_state. All this is done in the
318 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800319 * After the analysis ccw is done (do_analysis returned 0) the block
320 * device is setup.
321 * In case the analysis returns an error, the device setup is stopped
322 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100324static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100330 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800331 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 if (block) {
333 if (block->base->discipline->do_analysis != NULL)
334 rc = block->base->discipline->do_analysis(block);
335 if (rc) {
336 if (rc != -EAGAIN)
337 device->state = DASD_STATE_UNFMT;
338 return rc;
339 }
340 dasd_setup_queue(block);
341 set_capacity(block->gdp,
342 block->blocks << block->s2b_shift);
343 device->state = DASD_STATE_READY;
344 rc = dasd_scan_partitions(block);
345 if (rc)
346 device->state = DASD_STATE_BASIC;
347 } else {
348 device->state = DASD_STATE_READY;
349 }
Horst Hummel90f00942006-03-07 21:55:39 -0800350 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
354 * Remove device from block device layer. Destroy dirty buffers.
355 * Forget format information. Check if the target level is basic
356 * and if it is create fake disk for formatting.
357 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100358static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Horst Hummel8f617012006-08-30 14:33:33 +0200360 int rc;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100363 if (device->block) {
364 struct dasd_block *block = device->block;
365 rc = dasd_flush_block_queue(block);
366 if (rc) {
367 device->state = DASD_STATE_READY;
368 return rc;
369 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100370 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100371 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100372 block->blocks = 0;
373 block->bp_block = 0;
374 block->s2b_shift = 0;
375 }
Horst Hummel8f617012006-08-30 14:33:33 +0200376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/*
Horst Hummel90f00942006-03-07 21:55:39 -0800380 * Back to basic.
381 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100382static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800383{
384 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200385 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800386}
387
388/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * Make the device online and schedule the bottom half to start
390 * the requeueing of requests from the linux request queue to the
391 * ccw queue.
392 */
Horst Hummel8f617012006-08-30 14:33:33 +0200393static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394dasd_state_ready_to_online(struct dasd_device * device)
395{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100396 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100397 struct gendisk *disk;
398 struct disk_part_iter piter;
399 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100400
401 if (device->discipline->ready_to_online) {
402 rc = device->discipline->ready_to_online(device);
403 if (rc)
404 return rc;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100407 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100408 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100409 if ((device->features & DASD_FEATURE_USERAW)) {
410 disk = device->block->gdp;
411 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
412 return 0;
413 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100414 disk = device->block->bdev->bd_disk;
415 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
416 while ((part = disk_part_iter_next(&piter)))
417 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
418 disk_part_iter_exit(&piter);
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421}
422
423/*
424 * Stop the requeueing of requests again.
425 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100426static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100428 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100429 struct gendisk *disk;
430 struct disk_part_iter piter;
431 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100432
433 if (device->discipline->online_to_ready) {
434 rc = device->discipline->online_to_ready(device);
435 if (rc)
436 return rc;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100439 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100440 disk = device->block->bdev->bd_disk;
441 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
442 while ((part = disk_part_iter_next(&piter)))
443 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
444 disk_part_iter_exit(&piter);
445 }
Horst Hummel8f617012006-08-30 14:33:33 +0200446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * Device startup state changes.
451 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100452static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 int rc;
455
456 rc = 0;
457 if (device->state == DASD_STATE_NEW &&
458 device->target >= DASD_STATE_KNOWN)
459 rc = dasd_state_new_to_known(device);
460
461 if (!rc &&
462 device->state == DASD_STATE_KNOWN &&
463 device->target >= DASD_STATE_BASIC)
464 rc = dasd_state_known_to_basic(device);
465
466 if (!rc &&
467 device->state == DASD_STATE_BASIC &&
468 device->target >= DASD_STATE_READY)
469 rc = dasd_state_basic_to_ready(device);
470
471 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700472 device->state == DASD_STATE_UNFMT &&
473 device->target > DASD_STATE_UNFMT)
474 rc = -EPERM;
475
476 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 device->state == DASD_STATE_READY &&
478 device->target >= DASD_STATE_ONLINE)
479 rc = dasd_state_ready_to_online(device);
480
481 return rc;
482}
483
484/*
485 * Device shutdown state changes.
486 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100487static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Horst Hummel8f617012006-08-30 14:33:33 +0200489 int rc;
490
491 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (device->state == DASD_STATE_ONLINE &&
493 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200494 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200495
Horst Hummel8f617012006-08-30 14:33:33 +0200496 if (!rc &&
497 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200499 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800500
Horst Hummel8f617012006-08-30 14:33:33 +0200501 if (!rc &&
502 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800503 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200504 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800505
Horst Hummel8f617012006-08-30 14:33:33 +0200506 if (!rc &&
507 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200509 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200510
Horst Hummel8f617012006-08-30 14:33:33 +0200511 if (!rc &&
512 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200514 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Horst Hummel8f617012006-08-30 14:33:33 +0200516 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/*
520 * This is the main startup/shutdown routine.
521 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100522static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Sebastian Ott181d9522009-06-22 12:08:21 +0200524 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (device->state == device->target)
527 /* Already where we want to go today... */
528 return;
529 if (device->state < device->target)
530 rc = dasd_increase_state(device);
531 else
532 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200533 if (rc == -EAGAIN)
534 return;
535 if (rc)
536 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Stefan Haberland9eb25122010-02-26 22:37:46 +0100538 if (device->state == device->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200540
541 /* let user-space know that the device status changed */
542 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/*
546 * Kick starter for devices that did not complete the startup/shutdown
547 * procedure or were sleeping because of a pending state.
548 * dasd_kick_device will schedule a call do do_kick_device to the kernel
549 * event daemon.
550 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100551static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Al Viro4927b3f2006-12-06 19:18:20 +0000553 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100554 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100556 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100557 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 dasd_put_device(device);
559}
560
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100561void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 dasd_get_device(device);
564 /* queue call to dasd_kick_device to the kernel event daemon. */
565 schedule_work(&device->kick_work);
566}
567
568/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200569 * dasd_reload_device will schedule a call do do_reload_device to the kernel
570 * event daemon.
571 */
572static void do_reload_device(struct work_struct *work)
573{
574 struct dasd_device *device = container_of(work, struct dasd_device,
575 reload_device);
576 device->discipline->reload(device);
577 dasd_put_device(device);
578}
579
580void dasd_reload_device(struct dasd_device *device)
581{
582 dasd_get_device(device);
583 /* queue call to dasd_reload_device to the kernel event daemon. */
584 schedule_work(&device->reload_device);
585}
586EXPORT_SYMBOL(dasd_reload_device);
587
588/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200589 * dasd_restore_device will schedule a call do do_restore_device to the kernel
590 * event daemon.
591 */
592static void do_restore_device(struct work_struct *work)
593{
594 struct dasd_device *device = container_of(work, struct dasd_device,
595 restore_device);
596 device->cdev->drv->restore(device->cdev);
597 dasd_put_device(device);
598}
599
600void dasd_restore_device(struct dasd_device *device)
601{
602 dasd_get_device(device);
603 /* queue call to dasd_restore_device to the kernel event daemon. */
604 schedule_work(&device->restore_device);
605}
606
607/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Set the target state for a device and starts the state change.
609 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100610void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200612 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100613 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* If we are in probeonly mode stop at DASD_STATE_READY. */
615 if (dasd_probeonly && target > DASD_STATE_READY)
616 target = DASD_STATE_READY;
617 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100618 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 wake_up(&dasd_init_waitq);
620 device->target = target;
621 }
622 if (device->state != device->target)
623 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100624 mutex_unlock(&device->state_mutex);
625 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
629 * Enable devices with device numbers in [from..to].
630 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100631static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 return (device->state == device->target);
634}
635
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100636void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 dasd_set_target_state(device, DASD_STATE_ONLINE);
639 if (device->state <= DASD_STATE_KNOWN)
640 /* No discipline for device found. */
641 dasd_set_target_state(device, DASD_STATE_NEW);
642 /* Now wait for the devices to come up. */
643 wait_event(dasd_init_waitq, _wait_for_device(device));
644}
645
646/*
647 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
648 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200649
650unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652#ifdef CONFIG_DASD_PROFILE
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200653struct dasd_profile_info dasd_global_profile_data;
654static struct dentry *dasd_global_profile_dentry;
655static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657/*
658 * Add profiling information for cqr before execution.
659 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100660static void dasd_profile_start(struct dasd_block *block,
661 struct dasd_ccw_req *cqr,
662 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct list_head *l;
665 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200666 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* count the length of the chanq for statistics */
669 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200670 if (dasd_global_profile_level || block->profile.data)
671 list_for_each(l, &block->ccw_queue)
672 if (++counter >= 31)
673 break;
674
675 if (dasd_global_profile_level) {
676 dasd_global_profile_data.dasd_io_nr_req[counter]++;
677 if (rq_data_dir(req) == READ)
678 dasd_global_profile_data.dasd_read_nr_req[counter]++;
679 }
680
681 spin_lock(&block->profile.lock);
682 if (block->profile.data)
683 block->profile.data->dasd_io_nr_req[counter]++;
684 if (rq_data_dir(req) == READ)
685 block->profile.data->dasd_read_nr_req[counter]++;
686 spin_unlock(&block->profile.lock);
687
688 /*
689 * We count the request for the start device, even though it may run on
690 * some other device due to error recovery. This way we make sure that
691 * we count each request only once.
692 */
693 device = cqr->startdev;
694 if (device->profile.data) {
695 counter = 1; /* request is not yet queued on the start device */
696 list_for_each(l, &device->ccw_queue)
697 if (++counter >= 31)
698 break;
699 }
700 spin_lock(&device->profile.lock);
701 if (device->profile.data) {
702 device->profile.data->dasd_io_nr_req[counter]++;
703 if (rq_data_dir(req) == READ)
704 device->profile.data->dasd_read_nr_req[counter]++;
705 }
706 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
710 * Add profiling information for cqr after execution.
711 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200712
713#define dasd_profile_counter(value, index) \
714{ \
715 for (index = 0; index < 31 && value >> (2+index); index++) \
716 ; \
717}
718
719static void dasd_profile_end_add_data(struct dasd_profile_info *data,
720 int is_alias,
721 int is_tpm,
722 int is_read,
723 long sectors,
724 int sectors_ind,
725 int tottime_ind,
726 int tottimeps_ind,
727 int strtime_ind,
728 int irqtime_ind,
729 int irqtimeps_ind,
730 int endtime_ind)
731{
732 /* in case of an overflow, reset the whole profile */
733 if (data->dasd_io_reqs == UINT_MAX) {
734 memset(data, 0, sizeof(*data));
735 getnstimeofday(&data->starttod);
736 }
737 data->dasd_io_reqs++;
738 data->dasd_io_sects += sectors;
739 if (is_alias)
740 data->dasd_io_alias++;
741 if (is_tpm)
742 data->dasd_io_tpm++;
743
744 data->dasd_io_secs[sectors_ind]++;
745 data->dasd_io_times[tottime_ind]++;
746 data->dasd_io_timps[tottimeps_ind]++;
747 data->dasd_io_time1[strtime_ind]++;
748 data->dasd_io_time2[irqtime_ind]++;
749 data->dasd_io_time2ps[irqtimeps_ind]++;
750 data->dasd_io_time3[endtime_ind]++;
751
752 if (is_read) {
753 data->dasd_read_reqs++;
754 data->dasd_read_sects += sectors;
755 if (is_alias)
756 data->dasd_read_alias++;
757 if (is_tpm)
758 data->dasd_read_tpm++;
759 data->dasd_read_secs[sectors_ind]++;
760 data->dasd_read_times[tottime_ind]++;
761 data->dasd_read_time1[strtime_ind]++;
762 data->dasd_read_time2[irqtime_ind]++;
763 data->dasd_read_time3[endtime_ind]++;
764 }
765}
766
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100767static void dasd_profile_end(struct dasd_block *block,
768 struct dasd_ccw_req *cqr,
769 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 long strtime, irqtime, endtime, tottime; /* in microseconds */
772 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200773 struct dasd_device *device;
774 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
775 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200777 device = cqr->startdev;
778 if (!(dasd_global_profile_level ||
779 block->profile.data ||
780 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return;
782
Tejun Heo83096eb2009-05-07 22:24:39 +0900783 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!cqr->buildclk || !cqr->startclk ||
785 !cqr->stopclk || !cqr->endclk ||
786 !sectors)
787 return;
788
789 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
790 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
791 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
792 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
793 tottimeps = tottime / sectors;
794
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200795 dasd_profile_counter(sectors, sectors_ind);
796 dasd_profile_counter(tottime, tottime_ind);
797 dasd_profile_counter(tottimeps, tottimeps_ind);
798 dasd_profile_counter(strtime, strtime_ind);
799 dasd_profile_counter(irqtime, irqtime_ind);
800 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
801 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200803 if (dasd_global_profile_level) {
804 dasd_profile_end_add_data(&dasd_global_profile_data,
805 cqr->startdev != block->base,
806 cqr->cpmode == 1,
807 rq_data_dir(req) == READ,
808 sectors, sectors_ind, tottime_ind,
809 tottimeps_ind, strtime_ind,
810 irqtime_ind, irqtimeps_ind,
811 endtime_ind);
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200814 spin_lock(&block->profile.lock);
815 if (block->profile.data)
816 dasd_profile_end_add_data(block->profile.data,
817 cqr->startdev != block->base,
818 cqr->cpmode == 1,
819 rq_data_dir(req) == READ,
820 sectors, sectors_ind, tottime_ind,
821 tottimeps_ind, strtime_ind,
822 irqtime_ind, irqtimeps_ind,
823 endtime_ind);
824 spin_unlock(&block->profile.lock);
825
826 spin_lock(&device->profile.lock);
827 if (device->profile.data)
828 dasd_profile_end_add_data(device->profile.data,
829 cqr->startdev != block->base,
830 cqr->cpmode == 1,
831 rq_data_dir(req) == READ,
832 sectors, sectors_ind, tottime_ind,
833 tottimeps_ind, strtime_ind,
834 irqtime_ind, irqtimeps_ind,
835 endtime_ind);
836 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200838
839void dasd_profile_reset(struct dasd_profile *profile)
840{
841 struct dasd_profile_info *data;
842
843 spin_lock_bh(&profile->lock);
844 data = profile->data;
845 if (!data) {
846 spin_unlock_bh(&profile->lock);
847 return;
848 }
849 memset(data, 0, sizeof(*data));
850 getnstimeofday(&data->starttod);
851 spin_unlock_bh(&profile->lock);
852}
853
854void dasd_global_profile_reset(void)
855{
856 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
857 getnstimeofday(&dasd_global_profile_data.starttod);
858}
859
860int dasd_profile_on(struct dasd_profile *profile)
861{
862 struct dasd_profile_info *data;
863
864 data = kzalloc(sizeof(*data), GFP_KERNEL);
865 if (!data)
866 return -ENOMEM;
867 spin_lock_bh(&profile->lock);
868 if (profile->data) {
869 spin_unlock_bh(&profile->lock);
870 kfree(data);
871 return 0;
872 }
873 getnstimeofday(&data->starttod);
874 profile->data = data;
875 spin_unlock_bh(&profile->lock);
876 return 0;
877}
878
879void dasd_profile_off(struct dasd_profile *profile)
880{
881 spin_lock_bh(&profile->lock);
882 kfree(profile->data);
883 profile->data = NULL;
884 spin_unlock_bh(&profile->lock);
885}
886
887char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
888{
889 char *buffer;
890
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200891 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200892 if (buffer == NULL)
893 return ERR_PTR(-ENOMEM);
894 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200895 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200896 return ERR_PTR(-EFAULT);
897 }
898 /* got the string, now strip linefeed. */
899 if (buffer[user_len - 1] == '\n')
900 buffer[user_len - 1] = 0;
901 else
902 buffer[user_len] = 0;
903 return buffer;
904}
905
906static ssize_t dasd_stats_write(struct file *file,
907 const char __user *user_buf,
908 size_t user_len, loff_t *pos)
909{
910 char *buffer, *str;
911 int rc;
912 struct seq_file *m = (struct seq_file *)file->private_data;
913 struct dasd_profile *prof = m->private;
914
915 if (user_len > 65536)
916 user_len = 65536;
917 buffer = dasd_get_user_string(user_buf, user_len);
918 if (IS_ERR(buffer))
919 return PTR_ERR(buffer);
920
921 str = skip_spaces(buffer);
922 rc = user_len;
923 if (strncmp(str, "reset", 5) == 0) {
924 dasd_profile_reset(prof);
925 } else if (strncmp(str, "on", 2) == 0) {
926 rc = dasd_profile_on(prof);
927 if (!rc)
928 rc = user_len;
929 } else if (strncmp(str, "off", 3) == 0) {
930 dasd_profile_off(prof);
931 } else
932 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200933 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200934 return rc;
935}
936
937static void dasd_stats_array(struct seq_file *m, unsigned int *array)
938{
939 int i;
940
941 for (i = 0; i < 32; i++)
942 seq_printf(m, "%u ", array[i]);
943 seq_putc(m, '\n');
944}
945
946static void dasd_stats_seq_print(struct seq_file *m,
947 struct dasd_profile_info *data)
948{
949 seq_printf(m, "start_time %ld.%09ld\n",
950 data->starttod.tv_sec, data->starttod.tv_nsec);
951 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
952 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
953 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
954 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
955 seq_printf(m, "histogram_sectors ");
956 dasd_stats_array(m, data->dasd_io_secs);
957 seq_printf(m, "histogram_io_times ");
958 dasd_stats_array(m, data->dasd_io_times);
959 seq_printf(m, "histogram_io_times_weighted ");
960 dasd_stats_array(m, data->dasd_io_timps);
961 seq_printf(m, "histogram_time_build_to_ssch ");
962 dasd_stats_array(m, data->dasd_io_time1);
963 seq_printf(m, "histogram_time_ssch_to_irq ");
964 dasd_stats_array(m, data->dasd_io_time2);
965 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
966 dasd_stats_array(m, data->dasd_io_time2ps);
967 seq_printf(m, "histogram_time_irq_to_end ");
968 dasd_stats_array(m, data->dasd_io_time3);
969 seq_printf(m, "histogram_ccw_queue_length ");
970 dasd_stats_array(m, data->dasd_io_nr_req);
971 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
972 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
973 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
974 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
975 seq_printf(m, "histogram_read_sectors ");
976 dasd_stats_array(m, data->dasd_read_secs);
977 seq_printf(m, "histogram_read_times ");
978 dasd_stats_array(m, data->dasd_read_times);
979 seq_printf(m, "histogram_read_time_build_to_ssch ");
980 dasd_stats_array(m, data->dasd_read_time1);
981 seq_printf(m, "histogram_read_time_ssch_to_irq ");
982 dasd_stats_array(m, data->dasd_read_time2);
983 seq_printf(m, "histogram_read_time_irq_to_end ");
984 dasd_stats_array(m, data->dasd_read_time3);
985 seq_printf(m, "histogram_read_ccw_queue_length ");
986 dasd_stats_array(m, data->dasd_read_nr_req);
987}
988
989static int dasd_stats_show(struct seq_file *m, void *v)
990{
991 struct dasd_profile *profile;
992 struct dasd_profile_info *data;
993
994 profile = m->private;
995 spin_lock_bh(&profile->lock);
996 data = profile->data;
997 if (!data) {
998 spin_unlock_bh(&profile->lock);
999 seq_printf(m, "disabled\n");
1000 return 0;
1001 }
1002 dasd_stats_seq_print(m, data);
1003 spin_unlock_bh(&profile->lock);
1004 return 0;
1005}
1006
1007static int dasd_stats_open(struct inode *inode, struct file *file)
1008{
1009 struct dasd_profile *profile = inode->i_private;
1010 return single_open(file, dasd_stats_show, profile);
1011}
1012
1013static const struct file_operations dasd_stats_raw_fops = {
1014 .owner = THIS_MODULE,
1015 .open = dasd_stats_open,
1016 .read = seq_read,
1017 .llseek = seq_lseek,
1018 .release = single_release,
1019 .write = dasd_stats_write,
1020};
1021
1022static ssize_t dasd_stats_global_write(struct file *file,
1023 const char __user *user_buf,
1024 size_t user_len, loff_t *pos)
1025{
1026 char *buffer, *str;
1027 ssize_t rc;
1028
1029 if (user_len > 65536)
1030 user_len = 65536;
1031 buffer = dasd_get_user_string(user_buf, user_len);
1032 if (IS_ERR(buffer))
1033 return PTR_ERR(buffer);
1034 str = skip_spaces(buffer);
1035 rc = user_len;
1036 if (strncmp(str, "reset", 5) == 0) {
1037 dasd_global_profile_reset();
1038 } else if (strncmp(str, "on", 2) == 0) {
1039 dasd_global_profile_reset();
1040 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1041 } else if (strncmp(str, "off", 3) == 0) {
1042 dasd_global_profile_level = DASD_PROFILE_OFF;
1043 } else
1044 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001045 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001046 return rc;
1047}
1048
1049static int dasd_stats_global_show(struct seq_file *m, void *v)
1050{
1051 if (!dasd_global_profile_level) {
1052 seq_printf(m, "disabled\n");
1053 return 0;
1054 }
1055 dasd_stats_seq_print(m, &dasd_global_profile_data);
1056 return 0;
1057}
1058
1059static int dasd_stats_global_open(struct inode *inode, struct file *file)
1060{
1061 return single_open(file, dasd_stats_global_show, NULL);
1062}
1063
1064static const struct file_operations dasd_stats_global_fops = {
1065 .owner = THIS_MODULE,
1066 .open = dasd_stats_global_open,
1067 .read = seq_read,
1068 .llseek = seq_lseek,
1069 .release = single_release,
1070 .write = dasd_stats_global_write,
1071};
1072
1073static void dasd_profile_init(struct dasd_profile *profile,
1074 struct dentry *base_dentry)
1075{
1076 mode_t mode;
1077 struct dentry *pde;
1078
1079 if (!base_dentry)
1080 return;
1081 profile->dentry = NULL;
1082 profile->data = NULL;
1083 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1084 pde = debugfs_create_file("statistics", mode, base_dentry,
1085 profile, &dasd_stats_raw_fops);
1086 if (pde && !IS_ERR(pde))
1087 profile->dentry = pde;
1088 return;
1089}
1090
1091static void dasd_profile_exit(struct dasd_profile *profile)
1092{
1093 dasd_profile_off(profile);
1094 if (profile->dentry) {
1095 debugfs_remove(profile->dentry);
1096 profile->dentry = NULL;
1097 }
1098}
1099
1100static void dasd_statistics_removeroot(void)
1101{
1102 dasd_global_profile_level = DASD_PROFILE_OFF;
1103 if (dasd_global_profile_dentry) {
1104 debugfs_remove(dasd_global_profile_dentry);
1105 dasd_global_profile_dentry = NULL;
1106 }
1107 if (dasd_debugfs_global_entry)
1108 debugfs_remove(dasd_debugfs_global_entry);
1109 if (dasd_debugfs_root_entry)
1110 debugfs_remove(dasd_debugfs_root_entry);
1111}
1112
1113static void dasd_statistics_createroot(void)
1114{
1115 mode_t mode;
1116 struct dentry *pde;
1117
1118 dasd_debugfs_root_entry = NULL;
1119 dasd_debugfs_global_entry = NULL;
1120 dasd_global_profile_dentry = NULL;
1121 pde = debugfs_create_dir("dasd", NULL);
1122 if (!pde || IS_ERR(pde))
1123 goto error;
1124 dasd_debugfs_root_entry = pde;
1125 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1126 if (!pde || IS_ERR(pde))
1127 goto error;
1128 dasd_debugfs_global_entry = pde;
1129
1130 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1131 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1132 NULL, &dasd_stats_global_fops);
1133 if (!pde || IS_ERR(pde))
1134 goto error;
1135 dasd_global_profile_dentry = pde;
1136 return;
1137
1138error:
1139 DBF_EVENT(DBF_ERR, "%s",
1140 "Creation of the dasd debugfs interface failed");
1141 dasd_statistics_removeroot();
1142 return;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001146#define dasd_profile_start(block, cqr, req) do {} while (0)
1147#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001148
1149static void dasd_statistics_createroot(void)
1150{
1151 return;
1152}
1153
1154static void dasd_statistics_removeroot(void)
1155{
1156 return;
1157}
1158
1159int dasd_stats_generic_show(struct seq_file *m, void *v)
1160{
1161 seq_printf(m, "Statistics are not activated in this kernel\n");
1162 return 0;
1163}
1164
1165static void dasd_profile_init(struct dasd_profile *profile,
1166 struct dentry *base_dentry)
1167{
1168 return;
1169}
1170
1171static void dasd_profile_exit(struct dasd_profile *profile)
1172{
1173 return;
1174}
1175
1176int dasd_profile_on(struct dasd_profile *profile)
1177{
1178 return 0;
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181#endif /* CONFIG_DASD_PROFILE */
1182
1183/*
1184 * Allocate memory for a channel program with 'cplength' channel
1185 * command words and 'datasize' additional space. There are two
1186 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1187 * memory and 2) dasd_smalloc_request uses the static ccw memory
1188 * that gets allocated for each device.
1189 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001190struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001191 int datasize,
1192 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct dasd_ccw_req *cqr;
1195
1196 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001197 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001198 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001200 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (cqr == NULL)
1202 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 cqr->cpaddr = NULL;
1204 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001205 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 GFP_ATOMIC | GFP_DMA);
1207 if (cqr->cpaddr == NULL) {
1208 kfree(cqr);
1209 return ERR_PTR(-ENOMEM);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 cqr->data = NULL;
1213 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001214 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001216 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 kfree(cqr);
1218 return ERR_PTR(-ENOMEM);
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001221 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1223 dasd_get_device(device);
1224 return cqr;
1225}
1226
Stefan Haberland68b781f2009-09-11 10:28:29 +02001227struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001228 int datasize,
1229 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 unsigned long flags;
1232 struct dasd_ccw_req *cqr;
1233 char *data;
1234 int size;
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1237 if (cplength > 0)
1238 size += cplength * sizeof(struct ccw1);
1239 if (datasize > 0)
1240 size += datasize;
1241 spin_lock_irqsave(&device->mem_lock, flags);
1242 cqr = (struct dasd_ccw_req *)
1243 dasd_alloc_chunk(&device->ccw_chunks, size);
1244 spin_unlock_irqrestore(&device->mem_lock, flags);
1245 if (cqr == NULL)
1246 return ERR_PTR(-ENOMEM);
1247 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1248 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1249 cqr->cpaddr = NULL;
1250 if (cplength > 0) {
1251 cqr->cpaddr = (struct ccw1 *) data;
1252 data += cplength*sizeof(struct ccw1);
1253 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1254 }
1255 cqr->data = NULL;
1256 if (datasize > 0) {
1257 cqr->data = data;
1258 memset(cqr->data, 0, datasize);
1259 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001260 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1262 dasd_get_device(device);
1263 return cqr;
1264}
1265
1266/*
1267 * Free memory of a channel program. This function needs to free all the
1268 * idal lists that might have been created by dasd_set_cda and the
1269 * struct dasd_ccw_req itself.
1270 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001271void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001273#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct ccw1 *ccw;
1275
1276 /* Clear any idals used for the request. */
1277 ccw = cqr->cpaddr;
1278 do {
1279 clear_normalized_cda(ccw);
1280 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1281#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001282 kfree(cqr->cpaddr);
1283 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 kfree(cqr);
1285 dasd_put_device(device);
1286}
1287
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001288void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 unsigned long flags;
1291
1292 spin_lock_irqsave(&device->mem_lock, flags);
1293 dasd_free_chunk(&device->ccw_chunks, cqr);
1294 spin_unlock_irqrestore(&device->mem_lock, flags);
1295 dasd_put_device(device);
1296}
1297
1298/*
1299 * Check discipline magic in cqr.
1300 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001301static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct dasd_device *device;
1304
1305 if (cqr == NULL)
1306 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001307 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001309 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 " dasd_ccw_req 0x%08x magic doesn't match"
1311 " discipline 0x%08x",
1312 cqr->magic,
1313 *(unsigned int *) device->discipline->name);
1314 return -EINVAL;
1315 }
1316 return 0;
1317}
1318
1319/*
1320 * Terminate the current i/o and set the request to clear_pending.
1321 * Timer keeps device runnig.
1322 * ccw_device_clear can fail if the i/o subsystem
1323 * is in a bad mood.
1324 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001325int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct dasd_device *device;
1328 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001329 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* Check the cqr */
1332 rc = dasd_check_cqr(cqr);
1333 if (rc)
1334 return rc;
1335 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001336 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1338 rc = ccw_device_clear(device->cdev, (long) cqr);
1339 switch (rc) {
1340 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001341 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001343 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 DBF_DEV_EVENT(DBF_DEBUG, device,
1345 "terminate cqr %p successful",
1346 cqr);
1347 break;
1348 case -ENODEV:
1349 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1350 "device gone, retry");
1351 break;
1352 case -EIO:
1353 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1354 "I/O error, retry");
1355 break;
1356 case -EINVAL:
1357 case -EBUSY:
1358 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1359 "device busy, retry later");
1360 break;
1361 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001362 /* internal error 10 - unknown rc*/
1363 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1364 dev_err(&device->cdev->dev, "An error occurred in the "
1365 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 BUG();
1367 break;
1368 }
1369 retries++;
1370 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001371 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return rc;
1373}
1374
1375/*
1376 * Start the i/o. This start_IO can fail if the channel is really busy.
1377 * In that case set up a timer to start the request later.
1378 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001379int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct dasd_device *device;
1382 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001383 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* Check the cqr */
1386 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001387 if (rc) {
1388 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001390 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001391 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001392 if (((cqr->block &&
1393 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1394 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1395 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1396 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1397 "because of stolen lock", cqr);
1398 cqr->status = DASD_CQR_ERROR;
1399 cqr->intrc = -EPERM;
1400 return -EPERM;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001403 /* internal error 14 - start_IO run out of retries */
1404 sprintf(errorstring, "14 %p", cqr);
1405 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1406 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001407 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -EIO;
1409 }
1410 cqr->startclk = get_clock();
1411 cqr->starttime = jiffies;
1412 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001413 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1414 cqr->lpm &= device->path_data.opm;
1415 if (!cqr->lpm)
1416 cqr->lpm = device->path_data.opm;
1417 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001418 if (cqr->cpmode == 1) {
1419 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1420 (long) cqr, cqr->lpm);
1421 } else {
1422 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1423 (long) cqr, cqr->lpm, 0);
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 switch (rc) {
1426 case 0:
1427 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
1429 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001430 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 "start_IO: device busy, retry later");
1432 break;
1433 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001434 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 "start_IO: request timeout, retry later");
1436 break;
1437 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001438 /* -EACCES indicates that the request used only a subset of the
1439 * available paths and all these paths are gone. If the lpm of
1440 * this request was only a subset of the opm (e.g. the ppm) then
1441 * we just do a retry with all available paths.
1442 * If we already use the full opm, something is amiss, and we
1443 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001445 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1446 DBF_DEV_EVENT(DBF_WARNING, device,
1447 "start_IO: selected paths gone (%x)",
1448 cqr->lpm);
1449 } else if (cqr->lpm != device->path_data.opm) {
1450 cqr->lpm = device->path_data.opm;
1451 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1452 "start_IO: selected paths gone,"
1453 " retry on all paths");
1454 } else {
1455 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1456 "start_IO: all paths in opm gone,"
1457 " do path verification");
1458 dasd_generic_last_path_gone(device);
1459 device->path_data.opm = 0;
1460 device->path_data.ppm = 0;
1461 device->path_data.npm = 0;
1462 device->path_data.tbvpm =
1463 ccw_device_get_path_mask(device->cdev);
1464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 break;
1466 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001467 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001468 "start_IO: -ENODEV device gone, retry");
1469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001471 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001472 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001474 case -EINVAL:
1475 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001476 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001477 "start_IO: -EINVAL device currently "
1478 "not accessible");
1479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001481 /* internal error 11 - unknown rc */
1482 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1483 dev_err(&device->cdev->dev,
1484 "An error occurred in the DASD device driver, "
1485 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 BUG();
1487 break;
1488 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001489 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return rc;
1491}
1492
1493/*
1494 * Timeout function for dasd devices. This is used for different purposes
1495 * 1) missing interrupt handler for normal operation
1496 * 2) delayed start of request where start_IO failed with -EBUSY
1497 * 3) timeout for missing state change interrupts
1498 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1499 * DASD_CQR_QUEUED for 2) and 3).
1500 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001501static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 unsigned long flags;
1504 struct dasd_device *device;
1505
1506 device = (struct dasd_device *) ptr;
1507 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1508 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001509 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001511 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514/*
1515 * Setup timeout for a device in jiffies.
1516 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001517void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001519 if (expires == 0)
1520 del_timer(&device->timer);
1521 else
1522 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525/*
1526 * Clear timeout for a device.
1527 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001528void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001530 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001533static void dasd_handle_killed_request(struct ccw_device *cdev,
1534 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct dasd_ccw_req *cqr;
1537 struct dasd_device *device;
1538
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001539 if (!intparm)
1540 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 cqr = (struct dasd_ccw_req *) intparm;
1542 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001543 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1544 "invalid status in handle_killed_request: "
1545 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return;
1547 }
1548
Stefan Haberland589c74d2010-02-26 22:37:47 +01001549 device = dasd_device_from_cdev_locked(cdev);
1550 if (IS_ERR(device)) {
1551 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1552 "unable to get device from cdev");
1553 return;
1554 }
1555
1556 if (!cqr->startdev ||
1557 device != cqr->startdev ||
1558 strncmp(cqr->startdev->discipline->ebcname,
1559 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001560 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1561 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001562 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return;
1564 }
1565
1566 /* Schedule request to be retried. */
1567 cqr->status = DASD_CQR_QUEUED;
1568
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001569 dasd_device_clear_timer(device);
1570 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 dasd_put_device(device);
1572}
1573
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001574void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001576 /* First of all start sense subsystem status request. */
1577 dasd_eer_snss(device);
1578
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001579 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001580 dasd_schedule_device_bh(device);
1581 if (device->block)
1582 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/*
1586 * Interrupt handler for "normal" ssch-io based dasd devices.
1587 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001588void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1589 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct dasd_ccw_req *cqr, *next;
1592 struct dasd_device *device;
1593 unsigned long long now;
1594 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (IS_ERR(irb)) {
1597 switch (PTR_ERR(irb)) {
1598 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001601 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1602 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
1604 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001605 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1606 "unknown error %ld\n", __func__,
1607 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001609 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return;
1611 }
1612
1613 now = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001614 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001615 /* check for conditions that should be handled immediately */
1616 if (!cqr ||
1617 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1618 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001619 if (cqr)
1620 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001621 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001622 if (IS_ERR(device))
1623 return;
1624 /* ignore unsolicited interrupts for DIAG discipline */
1625 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001627 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001629 device->discipline->dump_sense_dbf(device, irb, "int");
1630 if (device->features & DASD_FEATURE_ERPLOG)
1631 device->discipline->dump_sense(device, cqr, irb);
1632 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001633 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001635 if (!cqr)
1636 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001638 device = (struct dasd_device *) cqr->startdev;
1639 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001641 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1642 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return;
1644 }
1645
1646 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001647 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001648 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001649 cqr->status = DASD_CQR_CLEARED;
1650 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001651 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001652 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return;
1654 }
1655
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001656 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001658 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1659 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return;
1661 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001662
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001663 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001665 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1666 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001667 /* request was completed successfully */
1668 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 cqr->stopclk = now;
1670 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001671 if (cqr->devlist.next != &device->ccw_queue) {
1672 next = list_entry(cqr->devlist.next,
1673 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001675 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001676 /*
1677 * If we don't want complex ERP for this request, then just
1678 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001679 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001680 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001681 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001682 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001683 DBF_DEV_EVENT(DBF_DEBUG, device,
1684 "default ERP in fastpath "
1685 "(%i retries left)",
1686 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001687 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1688 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001689 cqr->status = DASD_CQR_QUEUED;
1690 next = cqr;
1691 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001693 }
1694 if (next && (next->status == DASD_CQR_QUEUED) &&
1695 (!device->stopped)) {
1696 if (device->discipline->start_IO(next) == 0)
1697 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001700 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001702 dasd_device_clear_timer(device);
1703 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001706enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1707{
1708 struct dasd_device *device;
1709
1710 device = dasd_device_from_cdev_locked(cdev);
1711
1712 if (IS_ERR(device))
1713 goto out;
1714 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1715 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001716 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001717 dasd_put_device(device);
1718 goto out;
1719 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001720 if (device->discipline->dump_sense_dbf)
1721 device->discipline->dump_sense_dbf(device, irb, "uc");
1722 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001723 dasd_put_device(device);
1724out:
1725 return UC_TODO_RETRY;
1726}
1727EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001730 * If we have an error on a dasd_block layer request then we cancel
1731 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001733static void __dasd_device_recovery(struct dasd_device *device,
1734 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct list_head *l, *n;
1737 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001744
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001745 list_for_each_safe(l, n, &device->ccw_queue) {
1746 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1747 if (cqr->status == DASD_CQR_QUEUED &&
1748 ref_cqr->block == cqr->block) {
1749 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001751 }
1752};
1753
1754/*
1755 * Remove those ccw requests from the queue that need to be returned
1756 * to the upper layer.
1757 */
1758static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1759 struct list_head *final_queue)
1760{
1761 struct list_head *l, *n;
1762 struct dasd_ccw_req *cqr;
1763
1764 /* Process request with final status. */
1765 list_for_each_safe(l, n, &device->ccw_queue) {
1766 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1767
1768 /* Stop list processing at the first non-final request. */
1769 if (cqr->status == DASD_CQR_QUEUED ||
1770 cqr->status == DASD_CQR_IN_IO ||
1771 cqr->status == DASD_CQR_CLEAR_PENDING)
1772 break;
1773 if (cqr->status == DASD_CQR_ERROR) {
1774 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001776 /* Rechain finished requests to final queue */
1777 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779}
1780
1781/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001782 * the cqrs from the final queue are returned to the upper layer
1783 * by setting a dasd_block state and calling the callback function
1784 */
1785static void __dasd_device_process_final_queue(struct dasd_device *device,
1786 struct list_head *final_queue)
1787{
1788 struct list_head *l, *n;
1789 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001790 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001791 void (*callback)(struct dasd_ccw_req *, void *data);
1792 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001793 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001794
1795 list_for_each_safe(l, n, final_queue) {
1796 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1797 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001798 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001799 callback = cqr->callback;
1800 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001801 if (block)
1802 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001803 switch (cqr->status) {
1804 case DASD_CQR_SUCCESS:
1805 cqr->status = DASD_CQR_DONE;
1806 break;
1807 case DASD_CQR_ERROR:
1808 cqr->status = DASD_CQR_NEED_ERP;
1809 break;
1810 case DASD_CQR_CLEARED:
1811 cqr->status = DASD_CQR_TERMINATED;
1812 break;
1813 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001814 /* internal error 12 - wrong cqr status*/
1815 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1816 dev_err(&device->cdev->dev,
1817 "An error occurred in the DASD device driver, "
1818 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001819 BUG();
1820 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001821 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001822 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001823 if (block)
1824 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001825 }
1826}
1827
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001828/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * Take a look at the first request on the ccw queue and check
1830 * if it reached its expire time. If so, terminate the IO.
1831 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001832static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 struct dasd_ccw_req *cqr;
1835
1836 if (list_empty(&device->ccw_queue))
1837 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001838 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001839 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1840 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1841 if (device->discipline->term_IO(cqr) != 0) {
1842 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001843 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001844 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001845 "ended, retrying in 5 s\n",
1846 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001847 cqr->expires += 5*HZ;
1848 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001849 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001850 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001851 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001852 "remaining\n", cqr, (cqr->expires/HZ),
1853 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 }
1856}
1857
1858/*
1859 * Take a look at the first request on the ccw queue and check
1860 * if it needs to be started.
1861 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001862static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 struct dasd_ccw_req *cqr;
1865 int rc;
1866
1867 if (list_empty(&device->ccw_queue))
1868 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001869 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001870 if (cqr->status != DASD_CQR_QUEUED)
1871 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001872 /* when device is stopped, return request to previous layer
1873 * exception: only the disconnect or unresumed bits are set and the
1874 * cqr is a path verification request
1875 */
1876 if (device->stopped &&
1877 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1878 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1879 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001880 cqr->status = DASD_CQR_CLEARED;
1881 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001882 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001883 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001884
1885 rc = device->discipline->start_IO(cqr);
1886 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001887 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001888 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001889 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001890 } else
1891 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001892 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001893}
1894
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001895static void __dasd_device_check_path_events(struct dasd_device *device)
1896{
1897 int rc;
1898
1899 if (device->path_data.tbvpm) {
1900 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1901 DASD_UNRESUMED_PM))
1902 return;
1903 rc = device->discipline->verify_path(
1904 device, device->path_data.tbvpm);
1905 if (rc)
1906 dasd_device_set_timer(device, 50);
1907 else
1908 device->path_data.tbvpm = 0;
1909 }
1910};
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001913 * Go through all request on the dasd_device request queue,
1914 * terminate them on the cdev if necessary, and return them to the
1915 * submitting layer via callback.
1916 * Note:
1917 * Make sure that all 'submitting layers' still exist when
1918 * this function is called!. In other words, when 'device' is a base
1919 * device then all block layer requests must have been removed before
1920 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001922int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001924 struct dasd_ccw_req *cqr, *n;
1925 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 INIT_LIST_HEAD(&flush_queue);
1929 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001930 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001931 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001932 /* Check status and move request to flush_queue */
1933 switch (cqr->status) {
1934 case DASD_CQR_IN_IO:
1935 rc = device->discipline->term_IO(cqr);
1936 if (rc) {
1937 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001938 dev_err(&device->cdev->dev,
1939 "Flushing the DASD request queue "
1940 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001941 /* stop flush processing */
1942 goto finished;
1943 }
1944 break;
1945 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001947 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001948 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001950 break;
1951 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001952 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001953 }
Horst Hummel8f617012006-08-30 14:33:33 +02001954finished:
1955 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001956 /*
1957 * After this point all requests must be in state CLEAR_PENDING,
1958 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1959 * one of the others.
1960 */
1961 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1962 wait_event(dasd_flush_wq,
1963 (cqr->status != DASD_CQR_CLEAR_PENDING));
1964 /*
1965 * Now set each request back to TERMINATED, DONE or NEED_ERP
1966 * and call the callback function of flushed requests
1967 */
1968 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001969 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972/*
1973 * Acquire the device lock and process queues for the device.
1974 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001975static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 atomic_set (&device->tasklet_scheduled, 0);
1980 INIT_LIST_HEAD(&final_queue);
1981 spin_lock_irq(get_ccwdev_lock(device->cdev));
1982 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001983 __dasd_device_check_expire(device);
1984 /* find final requests on ccw queue */
1985 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001986 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1988 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001989 __dasd_device_process_final_queue(device, &final_queue);
1990 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001992 __dasd_device_start_head(device);
1993 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 dasd_put_device(device);
1995}
1996
1997/*
1998 * Schedules a call to dasd_tasklet over the device tasklet.
1999 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002000void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002003 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return;
2005 dasd_get_device(device);
2006 tasklet_hi_schedule(&device->tasklet);
2007}
2008
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002009void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2010{
2011 device->stopped |= bits;
2012}
2013EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2014
2015void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2016{
2017 device->stopped &= ~bits;
2018 if (!device->stopped)
2019 wake_up(&generic_waitq);
2020}
2021EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002024 * Queue a request to the head of the device ccw_queue.
2025 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002027void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct dasd_device *device;
2030 unsigned long flags;
2031
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002032 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 cqr->status = DASD_CQR_QUEUED;
2035 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002037 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2039}
2040
2041/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002042 * Queue a request to the tail of the device ccw_queue.
2043 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002045void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 struct dasd_device *device;
2048 unsigned long flags;
2049
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002050 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052 cqr->status = DASD_CQR_QUEUED;
2053 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002055 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2057}
2058
2059/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002060 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002062void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002064 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2065 cqr->callback_data = DASD_SLEEPON_END_TAG;
2066 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2067 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
Stefan Haberland5915a872011-10-30 15:16:57 +01002069EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002071static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 struct dasd_device *device;
2074 int rc;
2075
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002076 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002078 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2080 return rc;
2081}
2082
2083/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002084 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2085 */
2086static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2087{
2088 struct dasd_device *device;
2089 dasd_erp_fn_t erp_fn;
2090
2091 if (cqr->status == DASD_CQR_FILLED)
2092 return 0;
2093 device = cqr->startdev;
2094 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2095 if (cqr->status == DASD_CQR_TERMINATED) {
2096 device->discipline->handle_terminated_request(cqr);
2097 return 1;
2098 }
2099 if (cqr->status == DASD_CQR_NEED_ERP) {
2100 erp_fn = device->discipline->erp_action(cqr);
2101 erp_fn(cqr);
2102 return 1;
2103 }
2104 if (cqr->status == DASD_CQR_FAILED)
2105 dasd_log_sense(cqr, &cqr->irb);
2106 if (cqr->refers) {
2107 __dasd_process_erp(device, cqr);
2108 return 1;
2109 }
2110 }
2111 return 0;
2112}
2113
2114static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2115{
2116 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2117 if (cqr->refers) /* erp is not done yet */
2118 return 1;
2119 return ((cqr->status != DASD_CQR_DONE) &&
2120 (cqr->status != DASD_CQR_FAILED));
2121 } else
2122 return (cqr->status == DASD_CQR_FILLED);
2123}
2124
2125static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2126{
2127 struct dasd_device *device;
2128 int rc;
2129 struct list_head ccw_queue;
2130 struct dasd_ccw_req *cqr;
2131
2132 INIT_LIST_HEAD(&ccw_queue);
2133 maincqr->status = DASD_CQR_FILLED;
2134 device = maincqr->startdev;
2135 list_add(&maincqr->blocklist, &ccw_queue);
2136 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2137 cqr = list_first_entry(&ccw_queue,
2138 struct dasd_ccw_req, blocklist)) {
2139
2140 if (__dasd_sleep_on_erp(cqr))
2141 continue;
2142 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2143 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002144 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2145 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2146 cqr->status = DASD_CQR_FAILED;
2147 cqr->intrc = -EPERM;
2148 continue;
2149 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002150 /* Non-temporary stop condition will trigger fail fast */
2151 if (device->stopped & ~DASD_STOPPED_PENDING &&
2152 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2153 (!dasd_eer_enabled(device))) {
2154 cqr->status = DASD_CQR_FAILED;
2155 continue;
2156 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002157 /* Don't try to start requests if device is stopped */
2158 if (interruptible) {
2159 rc = wait_event_interruptible(
2160 generic_waitq, !(device->stopped));
2161 if (rc == -ERESTARTSYS) {
2162 cqr->status = DASD_CQR_FAILED;
2163 maincqr->intrc = rc;
2164 continue;
2165 }
2166 } else
2167 wait_event(generic_waitq, !(device->stopped));
2168
Stefan Haberland5915a872011-10-30 15:16:57 +01002169 if (!cqr->callback)
2170 cqr->callback = dasd_wakeup_cb;
2171
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002172 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002173 dasd_add_request_tail(cqr);
2174 if (interruptible) {
2175 rc = wait_event_interruptible(
2176 generic_waitq, _wait_for_wakeup(cqr));
2177 if (rc == -ERESTARTSYS) {
2178 dasd_cancel_req(cqr);
2179 /* wait (non-interruptible) for final status */
2180 wait_event(generic_waitq,
2181 _wait_for_wakeup(cqr));
2182 cqr->status = DASD_CQR_FAILED;
2183 maincqr->intrc = rc;
2184 continue;
2185 }
2186 } else
2187 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2188 }
2189
2190 maincqr->endclk = get_clock();
2191 if ((maincqr->status != DASD_CQR_DONE) &&
2192 (maincqr->intrc != -ERESTARTSYS))
2193 dasd_log_sense(maincqr, &maincqr->irb);
2194 if (maincqr->status == DASD_CQR_DONE)
2195 rc = 0;
2196 else if (maincqr->intrc)
2197 rc = maincqr->intrc;
2198 else
2199 rc = -EIO;
2200 return rc;
2201}
2202
2203/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002204 * Queue a request to the tail of the device ccw_queue and wait for
2205 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002207int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002209 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002213 * Queue a request to the tail of the device ccw_queue and wait
2214 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002216int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002218 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
2221/*
2222 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2223 * for eckd devices) the currently running request has to be terminated
2224 * and be put back to status queued, before the special request is added
2225 * to the head of the queue. Then the special request is waited on normally.
2226 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002227static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002230 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if (list_empty(&device->ccw_queue))
2233 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002234 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002235 rc = device->discipline->term_IO(cqr);
2236 if (!rc)
2237 /*
2238 * CQR terminated because a more important request is pending.
2239 * Undo decreasing of retry counter because this is
2240 * not an error case.
2241 */
2242 cqr->retries++;
2243 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002246int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 struct dasd_device *device;
2249 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002250
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002251 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002252 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2253 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2254 cqr->status = DASD_CQR_FAILED;
2255 cqr->intrc = -EPERM;
2256 return -EIO;
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 spin_lock_irq(get_ccwdev_lock(device->cdev));
2259 rc = _dasd_term_running_cqr(device);
2260 if (rc) {
2261 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2262 return rc;
2263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002265 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002267 /*
2268 * add new request as second
2269 * first the terminated cqr needs to be finished
2270 */
2271 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002274 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2277
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002278 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002279
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002280 if (cqr->status == DASD_CQR_DONE)
2281 rc = 0;
2282 else if (cqr->intrc)
2283 rc = cqr->intrc;
2284 else
2285 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return rc;
2287}
2288
2289/*
2290 * Cancels a request that was started with dasd_sleep_on_req.
2291 * This is useful to timeout requests. The request will be
2292 * terminated if it is currently in i/o.
2293 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002294 * 0 if there was no need to terminate the request (not started yet)
2295 * negative error code if termination failed
2296 * Cancellation of a request is an asynchronous operation! The calling
2297 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002299int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002301 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 unsigned long flags;
2303 int rc;
2304
2305 rc = 0;
2306 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2307 switch (cqr->status) {
2308 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002309 /* request was not started - just set to cleared */
2310 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 break;
2312 case DASD_CQR_IN_IO:
2313 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002314 rc = device->discipline->term_IO(cqr);
2315 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002316 dev_err(&device->cdev->dev,
2317 "Cancelling request %p failed with rc=%d\n",
2318 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002319 } else {
2320 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002323 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002327 dasd_schedule_device_bh(device);
2328 return rc;
2329}
2330
2331
2332/*
2333 * SECTION: Operations of the dasd_block layer.
2334 */
2335
2336/*
2337 * Timeout function for dasd_block. This is used when the block layer
2338 * is waiting for something that may not come reliably, (e.g. a state
2339 * change interrupt)
2340 */
2341static void dasd_block_timeout(unsigned long ptr)
2342{
2343 unsigned long flags;
2344 struct dasd_block *block;
2345
2346 block = (struct dasd_block *) ptr;
2347 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2348 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002349 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002350 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2351 dasd_schedule_block_bh(block);
2352}
2353
2354/*
2355 * Setup timeout for a dasd_block in jiffies.
2356 */
2357void dasd_block_set_timer(struct dasd_block *block, int expires)
2358{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002359 if (expires == 0)
2360 del_timer(&block->timer);
2361 else
2362 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002363}
2364
2365/*
2366 * Clear timeout for a dasd_block.
2367 */
2368void dasd_block_clear_timer(struct dasd_block *block)
2369{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002370 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002371}
2372
2373/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002374 * Process finished error recovery ccw.
2375 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002376static void __dasd_process_erp(struct dasd_device *device,
2377 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002378{
2379 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002380
2381 if (cqr->status == DASD_CQR_DONE)
2382 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2383 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002384 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002385 erp_fn = device->discipline->erp_postaction(cqr);
2386 erp_fn(cqr);
2387}
2388
2389/*
2390 * Fetch requests from the block device queue.
2391 */
2392static void __dasd_process_request_queue(struct dasd_block *block)
2393{
2394 struct request_queue *queue;
2395 struct request *req;
2396 struct dasd_ccw_req *cqr;
2397 struct dasd_device *basedev;
2398 unsigned long flags;
2399 queue = block->request_queue;
2400 basedev = block->base;
2401 /* No queue ? Then there is nothing to do. */
2402 if (queue == NULL)
2403 return;
2404
2405 /*
2406 * We requeue request from the block device queue to the ccw
2407 * queue only in two states. In state DASD_STATE_READY the
2408 * partition detection is done and we need to requeue requests
2409 * for that. State DASD_STATE_ONLINE is normal block device
2410 * operation.
2411 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002412 if (basedev->state < DASD_STATE_READY) {
2413 while ((req = blk_fetch_request(block->request_queue)))
2414 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002415 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002416 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002417 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002418 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002419 if (basedev->features & DASD_FEATURE_READONLY &&
2420 rq_data_dir(req) == WRITE) {
2421 DBF_DEV_EVENT(DBF_ERR, basedev,
2422 "Rejecting write request %p",
2423 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002424 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002425 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002426 continue;
2427 }
2428 cqr = basedev->discipline->build_cp(basedev, block, req);
2429 if (IS_ERR(cqr)) {
2430 if (PTR_ERR(cqr) == -EBUSY)
2431 break; /* normal end condition */
2432 if (PTR_ERR(cqr) == -ENOMEM)
2433 break; /* terminate request queue loop */
2434 if (PTR_ERR(cqr) == -EAGAIN) {
2435 /*
2436 * The current request cannot be build right
2437 * now, we have to try later. If this request
2438 * is the head-of-queue we stop the device
2439 * for 1/2 second.
2440 */
2441 if (!list_empty(&block->ccw_queue))
2442 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002443 spin_lock_irqsave(
2444 get_ccwdev_lock(basedev->cdev), flags);
2445 dasd_device_set_stop_bits(basedev,
2446 DASD_STOPPED_PENDING);
2447 spin_unlock_irqrestore(
2448 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002449 dasd_block_set_timer(block, HZ/2);
2450 break;
2451 }
2452 DBF_DEV_EVENT(DBF_ERR, basedev,
2453 "CCW creation failed (rc=%ld) "
2454 "on request %p",
2455 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002456 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002457 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002458 continue;
2459 }
2460 /*
2461 * Note: callback is set to dasd_return_cqr_cb in
2462 * __dasd_block_start_head to cover erp requests as well
2463 */
2464 cqr->callback_data = (void *) req;
2465 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002466 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002467 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2468 dasd_profile_start(block, cqr, req);
2469 }
2470}
2471
2472static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2473{
2474 struct request *req;
2475 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002476 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002477
2478 req = (struct request *) cqr->callback_data;
2479 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002480 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002481 if (status <= 0)
2482 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09002483 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002484}
2485
2486/*
2487 * Process ccw request queue.
2488 */
2489static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2490 struct list_head *final_queue)
2491{
2492 struct list_head *l, *n;
2493 struct dasd_ccw_req *cqr;
2494 dasd_erp_fn_t erp_fn;
2495 unsigned long flags;
2496 struct dasd_device *base = block->base;
2497
2498restart:
2499 /* Process request with final status. */
2500 list_for_each_safe(l, n, &block->ccw_queue) {
2501 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2502 if (cqr->status != DASD_CQR_DONE &&
2503 cqr->status != DASD_CQR_FAILED &&
2504 cqr->status != DASD_CQR_NEED_ERP &&
2505 cqr->status != DASD_CQR_TERMINATED)
2506 continue;
2507
2508 if (cqr->status == DASD_CQR_TERMINATED) {
2509 base->discipline->handle_terminated_request(cqr);
2510 goto restart;
2511 }
2512
2513 /* Process requests that may be recovered */
2514 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002515 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002516 if (IS_ERR(erp_fn(cqr)))
2517 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002518 goto restart;
2519 }
2520
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002521 /* log sense for fatal error */
2522 if (cqr->status == DASD_CQR_FAILED) {
2523 dasd_log_sense(cqr, &cqr->irb);
2524 }
2525
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002526 /* First of all call extended error reporting. */
2527 if (dasd_eer_enabled(base) &&
2528 cqr->status == DASD_CQR_FAILED) {
2529 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2530
2531 /* restart request */
2532 cqr->status = DASD_CQR_FILLED;
2533 cqr->retries = 255;
2534 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002535 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002536 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2537 flags);
2538 goto restart;
2539 }
2540
2541 /* Process finished ERP request. */
2542 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002543 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002544 goto restart;
2545 }
2546
2547 /* Rechain finished requests to final queue */
2548 cqr->endclk = get_clock();
2549 list_move_tail(&cqr->blocklist, final_queue);
2550 }
2551}
2552
2553static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2554{
2555 dasd_schedule_block_bh(cqr->block);
2556}
2557
2558static void __dasd_block_start_head(struct dasd_block *block)
2559{
2560 struct dasd_ccw_req *cqr;
2561
2562 if (list_empty(&block->ccw_queue))
2563 return;
2564 /* We allways begin with the first requests on the queue, as some
2565 * of previously started requests have to be enqueued on a
2566 * dasd_device again for error recovery.
2567 */
2568 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2569 if (cqr->status != DASD_CQR_FILLED)
2570 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002571 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2572 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2573 cqr->status = DASD_CQR_FAILED;
2574 cqr->intrc = -EPERM;
2575 dasd_schedule_block_bh(block);
2576 continue;
2577 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002578 /* Non-temporary stop condition will trigger fail fast */
2579 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2580 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2581 (!dasd_eer_enabled(block->base))) {
2582 cqr->status = DASD_CQR_FAILED;
2583 dasd_schedule_block_bh(block);
2584 continue;
2585 }
2586 /* Don't try to start requests if device is stopped */
2587 if (block->base->stopped)
2588 return;
2589
2590 /* just a fail safe check, should not happen */
2591 if (!cqr->startdev)
2592 cqr->startdev = block->base;
2593
2594 /* make sure that the requests we submit find their way back */
2595 cqr->callback = dasd_return_cqr_cb;
2596
2597 dasd_add_request_tail(cqr);
2598 }
2599}
2600
2601/*
2602 * Central dasd_block layer routine. Takes requests from the generic
2603 * block layer request queue, creates ccw requests, enqueues them on
2604 * a dasd_device and processes ccw requests that have been returned.
2605 */
2606static void dasd_block_tasklet(struct dasd_block *block)
2607{
2608 struct list_head final_queue;
2609 struct list_head *l, *n;
2610 struct dasd_ccw_req *cqr;
2611
2612 atomic_set(&block->tasklet_scheduled, 0);
2613 INIT_LIST_HEAD(&final_queue);
2614 spin_lock(&block->queue_lock);
2615 /* Finish off requests on ccw queue */
2616 __dasd_process_block_ccw_queue(block, &final_queue);
2617 spin_unlock(&block->queue_lock);
2618 /* Now call the callback function of requests with final status */
2619 spin_lock_irq(&block->request_queue_lock);
2620 list_for_each_safe(l, n, &final_queue) {
2621 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2622 list_del_init(&cqr->blocklist);
2623 __dasd_cleanup_cqr(cqr);
2624 }
2625 spin_lock(&block->queue_lock);
2626 /* Get new request from the block device request queue */
2627 __dasd_process_request_queue(block);
2628 /* Now check if the head of the ccw queue needs to be started. */
2629 __dasd_block_start_head(block);
2630 spin_unlock(&block->queue_lock);
2631 spin_unlock_irq(&block->request_queue_lock);
2632 dasd_put_device(block->base);
2633}
2634
2635static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2636{
2637 wake_up(&dasd_flush_wq);
2638}
2639
2640/*
2641 * Go through all request on the dasd_block request queue, cancel them
2642 * on the respective dasd_device, and return them to the generic
2643 * block layer.
2644 */
2645static int dasd_flush_block_queue(struct dasd_block *block)
2646{
2647 struct dasd_ccw_req *cqr, *n;
2648 int rc, i;
2649 struct list_head flush_queue;
2650
2651 INIT_LIST_HEAD(&flush_queue);
2652 spin_lock_bh(&block->queue_lock);
2653 rc = 0;
2654restart:
2655 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2656 /* if this request currently owned by a dasd_device cancel it */
2657 if (cqr->status >= DASD_CQR_QUEUED)
2658 rc = dasd_cancel_req(cqr);
2659 if (rc < 0)
2660 break;
2661 /* Rechain request (including erp chain) so it won't be
2662 * touched by the dasd_block_tasklet anymore.
2663 * Replace the callback so we notice when the request
2664 * is returned from the dasd_device layer.
2665 */
2666 cqr->callback = _dasd_wake_block_flush_cb;
2667 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2668 list_move_tail(&cqr->blocklist, &flush_queue);
2669 if (i > 1)
2670 /* moved more than one request - need to restart */
2671 goto restart;
2672 }
2673 spin_unlock_bh(&block->queue_lock);
2674 /* Now call the callback function of flushed requests */
2675restart_cb:
2676 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2677 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2678 /* Process finished ERP request. */
2679 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002680 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002681 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002682 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002683 /* restart list_for_xx loop since dasd_process_erp
2684 * might remove multiple elements */
2685 goto restart_cb;
2686 }
2687 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002688 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002689 cqr->endclk = get_clock();
2690 list_del_init(&cqr->blocklist);
2691 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002692 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return rc;
2695}
2696
2697/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002698 * Schedules a call to dasd_tasklet over the device tasklet.
2699 */
2700void dasd_schedule_block_bh(struct dasd_block *block)
2701{
2702 /* Protect against rescheduling. */
2703 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2704 return;
2705 /* life cycle of block is bound to it's base device */
2706 dasd_get_device(block->base);
2707 tasklet_hi_schedule(&block->tasklet);
2708}
2709
2710
2711/*
2712 * SECTION: external block device operations
2713 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 */
2715
2716/*
2717 * Dasd request queue function. Called from ll_rw_blk.c
2718 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002719static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002721 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002723 block = queue->queuedata;
2724 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002726 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002728 __dasd_block_start_head(block);
2729 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
2732/*
2733 * Allocate and initialize request queue and default I/O scheduler.
2734 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002735static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 int rc;
2738
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002739 block->request_queue = blk_init_queue(do_dasd_request,
2740 &block->request_queue_lock);
2741 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return -ENOMEM;
2743
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002744 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002746 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002747 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002748 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002750 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return rc;
2752 }
2753 return 0;
2754}
2755
2756/*
2757 * Allocate and initialize request queue.
2758 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002759static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 int max;
2762
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002763 if (block->base->features & DASD_FEATURE_USERAW) {
2764 /*
2765 * the max_blocks value for raw_track access is 256
2766 * it is higher than the native ECKD value because we
2767 * only need one ccw per track
2768 * so the max_hw_sectors are
2769 * 2048 x 512B = 1024kB = 16 tracks
2770 */
2771 max = 2048;
2772 } else {
2773 max = block->base->discipline->max_blocks << block->s2b_shift;
2774 }
2775 blk_queue_logical_block_size(block->request_queue,
2776 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002777 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002778 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002779 /* with page sized segments we can translate each segement into
2780 * one idaw/tidaw
2781 */
2782 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2783 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/*
2787 * Deactivate and free request queue.
2788 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002789static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002791 if (block->request_queue) {
2792 blk_cleanup_queue(block->request_queue);
2793 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
2795}
2796
2797/*
2798 * Flush request on the request queue.
2799 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002800static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 struct request *req;
2803
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002804 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002806
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002807 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002808 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002809 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002810 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
Al Viro57a7c0b2008-03-02 10:36:08 -05002813static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Stefan Haberland9eb25122010-02-26 22:37:46 +01002815 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 int rc;
2817
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002818 base = dasd_device_from_gendisk(bdev->bd_disk);
2819 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01002820 return -ENODEV;
2821
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002822 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002823 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 rc = -ENODEV;
2825 goto unlock;
2826 }
2827
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002828 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 rc = -EINVAL;
2830 goto unlock;
2831 }
2832
2833 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002834 dev_info(&base->cdev->dev,
2835 "Accessing the DASD failed because it is in "
2836 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 rc = -EPERM;
2838 goto out;
2839 }
2840
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002841 if (base->state <= DASD_STATE_BASIC) {
2842 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 " Cannot open unrecognized device");
2844 rc = -ENODEV;
2845 goto out;
2846 }
2847
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002848 if ((mode & FMODE_WRITE) &&
2849 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2850 (base->features & DASD_FEATURE_READONLY))) {
2851 rc = -EROFS;
2852 goto out;
2853 }
2854
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002855 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
2857
2858out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002859 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002861 atomic_dec(&base->block->open_count);
2862 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return rc;
2864}
2865
Al Viro57a7c0b2008-03-02 10:36:08 -05002866static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002868 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002870 base = dasd_device_from_gendisk(disk);
2871 if (!base)
2872 return -ENODEV;
2873
2874 atomic_dec(&base->block->open_count);
2875 module_put(base->discipline->owner);
2876 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return 0;
2878}
2879
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002880/*
2881 * Return disk geometry.
2882 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002883static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002884{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002885 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002886
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002887 base = dasd_device_from_gendisk(bdev->bd_disk);
2888 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002889 return -ENODEV;
2890
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002891 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002892 !base->discipline->fill_geometry) {
2893 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002894 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002895 }
2896 base->discipline->fill_geometry(base->block, geo);
2897 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
2898 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002899 return 0;
2900}
2901
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002902const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903dasd_device_operations = {
2904 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002905 .open = dasd_open,
2906 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002907 .ioctl = dasd_ioctl,
2908 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002909 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910};
2911
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002912/*******************************************************************************
2913 * end of block device operations
2914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916static void
2917dasd_exit(void)
2918{
2919#ifdef CONFIG_PROC_FS
2920 dasd_proc_exit();
2921#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002922 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002923 if (dasd_page_cache != NULL) {
2924 kmem_cache_destroy(dasd_page_cache);
2925 dasd_page_cache = NULL;
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 dasd_gendisk_exit();
2928 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (dasd_debug_area != NULL) {
2930 debug_unregister(dasd_debug_area);
2931 dasd_debug_area = NULL;
2932 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02002933 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
2936/*
2937 * SECTION: common functions for ccw_driver use
2938 */
2939
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002940/*
2941 * Is the device read-only?
2942 * Note that this function does not report the setting of the
2943 * readonly device attribute, but how it is configured in z/VM.
2944 */
2945int dasd_device_is_ro(struct dasd_device *device)
2946{
2947 struct ccw_dev_id dev_id;
2948 struct diag210 diag_data;
2949 int rc;
2950
2951 if (!MACHINE_IS_VM)
2952 return 0;
2953 ccw_device_get_id(device->cdev, &dev_id);
2954 memset(&diag_data, 0, sizeof(diag_data));
2955 diag_data.vrdcdvno = dev_id.devno;
2956 diag_data.vrdclen = sizeof(diag_data);
2957 rc = diag210(&diag_data);
2958 if (rc == 0 || rc == 2) {
2959 return diag_data.vrdcvfla & 0x80;
2960 } else {
2961 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2962 dev_id.devno, rc);
2963 return 0;
2964 }
2965}
2966EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2967
Cornelia Huckf3445a12009-04-14 15:36:23 +02002968static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2969{
2970 struct ccw_device *cdev = data;
2971 int ret;
2972
2973 ret = ccw_device_set_online(cdev);
2974 if (ret)
2975 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2976 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02002977}
2978
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002979/*
2980 * Initial attempt at a probe function. this can be simplified once
2981 * the other detection code is gone.
2982 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002983int dasd_generic_probe(struct ccw_device *cdev,
2984 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 int ret;
2987
2988 ret = dasd_add_sysfs_files(cdev);
2989 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002990 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2991 "dasd_generic_probe: could not add "
2992 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002993 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Horst Hummel40545572006-06-29 15:08:18 +02002995 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Horst Hummel40545572006-06-29 15:08:18 +02002997 /*
2998 * Automatically online either all dasd devices (dasd_autodetect)
2999 * or all devices specified with dasd= parameters during
3000 * initial probe.
3001 */
3002 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003003 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003004 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003008/*
3009 * This will one day be called from a global not_oper handler.
3010 * It is also used by driver_unregister during module unload.
3011 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003012void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
3014 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003015 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Horst Hummel59afda72005-05-16 21:53:39 -07003017 cdev->handler = NULL;
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 dasd_remove_sysfs_files(cdev);
3020 device = dasd_device_from_cdev(cdev);
3021 if (IS_ERR(device))
3022 return;
3023 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3024 /* Already doing offline processing */
3025 dasd_put_device(device);
3026 return;
3027 }
3028 /*
3029 * This device is removed unconditionally. Set offline
3030 * flag to prevent dasd_open from opening it while it is
3031 * no quite down yet.
3032 */
3033 dasd_set_target_state(device, DASD_STATE_NEW);
3034 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003035 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003037 /*
3038 * life cycle of block is bound to device, so delete it after
3039 * device was safely removed
3040 */
3041 if (block)
3042 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003045/*
3046 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003048 * or the user has started activation through sysfs.
3049 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003050int dasd_generic_set_online(struct ccw_device *cdev,
3051 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003053 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003055 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003056
Horst Hummel40545572006-06-29 15:08:18 +02003057 /* first online clears initial online feature flag */
3058 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 device = dasd_create_device(cdev);
3060 if (IS_ERR(device))
3061 return PTR_ERR(device);
3062
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003063 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003064 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003066 pr_warning("%s Setting the DASD online failed because "
3067 "of missing DIAG discipline\n",
3068 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 dasd_delete_device(device);
3070 return -ENODEV;
3071 }
3072 discipline = dasd_diag_discipline_pointer;
3073 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003074 if (!try_module_get(base_discipline->owner)) {
3075 dasd_delete_device(device);
3076 return -EINVAL;
3077 }
3078 if (!try_module_get(discipline->owner)) {
3079 module_put(base_discipline->owner);
3080 dasd_delete_device(device);
3081 return -EINVAL;
3082 }
3083 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 device->discipline = discipline;
3085
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003086 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 rc = discipline->check_device(device);
3088 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003089 pr_warning("%s Setting the DASD online with discipline %s "
3090 "failed with rc=%i\n",
3091 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003092 module_put(discipline->owner);
3093 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 dasd_delete_device(device);
3095 return rc;
3096 }
3097
3098 dasd_set_target_state(device, DASD_STATE_ONLINE);
3099 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003100 pr_warning("%s Setting the DASD online failed because of a "
3101 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 rc = -ENODEV;
3103 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003104 if (device->block)
3105 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 dasd_delete_device(device);
3107 } else
3108 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003109 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003110
3111 wait_event(dasd_init_waitq, _wait_for_device(device));
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return rc;
3115}
3116
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003117int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003120 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07003121 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 device = dasd_device_from_cdev(cdev);
3124 if (IS_ERR(device))
3125 return PTR_ERR(device);
3126 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3127 /* Already doing offline processing */
3128 dasd_put_device(device);
3129 return 0;
3130 }
3131 /*
3132 * We must make sure that this device is currently not in use.
3133 * The open_count is increased for every opener, that includes
3134 * the blkdev_get in dasd_scan_partitions. We are only interested
3135 * in the other openers.
3136 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003137 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003138 max_count = device->block->bdev ? 0 : -1;
3139 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003140 if (open_count > max_count) {
3141 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003142 pr_warning("%s: The DASD cannot be set offline "
3143 "with open count %i\n",
3144 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003145 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003146 pr_warning("%s: The DASD cannot be set offline "
3147 "while it is in use\n",
3148 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003149 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3150 dasd_put_device(device);
3151 return -EBUSY;
3152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154 dasd_set_target_state(device, DASD_STATE_NEW);
3155 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003156 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003158 /*
3159 * life cycle of block is bound to device, so delete it after
3160 * device was safely removed
3161 */
3162 if (block)
3163 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return 0;
3165}
3166
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003167int dasd_generic_last_path_gone(struct dasd_device *device)
3168{
3169 struct dasd_ccw_req *cqr;
3170
3171 dev_warn(&device->cdev->dev, "No operational channel path is left "
3172 "for the device\n");
3173 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3174 /* First of all call extended error reporting. */
3175 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3176
3177 if (device->state < DASD_STATE_BASIC)
3178 return 0;
3179 /* Device is active. We want to keep it. */
3180 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3181 if ((cqr->status == DASD_CQR_IN_IO) ||
3182 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3183 cqr->status = DASD_CQR_QUEUED;
3184 cqr->retries++;
3185 }
3186 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3187 dasd_device_clear_timer(device);
3188 dasd_schedule_device_bh(device);
3189 return 1;
3190}
3191EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3192
3193int dasd_generic_path_operational(struct dasd_device *device)
3194{
3195 dev_info(&device->cdev->dev, "A channel path to the device has become "
3196 "operational\n");
3197 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3198 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3199 if (device->stopped & DASD_UNRESUMED_PM) {
3200 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3201 dasd_restore_device(device);
3202 return 1;
3203 }
3204 dasd_schedule_device_bh(device);
3205 if (device->block)
3206 dasd_schedule_block_bh(device->block);
3207 return 1;
3208}
3209EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3210
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003211int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 int ret;
3215
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003216 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (IS_ERR(device))
3218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 ret = 0;
3220 switch (event) {
3221 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003222 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003224 device->path_data.opm = 0;
3225 device->path_data.ppm = 0;
3226 device->path_data.npm = 0;
3227 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 break;
3229 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003231 if (device->path_data.opm)
3232 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 break;
3234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 dasd_put_device(device);
3236 return ret;
3237}
3238
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003239void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3240{
3241 int chp;
3242 __u8 oldopm, eventlpm;
3243 struct dasd_device *device;
3244
3245 device = dasd_device_from_cdev_locked(cdev);
3246 if (IS_ERR(device))
3247 return;
3248 for (chp = 0; chp < 8; chp++) {
3249 eventlpm = 0x80 >> chp;
3250 if (path_event[chp] & PE_PATH_GONE) {
3251 oldopm = device->path_data.opm;
3252 device->path_data.opm &= ~eventlpm;
3253 device->path_data.ppm &= ~eventlpm;
3254 device->path_data.npm &= ~eventlpm;
3255 if (oldopm && !device->path_data.opm)
3256 dasd_generic_last_path_gone(device);
3257 }
3258 if (path_event[chp] & PE_PATH_AVAILABLE) {
3259 device->path_data.opm &= ~eventlpm;
3260 device->path_data.ppm &= ~eventlpm;
3261 device->path_data.npm &= ~eventlpm;
3262 device->path_data.tbvpm |= eventlpm;
3263 dasd_schedule_device_bh(device);
3264 }
3265 }
3266 dasd_put_device(device);
3267}
3268EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3269
3270int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3271{
3272 if (!device->path_data.opm && lpm) {
3273 device->path_data.opm = lpm;
3274 dasd_generic_path_operational(device);
3275 } else
3276 device->path_data.opm |= lpm;
3277 return 0;
3278}
3279EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3280
3281
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003282int dasd_generic_pm_freeze(struct ccw_device *cdev)
3283{
3284 struct dasd_ccw_req *cqr, *n;
3285 int rc;
3286 struct list_head freeze_queue;
3287 struct dasd_device *device = dasd_device_from_cdev(cdev);
3288
3289 if (IS_ERR(device))
3290 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003291
3292 if (device->discipline->freeze)
3293 rc = device->discipline->freeze(device);
3294
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003295 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003296 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003297 /* clear active requests */
3298 INIT_LIST_HEAD(&freeze_queue);
3299 spin_lock_irq(get_ccwdev_lock(cdev));
3300 rc = 0;
3301 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3302 /* Check status and move request to flush_queue */
3303 if (cqr->status == DASD_CQR_IN_IO) {
3304 rc = device->discipline->term_IO(cqr);
3305 if (rc) {
3306 /* unable to terminate requeust */
3307 dev_err(&device->cdev->dev,
3308 "Unable to terminate request %p "
3309 "on suspend\n", cqr);
3310 spin_unlock_irq(get_ccwdev_lock(cdev));
3311 dasd_put_device(device);
3312 return rc;
3313 }
3314 }
3315 list_move_tail(&cqr->devlist, &freeze_queue);
3316 }
3317
3318 spin_unlock_irq(get_ccwdev_lock(cdev));
3319
3320 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3321 wait_event(dasd_flush_wq,
3322 (cqr->status != DASD_CQR_CLEAR_PENDING));
3323 if (cqr->status == DASD_CQR_CLEARED)
3324 cqr->status = DASD_CQR_QUEUED;
3325 }
3326 /* move freeze_queue to start of the ccw_queue */
3327 spin_lock_irq(get_ccwdev_lock(cdev));
3328 list_splice_tail(&freeze_queue, &device->ccw_queue);
3329 spin_unlock_irq(get_ccwdev_lock(cdev));
3330
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003331 dasd_put_device(device);
3332 return rc;
3333}
3334EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3335
3336int dasd_generic_restore_device(struct ccw_device *cdev)
3337{
3338 struct dasd_device *device = dasd_device_from_cdev(cdev);
3339 int rc = 0;
3340
3341 if (IS_ERR(device))
3342 return PTR_ERR(device);
3343
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003344 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003345 dasd_device_remove_stop_bits(device,
3346 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003347
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003348 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003349
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003350 /*
3351 * call discipline restore function
3352 * if device is stopped do nothing e.g. for disconnected devices
3353 */
3354 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003355 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003356 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003357 /*
3358 * if the resume failed for the DASD we put it in
3359 * an UNRESUMED stop state
3360 */
3361 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003362
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003363 if (device->block)
3364 dasd_schedule_block_bh(device->block);
3365
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003366 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003367 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003368}
3369EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3370
Heiko Carstens763968e2007-05-10 15:45:46 +02003371static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3372 void *rdc_buffer,
3373 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003374 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003375{
3376 struct dasd_ccw_req *cqr;
3377 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003378 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003379
3380 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3381
3382 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003383 /* internal error 13 - Allocating the RDC request failed*/
3384 dev_err(&device->cdev->dev,
3385 "An error occurred in the DASD device driver, "
3386 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003387 return cqr;
3388 }
3389
3390 ccw = cqr->cpaddr;
3391 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003392 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3393 idaw = (unsigned long *) (cqr->data);
3394 ccw->cda = (__u32)(addr_t) idaw;
3395 ccw->flags = CCW_FLAG_IDA;
3396 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3397 } else {
3398 ccw->cda = (__u32)(addr_t) rdc_buffer;
3399 ccw->flags = 0;
3400 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003401
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003402 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003403 cqr->startdev = device;
3404 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003405 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003406 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02003407 cqr->buildclk = get_clock();
3408 cqr->status = DASD_CQR_FILLED;
3409 return cqr;
3410}
3411
3412
Stefan Haberland68b781f2009-09-11 10:28:29 +02003413int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003414 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003415{
3416 int ret;
3417 struct dasd_ccw_req *cqr;
3418
Sebastian Ott92636b12009-06-12 10:26:37 +02003419 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003420 magic);
3421 if (IS_ERR(cqr))
3422 return PTR_ERR(cqr);
3423
3424 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003425 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003426 return ret;
3427}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003428EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003429
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003430/*
3431 * In command mode and transport mode we need to look for sense
3432 * data in different places. The sense data itself is allways
3433 * an array of 32 bytes, so we can unify the sense data access
3434 * for both modes.
3435 */
3436char *dasd_get_sense(struct irb *irb)
3437{
3438 struct tsb *tsb = NULL;
3439 char *sense = NULL;
3440
3441 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3442 if (irb->scsw.tm.tcw)
3443 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3444 irb->scsw.tm.tcw);
3445 if (tsb && tsb->length == 64 && tsb->flags)
3446 switch (tsb->flags & 0x07) {
3447 case 1: /* tsa_iostat */
3448 sense = tsb->tsa.iostat.sense;
3449 break;
3450 case 2: /* tsa_ddpc */
3451 sense = tsb->tsa.ddpc.sense;
3452 break;
3453 default:
3454 /* currently we don't use interrogate data */
3455 break;
3456 }
3457 } else if (irb->esw.esw0.erw.cons) {
3458 sense = irb->ecw;
3459 }
3460 return sense;
3461}
3462EXPORT_SYMBOL_GPL(dasd_get_sense);
3463
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003464static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
3466 int rc;
3467
3468 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003469 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003470 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003473 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (dasd_debug_area == NULL) {
3475 rc = -ENOMEM;
3476 goto failed;
3477 }
3478 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003479 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3482
3483 dasd_diag_discipline_pointer = NULL;
3484
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003485 dasd_statistics_createroot();
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 rc = dasd_devmap_init();
3488 if (rc)
3489 goto failed;
3490 rc = dasd_gendisk_init();
3491 if (rc)
3492 goto failed;
3493 rc = dasd_parse();
3494 if (rc)
3495 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003496 rc = dasd_eer_init();
3497 if (rc)
3498 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499#ifdef CONFIG_PROC_FS
3500 rc = dasd_proc_init();
3501 if (rc)
3502 goto failed;
3503#endif
3504
3505 return 0;
3506failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003507 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 dasd_exit();
3509 return rc;
3510}
3511
3512module_init(dasd_init);
3513module_exit(dasd_exit);
3514
3515EXPORT_SYMBOL(dasd_debug_area);
3516EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3517
3518EXPORT_SYMBOL(dasd_add_request_head);
3519EXPORT_SYMBOL(dasd_add_request_tail);
3520EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003521EXPORT_SYMBOL(dasd_device_clear_timer);
3522EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523EXPORT_SYMBOL(dasd_enable_device);
3524EXPORT_SYMBOL(dasd_int_handler);
3525EXPORT_SYMBOL(dasd_kfree_request);
3526EXPORT_SYMBOL(dasd_kick_device);
3527EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003528EXPORT_SYMBOL(dasd_schedule_device_bh);
3529EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003531EXPORT_SYMBOL(dasd_device_set_timer);
3532EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533EXPORT_SYMBOL(dasd_sfree_request);
3534EXPORT_SYMBOL(dasd_sleep_on);
3535EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3536EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3537EXPORT_SYMBOL(dasd_smalloc_request);
3538EXPORT_SYMBOL(dasd_start_IO);
3539EXPORT_SYMBOL(dasd_term_IO);
3540
3541EXPORT_SYMBOL_GPL(dasd_generic_probe);
3542EXPORT_SYMBOL_GPL(dasd_generic_remove);
3543EXPORT_SYMBOL_GPL(dasd_generic_notify);
3544EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3545EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003546EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3547EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3548EXPORT_SYMBOL_GPL(dasd_alloc_block);
3549EXPORT_SYMBOL_GPL(dasd_free_block);