blob: ea2729fbc86c9bfec2e05ce54413cd7d84670666 [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);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200286 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100287 dasd_gendisk_free(device->block);
288 dasd_block_clear_timer(device->block);
289 }
290 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200291 if (rc)
292 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100293 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200294 dasd_profile_exit(&device->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200295 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
297 if (device->debug_area != NULL) {
298 debug_unregister(device->debug_area);
299 device->debug_area = NULL;
300 }
301 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * Do the initial analysis. The do_analysis function may return
307 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
308 * until the discipline decides to continue the startup sequence
309 * by calling the function dasd_change_state. The eckd disciplines
310 * uses this to start a ccw that detects the format. The completion
311 * interrupt for this detection ccw uses the kernel event daemon to
312 * trigger the call to dasd_change_state. All this is done in the
313 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800314 * After the analysis ccw is done (do_analysis returned 0) the block
315 * device is setup.
316 * In case the analysis returns an error, the device setup is stopped
317 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100319static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100322 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800326 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327 if (block) {
328 if (block->base->discipline->do_analysis != NULL)
329 rc = block->base->discipline->do_analysis(block);
330 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200331 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 device->state = DASD_STATE_UNFMT;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200333 goto out;
334 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100335 return rc;
336 }
337 dasd_setup_queue(block);
338 set_capacity(block->gdp,
339 block->blocks << block->s2b_shift);
340 device->state = DASD_STATE_READY;
341 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200342 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100343 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200344 return rc;
345 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100346 } else {
347 device->state = DASD_STATE_READY;
348 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200349out:
350 if (device->discipline->basic_to_ready)
351 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800352 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100355static inline
356int _wait_for_empty_queues(struct dasd_device *device)
357{
358 if (device->block)
359 return list_empty(&device->ccw_queue) &&
360 list_empty(&device->block->ccw_queue);
361 else
362 return list_empty(&device->ccw_queue);
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
366 * Remove device from block device layer. Destroy dirty buffers.
367 * Forget format information. Check if the target level is basic
368 * and if it is create fake disk for formatting.
369 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100370static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Horst Hummel8f617012006-08-30 14:33:33 +0200372 int rc;
373
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200374 if (device->discipline->ready_to_basic) {
375 rc = device->discipline->ready_to_basic(device);
376 if (rc)
377 return rc;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380 if (device->block) {
381 struct dasd_block *block = device->block;
382 rc = dasd_flush_block_queue(block);
383 if (rc) {
384 device->state = DASD_STATE_READY;
385 return rc;
386 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100387 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100388 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100389 block->blocks = 0;
390 block->bp_block = 0;
391 block->s2b_shift = 0;
392 }
Horst Hummel8f617012006-08-30 14:33:33 +0200393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
Horst Hummel90f00942006-03-07 21:55:39 -0800397 * Back to basic.
398 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100399static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800400{
401 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200402 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800403}
404
405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * Make the device online and schedule the bottom half to start
407 * the requeueing of requests from the linux request queue to the
408 * ccw queue.
409 */
Horst Hummel8f617012006-08-30 14:33:33 +0200410static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411dasd_state_ready_to_online(struct dasd_device * device)
412{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100413 struct gendisk *disk;
414 struct disk_part_iter piter;
415 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100418 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100419 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100420 if ((device->features & DASD_FEATURE_USERAW)) {
421 disk = device->block->gdp;
422 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
423 return 0;
424 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100425 disk = device->block->bdev->bd_disk;
426 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
427 while ((part = disk_part_iter_next(&piter)))
428 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
429 disk_part_iter_exit(&piter);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
434/*
435 * Stop the requeueing of requests again.
436 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100437static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100439 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100440 struct gendisk *disk;
441 struct disk_part_iter piter;
442 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100443
444 if (device->discipline->online_to_ready) {
445 rc = device->discipline->online_to_ready(device);
446 if (rc)
447 return rc;
448 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100451 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100452 disk = device->block->bdev->bd_disk;
453 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
454 while ((part = disk_part_iter_next(&piter)))
455 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
456 disk_part_iter_exit(&piter);
457 }
Horst Hummel8f617012006-08-30 14:33:33 +0200458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461/*
462 * Device startup state changes.
463 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100464static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 int rc;
467
468 rc = 0;
469 if (device->state == DASD_STATE_NEW &&
470 device->target >= DASD_STATE_KNOWN)
471 rc = dasd_state_new_to_known(device);
472
473 if (!rc &&
474 device->state == DASD_STATE_KNOWN &&
475 device->target >= DASD_STATE_BASIC)
476 rc = dasd_state_known_to_basic(device);
477
478 if (!rc &&
479 device->state == DASD_STATE_BASIC &&
480 device->target >= DASD_STATE_READY)
481 rc = dasd_state_basic_to_ready(device);
482
483 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700484 device->state == DASD_STATE_UNFMT &&
485 device->target > DASD_STATE_UNFMT)
486 rc = -EPERM;
487
488 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 device->state == DASD_STATE_READY &&
490 device->target >= DASD_STATE_ONLINE)
491 rc = dasd_state_ready_to_online(device);
492
493 return rc;
494}
495
496/*
497 * Device shutdown state changes.
498 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100499static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Horst Hummel8f617012006-08-30 14:33:33 +0200501 int rc;
502
503 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (device->state == DASD_STATE_ONLINE &&
505 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200506 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200507
Horst Hummel8f617012006-08-30 14:33:33 +0200508 if (!rc &&
509 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200511 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800512
Horst Hummel8f617012006-08-30 14:33:33 +0200513 if (!rc &&
514 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800515 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200516 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800517
Horst Hummel8f617012006-08-30 14:33:33 +0200518 if (!rc &&
519 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200521 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200522
Horst Hummel8f617012006-08-30 14:33:33 +0200523 if (!rc &&
524 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200526 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Horst Hummel8f617012006-08-30 14:33:33 +0200528 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * This is the main startup/shutdown routine.
533 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100534static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Sebastian Ott181d9522009-06-22 12:08:21 +0200536 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (device->state == device->target)
539 /* Already where we want to go today... */
540 return;
541 if (device->state < device->target)
542 rc = dasd_increase_state(device);
543 else
544 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200545 if (rc == -EAGAIN)
546 return;
547 if (rc)
548 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200550 /* let user-space know that the device status changed */
551 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200552
553 if (device->state == device->target)
554 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
558 * Kick starter for devices that did not complete the startup/shutdown
559 * procedure or were sleeping because of a pending state.
560 * dasd_kick_device will schedule a call do do_kick_device to the kernel
561 * event daemon.
562 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100563static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Al Viro4927b3f2006-12-06 19:18:20 +0000565 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100566 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100568 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100569 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dasd_put_device(device);
571}
572
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100573void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 dasd_get_device(device);
576 /* queue call to dasd_kick_device to the kernel event daemon. */
577 schedule_work(&device->kick_work);
578}
579
580/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200581 * dasd_reload_device will schedule a call do do_reload_device to the kernel
582 * event daemon.
583 */
584static void do_reload_device(struct work_struct *work)
585{
586 struct dasd_device *device = container_of(work, struct dasd_device,
587 reload_device);
588 device->discipline->reload(device);
589 dasd_put_device(device);
590}
591
592void dasd_reload_device(struct dasd_device *device)
593{
594 dasd_get_device(device);
595 /* queue call to dasd_reload_device to the kernel event daemon. */
596 schedule_work(&device->reload_device);
597}
598EXPORT_SYMBOL(dasd_reload_device);
599
600/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200601 * dasd_restore_device will schedule a call do do_restore_device to the kernel
602 * event daemon.
603 */
604static void do_restore_device(struct work_struct *work)
605{
606 struct dasd_device *device = container_of(work, struct dasd_device,
607 restore_device);
608 device->cdev->drv->restore(device->cdev);
609 dasd_put_device(device);
610}
611
612void dasd_restore_device(struct dasd_device *device)
613{
614 dasd_get_device(device);
615 /* queue call to dasd_restore_device to the kernel event daemon. */
616 schedule_work(&device->restore_device);
617}
618
619/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * Set the target state for a device and starts the state change.
621 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100622void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200624 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100625 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* If we are in probeonly mode stop at DASD_STATE_READY. */
627 if (dasd_probeonly && target > DASD_STATE_READY)
628 target = DASD_STATE_READY;
629 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100630 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 wake_up(&dasd_init_waitq);
632 device->target = target;
633 }
634 if (device->state != device->target)
635 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100636 mutex_unlock(&device->state_mutex);
637 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/*
641 * Enable devices with device numbers in [from..to].
642 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100643static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 return (device->state == device->target);
646}
647
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100648void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 dasd_set_target_state(device, DASD_STATE_ONLINE);
651 if (device->state <= DASD_STATE_KNOWN)
652 /* No discipline for device found. */
653 dasd_set_target_state(device, DASD_STATE_NEW);
654 /* Now wait for the devices to come up. */
655 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400656
657 dasd_reload_device(device);
658 if (device->discipline->kick_validate)
659 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
663 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
664 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200665
666unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#ifdef CONFIG_DASD_PROFILE
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200669struct dasd_profile_info dasd_global_profile_data;
670static struct dentry *dasd_global_profile_dentry;
671static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/*
674 * Add profiling information for cqr before execution.
675 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100676static void dasd_profile_start(struct dasd_block *block,
677 struct dasd_ccw_req *cqr,
678 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct list_head *l;
681 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200682 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* count the length of the chanq for statistics */
685 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200686 if (dasd_global_profile_level || block->profile.data)
687 list_for_each(l, &block->ccw_queue)
688 if (++counter >= 31)
689 break;
690
691 if (dasd_global_profile_level) {
692 dasd_global_profile_data.dasd_io_nr_req[counter]++;
693 if (rq_data_dir(req) == READ)
694 dasd_global_profile_data.dasd_read_nr_req[counter]++;
695 }
696
697 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200698 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200699 block->profile.data->dasd_io_nr_req[counter]++;
700 if (rq_data_dir(req) == READ)
701 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200702 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200703 spin_unlock(&block->profile.lock);
704
705 /*
706 * We count the request for the start device, even though it may run on
707 * some other device due to error recovery. This way we make sure that
708 * we count each request only once.
709 */
710 device = cqr->startdev;
711 if (device->profile.data) {
712 counter = 1; /* request is not yet queued on the start device */
713 list_for_each(l, &device->ccw_queue)
714 if (++counter >= 31)
715 break;
716 }
717 spin_lock(&device->profile.lock);
718 if (device->profile.data) {
719 device->profile.data->dasd_io_nr_req[counter]++;
720 if (rq_data_dir(req) == READ)
721 device->profile.data->dasd_read_nr_req[counter]++;
722 }
723 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
727 * Add profiling information for cqr after execution.
728 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200729
730#define dasd_profile_counter(value, index) \
731{ \
732 for (index = 0; index < 31 && value >> (2+index); index++) \
733 ; \
734}
735
736static void dasd_profile_end_add_data(struct dasd_profile_info *data,
737 int is_alias,
738 int is_tpm,
739 int is_read,
740 long sectors,
741 int sectors_ind,
742 int tottime_ind,
743 int tottimeps_ind,
744 int strtime_ind,
745 int irqtime_ind,
746 int irqtimeps_ind,
747 int endtime_ind)
748{
749 /* in case of an overflow, reset the whole profile */
750 if (data->dasd_io_reqs == UINT_MAX) {
751 memset(data, 0, sizeof(*data));
752 getnstimeofday(&data->starttod);
753 }
754 data->dasd_io_reqs++;
755 data->dasd_io_sects += sectors;
756 if (is_alias)
757 data->dasd_io_alias++;
758 if (is_tpm)
759 data->dasd_io_tpm++;
760
761 data->dasd_io_secs[sectors_ind]++;
762 data->dasd_io_times[tottime_ind]++;
763 data->dasd_io_timps[tottimeps_ind]++;
764 data->dasd_io_time1[strtime_ind]++;
765 data->dasd_io_time2[irqtime_ind]++;
766 data->dasd_io_time2ps[irqtimeps_ind]++;
767 data->dasd_io_time3[endtime_ind]++;
768
769 if (is_read) {
770 data->dasd_read_reqs++;
771 data->dasd_read_sects += sectors;
772 if (is_alias)
773 data->dasd_read_alias++;
774 if (is_tpm)
775 data->dasd_read_tpm++;
776 data->dasd_read_secs[sectors_ind]++;
777 data->dasd_read_times[tottime_ind]++;
778 data->dasd_read_time1[strtime_ind]++;
779 data->dasd_read_time2[irqtime_ind]++;
780 data->dasd_read_time3[endtime_ind]++;
781 }
782}
783
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100784static void dasd_profile_end(struct dasd_block *block,
785 struct dasd_ccw_req *cqr,
786 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 long strtime, irqtime, endtime, tottime; /* in microseconds */
789 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200790 struct dasd_device *device;
791 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
792 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200794 device = cqr->startdev;
795 if (!(dasd_global_profile_level ||
796 block->profile.data ||
797 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return;
799
Tejun Heo83096eb2009-05-07 22:24:39 +0900800 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!cqr->buildclk || !cqr->startclk ||
802 !cqr->stopclk || !cqr->endclk ||
803 !sectors)
804 return;
805
806 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
807 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
808 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
809 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
810 tottimeps = tottime / sectors;
811
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200812 dasd_profile_counter(sectors, sectors_ind);
813 dasd_profile_counter(tottime, tottime_ind);
814 dasd_profile_counter(tottimeps, tottimeps_ind);
815 dasd_profile_counter(strtime, strtime_ind);
816 dasd_profile_counter(irqtime, irqtime_ind);
817 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
818 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200820 if (dasd_global_profile_level) {
821 dasd_profile_end_add_data(&dasd_global_profile_data,
822 cqr->startdev != block->base,
823 cqr->cpmode == 1,
824 rq_data_dir(req) == READ,
825 sectors, sectors_ind, tottime_ind,
826 tottimeps_ind, strtime_ind,
827 irqtime_ind, irqtimeps_ind,
828 endtime_ind);
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200831 spin_lock(&block->profile.lock);
832 if (block->profile.data)
833 dasd_profile_end_add_data(block->profile.data,
834 cqr->startdev != block->base,
835 cqr->cpmode == 1,
836 rq_data_dir(req) == READ,
837 sectors, sectors_ind, tottime_ind,
838 tottimeps_ind, strtime_ind,
839 irqtime_ind, irqtimeps_ind,
840 endtime_ind);
841 spin_unlock(&block->profile.lock);
842
843 spin_lock(&device->profile.lock);
844 if (device->profile.data)
845 dasd_profile_end_add_data(device->profile.data,
846 cqr->startdev != block->base,
847 cqr->cpmode == 1,
848 rq_data_dir(req) == READ,
849 sectors, sectors_ind, tottime_ind,
850 tottimeps_ind, strtime_ind,
851 irqtime_ind, irqtimeps_ind,
852 endtime_ind);
853 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200855
856void dasd_profile_reset(struct dasd_profile *profile)
857{
858 struct dasd_profile_info *data;
859
860 spin_lock_bh(&profile->lock);
861 data = profile->data;
862 if (!data) {
863 spin_unlock_bh(&profile->lock);
864 return;
865 }
866 memset(data, 0, sizeof(*data));
867 getnstimeofday(&data->starttod);
868 spin_unlock_bh(&profile->lock);
869}
870
871void dasd_global_profile_reset(void)
872{
873 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
874 getnstimeofday(&dasd_global_profile_data.starttod);
875}
876
877int dasd_profile_on(struct dasd_profile *profile)
878{
879 struct dasd_profile_info *data;
880
881 data = kzalloc(sizeof(*data), GFP_KERNEL);
882 if (!data)
883 return -ENOMEM;
884 spin_lock_bh(&profile->lock);
885 if (profile->data) {
886 spin_unlock_bh(&profile->lock);
887 kfree(data);
888 return 0;
889 }
890 getnstimeofday(&data->starttod);
891 profile->data = data;
892 spin_unlock_bh(&profile->lock);
893 return 0;
894}
895
896void dasd_profile_off(struct dasd_profile *profile)
897{
898 spin_lock_bh(&profile->lock);
899 kfree(profile->data);
900 profile->data = NULL;
901 spin_unlock_bh(&profile->lock);
902}
903
904char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
905{
906 char *buffer;
907
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200908 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200909 if (buffer == NULL)
910 return ERR_PTR(-ENOMEM);
911 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200912 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200913 return ERR_PTR(-EFAULT);
914 }
915 /* got the string, now strip linefeed. */
916 if (buffer[user_len - 1] == '\n')
917 buffer[user_len - 1] = 0;
918 else
919 buffer[user_len] = 0;
920 return buffer;
921}
922
923static ssize_t dasd_stats_write(struct file *file,
924 const char __user *user_buf,
925 size_t user_len, loff_t *pos)
926{
927 char *buffer, *str;
928 int rc;
929 struct seq_file *m = (struct seq_file *)file->private_data;
930 struct dasd_profile *prof = m->private;
931
932 if (user_len > 65536)
933 user_len = 65536;
934 buffer = dasd_get_user_string(user_buf, user_len);
935 if (IS_ERR(buffer))
936 return PTR_ERR(buffer);
937
938 str = skip_spaces(buffer);
939 rc = user_len;
940 if (strncmp(str, "reset", 5) == 0) {
941 dasd_profile_reset(prof);
942 } else if (strncmp(str, "on", 2) == 0) {
943 rc = dasd_profile_on(prof);
944 if (!rc)
945 rc = user_len;
946 } else if (strncmp(str, "off", 3) == 0) {
947 dasd_profile_off(prof);
948 } else
949 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200950 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200951 return rc;
952}
953
954static void dasd_stats_array(struct seq_file *m, unsigned int *array)
955{
956 int i;
957
958 for (i = 0; i < 32; i++)
959 seq_printf(m, "%u ", array[i]);
960 seq_putc(m, '\n');
961}
962
963static void dasd_stats_seq_print(struct seq_file *m,
964 struct dasd_profile_info *data)
965{
966 seq_printf(m, "start_time %ld.%09ld\n",
967 data->starttod.tv_sec, data->starttod.tv_nsec);
968 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
969 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
970 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
971 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
972 seq_printf(m, "histogram_sectors ");
973 dasd_stats_array(m, data->dasd_io_secs);
974 seq_printf(m, "histogram_io_times ");
975 dasd_stats_array(m, data->dasd_io_times);
976 seq_printf(m, "histogram_io_times_weighted ");
977 dasd_stats_array(m, data->dasd_io_timps);
978 seq_printf(m, "histogram_time_build_to_ssch ");
979 dasd_stats_array(m, data->dasd_io_time1);
980 seq_printf(m, "histogram_time_ssch_to_irq ");
981 dasd_stats_array(m, data->dasd_io_time2);
982 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
983 dasd_stats_array(m, data->dasd_io_time2ps);
984 seq_printf(m, "histogram_time_irq_to_end ");
985 dasd_stats_array(m, data->dasd_io_time3);
986 seq_printf(m, "histogram_ccw_queue_length ");
987 dasd_stats_array(m, data->dasd_io_nr_req);
988 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
989 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
990 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
991 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
992 seq_printf(m, "histogram_read_sectors ");
993 dasd_stats_array(m, data->dasd_read_secs);
994 seq_printf(m, "histogram_read_times ");
995 dasd_stats_array(m, data->dasd_read_times);
996 seq_printf(m, "histogram_read_time_build_to_ssch ");
997 dasd_stats_array(m, data->dasd_read_time1);
998 seq_printf(m, "histogram_read_time_ssch_to_irq ");
999 dasd_stats_array(m, data->dasd_read_time2);
1000 seq_printf(m, "histogram_read_time_irq_to_end ");
1001 dasd_stats_array(m, data->dasd_read_time3);
1002 seq_printf(m, "histogram_read_ccw_queue_length ");
1003 dasd_stats_array(m, data->dasd_read_nr_req);
1004}
1005
1006static int dasd_stats_show(struct seq_file *m, void *v)
1007{
1008 struct dasd_profile *profile;
1009 struct dasd_profile_info *data;
1010
1011 profile = m->private;
1012 spin_lock_bh(&profile->lock);
1013 data = profile->data;
1014 if (!data) {
1015 spin_unlock_bh(&profile->lock);
1016 seq_printf(m, "disabled\n");
1017 return 0;
1018 }
1019 dasd_stats_seq_print(m, data);
1020 spin_unlock_bh(&profile->lock);
1021 return 0;
1022}
1023
1024static int dasd_stats_open(struct inode *inode, struct file *file)
1025{
1026 struct dasd_profile *profile = inode->i_private;
1027 return single_open(file, dasd_stats_show, profile);
1028}
1029
1030static const struct file_operations dasd_stats_raw_fops = {
1031 .owner = THIS_MODULE,
1032 .open = dasd_stats_open,
1033 .read = seq_read,
1034 .llseek = seq_lseek,
1035 .release = single_release,
1036 .write = dasd_stats_write,
1037};
1038
1039static ssize_t dasd_stats_global_write(struct file *file,
1040 const char __user *user_buf,
1041 size_t user_len, loff_t *pos)
1042{
1043 char *buffer, *str;
1044 ssize_t rc;
1045
1046 if (user_len > 65536)
1047 user_len = 65536;
1048 buffer = dasd_get_user_string(user_buf, user_len);
1049 if (IS_ERR(buffer))
1050 return PTR_ERR(buffer);
1051 str = skip_spaces(buffer);
1052 rc = user_len;
1053 if (strncmp(str, "reset", 5) == 0) {
1054 dasd_global_profile_reset();
1055 } else if (strncmp(str, "on", 2) == 0) {
1056 dasd_global_profile_reset();
1057 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1058 } else if (strncmp(str, "off", 3) == 0) {
1059 dasd_global_profile_level = DASD_PROFILE_OFF;
1060 } else
1061 rc = -EINVAL;
Stefan Weinhubere4258d52011-08-03 16:44:20 +02001062 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001063 return rc;
1064}
1065
1066static int dasd_stats_global_show(struct seq_file *m, void *v)
1067{
1068 if (!dasd_global_profile_level) {
1069 seq_printf(m, "disabled\n");
1070 return 0;
1071 }
1072 dasd_stats_seq_print(m, &dasd_global_profile_data);
1073 return 0;
1074}
1075
1076static int dasd_stats_global_open(struct inode *inode, struct file *file)
1077{
1078 return single_open(file, dasd_stats_global_show, NULL);
1079}
1080
1081static const struct file_operations dasd_stats_global_fops = {
1082 .owner = THIS_MODULE,
1083 .open = dasd_stats_global_open,
1084 .read = seq_read,
1085 .llseek = seq_lseek,
1086 .release = single_release,
1087 .write = dasd_stats_global_write,
1088};
1089
1090static void dasd_profile_init(struct dasd_profile *profile,
1091 struct dentry *base_dentry)
1092{
Al Virof4ae40a2011-07-24 04:33:43 -04001093 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001094 struct dentry *pde;
1095
1096 if (!base_dentry)
1097 return;
1098 profile->dentry = NULL;
1099 profile->data = NULL;
1100 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1101 pde = debugfs_create_file("statistics", mode, base_dentry,
1102 profile, &dasd_stats_raw_fops);
1103 if (pde && !IS_ERR(pde))
1104 profile->dentry = pde;
1105 return;
1106}
1107
1108static void dasd_profile_exit(struct dasd_profile *profile)
1109{
1110 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001111 debugfs_remove(profile->dentry);
1112 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001113}
1114
1115static void dasd_statistics_removeroot(void)
1116{
1117 dasd_global_profile_level = DASD_PROFILE_OFF;
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001118 debugfs_remove(dasd_global_profile_dentry);
1119 dasd_global_profile_dentry = NULL;
1120 debugfs_remove(dasd_debugfs_global_entry);
1121 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001122}
1123
1124static void dasd_statistics_createroot(void)
1125{
Al Virof4ae40a2011-07-24 04:33:43 -04001126 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001127 struct dentry *pde;
1128
1129 dasd_debugfs_root_entry = NULL;
1130 dasd_debugfs_global_entry = NULL;
1131 dasd_global_profile_dentry = NULL;
1132 pde = debugfs_create_dir("dasd", NULL);
1133 if (!pde || IS_ERR(pde))
1134 goto error;
1135 dasd_debugfs_root_entry = pde;
1136 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1137 if (!pde || IS_ERR(pde))
1138 goto error;
1139 dasd_debugfs_global_entry = pde;
1140
1141 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1142 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1143 NULL, &dasd_stats_global_fops);
1144 if (!pde || IS_ERR(pde))
1145 goto error;
1146 dasd_global_profile_dentry = pde;
1147 return;
1148
1149error:
1150 DBF_EVENT(DBF_ERR, "%s",
1151 "Creation of the dasd debugfs interface failed");
1152 dasd_statistics_removeroot();
1153 return;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001157#define dasd_profile_start(block, cqr, req) do {} while (0)
1158#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001159
1160static void dasd_statistics_createroot(void)
1161{
1162 return;
1163}
1164
1165static void dasd_statistics_removeroot(void)
1166{
1167 return;
1168}
1169
1170int dasd_stats_generic_show(struct seq_file *m, void *v)
1171{
1172 seq_printf(m, "Statistics are not activated in this kernel\n");
1173 return 0;
1174}
1175
1176static void dasd_profile_init(struct dasd_profile *profile,
1177 struct dentry *base_dentry)
1178{
1179 return;
1180}
1181
1182static void dasd_profile_exit(struct dasd_profile *profile)
1183{
1184 return;
1185}
1186
1187int dasd_profile_on(struct dasd_profile *profile)
1188{
1189 return 0;
1190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192#endif /* CONFIG_DASD_PROFILE */
1193
1194/*
1195 * Allocate memory for a channel program with 'cplength' channel
1196 * command words and 'datasize' additional space. There are two
1197 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1198 * memory and 2) dasd_smalloc_request uses the static ccw memory
1199 * that gets allocated for each device.
1200 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001201struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001202 int datasize,
1203 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct dasd_ccw_req *cqr;
1206
1207 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001208 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001209 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001211 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (cqr == NULL)
1213 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 cqr->cpaddr = NULL;
1215 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001216 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 GFP_ATOMIC | GFP_DMA);
1218 if (cqr->cpaddr == NULL) {
1219 kfree(cqr);
1220 return ERR_PTR(-ENOMEM);
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 cqr->data = NULL;
1224 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001225 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001227 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 kfree(cqr);
1229 return ERR_PTR(-ENOMEM);
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001232 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1234 dasd_get_device(device);
1235 return cqr;
1236}
1237
Stefan Haberland68b781f2009-09-11 10:28:29 +02001238struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001239 int datasize,
1240 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 unsigned long flags;
1243 struct dasd_ccw_req *cqr;
1244 char *data;
1245 int size;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1248 if (cplength > 0)
1249 size += cplength * sizeof(struct ccw1);
1250 if (datasize > 0)
1251 size += datasize;
1252 spin_lock_irqsave(&device->mem_lock, flags);
1253 cqr = (struct dasd_ccw_req *)
1254 dasd_alloc_chunk(&device->ccw_chunks, size);
1255 spin_unlock_irqrestore(&device->mem_lock, flags);
1256 if (cqr == NULL)
1257 return ERR_PTR(-ENOMEM);
1258 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1259 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1260 cqr->cpaddr = NULL;
1261 if (cplength > 0) {
1262 cqr->cpaddr = (struct ccw1 *) data;
1263 data += cplength*sizeof(struct ccw1);
1264 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1265 }
1266 cqr->data = NULL;
1267 if (datasize > 0) {
1268 cqr->data = data;
1269 memset(cqr->data, 0, datasize);
1270 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001271 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1273 dasd_get_device(device);
1274 return cqr;
1275}
1276
1277/*
1278 * Free memory of a channel program. This function needs to free all the
1279 * idal lists that might have been created by dasd_set_cda and the
1280 * struct dasd_ccw_req itself.
1281 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001282void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -08001284#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct ccw1 *ccw;
1286
1287 /* Clear any idals used for the request. */
1288 ccw = cqr->cpaddr;
1289 do {
1290 clear_normalized_cda(ccw);
1291 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1292#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -08001293 kfree(cqr->cpaddr);
1294 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 kfree(cqr);
1296 dasd_put_device(device);
1297}
1298
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001299void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 unsigned long flags;
1302
1303 spin_lock_irqsave(&device->mem_lock, flags);
1304 dasd_free_chunk(&device->ccw_chunks, cqr);
1305 spin_unlock_irqrestore(&device->mem_lock, flags);
1306 dasd_put_device(device);
1307}
1308
1309/*
1310 * Check discipline magic in cqr.
1311 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001312static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct dasd_device *device;
1315
1316 if (cqr == NULL)
1317 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001318 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001320 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 " dasd_ccw_req 0x%08x magic doesn't match"
1322 " discipline 0x%08x",
1323 cqr->magic,
1324 *(unsigned int *) device->discipline->name);
1325 return -EINVAL;
1326 }
1327 return 0;
1328}
1329
1330/*
1331 * Terminate the current i/o and set the request to clear_pending.
1332 * Timer keeps device runnig.
1333 * ccw_device_clear can fail if the i/o subsystem
1334 * is in a bad mood.
1335 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001336int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct dasd_device *device;
1339 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001340 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* Check the cqr */
1343 rc = dasd_check_cqr(cqr);
1344 if (rc)
1345 return rc;
1346 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001347 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1349 rc = ccw_device_clear(device->cdev, (long) cqr);
1350 switch (rc) {
1351 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001352 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001353 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001354 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 DBF_DEV_EVENT(DBF_DEBUG, device,
1356 "terminate cqr %p successful",
1357 cqr);
1358 break;
1359 case -ENODEV:
1360 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1361 "device gone, retry");
1362 break;
1363 case -EIO:
1364 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1365 "I/O error, retry");
1366 break;
1367 case -EINVAL:
1368 case -EBUSY:
1369 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1370 "device busy, retry later");
1371 break;
1372 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001373 /* internal error 10 - unknown rc*/
1374 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1375 dev_err(&device->cdev->dev, "An error occurred in the "
1376 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 BUG();
1378 break;
1379 }
1380 retries++;
1381 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001382 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return rc;
1384}
1385
1386/*
1387 * Start the i/o. This start_IO can fail if the channel is really busy.
1388 * In that case set up a timer to start the request later.
1389 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001390int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct dasd_device *device;
1393 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001394 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /* Check the cqr */
1397 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001398 if (rc) {
1399 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001401 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001402 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001403 if (((cqr->block &&
1404 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1405 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1406 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1407 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1408 "because of stolen lock", cqr);
1409 cqr->status = DASD_CQR_ERROR;
1410 cqr->intrc = -EPERM;
1411 return -EPERM;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001414 /* internal error 14 - start_IO run out of retries */
1415 sprintf(errorstring, "14 %p", cqr);
1416 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1417 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001418 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return -EIO;
1420 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001421 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 cqr->starttime = jiffies;
1423 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001424 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1425 cqr->lpm &= device->path_data.opm;
1426 if (!cqr->lpm)
1427 cqr->lpm = device->path_data.opm;
1428 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001429 if (cqr->cpmode == 1) {
1430 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1431 (long) cqr, cqr->lpm);
1432 } else {
1433 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1434 (long) cqr, cqr->lpm, 0);
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 switch (rc) {
1437 case 0:
1438 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 break;
1440 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001441 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 "start_IO: device busy, retry later");
1443 break;
1444 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001445 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 "start_IO: request timeout, retry later");
1447 break;
1448 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001449 /* -EACCES indicates that the request used only a subset of the
1450 * available paths and all these paths are gone. If the lpm of
1451 * this request was only a subset of the opm (e.g. the ppm) then
1452 * we just do a retry with all available paths.
1453 * If we already use the full opm, something is amiss, and we
1454 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001456 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1457 DBF_DEV_EVENT(DBF_WARNING, device,
1458 "start_IO: selected paths gone (%x)",
1459 cqr->lpm);
1460 } else if (cqr->lpm != device->path_data.opm) {
1461 cqr->lpm = device->path_data.opm;
1462 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1463 "start_IO: selected paths gone,"
1464 " retry on all paths");
1465 } else {
1466 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1467 "start_IO: all paths in opm gone,"
1468 " do path verification");
1469 dasd_generic_last_path_gone(device);
1470 device->path_data.opm = 0;
1471 device->path_data.ppm = 0;
1472 device->path_data.npm = 0;
1473 device->path_data.tbvpm =
1474 ccw_device_get_path_mask(device->cdev);
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001478 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001479 "start_IO: -ENODEV device gone, retry");
1480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001482 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001483 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001485 case -EINVAL:
1486 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001487 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001488 "start_IO: -EINVAL device currently "
1489 "not accessible");
1490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001492 /* internal error 11 - unknown rc */
1493 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1494 dev_err(&device->cdev->dev,
1495 "An error occurred in the DASD device driver, "
1496 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 BUG();
1498 break;
1499 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001500 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return rc;
1502}
1503
1504/*
1505 * Timeout function for dasd devices. This is used for different purposes
1506 * 1) missing interrupt handler for normal operation
1507 * 2) delayed start of request where start_IO failed with -EBUSY
1508 * 3) timeout for missing state change interrupts
1509 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1510 * DASD_CQR_QUEUED for 2) and 3).
1511 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001512static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 unsigned long flags;
1515 struct dasd_device *device;
1516
1517 device = (struct dasd_device *) ptr;
1518 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1519 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001520 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001522 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525/*
1526 * Setup timeout for a device in jiffies.
1527 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001528void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001530 if (expires == 0)
1531 del_timer(&device->timer);
1532 else
1533 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536/*
1537 * Clear timeout for a device.
1538 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001539void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001541 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001544static void dasd_handle_killed_request(struct ccw_device *cdev,
1545 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 struct dasd_ccw_req *cqr;
1548 struct dasd_device *device;
1549
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001550 if (!intparm)
1551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 cqr = (struct dasd_ccw_req *) intparm;
1553 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001554 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1555 "invalid status in handle_killed_request: "
1556 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return;
1558 }
1559
Stefan Haberland589c74d2010-02-26 22:37:47 +01001560 device = dasd_device_from_cdev_locked(cdev);
1561 if (IS_ERR(device)) {
1562 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1563 "unable to get device from cdev");
1564 return;
1565 }
1566
1567 if (!cqr->startdev ||
1568 device != cqr->startdev ||
1569 strncmp(cqr->startdev->discipline->ebcname,
1570 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001571 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1572 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001573 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return;
1575 }
1576
1577 /* Schedule request to be retried. */
1578 cqr->status = DASD_CQR_QUEUED;
1579
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001580 dasd_device_clear_timer(device);
1581 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 dasd_put_device(device);
1583}
1584
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001585void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001587 /* First of all start sense subsystem status request. */
1588 dasd_eer_snss(device);
1589
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001590 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001591 dasd_schedule_device_bh(device);
1592 if (device->block)
1593 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596/*
1597 * Interrupt handler for "normal" ssch-io based dasd devices.
1598 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001599void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1600 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct dasd_ccw_req *cqr, *next;
1603 struct dasd_device *device;
1604 unsigned long long now;
1605 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (IS_ERR(irb)) {
1608 switch (PTR_ERR(irb)) {
1609 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 break;
1611 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001612 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1613 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 break;
1615 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001616 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1617 "unknown error %ld\n", __func__,
1618 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001620 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return;
1622 }
1623
Heiko Carstens1aae0562013-01-30 09:49:40 +01001624 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001625 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001626 /* check for conditions that should be handled immediately */
1627 if (!cqr ||
1628 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1629 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001630 if (cqr)
1631 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001632 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001633 if (IS_ERR(device))
1634 return;
1635 /* ignore unsolicited interrupts for DIAG discipline */
1636 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001638 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001640 device->discipline->dump_sense_dbf(device, irb, "int");
1641 if (device->features & DASD_FEATURE_ERPLOG)
1642 device->discipline->dump_sense(device, cqr, irb);
1643 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001644 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001646 if (!cqr)
1647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001649 device = (struct dasd_device *) cqr->startdev;
1650 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001652 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1653 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return;
1655 }
1656
1657 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001658 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001659 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001660 cqr->status = DASD_CQR_CLEARED;
1661 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001662 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001663 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return;
1665 }
1666
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001667 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001669 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1670 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return;
1672 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001673
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001674 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001676 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1677 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001678 /* request was completed successfully */
1679 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 cqr->stopclk = now;
1681 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001682 if (cqr->devlist.next != &device->ccw_queue) {
1683 next = list_entry(cqr->devlist.next,
1684 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001686 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001687 /*
1688 * If we don't want complex ERP for this request, then just
1689 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001690 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001691 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001692 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001693 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001694 DBF_DEV_EVENT(DBF_DEBUG, device,
1695 "default ERP in fastpath "
1696 "(%i retries left)",
1697 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001698 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1699 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001700 cqr->status = DASD_CQR_QUEUED;
1701 next = cqr;
1702 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001704 }
1705 if (next && (next->status == DASD_CQR_QUEUED) &&
1706 (!device->stopped)) {
1707 if (device->discipline->start_IO(next) == 0)
1708 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001711 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001713 dasd_device_clear_timer(device);
1714 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001717enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1718{
1719 struct dasd_device *device;
1720
1721 device = dasd_device_from_cdev_locked(cdev);
1722
1723 if (IS_ERR(device))
1724 goto out;
1725 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1726 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001727 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001728 dasd_put_device(device);
1729 goto out;
1730 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001731 if (device->discipline->dump_sense_dbf)
1732 device->discipline->dump_sense_dbf(device, irb, "uc");
1733 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001734 dasd_put_device(device);
1735out:
1736 return UC_TODO_RETRY;
1737}
1738EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001741 * If we have an error on a dasd_block layer request then we cancel
1742 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001744static void __dasd_device_recovery(struct dasd_device *device,
1745 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct list_head *l, *n;
1748 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001751 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001753 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001755
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001756 list_for_each_safe(l, n, &device->ccw_queue) {
1757 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1758 if (cqr->status == DASD_CQR_QUEUED &&
1759 ref_cqr->block == cqr->block) {
1760 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 }
1763};
1764
1765/*
1766 * Remove those ccw requests from the queue that need to be returned
1767 * to the upper layer.
1768 */
1769static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1770 struct list_head *final_queue)
1771{
1772 struct list_head *l, *n;
1773 struct dasd_ccw_req *cqr;
1774
1775 /* Process request with final status. */
1776 list_for_each_safe(l, n, &device->ccw_queue) {
1777 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1778
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001779 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001780 if (cqr->status == DASD_CQR_QUEUED ||
1781 cqr->status == DASD_CQR_IN_IO ||
1782 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001783 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001784 if (cqr->status == DASD_CQR_ERROR) {
1785 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001787 /* Rechain finished requests to final queue */
1788 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790}
1791
1792/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001793 * the cqrs from the final queue are returned to the upper layer
1794 * by setting a dasd_block state and calling the callback function
1795 */
1796static void __dasd_device_process_final_queue(struct dasd_device *device,
1797 struct list_head *final_queue)
1798{
1799 struct list_head *l, *n;
1800 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001801 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001802 void (*callback)(struct dasd_ccw_req *, void *data);
1803 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001804 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001805
1806 list_for_each_safe(l, n, final_queue) {
1807 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1808 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001809 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001810 callback = cqr->callback;
1811 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001812 if (block)
1813 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001814 switch (cqr->status) {
1815 case DASD_CQR_SUCCESS:
1816 cqr->status = DASD_CQR_DONE;
1817 break;
1818 case DASD_CQR_ERROR:
1819 cqr->status = DASD_CQR_NEED_ERP;
1820 break;
1821 case DASD_CQR_CLEARED:
1822 cqr->status = DASD_CQR_TERMINATED;
1823 break;
1824 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001825 /* internal error 12 - wrong cqr status*/
1826 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1827 dev_err(&device->cdev->dev,
1828 "An error occurred in the DASD device driver, "
1829 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001830 BUG();
1831 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001832 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001833 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001834 if (block)
1835 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001836 }
1837}
1838
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001839/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 * Take a look at the first request on the ccw queue and check
1841 * if it reached its expire time. If so, terminate the IO.
1842 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001843static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct dasd_ccw_req *cqr;
1846
1847 if (list_empty(&device->ccw_queue))
1848 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001849 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001850 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1851 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001852 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1853 /*
1854 * IO in safe offline processing should not
1855 * run out of retries
1856 */
1857 cqr->retries++;
1858 }
Horst Hummel29145a62006-12-04 15:40:15 +01001859 if (device->discipline->term_IO(cqr) != 0) {
1860 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001861 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001862 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001863 "ended, retrying in 5 s\n",
1864 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001865 cqr->expires += 5*HZ;
1866 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001867 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001868 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001869 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001870 "remaining\n", cqr, (cqr->expires/HZ),
1871 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 }
1874}
1875
1876/*
1877 * Take a look at the first request on the ccw queue and check
1878 * if it needs to be started.
1879 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001880static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct dasd_ccw_req *cqr;
1883 int rc;
1884
1885 if (list_empty(&device->ccw_queue))
1886 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001887 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001888 if (cqr->status != DASD_CQR_QUEUED)
1889 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001890 /* when device is stopped, return request to previous layer
1891 * exception: only the disconnect or unresumed bits are set and the
1892 * cqr is a path verification request
1893 */
1894 if (device->stopped &&
1895 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1896 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1897 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001898 cqr->status = DASD_CQR_CLEARED;
1899 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001900 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001901 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001902
1903 rc = device->discipline->start_IO(cqr);
1904 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001905 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001906 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001907 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001908 } else
1909 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001910 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001911}
1912
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001913static void __dasd_device_check_path_events(struct dasd_device *device)
1914{
1915 int rc;
1916
1917 if (device->path_data.tbvpm) {
1918 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1919 DASD_UNRESUMED_PM))
1920 return;
1921 rc = device->discipline->verify_path(
1922 device, device->path_data.tbvpm);
1923 if (rc)
1924 dasd_device_set_timer(device, 50);
1925 else
1926 device->path_data.tbvpm = 0;
1927 }
1928};
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001931 * Go through all request on the dasd_device request queue,
1932 * terminate them on the cdev if necessary, and return them to the
1933 * submitting layer via callback.
1934 * Note:
1935 * Make sure that all 'submitting layers' still exist when
1936 * this function is called!. In other words, when 'device' is a base
1937 * device then all block layer requests must have been removed before
1938 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001940int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001942 struct dasd_ccw_req *cqr, *n;
1943 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 INIT_LIST_HEAD(&flush_queue);
1947 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001948 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001950 /* Check status and move request to flush_queue */
1951 switch (cqr->status) {
1952 case DASD_CQR_IN_IO:
1953 rc = device->discipline->term_IO(cqr);
1954 if (rc) {
1955 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001956 dev_err(&device->cdev->dev,
1957 "Flushing the DASD request queue "
1958 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001959 /* stop flush processing */
1960 goto finished;
1961 }
1962 break;
1963 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001964 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001965 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001966 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001967 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001968 break;
1969 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001970 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001971 }
Horst Hummel8f617012006-08-30 14:33:33 +02001972finished:
1973 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001974 /*
1975 * After this point all requests must be in state CLEAR_PENDING,
1976 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1977 * one of the others.
1978 */
1979 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1980 wait_event(dasd_flush_wq,
1981 (cqr->status != DASD_CQR_CLEAR_PENDING));
1982 /*
1983 * Now set each request back to TERMINATED, DONE or NEED_ERP
1984 * and call the callback function of flushed requests
1985 */
1986 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001987 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990/*
1991 * Acquire the device lock and process queues for the device.
1992 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001993static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 atomic_set (&device->tasklet_scheduled, 0);
1998 INIT_LIST_HEAD(&final_queue);
1999 spin_lock_irq(get_ccwdev_lock(device->cdev));
2000 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002001 __dasd_device_check_expire(device);
2002 /* find final requests on ccw queue */
2003 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002004 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2006 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002007 __dasd_device_process_final_queue(device, &final_queue);
2008 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002010 __dasd_device_start_head(device);
2011 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002012 if (waitqueue_active(&shutdown_waitq))
2013 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 dasd_put_device(device);
2015}
2016
2017/*
2018 * Schedules a call to dasd_tasklet over the device tasklet.
2019 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002020void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002023 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return;
2025 dasd_get_device(device);
2026 tasklet_hi_schedule(&device->tasklet);
2027}
2028
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002029void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2030{
2031 device->stopped |= bits;
2032}
2033EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2034
2035void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2036{
2037 device->stopped &= ~bits;
2038 if (!device->stopped)
2039 wake_up(&generic_waitq);
2040}
2041EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002044 * Queue a request to the head of the device ccw_queue.
2045 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002047void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct dasd_device *device;
2050 unsigned long flags;
2051
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002054 cqr->status = DASD_CQR_QUEUED;
2055 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002057 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2059}
2060
2061/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 * Queue a request to the tail of the device ccw_queue.
2063 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002065void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct dasd_device *device;
2068 unsigned long flags;
2069
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002070 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 cqr->status = DASD_CQR_QUEUED;
2073 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002075 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2077}
2078
2079/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002080 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002082void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002084 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2085 cqr->callback_data = DASD_SLEEPON_END_TAG;
2086 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2087 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
Stefan Haberland5915a872011-10-30 15:16:57 +01002089EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002091static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct dasd_device *device;
2094 int rc;
2095
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002096 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002098 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2100 return rc;
2101}
2102
2103/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002104 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2105 */
2106static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2107{
2108 struct dasd_device *device;
2109 dasd_erp_fn_t erp_fn;
2110
2111 if (cqr->status == DASD_CQR_FILLED)
2112 return 0;
2113 device = cqr->startdev;
2114 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2115 if (cqr->status == DASD_CQR_TERMINATED) {
2116 device->discipline->handle_terminated_request(cqr);
2117 return 1;
2118 }
2119 if (cqr->status == DASD_CQR_NEED_ERP) {
2120 erp_fn = device->discipline->erp_action(cqr);
2121 erp_fn(cqr);
2122 return 1;
2123 }
2124 if (cqr->status == DASD_CQR_FAILED)
2125 dasd_log_sense(cqr, &cqr->irb);
2126 if (cqr->refers) {
2127 __dasd_process_erp(device, cqr);
2128 return 1;
2129 }
2130 }
2131 return 0;
2132}
2133
2134static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2135{
2136 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2137 if (cqr->refers) /* erp is not done yet */
2138 return 1;
2139 return ((cqr->status != DASD_CQR_DONE) &&
2140 (cqr->status != DASD_CQR_FAILED));
2141 } else
2142 return (cqr->status == DASD_CQR_FILLED);
2143}
2144
2145static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2146{
2147 struct dasd_device *device;
2148 int rc;
2149 struct list_head ccw_queue;
2150 struct dasd_ccw_req *cqr;
2151
2152 INIT_LIST_HEAD(&ccw_queue);
2153 maincqr->status = DASD_CQR_FILLED;
2154 device = maincqr->startdev;
2155 list_add(&maincqr->blocklist, &ccw_queue);
2156 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2157 cqr = list_first_entry(&ccw_queue,
2158 struct dasd_ccw_req, blocklist)) {
2159
2160 if (__dasd_sleep_on_erp(cqr))
2161 continue;
2162 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2163 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002164 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2165 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2166 cqr->status = DASD_CQR_FAILED;
2167 cqr->intrc = -EPERM;
2168 continue;
2169 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002170 /* Non-temporary stop condition will trigger fail fast */
2171 if (device->stopped & ~DASD_STOPPED_PENDING &&
2172 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2173 (!dasd_eer_enabled(device))) {
2174 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002175 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002176 continue;
2177 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002178 /* Don't try to start requests if device is stopped */
2179 if (interruptible) {
2180 rc = wait_event_interruptible(
2181 generic_waitq, !(device->stopped));
2182 if (rc == -ERESTARTSYS) {
2183 cqr->status = DASD_CQR_FAILED;
2184 maincqr->intrc = rc;
2185 continue;
2186 }
2187 } else
2188 wait_event(generic_waitq, !(device->stopped));
2189
Stefan Haberland5915a872011-10-30 15:16:57 +01002190 if (!cqr->callback)
2191 cqr->callback = dasd_wakeup_cb;
2192
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002193 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002194 dasd_add_request_tail(cqr);
2195 if (interruptible) {
2196 rc = wait_event_interruptible(
2197 generic_waitq, _wait_for_wakeup(cqr));
2198 if (rc == -ERESTARTSYS) {
2199 dasd_cancel_req(cqr);
2200 /* wait (non-interruptible) for final status */
2201 wait_event(generic_waitq,
2202 _wait_for_wakeup(cqr));
2203 cqr->status = DASD_CQR_FAILED;
2204 maincqr->intrc = rc;
2205 continue;
2206 }
2207 } else
2208 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2209 }
2210
Heiko Carstens1aae0562013-01-30 09:49:40 +01002211 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002212 if ((maincqr->status != DASD_CQR_DONE) &&
2213 (maincqr->intrc != -ERESTARTSYS))
2214 dasd_log_sense(maincqr, &maincqr->irb);
2215 if (maincqr->status == DASD_CQR_DONE)
2216 rc = 0;
2217 else if (maincqr->intrc)
2218 rc = maincqr->intrc;
2219 else
2220 rc = -EIO;
2221 return rc;
2222}
2223
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002224static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2225{
2226 struct dasd_ccw_req *cqr;
2227
2228 list_for_each_entry(cqr, ccw_queue, blocklist) {
2229 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2230 return 0;
2231 }
2232
2233 return 1;
2234}
2235
2236static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2237{
2238 struct dasd_device *device;
2239 int rc;
2240 struct dasd_ccw_req *cqr, *n;
2241
2242retry:
2243 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2244 device = cqr->startdev;
2245 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2246 continue;
2247
2248 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2249 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2250 cqr->status = DASD_CQR_FAILED;
2251 cqr->intrc = -EPERM;
2252 continue;
2253 }
2254 /*Non-temporary stop condition will trigger fail fast*/
2255 if (device->stopped & ~DASD_STOPPED_PENDING &&
2256 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2257 !dasd_eer_enabled(device)) {
2258 cqr->status = DASD_CQR_FAILED;
2259 cqr->intrc = -EAGAIN;
2260 continue;
2261 }
2262
2263 /*Don't try to start requests if device is stopped*/
2264 if (interruptible) {
2265 rc = wait_event_interruptible(
2266 generic_waitq, !device->stopped);
2267 if (rc == -ERESTARTSYS) {
2268 cqr->status = DASD_CQR_FAILED;
2269 cqr->intrc = rc;
2270 continue;
2271 }
2272 } else
2273 wait_event(generic_waitq, !(device->stopped));
2274
2275 if (!cqr->callback)
2276 cqr->callback = dasd_wakeup_cb;
2277 cqr->callback_data = DASD_SLEEPON_START_TAG;
2278 dasd_add_request_tail(cqr);
2279 }
2280
2281 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2282
2283 rc = 0;
2284 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2285 if (__dasd_sleep_on_erp(cqr))
2286 rc = 1;
2287 }
2288 if (rc)
2289 goto retry;
2290
2291
2292 return 0;
2293}
2294
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002295/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002296 * Queue a request to the tail of the device ccw_queue and wait for
2297 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002299int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002301 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002305 * Start requests from a ccw_queue and wait for their completion.
2306 */
2307int dasd_sleep_on_queue(struct list_head *ccw_queue)
2308{
2309 return _dasd_sleep_on_queue(ccw_queue, 0);
2310}
2311EXPORT_SYMBOL(dasd_sleep_on_queue);
2312
2313/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002314 * Queue a request to the tail of the device ccw_queue and wait
2315 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002317int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002319 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
2322/*
2323 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2324 * for eckd devices) the currently running request has to be terminated
2325 * and be put back to status queued, before the special request is added
2326 * to the head of the queue. Then the special request is waited on normally.
2327 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002328static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002331 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 if (list_empty(&device->ccw_queue))
2334 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002335 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002336 rc = device->discipline->term_IO(cqr);
2337 if (!rc)
2338 /*
2339 * CQR terminated because a more important request is pending.
2340 * Undo decreasing of retry counter because this is
2341 * not an error case.
2342 */
2343 cqr->retries++;
2344 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002347int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct dasd_device *device;
2350 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002351
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002352 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002353 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2354 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2355 cqr->status = DASD_CQR_FAILED;
2356 cqr->intrc = -EPERM;
2357 return -EIO;
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 spin_lock_irq(get_ccwdev_lock(device->cdev));
2360 rc = _dasd_term_running_cqr(device);
2361 if (rc) {
2362 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2363 return rc;
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002366 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002368 /*
2369 * add new request as second
2370 * first the terminated cqr needs to be finished
2371 */
2372 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002375 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2378
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002379 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002380
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002381 if (cqr->status == DASD_CQR_DONE)
2382 rc = 0;
2383 else if (cqr->intrc)
2384 rc = cqr->intrc;
2385 else
2386 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002387
2388 /* kick tasklets */
2389 dasd_schedule_device_bh(device);
2390 if (device->block)
2391 dasd_schedule_block_bh(device->block);
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return rc;
2394}
2395
2396/*
2397 * Cancels a request that was started with dasd_sleep_on_req.
2398 * This is useful to timeout requests. The request will be
2399 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002400 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002401 * negative error code if termination failed
2402 * Cancellation of a request is an asynchronous operation! The calling
2403 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002405int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002407 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 unsigned long flags;
2409 int rc;
2410
2411 rc = 0;
2412 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2413 switch (cqr->status) {
2414 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002415 /* request was not started - just set to cleared */
2416 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 break;
2418 case DASD_CQR_IN_IO:
2419 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002420 rc = device->discipline->term_IO(cqr);
2421 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002422 dev_err(&device->cdev->dev,
2423 "Cancelling request %p failed with rc=%d\n",
2424 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002425 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002426 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002429 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002433 dasd_schedule_device_bh(device);
2434 return rc;
2435}
2436
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002437/*
2438 * SECTION: Operations of the dasd_block layer.
2439 */
2440
2441/*
2442 * Timeout function for dasd_block. This is used when the block layer
2443 * is waiting for something that may not come reliably, (e.g. a state
2444 * change interrupt)
2445 */
2446static void dasd_block_timeout(unsigned long ptr)
2447{
2448 unsigned long flags;
2449 struct dasd_block *block;
2450
2451 block = (struct dasd_block *) ptr;
2452 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2453 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002454 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002455 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2456 dasd_schedule_block_bh(block);
2457}
2458
2459/*
2460 * Setup timeout for a dasd_block in jiffies.
2461 */
2462void dasd_block_set_timer(struct dasd_block *block, int expires)
2463{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002464 if (expires == 0)
2465 del_timer(&block->timer);
2466 else
2467 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002468}
2469
2470/*
2471 * Clear timeout for a dasd_block.
2472 */
2473void dasd_block_clear_timer(struct dasd_block *block)
2474{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002475 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002476}
2477
2478/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002479 * Process finished error recovery ccw.
2480 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002481static void __dasd_process_erp(struct dasd_device *device,
2482 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002483{
2484 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002485
2486 if (cqr->status == DASD_CQR_DONE)
2487 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2488 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002489 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002490 erp_fn = device->discipline->erp_postaction(cqr);
2491 erp_fn(cqr);
2492}
2493
2494/*
2495 * Fetch requests from the block device queue.
2496 */
2497static void __dasd_process_request_queue(struct dasd_block *block)
2498{
2499 struct request_queue *queue;
2500 struct request *req;
2501 struct dasd_ccw_req *cqr;
2502 struct dasd_device *basedev;
2503 unsigned long flags;
2504 queue = block->request_queue;
2505 basedev = block->base;
2506 /* No queue ? Then there is nothing to do. */
2507 if (queue == NULL)
2508 return;
2509
2510 /*
2511 * We requeue request from the block device queue to the ccw
2512 * queue only in two states. In state DASD_STATE_READY the
2513 * partition detection is done and we need to requeue requests
2514 * for that. State DASD_STATE_ONLINE is normal block device
2515 * operation.
2516 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002517 if (basedev->state < DASD_STATE_READY) {
2518 while ((req = blk_fetch_request(block->request_queue)))
2519 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002520 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002521 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002522 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002523 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002524 if (basedev->features & DASD_FEATURE_READONLY &&
2525 rq_data_dir(req) == WRITE) {
2526 DBF_DEV_EVENT(DBF_ERR, basedev,
2527 "Rejecting write request %p",
2528 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002529 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002530 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002531 continue;
2532 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002533 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2534 (basedev->features & DASD_FEATURE_FAILFAST ||
2535 blk_noretry_request(req))) {
2536 DBF_DEV_EVENT(DBF_ERR, basedev,
2537 "Rejecting failfast request %p",
2538 req);
2539 blk_start_request(req);
2540 __blk_end_request_all(req, -ETIMEDOUT);
2541 continue;
2542 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002543 cqr = basedev->discipline->build_cp(basedev, block, req);
2544 if (IS_ERR(cqr)) {
2545 if (PTR_ERR(cqr) == -EBUSY)
2546 break; /* normal end condition */
2547 if (PTR_ERR(cqr) == -ENOMEM)
2548 break; /* terminate request queue loop */
2549 if (PTR_ERR(cqr) == -EAGAIN) {
2550 /*
2551 * The current request cannot be build right
2552 * now, we have to try later. If this request
2553 * is the head-of-queue we stop the device
2554 * for 1/2 second.
2555 */
2556 if (!list_empty(&block->ccw_queue))
2557 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002558 spin_lock_irqsave(
2559 get_ccwdev_lock(basedev->cdev), flags);
2560 dasd_device_set_stop_bits(basedev,
2561 DASD_STOPPED_PENDING);
2562 spin_unlock_irqrestore(
2563 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002564 dasd_block_set_timer(block, HZ/2);
2565 break;
2566 }
2567 DBF_DEV_EVENT(DBF_ERR, basedev,
2568 "CCW creation failed (rc=%ld) "
2569 "on request %p",
2570 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002571 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002572 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002573 continue;
2574 }
2575 /*
2576 * Note: callback is set to dasd_return_cqr_cb in
2577 * __dasd_block_start_head to cover erp requests as well
2578 */
2579 cqr->callback_data = (void *) req;
2580 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002581 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002582 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002583 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002584 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002585 dasd_profile_start(block, cqr, req);
2586 }
2587}
2588
2589static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2590{
2591 struct request *req;
2592 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002593 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002594
2595 req = (struct request *) cqr->callback_data;
2596 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002597 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002598 if (status < 0)
2599 error = status;
2600 else if (status == 0) {
2601 if (cqr->intrc == -EPERM)
2602 error = -EBADE;
2603 else if (cqr->intrc == -ENOLINK ||
2604 cqr->intrc == -ETIMEDOUT)
2605 error = cqr->intrc;
2606 else
2607 error = -EIO;
2608 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002609 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002610}
2611
2612/*
2613 * Process ccw request queue.
2614 */
2615static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2616 struct list_head *final_queue)
2617{
2618 struct list_head *l, *n;
2619 struct dasd_ccw_req *cqr;
2620 dasd_erp_fn_t erp_fn;
2621 unsigned long flags;
2622 struct dasd_device *base = block->base;
2623
2624restart:
2625 /* Process request with final status. */
2626 list_for_each_safe(l, n, &block->ccw_queue) {
2627 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2628 if (cqr->status != DASD_CQR_DONE &&
2629 cqr->status != DASD_CQR_FAILED &&
2630 cqr->status != DASD_CQR_NEED_ERP &&
2631 cqr->status != DASD_CQR_TERMINATED)
2632 continue;
2633
2634 if (cqr->status == DASD_CQR_TERMINATED) {
2635 base->discipline->handle_terminated_request(cqr);
2636 goto restart;
2637 }
2638
2639 /* Process requests that may be recovered */
2640 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002641 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002642 if (IS_ERR(erp_fn(cqr)))
2643 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002644 goto restart;
2645 }
2646
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002647 /* log sense for fatal error */
2648 if (cqr->status == DASD_CQR_FAILED) {
2649 dasd_log_sense(cqr, &cqr->irb);
2650 }
2651
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002652 /* First of all call extended error reporting. */
2653 if (dasd_eer_enabled(base) &&
2654 cqr->status == DASD_CQR_FAILED) {
2655 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2656
2657 /* restart request */
2658 cqr->status = DASD_CQR_FILLED;
2659 cqr->retries = 255;
2660 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002661 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002662 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2663 flags);
2664 goto restart;
2665 }
2666
2667 /* Process finished ERP request. */
2668 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002669 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002670 goto restart;
2671 }
2672
2673 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002674 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002675 list_move_tail(&cqr->blocklist, final_queue);
2676 }
2677}
2678
2679static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2680{
2681 dasd_schedule_block_bh(cqr->block);
2682}
2683
2684static void __dasd_block_start_head(struct dasd_block *block)
2685{
2686 struct dasd_ccw_req *cqr;
2687
2688 if (list_empty(&block->ccw_queue))
2689 return;
2690 /* We allways begin with the first requests on the queue, as some
2691 * of previously started requests have to be enqueued on a
2692 * dasd_device again for error recovery.
2693 */
2694 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2695 if (cqr->status != DASD_CQR_FILLED)
2696 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002697 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2698 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2699 cqr->status = DASD_CQR_FAILED;
2700 cqr->intrc = -EPERM;
2701 dasd_schedule_block_bh(block);
2702 continue;
2703 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002704 /* Non-temporary stop condition will trigger fail fast */
2705 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2706 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2707 (!dasd_eer_enabled(block->base))) {
2708 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002709 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002710 dasd_schedule_block_bh(block);
2711 continue;
2712 }
2713 /* Don't try to start requests if device is stopped */
2714 if (block->base->stopped)
2715 return;
2716
2717 /* just a fail safe check, should not happen */
2718 if (!cqr->startdev)
2719 cqr->startdev = block->base;
2720
2721 /* make sure that the requests we submit find their way back */
2722 cqr->callback = dasd_return_cqr_cb;
2723
2724 dasd_add_request_tail(cqr);
2725 }
2726}
2727
2728/*
2729 * Central dasd_block layer routine. Takes requests from the generic
2730 * block layer request queue, creates ccw requests, enqueues them on
2731 * a dasd_device and processes ccw requests that have been returned.
2732 */
2733static void dasd_block_tasklet(struct dasd_block *block)
2734{
2735 struct list_head final_queue;
2736 struct list_head *l, *n;
2737 struct dasd_ccw_req *cqr;
2738
2739 atomic_set(&block->tasklet_scheduled, 0);
2740 INIT_LIST_HEAD(&final_queue);
2741 spin_lock(&block->queue_lock);
2742 /* Finish off requests on ccw queue */
2743 __dasd_process_block_ccw_queue(block, &final_queue);
2744 spin_unlock(&block->queue_lock);
2745 /* Now call the callback function of requests with final status */
2746 spin_lock_irq(&block->request_queue_lock);
2747 list_for_each_safe(l, n, &final_queue) {
2748 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2749 list_del_init(&cqr->blocklist);
2750 __dasd_cleanup_cqr(cqr);
2751 }
2752 spin_lock(&block->queue_lock);
2753 /* Get new request from the block device request queue */
2754 __dasd_process_request_queue(block);
2755 /* Now check if the head of the ccw queue needs to be started. */
2756 __dasd_block_start_head(block);
2757 spin_unlock(&block->queue_lock);
2758 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002759 if (waitqueue_active(&shutdown_waitq))
2760 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002761 dasd_put_device(block->base);
2762}
2763
2764static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2765{
2766 wake_up(&dasd_flush_wq);
2767}
2768
2769/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002770 * Requeue a request back to the block request queue
2771 * only works for block requests
2772 */
2773static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2774{
2775 struct dasd_block *block = cqr->block;
2776 struct request *req;
2777 unsigned long flags;
2778
2779 if (!block)
2780 return -EINVAL;
2781 spin_lock_irqsave(&block->queue_lock, flags);
2782 req = (struct request *) cqr->callback_data;
2783 blk_requeue_request(block->request_queue, req);
2784 spin_unlock_irqrestore(&block->queue_lock, flags);
2785
2786 return 0;
2787}
2788
2789/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002790 * Go through all request on the dasd_block request queue, cancel them
2791 * on the respective dasd_device, and return them to the generic
2792 * block layer.
2793 */
2794static int dasd_flush_block_queue(struct dasd_block *block)
2795{
2796 struct dasd_ccw_req *cqr, *n;
2797 int rc, i;
2798 struct list_head flush_queue;
2799
2800 INIT_LIST_HEAD(&flush_queue);
2801 spin_lock_bh(&block->queue_lock);
2802 rc = 0;
2803restart:
2804 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2805 /* if this request currently owned by a dasd_device cancel it */
2806 if (cqr->status >= DASD_CQR_QUEUED)
2807 rc = dasd_cancel_req(cqr);
2808 if (rc < 0)
2809 break;
2810 /* Rechain request (including erp chain) so it won't be
2811 * touched by the dasd_block_tasklet anymore.
2812 * Replace the callback so we notice when the request
2813 * is returned from the dasd_device layer.
2814 */
2815 cqr->callback = _dasd_wake_block_flush_cb;
2816 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2817 list_move_tail(&cqr->blocklist, &flush_queue);
2818 if (i > 1)
2819 /* moved more than one request - need to restart */
2820 goto restart;
2821 }
2822 spin_unlock_bh(&block->queue_lock);
2823 /* Now call the callback function of flushed requests */
2824restart_cb:
2825 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2826 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2827 /* Process finished ERP request. */
2828 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002829 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002830 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002831 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002832 /* restart list_for_xx loop since dasd_process_erp
2833 * might remove multiple elements */
2834 goto restart_cb;
2835 }
2836 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002837 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002838 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002839 list_del_init(&cqr->blocklist);
2840 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002841 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return rc;
2844}
2845
2846/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002847 * Schedules a call to dasd_tasklet over the device tasklet.
2848 */
2849void dasd_schedule_block_bh(struct dasd_block *block)
2850{
2851 /* Protect against rescheduling. */
2852 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2853 return;
2854 /* life cycle of block is bound to it's base device */
2855 dasd_get_device(block->base);
2856 tasklet_hi_schedule(&block->tasklet);
2857}
2858
2859
2860/*
2861 * SECTION: external block device operations
2862 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 */
2864
2865/*
2866 * Dasd request queue function. Called from ll_rw_blk.c
2867 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002868static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002870 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002872 block = queue->queuedata;
2873 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002875 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002877 __dasd_block_start_head(block);
2878 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002882 * Block timeout callback, called from the block layer
2883 *
2884 * request_queue lock is held on entry.
2885 *
2886 * Return values:
2887 * BLK_EH_RESET_TIMER if the request should be left running
2888 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2889 * by the driver.
2890 */
2891enum blk_eh_timer_return dasd_times_out(struct request *req)
2892{
2893 struct dasd_ccw_req *cqr = req->completion_data;
2894 struct dasd_block *block = req->q->queuedata;
2895 struct dasd_device *device;
2896 int rc = 0;
2897
2898 if (!cqr)
2899 return BLK_EH_NOT_HANDLED;
2900
2901 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002902 if (!device->blk_timeout)
2903 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002904 DBF_DEV_EVENT(DBF_WARNING, device,
2905 " dasd_times_out cqr %p status %x",
2906 cqr, cqr->status);
2907
2908 spin_lock(&block->queue_lock);
2909 spin_lock(get_ccwdev_lock(device->cdev));
2910 cqr->retries = -1;
2911 cqr->intrc = -ETIMEDOUT;
2912 if (cqr->status >= DASD_CQR_QUEUED) {
2913 spin_unlock(get_ccwdev_lock(device->cdev));
2914 rc = dasd_cancel_req(cqr);
2915 } else if (cqr->status == DASD_CQR_FILLED ||
2916 cqr->status == DASD_CQR_NEED_ERP) {
2917 cqr->status = DASD_CQR_TERMINATED;
2918 spin_unlock(get_ccwdev_lock(device->cdev));
2919 } else if (cqr->status == DASD_CQR_IN_ERP) {
2920 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2921
2922 list_for_each_entry_safe(searchcqr, nextcqr,
2923 &block->ccw_queue, blocklist) {
2924 tmpcqr = searchcqr;
2925 while (tmpcqr->refers)
2926 tmpcqr = tmpcqr->refers;
2927 if (tmpcqr != cqr)
2928 continue;
2929 /* searchcqr is an ERP request for cqr */
2930 searchcqr->retries = -1;
2931 searchcqr->intrc = -ETIMEDOUT;
2932 if (searchcqr->status >= DASD_CQR_QUEUED) {
2933 spin_unlock(get_ccwdev_lock(device->cdev));
2934 rc = dasd_cancel_req(searchcqr);
2935 spin_lock(get_ccwdev_lock(device->cdev));
2936 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2937 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2938 searchcqr->status = DASD_CQR_TERMINATED;
2939 rc = 0;
2940 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2941 /*
2942 * Shouldn't happen; most recent ERP
2943 * request is at the front of queue
2944 */
2945 continue;
2946 }
2947 break;
2948 }
2949 spin_unlock(get_ccwdev_lock(device->cdev));
2950 }
2951 dasd_schedule_block_bh(block);
2952 spin_unlock(&block->queue_lock);
2953
2954 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2955}
2956
2957/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 * Allocate and initialize request queue and default I/O scheduler.
2959 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002960static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 int rc;
2963
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002964 block->request_queue = blk_init_queue(do_dasd_request,
2965 &block->request_queue_lock);
2966 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return -ENOMEM;
2968
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002969 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002971 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002972 block->request_queue->elevator = NULL;
Heiko Carstensef089942013-10-31 13:24:28 +01002973 mutex_lock(&block->request_queue->sysfs_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002974 rc = elevator_init(block->request_queue, "deadline");
Heiko Carstensef089942013-10-31 13:24:28 +01002975 if (rc)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002976 blk_cleanup_queue(block->request_queue);
Heiko Carstensef089942013-10-31 13:24:28 +01002977 mutex_unlock(&block->request_queue->sysfs_lock);
2978 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
2981/*
2982 * Allocate and initialize request queue.
2983 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002984static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 int max;
2987
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002988 if (block->base->features & DASD_FEATURE_USERAW) {
2989 /*
2990 * the max_blocks value for raw_track access is 256
2991 * it is higher than the native ECKD value because we
2992 * only need one ccw per track
2993 * so the max_hw_sectors are
2994 * 2048 x 512B = 1024kB = 16 tracks
2995 */
2996 max = 2048;
2997 } else {
2998 max = block->base->discipline->max_blocks << block->s2b_shift;
2999 }
3000 blk_queue_logical_block_size(block->request_queue,
3001 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003002 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003003 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003004 /* with page sized segments we can translate each segement into
3005 * one idaw/tidaw
3006 */
3007 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3008 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
3011/*
3012 * Deactivate and free request queue.
3013 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003014static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003016 if (block->request_queue) {
3017 blk_cleanup_queue(block->request_queue);
3018 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020}
3021
3022/*
3023 * Flush request on the request queue.
3024 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003025static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 struct request *req;
3028
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003029 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003031
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003032 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003033 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003034 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003035 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Al Viro57a7c0b2008-03-02 10:36:08 -05003038static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003040 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 int rc;
3042
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003043 base = dasd_device_from_gendisk(bdev->bd_disk);
3044 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003045 return -ENODEV;
3046
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003047 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003048 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 rc = -ENODEV;
3050 goto unlock;
3051 }
3052
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003053 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 rc = -EINVAL;
3055 goto unlock;
3056 }
3057
3058 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003059 dev_info(&base->cdev->dev,
3060 "Accessing the DASD failed because it is in "
3061 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 rc = -EPERM;
3063 goto out;
3064 }
3065
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003066 if (base->state <= DASD_STATE_BASIC) {
3067 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 " Cannot open unrecognized device");
3069 rc = -ENODEV;
3070 goto out;
3071 }
3072
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003073 if ((mode & FMODE_WRITE) &&
3074 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3075 (base->features & DASD_FEATURE_READONLY))) {
3076 rc = -EROFS;
3077 goto out;
3078 }
3079
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003080 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082
3083out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003084 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003086 atomic_dec(&base->block->open_count);
3087 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return rc;
3089}
3090
Al Virodb2a1442013-05-05 21:52:57 -04003091static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Al Virodb2a1442013-05-05 21:52:57 -04003093 struct dasd_device *base = dasd_device_from_gendisk(disk);
3094 if (base) {
3095 atomic_dec(&base->block->open_count);
3096 module_put(base->discipline->owner);
3097 dasd_put_device(base);
3098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003101/*
3102 * Return disk geometry.
3103 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003104static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003105{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003106 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003107
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003108 base = dasd_device_from_gendisk(bdev->bd_disk);
3109 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003110 return -ENODEV;
3111
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003112 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003113 !base->discipline->fill_geometry) {
3114 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003115 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003116 }
3117 base->discipline->fill_geometry(base->block, geo);
3118 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3119 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003120 return 0;
3121}
3122
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003123const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124dasd_device_operations = {
3125 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003126 .open = dasd_open,
3127 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003128 .ioctl = dasd_ioctl,
3129 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003130 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131};
3132
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003133/*******************************************************************************
3134 * end of block device operations
3135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137static void
3138dasd_exit(void)
3139{
3140#ifdef CONFIG_PROC_FS
3141 dasd_proc_exit();
3142#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003143 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003144 if (dasd_page_cache != NULL) {
3145 kmem_cache_destroy(dasd_page_cache);
3146 dasd_page_cache = NULL;
3147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 dasd_gendisk_exit();
3149 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (dasd_debug_area != NULL) {
3151 debug_unregister(dasd_debug_area);
3152 dasd_debug_area = NULL;
3153 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003154 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
3157/*
3158 * SECTION: common functions for ccw_driver use
3159 */
3160
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003161/*
3162 * Is the device read-only?
3163 * Note that this function does not report the setting of the
3164 * readonly device attribute, but how it is configured in z/VM.
3165 */
3166int dasd_device_is_ro(struct dasd_device *device)
3167{
3168 struct ccw_dev_id dev_id;
3169 struct diag210 diag_data;
3170 int rc;
3171
3172 if (!MACHINE_IS_VM)
3173 return 0;
3174 ccw_device_get_id(device->cdev, &dev_id);
3175 memset(&diag_data, 0, sizeof(diag_data));
3176 diag_data.vrdcdvno = dev_id.devno;
3177 diag_data.vrdclen = sizeof(diag_data);
3178 rc = diag210(&diag_data);
3179 if (rc == 0 || rc == 2) {
3180 return diag_data.vrdcvfla & 0x80;
3181 } else {
3182 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3183 dev_id.devno, rc);
3184 return 0;
3185 }
3186}
3187EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3188
Cornelia Huckf3445a12009-04-14 15:36:23 +02003189static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3190{
3191 struct ccw_device *cdev = data;
3192 int ret;
3193
3194 ret = ccw_device_set_online(cdev);
3195 if (ret)
3196 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3197 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003198}
3199
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003200/*
3201 * Initial attempt at a probe function. this can be simplified once
3202 * the other detection code is gone.
3203 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003204int dasd_generic_probe(struct ccw_device *cdev,
3205 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 int ret;
3208
3209 ret = dasd_add_sysfs_files(cdev);
3210 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003211 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3212 "dasd_generic_probe: could not add "
3213 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
Horst Hummel40545572006-06-29 15:08:18 +02003216 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Horst Hummel40545572006-06-29 15:08:18 +02003218 /*
3219 * Automatically online either all dasd devices (dasd_autodetect)
3220 * or all devices specified with dasd= parameters during
3221 * initial probe.
3222 */
3223 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003224 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003225 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003229/*
3230 * This will one day be called from a global not_oper handler.
3231 * It is also used by driver_unregister during module unload.
3232 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003233void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003236 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Horst Hummel59afda72005-05-16 21:53:39 -07003238 cdev->handler = NULL;
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003241 if (IS_ERR(device)) {
3242 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003244 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003245 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3246 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 /* Already doing offline processing */
3248 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003249 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return;
3251 }
3252 /*
3253 * This device is removed unconditionally. Set offline
3254 * flag to prevent dasd_open from opening it while it is
3255 * no quite down yet.
3256 */
3257 dasd_set_target_state(device, DASD_STATE_NEW);
3258 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003259 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003261 /*
3262 * life cycle of block is bound to device, so delete it after
3263 * device was safely removed
3264 */
3265 if (block)
3266 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003267
3268 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003271/*
3272 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003274 * or the user has started activation through sysfs.
3275 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003276int dasd_generic_set_online(struct ccw_device *cdev,
3277 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003279 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003281 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003282
Horst Hummel40545572006-06-29 15:08:18 +02003283 /* first online clears initial online feature flag */
3284 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 device = dasd_create_device(cdev);
3286 if (IS_ERR(device))
3287 return PTR_ERR(device);
3288
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003289 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003290 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003292 pr_warning("%s Setting the DASD online failed because "
3293 "of missing DIAG discipline\n",
3294 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 dasd_delete_device(device);
3296 return -ENODEV;
3297 }
3298 discipline = dasd_diag_discipline_pointer;
3299 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003300 if (!try_module_get(base_discipline->owner)) {
3301 dasd_delete_device(device);
3302 return -EINVAL;
3303 }
3304 if (!try_module_get(discipline->owner)) {
3305 module_put(base_discipline->owner);
3306 dasd_delete_device(device);
3307 return -EINVAL;
3308 }
3309 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 device->discipline = discipline;
3311
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003312 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 rc = discipline->check_device(device);
3314 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003315 pr_warning("%s Setting the DASD online with discipline %s "
3316 "failed with rc=%i\n",
3317 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003318 module_put(discipline->owner);
3319 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 dasd_delete_device(device);
3321 return rc;
3322 }
3323
3324 dasd_set_target_state(device, DASD_STATE_ONLINE);
3325 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003326 pr_warning("%s Setting the DASD online failed because of a "
3327 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 rc = -ENODEV;
3329 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003330 if (device->block)
3331 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 dasd_delete_device(device);
3333 } else
3334 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003335 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003336
3337 wait_event(dasd_init_waitq, _wait_for_device(device));
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return rc;
3341}
3342
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003343int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003346 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003347 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003349 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 device = dasd_device_from_cdev(cdev);
3351 if (IS_ERR(device))
3352 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 /*
3355 * We must make sure that this device is currently not in use.
3356 * The open_count is increased for every opener, that includes
3357 * the blkdev_get in dasd_scan_partitions. We are only interested
3358 * in the other openers.
3359 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003360 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003361 max_count = device->block->bdev ? 0 : -1;
3362 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003363 if (open_count > max_count) {
3364 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003365 pr_warning("%s: The DASD cannot be set offline "
3366 "with open count %i\n",
3367 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003368 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003369 pr_warning("%s: The DASD cannot be set offline "
3370 "while it is in use\n",
3371 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003372 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3373 dasd_put_device(device);
3374 return -EBUSY;
3375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003377
3378 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3379 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003380 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003381 * could only be called by normal offline so safe_offline flag
3382 * needs to be removed to run normal offline and kill all I/O
3383 */
3384 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3385 /* Already doing normal offline processing */
3386 dasd_put_device(device);
3387 return -EBUSY;
3388 } else
3389 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3390
3391 } else
3392 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3393 /* Already doing offline processing */
3394 dasd_put_device(device);
3395 return -EBUSY;
3396 }
3397
3398 /*
3399 * if safe_offline called set safe_offline_running flag and
3400 * clear safe_offline so that a call to normal offline
3401 * can overrun safe_offline processing
3402 */
3403 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3404 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3405 /*
3406 * If we want to set the device safe offline all IO operations
3407 * should be finished before continuing the offline process
3408 * so sync bdev first and then wait for our queues to become
3409 * empty
3410 */
3411 /* sync blockdev and partitions */
3412 rc = fsync_bdev(device->block->bdev);
3413 if (rc != 0)
3414 goto interrupted;
3415
3416 /* schedule device tasklet and wait for completion */
3417 dasd_schedule_device_bh(device);
3418 rc = wait_event_interruptible(shutdown_waitq,
3419 _wait_for_empty_queues(device));
3420 if (rc != 0)
3421 goto interrupted;
3422 }
3423
3424 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 dasd_set_target_state(device, DASD_STATE_NEW);
3426 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003427 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003429 /*
3430 * life cycle of block is bound to device, so delete it after
3431 * device was safely removed
3432 */
3433 if (block)
3434 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003436
3437interrupted:
3438 /* interrupted by signal */
3439 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3440 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3441 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3442 dasd_put_device(device);
3443 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003446int dasd_generic_last_path_gone(struct dasd_device *device)
3447{
3448 struct dasd_ccw_req *cqr;
3449
3450 dev_warn(&device->cdev->dev, "No operational channel path is left "
3451 "for the device\n");
3452 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3453 /* First of all call extended error reporting. */
3454 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3455
3456 if (device->state < DASD_STATE_BASIC)
3457 return 0;
3458 /* Device is active. We want to keep it. */
3459 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3460 if ((cqr->status == DASD_CQR_IN_IO) ||
3461 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3462 cqr->status = DASD_CQR_QUEUED;
3463 cqr->retries++;
3464 }
3465 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3466 dasd_device_clear_timer(device);
3467 dasd_schedule_device_bh(device);
3468 return 1;
3469}
3470EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3471
3472int dasd_generic_path_operational(struct dasd_device *device)
3473{
3474 dev_info(&device->cdev->dev, "A channel path to the device has become "
3475 "operational\n");
3476 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3477 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3478 if (device->stopped & DASD_UNRESUMED_PM) {
3479 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3480 dasd_restore_device(device);
3481 return 1;
3482 }
3483 dasd_schedule_device_bh(device);
3484 if (device->block)
3485 dasd_schedule_block_bh(device->block);
3486 return 1;
3487}
3488EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3489
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003490int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 int ret;
3494
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003495 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (IS_ERR(device))
3497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 ret = 0;
3499 switch (event) {
3500 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003501 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003503 device->path_data.opm = 0;
3504 device->path_data.ppm = 0;
3505 device->path_data.npm = 0;
3506 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 break;
3508 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003510 if (device->path_data.opm)
3511 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 break;
3513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 dasd_put_device(device);
3515 return ret;
3516}
3517
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003518void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3519{
3520 int chp;
3521 __u8 oldopm, eventlpm;
3522 struct dasd_device *device;
3523
3524 device = dasd_device_from_cdev_locked(cdev);
3525 if (IS_ERR(device))
3526 return;
3527 for (chp = 0; chp < 8; chp++) {
3528 eventlpm = 0x80 >> chp;
3529 if (path_event[chp] & PE_PATH_GONE) {
3530 oldopm = device->path_data.opm;
3531 device->path_data.opm &= ~eventlpm;
3532 device->path_data.ppm &= ~eventlpm;
3533 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003534 if (oldopm && !device->path_data.opm) {
3535 dev_warn(&device->cdev->dev,
3536 "No verified channel paths remain "
3537 "for the device\n");
3538 DBF_DEV_EVENT(DBF_WARNING, device,
3539 "%s", "last verified path gone");
3540 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3541 dasd_device_set_stop_bits(device,
3542 DASD_STOPPED_DC_WAIT);
3543 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003544 }
3545 if (path_event[chp] & PE_PATH_AVAILABLE) {
3546 device->path_data.opm &= ~eventlpm;
3547 device->path_data.ppm &= ~eventlpm;
3548 device->path_data.npm &= ~eventlpm;
3549 device->path_data.tbvpm |= eventlpm;
3550 dasd_schedule_device_bh(device);
3551 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003552 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003553 if (!(device->path_data.opm & eventlpm) &&
3554 !(device->path_data.tbvpm & eventlpm)) {
3555 /*
3556 * we can not establish a pathgroup on an
3557 * unavailable path, so trigger a path
3558 * verification first
3559 */
3560 device->path_data.tbvpm |= eventlpm;
3561 dasd_schedule_device_bh(device);
3562 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003563 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3564 "Pathgroup re-established\n");
3565 if (device->discipline->kick_validate)
3566 device->discipline->kick_validate(device);
3567 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003568 }
3569 dasd_put_device(device);
3570}
3571EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3572
3573int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3574{
3575 if (!device->path_data.opm && lpm) {
3576 device->path_data.opm = lpm;
3577 dasd_generic_path_operational(device);
3578 } else
3579 device->path_data.opm |= lpm;
3580 return 0;
3581}
3582EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3583
3584
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003585int dasd_generic_pm_freeze(struct ccw_device *cdev)
3586{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003587 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003588 struct list_head freeze_queue;
3589 struct dasd_ccw_req *cqr, *n;
3590 struct dasd_ccw_req *refers;
3591 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003592
3593 if (IS_ERR(device))
3594 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003595
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003596 /* mark device as suspended */
3597 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3598
Stefan Haberland6f272b92011-01-05 12:48:05 +01003599 if (device->discipline->freeze)
3600 rc = device->discipline->freeze(device);
3601
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003602 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003603 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003604
3605 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003606 INIT_LIST_HEAD(&freeze_queue);
3607 spin_lock_irq(get_ccwdev_lock(cdev));
3608 rc = 0;
3609 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3610 /* Check status and move request to flush_queue */
3611 if (cqr->status == DASD_CQR_IN_IO) {
3612 rc = device->discipline->term_IO(cqr);
3613 if (rc) {
3614 /* unable to terminate requeust */
3615 dev_err(&device->cdev->dev,
3616 "Unable to terminate request %p "
3617 "on suspend\n", cqr);
3618 spin_unlock_irq(get_ccwdev_lock(cdev));
3619 dasd_put_device(device);
3620 return rc;
3621 }
3622 }
3623 list_move_tail(&cqr->devlist, &freeze_queue);
3624 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003625 spin_unlock_irq(get_ccwdev_lock(cdev));
3626
3627 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3628 wait_event(dasd_flush_wq,
3629 (cqr->status != DASD_CQR_CLEAR_PENDING));
3630 if (cqr->status == DASD_CQR_CLEARED)
3631 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003632
Stefan Haberlandc5576872013-04-15 16:41:31 +02003633 /* requeue requests to blocklayer will only work for
3634 block device requests */
3635 if (_dasd_requeue_request(cqr))
3636 continue;
3637
3638 /* remove requests from device and block queue */
3639 list_del_init(&cqr->devlist);
3640 while (cqr->refers != NULL) {
3641 refers = cqr->refers;
3642 /* remove the request from the block queue */
3643 list_del(&cqr->blocklist);
3644 /* free the finished erp request */
3645 dasd_free_erp_request(cqr, cqr->memdev);
3646 cqr = refers;
3647 }
3648 if (cqr->block)
3649 list_del_init(&cqr->blocklist);
3650 cqr->block->base->discipline->free_cp(
3651 cqr, (struct request *) cqr->callback_data);
3652 }
3653
3654 /*
3655 * if requests remain then they are internal request
3656 * and go back to the device queue
3657 */
3658 if (!list_empty(&freeze_queue)) {
3659 /* move freeze_queue to start of the ccw_queue */
3660 spin_lock_irq(get_ccwdev_lock(cdev));
3661 list_splice_tail(&freeze_queue, &device->ccw_queue);
3662 spin_unlock_irq(get_ccwdev_lock(cdev));
3663 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003664 dasd_put_device(device);
3665 return rc;
3666}
3667EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3668
3669int dasd_generic_restore_device(struct ccw_device *cdev)
3670{
3671 struct dasd_device *device = dasd_device_from_cdev(cdev);
3672 int rc = 0;
3673
3674 if (IS_ERR(device))
3675 return PTR_ERR(device);
3676
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003677 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003678 dasd_device_remove_stop_bits(device,
3679 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003680
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003681 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003682
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003683 /*
3684 * call discipline restore function
3685 * if device is stopped do nothing e.g. for disconnected devices
3686 */
3687 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003688 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003689 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003690 /*
3691 * if the resume failed for the DASD we put it in
3692 * an UNRESUMED stop state
3693 */
3694 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003695
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003696 if (device->block)
3697 dasd_schedule_block_bh(device->block);
3698
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003699 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003700 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003701 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003702}
3703EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3704
Heiko Carstens763968e2007-05-10 15:45:46 +02003705static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3706 void *rdc_buffer,
3707 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003708 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003709{
3710 struct dasd_ccw_req *cqr;
3711 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003712 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003713
3714 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3715
3716 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003717 /* internal error 13 - Allocating the RDC request failed*/
3718 dev_err(&device->cdev->dev,
3719 "An error occurred in the DASD device driver, "
3720 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003721 return cqr;
3722 }
3723
3724 ccw = cqr->cpaddr;
3725 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003726 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3727 idaw = (unsigned long *) (cqr->data);
3728 ccw->cda = (__u32)(addr_t) idaw;
3729 ccw->flags = CCW_FLAG_IDA;
3730 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3731 } else {
3732 ccw->cda = (__u32)(addr_t) rdc_buffer;
3733 ccw->flags = 0;
3734 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003735
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003736 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003737 cqr->startdev = device;
3738 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003739 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003740 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003741 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003742 cqr->status = DASD_CQR_FILLED;
3743 return cqr;
3744}
3745
3746
Stefan Haberland68b781f2009-09-11 10:28:29 +02003747int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003748 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003749{
3750 int ret;
3751 struct dasd_ccw_req *cqr;
3752
Sebastian Ott92636b12009-06-12 10:26:37 +02003753 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003754 magic);
3755 if (IS_ERR(cqr))
3756 return PTR_ERR(cqr);
3757
3758 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003759 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003760 return ret;
3761}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003762EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003763
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003764/*
3765 * In command mode and transport mode we need to look for sense
3766 * data in different places. The sense data itself is allways
3767 * an array of 32 bytes, so we can unify the sense data access
3768 * for both modes.
3769 */
3770char *dasd_get_sense(struct irb *irb)
3771{
3772 struct tsb *tsb = NULL;
3773 char *sense = NULL;
3774
3775 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3776 if (irb->scsw.tm.tcw)
3777 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3778 irb->scsw.tm.tcw);
3779 if (tsb && tsb->length == 64 && tsb->flags)
3780 switch (tsb->flags & 0x07) {
3781 case 1: /* tsa_iostat */
3782 sense = tsb->tsa.iostat.sense;
3783 break;
3784 case 2: /* tsa_ddpc */
3785 sense = tsb->tsa.ddpc.sense;
3786 break;
3787 default:
3788 /* currently we don't use interrogate data */
3789 break;
3790 }
3791 } else if (irb->esw.esw0.erw.cons) {
3792 sense = irb->ecw;
3793 }
3794 return sense;
3795}
3796EXPORT_SYMBOL_GPL(dasd_get_sense);
3797
Stefan Haberland4679e892012-06-19 17:30:12 +02003798void dasd_generic_shutdown(struct ccw_device *cdev)
3799{
3800 struct dasd_device *device;
3801
3802 device = dasd_device_from_cdev(cdev);
3803 if (IS_ERR(device))
3804 return;
3805
3806 if (device->block)
3807 dasd_schedule_block_bh(device->block);
3808
3809 dasd_schedule_device_bh(device);
3810
3811 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3812}
3813EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3814
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003815static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 int rc;
3818
3819 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003820 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003821 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003822 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003825 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if (dasd_debug_area == NULL) {
3827 rc = -ENOMEM;
3828 goto failed;
3829 }
3830 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003831 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3834
3835 dasd_diag_discipline_pointer = NULL;
3836
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003837 dasd_statistics_createroot();
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 rc = dasd_devmap_init();
3840 if (rc)
3841 goto failed;
3842 rc = dasd_gendisk_init();
3843 if (rc)
3844 goto failed;
3845 rc = dasd_parse();
3846 if (rc)
3847 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003848 rc = dasd_eer_init();
3849 if (rc)
3850 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851#ifdef CONFIG_PROC_FS
3852 rc = dasd_proc_init();
3853 if (rc)
3854 goto failed;
3855#endif
3856
3857 return 0;
3858failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003859 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 dasd_exit();
3861 return rc;
3862}
3863
3864module_init(dasd_init);
3865module_exit(dasd_exit);
3866
3867EXPORT_SYMBOL(dasd_debug_area);
3868EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3869
3870EXPORT_SYMBOL(dasd_add_request_head);
3871EXPORT_SYMBOL(dasd_add_request_tail);
3872EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003873EXPORT_SYMBOL(dasd_device_clear_timer);
3874EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875EXPORT_SYMBOL(dasd_enable_device);
3876EXPORT_SYMBOL(dasd_int_handler);
3877EXPORT_SYMBOL(dasd_kfree_request);
3878EXPORT_SYMBOL(dasd_kick_device);
3879EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003880EXPORT_SYMBOL(dasd_schedule_device_bh);
3881EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003883EXPORT_SYMBOL(dasd_device_set_timer);
3884EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885EXPORT_SYMBOL(dasd_sfree_request);
3886EXPORT_SYMBOL(dasd_sleep_on);
3887EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3888EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3889EXPORT_SYMBOL(dasd_smalloc_request);
3890EXPORT_SYMBOL(dasd_start_IO);
3891EXPORT_SYMBOL(dasd_term_IO);
3892
3893EXPORT_SYMBOL_GPL(dasd_generic_probe);
3894EXPORT_SYMBOL_GPL(dasd_generic_remove);
3895EXPORT_SYMBOL_GPL(dasd_generic_notify);
3896EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3897EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003898EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3899EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3900EXPORT_SYMBOL_GPL(dasd_alloc_block);
3901EXPORT_SYMBOL_GPL(dasd_free_block);