blob: 1eef0f586950b6014ab46a60170a2ccf74f0a299 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02007 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Stefan Haberlandfc19f382009-03-26 15:23:49 +010010#define KMSG_COMPONENT "dasd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kmod.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/ctype.h>
17#include <linux/major.h>
18#include <linux/slab.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020020#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010021#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020022#include <linux/debugfs.h>
23#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020024#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010029#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010030#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
41/*
42 * SECTION: exported variables of dasd.c
43 */
44debug_info_t *dasd_debug_area;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020045static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010047void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
50MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020051 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053MODULE_LICENSE("GPL");
54
55/*
56 * SECTION: prototypes for static functions of dasd.c
57 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010058static int dasd_alloc_queue(struct dasd_block *);
59static void dasd_setup_queue(struct dasd_block *);
60static void dasd_free_queue(struct dasd_block *);
61static void dasd_flush_request_queue(struct dasd_block *);
62static int dasd_flush_block_queue(struct dasd_block *);
63static void dasd_device_tasklet(struct dasd_device *);
64static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000065static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020066static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020067static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010068static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010069static void dasd_device_timeout(unsigned long);
70static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010071static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020072static void dasd_profile_init(struct dasd_profile *, struct dentry *);
73static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * SECTION: Operations on the device structure.
77 */
78static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020079static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020080static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020081static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * Allocate memory for a new device structure.
85 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010086struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct dasd_device *device;
89
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010090 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
91 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* Get two pages for normal block device operations. */
95 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010096 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 kfree(device);
98 return ERR_PTR(-ENOMEM);
99 }
100 /* Get one page for error recovery. */
101 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100102 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 free_pages((unsigned long) device->ccw_mem, 1);
104 kfree(device);
105 return ERR_PTR(-ENOMEM);
106 }
107
108 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
109 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
110 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100111 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200112 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100113 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 (unsigned long) device);
115 INIT_LIST_HEAD(&device->ccw_queue);
116 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100117 device->timer.function = dasd_device_timeout;
118 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000119 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200120 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200121 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 device->state = DASD_STATE_NEW;
123 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100124 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200125 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return device;
127}
128
129/*
130 * Free memory of a device structure.
131 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100132void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800134 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 free_page((unsigned long) device->erp_mem);
136 free_pages((unsigned long) device->ccw_mem, 1);
137 kfree(device);
138}
139
140/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100141 * Allocate memory for a new device structure.
142 */
143struct dasd_block *dasd_alloc_block(void)
144{
145 struct dasd_block *block;
146
147 block = kzalloc(sizeof(*block), GFP_ATOMIC);
148 if (!block)
149 return ERR_PTR(-ENOMEM);
150 /* open_count = 0 means device online but not in use */
151 atomic_set(&block->open_count, -1);
152
153 spin_lock_init(&block->request_queue_lock);
154 atomic_set(&block->tasklet_scheduled, 0);
155 tasklet_init(&block->tasklet,
156 (void (*)(unsigned long)) dasd_block_tasklet,
157 (unsigned long) block);
158 INIT_LIST_HEAD(&block->ccw_queue);
159 spin_lock_init(&block->queue_lock);
160 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100161 block->timer.function = dasd_block_timeout;
162 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200163 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100164
165 return block;
166}
167
168/*
169 * Free memory of a device structure.
170 */
171void dasd_free_block(struct dasd_block *block)
172{
173 kfree(block);
174}
175
176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * Make a new device known to the system.
178 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100179static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int rc;
182
183 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200184 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * keep the reference count > 0.
186 */
187 dasd_get_device(device);
188
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100189 if (device->block) {
190 rc = dasd_alloc_queue(device->block);
191 if (rc) {
192 dasd_put_device(device);
193 return rc;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 device->state = DASD_STATE_KNOWN;
197 return 0;
198}
199
200/*
201 * Let the system forget about a device.
202 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100203static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800205 /* Disable extended error reporting for this device. */
206 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100208 if (device->discipline) {
209 if (device->discipline->uncheck_device)
210 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800211 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800214 if (device->base_discipline)
215 module_put(device->base_discipline->owner);
216 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->state = DASD_STATE_NEW;
218
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 if (device->block)
220 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Give up reference we took in dasd_state_new_to_known. */
223 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200227static struct dentry *dasd_debugfs_setup(const char *name,
228 struct dentry *base_dentry)
229{
230 struct dentry *pde;
231
232 if (!base_dentry)
233 return NULL;
234 pde = debugfs_create_dir(name, base_dentry);
235 if (!pde || IS_ERR(pde))
236 return NULL;
237 return pde;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Request the irq line for the device.
242 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100243static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200245 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200246 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200249 if (block) {
250 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100251 if (rc)
252 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 block->debugfs_dentry =
254 dasd_debugfs_setup(block->gdp->disk_name,
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&block->profile, block->debugfs_dentry);
257 if (dasd_global_profile_level == DASD_PROFILE_ON)
258 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100259 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200260 device->debugfs_dentry =
261 dasd_debugfs_setup(dev_name(&device->cdev->dev),
262 dasd_debugfs_root_entry);
263 dasd_profile_init(&device->profile, device->debugfs_dentry);
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100266 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100267 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200269 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
271
272 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200273
274 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/*
278 * Release the irq line for the device. Terminate any running i/o.
279 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100280static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Horst Hummel8f617012006-08-30 14:33:33 +0200282 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200283
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200285 dasd_profile_exit(&device->block->profile);
286 if (device->block->debugfs_dentry)
287 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100288 dasd_gendisk_free(device->block);
289 dasd_block_clear_timer(device->block);
290 }
291 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200292 if (rc)
293 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200295 dasd_profile_exit(&device->profile);
296 if (device->debugfs_dentry)
297 debugfs_remove(device->debugfs_dentry);
Horst Hummel8f617012006-08-30 14:33:33 +0200298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
300 if (device->debug_area != NULL) {
301 debug_unregister(device->debug_area);
302 device->debug_area = NULL;
303 }
304 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
309 * Do the initial analysis. The do_analysis function may return
310 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
311 * until the discipline decides to continue the startup sequence
312 * by calling the function dasd_change_state. The eckd disciplines
313 * uses this to start a ccw that detects the format. The completion
314 * interrupt for this detection ccw uses the kernel event daemon to
315 * trigger the call to dasd_change_state. All this is done in the
316 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800317 * After the analysis ccw is done (do_analysis returned 0) the block
318 * device is setup.
319 * In case the analysis returns an error, the device setup is stopped
320 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100322static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100328 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800329 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100330 if (block) {
331 if (block->base->discipline->do_analysis != NULL)
332 rc = block->base->discipline->do_analysis(block);
333 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200334 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100335 device->state = DASD_STATE_UNFMT;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200336 goto out;
337 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100338 return rc;
339 }
340 dasd_setup_queue(block);
341 set_capacity(block->gdp,
342 block->blocks << block->s2b_shift);
343 device->state = DASD_STATE_READY;
344 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200345 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100346 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200347 return rc;
348 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100349 } else {
350 device->state = DASD_STATE_READY;
351 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200352out:
353 if (device->discipline->basic_to_ready)
354 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800355 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100358static inline
359int _wait_for_empty_queues(struct dasd_device *device)
360{
361 if (device->block)
362 return list_empty(&device->ccw_queue) &&
363 list_empty(&device->block->ccw_queue);
364 else
365 return list_empty(&device->ccw_queue);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * Remove device from block device layer. Destroy dirty buffers.
370 * Forget format information. Check if the target level is basic
371 * and if it is create fake disk for formatting.
372 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100373static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Horst Hummel8f617012006-08-30 14:33:33 +0200375 int rc;
376
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200377 if (device->discipline->ready_to_basic) {
378 rc = device->discipline->ready_to_basic(device);
379 if (rc)
380 return rc;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100383 if (device->block) {
384 struct dasd_block *block = device->block;
385 rc = dasd_flush_block_queue(block);
386 if (rc) {
387 device->state = DASD_STATE_READY;
388 return rc;
389 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100390 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100391 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100392 block->blocks = 0;
393 block->bp_block = 0;
394 block->s2b_shift = 0;
395 }
Horst Hummel8f617012006-08-30 14:33:33 +0200396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/*
Horst Hummel90f00942006-03-07 21:55:39 -0800400 * Back to basic.
401 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100402static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800403{
404 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200405 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800406}
407
408/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * Make the device online and schedule the bottom half to start
410 * the requeueing of requests from the linux request queue to the
411 * ccw queue.
412 */
Horst Hummel8f617012006-08-30 14:33:33 +0200413static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414dasd_state_ready_to_online(struct dasd_device * device)
415{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100416 struct gendisk *disk;
417 struct disk_part_iter piter;
418 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100421 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100422 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100423 if ((device->features & DASD_FEATURE_USERAW)) {
424 disk = device->block->gdp;
425 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
426 return 0;
427 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100428 disk = device->block->bdev->bd_disk;
429 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
430 while ((part = disk_part_iter_next(&piter)))
431 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
432 disk_part_iter_exit(&piter);
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437/*
438 * Stop the requeueing of requests again.
439 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100440static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100442 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100443 struct gendisk *disk;
444 struct disk_part_iter piter;
445 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100446
447 if (device->discipline->online_to_ready) {
448 rc = device->discipline->online_to_ready(device);
449 if (rc)
450 return rc;
451 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100454 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100455 disk = device->block->bdev->bd_disk;
456 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
457 while ((part = disk_part_iter_next(&piter)))
458 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
459 disk_part_iter_exit(&piter);
460 }
Horst Hummel8f617012006-08-30 14:33:33 +0200461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/*
465 * Device startup state changes.
466 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100467static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int rc;
470
471 rc = 0;
472 if (device->state == DASD_STATE_NEW &&
473 device->target >= DASD_STATE_KNOWN)
474 rc = dasd_state_new_to_known(device);
475
476 if (!rc &&
477 device->state == DASD_STATE_KNOWN &&
478 device->target >= DASD_STATE_BASIC)
479 rc = dasd_state_known_to_basic(device);
480
481 if (!rc &&
482 device->state == DASD_STATE_BASIC &&
483 device->target >= DASD_STATE_READY)
484 rc = dasd_state_basic_to_ready(device);
485
486 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700487 device->state == DASD_STATE_UNFMT &&
488 device->target > DASD_STATE_UNFMT)
489 rc = -EPERM;
490
491 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 device->state == DASD_STATE_READY &&
493 device->target >= DASD_STATE_ONLINE)
494 rc = dasd_state_ready_to_online(device);
495
496 return rc;
497}
498
499/*
500 * Device shutdown state changes.
501 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100502static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Horst Hummel8f617012006-08-30 14:33:33 +0200504 int rc;
505
506 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (device->state == DASD_STATE_ONLINE &&
508 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200509 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200510
Horst Hummel8f617012006-08-30 14:33:33 +0200511 if (!rc &&
512 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200514 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800515
Horst Hummel8f617012006-08-30 14:33:33 +0200516 if (!rc &&
517 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800518 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200519 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800520
Horst Hummel8f617012006-08-30 14:33:33 +0200521 if (!rc &&
522 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200524 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200525
Horst Hummel8f617012006-08-30 14:33:33 +0200526 if (!rc &&
527 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200529 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Horst Hummel8f617012006-08-30 14:33:33 +0200531 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * This is the main startup/shutdown routine.
536 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100537static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Sebastian Ott181d9522009-06-22 12:08:21 +0200539 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (device->state == device->target)
542 /* Already where we want to go today... */
543 return;
544 if (device->state < device->target)
545 rc = dasd_increase_state(device);
546 else
547 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200548 if (rc == -EAGAIN)
549 return;
550 if (rc)
551 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200553 /* let user-space know that the device status changed */
554 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200555
556 if (device->state == device->target)
557 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/*
561 * Kick starter for devices that did not complete the startup/shutdown
562 * procedure or were sleeping because of a pending state.
563 * dasd_kick_device will schedule a call do do_kick_device to the kernel
564 * event daemon.
565 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100566static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Al Viro4927b3f2006-12-06 19:18:20 +0000568 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100569 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100571 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100572 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 dasd_put_device(device);
574}
575
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100576void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 dasd_get_device(device);
579 /* queue call to dasd_kick_device to the kernel event daemon. */
580 schedule_work(&device->kick_work);
581}
582
583/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200584 * dasd_reload_device will schedule a call do do_reload_device to the kernel
585 * event daemon.
586 */
587static void do_reload_device(struct work_struct *work)
588{
589 struct dasd_device *device = container_of(work, struct dasd_device,
590 reload_device);
591 device->discipline->reload(device);
592 dasd_put_device(device);
593}
594
595void dasd_reload_device(struct dasd_device *device)
596{
597 dasd_get_device(device);
598 /* queue call to dasd_reload_device to the kernel event daemon. */
599 schedule_work(&device->reload_device);
600}
601EXPORT_SYMBOL(dasd_reload_device);
602
603/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200604 * dasd_restore_device will schedule a call do do_restore_device to the kernel
605 * event daemon.
606 */
607static void do_restore_device(struct work_struct *work)
608{
609 struct dasd_device *device = container_of(work, struct dasd_device,
610 restore_device);
611 device->cdev->drv->restore(device->cdev);
612 dasd_put_device(device);
613}
614
615void dasd_restore_device(struct dasd_device *device)
616{
617 dasd_get_device(device);
618 /* queue call to dasd_restore_device to the kernel event daemon. */
619 schedule_work(&device->restore_device);
620}
621
622/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * Set the target state for a device and starts the state change.
624 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100625void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200627 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100628 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* If we are in probeonly mode stop at DASD_STATE_READY. */
630 if (dasd_probeonly && target > DASD_STATE_READY)
631 target = DASD_STATE_READY;
632 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100633 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 wake_up(&dasd_init_waitq);
635 device->target = target;
636 }
637 if (device->state != device->target)
638 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100639 mutex_unlock(&device->state_mutex);
640 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/*
644 * Enable devices with device numbers in [from..to].
645 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100646static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 return (device->state == device->target);
649}
650
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100651void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 dasd_set_target_state(device, DASD_STATE_ONLINE);
654 if (device->state <= DASD_STATE_KNOWN)
655 /* No discipline for device found. */
656 dasd_set_target_state(device, DASD_STATE_NEW);
657 /* Now wait for the devices to come up. */
658 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400659
660 dasd_reload_device(device);
661 if (device->discipline->kick_validate)
662 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/*
666 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
667 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200668
669unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#ifdef CONFIG_DASD_PROFILE
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200672struct dasd_profile_info dasd_global_profile_data;
673static struct dentry *dasd_global_profile_dentry;
674static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676/*
677 * Add profiling information for cqr before execution.
678 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100679static void dasd_profile_start(struct dasd_block *block,
680 struct dasd_ccw_req *cqr,
681 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct list_head *l;
684 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200685 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* count the length of the chanq for statistics */
688 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200689 if (dasd_global_profile_level || block->profile.data)
690 list_for_each(l, &block->ccw_queue)
691 if (++counter >= 31)
692 break;
693
694 if (dasd_global_profile_level) {
695 dasd_global_profile_data.dasd_io_nr_req[counter]++;
696 if (rq_data_dir(req) == READ)
697 dasd_global_profile_data.dasd_read_nr_req[counter]++;
698 }
699
700 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200701 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200702 block->profile.data->dasd_io_nr_req[counter]++;
703 if (rq_data_dir(req) == READ)
704 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200705 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200706 spin_unlock(&block->profile.lock);
707
708 /*
709 * We count the request for the start device, even though it may run on
710 * some other device due to error recovery. This way we make sure that
711 * we count each request only once.
712 */
713 device = cqr->startdev;
714 if (device->profile.data) {
715 counter = 1; /* request is not yet queued on the start device */
716 list_for_each(l, &device->ccw_queue)
717 if (++counter >= 31)
718 break;
719 }
720 spin_lock(&device->profile.lock);
721 if (device->profile.data) {
722 device->profile.data->dasd_io_nr_req[counter]++;
723 if (rq_data_dir(req) == READ)
724 device->profile.data->dasd_read_nr_req[counter]++;
725 }
726 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/*
730 * Add profiling information for cqr after execution.
731 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200732
733#define dasd_profile_counter(value, index) \
734{ \
735 for (index = 0; index < 31 && value >> (2+index); index++) \
736 ; \
737}
738
739static void dasd_profile_end_add_data(struct dasd_profile_info *data,
740 int is_alias,
741 int is_tpm,
742 int is_read,
743 long sectors,
744 int sectors_ind,
745 int tottime_ind,
746 int tottimeps_ind,
747 int strtime_ind,
748 int irqtime_ind,
749 int irqtimeps_ind,
750 int endtime_ind)
751{
752 /* in case of an overflow, reset the whole profile */
753 if (data->dasd_io_reqs == UINT_MAX) {
754 memset(data, 0, sizeof(*data));
755 getnstimeofday(&data->starttod);
756 }
757 data->dasd_io_reqs++;
758 data->dasd_io_sects += sectors;
759 if (is_alias)
760 data->dasd_io_alias++;
761 if (is_tpm)
762 data->dasd_io_tpm++;
763
764 data->dasd_io_secs[sectors_ind]++;
765 data->dasd_io_times[tottime_ind]++;
766 data->dasd_io_timps[tottimeps_ind]++;
767 data->dasd_io_time1[strtime_ind]++;
768 data->dasd_io_time2[irqtime_ind]++;
769 data->dasd_io_time2ps[irqtimeps_ind]++;
770 data->dasd_io_time3[endtime_ind]++;
771
772 if (is_read) {
773 data->dasd_read_reqs++;
774 data->dasd_read_sects += sectors;
775 if (is_alias)
776 data->dasd_read_alias++;
777 if (is_tpm)
778 data->dasd_read_tpm++;
779 data->dasd_read_secs[sectors_ind]++;
780 data->dasd_read_times[tottime_ind]++;
781 data->dasd_read_time1[strtime_ind]++;
782 data->dasd_read_time2[irqtime_ind]++;
783 data->dasd_read_time3[endtime_ind]++;
784 }
785}
786
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100787static void dasd_profile_end(struct dasd_block *block,
788 struct dasd_ccw_req *cqr,
789 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 long strtime, irqtime, endtime, tottime; /* in microseconds */
792 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200793 struct dasd_device *device;
794 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
795 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200797 device = cqr->startdev;
798 if (!(dasd_global_profile_level ||
799 block->profile.data ||
800 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return;
802
Tejun Heo83096eb2009-05-07 22:24:39 +0900803 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!cqr->buildclk || !cqr->startclk ||
805 !cqr->stopclk || !cqr->endclk ||
806 !sectors)
807 return;
808
809 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
810 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
811 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
812 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
813 tottimeps = tottime / sectors;
814
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200815 dasd_profile_counter(sectors, sectors_ind);
816 dasd_profile_counter(tottime, tottime_ind);
817 dasd_profile_counter(tottimeps, tottimeps_ind);
818 dasd_profile_counter(strtime, strtime_ind);
819 dasd_profile_counter(irqtime, irqtime_ind);
820 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
821 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200823 if (dasd_global_profile_level) {
824 dasd_profile_end_add_data(&dasd_global_profile_data,
825 cqr->startdev != block->base,
826 cqr->cpmode == 1,
827 rq_data_dir(req) == READ,
828 sectors, sectors_ind, tottime_ind,
829 tottimeps_ind, strtime_ind,
830 irqtime_ind, irqtimeps_ind,
831 endtime_ind);
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200834 spin_lock(&block->profile.lock);
835 if (block->profile.data)
836 dasd_profile_end_add_data(block->profile.data,
837 cqr->startdev != block->base,
838 cqr->cpmode == 1,
839 rq_data_dir(req) == READ,
840 sectors, sectors_ind, tottime_ind,
841 tottimeps_ind, strtime_ind,
842 irqtime_ind, irqtimeps_ind,
843 endtime_ind);
844 spin_unlock(&block->profile.lock);
845
846 spin_lock(&device->profile.lock);
847 if (device->profile.data)
848 dasd_profile_end_add_data(device->profile.data,
849 cqr->startdev != block->base,
850 cqr->cpmode == 1,
851 rq_data_dir(req) == READ,
852 sectors, sectors_ind, tottime_ind,
853 tottimeps_ind, strtime_ind,
854 irqtime_ind, irqtimeps_ind,
855 endtime_ind);
856 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200858
859void dasd_profile_reset(struct dasd_profile *profile)
860{
861 struct dasd_profile_info *data;
862
863 spin_lock_bh(&profile->lock);
864 data = profile->data;
865 if (!data) {
866 spin_unlock_bh(&profile->lock);
867 return;
868 }
869 memset(data, 0, sizeof(*data));
870 getnstimeofday(&data->starttod);
871 spin_unlock_bh(&profile->lock);
872}
873
874void dasd_global_profile_reset(void)
875{
876 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
877 getnstimeofday(&dasd_global_profile_data.starttod);
878}
879
880int dasd_profile_on(struct dasd_profile *profile)
881{
882 struct dasd_profile_info *data;
883
884 data = kzalloc(sizeof(*data), GFP_KERNEL);
885 if (!data)
886 return -ENOMEM;
887 spin_lock_bh(&profile->lock);
888 if (profile->data) {
889 spin_unlock_bh(&profile->lock);
890 kfree(data);
891 return 0;
892 }
893 getnstimeofday(&data->starttod);
894 profile->data = data;
895 spin_unlock_bh(&profile->lock);
896 return 0;
897}
898
899void dasd_profile_off(struct dasd_profile *profile)
900{
901 spin_lock_bh(&profile->lock);
902 kfree(profile->data);
903 profile->data = NULL;
904 spin_unlock_bh(&profile->lock);
905}
906
907char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
908{
909 char *buffer;
910
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200911 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200912 if (buffer == NULL)
913 return ERR_PTR(-ENOMEM);
914 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200915 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200916 return ERR_PTR(-EFAULT);
917 }
918 /* got the string, now strip linefeed. */
919 if (buffer[user_len - 1] == '\n')
920 buffer[user_len - 1] = 0;
921 else
922 buffer[user_len] = 0;
923 return buffer;
924}
925
926static ssize_t dasd_stats_write(struct file *file,
927 const char __user *user_buf,
928 size_t user_len, loff_t *pos)
929{
930 char *buffer, *str;
931 int rc;
932 struct seq_file *m = (struct seq_file *)file->private_data;
933 struct dasd_profile *prof = m->private;
934
935 if (user_len > 65536)
936 user_len = 65536;
937 buffer = dasd_get_user_string(user_buf, user_len);
938 if (IS_ERR(buffer))
939 return PTR_ERR(buffer);
940
941 str = skip_spaces(buffer);
942 rc = user_len;
943 if (strncmp(str, "reset", 5) == 0) {
944 dasd_profile_reset(prof);
945 } else if (strncmp(str, "on", 2) == 0) {
946 rc = dasd_profile_on(prof);
947 if (!rc)
948 rc = user_len;
949 } else if (strncmp(str, "off", 3) == 0) {
950 dasd_profile_off(prof);
951 } else
952 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200953 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200954 return rc;
955}
956
957static void dasd_stats_array(struct seq_file *m, unsigned int *array)
958{
959 int i;
960
961 for (i = 0; i < 32; i++)
962 seq_printf(m, "%u ", array[i]);
963 seq_putc(m, '\n');
964}
965
966static void dasd_stats_seq_print(struct seq_file *m,
967 struct dasd_profile_info *data)
968{
969 seq_printf(m, "start_time %ld.%09ld\n",
970 data->starttod.tv_sec, data->starttod.tv_nsec);
971 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
972 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
973 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
974 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
975 seq_printf(m, "histogram_sectors ");
976 dasd_stats_array(m, data->dasd_io_secs);
977 seq_printf(m, "histogram_io_times ");
978 dasd_stats_array(m, data->dasd_io_times);
979 seq_printf(m, "histogram_io_times_weighted ");
980 dasd_stats_array(m, data->dasd_io_timps);
981 seq_printf(m, "histogram_time_build_to_ssch ");
982 dasd_stats_array(m, data->dasd_io_time1);
983 seq_printf(m, "histogram_time_ssch_to_irq ");
984 dasd_stats_array(m, data->dasd_io_time2);
985 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
986 dasd_stats_array(m, data->dasd_io_time2ps);
987 seq_printf(m, "histogram_time_irq_to_end ");
988 dasd_stats_array(m, data->dasd_io_time3);
989 seq_printf(m, "histogram_ccw_queue_length ");
990 dasd_stats_array(m, data->dasd_io_nr_req);
991 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
992 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
993 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
994 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
995 seq_printf(m, "histogram_read_sectors ");
996 dasd_stats_array(m, data->dasd_read_secs);
997 seq_printf(m, "histogram_read_times ");
998 dasd_stats_array(m, data->dasd_read_times);
999 seq_printf(m, "histogram_read_time_build_to_ssch ");
1000 dasd_stats_array(m, data->dasd_read_time1);
1001 seq_printf(m, "histogram_read_time_ssch_to_irq ");
1002 dasd_stats_array(m, data->dasd_read_time2);
1003 seq_printf(m, "histogram_read_time_irq_to_end ");
1004 dasd_stats_array(m, data->dasd_read_time3);
1005 seq_printf(m, "histogram_read_ccw_queue_length ");
1006 dasd_stats_array(m, data->dasd_read_nr_req);
1007}
1008
1009static int dasd_stats_show(struct seq_file *m, void *v)
1010{
1011 struct dasd_profile *profile;
1012 struct dasd_profile_info *data;
1013
1014 profile = m->private;
1015 spin_lock_bh(&profile->lock);
1016 data = profile->data;
1017 if (!data) {
1018 spin_unlock_bh(&profile->lock);
1019 seq_printf(m, "disabled\n");
1020 return 0;
1021 }
1022 dasd_stats_seq_print(m, data);
1023 spin_unlock_bh(&profile->lock);
1024 return 0;
1025}
1026
1027static int dasd_stats_open(struct inode *inode, struct file *file)
1028{
1029 struct dasd_profile *profile = inode->i_private;
1030 return single_open(file, dasd_stats_show, profile);
1031}
1032
1033static const struct file_operations dasd_stats_raw_fops = {
1034 .owner = THIS_MODULE,
1035 .open = dasd_stats_open,
1036 .read = seq_read,
1037 .llseek = seq_lseek,
1038 .release = single_release,
1039 .write = dasd_stats_write,
1040};
1041
1042static ssize_t dasd_stats_global_write(struct file *file,
1043 const char __user *user_buf,
1044 size_t user_len, loff_t *pos)
1045{
1046 char *buffer, *str;
1047 ssize_t rc;
1048
1049 if (user_len > 65536)
1050 user_len = 65536;
1051 buffer = dasd_get_user_string(user_buf, user_len);
1052 if (IS_ERR(buffer))
1053 return PTR_ERR(buffer);
1054 str = skip_spaces(buffer);
1055 rc = user_len;
1056 if (strncmp(str, "reset", 5) == 0) {
1057 dasd_global_profile_reset();
1058 } else if (strncmp(str, "on", 2) == 0) {
1059 dasd_global_profile_reset();
1060 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1061 } else if (strncmp(str, "off", 3) == 0) {
1062 dasd_global_profile_level = DASD_PROFILE_OFF;
1063 } else
1064 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001065 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001066 return rc;
1067}
1068
1069static int dasd_stats_global_show(struct seq_file *m, void *v)
1070{
1071 if (!dasd_global_profile_level) {
1072 seq_printf(m, "disabled\n");
1073 return 0;
1074 }
1075 dasd_stats_seq_print(m, &dasd_global_profile_data);
1076 return 0;
1077}
1078
1079static int dasd_stats_global_open(struct inode *inode, struct file *file)
1080{
1081 return single_open(file, dasd_stats_global_show, NULL);
1082}
1083
1084static const struct file_operations dasd_stats_global_fops = {
1085 .owner = THIS_MODULE,
1086 .open = dasd_stats_global_open,
1087 .read = seq_read,
1088 .llseek = seq_lseek,
1089 .release = single_release,
1090 .write = dasd_stats_global_write,
1091};
1092
1093static void dasd_profile_init(struct dasd_profile *profile,
1094 struct dentry *base_dentry)
1095{
Al Virof4ae40a2011-07-24 04:33:43 -04001096 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001097 struct dentry *pde;
1098
1099 if (!base_dentry)
1100 return;
1101 profile->dentry = NULL;
1102 profile->data = NULL;
1103 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1104 pde = debugfs_create_file("statistics", mode, base_dentry,
1105 profile, &dasd_stats_raw_fops);
1106 if (pde && !IS_ERR(pde))
1107 profile->dentry = pde;
1108 return;
1109}
1110
1111static void dasd_profile_exit(struct dasd_profile *profile)
1112{
1113 dasd_profile_off(profile);
1114 if (profile->dentry) {
1115 debugfs_remove(profile->dentry);
1116 profile->dentry = NULL;
1117 }
1118}
1119
1120static void dasd_statistics_removeroot(void)
1121{
1122 dasd_global_profile_level = DASD_PROFILE_OFF;
1123 if (dasd_global_profile_dentry) {
1124 debugfs_remove(dasd_global_profile_dentry);
1125 dasd_global_profile_dentry = NULL;
1126 }
1127 if (dasd_debugfs_global_entry)
1128 debugfs_remove(dasd_debugfs_global_entry);
1129 if (dasd_debugfs_root_entry)
1130 debugfs_remove(dasd_debugfs_root_entry);
1131}
1132
1133static void dasd_statistics_createroot(void)
1134{
Al Virof4ae40a2011-07-24 04:33:43 -04001135 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001136 struct dentry *pde;
1137
1138 dasd_debugfs_root_entry = NULL;
1139 dasd_debugfs_global_entry = NULL;
1140 dasd_global_profile_dentry = NULL;
1141 pde = debugfs_create_dir("dasd", NULL);
1142 if (!pde || IS_ERR(pde))
1143 goto error;
1144 dasd_debugfs_root_entry = pde;
1145 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1146 if (!pde || IS_ERR(pde))
1147 goto error;
1148 dasd_debugfs_global_entry = pde;
1149
1150 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1151 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1152 NULL, &dasd_stats_global_fops);
1153 if (!pde || IS_ERR(pde))
1154 goto error;
1155 dasd_global_profile_dentry = pde;
1156 return;
1157
1158error:
1159 DBF_EVENT(DBF_ERR, "%s",
1160 "Creation of the dasd debugfs interface failed");
1161 dasd_statistics_removeroot();
1162 return;
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001166#define dasd_profile_start(block, cqr, req) do {} while (0)
1167#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001168
1169static void dasd_statistics_createroot(void)
1170{
1171 return;
1172}
1173
1174static void dasd_statistics_removeroot(void)
1175{
1176 return;
1177}
1178
1179int dasd_stats_generic_show(struct seq_file *m, void *v)
1180{
1181 seq_printf(m, "Statistics are not activated in this kernel\n");
1182 return 0;
1183}
1184
1185static void dasd_profile_init(struct dasd_profile *profile,
1186 struct dentry *base_dentry)
1187{
1188 return;
1189}
1190
1191static void dasd_profile_exit(struct dasd_profile *profile)
1192{
1193 return;
1194}
1195
1196int dasd_profile_on(struct dasd_profile *profile)
1197{
1198 return 0;
1199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#endif /* CONFIG_DASD_PROFILE */
1202
1203/*
1204 * Allocate memory for a channel program with 'cplength' channel
1205 * command words and 'datasize' additional space. There are two
1206 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1207 * memory and 2) dasd_smalloc_request uses the static ccw memory
1208 * that gets allocated for each device.
1209 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001210struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001211 int datasize,
1212 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct dasd_ccw_req *cqr;
1215
1216 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001217 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001218 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001220 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (cqr == NULL)
1222 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 cqr->cpaddr = NULL;
1224 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001225 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 GFP_ATOMIC | GFP_DMA);
1227 if (cqr->cpaddr == NULL) {
1228 kfree(cqr);
1229 return ERR_PTR(-ENOMEM);
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 cqr->data = NULL;
1233 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001234 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001236 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 kfree(cqr);
1238 return ERR_PTR(-ENOMEM);
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001241 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1243 dasd_get_device(device);
1244 return cqr;
1245}
1246
Stefan Haberland68b781f2009-09-11 10:28:29 +02001247struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001248 int datasize,
1249 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 unsigned long flags;
1252 struct dasd_ccw_req *cqr;
1253 char *data;
1254 int size;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1257 if (cplength > 0)
1258 size += cplength * sizeof(struct ccw1);
1259 if (datasize > 0)
1260 size += datasize;
1261 spin_lock_irqsave(&device->mem_lock, flags);
1262 cqr = (struct dasd_ccw_req *)
1263 dasd_alloc_chunk(&device->ccw_chunks, size);
1264 spin_unlock_irqrestore(&device->mem_lock, flags);
1265 if (cqr == NULL)
1266 return ERR_PTR(-ENOMEM);
1267 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1268 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1269 cqr->cpaddr = NULL;
1270 if (cplength > 0) {
1271 cqr->cpaddr = (struct ccw1 *) data;
1272 data += cplength*sizeof(struct ccw1);
1273 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1274 }
1275 cqr->data = NULL;
1276 if (datasize > 0) {
1277 cqr->data = data;
1278 memset(cqr->data, 0, datasize);
1279 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001280 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1282 dasd_get_device(device);
1283 return cqr;
1284}
1285
1286/*
1287 * Free memory of a channel program. This function needs to free all the
1288 * idal lists that might have been created by dasd_set_cda and the
1289 * struct dasd_ccw_req itself.
1290 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001291void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001293#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct ccw1 *ccw;
1295
1296 /* Clear any idals used for the request. */
1297 ccw = cqr->cpaddr;
1298 do {
1299 clear_normalized_cda(ccw);
1300 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1301#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001302 kfree(cqr->cpaddr);
1303 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 kfree(cqr);
1305 dasd_put_device(device);
1306}
1307
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001308void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 unsigned long flags;
1311
1312 spin_lock_irqsave(&device->mem_lock, flags);
1313 dasd_free_chunk(&device->ccw_chunks, cqr);
1314 spin_unlock_irqrestore(&device->mem_lock, flags);
1315 dasd_put_device(device);
1316}
1317
1318/*
1319 * Check discipline magic in cqr.
1320 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001321static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct dasd_device *device;
1324
1325 if (cqr == NULL)
1326 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001327 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001329 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 " dasd_ccw_req 0x%08x magic doesn't match"
1331 " discipline 0x%08x",
1332 cqr->magic,
1333 *(unsigned int *) device->discipline->name);
1334 return -EINVAL;
1335 }
1336 return 0;
1337}
1338
1339/*
1340 * Terminate the current i/o and set the request to clear_pending.
1341 * Timer keeps device runnig.
1342 * ccw_device_clear can fail if the i/o subsystem
1343 * is in a bad mood.
1344 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001345int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct dasd_device *device;
1348 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001349 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* Check the cqr */
1352 rc = dasd_check_cqr(cqr);
1353 if (rc)
1354 return rc;
1355 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001356 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1358 rc = ccw_device_clear(device->cdev, (long) cqr);
1359 switch (rc) {
1360 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001361 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001362 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001363 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 DBF_DEV_EVENT(DBF_DEBUG, device,
1365 "terminate cqr %p successful",
1366 cqr);
1367 break;
1368 case -ENODEV:
1369 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1370 "device gone, retry");
1371 break;
1372 case -EIO:
1373 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1374 "I/O error, retry");
1375 break;
1376 case -EINVAL:
1377 case -EBUSY:
1378 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1379 "device busy, retry later");
1380 break;
1381 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001382 /* internal error 10 - unknown rc*/
1383 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1384 dev_err(&device->cdev->dev, "An error occurred in the "
1385 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 BUG();
1387 break;
1388 }
1389 retries++;
1390 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001391 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return rc;
1393}
1394
1395/*
1396 * Start the i/o. This start_IO can fail if the channel is really busy.
1397 * In that case set up a timer to start the request later.
1398 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001399int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 struct dasd_device *device;
1402 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001403 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Check the cqr */
1406 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001407 if (rc) {
1408 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001410 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001411 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001412 if (((cqr->block &&
1413 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1414 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1415 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1416 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1417 "because of stolen lock", cqr);
1418 cqr->status = DASD_CQR_ERROR;
1419 cqr->intrc = -EPERM;
1420 return -EPERM;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001423 /* internal error 14 - start_IO run out of retries */
1424 sprintf(errorstring, "14 %p", cqr);
1425 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1426 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001427 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EIO;
1429 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001430 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 cqr->starttime = jiffies;
1432 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001433 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1434 cqr->lpm &= device->path_data.opm;
1435 if (!cqr->lpm)
1436 cqr->lpm = device->path_data.opm;
1437 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001438 if (cqr->cpmode == 1) {
1439 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1440 (long) cqr, cqr->lpm);
1441 } else {
1442 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1443 (long) cqr, cqr->lpm, 0);
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 switch (rc) {
1446 case 0:
1447 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001450 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 "start_IO: device busy, retry later");
1452 break;
1453 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001454 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 "start_IO: request timeout, retry later");
1456 break;
1457 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 /* -EACCES indicates that the request used only a subset of the
1459 * available paths and all these paths are gone. If the lpm of
1460 * this request was only a subset of the opm (e.g. the ppm) then
1461 * we just do a retry with all available paths.
1462 * If we already use the full opm, something is amiss, and we
1463 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001465 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1466 DBF_DEV_EVENT(DBF_WARNING, device,
1467 "start_IO: selected paths gone (%x)",
1468 cqr->lpm);
1469 } else if (cqr->lpm != device->path_data.opm) {
1470 cqr->lpm = device->path_data.opm;
1471 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1472 "start_IO: selected paths gone,"
1473 " retry on all paths");
1474 } else {
1475 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1476 "start_IO: all paths in opm gone,"
1477 " do path verification");
1478 dasd_generic_last_path_gone(device);
1479 device->path_data.opm = 0;
1480 device->path_data.ppm = 0;
1481 device->path_data.npm = 0;
1482 device->path_data.tbvpm =
1483 ccw_device_get_path_mask(device->cdev);
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001487 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001488 "start_IO: -ENODEV device gone, retry");
1489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001491 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001492 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001494 case -EINVAL:
1495 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001496 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001497 "start_IO: -EINVAL device currently "
1498 "not accessible");
1499 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001501 /* internal error 11 - unknown rc */
1502 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1503 dev_err(&device->cdev->dev,
1504 "An error occurred in the DASD device driver, "
1505 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 BUG();
1507 break;
1508 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001509 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return rc;
1511}
1512
1513/*
1514 * Timeout function for dasd devices. This is used for different purposes
1515 * 1) missing interrupt handler for normal operation
1516 * 2) delayed start of request where start_IO failed with -EBUSY
1517 * 3) timeout for missing state change interrupts
1518 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1519 * DASD_CQR_QUEUED for 2) and 3).
1520 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001521static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 unsigned long flags;
1524 struct dasd_device *device;
1525
1526 device = (struct dasd_device *) ptr;
1527 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1528 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001529 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001531 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/*
1535 * Setup timeout for a device in jiffies.
1536 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001537void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001539 if (expires == 0)
1540 del_timer(&device->timer);
1541 else
1542 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545/*
1546 * Clear timeout for a device.
1547 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001548void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001550 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001553static void dasd_handle_killed_request(struct ccw_device *cdev,
1554 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 struct dasd_ccw_req *cqr;
1557 struct dasd_device *device;
1558
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001559 if (!intparm)
1560 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 cqr = (struct dasd_ccw_req *) intparm;
1562 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001563 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1564 "invalid status in handle_killed_request: "
1565 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return;
1567 }
1568
Stefan Haberland589c74d2010-02-26 22:37:47 +01001569 device = dasd_device_from_cdev_locked(cdev);
1570 if (IS_ERR(device)) {
1571 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1572 "unable to get device from cdev");
1573 return;
1574 }
1575
1576 if (!cqr->startdev ||
1577 device != cqr->startdev ||
1578 strncmp(cqr->startdev->discipline->ebcname,
1579 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001580 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1581 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001582 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return;
1584 }
1585
1586 /* Schedule request to be retried. */
1587 cqr->status = DASD_CQR_QUEUED;
1588
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001589 dasd_device_clear_timer(device);
1590 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 dasd_put_device(device);
1592}
1593
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001594void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001596 /* First of all start sense subsystem status request. */
1597 dasd_eer_snss(device);
1598
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001599 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001600 dasd_schedule_device_bh(device);
1601 if (device->block)
1602 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605/*
1606 * Interrupt handler for "normal" ssch-io based dasd devices.
1607 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001608void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1609 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 struct dasd_ccw_req *cqr, *next;
1612 struct dasd_device *device;
1613 unsigned long long now;
1614 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 if (IS_ERR(irb)) {
1617 switch (PTR_ERR(irb)) {
1618 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 break;
1620 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001621 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1622 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 break;
1624 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001625 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1626 "unknown error %ld\n", __func__,
1627 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001629 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return;
1631 }
1632
Heiko Carstens1aae0562013-01-30 09:49:40 +01001633 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001634 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001635 /* check for conditions that should be handled immediately */
1636 if (!cqr ||
1637 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1638 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001639 if (cqr)
1640 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001641 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001642 if (IS_ERR(device))
1643 return;
1644 /* ignore unsolicited interrupts for DIAG discipline */
1645 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001649 device->discipline->dump_sense_dbf(device, irb, "int");
1650 if (device->features & DASD_FEATURE_ERPLOG)
1651 device->discipline->dump_sense(device, cqr, irb);
1652 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001653 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001655 if (!cqr)
1656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001658 device = (struct dasd_device *) cqr->startdev;
1659 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001661 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1662 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
1664 }
1665
1666 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001667 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001668 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001669 cqr->status = DASD_CQR_CLEARED;
1670 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001671 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001672 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return;
1674 }
1675
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001676 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001678 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1679 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return;
1681 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001682
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001683 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001685 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1686 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001687 /* request was completed successfully */
1688 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 cqr->stopclk = now;
1690 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001691 if (cqr->devlist.next != &device->ccw_queue) {
1692 next = list_entry(cqr->devlist.next,
1693 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001695 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001696 /*
1697 * If we don't want complex ERP for this request, then just
1698 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001699 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001700 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001701 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001702 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001703 DBF_DEV_EVENT(DBF_DEBUG, device,
1704 "default ERP in fastpath "
1705 "(%i retries left)",
1706 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001707 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1708 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001709 cqr->status = DASD_CQR_QUEUED;
1710 next = cqr;
1711 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001713 }
1714 if (next && (next->status == DASD_CQR_QUEUED) &&
1715 (!device->stopped)) {
1716 if (device->discipline->start_IO(next) == 0)
1717 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001720 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001722 dasd_device_clear_timer(device);
1723 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001726enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1727{
1728 struct dasd_device *device;
1729
1730 device = dasd_device_from_cdev_locked(cdev);
1731
1732 if (IS_ERR(device))
1733 goto out;
1734 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1735 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001736 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001737 dasd_put_device(device);
1738 goto out;
1739 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001740 if (device->discipline->dump_sense_dbf)
1741 device->discipline->dump_sense_dbf(device, irb, "uc");
1742 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001743 dasd_put_device(device);
1744out:
1745 return UC_TODO_RETRY;
1746}
1747EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001750 * If we have an error on a dasd_block layer request then we cancel
1751 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001753static void __dasd_device_recovery(struct dasd_device *device,
1754 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 struct list_head *l, *n;
1757 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001764
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001765 list_for_each_safe(l, n, &device->ccw_queue) {
1766 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1767 if (cqr->status == DASD_CQR_QUEUED &&
1768 ref_cqr->block == cqr->block) {
1769 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001771 }
1772};
1773
1774/*
1775 * Remove those ccw requests from the queue that need to be returned
1776 * to the upper layer.
1777 */
1778static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1779 struct list_head *final_queue)
1780{
1781 struct list_head *l, *n;
1782 struct dasd_ccw_req *cqr;
1783
1784 /* Process request with final status. */
1785 list_for_each_safe(l, n, &device->ccw_queue) {
1786 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1787
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001788 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001789 if (cqr->status == DASD_CQR_QUEUED ||
1790 cqr->status == DASD_CQR_IN_IO ||
1791 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001792 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001793 if (cqr->status == DASD_CQR_ERROR) {
1794 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001796 /* Rechain finished requests to final queue */
1797 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799}
1800
1801/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001802 * the cqrs from the final queue are returned to the upper layer
1803 * by setting a dasd_block state and calling the callback function
1804 */
1805static void __dasd_device_process_final_queue(struct dasd_device *device,
1806 struct list_head *final_queue)
1807{
1808 struct list_head *l, *n;
1809 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001810 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001811 void (*callback)(struct dasd_ccw_req *, void *data);
1812 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001813 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001814
1815 list_for_each_safe(l, n, final_queue) {
1816 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1817 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001818 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001819 callback = cqr->callback;
1820 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001821 if (block)
1822 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001823 switch (cqr->status) {
1824 case DASD_CQR_SUCCESS:
1825 cqr->status = DASD_CQR_DONE;
1826 break;
1827 case DASD_CQR_ERROR:
1828 cqr->status = DASD_CQR_NEED_ERP;
1829 break;
1830 case DASD_CQR_CLEARED:
1831 cqr->status = DASD_CQR_TERMINATED;
1832 break;
1833 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001834 /* internal error 12 - wrong cqr status*/
1835 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1836 dev_err(&device->cdev->dev,
1837 "An error occurred in the DASD device driver, "
1838 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001839 BUG();
1840 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001841 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001842 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001843 if (block)
1844 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001845 }
1846}
1847
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * Take a look at the first request on the ccw queue and check
1850 * if it reached its expire time. If so, terminate the IO.
1851 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001852static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct dasd_ccw_req *cqr;
1855
1856 if (list_empty(&device->ccw_queue))
1857 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001858 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001859 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1860 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001861 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1862 /*
1863 * IO in safe offline processing should not
1864 * run out of retries
1865 */
1866 cqr->retries++;
1867 }
Horst Hummel29145a62006-12-04 15:40:15 +01001868 if (device->discipline->term_IO(cqr) != 0) {
1869 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001870 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001871 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001872 "ended, retrying in 5 s\n",
1873 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001874 cqr->expires += 5*HZ;
1875 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001876 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001877 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001878 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001879 "remaining\n", cqr, (cqr->expires/HZ),
1880 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 }
1883}
1884
1885/*
1886 * Take a look at the first request on the ccw queue and check
1887 * if it needs to be started.
1888 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001889static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 struct dasd_ccw_req *cqr;
1892 int rc;
1893
1894 if (list_empty(&device->ccw_queue))
1895 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001896 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001897 if (cqr->status != DASD_CQR_QUEUED)
1898 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001899 /* when device is stopped, return request to previous layer
1900 * exception: only the disconnect or unresumed bits are set and the
1901 * cqr is a path verification request
1902 */
1903 if (device->stopped &&
1904 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1905 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1906 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001907 cqr->status = DASD_CQR_CLEARED;
1908 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001909 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001910 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001911
1912 rc = device->discipline->start_IO(cqr);
1913 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001914 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001915 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001916 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001917 } else
1918 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001919 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001920}
1921
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001922static void __dasd_device_check_path_events(struct dasd_device *device)
1923{
1924 int rc;
1925
1926 if (device->path_data.tbvpm) {
1927 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1928 DASD_UNRESUMED_PM))
1929 return;
1930 rc = device->discipline->verify_path(
1931 device, device->path_data.tbvpm);
1932 if (rc)
1933 dasd_device_set_timer(device, 50);
1934 else
1935 device->path_data.tbvpm = 0;
1936 }
1937};
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001940 * Go through all request on the dasd_device request queue,
1941 * terminate them on the cdev if necessary, and return them to the
1942 * submitting layer via callback.
1943 * Note:
1944 * Make sure that all 'submitting layers' still exist when
1945 * this function is called!. In other words, when 'device' is a base
1946 * device then all block layer requests must have been removed before
1947 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001951 struct dasd_ccw_req *cqr, *n;
1952 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 INIT_LIST_HEAD(&flush_queue);
1956 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001957 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001958 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001959 /* Check status and move request to flush_queue */
1960 switch (cqr->status) {
1961 case DASD_CQR_IN_IO:
1962 rc = device->discipline->term_IO(cqr);
1963 if (rc) {
1964 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001965 dev_err(&device->cdev->dev,
1966 "Flushing the DASD request queue "
1967 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001968 /* stop flush processing */
1969 goto finished;
1970 }
1971 break;
1972 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001973 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001974 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001975 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001976 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001977 break;
1978 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001979 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001980 }
Horst Hummel8f617012006-08-30 14:33:33 +02001981finished:
1982 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001983 /*
1984 * After this point all requests must be in state CLEAR_PENDING,
1985 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1986 * one of the others.
1987 */
1988 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1989 wait_event(dasd_flush_wq,
1990 (cqr->status != DASD_CQR_CLEAR_PENDING));
1991 /*
1992 * Now set each request back to TERMINATED, DONE or NEED_ERP
1993 * and call the callback function of flushed requests
1994 */
1995 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001996 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999/*
2000 * Acquire the device lock and process queues for the device.
2001 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002002static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 atomic_set (&device->tasklet_scheduled, 0);
2007 INIT_LIST_HEAD(&final_queue);
2008 spin_lock_irq(get_ccwdev_lock(device->cdev));
2009 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002010 __dasd_device_check_expire(device);
2011 /* find final requests on ccw queue */
2012 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002013 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2015 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002016 __dasd_device_process_final_queue(device, &final_queue);
2017 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002019 __dasd_device_start_head(device);
2020 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002021 if (waitqueue_active(&shutdown_waitq))
2022 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 dasd_put_device(device);
2024}
2025
2026/*
2027 * Schedules a call to dasd_tasklet over the device tasklet.
2028 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002029void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002032 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return;
2034 dasd_get_device(device);
2035 tasklet_hi_schedule(&device->tasklet);
2036}
2037
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002038void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2039{
2040 device->stopped |= bits;
2041}
2042EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2043
2044void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2045{
2046 device->stopped &= ~bits;
2047 if (!device->stopped)
2048 wake_up(&generic_waitq);
2049}
2050EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002053 * Queue a request to the head of the device ccw_queue.
2054 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002056void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct dasd_device *device;
2059 unsigned long flags;
2060
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002061 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002063 cqr->status = DASD_CQR_QUEUED;
2064 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002066 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2068}
2069
2070/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002071 * Queue a request to the tail of the device ccw_queue.
2072 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002074void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct dasd_device *device;
2077 unsigned long flags;
2078
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002079 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002081 cqr->status = DASD_CQR_QUEUED;
2082 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2086}
2087
2088/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002089 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002091void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002093 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2094 cqr->callback_data = DASD_SLEEPON_END_TAG;
2095 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2096 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
Stefan Haberland5915a872011-10-30 15:16:57 +01002098EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002100static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct dasd_device *device;
2103 int rc;
2104
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002105 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002107 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2109 return rc;
2110}
2111
2112/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002113 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2114 */
2115static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2116{
2117 struct dasd_device *device;
2118 dasd_erp_fn_t erp_fn;
2119
2120 if (cqr->status == DASD_CQR_FILLED)
2121 return 0;
2122 device = cqr->startdev;
2123 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2124 if (cqr->status == DASD_CQR_TERMINATED) {
2125 device->discipline->handle_terminated_request(cqr);
2126 return 1;
2127 }
2128 if (cqr->status == DASD_CQR_NEED_ERP) {
2129 erp_fn = device->discipline->erp_action(cqr);
2130 erp_fn(cqr);
2131 return 1;
2132 }
2133 if (cqr->status == DASD_CQR_FAILED)
2134 dasd_log_sense(cqr, &cqr->irb);
2135 if (cqr->refers) {
2136 __dasd_process_erp(device, cqr);
2137 return 1;
2138 }
2139 }
2140 return 0;
2141}
2142
2143static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2144{
2145 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2146 if (cqr->refers) /* erp is not done yet */
2147 return 1;
2148 return ((cqr->status != DASD_CQR_DONE) &&
2149 (cqr->status != DASD_CQR_FAILED));
2150 } else
2151 return (cqr->status == DASD_CQR_FILLED);
2152}
2153
2154static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2155{
2156 struct dasd_device *device;
2157 int rc;
2158 struct list_head ccw_queue;
2159 struct dasd_ccw_req *cqr;
2160
2161 INIT_LIST_HEAD(&ccw_queue);
2162 maincqr->status = DASD_CQR_FILLED;
2163 device = maincqr->startdev;
2164 list_add(&maincqr->blocklist, &ccw_queue);
2165 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2166 cqr = list_first_entry(&ccw_queue,
2167 struct dasd_ccw_req, blocklist)) {
2168
2169 if (__dasd_sleep_on_erp(cqr))
2170 continue;
2171 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2172 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002173 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2174 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2175 cqr->status = DASD_CQR_FAILED;
2176 cqr->intrc = -EPERM;
2177 continue;
2178 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002179 /* Non-temporary stop condition will trigger fail fast */
2180 if (device->stopped & ~DASD_STOPPED_PENDING &&
2181 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2182 (!dasd_eer_enabled(device))) {
2183 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002184 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002185 continue;
2186 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002187 /* Don't try to start requests if device is stopped */
2188 if (interruptible) {
2189 rc = wait_event_interruptible(
2190 generic_waitq, !(device->stopped));
2191 if (rc == -ERESTARTSYS) {
2192 cqr->status = DASD_CQR_FAILED;
2193 maincqr->intrc = rc;
2194 continue;
2195 }
2196 } else
2197 wait_event(generic_waitq, !(device->stopped));
2198
Stefan Haberland5915a872011-10-30 15:16:57 +01002199 if (!cqr->callback)
2200 cqr->callback = dasd_wakeup_cb;
2201
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002202 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002203 dasd_add_request_tail(cqr);
2204 if (interruptible) {
2205 rc = wait_event_interruptible(
2206 generic_waitq, _wait_for_wakeup(cqr));
2207 if (rc == -ERESTARTSYS) {
2208 dasd_cancel_req(cqr);
2209 /* wait (non-interruptible) for final status */
2210 wait_event(generic_waitq,
2211 _wait_for_wakeup(cqr));
2212 cqr->status = DASD_CQR_FAILED;
2213 maincqr->intrc = rc;
2214 continue;
2215 }
2216 } else
2217 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2218 }
2219
Heiko Carstens1aae0562013-01-30 09:49:40 +01002220 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002221 if ((maincqr->status != DASD_CQR_DONE) &&
2222 (maincqr->intrc != -ERESTARTSYS))
2223 dasd_log_sense(maincqr, &maincqr->irb);
2224 if (maincqr->status == DASD_CQR_DONE)
2225 rc = 0;
2226 else if (maincqr->intrc)
2227 rc = maincqr->intrc;
2228 else
2229 rc = -EIO;
2230 return rc;
2231}
2232
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002233static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2234{
2235 struct dasd_ccw_req *cqr;
2236
2237 list_for_each_entry(cqr, ccw_queue, blocklist) {
2238 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2239 return 0;
2240 }
2241
2242 return 1;
2243}
2244
2245static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2246{
2247 struct dasd_device *device;
2248 int rc;
2249 struct dasd_ccw_req *cqr, *n;
2250
2251retry:
2252 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2253 device = cqr->startdev;
2254 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2255 continue;
2256
2257 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2258 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2259 cqr->status = DASD_CQR_FAILED;
2260 cqr->intrc = -EPERM;
2261 continue;
2262 }
2263 /*Non-temporary stop condition will trigger fail fast*/
2264 if (device->stopped & ~DASD_STOPPED_PENDING &&
2265 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2266 !dasd_eer_enabled(device)) {
2267 cqr->status = DASD_CQR_FAILED;
2268 cqr->intrc = -EAGAIN;
2269 continue;
2270 }
2271
2272 /*Don't try to start requests if device is stopped*/
2273 if (interruptible) {
2274 rc = wait_event_interruptible(
2275 generic_waitq, !device->stopped);
2276 if (rc == -ERESTARTSYS) {
2277 cqr->status = DASD_CQR_FAILED;
2278 cqr->intrc = rc;
2279 continue;
2280 }
2281 } else
2282 wait_event(generic_waitq, !(device->stopped));
2283
2284 if (!cqr->callback)
2285 cqr->callback = dasd_wakeup_cb;
2286 cqr->callback_data = DASD_SLEEPON_START_TAG;
2287 dasd_add_request_tail(cqr);
2288 }
2289
2290 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2291
2292 rc = 0;
2293 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2294 if (__dasd_sleep_on_erp(cqr))
2295 rc = 1;
2296 }
2297 if (rc)
2298 goto retry;
2299
2300
2301 return 0;
2302}
2303
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002304/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002305 * Queue a request to the tail of the device ccw_queue and wait for
2306 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002308int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002310 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002314 * Start requests from a ccw_queue and wait for their completion.
2315 */
2316int dasd_sleep_on_queue(struct list_head *ccw_queue)
2317{
2318 return _dasd_sleep_on_queue(ccw_queue, 0);
2319}
2320EXPORT_SYMBOL(dasd_sleep_on_queue);
2321
2322/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002323 * Queue a request to the tail of the device ccw_queue and wait
2324 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002326int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002328 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
2331/*
2332 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2333 * for eckd devices) the currently running request has to be terminated
2334 * and be put back to status queued, before the special request is added
2335 * to the head of the queue. Then the special request is waited on normally.
2336 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002337static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002340 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 if (list_empty(&device->ccw_queue))
2343 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002344 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002345 rc = device->discipline->term_IO(cqr);
2346 if (!rc)
2347 /*
2348 * CQR terminated because a more important request is pending.
2349 * Undo decreasing of retry counter because this is
2350 * not an error case.
2351 */
2352 cqr->retries++;
2353 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002356int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct dasd_device *device;
2359 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002360
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002361 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002362 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2363 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2364 cqr->status = DASD_CQR_FAILED;
2365 cqr->intrc = -EPERM;
2366 return -EIO;
2367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 spin_lock_irq(get_ccwdev_lock(device->cdev));
2369 rc = _dasd_term_running_cqr(device);
2370 if (rc) {
2371 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2372 return rc;
2373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002375 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002377 /*
2378 * add new request as second
2379 * first the terminated cqr needs to be finished
2380 */
2381 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002384 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2387
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002388 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002389
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002390 if (cqr->status == DASD_CQR_DONE)
2391 rc = 0;
2392 else if (cqr->intrc)
2393 rc = cqr->intrc;
2394 else
2395 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002396
2397 /* kick tasklets */
2398 dasd_schedule_device_bh(device);
2399 if (device->block)
2400 dasd_schedule_block_bh(device->block);
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return rc;
2403}
2404
2405/*
2406 * Cancels a request that was started with dasd_sleep_on_req.
2407 * This is useful to timeout requests. The request will be
2408 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002409 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002410 * negative error code if termination failed
2411 * Cancellation of a request is an asynchronous operation! The calling
2412 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002414int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002416 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 unsigned long flags;
2418 int rc;
2419
2420 rc = 0;
2421 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2422 switch (cqr->status) {
2423 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002424 /* request was not started - just set to cleared */
2425 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 break;
2427 case DASD_CQR_IN_IO:
2428 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002429 rc = device->discipline->term_IO(cqr);
2430 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002431 dev_err(&device->cdev->dev,
2432 "Cancelling request %p failed with rc=%d\n",
2433 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002434 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002435 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002438 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002442 dasd_schedule_device_bh(device);
2443 return rc;
2444}
2445
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002446/*
2447 * SECTION: Operations of the dasd_block layer.
2448 */
2449
2450/*
2451 * Timeout function for dasd_block. This is used when the block layer
2452 * is waiting for something that may not come reliably, (e.g. a state
2453 * change interrupt)
2454 */
2455static void dasd_block_timeout(unsigned long ptr)
2456{
2457 unsigned long flags;
2458 struct dasd_block *block;
2459
2460 block = (struct dasd_block *) ptr;
2461 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2462 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002463 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002464 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2465 dasd_schedule_block_bh(block);
2466}
2467
2468/*
2469 * Setup timeout for a dasd_block in jiffies.
2470 */
2471void dasd_block_set_timer(struct dasd_block *block, int expires)
2472{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002473 if (expires == 0)
2474 del_timer(&block->timer);
2475 else
2476 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002477}
2478
2479/*
2480 * Clear timeout for a dasd_block.
2481 */
2482void dasd_block_clear_timer(struct dasd_block *block)
2483{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002484 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002485}
2486
2487/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002488 * Process finished error recovery ccw.
2489 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002490static void __dasd_process_erp(struct dasd_device *device,
2491 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002492{
2493 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002494
2495 if (cqr->status == DASD_CQR_DONE)
2496 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2497 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002498 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002499 erp_fn = device->discipline->erp_postaction(cqr);
2500 erp_fn(cqr);
2501}
2502
2503/*
2504 * Fetch requests from the block device queue.
2505 */
2506static void __dasd_process_request_queue(struct dasd_block *block)
2507{
2508 struct request_queue *queue;
2509 struct request *req;
2510 struct dasd_ccw_req *cqr;
2511 struct dasd_device *basedev;
2512 unsigned long flags;
2513 queue = block->request_queue;
2514 basedev = block->base;
2515 /* No queue ? Then there is nothing to do. */
2516 if (queue == NULL)
2517 return;
2518
2519 /*
2520 * We requeue request from the block device queue to the ccw
2521 * queue only in two states. In state DASD_STATE_READY the
2522 * partition detection is done and we need to requeue requests
2523 * for that. State DASD_STATE_ONLINE is normal block device
2524 * operation.
2525 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002526 if (basedev->state < DASD_STATE_READY) {
2527 while ((req = blk_fetch_request(block->request_queue)))
2528 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002529 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002530 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002531 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002532 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002533 if (basedev->features & DASD_FEATURE_READONLY &&
2534 rq_data_dir(req) == WRITE) {
2535 DBF_DEV_EVENT(DBF_ERR, basedev,
2536 "Rejecting write request %p",
2537 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002538 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002539 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002540 continue;
2541 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002542 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2543 (basedev->features & DASD_FEATURE_FAILFAST ||
2544 blk_noretry_request(req))) {
2545 DBF_DEV_EVENT(DBF_ERR, basedev,
2546 "Rejecting failfast request %p",
2547 req);
2548 blk_start_request(req);
2549 __blk_end_request_all(req, -ETIMEDOUT);
2550 continue;
2551 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002552 cqr = basedev->discipline->build_cp(basedev, block, req);
2553 if (IS_ERR(cqr)) {
2554 if (PTR_ERR(cqr) == -EBUSY)
2555 break; /* normal end condition */
2556 if (PTR_ERR(cqr) == -ENOMEM)
2557 break; /* terminate request queue loop */
2558 if (PTR_ERR(cqr) == -EAGAIN) {
2559 /*
2560 * The current request cannot be build right
2561 * now, we have to try later. If this request
2562 * is the head-of-queue we stop the device
2563 * for 1/2 second.
2564 */
2565 if (!list_empty(&block->ccw_queue))
2566 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002567 spin_lock_irqsave(
2568 get_ccwdev_lock(basedev->cdev), flags);
2569 dasd_device_set_stop_bits(basedev,
2570 DASD_STOPPED_PENDING);
2571 spin_unlock_irqrestore(
2572 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002573 dasd_block_set_timer(block, HZ/2);
2574 break;
2575 }
2576 DBF_DEV_EVENT(DBF_ERR, basedev,
2577 "CCW creation failed (rc=%ld) "
2578 "on request %p",
2579 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002580 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002581 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002582 continue;
2583 }
2584 /*
2585 * Note: callback is set to dasd_return_cqr_cb in
2586 * __dasd_block_start_head to cover erp requests as well
2587 */
2588 cqr->callback_data = (void *) req;
2589 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002590 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002591 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002592 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002593 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002594 dasd_profile_start(block, cqr, req);
2595 }
2596}
2597
2598static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2599{
2600 struct request *req;
2601 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002602 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002603
2604 req = (struct request *) cqr->callback_data;
2605 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002606 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002607 if (status < 0)
2608 error = status;
2609 else if (status == 0) {
2610 if (cqr->intrc == -EPERM)
2611 error = -EBADE;
2612 else if (cqr->intrc == -ENOLINK ||
2613 cqr->intrc == -ETIMEDOUT)
2614 error = cqr->intrc;
2615 else
2616 error = -EIO;
2617 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002618 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002619}
2620
2621/*
2622 * Process ccw request queue.
2623 */
2624static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2625 struct list_head *final_queue)
2626{
2627 struct list_head *l, *n;
2628 struct dasd_ccw_req *cqr;
2629 dasd_erp_fn_t erp_fn;
2630 unsigned long flags;
2631 struct dasd_device *base = block->base;
2632
2633restart:
2634 /* Process request with final status. */
2635 list_for_each_safe(l, n, &block->ccw_queue) {
2636 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2637 if (cqr->status != DASD_CQR_DONE &&
2638 cqr->status != DASD_CQR_FAILED &&
2639 cqr->status != DASD_CQR_NEED_ERP &&
2640 cqr->status != DASD_CQR_TERMINATED)
2641 continue;
2642
2643 if (cqr->status == DASD_CQR_TERMINATED) {
2644 base->discipline->handle_terminated_request(cqr);
2645 goto restart;
2646 }
2647
2648 /* Process requests that may be recovered */
2649 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002650 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002651 if (IS_ERR(erp_fn(cqr)))
2652 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002653 goto restart;
2654 }
2655
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002656 /* log sense for fatal error */
2657 if (cqr->status == DASD_CQR_FAILED) {
2658 dasd_log_sense(cqr, &cqr->irb);
2659 }
2660
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002661 /* First of all call extended error reporting. */
2662 if (dasd_eer_enabled(base) &&
2663 cqr->status == DASD_CQR_FAILED) {
2664 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2665
2666 /* restart request */
2667 cqr->status = DASD_CQR_FILLED;
2668 cqr->retries = 255;
2669 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002670 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002671 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2672 flags);
2673 goto restart;
2674 }
2675
2676 /* Process finished ERP request. */
2677 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002678 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002679 goto restart;
2680 }
2681
2682 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002683 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002684 list_move_tail(&cqr->blocklist, final_queue);
2685 }
2686}
2687
2688static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2689{
2690 dasd_schedule_block_bh(cqr->block);
2691}
2692
2693static void __dasd_block_start_head(struct dasd_block *block)
2694{
2695 struct dasd_ccw_req *cqr;
2696
2697 if (list_empty(&block->ccw_queue))
2698 return;
2699 /* We allways begin with the first requests on the queue, as some
2700 * of previously started requests have to be enqueued on a
2701 * dasd_device again for error recovery.
2702 */
2703 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2704 if (cqr->status != DASD_CQR_FILLED)
2705 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002706 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2707 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2708 cqr->status = DASD_CQR_FAILED;
2709 cqr->intrc = -EPERM;
2710 dasd_schedule_block_bh(block);
2711 continue;
2712 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002713 /* Non-temporary stop condition will trigger fail fast */
2714 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2715 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2716 (!dasd_eer_enabled(block->base))) {
2717 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002718 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002719 dasd_schedule_block_bh(block);
2720 continue;
2721 }
2722 /* Don't try to start requests if device is stopped */
2723 if (block->base->stopped)
2724 return;
2725
2726 /* just a fail safe check, should not happen */
2727 if (!cqr->startdev)
2728 cqr->startdev = block->base;
2729
2730 /* make sure that the requests we submit find their way back */
2731 cqr->callback = dasd_return_cqr_cb;
2732
2733 dasd_add_request_tail(cqr);
2734 }
2735}
2736
2737/*
2738 * Central dasd_block layer routine. Takes requests from the generic
2739 * block layer request queue, creates ccw requests, enqueues them on
2740 * a dasd_device and processes ccw requests that have been returned.
2741 */
2742static void dasd_block_tasklet(struct dasd_block *block)
2743{
2744 struct list_head final_queue;
2745 struct list_head *l, *n;
2746 struct dasd_ccw_req *cqr;
2747
2748 atomic_set(&block->tasklet_scheduled, 0);
2749 INIT_LIST_HEAD(&final_queue);
2750 spin_lock(&block->queue_lock);
2751 /* Finish off requests on ccw queue */
2752 __dasd_process_block_ccw_queue(block, &final_queue);
2753 spin_unlock(&block->queue_lock);
2754 /* Now call the callback function of requests with final status */
2755 spin_lock_irq(&block->request_queue_lock);
2756 list_for_each_safe(l, n, &final_queue) {
2757 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2758 list_del_init(&cqr->blocklist);
2759 __dasd_cleanup_cqr(cqr);
2760 }
2761 spin_lock(&block->queue_lock);
2762 /* Get new request from the block device request queue */
2763 __dasd_process_request_queue(block);
2764 /* Now check if the head of the ccw queue needs to be started. */
2765 __dasd_block_start_head(block);
2766 spin_unlock(&block->queue_lock);
2767 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002768 if (waitqueue_active(&shutdown_waitq))
2769 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002770 dasd_put_device(block->base);
2771}
2772
2773static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2774{
2775 wake_up(&dasd_flush_wq);
2776}
2777
2778/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002779 * Requeue a request back to the block request queue
2780 * only works for block requests
2781 */
2782static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2783{
2784 struct dasd_block *block = cqr->block;
2785 struct request *req;
2786 unsigned long flags;
2787
2788 if (!block)
2789 return -EINVAL;
2790 spin_lock_irqsave(&block->queue_lock, flags);
2791 req = (struct request *) cqr->callback_data;
2792 blk_requeue_request(block->request_queue, req);
2793 spin_unlock_irqrestore(&block->queue_lock, flags);
2794
2795 return 0;
2796}
2797
2798/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002799 * Go through all request on the dasd_block request queue, cancel them
2800 * on the respective dasd_device, and return them to the generic
2801 * block layer.
2802 */
2803static int dasd_flush_block_queue(struct dasd_block *block)
2804{
2805 struct dasd_ccw_req *cqr, *n;
2806 int rc, i;
2807 struct list_head flush_queue;
2808
2809 INIT_LIST_HEAD(&flush_queue);
2810 spin_lock_bh(&block->queue_lock);
2811 rc = 0;
2812restart:
2813 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2814 /* if this request currently owned by a dasd_device cancel it */
2815 if (cqr->status >= DASD_CQR_QUEUED)
2816 rc = dasd_cancel_req(cqr);
2817 if (rc < 0)
2818 break;
2819 /* Rechain request (including erp chain) so it won't be
2820 * touched by the dasd_block_tasklet anymore.
2821 * Replace the callback so we notice when the request
2822 * is returned from the dasd_device layer.
2823 */
2824 cqr->callback = _dasd_wake_block_flush_cb;
2825 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2826 list_move_tail(&cqr->blocklist, &flush_queue);
2827 if (i > 1)
2828 /* moved more than one request - need to restart */
2829 goto restart;
2830 }
2831 spin_unlock_bh(&block->queue_lock);
2832 /* Now call the callback function of flushed requests */
2833restart_cb:
2834 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2835 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2836 /* Process finished ERP request. */
2837 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002838 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002839 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002840 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002841 /* restart list_for_xx loop since dasd_process_erp
2842 * might remove multiple elements */
2843 goto restart_cb;
2844 }
2845 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002846 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002847 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002848 list_del_init(&cqr->blocklist);
2849 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002850 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return rc;
2853}
2854
2855/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002856 * Schedules a call to dasd_tasklet over the device tasklet.
2857 */
2858void dasd_schedule_block_bh(struct dasd_block *block)
2859{
2860 /* Protect against rescheduling. */
2861 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2862 return;
2863 /* life cycle of block is bound to it's base device */
2864 dasd_get_device(block->base);
2865 tasklet_hi_schedule(&block->tasklet);
2866}
2867
2868
2869/*
2870 * SECTION: external block device operations
2871 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 */
2873
2874/*
2875 * Dasd request queue function. Called from ll_rw_blk.c
2876 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002877static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002879 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002881 block = queue->queuedata;
2882 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002884 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002886 __dasd_block_start_head(block);
2887 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
2890/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002891 * Block timeout callback, called from the block layer
2892 *
2893 * request_queue lock is held on entry.
2894 *
2895 * Return values:
2896 * BLK_EH_RESET_TIMER if the request should be left running
2897 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2898 * by the driver.
2899 */
2900enum blk_eh_timer_return dasd_times_out(struct request *req)
2901{
2902 struct dasd_ccw_req *cqr = req->completion_data;
2903 struct dasd_block *block = req->q->queuedata;
2904 struct dasd_device *device;
2905 int rc = 0;
2906
2907 if (!cqr)
2908 return BLK_EH_NOT_HANDLED;
2909
2910 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002911 if (!device->blk_timeout)
2912 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002913 DBF_DEV_EVENT(DBF_WARNING, device,
2914 " dasd_times_out cqr %p status %x",
2915 cqr, cqr->status);
2916
2917 spin_lock(&block->queue_lock);
2918 spin_lock(get_ccwdev_lock(device->cdev));
2919 cqr->retries = -1;
2920 cqr->intrc = -ETIMEDOUT;
2921 if (cqr->status >= DASD_CQR_QUEUED) {
2922 spin_unlock(get_ccwdev_lock(device->cdev));
2923 rc = dasd_cancel_req(cqr);
2924 } else if (cqr->status == DASD_CQR_FILLED ||
2925 cqr->status == DASD_CQR_NEED_ERP) {
2926 cqr->status = DASD_CQR_TERMINATED;
2927 spin_unlock(get_ccwdev_lock(device->cdev));
2928 } else if (cqr->status == DASD_CQR_IN_ERP) {
2929 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2930
2931 list_for_each_entry_safe(searchcqr, nextcqr,
2932 &block->ccw_queue, blocklist) {
2933 tmpcqr = searchcqr;
2934 while (tmpcqr->refers)
2935 tmpcqr = tmpcqr->refers;
2936 if (tmpcqr != cqr)
2937 continue;
2938 /* searchcqr is an ERP request for cqr */
2939 searchcqr->retries = -1;
2940 searchcqr->intrc = -ETIMEDOUT;
2941 if (searchcqr->status >= DASD_CQR_QUEUED) {
2942 spin_unlock(get_ccwdev_lock(device->cdev));
2943 rc = dasd_cancel_req(searchcqr);
2944 spin_lock(get_ccwdev_lock(device->cdev));
2945 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2946 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2947 searchcqr->status = DASD_CQR_TERMINATED;
2948 rc = 0;
2949 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2950 /*
2951 * Shouldn't happen; most recent ERP
2952 * request is at the front of queue
2953 */
2954 continue;
2955 }
2956 break;
2957 }
2958 spin_unlock(get_ccwdev_lock(device->cdev));
2959 }
2960 dasd_schedule_block_bh(block);
2961 spin_unlock(&block->queue_lock);
2962
2963 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2964}
2965
2966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * Allocate and initialize request queue and default I/O scheduler.
2968 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002969static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 int rc;
2972
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002973 block->request_queue = blk_init_queue(do_dasd_request,
2974 &block->request_queue_lock);
2975 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return -ENOMEM;
2977
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002978 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002980 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002981 block->request_queue->elevator = NULL;
Heiko Carstensef089942013-10-31 13:24:28 +01002982 mutex_lock(&block->request_queue->sysfs_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002983 rc = elevator_init(block->request_queue, "deadline");
Heiko Carstensef089942013-10-31 13:24:28 +01002984 if (rc)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002985 blk_cleanup_queue(block->request_queue);
Heiko Carstensef089942013-10-31 13:24:28 +01002986 mutex_unlock(&block->request_queue->sysfs_lock);
2987 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
2990/*
2991 * Allocate and initialize request queue.
2992 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002993static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 int max;
2996
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002997 if (block->base->features & DASD_FEATURE_USERAW) {
2998 /*
2999 * the max_blocks value for raw_track access is 256
3000 * it is higher than the native ECKD value because we
3001 * only need one ccw per track
3002 * so the max_hw_sectors are
3003 * 2048 x 512B = 1024kB = 16 tracks
3004 */
3005 max = 2048;
3006 } else {
3007 max = block->base->discipline->max_blocks << block->s2b_shift;
3008 }
3009 blk_queue_logical_block_size(block->request_queue,
3010 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003011 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003012 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003013 /* with page sized segments we can translate each segement into
3014 * one idaw/tidaw
3015 */
3016 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3017 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020/*
3021 * Deactivate and free request queue.
3022 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003023static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003025 if (block->request_queue) {
3026 blk_cleanup_queue(block->request_queue);
3027 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029}
3030
3031/*
3032 * Flush request on the request queue.
3033 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003034static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct request *req;
3037
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003038 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003040
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003041 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003042 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003043 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003044 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Al Viro57a7c0b2008-03-02 10:36:08 -05003047static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003049 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 int rc;
3051
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003052 base = dasd_device_from_gendisk(bdev->bd_disk);
3053 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003054 return -ENODEV;
3055
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003056 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003057 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 rc = -ENODEV;
3059 goto unlock;
3060 }
3061
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003062 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 rc = -EINVAL;
3064 goto unlock;
3065 }
3066
3067 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003068 dev_info(&base->cdev->dev,
3069 "Accessing the DASD failed because it is in "
3070 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 rc = -EPERM;
3072 goto out;
3073 }
3074
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003075 if (base->state <= DASD_STATE_BASIC) {
3076 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 " Cannot open unrecognized device");
3078 rc = -ENODEV;
3079 goto out;
3080 }
3081
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003082 if ((mode & FMODE_WRITE) &&
3083 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3084 (base->features & DASD_FEATURE_READONLY))) {
3085 rc = -EROFS;
3086 goto out;
3087 }
3088
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003089 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return 0;
3091
3092out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003093 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003095 atomic_dec(&base->block->open_count);
3096 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return rc;
3098}
3099
Al Virodb2a1442013-05-05 21:52:57 -04003100static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Al Virodb2a1442013-05-05 21:52:57 -04003102 struct dasd_device *base = dasd_device_from_gendisk(disk);
3103 if (base) {
3104 atomic_dec(&base->block->open_count);
3105 module_put(base->discipline->owner);
3106 dasd_put_device(base);
3107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003110/*
3111 * Return disk geometry.
3112 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003113static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003114{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003115 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003116
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003117 base = dasd_device_from_gendisk(bdev->bd_disk);
3118 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003119 return -ENODEV;
3120
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003121 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003122 !base->discipline->fill_geometry) {
3123 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003124 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003125 }
3126 base->discipline->fill_geometry(base->block, geo);
3127 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3128 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003129 return 0;
3130}
3131
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003132const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133dasd_device_operations = {
3134 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003135 .open = dasd_open,
3136 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003137 .ioctl = dasd_ioctl,
3138 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003139 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140};
3141
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003142/*******************************************************************************
3143 * end of block device operations
3144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
3146static void
3147dasd_exit(void)
3148{
3149#ifdef CONFIG_PROC_FS
3150 dasd_proc_exit();
3151#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003152 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003153 if (dasd_page_cache != NULL) {
3154 kmem_cache_destroy(dasd_page_cache);
3155 dasd_page_cache = NULL;
3156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 dasd_gendisk_exit();
3158 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (dasd_debug_area != NULL) {
3160 debug_unregister(dasd_debug_area);
3161 dasd_debug_area = NULL;
3162 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003163 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
3166/*
3167 * SECTION: common functions for ccw_driver use
3168 */
3169
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003170/*
3171 * Is the device read-only?
3172 * Note that this function does not report the setting of the
3173 * readonly device attribute, but how it is configured in z/VM.
3174 */
3175int dasd_device_is_ro(struct dasd_device *device)
3176{
3177 struct ccw_dev_id dev_id;
3178 struct diag210 diag_data;
3179 int rc;
3180
3181 if (!MACHINE_IS_VM)
3182 return 0;
3183 ccw_device_get_id(device->cdev, &dev_id);
3184 memset(&diag_data, 0, sizeof(diag_data));
3185 diag_data.vrdcdvno = dev_id.devno;
3186 diag_data.vrdclen = sizeof(diag_data);
3187 rc = diag210(&diag_data);
3188 if (rc == 0 || rc == 2) {
3189 return diag_data.vrdcvfla & 0x80;
3190 } else {
3191 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3192 dev_id.devno, rc);
3193 return 0;
3194 }
3195}
3196EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3197
Cornelia Huckf3445a12009-04-14 15:36:23 +02003198static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3199{
3200 struct ccw_device *cdev = data;
3201 int ret;
3202
3203 ret = ccw_device_set_online(cdev);
3204 if (ret)
3205 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3206 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003207}
3208
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003209/*
3210 * Initial attempt at a probe function. this can be simplified once
3211 * the other detection code is gone.
3212 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003213int dasd_generic_probe(struct ccw_device *cdev,
3214 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 int ret;
3217
3218 ret = dasd_add_sysfs_files(cdev);
3219 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003220 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3221 "dasd_generic_probe: could not add "
3222 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
Horst Hummel40545572006-06-29 15:08:18 +02003225 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Horst Hummel40545572006-06-29 15:08:18 +02003227 /*
3228 * Automatically online either all dasd devices (dasd_autodetect)
3229 * or all devices specified with dasd= parameters during
3230 * initial probe.
3231 */
3232 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003233 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003234 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003238/*
3239 * This will one day be called from a global not_oper handler.
3240 * It is also used by driver_unregister during module unload.
3241 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003242void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003245 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Horst Hummel59afda72005-05-16 21:53:39 -07003247 cdev->handler = NULL;
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003250 if (IS_ERR(device)) {
3251 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003253 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003254 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3255 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 /* Already doing offline processing */
3257 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003258 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return;
3260 }
3261 /*
3262 * This device is removed unconditionally. Set offline
3263 * flag to prevent dasd_open from opening it while it is
3264 * no quite down yet.
3265 */
3266 dasd_set_target_state(device, DASD_STATE_NEW);
3267 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003268 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003270 /*
3271 * life cycle of block is bound to device, so delete it after
3272 * device was safely removed
3273 */
3274 if (block)
3275 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003276
3277 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003280/*
3281 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003283 * or the user has started activation through sysfs.
3284 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003285int dasd_generic_set_online(struct ccw_device *cdev,
3286 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003288 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003290 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003291
Horst Hummel40545572006-06-29 15:08:18 +02003292 /* first online clears initial online feature flag */
3293 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 device = dasd_create_device(cdev);
3295 if (IS_ERR(device))
3296 return PTR_ERR(device);
3297
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003298 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003299 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003301 pr_warning("%s Setting the DASD online failed because "
3302 "of missing DIAG discipline\n",
3303 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 dasd_delete_device(device);
3305 return -ENODEV;
3306 }
3307 discipline = dasd_diag_discipline_pointer;
3308 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003309 if (!try_module_get(base_discipline->owner)) {
3310 dasd_delete_device(device);
3311 return -EINVAL;
3312 }
3313 if (!try_module_get(discipline->owner)) {
3314 module_put(base_discipline->owner);
3315 dasd_delete_device(device);
3316 return -EINVAL;
3317 }
3318 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 device->discipline = discipline;
3320
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003321 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 rc = discipline->check_device(device);
3323 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003324 pr_warning("%s Setting the DASD online with discipline %s "
3325 "failed with rc=%i\n",
3326 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003327 module_put(discipline->owner);
3328 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 dasd_delete_device(device);
3330 return rc;
3331 }
3332
3333 dasd_set_target_state(device, DASD_STATE_ONLINE);
3334 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003335 pr_warning("%s Setting the DASD online failed because of a "
3336 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 rc = -ENODEV;
3338 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003339 if (device->block)
3340 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 dasd_delete_device(device);
3342 } else
3343 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003344 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003345
3346 wait_event(dasd_init_waitq, _wait_for_device(device));
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return rc;
3350}
3351
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003352int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003355 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003356 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003358 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 device = dasd_device_from_cdev(cdev);
3360 if (IS_ERR(device))
3361 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 /*
3364 * We must make sure that this device is currently not in use.
3365 * The open_count is increased for every opener, that includes
3366 * the blkdev_get in dasd_scan_partitions. We are only interested
3367 * in the other openers.
3368 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003369 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003370 max_count = device->block->bdev ? 0 : -1;
3371 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003372 if (open_count > max_count) {
3373 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003374 pr_warning("%s: The DASD cannot be set offline "
3375 "with open count %i\n",
3376 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003377 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003378 pr_warning("%s: The DASD cannot be set offline "
3379 "while it is in use\n",
3380 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003381 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3382 dasd_put_device(device);
3383 return -EBUSY;
3384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003386
3387 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3388 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003389 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003390 * could only be called by normal offline so safe_offline flag
3391 * needs to be removed to run normal offline and kill all I/O
3392 */
3393 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3394 /* Already doing normal offline processing */
3395 dasd_put_device(device);
3396 return -EBUSY;
3397 } else
3398 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3399
3400 } else
3401 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3402 /* Already doing offline processing */
3403 dasd_put_device(device);
3404 return -EBUSY;
3405 }
3406
3407 /*
3408 * if safe_offline called set safe_offline_running flag and
3409 * clear safe_offline so that a call to normal offline
3410 * can overrun safe_offline processing
3411 */
3412 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3413 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3414 /*
3415 * If we want to set the device safe offline all IO operations
3416 * should be finished before continuing the offline process
3417 * so sync bdev first and then wait for our queues to become
3418 * empty
3419 */
3420 /* sync blockdev and partitions */
3421 rc = fsync_bdev(device->block->bdev);
3422 if (rc != 0)
3423 goto interrupted;
3424
3425 /* schedule device tasklet and wait for completion */
3426 dasd_schedule_device_bh(device);
3427 rc = wait_event_interruptible(shutdown_waitq,
3428 _wait_for_empty_queues(device));
3429 if (rc != 0)
3430 goto interrupted;
3431 }
3432
3433 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 dasd_set_target_state(device, DASD_STATE_NEW);
3435 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003436 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003438 /*
3439 * life cycle of block is bound to device, so delete it after
3440 * device was safely removed
3441 */
3442 if (block)
3443 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003445
3446interrupted:
3447 /* interrupted by signal */
3448 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3449 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3450 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3451 dasd_put_device(device);
3452 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003455int dasd_generic_last_path_gone(struct dasd_device *device)
3456{
3457 struct dasd_ccw_req *cqr;
3458
3459 dev_warn(&device->cdev->dev, "No operational channel path is left "
3460 "for the device\n");
3461 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3462 /* First of all call extended error reporting. */
3463 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3464
3465 if (device->state < DASD_STATE_BASIC)
3466 return 0;
3467 /* Device is active. We want to keep it. */
3468 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3469 if ((cqr->status == DASD_CQR_IN_IO) ||
3470 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3471 cqr->status = DASD_CQR_QUEUED;
3472 cqr->retries++;
3473 }
3474 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3475 dasd_device_clear_timer(device);
3476 dasd_schedule_device_bh(device);
3477 return 1;
3478}
3479EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3480
3481int dasd_generic_path_operational(struct dasd_device *device)
3482{
3483 dev_info(&device->cdev->dev, "A channel path to the device has become "
3484 "operational\n");
3485 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3486 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3487 if (device->stopped & DASD_UNRESUMED_PM) {
3488 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3489 dasd_restore_device(device);
3490 return 1;
3491 }
3492 dasd_schedule_device_bh(device);
3493 if (device->block)
3494 dasd_schedule_block_bh(device->block);
3495 return 1;
3496}
3497EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3498
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003499int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500{
3501 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 int ret;
3503
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003504 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 if (IS_ERR(device))
3506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 ret = 0;
3508 switch (event) {
3509 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003510 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003512 device->path_data.opm = 0;
3513 device->path_data.ppm = 0;
3514 device->path_data.npm = 0;
3515 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 break;
3517 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003519 if (device->path_data.opm)
3520 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 break;
3522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 dasd_put_device(device);
3524 return ret;
3525}
3526
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003527void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3528{
3529 int chp;
3530 __u8 oldopm, eventlpm;
3531 struct dasd_device *device;
3532
3533 device = dasd_device_from_cdev_locked(cdev);
3534 if (IS_ERR(device))
3535 return;
3536 for (chp = 0; chp < 8; chp++) {
3537 eventlpm = 0x80 >> chp;
3538 if (path_event[chp] & PE_PATH_GONE) {
3539 oldopm = device->path_data.opm;
3540 device->path_data.opm &= ~eventlpm;
3541 device->path_data.ppm &= ~eventlpm;
3542 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003543 if (oldopm && !device->path_data.opm) {
3544 dev_warn(&device->cdev->dev,
3545 "No verified channel paths remain "
3546 "for the device\n");
3547 DBF_DEV_EVENT(DBF_WARNING, device,
3548 "%s", "last verified path gone");
3549 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3550 dasd_device_set_stop_bits(device,
3551 DASD_STOPPED_DC_WAIT);
3552 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003553 }
3554 if (path_event[chp] & PE_PATH_AVAILABLE) {
3555 device->path_data.opm &= ~eventlpm;
3556 device->path_data.ppm &= ~eventlpm;
3557 device->path_data.npm &= ~eventlpm;
3558 device->path_data.tbvpm |= eventlpm;
3559 dasd_schedule_device_bh(device);
3560 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003561 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003562 if (!(device->path_data.opm & eventlpm) &&
3563 !(device->path_data.tbvpm & eventlpm)) {
3564 /*
3565 * we can not establish a pathgroup on an
3566 * unavailable path, so trigger a path
3567 * verification first
3568 */
3569 device->path_data.tbvpm |= eventlpm;
3570 dasd_schedule_device_bh(device);
3571 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003572 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3573 "Pathgroup re-established\n");
3574 if (device->discipline->kick_validate)
3575 device->discipline->kick_validate(device);
3576 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003577 }
3578 dasd_put_device(device);
3579}
3580EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3581
3582int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3583{
3584 if (!device->path_data.opm && lpm) {
3585 device->path_data.opm = lpm;
3586 dasd_generic_path_operational(device);
3587 } else
3588 device->path_data.opm |= lpm;
3589 return 0;
3590}
3591EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3592
3593
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003594int dasd_generic_pm_freeze(struct ccw_device *cdev)
3595{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003596 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003597 struct list_head freeze_queue;
3598 struct dasd_ccw_req *cqr, *n;
3599 struct dasd_ccw_req *refers;
3600 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003601
3602 if (IS_ERR(device))
3603 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003604
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003605 /* mark device as suspended */
3606 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3607
Stefan Haberland6f272b92011-01-05 12:48:05 +01003608 if (device->discipline->freeze)
3609 rc = device->discipline->freeze(device);
3610
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003611 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003612 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003613
3614 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003615 INIT_LIST_HEAD(&freeze_queue);
3616 spin_lock_irq(get_ccwdev_lock(cdev));
3617 rc = 0;
3618 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3619 /* Check status and move request to flush_queue */
3620 if (cqr->status == DASD_CQR_IN_IO) {
3621 rc = device->discipline->term_IO(cqr);
3622 if (rc) {
3623 /* unable to terminate requeust */
3624 dev_err(&device->cdev->dev,
3625 "Unable to terminate request %p "
3626 "on suspend\n", cqr);
3627 spin_unlock_irq(get_ccwdev_lock(cdev));
3628 dasd_put_device(device);
3629 return rc;
3630 }
3631 }
3632 list_move_tail(&cqr->devlist, &freeze_queue);
3633 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003634 spin_unlock_irq(get_ccwdev_lock(cdev));
3635
3636 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3637 wait_event(dasd_flush_wq,
3638 (cqr->status != DASD_CQR_CLEAR_PENDING));
3639 if (cqr->status == DASD_CQR_CLEARED)
3640 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003641
Stefan Haberlandc5576872013-04-15 16:41:31 +02003642 /* requeue requests to blocklayer will only work for
3643 block device requests */
3644 if (_dasd_requeue_request(cqr))
3645 continue;
3646
3647 /* remove requests from device and block queue */
3648 list_del_init(&cqr->devlist);
3649 while (cqr->refers != NULL) {
3650 refers = cqr->refers;
3651 /* remove the request from the block queue */
3652 list_del(&cqr->blocklist);
3653 /* free the finished erp request */
3654 dasd_free_erp_request(cqr, cqr->memdev);
3655 cqr = refers;
3656 }
3657 if (cqr->block)
3658 list_del_init(&cqr->blocklist);
3659 cqr->block->base->discipline->free_cp(
3660 cqr, (struct request *) cqr->callback_data);
3661 }
3662
3663 /*
3664 * if requests remain then they are internal request
3665 * and go back to the device queue
3666 */
3667 if (!list_empty(&freeze_queue)) {
3668 /* move freeze_queue to start of the ccw_queue */
3669 spin_lock_irq(get_ccwdev_lock(cdev));
3670 list_splice_tail(&freeze_queue, &device->ccw_queue);
3671 spin_unlock_irq(get_ccwdev_lock(cdev));
3672 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003673 dasd_put_device(device);
3674 return rc;
3675}
3676EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3677
3678int dasd_generic_restore_device(struct ccw_device *cdev)
3679{
3680 struct dasd_device *device = dasd_device_from_cdev(cdev);
3681 int rc = 0;
3682
3683 if (IS_ERR(device))
3684 return PTR_ERR(device);
3685
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003686 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003687 dasd_device_remove_stop_bits(device,
3688 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003689
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003690 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003691
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003692 /*
3693 * call discipline restore function
3694 * if device is stopped do nothing e.g. for disconnected devices
3695 */
3696 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003697 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003698 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003699 /*
3700 * if the resume failed for the DASD we put it in
3701 * an UNRESUMED stop state
3702 */
3703 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003704
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003705 if (device->block)
3706 dasd_schedule_block_bh(device->block);
3707
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003708 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003709 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003710 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003711}
3712EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3713
Heiko Carstens763968e2007-05-10 15:45:46 +02003714static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3715 void *rdc_buffer,
3716 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003717 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003718{
3719 struct dasd_ccw_req *cqr;
3720 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003721 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003722
3723 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3724
3725 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003726 /* internal error 13 - Allocating the RDC request failed*/
3727 dev_err(&device->cdev->dev,
3728 "An error occurred in the DASD device driver, "
3729 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003730 return cqr;
3731 }
3732
3733 ccw = cqr->cpaddr;
3734 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003735 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3736 idaw = (unsigned long *) (cqr->data);
3737 ccw->cda = (__u32)(addr_t) idaw;
3738 ccw->flags = CCW_FLAG_IDA;
3739 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3740 } else {
3741 ccw->cda = (__u32)(addr_t) rdc_buffer;
3742 ccw->flags = 0;
3743 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003744
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003745 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003746 cqr->startdev = device;
3747 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003748 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003749 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003750 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003751 cqr->status = DASD_CQR_FILLED;
3752 return cqr;
3753}
3754
3755
Stefan Haberland68b781f2009-09-11 10:28:29 +02003756int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003757 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003758{
3759 int ret;
3760 struct dasd_ccw_req *cqr;
3761
Sebastian Ott92636b12009-06-12 10:26:37 +02003762 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003763 magic);
3764 if (IS_ERR(cqr))
3765 return PTR_ERR(cqr);
3766
3767 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003768 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003769 return ret;
3770}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003771EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003772
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003773/*
3774 * In command mode and transport mode we need to look for sense
3775 * data in different places. The sense data itself is allways
3776 * an array of 32 bytes, so we can unify the sense data access
3777 * for both modes.
3778 */
3779char *dasd_get_sense(struct irb *irb)
3780{
3781 struct tsb *tsb = NULL;
3782 char *sense = NULL;
3783
3784 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3785 if (irb->scsw.tm.tcw)
3786 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3787 irb->scsw.tm.tcw);
3788 if (tsb && tsb->length == 64 && tsb->flags)
3789 switch (tsb->flags & 0x07) {
3790 case 1: /* tsa_iostat */
3791 sense = tsb->tsa.iostat.sense;
3792 break;
3793 case 2: /* tsa_ddpc */
3794 sense = tsb->tsa.ddpc.sense;
3795 break;
3796 default:
3797 /* currently we don't use interrogate data */
3798 break;
3799 }
3800 } else if (irb->esw.esw0.erw.cons) {
3801 sense = irb->ecw;
3802 }
3803 return sense;
3804}
3805EXPORT_SYMBOL_GPL(dasd_get_sense);
3806
Stefan Haberland4679e892012-06-19 17:30:12 +02003807void dasd_generic_shutdown(struct ccw_device *cdev)
3808{
3809 struct dasd_device *device;
3810
3811 device = dasd_device_from_cdev(cdev);
3812 if (IS_ERR(device))
3813 return;
3814
3815 if (device->block)
3816 dasd_schedule_block_bh(device->block);
3817
3818 dasd_schedule_device_bh(device);
3819
3820 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3821}
3822EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3823
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003824static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
3826 int rc;
3827
3828 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003829 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003830 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003831 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003834 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 if (dasd_debug_area == NULL) {
3836 rc = -ENOMEM;
3837 goto failed;
3838 }
3839 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003840 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3843
3844 dasd_diag_discipline_pointer = NULL;
3845
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003846 dasd_statistics_createroot();
3847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 rc = dasd_devmap_init();
3849 if (rc)
3850 goto failed;
3851 rc = dasd_gendisk_init();
3852 if (rc)
3853 goto failed;
3854 rc = dasd_parse();
3855 if (rc)
3856 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003857 rc = dasd_eer_init();
3858 if (rc)
3859 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860#ifdef CONFIG_PROC_FS
3861 rc = dasd_proc_init();
3862 if (rc)
3863 goto failed;
3864#endif
3865
3866 return 0;
3867failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003868 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 dasd_exit();
3870 return rc;
3871}
3872
3873module_init(dasd_init);
3874module_exit(dasd_exit);
3875
3876EXPORT_SYMBOL(dasd_debug_area);
3877EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3878
3879EXPORT_SYMBOL(dasd_add_request_head);
3880EXPORT_SYMBOL(dasd_add_request_tail);
3881EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003882EXPORT_SYMBOL(dasd_device_clear_timer);
3883EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884EXPORT_SYMBOL(dasd_enable_device);
3885EXPORT_SYMBOL(dasd_int_handler);
3886EXPORT_SYMBOL(dasd_kfree_request);
3887EXPORT_SYMBOL(dasd_kick_device);
3888EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003889EXPORT_SYMBOL(dasd_schedule_device_bh);
3890EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003892EXPORT_SYMBOL(dasd_device_set_timer);
3893EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894EXPORT_SYMBOL(dasd_sfree_request);
3895EXPORT_SYMBOL(dasd_sleep_on);
3896EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3897EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3898EXPORT_SYMBOL(dasd_smalloc_request);
3899EXPORT_SYMBOL(dasd_start_IO);
3900EXPORT_SYMBOL(dasd_term_IO);
3901
3902EXPORT_SYMBOL_GPL(dasd_generic_probe);
3903EXPORT_SYMBOL_GPL(dasd_generic_remove);
3904EXPORT_SYMBOL_GPL(dasd_generic_notify);
3905EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3906EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003907EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3908EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3909EXPORT_SYMBOL_GPL(dasd_alloc_block);
3910EXPORT_SYMBOL_GPL(dasd_free_block);