blob: a1d3ddba99ccad4b90d4a9a06af1fd371b2d0033 [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
Heiko Carstens32839422011-01-05 12:47:30 +010014#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kmod.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ctype.h>
19#include <linux/major.h>
20#include <linux/slab.h>
21#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080022#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020023#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010024#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020025#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020027#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/ccwdev.h>
30#include <asm/ebcdic.h>
31#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010032#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010033#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* This is ugly... */
36#define PRINTK_HEADER "dasd:"
37
38#include "dasd_int.h"
39/*
40 * SECTION: Constant definitions to be used within this file
41 */
42#define DASD_CHANQ_MAX_SIZE 4
43
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +020044#define DASD_SLEEPON_START_TAG (void *) 1
45#define DASD_SLEEPON_END_TAG (void *) 2
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * SECTION: exported variables of dasd.c
49 */
50debug_info_t *dasd_debug_area;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020051static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010053void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
56MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
57 " Copyright 2000 IBM Corporation");
58MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_LICENSE("GPL");
60
61/*
62 * SECTION: prototypes for static functions of dasd.c
63 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010064static int dasd_alloc_queue(struct dasd_block *);
65static void dasd_setup_queue(struct dasd_block *);
66static void dasd_free_queue(struct dasd_block *);
67static void dasd_flush_request_queue(struct dasd_block *);
68static int dasd_flush_block_queue(struct dasd_block *);
69static void dasd_device_tasklet(struct dasd_device *);
70static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000071static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020072static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020073static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010074static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010075static void dasd_device_timeout(unsigned long);
76static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010077static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020078static void dasd_profile_init(struct dasd_profile *, struct dentry *);
79static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * SECTION: Operations on the device structure.
83 */
84static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020085static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020086static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
89 * Allocate memory for a new device structure.
90 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010091struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct dasd_device *device;
94
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010095 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
96 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* Get two pages for normal block device operations. */
100 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100101 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 kfree(device);
103 return ERR_PTR(-ENOMEM);
104 }
105 /* Get one page for error recovery. */
106 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100107 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 free_pages((unsigned long) device->ccw_mem, 1);
109 kfree(device);
110 return ERR_PTR(-ENOMEM);
111 }
112
113 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
114 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
115 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100116 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200117 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100118 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 (unsigned long) device);
120 INIT_LIST_HEAD(&device->ccw_queue);
121 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100122 device->timer.function = dasd_device_timeout;
123 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000124 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200125 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200126 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 device->state = DASD_STATE_NEW;
128 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100129 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200130 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return device;
132}
133
134/*
135 * Free memory of a device structure.
136 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100137void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800139 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 free_page((unsigned long) device->erp_mem);
141 free_pages((unsigned long) device->ccw_mem, 1);
142 kfree(device);
143}
144
145/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100146 * Allocate memory for a new device structure.
147 */
148struct dasd_block *dasd_alloc_block(void)
149{
150 struct dasd_block *block;
151
152 block = kzalloc(sizeof(*block), GFP_ATOMIC);
153 if (!block)
154 return ERR_PTR(-ENOMEM);
155 /* open_count = 0 means device online but not in use */
156 atomic_set(&block->open_count, -1);
157
158 spin_lock_init(&block->request_queue_lock);
159 atomic_set(&block->tasklet_scheduled, 0);
160 tasklet_init(&block->tasklet,
161 (void (*)(unsigned long)) dasd_block_tasklet,
162 (unsigned long) block);
163 INIT_LIST_HEAD(&block->ccw_queue);
164 spin_lock_init(&block->queue_lock);
165 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100166 block->timer.function = dasd_block_timeout;
167 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200168 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100169
170 return block;
171}
172
173/*
174 * Free memory of a device structure.
175 */
176void dasd_free_block(struct dasd_block *block)
177{
178 kfree(block);
179}
180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Make a new device known to the system.
183 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100184static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 int rc;
187
188 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200189 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * keep the reference count > 0.
191 */
192 dasd_get_device(device);
193
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100194 if (device->block) {
195 rc = dasd_alloc_queue(device->block);
196 if (rc) {
197 dasd_put_device(device);
198 return rc;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 device->state = DASD_STATE_KNOWN;
202 return 0;
203}
204
205/*
206 * Let the system forget about a device.
207 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100208static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800210 /* Disable extended error reporting for this device. */
211 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100213 if (device->discipline) {
214 if (device->discipline->uncheck_device)
215 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800216 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800219 if (device->base_discipline)
220 module_put(device->base_discipline->owner);
221 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 device->state = DASD_STATE_NEW;
223
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100224 if (device->block)
225 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Give up reference we took in dasd_state_new_to_known. */
228 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200232static struct dentry *dasd_debugfs_setup(const char *name,
233 struct dentry *base_dentry)
234{
235 struct dentry *pde;
236
237 if (!base_dentry)
238 return NULL;
239 pde = debugfs_create_dir(name, base_dentry);
240 if (!pde || IS_ERR(pde))
241 return NULL;
242 return pde;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * Request the irq line for the device.
247 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100248static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200250 struct dasd_block *block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int rc;
252
253 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200254 if (block) {
255 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100256 if (rc)
257 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200258 block->debugfs_dentry =
259 dasd_debugfs_setup(block->gdp->disk_name,
260 dasd_debugfs_root_entry);
261 dasd_profile_init(&block->profile, block->debugfs_dentry);
262 if (dasd_global_profile_level == DASD_PROFILE_ON)
263 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100264 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200265 device->debugfs_dentry =
266 dasd_debugfs_setup(dev_name(&device->cdev->dev),
267 dasd_debugfs_root_entry);
268 dasd_profile_init(&device->profile, device->debugfs_dentry);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100271 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100272 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200274 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
276
277 device->state = DASD_STATE_BASIC;
278 return 0;
279}
280
281/*
282 * Release the irq line for the device. Terminate any running i/o.
283 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Horst Hummel8f617012006-08-30 14:33:33 +0200286 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100287 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200288 dasd_profile_exit(&device->block->profile);
289 if (device->block->debugfs_dentry)
290 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100291 dasd_gendisk_free(device->block);
292 dasd_block_clear_timer(device->block);
293 }
294 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200295 if (rc)
296 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100297 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200298 dasd_profile_exit(&device->profile);
299 if (device->debugfs_dentry)
300 debugfs_remove(device->debugfs_dentry);
Horst Hummel8f617012006-08-30 14:33:33 +0200301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
303 if (device->debug_area != NULL) {
304 debug_unregister(device->debug_area);
305 device->debug_area = NULL;
306 }
307 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
312 * Do the initial analysis. The do_analysis function may return
313 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
314 * until the discipline decides to continue the startup sequence
315 * by calling the function dasd_change_state. The eckd disciplines
316 * uses this to start a ccw that detects the format. The completion
317 * interrupt for this detection ccw uses the kernel event daemon to
318 * trigger the call to dasd_change_state. All this is done in the
319 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800320 * After the analysis ccw is done (do_analysis returned 0) the block
321 * device is setup.
322 * In case the analysis returns an error, the device setup is stopped
323 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100328 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100331 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800332 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100333 if (block) {
334 if (block->base->discipline->do_analysis != NULL)
335 rc = block->base->discipline->do_analysis(block);
336 if (rc) {
337 if (rc != -EAGAIN)
338 device->state = DASD_STATE_UNFMT;
339 return rc;
340 }
341 dasd_setup_queue(block);
342 set_capacity(block->gdp,
343 block->blocks << block->s2b_shift);
344 device->state = DASD_STATE_READY;
345 rc = dasd_scan_partitions(block);
346 if (rc)
347 device->state = DASD_STATE_BASIC;
348 } else {
349 device->state = DASD_STATE_READY;
350 }
Horst Hummel90f00942006-03-07 21:55:39 -0800351 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/*
355 * Remove device from block device layer. Destroy dirty buffers.
356 * Forget format information. Check if the target level is basic
357 * and if it is create fake disk for formatting.
358 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100359static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Horst Hummel8f617012006-08-30 14:33:33 +0200361 int rc;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100364 if (device->block) {
365 struct dasd_block *block = device->block;
366 rc = dasd_flush_block_queue(block);
367 if (rc) {
368 device->state = DASD_STATE_READY;
369 return rc;
370 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100371 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100372 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100373 block->blocks = 0;
374 block->bp_block = 0;
375 block->s2b_shift = 0;
376 }
Horst Hummel8f617012006-08-30 14:33:33 +0200377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Horst Hummel90f00942006-03-07 21:55:39 -0800381 * Back to basic.
382 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100383static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800384{
385 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200386 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800387}
388
389/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * Make the device online and schedule the bottom half to start
391 * the requeueing of requests from the linux request queue to the
392 * ccw queue.
393 */
Horst Hummel8f617012006-08-30 14:33:33 +0200394static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395dasd_state_ready_to_online(struct dasd_device * device)
396{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100397 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100398 struct gendisk *disk;
399 struct disk_part_iter piter;
400 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100401
402 if (device->discipline->ready_to_online) {
403 rc = device->discipline->ready_to_online(device);
404 if (rc)
405 return rc;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100408 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100409 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100410 if ((device->features & DASD_FEATURE_USERAW)) {
411 disk = device->block->gdp;
412 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
413 return 0;
414 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100415 disk = device->block->bdev->bd_disk;
416 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
417 while ((part = disk_part_iter_next(&piter)))
418 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
419 disk_part_iter_exit(&piter);
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423
424/*
425 * Stop the requeueing of requests again.
426 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100427static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100429 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100430 struct gendisk *disk;
431 struct disk_part_iter piter;
432 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100433
434 if (device->discipline->online_to_ready) {
435 rc = device->discipline->online_to_ready(device);
436 if (rc)
437 return rc;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100440 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100441 disk = device->block->bdev->bd_disk;
442 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
443 while ((part = disk_part_iter_next(&piter)))
444 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
445 disk_part_iter_exit(&piter);
446 }
Horst Hummel8f617012006-08-30 14:33:33 +0200447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * Device startup state changes.
452 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100453static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int rc;
456
457 rc = 0;
458 if (device->state == DASD_STATE_NEW &&
459 device->target >= DASD_STATE_KNOWN)
460 rc = dasd_state_new_to_known(device);
461
462 if (!rc &&
463 device->state == DASD_STATE_KNOWN &&
464 device->target >= DASD_STATE_BASIC)
465 rc = dasd_state_known_to_basic(device);
466
467 if (!rc &&
468 device->state == DASD_STATE_BASIC &&
469 device->target >= DASD_STATE_READY)
470 rc = dasd_state_basic_to_ready(device);
471
472 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700473 device->state == DASD_STATE_UNFMT &&
474 device->target > DASD_STATE_UNFMT)
475 rc = -EPERM;
476
477 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 device->state == DASD_STATE_READY &&
479 device->target >= DASD_STATE_ONLINE)
480 rc = dasd_state_ready_to_online(device);
481
482 return rc;
483}
484
485/*
486 * Device shutdown state changes.
487 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100488static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Horst Hummel8f617012006-08-30 14:33:33 +0200490 int rc;
491
492 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (device->state == DASD_STATE_ONLINE &&
494 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200495 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200496
Horst Hummel8f617012006-08-30 14:33:33 +0200497 if (!rc &&
498 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200500 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800501
Horst Hummel8f617012006-08-30 14:33:33 +0200502 if (!rc &&
503 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800504 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200505 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800506
Horst Hummel8f617012006-08-30 14:33:33 +0200507 if (!rc &&
508 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200510 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200511
Horst Hummel8f617012006-08-30 14:33:33 +0200512 if (!rc &&
513 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200515 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Horst Hummel8f617012006-08-30 14:33:33 +0200517 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520/*
521 * This is the main startup/shutdown routine.
522 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100523static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Sebastian Ott181d9522009-06-22 12:08:21 +0200525 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (device->state == device->target)
528 /* Already where we want to go today... */
529 return;
530 if (device->state < device->target)
531 rc = dasd_increase_state(device);
532 else
533 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200534 if (rc == -EAGAIN)
535 return;
536 if (rc)
537 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Stefan Haberland9eb25122010-02-26 22:37:46 +0100539 if (device->state == device->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200541
542 /* let user-space know that the device status changed */
543 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
547 * Kick starter for devices that did not complete the startup/shutdown
548 * procedure or were sleeping because of a pending state.
549 * dasd_kick_device will schedule a call do do_kick_device to the kernel
550 * event daemon.
551 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100552static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Al Viro4927b3f2006-12-06 19:18:20 +0000554 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100555 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100557 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100558 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 dasd_put_device(device);
560}
561
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100562void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 dasd_get_device(device);
565 /* queue call to dasd_kick_device to the kernel event daemon. */
566 schedule_work(&device->kick_work);
567}
568
569/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200570 * dasd_reload_device will schedule a call do do_reload_device to the kernel
571 * event daemon.
572 */
573static void do_reload_device(struct work_struct *work)
574{
575 struct dasd_device *device = container_of(work, struct dasd_device,
576 reload_device);
577 device->discipline->reload(device);
578 dasd_put_device(device);
579}
580
581void dasd_reload_device(struct dasd_device *device)
582{
583 dasd_get_device(device);
584 /* queue call to dasd_reload_device to the kernel event daemon. */
585 schedule_work(&device->reload_device);
586}
587EXPORT_SYMBOL(dasd_reload_device);
588
589/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200590 * dasd_restore_device will schedule a call do do_restore_device to the kernel
591 * event daemon.
592 */
593static void do_restore_device(struct work_struct *work)
594{
595 struct dasd_device *device = container_of(work, struct dasd_device,
596 restore_device);
597 device->cdev->drv->restore(device->cdev);
598 dasd_put_device(device);
599}
600
601void dasd_restore_device(struct dasd_device *device)
602{
603 dasd_get_device(device);
604 /* queue call to dasd_restore_device to the kernel event daemon. */
605 schedule_work(&device->restore_device);
606}
607
608/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * Set the target state for a device and starts the state change.
610 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100611void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200613 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100614 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* If we are in probeonly mode stop at DASD_STATE_READY. */
616 if (dasd_probeonly && target > DASD_STATE_READY)
617 target = DASD_STATE_READY;
618 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100619 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 wake_up(&dasd_init_waitq);
621 device->target = target;
622 }
623 if (device->state != device->target)
624 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100625 mutex_unlock(&device->state_mutex);
626 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
630 * Enable devices with device numbers in [from..to].
631 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100632static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 return (device->state == device->target);
635}
636
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100637void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 dasd_set_target_state(device, DASD_STATE_ONLINE);
640 if (device->state <= DASD_STATE_KNOWN)
641 /* No discipline for device found. */
642 dasd_set_target_state(device, DASD_STATE_NEW);
643 /* Now wait for the devices to come up. */
644 wait_event(dasd_init_waitq, _wait_for_device(device));
645}
646
647/*
648 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
649 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200650
651unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#ifdef CONFIG_DASD_PROFILE
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200654struct dasd_profile_info dasd_global_profile_data;
655static struct dentry *dasd_global_profile_dentry;
656static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658/*
659 * Add profiling information for cqr before execution.
660 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100661static void dasd_profile_start(struct dasd_block *block,
662 struct dasd_ccw_req *cqr,
663 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct list_head *l;
666 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200667 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* count the length of the chanq for statistics */
670 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200671 if (dasd_global_profile_level || block->profile.data)
672 list_for_each(l, &block->ccw_queue)
673 if (++counter >= 31)
674 break;
675
676 if (dasd_global_profile_level) {
677 dasd_global_profile_data.dasd_io_nr_req[counter]++;
678 if (rq_data_dir(req) == READ)
679 dasd_global_profile_data.dasd_read_nr_req[counter]++;
680 }
681
682 spin_lock(&block->profile.lock);
683 if (block->profile.data)
684 block->profile.data->dasd_io_nr_req[counter]++;
685 if (rq_data_dir(req) == READ)
686 block->profile.data->dasd_read_nr_req[counter]++;
687 spin_unlock(&block->profile.lock);
688
689 /*
690 * We count the request for the start device, even though it may run on
691 * some other device due to error recovery. This way we make sure that
692 * we count each request only once.
693 */
694 device = cqr->startdev;
695 if (device->profile.data) {
696 counter = 1; /* request is not yet queued on the start device */
697 list_for_each(l, &device->ccw_queue)
698 if (++counter >= 31)
699 break;
700 }
701 spin_lock(&device->profile.lock);
702 if (device->profile.data) {
703 device->profile.data->dasd_io_nr_req[counter]++;
704 if (rq_data_dir(req) == READ)
705 device->profile.data->dasd_read_nr_req[counter]++;
706 }
707 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * Add profiling information for cqr after execution.
712 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200713
714#define dasd_profile_counter(value, index) \
715{ \
716 for (index = 0; index < 31 && value >> (2+index); index++) \
717 ; \
718}
719
720static void dasd_profile_end_add_data(struct dasd_profile_info *data,
721 int is_alias,
722 int is_tpm,
723 int is_read,
724 long sectors,
725 int sectors_ind,
726 int tottime_ind,
727 int tottimeps_ind,
728 int strtime_ind,
729 int irqtime_ind,
730 int irqtimeps_ind,
731 int endtime_ind)
732{
733 /* in case of an overflow, reset the whole profile */
734 if (data->dasd_io_reqs == UINT_MAX) {
735 memset(data, 0, sizeof(*data));
736 getnstimeofday(&data->starttod);
737 }
738 data->dasd_io_reqs++;
739 data->dasd_io_sects += sectors;
740 if (is_alias)
741 data->dasd_io_alias++;
742 if (is_tpm)
743 data->dasd_io_tpm++;
744
745 data->dasd_io_secs[sectors_ind]++;
746 data->dasd_io_times[tottime_ind]++;
747 data->dasd_io_timps[tottimeps_ind]++;
748 data->dasd_io_time1[strtime_ind]++;
749 data->dasd_io_time2[irqtime_ind]++;
750 data->dasd_io_time2ps[irqtimeps_ind]++;
751 data->dasd_io_time3[endtime_ind]++;
752
753 if (is_read) {
754 data->dasd_read_reqs++;
755 data->dasd_read_sects += sectors;
756 if (is_alias)
757 data->dasd_read_alias++;
758 if (is_tpm)
759 data->dasd_read_tpm++;
760 data->dasd_read_secs[sectors_ind]++;
761 data->dasd_read_times[tottime_ind]++;
762 data->dasd_read_time1[strtime_ind]++;
763 data->dasd_read_time2[irqtime_ind]++;
764 data->dasd_read_time3[endtime_ind]++;
765 }
766}
767
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100768static void dasd_profile_end(struct dasd_block *block,
769 struct dasd_ccw_req *cqr,
770 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 long strtime, irqtime, endtime, tottime; /* in microseconds */
773 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200774 struct dasd_device *device;
775 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
776 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200778 device = cqr->startdev;
779 if (!(dasd_global_profile_level ||
780 block->profile.data ||
781 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return;
783
Tejun Heo83096eb2009-05-07 22:24:39 +0900784 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!cqr->buildclk || !cqr->startclk ||
786 !cqr->stopclk || !cqr->endclk ||
787 !sectors)
788 return;
789
790 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
791 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
792 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
793 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
794 tottimeps = tottime / sectors;
795
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200796 dasd_profile_counter(sectors, sectors_ind);
797 dasd_profile_counter(tottime, tottime_ind);
798 dasd_profile_counter(tottimeps, tottimeps_ind);
799 dasd_profile_counter(strtime, strtime_ind);
800 dasd_profile_counter(irqtime, irqtime_ind);
801 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
802 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200804 if (dasd_global_profile_level) {
805 dasd_profile_end_add_data(&dasd_global_profile_data,
806 cqr->startdev != block->base,
807 cqr->cpmode == 1,
808 rq_data_dir(req) == READ,
809 sectors, sectors_ind, tottime_ind,
810 tottimeps_ind, strtime_ind,
811 irqtime_ind, irqtimeps_ind,
812 endtime_ind);
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200815 spin_lock(&block->profile.lock);
816 if (block->profile.data)
817 dasd_profile_end_add_data(block->profile.data,
818 cqr->startdev != block->base,
819 cqr->cpmode == 1,
820 rq_data_dir(req) == READ,
821 sectors, sectors_ind, tottime_ind,
822 tottimeps_ind, strtime_ind,
823 irqtime_ind, irqtimeps_ind,
824 endtime_ind);
825 spin_unlock(&block->profile.lock);
826
827 spin_lock(&device->profile.lock);
828 if (device->profile.data)
829 dasd_profile_end_add_data(device->profile.data,
830 cqr->startdev != block->base,
831 cqr->cpmode == 1,
832 rq_data_dir(req) == READ,
833 sectors, sectors_ind, tottime_ind,
834 tottimeps_ind, strtime_ind,
835 irqtime_ind, irqtimeps_ind,
836 endtime_ind);
837 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200839
840void dasd_profile_reset(struct dasd_profile *profile)
841{
842 struct dasd_profile_info *data;
843
844 spin_lock_bh(&profile->lock);
845 data = profile->data;
846 if (!data) {
847 spin_unlock_bh(&profile->lock);
848 return;
849 }
850 memset(data, 0, sizeof(*data));
851 getnstimeofday(&data->starttod);
852 spin_unlock_bh(&profile->lock);
853}
854
855void dasd_global_profile_reset(void)
856{
857 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
858 getnstimeofday(&dasd_global_profile_data.starttod);
859}
860
861int dasd_profile_on(struct dasd_profile *profile)
862{
863 struct dasd_profile_info *data;
864
865 data = kzalloc(sizeof(*data), GFP_KERNEL);
866 if (!data)
867 return -ENOMEM;
868 spin_lock_bh(&profile->lock);
869 if (profile->data) {
870 spin_unlock_bh(&profile->lock);
871 kfree(data);
872 return 0;
873 }
874 getnstimeofday(&data->starttod);
875 profile->data = data;
876 spin_unlock_bh(&profile->lock);
877 return 0;
878}
879
880void dasd_profile_off(struct dasd_profile *profile)
881{
882 spin_lock_bh(&profile->lock);
883 kfree(profile->data);
884 profile->data = NULL;
885 spin_unlock_bh(&profile->lock);
886}
887
888char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
889{
890 char *buffer;
891
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200892 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200893 if (buffer == NULL)
894 return ERR_PTR(-ENOMEM);
895 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200896 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200897 return ERR_PTR(-EFAULT);
898 }
899 /* got the string, now strip linefeed. */
900 if (buffer[user_len - 1] == '\n')
901 buffer[user_len - 1] = 0;
902 else
903 buffer[user_len] = 0;
904 return buffer;
905}
906
907static ssize_t dasd_stats_write(struct file *file,
908 const char __user *user_buf,
909 size_t user_len, loff_t *pos)
910{
911 char *buffer, *str;
912 int rc;
913 struct seq_file *m = (struct seq_file *)file->private_data;
914 struct dasd_profile *prof = m->private;
915
916 if (user_len > 65536)
917 user_len = 65536;
918 buffer = dasd_get_user_string(user_buf, user_len);
919 if (IS_ERR(buffer))
920 return PTR_ERR(buffer);
921
922 str = skip_spaces(buffer);
923 rc = user_len;
924 if (strncmp(str, "reset", 5) == 0) {
925 dasd_profile_reset(prof);
926 } else if (strncmp(str, "on", 2) == 0) {
927 rc = dasd_profile_on(prof);
928 if (!rc)
929 rc = user_len;
930 } else if (strncmp(str, "off", 3) == 0) {
931 dasd_profile_off(prof);
932 } else
933 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200934 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200935 return rc;
936}
937
938static void dasd_stats_array(struct seq_file *m, unsigned int *array)
939{
940 int i;
941
942 for (i = 0; i < 32; i++)
943 seq_printf(m, "%u ", array[i]);
944 seq_putc(m, '\n');
945}
946
947static void dasd_stats_seq_print(struct seq_file *m,
948 struct dasd_profile_info *data)
949{
950 seq_printf(m, "start_time %ld.%09ld\n",
951 data->starttod.tv_sec, data->starttod.tv_nsec);
952 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
953 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
954 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
955 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
956 seq_printf(m, "histogram_sectors ");
957 dasd_stats_array(m, data->dasd_io_secs);
958 seq_printf(m, "histogram_io_times ");
959 dasd_stats_array(m, data->dasd_io_times);
960 seq_printf(m, "histogram_io_times_weighted ");
961 dasd_stats_array(m, data->dasd_io_timps);
962 seq_printf(m, "histogram_time_build_to_ssch ");
963 dasd_stats_array(m, data->dasd_io_time1);
964 seq_printf(m, "histogram_time_ssch_to_irq ");
965 dasd_stats_array(m, data->dasd_io_time2);
966 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
967 dasd_stats_array(m, data->dasd_io_time2ps);
968 seq_printf(m, "histogram_time_irq_to_end ");
969 dasd_stats_array(m, data->dasd_io_time3);
970 seq_printf(m, "histogram_ccw_queue_length ");
971 dasd_stats_array(m, data->dasd_io_nr_req);
972 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
973 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
974 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
975 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
976 seq_printf(m, "histogram_read_sectors ");
977 dasd_stats_array(m, data->dasd_read_secs);
978 seq_printf(m, "histogram_read_times ");
979 dasd_stats_array(m, data->dasd_read_times);
980 seq_printf(m, "histogram_read_time_build_to_ssch ");
981 dasd_stats_array(m, data->dasd_read_time1);
982 seq_printf(m, "histogram_read_time_ssch_to_irq ");
983 dasd_stats_array(m, data->dasd_read_time2);
984 seq_printf(m, "histogram_read_time_irq_to_end ");
985 dasd_stats_array(m, data->dasd_read_time3);
986 seq_printf(m, "histogram_read_ccw_queue_length ");
987 dasd_stats_array(m, data->dasd_read_nr_req);
988}
989
990static int dasd_stats_show(struct seq_file *m, void *v)
991{
992 struct dasd_profile *profile;
993 struct dasd_profile_info *data;
994
995 profile = m->private;
996 spin_lock_bh(&profile->lock);
997 data = profile->data;
998 if (!data) {
999 spin_unlock_bh(&profile->lock);
1000 seq_printf(m, "disabled\n");
1001 return 0;
1002 }
1003 dasd_stats_seq_print(m, data);
1004 spin_unlock_bh(&profile->lock);
1005 return 0;
1006}
1007
1008static int dasd_stats_open(struct inode *inode, struct file *file)
1009{
1010 struct dasd_profile *profile = inode->i_private;
1011 return single_open(file, dasd_stats_show, profile);
1012}
1013
1014static const struct file_operations dasd_stats_raw_fops = {
1015 .owner = THIS_MODULE,
1016 .open = dasd_stats_open,
1017 .read = seq_read,
1018 .llseek = seq_lseek,
1019 .release = single_release,
1020 .write = dasd_stats_write,
1021};
1022
1023static ssize_t dasd_stats_global_write(struct file *file,
1024 const char __user *user_buf,
1025 size_t user_len, loff_t *pos)
1026{
1027 char *buffer, *str;
1028 ssize_t rc;
1029
1030 if (user_len > 65536)
1031 user_len = 65536;
1032 buffer = dasd_get_user_string(user_buf, user_len);
1033 if (IS_ERR(buffer))
1034 return PTR_ERR(buffer);
1035 str = skip_spaces(buffer);
1036 rc = user_len;
1037 if (strncmp(str, "reset", 5) == 0) {
1038 dasd_global_profile_reset();
1039 } else if (strncmp(str, "on", 2) == 0) {
1040 dasd_global_profile_reset();
1041 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1042 } else if (strncmp(str, "off", 3) == 0) {
1043 dasd_global_profile_level = DASD_PROFILE_OFF;
1044 } else
1045 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001046 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001047 return rc;
1048}
1049
1050static int dasd_stats_global_show(struct seq_file *m, void *v)
1051{
1052 if (!dasd_global_profile_level) {
1053 seq_printf(m, "disabled\n");
1054 return 0;
1055 }
1056 dasd_stats_seq_print(m, &dasd_global_profile_data);
1057 return 0;
1058}
1059
1060static int dasd_stats_global_open(struct inode *inode, struct file *file)
1061{
1062 return single_open(file, dasd_stats_global_show, NULL);
1063}
1064
1065static const struct file_operations dasd_stats_global_fops = {
1066 .owner = THIS_MODULE,
1067 .open = dasd_stats_global_open,
1068 .read = seq_read,
1069 .llseek = seq_lseek,
1070 .release = single_release,
1071 .write = dasd_stats_global_write,
1072};
1073
1074static void dasd_profile_init(struct dasd_profile *profile,
1075 struct dentry *base_dentry)
1076{
1077 mode_t mode;
1078 struct dentry *pde;
1079
1080 if (!base_dentry)
1081 return;
1082 profile->dentry = NULL;
1083 profile->data = NULL;
1084 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1085 pde = debugfs_create_file("statistics", mode, base_dentry,
1086 profile, &dasd_stats_raw_fops);
1087 if (pde && !IS_ERR(pde))
1088 profile->dentry = pde;
1089 return;
1090}
1091
1092static void dasd_profile_exit(struct dasd_profile *profile)
1093{
1094 dasd_profile_off(profile);
1095 if (profile->dentry) {
1096 debugfs_remove(profile->dentry);
1097 profile->dentry = NULL;
1098 }
1099}
1100
1101static void dasd_statistics_removeroot(void)
1102{
1103 dasd_global_profile_level = DASD_PROFILE_OFF;
1104 if (dasd_global_profile_dentry) {
1105 debugfs_remove(dasd_global_profile_dentry);
1106 dasd_global_profile_dentry = NULL;
1107 }
1108 if (dasd_debugfs_global_entry)
1109 debugfs_remove(dasd_debugfs_global_entry);
1110 if (dasd_debugfs_root_entry)
1111 debugfs_remove(dasd_debugfs_root_entry);
1112}
1113
1114static void dasd_statistics_createroot(void)
1115{
1116 mode_t mode;
1117 struct dentry *pde;
1118
1119 dasd_debugfs_root_entry = NULL;
1120 dasd_debugfs_global_entry = NULL;
1121 dasd_global_profile_dentry = NULL;
1122 pde = debugfs_create_dir("dasd", NULL);
1123 if (!pde || IS_ERR(pde))
1124 goto error;
1125 dasd_debugfs_root_entry = pde;
1126 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1127 if (!pde || IS_ERR(pde))
1128 goto error;
1129 dasd_debugfs_global_entry = pde;
1130
1131 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1132 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1133 NULL, &dasd_stats_global_fops);
1134 if (!pde || IS_ERR(pde))
1135 goto error;
1136 dasd_global_profile_dentry = pde;
1137 return;
1138
1139error:
1140 DBF_EVENT(DBF_ERR, "%s",
1141 "Creation of the dasd debugfs interface failed");
1142 dasd_statistics_removeroot();
1143 return;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001147#define dasd_profile_start(block, cqr, req) do {} while (0)
1148#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001149
1150static void dasd_statistics_createroot(void)
1151{
1152 return;
1153}
1154
1155static void dasd_statistics_removeroot(void)
1156{
1157 return;
1158}
1159
1160int dasd_stats_generic_show(struct seq_file *m, void *v)
1161{
1162 seq_printf(m, "Statistics are not activated in this kernel\n");
1163 return 0;
1164}
1165
1166static void dasd_profile_init(struct dasd_profile *profile,
1167 struct dentry *base_dentry)
1168{
1169 return;
1170}
1171
1172static void dasd_profile_exit(struct dasd_profile *profile)
1173{
1174 return;
1175}
1176
1177int dasd_profile_on(struct dasd_profile *profile)
1178{
1179 return 0;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#endif /* CONFIG_DASD_PROFILE */
1183
1184/*
1185 * Allocate memory for a channel program with 'cplength' channel
1186 * command words and 'datasize' additional space. There are two
1187 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1188 * memory and 2) dasd_smalloc_request uses the static ccw memory
1189 * that gets allocated for each device.
1190 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001191struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001192 int datasize,
1193 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct dasd_ccw_req *cqr;
1196
1197 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001198 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001199 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001201 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (cqr == NULL)
1203 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 cqr->cpaddr = NULL;
1205 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001206 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 GFP_ATOMIC | GFP_DMA);
1208 if (cqr->cpaddr == NULL) {
1209 kfree(cqr);
1210 return ERR_PTR(-ENOMEM);
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213 cqr->data = NULL;
1214 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001215 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001217 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 kfree(cqr);
1219 return ERR_PTR(-ENOMEM);
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001222 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1224 dasd_get_device(device);
1225 return cqr;
1226}
1227
Stefan Haberland68b781f2009-09-11 10:28:29 +02001228struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001229 int datasize,
1230 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 unsigned long flags;
1233 struct dasd_ccw_req *cqr;
1234 char *data;
1235 int size;
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1238 if (cplength > 0)
1239 size += cplength * sizeof(struct ccw1);
1240 if (datasize > 0)
1241 size += datasize;
1242 spin_lock_irqsave(&device->mem_lock, flags);
1243 cqr = (struct dasd_ccw_req *)
1244 dasd_alloc_chunk(&device->ccw_chunks, size);
1245 spin_unlock_irqrestore(&device->mem_lock, flags);
1246 if (cqr == NULL)
1247 return ERR_PTR(-ENOMEM);
1248 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1249 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1250 cqr->cpaddr = NULL;
1251 if (cplength > 0) {
1252 cqr->cpaddr = (struct ccw1 *) data;
1253 data += cplength*sizeof(struct ccw1);
1254 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1255 }
1256 cqr->data = NULL;
1257 if (datasize > 0) {
1258 cqr->data = data;
1259 memset(cqr->data, 0, datasize);
1260 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001261 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1263 dasd_get_device(device);
1264 return cqr;
1265}
1266
1267/*
1268 * Free memory of a channel program. This function needs to free all the
1269 * idal lists that might have been created by dasd_set_cda and the
1270 * struct dasd_ccw_req itself.
1271 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001272void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001274#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct ccw1 *ccw;
1276
1277 /* Clear any idals used for the request. */
1278 ccw = cqr->cpaddr;
1279 do {
1280 clear_normalized_cda(ccw);
1281 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1282#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001283 kfree(cqr->cpaddr);
1284 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 kfree(cqr);
1286 dasd_put_device(device);
1287}
1288
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001289void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 unsigned long flags;
1292
1293 spin_lock_irqsave(&device->mem_lock, flags);
1294 dasd_free_chunk(&device->ccw_chunks, cqr);
1295 spin_unlock_irqrestore(&device->mem_lock, flags);
1296 dasd_put_device(device);
1297}
1298
1299/*
1300 * Check discipline magic in cqr.
1301 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001302static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct dasd_device *device;
1305
1306 if (cqr == NULL)
1307 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001308 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001310 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 " dasd_ccw_req 0x%08x magic doesn't match"
1312 " discipline 0x%08x",
1313 cqr->magic,
1314 *(unsigned int *) device->discipline->name);
1315 return -EINVAL;
1316 }
1317 return 0;
1318}
1319
1320/*
1321 * Terminate the current i/o and set the request to clear_pending.
1322 * Timer keeps device runnig.
1323 * ccw_device_clear can fail if the i/o subsystem
1324 * is in a bad mood.
1325 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001326int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct dasd_device *device;
1329 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001330 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* Check the cqr */
1333 rc = dasd_check_cqr(cqr);
1334 if (rc)
1335 return rc;
1336 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001337 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1339 rc = ccw_device_clear(device->cdev, (long) cqr);
1340 switch (rc) {
1341 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001342 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001344 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 DBF_DEV_EVENT(DBF_DEBUG, device,
1346 "terminate cqr %p successful",
1347 cqr);
1348 break;
1349 case -ENODEV:
1350 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1351 "device gone, retry");
1352 break;
1353 case -EIO:
1354 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1355 "I/O error, retry");
1356 break;
1357 case -EINVAL:
1358 case -EBUSY:
1359 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1360 "device busy, retry later");
1361 break;
1362 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001363 /* internal error 10 - unknown rc*/
1364 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1365 dev_err(&device->cdev->dev, "An error occurred in the "
1366 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 BUG();
1368 break;
1369 }
1370 retries++;
1371 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001372 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return rc;
1374}
1375
1376/*
1377 * Start the i/o. This start_IO can fail if the channel is really busy.
1378 * In that case set up a timer to start the request later.
1379 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001380int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct dasd_device *device;
1383 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001384 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* Check the cqr */
1387 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001388 if (rc) {
1389 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001391 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001393 if (((cqr->block &&
1394 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1395 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1396 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1397 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1398 "because of stolen lock", cqr);
1399 cqr->status = DASD_CQR_ERROR;
1400 cqr->intrc = -EPERM;
1401 return -EPERM;
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001404 /* internal error 14 - start_IO run out of retries */
1405 sprintf(errorstring, "14 %p", cqr);
1406 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1407 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001408 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EIO;
1410 }
1411 cqr->startclk = get_clock();
1412 cqr->starttime = jiffies;
1413 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001414 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1415 cqr->lpm &= device->path_data.opm;
1416 if (!cqr->lpm)
1417 cqr->lpm = device->path_data.opm;
1418 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001419 if (cqr->cpmode == 1) {
1420 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1421 (long) cqr, cqr->lpm);
1422 } else {
1423 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1424 (long) cqr, cqr->lpm, 0);
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 switch (rc) {
1427 case 0:
1428 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
1430 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001431 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 "start_IO: device busy, retry later");
1433 break;
1434 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001435 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 "start_IO: request timeout, retry later");
1437 break;
1438 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001439 /* -EACCES indicates that the request used only a subset of the
1440 * available paths and all these paths are gone. If the lpm of
1441 * this request was only a subset of the opm (e.g. the ppm) then
1442 * we just do a retry with all available paths.
1443 * If we already use the full opm, something is amiss, and we
1444 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001446 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1447 DBF_DEV_EVENT(DBF_WARNING, device,
1448 "start_IO: selected paths gone (%x)",
1449 cqr->lpm);
1450 } else if (cqr->lpm != device->path_data.opm) {
1451 cqr->lpm = device->path_data.opm;
1452 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1453 "start_IO: selected paths gone,"
1454 " retry on all paths");
1455 } else {
1456 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1457 "start_IO: all paths in opm gone,"
1458 " do path verification");
1459 dasd_generic_last_path_gone(device);
1460 device->path_data.opm = 0;
1461 device->path_data.ppm = 0;
1462 device->path_data.npm = 0;
1463 device->path_data.tbvpm =
1464 ccw_device_get_path_mask(device->cdev);
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001468 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001469 "start_IO: -ENODEV device gone, retry");
1470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001472 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001473 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001475 case -EINVAL:
1476 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001477 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001478 "start_IO: -EINVAL device currently "
1479 "not accessible");
1480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001482 /* internal error 11 - unknown rc */
1483 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1484 dev_err(&device->cdev->dev,
1485 "An error occurred in the DASD device driver, "
1486 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 BUG();
1488 break;
1489 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001490 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return rc;
1492}
1493
1494/*
1495 * Timeout function for dasd devices. This is used for different purposes
1496 * 1) missing interrupt handler for normal operation
1497 * 2) delayed start of request where start_IO failed with -EBUSY
1498 * 3) timeout for missing state change interrupts
1499 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1500 * DASD_CQR_QUEUED for 2) and 3).
1501 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001502static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 unsigned long flags;
1505 struct dasd_device *device;
1506
1507 device = (struct dasd_device *) ptr;
1508 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1509 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001510 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001512 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/*
1516 * Setup timeout for a device in jiffies.
1517 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001520 if (expires == 0)
1521 del_timer(&device->timer);
1522 else
1523 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
1527 * Clear timeout for a device.
1528 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001529void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001531 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001534static void dasd_handle_killed_request(struct ccw_device *cdev,
1535 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct dasd_ccw_req *cqr;
1538 struct dasd_device *device;
1539
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001540 if (!intparm)
1541 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 cqr = (struct dasd_ccw_req *) intparm;
1543 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001544 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1545 "invalid status in handle_killed_request: "
1546 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return;
1548 }
1549
Stefan Haberland589c74d2010-02-26 22:37:47 +01001550 device = dasd_device_from_cdev_locked(cdev);
1551 if (IS_ERR(device)) {
1552 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1553 "unable to get device from cdev");
1554 return;
1555 }
1556
1557 if (!cqr->startdev ||
1558 device != cqr->startdev ||
1559 strncmp(cqr->startdev->discipline->ebcname,
1560 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001561 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1562 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001563 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return;
1565 }
1566
1567 /* Schedule request to be retried. */
1568 cqr->status = DASD_CQR_QUEUED;
1569
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001570 dasd_device_clear_timer(device);
1571 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 dasd_put_device(device);
1573}
1574
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001575void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001577 /* First of all start sense subsystem status request. */
1578 dasd_eer_snss(device);
1579
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001580 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001581 dasd_schedule_device_bh(device);
1582 if (device->block)
1583 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/*
1587 * Interrupt handler for "normal" ssch-io based dasd devices.
1588 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001589void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1590 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct dasd_ccw_req *cqr, *next;
1593 struct dasd_device *device;
1594 unsigned long long now;
1595 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Heiko Carstens32839422011-01-05 12:47:30 +01001597 kstat_cpu(smp_processor_id()).irqs[IOINT_DAS]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (IS_ERR(irb)) {
1599 switch (PTR_ERR(irb)) {
1600 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001603 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1604 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001607 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1608 "unknown error %ld\n", __func__,
1609 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001611 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return;
1613 }
1614
1615 now = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001616 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001617 /* check for conditions that should be handled immediately */
1618 if (!cqr ||
1619 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1620 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001621 if (cqr)
1622 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001623 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001624 if (IS_ERR(device))
1625 return;
1626 /* ignore unsolicited interrupts for DIAG discipline */
1627 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001631 device->discipline->dump_sense_dbf(device, irb, "int");
1632 if (device->features & DASD_FEATURE_ERPLOG)
1633 device->discipline->dump_sense(device, cqr, irb);
1634 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001635 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001637 if (!cqr)
1638 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001640 device = (struct dasd_device *) cqr->startdev;
1641 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001643 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1644 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return;
1646 }
1647
1648 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001649 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001650 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001651 cqr->status = DASD_CQR_CLEARED;
1652 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001653 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001654 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return;
1656 }
1657
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001658 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001660 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1661 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return;
1663 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001664
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001665 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001667 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1668 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001669 /* request was completed successfully */
1670 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 cqr->stopclk = now;
1672 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673 if (cqr->devlist.next != &device->ccw_queue) {
1674 next = list_entry(cqr->devlist.next,
1675 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001677 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001678 /*
1679 * If we don't want complex ERP for this request, then just
1680 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001681 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001682 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001683 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001684 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001685 DBF_DEV_EVENT(DBF_DEBUG, device,
1686 "default ERP in fastpath "
1687 "(%i retries left)",
1688 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001689 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1690 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001691 cqr->status = DASD_CQR_QUEUED;
1692 next = cqr;
1693 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001695 }
1696 if (next && (next->status == DASD_CQR_QUEUED) &&
1697 (!device->stopped)) {
1698 if (device->discipline->start_IO(next) == 0)
1699 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001702 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001704 dasd_device_clear_timer(device);
1705 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001708enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1709{
1710 struct dasd_device *device;
1711
1712 device = dasd_device_from_cdev_locked(cdev);
1713
1714 if (IS_ERR(device))
1715 goto out;
1716 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1717 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001718 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001719 dasd_put_device(device);
1720 goto out;
1721 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001722 if (device->discipline->dump_sense_dbf)
1723 device->discipline->dump_sense_dbf(device, irb, "uc");
1724 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001725 dasd_put_device(device);
1726out:
1727 return UC_TODO_RETRY;
1728}
1729EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001732 * If we have an error on a dasd_block layer request then we cancel
1733 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001735static void __dasd_device_recovery(struct dasd_device *device,
1736 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct list_head *l, *n;
1739 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001744 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001746
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001747 list_for_each_safe(l, n, &device->ccw_queue) {
1748 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1749 if (cqr->status == DASD_CQR_QUEUED &&
1750 ref_cqr->block == cqr->block) {
1751 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001753 }
1754};
1755
1756/*
1757 * Remove those ccw requests from the queue that need to be returned
1758 * to the upper layer.
1759 */
1760static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1761 struct list_head *final_queue)
1762{
1763 struct list_head *l, *n;
1764 struct dasd_ccw_req *cqr;
1765
1766 /* Process request with final status. */
1767 list_for_each_safe(l, n, &device->ccw_queue) {
1768 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1769
1770 /* Stop list processing at the first non-final request. */
1771 if (cqr->status == DASD_CQR_QUEUED ||
1772 cqr->status == DASD_CQR_IN_IO ||
1773 cqr->status == DASD_CQR_CLEAR_PENDING)
1774 break;
1775 if (cqr->status == DASD_CQR_ERROR) {
1776 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001778 /* Rechain finished requests to final queue */
1779 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781}
1782
1783/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001784 * the cqrs from the final queue are returned to the upper layer
1785 * by setting a dasd_block state and calling the callback function
1786 */
1787static void __dasd_device_process_final_queue(struct dasd_device *device,
1788 struct list_head *final_queue)
1789{
1790 struct list_head *l, *n;
1791 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001792 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001793 void (*callback)(struct dasd_ccw_req *, void *data);
1794 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001795 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001796
1797 list_for_each_safe(l, n, final_queue) {
1798 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1799 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001800 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001801 callback = cqr->callback;
1802 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001803 if (block)
1804 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001805 switch (cqr->status) {
1806 case DASD_CQR_SUCCESS:
1807 cqr->status = DASD_CQR_DONE;
1808 break;
1809 case DASD_CQR_ERROR:
1810 cqr->status = DASD_CQR_NEED_ERP;
1811 break;
1812 case DASD_CQR_CLEARED:
1813 cqr->status = DASD_CQR_TERMINATED;
1814 break;
1815 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001816 /* internal error 12 - wrong cqr status*/
1817 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1818 dev_err(&device->cdev->dev,
1819 "An error occurred in the DASD device driver, "
1820 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001821 BUG();
1822 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001823 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001824 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001825 if (block)
1826 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001827 }
1828}
1829
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 * Take a look at the first request on the ccw queue and check
1832 * if it reached its expire time. If so, terminate the IO.
1833 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001834static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 struct dasd_ccw_req *cqr;
1837
1838 if (list_empty(&device->ccw_queue))
1839 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001840 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001841 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1842 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1843 if (device->discipline->term_IO(cqr) != 0) {
1844 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001845 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001846 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001847 "ended, retrying in 5 s\n",
1848 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001849 cqr->expires += 5*HZ;
1850 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001851 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001852 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001853 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001854 "remaining\n", cqr, (cqr->expires/HZ),
1855 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 }
1858}
1859
1860/*
1861 * Take a look at the first request on the ccw queue and check
1862 * if it needs to be started.
1863 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001864static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct dasd_ccw_req *cqr;
1867 int rc;
1868
1869 if (list_empty(&device->ccw_queue))
1870 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001871 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001872 if (cqr->status != DASD_CQR_QUEUED)
1873 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001874 /* when device is stopped, return request to previous layer
1875 * exception: only the disconnect or unresumed bits are set and the
1876 * cqr is a path verification request
1877 */
1878 if (device->stopped &&
1879 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1880 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1881 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001882 cqr->status = DASD_CQR_CLEARED;
1883 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001884 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001885 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001886
1887 rc = device->discipline->start_IO(cqr);
1888 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001889 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001890 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001891 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001892 } else
1893 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001894 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001895}
1896
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001897static void __dasd_device_check_path_events(struct dasd_device *device)
1898{
1899 int rc;
1900
1901 if (device->path_data.tbvpm) {
1902 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1903 DASD_UNRESUMED_PM))
1904 return;
1905 rc = device->discipline->verify_path(
1906 device, device->path_data.tbvpm);
1907 if (rc)
1908 dasd_device_set_timer(device, 50);
1909 else
1910 device->path_data.tbvpm = 0;
1911 }
1912};
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001915 * Go through all request on the dasd_device request queue,
1916 * terminate them on the cdev if necessary, and return them to the
1917 * submitting layer via callback.
1918 * Note:
1919 * Make sure that all 'submitting layers' still exist when
1920 * this function is called!. In other words, when 'device' is a base
1921 * device then all block layer requests must have been removed before
1922 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001924int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001926 struct dasd_ccw_req *cqr, *n;
1927 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 INIT_LIST_HEAD(&flush_queue);
1931 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001932 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001933 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001934 /* Check status and move request to flush_queue */
1935 switch (cqr->status) {
1936 case DASD_CQR_IN_IO:
1937 rc = device->discipline->term_IO(cqr);
1938 if (rc) {
1939 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001940 dev_err(&device->cdev->dev,
1941 "Flushing the DASD request queue "
1942 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001943 /* stop flush processing */
1944 goto finished;
1945 }
1946 break;
1947 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001950 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001951 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001952 break;
1953 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001955 }
Horst Hummel8f617012006-08-30 14:33:33 +02001956finished:
1957 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001958 /*
1959 * After this point all requests must be in state CLEAR_PENDING,
1960 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1961 * one of the others.
1962 */
1963 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1964 wait_event(dasd_flush_wq,
1965 (cqr->status != DASD_CQR_CLEAR_PENDING));
1966 /*
1967 * Now set each request back to TERMINATED, DONE or NEED_ERP
1968 * and call the callback function of flushed requests
1969 */
1970 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001971 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/*
1975 * Acquire the device lock and process queues for the device.
1976 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001977static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 atomic_set (&device->tasklet_scheduled, 0);
1982 INIT_LIST_HEAD(&final_queue);
1983 spin_lock_irq(get_ccwdev_lock(device->cdev));
1984 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001985 __dasd_device_check_expire(device);
1986 /* find final requests on ccw queue */
1987 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001988 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1990 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001991 __dasd_device_process_final_queue(device, &final_queue);
1992 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001994 __dasd_device_start_head(device);
1995 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 dasd_put_device(device);
1997}
1998
1999/*
2000 * Schedules a call to dasd_tasklet over the device tasklet.
2001 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002002void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002005 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return;
2007 dasd_get_device(device);
2008 tasklet_hi_schedule(&device->tasklet);
2009}
2010
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002011void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2012{
2013 device->stopped |= bits;
2014}
2015EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2016
2017void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2018{
2019 device->stopped &= ~bits;
2020 if (!device->stopped)
2021 wake_up(&generic_waitq);
2022}
2023EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002026 * Queue a request to the head of the device ccw_queue.
2027 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002029void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 struct dasd_device *device;
2032 unsigned long flags;
2033
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002036 cqr->status = DASD_CQR_QUEUED;
2037 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002039 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2041}
2042
2043/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002044 * Queue a request to the tail of the device ccw_queue.
2045 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002047void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct dasd_device *device;
2050 unsigned long flags;
2051
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002054 cqr->status = DASD_CQR_QUEUED;
2055 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002057 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2059}
2060
2061/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002064static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002066 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2067 cqr->callback_data = DASD_SLEEPON_END_TAG;
2068 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2069 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 struct dasd_device *device;
2075 int rc;
2076
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002077 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002079 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2081 return rc;
2082}
2083
2084/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002085 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2086 */
2087static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2088{
2089 struct dasd_device *device;
2090 dasd_erp_fn_t erp_fn;
2091
2092 if (cqr->status == DASD_CQR_FILLED)
2093 return 0;
2094 device = cqr->startdev;
2095 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2096 if (cqr->status == DASD_CQR_TERMINATED) {
2097 device->discipline->handle_terminated_request(cqr);
2098 return 1;
2099 }
2100 if (cqr->status == DASD_CQR_NEED_ERP) {
2101 erp_fn = device->discipline->erp_action(cqr);
2102 erp_fn(cqr);
2103 return 1;
2104 }
2105 if (cqr->status == DASD_CQR_FAILED)
2106 dasd_log_sense(cqr, &cqr->irb);
2107 if (cqr->refers) {
2108 __dasd_process_erp(device, cqr);
2109 return 1;
2110 }
2111 }
2112 return 0;
2113}
2114
2115static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2116{
2117 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2118 if (cqr->refers) /* erp is not done yet */
2119 return 1;
2120 return ((cqr->status != DASD_CQR_DONE) &&
2121 (cqr->status != DASD_CQR_FAILED));
2122 } else
2123 return (cqr->status == DASD_CQR_FILLED);
2124}
2125
2126static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2127{
2128 struct dasd_device *device;
2129 int rc;
2130 struct list_head ccw_queue;
2131 struct dasd_ccw_req *cqr;
2132
2133 INIT_LIST_HEAD(&ccw_queue);
2134 maincqr->status = DASD_CQR_FILLED;
2135 device = maincqr->startdev;
2136 list_add(&maincqr->blocklist, &ccw_queue);
2137 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2138 cqr = list_first_entry(&ccw_queue,
2139 struct dasd_ccw_req, blocklist)) {
2140
2141 if (__dasd_sleep_on_erp(cqr))
2142 continue;
2143 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2144 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002145 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2146 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2147 cqr->status = DASD_CQR_FAILED;
2148 cqr->intrc = -EPERM;
2149 continue;
2150 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002151 /* Non-temporary stop condition will trigger fail fast */
2152 if (device->stopped & ~DASD_STOPPED_PENDING &&
2153 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2154 (!dasd_eer_enabled(device))) {
2155 cqr->status = DASD_CQR_FAILED;
2156 continue;
2157 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002158 /* Don't try to start requests if device is stopped */
2159 if (interruptible) {
2160 rc = wait_event_interruptible(
2161 generic_waitq, !(device->stopped));
2162 if (rc == -ERESTARTSYS) {
2163 cqr->status = DASD_CQR_FAILED;
2164 maincqr->intrc = rc;
2165 continue;
2166 }
2167 } else
2168 wait_event(generic_waitq, !(device->stopped));
2169
2170 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002171 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002172 dasd_add_request_tail(cqr);
2173 if (interruptible) {
2174 rc = wait_event_interruptible(
2175 generic_waitq, _wait_for_wakeup(cqr));
2176 if (rc == -ERESTARTSYS) {
2177 dasd_cancel_req(cqr);
2178 /* wait (non-interruptible) for final status */
2179 wait_event(generic_waitq,
2180 _wait_for_wakeup(cqr));
2181 cqr->status = DASD_CQR_FAILED;
2182 maincqr->intrc = rc;
2183 continue;
2184 }
2185 } else
2186 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2187 }
2188
2189 maincqr->endclk = get_clock();
2190 if ((maincqr->status != DASD_CQR_DONE) &&
2191 (maincqr->intrc != -ERESTARTSYS))
2192 dasd_log_sense(maincqr, &maincqr->irb);
2193 if (maincqr->status == DASD_CQR_DONE)
2194 rc = 0;
2195 else if (maincqr->intrc)
2196 rc = maincqr->intrc;
2197 else
2198 rc = -EIO;
2199 return rc;
2200}
2201
2202/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002203 * Queue a request to the tail of the device ccw_queue and wait for
2204 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002206int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002208 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002212 * Queue a request to the tail of the device ccw_queue and wait
2213 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002215int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002217 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
2220/*
2221 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2222 * for eckd devices) the currently running request has to be terminated
2223 * and be put back to status queued, before the special request is added
2224 * to the head of the queue. Then the special request is waited on normally.
2225 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002226static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002229 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 if (list_empty(&device->ccw_queue))
2232 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002233 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002234 rc = device->discipline->term_IO(cqr);
2235 if (!rc)
2236 /*
2237 * CQR terminated because a more important request is pending.
2238 * Undo decreasing of retry counter because this is
2239 * not an error case.
2240 */
2241 cqr->retries++;
2242 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002245int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct dasd_device *device;
2248 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002249
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002250 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002251 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2252 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2253 cqr->status = DASD_CQR_FAILED;
2254 cqr->intrc = -EPERM;
2255 return -EIO;
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 spin_lock_irq(get_ccwdev_lock(device->cdev));
2258 rc = _dasd_term_running_cqr(device);
2259 if (rc) {
2260 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2261 return rc;
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002264 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002266 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002269 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2272
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002273 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002274
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002275 if (cqr->status == DASD_CQR_DONE)
2276 rc = 0;
2277 else if (cqr->intrc)
2278 rc = cqr->intrc;
2279 else
2280 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return rc;
2282}
2283
2284/*
2285 * Cancels a request that was started with dasd_sleep_on_req.
2286 * This is useful to timeout requests. The request will be
2287 * terminated if it is currently in i/o.
2288 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002289 * 0 if there was no need to terminate the request (not started yet)
2290 * negative error code if termination failed
2291 * Cancellation of a request is an asynchronous operation! The calling
2292 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002294int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002296 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 unsigned long flags;
2298 int rc;
2299
2300 rc = 0;
2301 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2302 switch (cqr->status) {
2303 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002304 /* request was not started - just set to cleared */
2305 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 break;
2307 case DASD_CQR_IN_IO:
2308 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002309 rc = device->discipline->term_IO(cqr);
2310 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002311 dev_err(&device->cdev->dev,
2312 "Cancelling request %p failed with rc=%d\n",
2313 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002314 } else {
2315 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002318 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002322 dasd_schedule_device_bh(device);
2323 return rc;
2324}
2325
2326
2327/*
2328 * SECTION: Operations of the dasd_block layer.
2329 */
2330
2331/*
2332 * Timeout function for dasd_block. This is used when the block layer
2333 * is waiting for something that may not come reliably, (e.g. a state
2334 * change interrupt)
2335 */
2336static void dasd_block_timeout(unsigned long ptr)
2337{
2338 unsigned long flags;
2339 struct dasd_block *block;
2340
2341 block = (struct dasd_block *) ptr;
2342 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2343 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002344 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002345 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2346 dasd_schedule_block_bh(block);
2347}
2348
2349/*
2350 * Setup timeout for a dasd_block in jiffies.
2351 */
2352void dasd_block_set_timer(struct dasd_block *block, int expires)
2353{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002354 if (expires == 0)
2355 del_timer(&block->timer);
2356 else
2357 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002358}
2359
2360/*
2361 * Clear timeout for a dasd_block.
2362 */
2363void dasd_block_clear_timer(struct dasd_block *block)
2364{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002365 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002366}
2367
2368/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002369 * Process finished error recovery ccw.
2370 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002371static void __dasd_process_erp(struct dasd_device *device,
2372 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002373{
2374 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002375
2376 if (cqr->status == DASD_CQR_DONE)
2377 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2378 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002379 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002380 erp_fn = device->discipline->erp_postaction(cqr);
2381 erp_fn(cqr);
2382}
2383
2384/*
2385 * Fetch requests from the block device queue.
2386 */
2387static void __dasd_process_request_queue(struct dasd_block *block)
2388{
2389 struct request_queue *queue;
2390 struct request *req;
2391 struct dasd_ccw_req *cqr;
2392 struct dasd_device *basedev;
2393 unsigned long flags;
2394 queue = block->request_queue;
2395 basedev = block->base;
2396 /* No queue ? Then there is nothing to do. */
2397 if (queue == NULL)
2398 return;
2399
2400 /*
2401 * We requeue request from the block device queue to the ccw
2402 * queue only in two states. In state DASD_STATE_READY the
2403 * partition detection is done and we need to requeue requests
2404 * for that. State DASD_STATE_ONLINE is normal block device
2405 * operation.
2406 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002407 if (basedev->state < DASD_STATE_READY) {
2408 while ((req = blk_fetch_request(block->request_queue)))
2409 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002410 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002411 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002412 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002413 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002414 if (basedev->features & DASD_FEATURE_READONLY &&
2415 rq_data_dir(req) == WRITE) {
2416 DBF_DEV_EVENT(DBF_ERR, basedev,
2417 "Rejecting write request %p",
2418 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002419 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002420 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002421 continue;
2422 }
2423 cqr = basedev->discipline->build_cp(basedev, block, req);
2424 if (IS_ERR(cqr)) {
2425 if (PTR_ERR(cqr) == -EBUSY)
2426 break; /* normal end condition */
2427 if (PTR_ERR(cqr) == -ENOMEM)
2428 break; /* terminate request queue loop */
2429 if (PTR_ERR(cqr) == -EAGAIN) {
2430 /*
2431 * The current request cannot be build right
2432 * now, we have to try later. If this request
2433 * is the head-of-queue we stop the device
2434 * for 1/2 second.
2435 */
2436 if (!list_empty(&block->ccw_queue))
2437 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002438 spin_lock_irqsave(
2439 get_ccwdev_lock(basedev->cdev), flags);
2440 dasd_device_set_stop_bits(basedev,
2441 DASD_STOPPED_PENDING);
2442 spin_unlock_irqrestore(
2443 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002444 dasd_block_set_timer(block, HZ/2);
2445 break;
2446 }
2447 DBF_DEV_EVENT(DBF_ERR, basedev,
2448 "CCW creation failed (rc=%ld) "
2449 "on request %p",
2450 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002451 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002452 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002453 continue;
2454 }
2455 /*
2456 * Note: callback is set to dasd_return_cqr_cb in
2457 * __dasd_block_start_head to cover erp requests as well
2458 */
2459 cqr->callback_data = (void *) req;
2460 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002461 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002462 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2463 dasd_profile_start(block, cqr, req);
2464 }
2465}
2466
2467static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2468{
2469 struct request *req;
2470 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002471 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002472
2473 req = (struct request *) cqr->callback_data;
2474 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002475 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002476 if (status <= 0)
2477 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09002478 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002479}
2480
2481/*
2482 * Process ccw request queue.
2483 */
2484static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2485 struct list_head *final_queue)
2486{
2487 struct list_head *l, *n;
2488 struct dasd_ccw_req *cqr;
2489 dasd_erp_fn_t erp_fn;
2490 unsigned long flags;
2491 struct dasd_device *base = block->base;
2492
2493restart:
2494 /* Process request with final status. */
2495 list_for_each_safe(l, n, &block->ccw_queue) {
2496 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2497 if (cqr->status != DASD_CQR_DONE &&
2498 cqr->status != DASD_CQR_FAILED &&
2499 cqr->status != DASD_CQR_NEED_ERP &&
2500 cqr->status != DASD_CQR_TERMINATED)
2501 continue;
2502
2503 if (cqr->status == DASD_CQR_TERMINATED) {
2504 base->discipline->handle_terminated_request(cqr);
2505 goto restart;
2506 }
2507
2508 /* Process requests that may be recovered */
2509 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002510 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002511 if (IS_ERR(erp_fn(cqr)))
2512 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002513 goto restart;
2514 }
2515
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002516 /* log sense for fatal error */
2517 if (cqr->status == DASD_CQR_FAILED) {
2518 dasd_log_sense(cqr, &cqr->irb);
2519 }
2520
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002521 /* First of all call extended error reporting. */
2522 if (dasd_eer_enabled(base) &&
2523 cqr->status == DASD_CQR_FAILED) {
2524 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2525
2526 /* restart request */
2527 cqr->status = DASD_CQR_FILLED;
2528 cqr->retries = 255;
2529 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002530 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002531 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2532 flags);
2533 goto restart;
2534 }
2535
2536 /* Process finished ERP request. */
2537 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002538 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002539 goto restart;
2540 }
2541
2542 /* Rechain finished requests to final queue */
2543 cqr->endclk = get_clock();
2544 list_move_tail(&cqr->blocklist, final_queue);
2545 }
2546}
2547
2548static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2549{
2550 dasd_schedule_block_bh(cqr->block);
2551}
2552
2553static void __dasd_block_start_head(struct dasd_block *block)
2554{
2555 struct dasd_ccw_req *cqr;
2556
2557 if (list_empty(&block->ccw_queue))
2558 return;
2559 /* We allways begin with the first requests on the queue, as some
2560 * of previously started requests have to be enqueued on a
2561 * dasd_device again for error recovery.
2562 */
2563 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2564 if (cqr->status != DASD_CQR_FILLED)
2565 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002566 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2567 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2568 cqr->status = DASD_CQR_FAILED;
2569 cqr->intrc = -EPERM;
2570 dasd_schedule_block_bh(block);
2571 continue;
2572 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002573 /* Non-temporary stop condition will trigger fail fast */
2574 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2575 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2576 (!dasd_eer_enabled(block->base))) {
2577 cqr->status = DASD_CQR_FAILED;
2578 dasd_schedule_block_bh(block);
2579 continue;
2580 }
2581 /* Don't try to start requests if device is stopped */
2582 if (block->base->stopped)
2583 return;
2584
2585 /* just a fail safe check, should not happen */
2586 if (!cqr->startdev)
2587 cqr->startdev = block->base;
2588
2589 /* make sure that the requests we submit find their way back */
2590 cqr->callback = dasd_return_cqr_cb;
2591
2592 dasd_add_request_tail(cqr);
2593 }
2594}
2595
2596/*
2597 * Central dasd_block layer routine. Takes requests from the generic
2598 * block layer request queue, creates ccw requests, enqueues them on
2599 * a dasd_device and processes ccw requests that have been returned.
2600 */
2601static void dasd_block_tasklet(struct dasd_block *block)
2602{
2603 struct list_head final_queue;
2604 struct list_head *l, *n;
2605 struct dasd_ccw_req *cqr;
2606
2607 atomic_set(&block->tasklet_scheduled, 0);
2608 INIT_LIST_HEAD(&final_queue);
2609 spin_lock(&block->queue_lock);
2610 /* Finish off requests on ccw queue */
2611 __dasd_process_block_ccw_queue(block, &final_queue);
2612 spin_unlock(&block->queue_lock);
2613 /* Now call the callback function of requests with final status */
2614 spin_lock_irq(&block->request_queue_lock);
2615 list_for_each_safe(l, n, &final_queue) {
2616 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2617 list_del_init(&cqr->blocklist);
2618 __dasd_cleanup_cqr(cqr);
2619 }
2620 spin_lock(&block->queue_lock);
2621 /* Get new request from the block device request queue */
2622 __dasd_process_request_queue(block);
2623 /* Now check if the head of the ccw queue needs to be started. */
2624 __dasd_block_start_head(block);
2625 spin_unlock(&block->queue_lock);
2626 spin_unlock_irq(&block->request_queue_lock);
2627 dasd_put_device(block->base);
2628}
2629
2630static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2631{
2632 wake_up(&dasd_flush_wq);
2633}
2634
2635/*
2636 * Go through all request on the dasd_block request queue, cancel them
2637 * on the respective dasd_device, and return them to the generic
2638 * block layer.
2639 */
2640static int dasd_flush_block_queue(struct dasd_block *block)
2641{
2642 struct dasd_ccw_req *cqr, *n;
2643 int rc, i;
2644 struct list_head flush_queue;
2645
2646 INIT_LIST_HEAD(&flush_queue);
2647 spin_lock_bh(&block->queue_lock);
2648 rc = 0;
2649restart:
2650 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2651 /* if this request currently owned by a dasd_device cancel it */
2652 if (cqr->status >= DASD_CQR_QUEUED)
2653 rc = dasd_cancel_req(cqr);
2654 if (rc < 0)
2655 break;
2656 /* Rechain request (including erp chain) so it won't be
2657 * touched by the dasd_block_tasklet anymore.
2658 * Replace the callback so we notice when the request
2659 * is returned from the dasd_device layer.
2660 */
2661 cqr->callback = _dasd_wake_block_flush_cb;
2662 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2663 list_move_tail(&cqr->blocklist, &flush_queue);
2664 if (i > 1)
2665 /* moved more than one request - need to restart */
2666 goto restart;
2667 }
2668 spin_unlock_bh(&block->queue_lock);
2669 /* Now call the callback function of flushed requests */
2670restart_cb:
2671 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2672 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2673 /* Process finished ERP request. */
2674 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002675 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002676 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002677 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002678 /* restart list_for_xx loop since dasd_process_erp
2679 * might remove multiple elements */
2680 goto restart_cb;
2681 }
2682 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002683 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002684 cqr->endclk = get_clock();
2685 list_del_init(&cqr->blocklist);
2686 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002687 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return rc;
2690}
2691
2692/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002693 * Schedules a call to dasd_tasklet over the device tasklet.
2694 */
2695void dasd_schedule_block_bh(struct dasd_block *block)
2696{
2697 /* Protect against rescheduling. */
2698 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2699 return;
2700 /* life cycle of block is bound to it's base device */
2701 dasd_get_device(block->base);
2702 tasklet_hi_schedule(&block->tasklet);
2703}
2704
2705
2706/*
2707 * SECTION: external block device operations
2708 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 */
2710
2711/*
2712 * Dasd request queue function. Called from ll_rw_blk.c
2713 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002714static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002716 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002718 block = queue->queuedata;
2719 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002721 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002723 __dasd_block_start_head(block);
2724 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
2727/*
2728 * Allocate and initialize request queue and default I/O scheduler.
2729 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002730static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 int rc;
2733
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002734 block->request_queue = blk_init_queue(do_dasd_request,
2735 &block->request_queue_lock);
2736 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return -ENOMEM;
2738
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002739 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002741 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002742 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002743 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002745 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return rc;
2747 }
2748 return 0;
2749}
2750
2751/*
2752 * Allocate and initialize request queue.
2753 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002754static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
2756 int max;
2757
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002758 if (block->base->features & DASD_FEATURE_USERAW) {
2759 /*
2760 * the max_blocks value for raw_track access is 256
2761 * it is higher than the native ECKD value because we
2762 * only need one ccw per track
2763 * so the max_hw_sectors are
2764 * 2048 x 512B = 1024kB = 16 tracks
2765 */
2766 max = 2048;
2767 } else {
2768 max = block->base->discipline->max_blocks << block->s2b_shift;
2769 }
2770 blk_queue_logical_block_size(block->request_queue,
2771 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002772 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002773 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002774 /* with page sized segments we can translate each segement into
2775 * one idaw/tidaw
2776 */
2777 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2778 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781/*
2782 * Deactivate and free request queue.
2783 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002784static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002786 if (block->request_queue) {
2787 blk_cleanup_queue(block->request_queue);
2788 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790}
2791
2792/*
2793 * Flush request on the request queue.
2794 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002795static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
2797 struct request *req;
2798
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002799 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002801
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002802 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002803 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002804 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002805 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
Al Viro57a7c0b2008-03-02 10:36:08 -05002808static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Stefan Haberland9eb25122010-02-26 22:37:46 +01002810 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int rc;
2812
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002813 base = dasd_device_from_gendisk(bdev->bd_disk);
2814 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01002815 return -ENODEV;
2816
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002817 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002818 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 rc = -ENODEV;
2820 goto unlock;
2821 }
2822
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002823 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 rc = -EINVAL;
2825 goto unlock;
2826 }
2827
2828 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002829 dev_info(&base->cdev->dev,
2830 "Accessing the DASD failed because it is in "
2831 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 rc = -EPERM;
2833 goto out;
2834 }
2835
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002836 if (base->state <= DASD_STATE_BASIC) {
2837 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 " Cannot open unrecognized device");
2839 rc = -ENODEV;
2840 goto out;
2841 }
2842
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002843 if ((mode & FMODE_WRITE) &&
2844 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2845 (base->features & DASD_FEATURE_READONLY))) {
2846 rc = -EROFS;
2847 goto out;
2848 }
2849
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002850 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return 0;
2852
2853out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002854 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002856 atomic_dec(&base->block->open_count);
2857 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return rc;
2859}
2860
Al Viro57a7c0b2008-03-02 10:36:08 -05002861static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002863 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002865 base = dasd_device_from_gendisk(disk);
2866 if (!base)
2867 return -ENODEV;
2868
2869 atomic_dec(&base->block->open_count);
2870 module_put(base->discipline->owner);
2871 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return 0;
2873}
2874
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002875/*
2876 * Return disk geometry.
2877 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002878static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002879{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002880 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002881
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002882 base = dasd_device_from_gendisk(bdev->bd_disk);
2883 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002884 return -ENODEV;
2885
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002886 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002887 !base->discipline->fill_geometry) {
2888 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002889 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02002890 }
2891 base->discipline->fill_geometry(base->block, geo);
2892 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
2893 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002894 return 0;
2895}
2896
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002897const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898dasd_device_operations = {
2899 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002900 .open = dasd_open,
2901 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002902 .ioctl = dasd_ioctl,
2903 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002904 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905};
2906
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002907/*******************************************************************************
2908 * end of block device operations
2909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911static void
2912dasd_exit(void)
2913{
2914#ifdef CONFIG_PROC_FS
2915 dasd_proc_exit();
2916#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002917 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002918 if (dasd_page_cache != NULL) {
2919 kmem_cache_destroy(dasd_page_cache);
2920 dasd_page_cache = NULL;
2921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 dasd_gendisk_exit();
2923 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (dasd_debug_area != NULL) {
2925 debug_unregister(dasd_debug_area);
2926 dasd_debug_area = NULL;
2927 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02002928 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929}
2930
2931/*
2932 * SECTION: common functions for ccw_driver use
2933 */
2934
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002935/*
2936 * Is the device read-only?
2937 * Note that this function does not report the setting of the
2938 * readonly device attribute, but how it is configured in z/VM.
2939 */
2940int dasd_device_is_ro(struct dasd_device *device)
2941{
2942 struct ccw_dev_id dev_id;
2943 struct diag210 diag_data;
2944 int rc;
2945
2946 if (!MACHINE_IS_VM)
2947 return 0;
2948 ccw_device_get_id(device->cdev, &dev_id);
2949 memset(&diag_data, 0, sizeof(diag_data));
2950 diag_data.vrdcdvno = dev_id.devno;
2951 diag_data.vrdclen = sizeof(diag_data);
2952 rc = diag210(&diag_data);
2953 if (rc == 0 || rc == 2) {
2954 return diag_data.vrdcvfla & 0x80;
2955 } else {
2956 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2957 dev_id.devno, rc);
2958 return 0;
2959 }
2960}
2961EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2962
Cornelia Huckf3445a12009-04-14 15:36:23 +02002963static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2964{
2965 struct ccw_device *cdev = data;
2966 int ret;
2967
2968 ret = ccw_device_set_online(cdev);
2969 if (ret)
2970 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2971 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02002972}
2973
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002974/*
2975 * Initial attempt at a probe function. this can be simplified once
2976 * the other detection code is gone.
2977 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002978int dasd_generic_probe(struct ccw_device *cdev,
2979 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 int ret;
2982
2983 ret = dasd_add_sysfs_files(cdev);
2984 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002985 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2986 "dasd_generic_probe: could not add "
2987 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002988 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Horst Hummel40545572006-06-29 15:08:18 +02002990 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Horst Hummel40545572006-06-29 15:08:18 +02002992 /*
2993 * Automatically online either all dasd devices (dasd_autodetect)
2994 * or all devices specified with dasd= parameters during
2995 * initial probe.
2996 */
2997 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002998 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002999 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003003/*
3004 * This will one day be called from a global not_oper handler.
3005 * It is also used by driver_unregister during module unload.
3006 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003007void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
3009 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003010 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Horst Hummel59afda72005-05-16 21:53:39 -07003012 cdev->handler = NULL;
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 dasd_remove_sysfs_files(cdev);
3015 device = dasd_device_from_cdev(cdev);
3016 if (IS_ERR(device))
3017 return;
3018 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3019 /* Already doing offline processing */
3020 dasd_put_device(device);
3021 return;
3022 }
3023 /*
3024 * This device is removed unconditionally. Set offline
3025 * flag to prevent dasd_open from opening it while it is
3026 * no quite down yet.
3027 */
3028 dasd_set_target_state(device, DASD_STATE_NEW);
3029 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003030 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003032 /*
3033 * life cycle of block is bound to device, so delete it after
3034 * device was safely removed
3035 */
3036 if (block)
3037 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003040/*
3041 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003043 * or the user has started activation through sysfs.
3044 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003045int dasd_generic_set_online(struct ccw_device *cdev,
3046 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003048 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003050 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003051
Horst Hummel40545572006-06-29 15:08:18 +02003052 /* first online clears initial online feature flag */
3053 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 device = dasd_create_device(cdev);
3055 if (IS_ERR(device))
3056 return PTR_ERR(device);
3057
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003058 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003059 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003061 pr_warning("%s Setting the DASD online failed because "
3062 "of missing DIAG discipline\n",
3063 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 dasd_delete_device(device);
3065 return -ENODEV;
3066 }
3067 discipline = dasd_diag_discipline_pointer;
3068 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003069 if (!try_module_get(base_discipline->owner)) {
3070 dasd_delete_device(device);
3071 return -EINVAL;
3072 }
3073 if (!try_module_get(discipline->owner)) {
3074 module_put(base_discipline->owner);
3075 dasd_delete_device(device);
3076 return -EINVAL;
3077 }
3078 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 device->discipline = discipline;
3080
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003081 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 rc = discipline->check_device(device);
3083 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003084 pr_warning("%s Setting the DASD online with discipline %s "
3085 "failed with rc=%i\n",
3086 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003087 module_put(discipline->owner);
3088 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 dasd_delete_device(device);
3090 return rc;
3091 }
3092
3093 dasd_set_target_state(device, DASD_STATE_ONLINE);
3094 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003095 pr_warning("%s Setting the DASD online failed because of a "
3096 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 rc = -ENODEV;
3098 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003099 if (device->block)
3100 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 dasd_delete_device(device);
3102 } else
3103 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003104 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003105
3106 wait_event(dasd_init_waitq, _wait_for_device(device));
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return rc;
3110}
3111
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003112int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003115 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07003116 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 device = dasd_device_from_cdev(cdev);
3119 if (IS_ERR(device))
3120 return PTR_ERR(device);
3121 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3122 /* Already doing offline processing */
3123 dasd_put_device(device);
3124 return 0;
3125 }
3126 /*
3127 * We must make sure that this device is currently not in use.
3128 * The open_count is increased for every opener, that includes
3129 * the blkdev_get in dasd_scan_partitions. We are only interested
3130 * in the other openers.
3131 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003132 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003133 max_count = device->block->bdev ? 0 : -1;
3134 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003135 if (open_count > max_count) {
3136 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003137 pr_warning("%s: The DASD cannot be set offline "
3138 "with open count %i\n",
3139 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003140 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003141 pr_warning("%s: The DASD cannot be set offline "
3142 "while it is in use\n",
3143 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003144 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3145 dasd_put_device(device);
3146 return -EBUSY;
3147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
3149 dasd_set_target_state(device, DASD_STATE_NEW);
3150 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003151 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003153 /*
3154 * life cycle of block is bound to device, so delete it after
3155 * device was safely removed
3156 */
3157 if (block)
3158 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return 0;
3160}
3161
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003162int dasd_generic_last_path_gone(struct dasd_device *device)
3163{
3164 struct dasd_ccw_req *cqr;
3165
3166 dev_warn(&device->cdev->dev, "No operational channel path is left "
3167 "for the device\n");
3168 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3169 /* First of all call extended error reporting. */
3170 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3171
3172 if (device->state < DASD_STATE_BASIC)
3173 return 0;
3174 /* Device is active. We want to keep it. */
3175 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3176 if ((cqr->status == DASD_CQR_IN_IO) ||
3177 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3178 cqr->status = DASD_CQR_QUEUED;
3179 cqr->retries++;
3180 }
3181 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3182 dasd_device_clear_timer(device);
3183 dasd_schedule_device_bh(device);
3184 return 1;
3185}
3186EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3187
3188int dasd_generic_path_operational(struct dasd_device *device)
3189{
3190 dev_info(&device->cdev->dev, "A channel path to the device has become "
3191 "operational\n");
3192 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3193 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3194 if (device->stopped & DASD_UNRESUMED_PM) {
3195 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3196 dasd_restore_device(device);
3197 return 1;
3198 }
3199 dasd_schedule_device_bh(device);
3200 if (device->block)
3201 dasd_schedule_block_bh(device->block);
3202 return 1;
3203}
3204EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3205
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003206int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 int ret;
3210
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003211 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (IS_ERR(device))
3213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 ret = 0;
3215 switch (event) {
3216 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003217 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003219 device->path_data.opm = 0;
3220 device->path_data.ppm = 0;
3221 device->path_data.npm = 0;
3222 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 break;
3224 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003226 if (device->path_data.opm)
3227 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 break;
3229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 dasd_put_device(device);
3231 return ret;
3232}
3233
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003234void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3235{
3236 int chp;
3237 __u8 oldopm, eventlpm;
3238 struct dasd_device *device;
3239
3240 device = dasd_device_from_cdev_locked(cdev);
3241 if (IS_ERR(device))
3242 return;
3243 for (chp = 0; chp < 8; chp++) {
3244 eventlpm = 0x80 >> chp;
3245 if (path_event[chp] & PE_PATH_GONE) {
3246 oldopm = device->path_data.opm;
3247 device->path_data.opm &= ~eventlpm;
3248 device->path_data.ppm &= ~eventlpm;
3249 device->path_data.npm &= ~eventlpm;
3250 if (oldopm && !device->path_data.opm)
3251 dasd_generic_last_path_gone(device);
3252 }
3253 if (path_event[chp] & PE_PATH_AVAILABLE) {
3254 device->path_data.opm &= ~eventlpm;
3255 device->path_data.ppm &= ~eventlpm;
3256 device->path_data.npm &= ~eventlpm;
3257 device->path_data.tbvpm |= eventlpm;
3258 dasd_schedule_device_bh(device);
3259 }
3260 }
3261 dasd_put_device(device);
3262}
3263EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3264
3265int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3266{
3267 if (!device->path_data.opm && lpm) {
3268 device->path_data.opm = lpm;
3269 dasd_generic_path_operational(device);
3270 } else
3271 device->path_data.opm |= lpm;
3272 return 0;
3273}
3274EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3275
3276
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003277int dasd_generic_pm_freeze(struct ccw_device *cdev)
3278{
3279 struct dasd_ccw_req *cqr, *n;
3280 int rc;
3281 struct list_head freeze_queue;
3282 struct dasd_device *device = dasd_device_from_cdev(cdev);
3283
3284 if (IS_ERR(device))
3285 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003286
3287 if (device->discipline->freeze)
3288 rc = device->discipline->freeze(device);
3289
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003290 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003291 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003292 /* clear active requests */
3293 INIT_LIST_HEAD(&freeze_queue);
3294 spin_lock_irq(get_ccwdev_lock(cdev));
3295 rc = 0;
3296 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3297 /* Check status and move request to flush_queue */
3298 if (cqr->status == DASD_CQR_IN_IO) {
3299 rc = device->discipline->term_IO(cqr);
3300 if (rc) {
3301 /* unable to terminate requeust */
3302 dev_err(&device->cdev->dev,
3303 "Unable to terminate request %p "
3304 "on suspend\n", cqr);
3305 spin_unlock_irq(get_ccwdev_lock(cdev));
3306 dasd_put_device(device);
3307 return rc;
3308 }
3309 }
3310 list_move_tail(&cqr->devlist, &freeze_queue);
3311 }
3312
3313 spin_unlock_irq(get_ccwdev_lock(cdev));
3314
3315 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3316 wait_event(dasd_flush_wq,
3317 (cqr->status != DASD_CQR_CLEAR_PENDING));
3318 if (cqr->status == DASD_CQR_CLEARED)
3319 cqr->status = DASD_CQR_QUEUED;
3320 }
3321 /* move freeze_queue to start of the ccw_queue */
3322 spin_lock_irq(get_ccwdev_lock(cdev));
3323 list_splice_tail(&freeze_queue, &device->ccw_queue);
3324 spin_unlock_irq(get_ccwdev_lock(cdev));
3325
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003326 dasd_put_device(device);
3327 return rc;
3328}
3329EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3330
3331int dasd_generic_restore_device(struct ccw_device *cdev)
3332{
3333 struct dasd_device *device = dasd_device_from_cdev(cdev);
3334 int rc = 0;
3335
3336 if (IS_ERR(device))
3337 return PTR_ERR(device);
3338
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003339 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003340 dasd_device_remove_stop_bits(device,
3341 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003342
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003343 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003344
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003345 /*
3346 * call discipline restore function
3347 * if device is stopped do nothing e.g. for disconnected devices
3348 */
3349 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003350 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003351 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003352 /*
3353 * if the resume failed for the DASD we put it in
3354 * an UNRESUMED stop state
3355 */
3356 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003357
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003358 if (device->block)
3359 dasd_schedule_block_bh(device->block);
3360
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003361 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003362 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003363}
3364EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3365
Heiko Carstens763968e2007-05-10 15:45:46 +02003366static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3367 void *rdc_buffer,
3368 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003369 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003370{
3371 struct dasd_ccw_req *cqr;
3372 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003373 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003374
3375 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3376
3377 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003378 /* internal error 13 - Allocating the RDC request failed*/
3379 dev_err(&device->cdev->dev,
3380 "An error occurred in the DASD device driver, "
3381 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003382 return cqr;
3383 }
3384
3385 ccw = cqr->cpaddr;
3386 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003387 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3388 idaw = (unsigned long *) (cqr->data);
3389 ccw->cda = (__u32)(addr_t) idaw;
3390 ccw->flags = CCW_FLAG_IDA;
3391 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3392 } else {
3393 ccw->cda = (__u32)(addr_t) rdc_buffer;
3394 ccw->flags = 0;
3395 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003396
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003397 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003398 cqr->startdev = device;
3399 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003400 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003401 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02003402 cqr->buildclk = get_clock();
3403 cqr->status = DASD_CQR_FILLED;
3404 return cqr;
3405}
3406
3407
Stefan Haberland68b781f2009-09-11 10:28:29 +02003408int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003409 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003410{
3411 int ret;
3412 struct dasd_ccw_req *cqr;
3413
Sebastian Ott92636b12009-06-12 10:26:37 +02003414 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003415 magic);
3416 if (IS_ERR(cqr))
3417 return PTR_ERR(cqr);
3418
3419 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003420 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003421 return ret;
3422}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003423EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003424
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003425/*
3426 * In command mode and transport mode we need to look for sense
3427 * data in different places. The sense data itself is allways
3428 * an array of 32 bytes, so we can unify the sense data access
3429 * for both modes.
3430 */
3431char *dasd_get_sense(struct irb *irb)
3432{
3433 struct tsb *tsb = NULL;
3434 char *sense = NULL;
3435
3436 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3437 if (irb->scsw.tm.tcw)
3438 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3439 irb->scsw.tm.tcw);
3440 if (tsb && tsb->length == 64 && tsb->flags)
3441 switch (tsb->flags & 0x07) {
3442 case 1: /* tsa_iostat */
3443 sense = tsb->tsa.iostat.sense;
3444 break;
3445 case 2: /* tsa_ddpc */
3446 sense = tsb->tsa.ddpc.sense;
3447 break;
3448 default:
3449 /* currently we don't use interrogate data */
3450 break;
3451 }
3452 } else if (irb->esw.esw0.erw.cons) {
3453 sense = irb->ecw;
3454 }
3455 return sense;
3456}
3457EXPORT_SYMBOL_GPL(dasd_get_sense);
3458
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003459static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
3461 int rc;
3462
3463 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003464 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003465 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003468 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (dasd_debug_area == NULL) {
3470 rc = -ENOMEM;
3471 goto failed;
3472 }
3473 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003474 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3477
3478 dasd_diag_discipline_pointer = NULL;
3479
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003480 dasd_statistics_createroot();
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 rc = dasd_devmap_init();
3483 if (rc)
3484 goto failed;
3485 rc = dasd_gendisk_init();
3486 if (rc)
3487 goto failed;
3488 rc = dasd_parse();
3489 if (rc)
3490 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003491 rc = dasd_eer_init();
3492 if (rc)
3493 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494#ifdef CONFIG_PROC_FS
3495 rc = dasd_proc_init();
3496 if (rc)
3497 goto failed;
3498#endif
3499
3500 return 0;
3501failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003502 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 dasd_exit();
3504 return rc;
3505}
3506
3507module_init(dasd_init);
3508module_exit(dasd_exit);
3509
3510EXPORT_SYMBOL(dasd_debug_area);
3511EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3512
3513EXPORT_SYMBOL(dasd_add_request_head);
3514EXPORT_SYMBOL(dasd_add_request_tail);
3515EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003516EXPORT_SYMBOL(dasd_device_clear_timer);
3517EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518EXPORT_SYMBOL(dasd_enable_device);
3519EXPORT_SYMBOL(dasd_int_handler);
3520EXPORT_SYMBOL(dasd_kfree_request);
3521EXPORT_SYMBOL(dasd_kick_device);
3522EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003523EXPORT_SYMBOL(dasd_schedule_device_bh);
3524EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003526EXPORT_SYMBOL(dasd_device_set_timer);
3527EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528EXPORT_SYMBOL(dasd_sfree_request);
3529EXPORT_SYMBOL(dasd_sleep_on);
3530EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3531EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3532EXPORT_SYMBOL(dasd_smalloc_request);
3533EXPORT_SYMBOL(dasd_start_IO);
3534EXPORT_SYMBOL(dasd_term_IO);
3535
3536EXPORT_SYMBOL_GPL(dasd_generic_probe);
3537EXPORT_SYMBOL_GPL(dasd_generic_remove);
3538EXPORT_SYMBOL_GPL(dasd_generic_notify);
3539EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3540EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003541EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3542EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3543EXPORT_SYMBOL_GPL(dasd_alloc_block);
3544EXPORT_SYMBOL_GPL(dasd_free_block);