blob: 1aec8ff0b58743214ff2ad520dea009d0a1f3f96 [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
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +020041#define DASD_DIAG_MOD "dasd_diag_mod"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * SECTION: exported variables of dasd.c
45 */
46debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020047EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020048static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020050EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010051void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
54MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020055 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_LICENSE("GPL");
58
59/*
60 * SECTION: prototypes for static functions of dasd.c
61 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010062static int dasd_alloc_queue(struct dasd_block *);
63static void dasd_setup_queue(struct dasd_block *);
64static void dasd_free_queue(struct dasd_block *);
65static void dasd_flush_request_queue(struct dasd_block *);
66static int dasd_flush_block_queue(struct dasd_block *);
67static void dasd_device_tasklet(struct dasd_device *);
68static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000069static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020070static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020071static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010072static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010073static void dasd_device_timeout(unsigned long);
74static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010075static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020076static void dasd_profile_init(struct dasd_profile *, struct dentry *);
77static void dasd_profile_exit(struct dasd_profile *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * SECTION: Operations on the device structure.
81 */
82static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020083static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020084static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020085static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Allocate memory for a new device structure.
89 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010090struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct dasd_device *device;
93
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010094 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
95 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Get two pages for normal block device operations. */
99 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100100 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 kfree(device);
102 return ERR_PTR(-ENOMEM);
103 }
104 /* Get one page for error recovery. */
105 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100106 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 free_pages((unsigned long) device->ccw_mem, 1);
108 kfree(device);
109 return ERR_PTR(-ENOMEM);
110 }
111
112 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
113 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
114 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100115 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200116 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100117 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 (unsigned long) device);
119 INIT_LIST_HEAD(&device->ccw_queue);
120 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100121 device->timer.function = dasd_device_timeout;
122 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000123 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200124 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200125 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 device->state = DASD_STATE_NEW;
127 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100128 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200129 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return device;
131}
132
133/*
134 * Free memory of a device structure.
135 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100136void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800138 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 free_page((unsigned long) device->erp_mem);
140 free_pages((unsigned long) device->ccw_mem, 1);
141 kfree(device);
142}
143
144/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100145 * Allocate memory for a new device structure.
146 */
147struct dasd_block *dasd_alloc_block(void)
148{
149 struct dasd_block *block;
150
151 block = kzalloc(sizeof(*block), GFP_ATOMIC);
152 if (!block)
153 return ERR_PTR(-ENOMEM);
154 /* open_count = 0 means device online but not in use */
155 atomic_set(&block->open_count, -1);
156
157 spin_lock_init(&block->request_queue_lock);
158 atomic_set(&block->tasklet_scheduled, 0);
159 tasklet_init(&block->tasklet,
160 (void (*)(unsigned long)) dasd_block_tasklet,
161 (unsigned long) block);
162 INIT_LIST_HEAD(&block->ccw_queue);
163 spin_lock_init(&block->queue_lock);
164 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100165 block->timer.function = dasd_block_timeout;
166 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200167 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100168
169 return block;
170}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200171EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100172
173/*
174 * Free memory of a device structure.
175 */
176void dasd_free_block(struct dasd_block *block)
177{
178 kfree(block);
179}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200180EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100181
182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Make a new device known to the system.
184 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100185static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int rc;
188
189 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200190 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * keep the reference count > 0.
192 */
193 dasd_get_device(device);
194
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195 if (device->block) {
196 rc = dasd_alloc_queue(device->block);
197 if (rc) {
198 dasd_put_device(device);
199 return rc;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 device->state = DASD_STATE_KNOWN;
203 return 0;
204}
205
206/*
207 * Let the system forget about a device.
208 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100209static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800211 /* Disable extended error reporting for this device. */
212 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100214 if (device->discipline) {
215 if (device->discipline->uncheck_device)
216 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800217 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800220 if (device->base_discipline)
221 module_put(device->base_discipline->owner);
222 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 device->state = DASD_STATE_NEW;
224
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100225 if (device->block)
226 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Give up reference we took in dasd_state_new_to_known. */
229 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200233static struct dentry *dasd_debugfs_setup(const char *name,
234 struct dentry *base_dentry)
235{
236 struct dentry *pde;
237
238 if (!base_dentry)
239 return NULL;
240 pde = debugfs_create_dir(name, base_dentry);
241 if (!pde || IS_ERR(pde))
242 return NULL;
243 return pde;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
247 * Request the irq line for the device.
248 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100249static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200251 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200252 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200255 if (block) {
256 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100257 if (rc)
258 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200259 block->debugfs_dentry =
260 dasd_debugfs_setup(block->gdp->disk_name,
261 dasd_debugfs_root_entry);
262 dasd_profile_init(&block->profile, block->debugfs_dentry);
263 if (dasd_global_profile_level == DASD_PROFILE_ON)
264 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100265 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200266 device->debugfs_dentry =
267 dasd_debugfs_setup(dev_name(&device->cdev->dev),
268 dasd_debugfs_root_entry);
269 dasd_profile_init(&device->profile, device->debugfs_dentry);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100272 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100273 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200275 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
277
278 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200279
280 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/*
284 * Release the irq line for the device. Terminate any running i/o.
285 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100286static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Horst Hummel8f617012006-08-30 14:33:33 +0200288 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200289
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200290 if (device->discipline->basic_to_known) {
291 rc = device->discipline->basic_to_known(device);
292 if (rc)
293 return rc;
294 }
295
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100296 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200297 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200298 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100299 dasd_gendisk_free(device->block);
300 dasd_block_clear_timer(device->block);
301 }
302 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200303 if (rc)
304 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100305 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200306 dasd_profile_exit(&device->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200307 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
309 if (device->debug_area != NULL) {
310 debug_unregister(device->debug_area);
311 device->debug_area = NULL;
312 }
313 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317/*
318 * Do the initial analysis. The do_analysis function may return
319 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
320 * until the discipline decides to continue the startup sequence
321 * by calling the function dasd_change_state. The eckd disciplines
322 * uses this to start a ccw that detects the format. The completion
323 * interrupt for this detection ccw uses the kernel event daemon to
324 * trigger the call to dasd_change_state. All this is done in the
325 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800326 * After the analysis ccw is done (do_analysis returned 0) the block
327 * device is setup.
328 * In case the analysis returns an error, the device setup is stopped
329 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100331static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100337 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800338 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100339 if (block) {
340 if (block->base->discipline->do_analysis != NULL)
341 rc = block->base->discipline->do_analysis(block);
342 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200343 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100344 device->state = DASD_STATE_UNFMT;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200345 goto out;
346 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100347 return rc;
348 }
349 dasd_setup_queue(block);
350 set_capacity(block->gdp,
351 block->blocks << block->s2b_shift);
352 device->state = DASD_STATE_READY;
353 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200354 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100355 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200356 return rc;
357 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100358 } else {
359 device->state = DASD_STATE_READY;
360 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200361out:
362 if (device->discipline->basic_to_ready)
363 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800364 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100367static inline
368int _wait_for_empty_queues(struct dasd_device *device)
369{
370 if (device->block)
371 return list_empty(&device->ccw_queue) &&
372 list_empty(&device->block->ccw_queue);
373 else
374 return list_empty(&device->ccw_queue);
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
378 * Remove device from block device layer. Destroy dirty buffers.
379 * Forget format information. Check if the target level is basic
380 * and if it is create fake disk for formatting.
381 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100382static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Horst Hummel8f617012006-08-30 14:33:33 +0200384 int rc;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100387 if (device->block) {
388 struct dasd_block *block = device->block;
389 rc = dasd_flush_block_queue(block);
390 if (rc) {
391 device->state = DASD_STATE_READY;
392 return rc;
393 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100394 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100395 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100396 block->blocks = 0;
397 block->bp_block = 0;
398 block->s2b_shift = 0;
399 }
Horst Hummel8f617012006-08-30 14:33:33 +0200400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403/*
Horst Hummel90f00942006-03-07 21:55:39 -0800404 * Back to basic.
405 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100406static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800407{
408 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200409 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800410}
411
412/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * Make the device online and schedule the bottom half to start
414 * the requeueing of requests from the linux request queue to the
415 * ccw queue.
416 */
Horst Hummel8f617012006-08-30 14:33:33 +0200417static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418dasd_state_ready_to_online(struct dasd_device * device)
419{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100420 struct gendisk *disk;
421 struct disk_part_iter piter;
422 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100425 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100426 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100427 if ((device->features & DASD_FEATURE_USERAW)) {
428 disk = device->block->gdp;
429 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
430 return 0;
431 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100432 disk = device->block->bdev->bd_disk;
433 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
434 while ((part = disk_part_iter_next(&piter)))
435 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
436 disk_part_iter_exit(&piter);
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
441/*
442 * Stop the requeueing of requests again.
443 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100446 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100447 struct gendisk *disk;
448 struct disk_part_iter piter;
449 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100450
451 if (device->discipline->online_to_ready) {
452 rc = device->discipline->online_to_ready(device);
453 if (rc)
454 return rc;
455 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100458 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100459 disk = device->block->bdev->bd_disk;
460 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
461 while ((part = disk_part_iter_next(&piter)))
462 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
463 disk_part_iter_exit(&piter);
464 }
Horst Hummel8f617012006-08-30 14:33:33 +0200465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/*
469 * Device startup state changes.
470 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100471static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int rc;
474
475 rc = 0;
476 if (device->state == DASD_STATE_NEW &&
477 device->target >= DASD_STATE_KNOWN)
478 rc = dasd_state_new_to_known(device);
479
480 if (!rc &&
481 device->state == DASD_STATE_KNOWN &&
482 device->target >= DASD_STATE_BASIC)
483 rc = dasd_state_known_to_basic(device);
484
485 if (!rc &&
486 device->state == DASD_STATE_BASIC &&
487 device->target >= DASD_STATE_READY)
488 rc = dasd_state_basic_to_ready(device);
489
490 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700491 device->state == DASD_STATE_UNFMT &&
492 device->target > DASD_STATE_UNFMT)
493 rc = -EPERM;
494
495 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 device->state == DASD_STATE_READY &&
497 device->target >= DASD_STATE_ONLINE)
498 rc = dasd_state_ready_to_online(device);
499
500 return rc;
501}
502
503/*
504 * Device shutdown state changes.
505 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100506static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Horst Hummel8f617012006-08-30 14:33:33 +0200508 int rc;
509
510 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (device->state == DASD_STATE_ONLINE &&
512 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200513 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200514
Horst Hummel8f617012006-08-30 14:33:33 +0200515 if (!rc &&
516 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200518 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800519
Horst Hummel8f617012006-08-30 14:33:33 +0200520 if (!rc &&
521 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800522 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200523 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800524
Horst Hummel8f617012006-08-30 14:33:33 +0200525 if (!rc &&
526 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200528 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200529
Horst Hummel8f617012006-08-30 14:33:33 +0200530 if (!rc &&
531 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200533 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Horst Hummel8f617012006-08-30 14:33:33 +0200535 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*
539 * This is the main startup/shutdown routine.
540 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100541static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Sebastian Ott181d9522009-06-22 12:08:21 +0200543 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (device->state == device->target)
546 /* Already where we want to go today... */
547 return;
548 if (device->state < device->target)
549 rc = dasd_increase_state(device);
550 else
551 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200552 if (rc == -EAGAIN)
553 return;
554 if (rc)
555 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200557 /* let user-space know that the device status changed */
558 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200559
560 if (device->state == device->target)
561 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
565 * Kick starter for devices that did not complete the startup/shutdown
566 * procedure or were sleeping because of a pending state.
567 * dasd_kick_device will schedule a call do do_kick_device to the kernel
568 * event daemon.
569 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100570static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Al Viro4927b3f2006-12-06 19:18:20 +0000572 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100573 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100575 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100576 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 dasd_put_device(device);
578}
579
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100580void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 dasd_get_device(device);
583 /* queue call to dasd_kick_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200584 if (!schedule_work(&device->kick_work))
585 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200587EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200590 * dasd_reload_device will schedule a call do do_reload_device to the kernel
591 * event daemon.
592 */
593static void do_reload_device(struct work_struct *work)
594{
595 struct dasd_device *device = container_of(work, struct dasd_device,
596 reload_device);
597 device->discipline->reload(device);
598 dasd_put_device(device);
599}
600
601void dasd_reload_device(struct dasd_device *device)
602{
603 dasd_get_device(device);
604 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200605 if (!schedule_work(&device->reload_device))
606 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200607}
608EXPORT_SYMBOL(dasd_reload_device);
609
610/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200611 * dasd_restore_device will schedule a call do do_restore_device to the kernel
612 * event daemon.
613 */
614static void do_restore_device(struct work_struct *work)
615{
616 struct dasd_device *device = container_of(work, struct dasd_device,
617 restore_device);
618 device->cdev->drv->restore(device->cdev);
619 dasd_put_device(device);
620}
621
622void dasd_restore_device(struct dasd_device *device)
623{
624 dasd_get_device(device);
625 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200626 if (!schedule_work(&device->restore_device))
627 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200628}
629
630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Set the target state for a device and starts the state change.
632 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100633void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200635 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100636 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* If we are in probeonly mode stop at DASD_STATE_READY. */
638 if (dasd_probeonly && target > DASD_STATE_READY)
639 target = DASD_STATE_READY;
640 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100641 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 wake_up(&dasd_init_waitq);
643 device->target = target;
644 }
645 if (device->state != device->target)
646 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100647 mutex_unlock(&device->state_mutex);
648 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200650EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/*
653 * Enable devices with device numbers in [from..to].
654 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100655static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 return (device->state == device->target);
658}
659
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100660void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 dasd_set_target_state(device, DASD_STATE_ONLINE);
663 if (device->state <= DASD_STATE_KNOWN)
664 /* No discipline for device found. */
665 dasd_set_target_state(device, DASD_STATE_NEW);
666 /* Now wait for the devices to come up. */
667 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400668
669 dasd_reload_device(device);
670 if (device->discipline->kick_validate)
671 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200673EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675/*
676 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
677 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200678
679unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100682struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100683 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
684};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200685static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687/*
688 * Add profiling information for cqr before execution.
689 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100690static void dasd_profile_start(struct dasd_block *block,
691 struct dasd_ccw_req *cqr,
692 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct list_head *l;
695 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200696 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* count the length of the chanq for statistics */
699 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200700 if (dasd_global_profile_level || block->profile.data)
701 list_for_each(l, &block->ccw_queue)
702 if (++counter >= 31)
703 break;
704
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100705 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100706 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100707 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200708 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100709 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200710 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100711 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200712
713 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200714 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200715 block->profile.data->dasd_io_nr_req[counter]++;
716 if (rq_data_dir(req) == READ)
717 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200718 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200719 spin_unlock(&block->profile.lock);
720
721 /*
722 * We count the request for the start device, even though it may run on
723 * some other device due to error recovery. This way we make sure that
724 * we count each request only once.
725 */
726 device = cqr->startdev;
727 if (device->profile.data) {
728 counter = 1; /* request is not yet queued on the start device */
729 list_for_each(l, &device->ccw_queue)
730 if (++counter >= 31)
731 break;
732 }
733 spin_lock(&device->profile.lock);
734 if (device->profile.data) {
735 device->profile.data->dasd_io_nr_req[counter]++;
736 if (rq_data_dir(req) == READ)
737 device->profile.data->dasd_read_nr_req[counter]++;
738 }
739 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Add profiling information for cqr after execution.
744 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200745
746#define dasd_profile_counter(value, index) \
747{ \
748 for (index = 0; index < 31 && value >> (2+index); index++) \
749 ; \
750}
751
752static void dasd_profile_end_add_data(struct dasd_profile_info *data,
753 int is_alias,
754 int is_tpm,
755 int is_read,
756 long sectors,
757 int sectors_ind,
758 int tottime_ind,
759 int tottimeps_ind,
760 int strtime_ind,
761 int irqtime_ind,
762 int irqtimeps_ind,
763 int endtime_ind)
764{
765 /* in case of an overflow, reset the whole profile */
766 if (data->dasd_io_reqs == UINT_MAX) {
767 memset(data, 0, sizeof(*data));
768 getnstimeofday(&data->starttod);
769 }
770 data->dasd_io_reqs++;
771 data->dasd_io_sects += sectors;
772 if (is_alias)
773 data->dasd_io_alias++;
774 if (is_tpm)
775 data->dasd_io_tpm++;
776
777 data->dasd_io_secs[sectors_ind]++;
778 data->dasd_io_times[tottime_ind]++;
779 data->dasd_io_timps[tottimeps_ind]++;
780 data->dasd_io_time1[strtime_ind]++;
781 data->dasd_io_time2[irqtime_ind]++;
782 data->dasd_io_time2ps[irqtimeps_ind]++;
783 data->dasd_io_time3[endtime_ind]++;
784
785 if (is_read) {
786 data->dasd_read_reqs++;
787 data->dasd_read_sects += sectors;
788 if (is_alias)
789 data->dasd_read_alias++;
790 if (is_tpm)
791 data->dasd_read_tpm++;
792 data->dasd_read_secs[sectors_ind]++;
793 data->dasd_read_times[tottime_ind]++;
794 data->dasd_read_time1[strtime_ind]++;
795 data->dasd_read_time2[irqtime_ind]++;
796 data->dasd_read_time3[endtime_ind]++;
797 }
798}
799
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100800static void dasd_profile_end(struct dasd_block *block,
801 struct dasd_ccw_req *cqr,
802 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 long strtime, irqtime, endtime, tottime; /* in microseconds */
805 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200806 struct dasd_device *device;
807 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
808 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200810 device = cqr->startdev;
811 if (!(dasd_global_profile_level ||
812 block->profile.data ||
813 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815
Tejun Heo83096eb2009-05-07 22:24:39 +0900816 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!cqr->buildclk || !cqr->startclk ||
818 !cqr->stopclk || !cqr->endclk ||
819 !sectors)
820 return;
821
822 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
823 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
824 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
825 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
826 tottimeps = tottime / sectors;
827
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200828 dasd_profile_counter(sectors, sectors_ind);
829 dasd_profile_counter(tottime, tottime_ind);
830 dasd_profile_counter(tottimeps, tottimeps_ind);
831 dasd_profile_counter(strtime, strtime_ind);
832 dasd_profile_counter(irqtime, irqtime_ind);
833 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
834 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100836 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100837 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100838 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200839 cqr->startdev != block->base,
840 cqr->cpmode == 1,
841 rq_data_dir(req) == READ,
842 sectors, sectors_ind, tottime_ind,
843 tottimeps_ind, strtime_ind,
844 irqtime_ind, irqtimeps_ind,
845 endtime_ind);
846 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100847 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200849 spin_lock(&block->profile.lock);
850 if (block->profile.data)
851 dasd_profile_end_add_data(block->profile.data,
852 cqr->startdev != block->base,
853 cqr->cpmode == 1,
854 rq_data_dir(req) == READ,
855 sectors, sectors_ind, tottime_ind,
856 tottimeps_ind, strtime_ind,
857 irqtime_ind, irqtimeps_ind,
858 endtime_ind);
859 spin_unlock(&block->profile.lock);
860
861 spin_lock(&device->profile.lock);
862 if (device->profile.data)
863 dasd_profile_end_add_data(device->profile.data,
864 cqr->startdev != block->base,
865 cqr->cpmode == 1,
866 rq_data_dir(req) == READ,
867 sectors, sectors_ind, tottime_ind,
868 tottimeps_ind, strtime_ind,
869 irqtime_ind, irqtimeps_ind,
870 endtime_ind);
871 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200873
874void dasd_profile_reset(struct dasd_profile *profile)
875{
876 struct dasd_profile_info *data;
877
878 spin_lock_bh(&profile->lock);
879 data = profile->data;
880 if (!data) {
881 spin_unlock_bh(&profile->lock);
882 return;
883 }
884 memset(data, 0, sizeof(*data));
885 getnstimeofday(&data->starttod);
886 spin_unlock_bh(&profile->lock);
887}
888
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200889int dasd_profile_on(struct dasd_profile *profile)
890{
891 struct dasd_profile_info *data;
892
893 data = kzalloc(sizeof(*data), GFP_KERNEL);
894 if (!data)
895 return -ENOMEM;
896 spin_lock_bh(&profile->lock);
897 if (profile->data) {
898 spin_unlock_bh(&profile->lock);
899 kfree(data);
900 return 0;
901 }
902 getnstimeofday(&data->starttod);
903 profile->data = data;
904 spin_unlock_bh(&profile->lock);
905 return 0;
906}
907
908void dasd_profile_off(struct dasd_profile *profile)
909{
910 spin_lock_bh(&profile->lock);
911 kfree(profile->data);
912 profile->data = NULL;
913 spin_unlock_bh(&profile->lock);
914}
915
916char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
917{
918 char *buffer;
919
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200920 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200921 if (buffer == NULL)
922 return ERR_PTR(-ENOMEM);
923 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200924 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200925 return ERR_PTR(-EFAULT);
926 }
927 /* got the string, now strip linefeed. */
928 if (buffer[user_len - 1] == '\n')
929 buffer[user_len - 1] = 0;
930 else
931 buffer[user_len] = 0;
932 return buffer;
933}
934
935static ssize_t dasd_stats_write(struct file *file,
936 const char __user *user_buf,
937 size_t user_len, loff_t *pos)
938{
939 char *buffer, *str;
940 int rc;
941 struct seq_file *m = (struct seq_file *)file->private_data;
942 struct dasd_profile *prof = m->private;
943
944 if (user_len > 65536)
945 user_len = 65536;
946 buffer = dasd_get_user_string(user_buf, user_len);
947 if (IS_ERR(buffer))
948 return PTR_ERR(buffer);
949
950 str = skip_spaces(buffer);
951 rc = user_len;
952 if (strncmp(str, "reset", 5) == 0) {
953 dasd_profile_reset(prof);
954 } else if (strncmp(str, "on", 2) == 0) {
955 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100956 if (rc)
957 goto out;
958 rc = user_len;
959 if (prof == &dasd_global_profile) {
960 dasd_profile_reset(prof);
961 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
962 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200963 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100964 if (prof == &dasd_global_profile)
965 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200966 dasd_profile_off(prof);
967 } else
968 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100969out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200970 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200971 return rc;
972}
973
974static void dasd_stats_array(struct seq_file *m, unsigned int *array)
975{
976 int i;
977
978 for (i = 0; i < 32; i++)
979 seq_printf(m, "%u ", array[i]);
980 seq_putc(m, '\n');
981}
982
983static void dasd_stats_seq_print(struct seq_file *m,
984 struct dasd_profile_info *data)
985{
986 seq_printf(m, "start_time %ld.%09ld\n",
987 data->starttod.tv_sec, data->starttod.tv_nsec);
988 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
989 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
990 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
991 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200992 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200993 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200994 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200995 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200996 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200997 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200998 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200999 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001000 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001001 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001002 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001003 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001004 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001005 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001006 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001007 dasd_stats_array(m, data->dasd_io_nr_req);
1008 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1009 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1010 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1011 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001012 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001013 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001014 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001015 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001016 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001017 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001018 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001019 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001020 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001021 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001022 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001023 dasd_stats_array(m, data->dasd_read_nr_req);
1024}
1025
1026static int dasd_stats_show(struct seq_file *m, void *v)
1027{
1028 struct dasd_profile *profile;
1029 struct dasd_profile_info *data;
1030
1031 profile = m->private;
1032 spin_lock_bh(&profile->lock);
1033 data = profile->data;
1034 if (!data) {
1035 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001036 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001037 return 0;
1038 }
1039 dasd_stats_seq_print(m, data);
1040 spin_unlock_bh(&profile->lock);
1041 return 0;
1042}
1043
1044static int dasd_stats_open(struct inode *inode, struct file *file)
1045{
1046 struct dasd_profile *profile = inode->i_private;
1047 return single_open(file, dasd_stats_show, profile);
1048}
1049
1050static const struct file_operations dasd_stats_raw_fops = {
1051 .owner = THIS_MODULE,
1052 .open = dasd_stats_open,
1053 .read = seq_read,
1054 .llseek = seq_lseek,
1055 .release = single_release,
1056 .write = dasd_stats_write,
1057};
1058
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001059static void dasd_profile_init(struct dasd_profile *profile,
1060 struct dentry *base_dentry)
1061{
Al Virof4ae40a2011-07-24 04:33:43 -04001062 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001063 struct dentry *pde;
1064
1065 if (!base_dentry)
1066 return;
1067 profile->dentry = NULL;
1068 profile->data = NULL;
1069 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1070 pde = debugfs_create_file("statistics", mode, base_dentry,
1071 profile, &dasd_stats_raw_fops);
1072 if (pde && !IS_ERR(pde))
1073 profile->dentry = pde;
1074 return;
1075}
1076
1077static void dasd_profile_exit(struct dasd_profile *profile)
1078{
1079 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001080 debugfs_remove(profile->dentry);
1081 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001082}
1083
1084static void dasd_statistics_removeroot(void)
1085{
1086 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001087 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001088 debugfs_remove(dasd_debugfs_global_entry);
1089 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001090}
1091
1092static void dasd_statistics_createroot(void)
1093{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001094 struct dentry *pde;
1095
1096 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001097 pde = debugfs_create_dir("dasd", NULL);
1098 if (!pde || IS_ERR(pde))
1099 goto error;
1100 dasd_debugfs_root_entry = pde;
1101 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1102 if (!pde || IS_ERR(pde))
1103 goto error;
1104 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001105 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001106 return;
1107
1108error:
1109 DBF_EVENT(DBF_ERR, "%s",
1110 "Creation of the dasd debugfs interface failed");
1111 dasd_statistics_removeroot();
1112 return;
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001116#define dasd_profile_start(block, cqr, req) do {} while (0)
1117#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001118
1119static void dasd_statistics_createroot(void)
1120{
1121 return;
1122}
1123
1124static void dasd_statistics_removeroot(void)
1125{
1126 return;
1127}
1128
1129int dasd_stats_generic_show(struct seq_file *m, void *v)
1130{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001131 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001132 return 0;
1133}
1134
1135static void dasd_profile_init(struct dasd_profile *profile,
1136 struct dentry *base_dentry)
1137{
1138 return;
1139}
1140
1141static void dasd_profile_exit(struct dasd_profile *profile)
1142{
1143 return;
1144}
1145
1146int dasd_profile_on(struct dasd_profile *profile)
1147{
1148 return 0;
1149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151#endif /* CONFIG_DASD_PROFILE */
1152
1153/*
1154 * Allocate memory for a channel program with 'cplength' channel
1155 * command words and 'datasize' additional space. There are two
1156 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1157 * memory and 2) dasd_smalloc_request uses the static ccw memory
1158 * that gets allocated for each device.
1159 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001160struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001161 int datasize,
1162 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct dasd_ccw_req *cqr;
1165
1166 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001167 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001168 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001170 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (cqr == NULL)
1172 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 cqr->cpaddr = NULL;
1174 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001175 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 GFP_ATOMIC | GFP_DMA);
1177 if (cqr->cpaddr == NULL) {
1178 kfree(cqr);
1179 return ERR_PTR(-ENOMEM);
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 cqr->data = NULL;
1183 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001184 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001186 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 kfree(cqr);
1188 return ERR_PTR(-ENOMEM);
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001191 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1193 dasd_get_device(device);
1194 return cqr;
1195}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001196EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Stefan Haberland68b781f2009-09-11 10:28:29 +02001198struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001199 int datasize,
1200 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 unsigned long flags;
1203 struct dasd_ccw_req *cqr;
1204 char *data;
1205 int size;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1208 if (cplength > 0)
1209 size += cplength * sizeof(struct ccw1);
1210 if (datasize > 0)
1211 size += datasize;
1212 spin_lock_irqsave(&device->mem_lock, flags);
1213 cqr = (struct dasd_ccw_req *)
1214 dasd_alloc_chunk(&device->ccw_chunks, size);
1215 spin_unlock_irqrestore(&device->mem_lock, flags);
1216 if (cqr == NULL)
1217 return ERR_PTR(-ENOMEM);
1218 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1219 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1220 cqr->cpaddr = NULL;
1221 if (cplength > 0) {
1222 cqr->cpaddr = (struct ccw1 *) data;
1223 data += cplength*sizeof(struct ccw1);
1224 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1225 }
1226 cqr->data = NULL;
1227 if (datasize > 0) {
1228 cqr->data = data;
1229 memset(cqr->data, 0, datasize);
1230 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001231 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1233 dasd_get_device(device);
1234 return cqr;
1235}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001236EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238/*
1239 * Free memory of a channel program. This function needs to free all the
1240 * idal lists that might have been created by dasd_set_cda and the
1241 * struct dasd_ccw_req itself.
1242 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001243void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct ccw1 *ccw;
1246
1247 /* Clear any idals used for the request. */
1248 ccw = cqr->cpaddr;
1249 do {
1250 clear_normalized_cda(ccw);
1251 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
Jesper Juhl17fd6822005-11-07 01:01:30 -08001252 kfree(cqr->cpaddr);
1253 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 kfree(cqr);
1255 dasd_put_device(device);
1256}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001257EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001259void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 unsigned long flags;
1262
1263 spin_lock_irqsave(&device->mem_lock, flags);
1264 dasd_free_chunk(&device->ccw_chunks, cqr);
1265 spin_unlock_irqrestore(&device->mem_lock, flags);
1266 dasd_put_device(device);
1267}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001268EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270/*
1271 * Check discipline magic in cqr.
1272 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001273static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct dasd_device *device;
1276
1277 if (cqr == NULL)
1278 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001279 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001281 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 " dasd_ccw_req 0x%08x magic doesn't match"
1283 " discipline 0x%08x",
1284 cqr->magic,
1285 *(unsigned int *) device->discipline->name);
1286 return -EINVAL;
1287 }
1288 return 0;
1289}
1290
1291/*
1292 * Terminate the current i/o and set the request to clear_pending.
1293 * Timer keeps device runnig.
1294 * ccw_device_clear can fail if the i/o subsystem
1295 * is in a bad mood.
1296 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001297int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct dasd_device *device;
1300 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001301 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* Check the cqr */
1304 rc = dasd_check_cqr(cqr);
1305 if (rc)
1306 return rc;
1307 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001308 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1310 rc = ccw_device_clear(device->cdev, (long) cqr);
1311 switch (rc) {
1312 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001313 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001314 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001315 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 DBF_DEV_EVENT(DBF_DEBUG, device,
1317 "terminate cqr %p successful",
1318 cqr);
1319 break;
1320 case -ENODEV:
1321 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1322 "device gone, retry");
1323 break;
1324 case -EIO:
1325 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1326 "I/O error, retry");
1327 break;
1328 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001329 /*
1330 * device not valid so no I/O could be running
1331 * handle CQR as termination successful
1332 */
1333 cqr->status = DASD_CQR_CLEARED;
1334 cqr->stopclk = get_tod_clock();
1335 cqr->starttime = 0;
1336 /* no retries for invalid devices */
1337 cqr->retries = -1;
1338 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1339 "EINVAL, handle as terminated");
1340 /* fake rc to success */
1341 rc = 0;
1342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 case -EBUSY:
1344 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1345 "device busy, retry later");
1346 break;
1347 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001348 /* internal error 10 - unknown rc*/
1349 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1350 dev_err(&device->cdev->dev, "An error occurred in the "
1351 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 BUG();
1353 break;
1354 }
1355 retries++;
1356 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001357 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return rc;
1359}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001360EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362/*
1363 * Start the i/o. This start_IO can fail if the channel is really busy.
1364 * In that case set up a timer to start the request later.
1365 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001366int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct dasd_device *device;
1369 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001370 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* Check the cqr */
1373 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001374 if (rc) {
1375 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001377 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001378 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001379 if (((cqr->block &&
1380 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1381 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1382 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1383 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1384 "because of stolen lock", cqr);
1385 cqr->status = DASD_CQR_ERROR;
1386 cqr->intrc = -EPERM;
1387 return -EPERM;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001390 /* internal error 14 - start_IO run out of retries */
1391 sprintf(errorstring, "14 %p", cqr);
1392 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1393 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001394 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EIO;
1396 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001397 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 cqr->starttime = jiffies;
1399 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001400 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1401 cqr->lpm &= device->path_data.opm;
1402 if (!cqr->lpm)
1403 cqr->lpm = device->path_data.opm;
1404 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001405 if (cqr->cpmode == 1) {
1406 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1407 (long) cqr, cqr->lpm);
1408 } else {
1409 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1410 (long) cqr, cqr->lpm, 0);
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 switch (rc) {
1413 case 0:
1414 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001417 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 "start_IO: device busy, retry later");
1419 break;
1420 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001421 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 "start_IO: request timeout, retry later");
1423 break;
1424 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001425 /* -EACCES indicates that the request used only a subset of the
1426 * available paths and all these paths are gone. If the lpm of
1427 * this request was only a subset of the opm (e.g. the ppm) then
1428 * we just do a retry with all available paths.
1429 * If we already use the full opm, something is amiss, and we
1430 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001432 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1433 DBF_DEV_EVENT(DBF_WARNING, device,
1434 "start_IO: selected paths gone (%x)",
1435 cqr->lpm);
1436 } else if (cqr->lpm != device->path_data.opm) {
1437 cqr->lpm = device->path_data.opm;
1438 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1439 "start_IO: selected paths gone,"
1440 " retry on all paths");
1441 } else {
1442 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1443 "start_IO: all paths in opm gone,"
1444 " do path verification");
1445 dasd_generic_last_path_gone(device);
1446 device->path_data.opm = 0;
1447 device->path_data.ppm = 0;
1448 device->path_data.npm = 0;
1449 device->path_data.tbvpm =
1450 ccw_device_get_path_mask(device->cdev);
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
1453 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001454 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001455 "start_IO: -ENODEV device gone, retry");
1456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001458 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001459 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001461 case -EINVAL:
1462 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001463 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001464 "start_IO: -EINVAL device currently "
1465 "not accessible");
1466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001468 /* internal error 11 - unknown rc */
1469 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1470 dev_err(&device->cdev->dev,
1471 "An error occurred in the DASD device driver, "
1472 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 BUG();
1474 break;
1475 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001476 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return rc;
1478}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001479EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481/*
1482 * Timeout function for dasd devices. This is used for different purposes
1483 * 1) missing interrupt handler for normal operation
1484 * 2) delayed start of request where start_IO failed with -EBUSY
1485 * 3) timeout for missing state change interrupts
1486 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1487 * DASD_CQR_QUEUED for 2) and 3).
1488 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001489static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 unsigned long flags;
1492 struct dasd_device *device;
1493
1494 device = (struct dasd_device *) ptr;
1495 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1496 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001497 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001499 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502/*
1503 * Setup timeout for a device in jiffies.
1504 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001505void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001507 if (expires == 0)
1508 del_timer(&device->timer);
1509 else
1510 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001512EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514/*
1515 * Clear timeout for a device.
1516 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001517void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001519 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001521EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001523static void dasd_handle_killed_request(struct ccw_device *cdev,
1524 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 struct dasd_ccw_req *cqr;
1527 struct dasd_device *device;
1528
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001529 if (!intparm)
1530 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 cqr = (struct dasd_ccw_req *) intparm;
1532 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001533 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1534 "invalid status in handle_killed_request: "
1535 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return;
1537 }
1538
Stefan Haberland589c74d2010-02-26 22:37:47 +01001539 device = dasd_device_from_cdev_locked(cdev);
1540 if (IS_ERR(device)) {
1541 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1542 "unable to get device from cdev");
1543 return;
1544 }
1545
1546 if (!cqr->startdev ||
1547 device != cqr->startdev ||
1548 strncmp(cqr->startdev->discipline->ebcname,
1549 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001550 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1551 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001552 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return;
1554 }
1555
1556 /* Schedule request to be retried. */
1557 cqr->status = DASD_CQR_QUEUED;
1558
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001559 dasd_device_clear_timer(device);
1560 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 dasd_put_device(device);
1562}
1563
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001564void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001566 /* First of all start sense subsystem status request. */
1567 dasd_eer_snss(device);
1568
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001569 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001570 dasd_schedule_device_bh(device);
1571 if (device->block)
1572 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001574EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576/*
1577 * Interrupt handler for "normal" ssch-io based dasd devices.
1578 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001579void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1580 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct dasd_ccw_req *cqr, *next;
1583 struct dasd_device *device;
1584 unsigned long long now;
1585 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 if (IS_ERR(irb)) {
1588 switch (PTR_ERR(irb)) {
1589 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
1591 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001592 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1593 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
1595 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001596 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1597 "unknown error %ld\n", __func__,
1598 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001600 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return;
1602 }
1603
Heiko Carstens1aae0562013-01-30 09:49:40 +01001604 now = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001605 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001606 /* check for conditions that should be handled immediately */
1607 if (!cqr ||
1608 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1609 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001610 if (cqr)
1611 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001612 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001613 if (IS_ERR(device))
1614 return;
1615 /* ignore unsolicited interrupts for DIAG discipline */
1616 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001618 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001620 device->discipline->dump_sense_dbf(device, irb, "int");
1621 if (device->features & DASD_FEATURE_ERPLOG)
1622 device->discipline->dump_sense(device, cqr, irb);
1623 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001624 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001626
1627 /* check for for attention message */
1628 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1629 device = dasd_device_from_cdev_locked(cdev);
1630 device->discipline->check_attention(device, irb->esw.esw1.lpum);
1631 dasd_put_device(device);
1632 }
1633
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001634 if (!cqr)
1635 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001637 device = (struct dasd_device *) cqr->startdev;
1638 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001640 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1641 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return;
1643 }
1644
1645 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001646 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001647 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001648 cqr->status = DASD_CQR_CLEARED;
1649 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001650 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001651 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return;
1653 }
1654
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001655 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001657 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1658 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return;
1660 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001661
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001662 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001664 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1665 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001666 /* request was completed successfully */
1667 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 cqr->stopclk = now;
1669 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001670 if (cqr->devlist.next != &device->ccw_queue) {
1671 next = list_entry(cqr->devlist.next,
1672 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001674 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001675 /*
1676 * If we don't want complex ERP for this request, then just
1677 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001678 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001679 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001680 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001681 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001682 DBF_DEV_EVENT(DBF_DEBUG, device,
1683 "default ERP in fastpath "
1684 "(%i retries left)",
1685 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001686 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1687 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001688 cqr->status = DASD_CQR_QUEUED;
1689 next = cqr;
1690 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001692 }
1693 if (next && (next->status == DASD_CQR_QUEUED) &&
1694 (!device->stopped)) {
1695 if (device->discipline->start_IO(next) == 0)
1696 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001699 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001701 dasd_device_clear_timer(device);
1702 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001704EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001706enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1707{
1708 struct dasd_device *device;
1709
1710 device = dasd_device_from_cdev_locked(cdev);
1711
1712 if (IS_ERR(device))
1713 goto out;
1714 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1715 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001716 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001717 dasd_put_device(device);
1718 goto out;
1719 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001720 if (device->discipline->dump_sense_dbf)
1721 device->discipline->dump_sense_dbf(device, irb, "uc");
1722 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001723 dasd_put_device(device);
1724out:
1725 return UC_TODO_RETRY;
1726}
1727EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001730 * If we have an error on a dasd_block layer request then we cancel
1731 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001733static void __dasd_device_recovery(struct dasd_device *device,
1734 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct list_head *l, *n;
1737 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001744
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001745 list_for_each_safe(l, n, &device->ccw_queue) {
1746 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1747 if (cqr->status == DASD_CQR_QUEUED &&
1748 ref_cqr->block == cqr->block) {
1749 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001751 }
1752};
1753
1754/*
1755 * Remove those ccw requests from the queue that need to be returned
1756 * to the upper layer.
1757 */
1758static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1759 struct list_head *final_queue)
1760{
1761 struct list_head *l, *n;
1762 struct dasd_ccw_req *cqr;
1763
1764 /* Process request with final status. */
1765 list_for_each_safe(l, n, &device->ccw_queue) {
1766 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1767
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001768 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001769 if (cqr->status == DASD_CQR_QUEUED ||
1770 cqr->status == DASD_CQR_IN_IO ||
1771 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001772 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001773 if (cqr->status == DASD_CQR_ERROR) {
1774 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001776 /* Rechain finished requests to final queue */
1777 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779}
1780
1781/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001782 * the cqrs from the final queue are returned to the upper layer
1783 * by setting a dasd_block state and calling the callback function
1784 */
1785static void __dasd_device_process_final_queue(struct dasd_device *device,
1786 struct list_head *final_queue)
1787{
1788 struct list_head *l, *n;
1789 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001790 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001791 void (*callback)(struct dasd_ccw_req *, void *data);
1792 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001793 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001794
1795 list_for_each_safe(l, n, final_queue) {
1796 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1797 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001798 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001799 callback = cqr->callback;
1800 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001801 if (block)
1802 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001803 switch (cqr->status) {
1804 case DASD_CQR_SUCCESS:
1805 cqr->status = DASD_CQR_DONE;
1806 break;
1807 case DASD_CQR_ERROR:
1808 cqr->status = DASD_CQR_NEED_ERP;
1809 break;
1810 case DASD_CQR_CLEARED:
1811 cqr->status = DASD_CQR_TERMINATED;
1812 break;
1813 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001814 /* internal error 12 - wrong cqr status*/
1815 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1816 dev_err(&device->cdev->dev,
1817 "An error occurred in the DASD device driver, "
1818 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001819 BUG();
1820 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001821 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001822 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001823 if (block)
1824 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001825 }
1826}
1827
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001828/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * Take a look at the first request on the ccw queue and check
1830 * if it reached its expire time. If so, terminate the IO.
1831 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001832static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 struct dasd_ccw_req *cqr;
1835
1836 if (list_empty(&device->ccw_queue))
1837 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001838 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001839 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1840 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001841 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1842 /*
1843 * IO in safe offline processing should not
1844 * run out of retries
1845 */
1846 cqr->retries++;
1847 }
Horst Hummel29145a62006-12-04 15:40:15 +01001848 if (device->discipline->term_IO(cqr) != 0) {
1849 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001850 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001851 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001852 "ended, retrying in 5 s\n",
1853 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001854 cqr->expires += 5*HZ;
1855 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001856 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001857 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001858 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001859 "remaining\n", cqr, (cqr->expires/HZ),
1860 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862 }
1863}
1864
1865/*
1866 * Take a look at the first request on the ccw queue and check
1867 * if it needs to be started.
1868 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001869static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 struct dasd_ccw_req *cqr;
1872 int rc;
1873
1874 if (list_empty(&device->ccw_queue))
1875 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001876 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001877 if (cqr->status != DASD_CQR_QUEUED)
1878 return;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001879 /* when device is stopped, return request to previous layer
1880 * exception: only the disconnect or unresumed bits are set and the
1881 * cqr is a path verification request
1882 */
1883 if (device->stopped &&
1884 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1885 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1886 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001887 cqr->status = DASD_CQR_CLEARED;
1888 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001889 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001890 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001891
1892 rc = device->discipline->start_IO(cqr);
1893 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001894 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001895 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001896 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001897 } else
1898 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001899 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001900}
1901
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001902static void __dasd_device_check_path_events(struct dasd_device *device)
1903{
1904 int rc;
1905
1906 if (device->path_data.tbvpm) {
1907 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1908 DASD_UNRESUMED_PM))
1909 return;
1910 rc = device->discipline->verify_path(
1911 device, device->path_data.tbvpm);
1912 if (rc)
1913 dasd_device_set_timer(device, 50);
1914 else
1915 device->path_data.tbvpm = 0;
1916 }
1917};
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001920 * Go through all request on the dasd_device request queue,
1921 * terminate them on the cdev if necessary, and return them to the
1922 * submitting layer via callback.
1923 * Note:
1924 * Make sure that all 'submitting layers' still exist when
1925 * this function is called!. In other words, when 'device' is a base
1926 * device then all block layer requests must have been removed before
1927 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001929int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001931 struct dasd_ccw_req *cqr, *n;
1932 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 INIT_LIST_HEAD(&flush_queue);
1936 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001937 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001938 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001939 /* Check status and move request to flush_queue */
1940 switch (cqr->status) {
1941 case DASD_CQR_IN_IO:
1942 rc = device->discipline->term_IO(cqr);
1943 if (rc) {
1944 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001945 dev_err(&device->cdev->dev,
1946 "Flushing the DASD request queue "
1947 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001948 /* stop flush processing */
1949 goto finished;
1950 }
1951 break;
1952 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01001953 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001955 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001956 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001957 break;
1958 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001959 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001960 }
Horst Hummel8f617012006-08-30 14:33:33 +02001961finished:
1962 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001963 /*
1964 * After this point all requests must be in state CLEAR_PENDING,
1965 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1966 * one of the others.
1967 */
1968 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1969 wait_event(dasd_flush_wq,
1970 (cqr->status != DASD_CQR_CLEAR_PENDING));
1971 /*
1972 * Now set each request back to TERMINATED, DONE or NEED_ERP
1973 * and call the callback function of flushed requests
1974 */
1975 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001976 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001978EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980/*
1981 * Acquire the device lock and process queues for the device.
1982 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001983static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 atomic_set (&device->tasklet_scheduled, 0);
1988 INIT_LIST_HEAD(&final_queue);
1989 spin_lock_irq(get_ccwdev_lock(device->cdev));
1990 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001991 __dasd_device_check_expire(device);
1992 /* find final requests on ccw queue */
1993 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001994 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1996 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001997 __dasd_device_process_final_queue(device, &final_queue);
1998 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002000 __dasd_device_start_head(device);
2001 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002002 if (waitqueue_active(&shutdown_waitq))
2003 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 dasd_put_device(device);
2005}
2006
2007/*
2008 * Schedules a call to dasd_tasklet over the device tasklet.
2009 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002010void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002013 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return;
2015 dasd_get_device(device);
2016 tasklet_hi_schedule(&device->tasklet);
2017}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002018EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002020void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2021{
2022 device->stopped |= bits;
2023}
2024EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2025
2026void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2027{
2028 device->stopped &= ~bits;
2029 if (!device->stopped)
2030 wake_up(&generic_waitq);
2031}
2032EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002035 * Queue a request to the head of the device ccw_queue.
2036 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002038void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct dasd_device *device;
2041 unsigned long flags;
2042
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002043 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002045 cqr->status = DASD_CQR_QUEUED;
2046 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002048 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2050}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002051EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002054 * Queue a request to the tail of the device ccw_queue.
2055 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002057void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct dasd_device *device;
2060 unsigned long flags;
2061
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002064 cqr->status = DASD_CQR_QUEUED;
2065 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002067 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2069}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002070EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002073 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002075void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002077 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2078 cqr->callback_data = DASD_SLEEPON_END_TAG;
2079 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2080 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
Stefan Haberland5915a872011-10-30 15:16:57 +01002082EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 struct dasd_device *device;
2087 int rc;
2088
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002089 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002091 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2093 return rc;
2094}
2095
2096/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002097 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2098 */
2099static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2100{
2101 struct dasd_device *device;
2102 dasd_erp_fn_t erp_fn;
2103
2104 if (cqr->status == DASD_CQR_FILLED)
2105 return 0;
2106 device = cqr->startdev;
2107 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2108 if (cqr->status == DASD_CQR_TERMINATED) {
2109 device->discipline->handle_terminated_request(cqr);
2110 return 1;
2111 }
2112 if (cqr->status == DASD_CQR_NEED_ERP) {
2113 erp_fn = device->discipline->erp_action(cqr);
2114 erp_fn(cqr);
2115 return 1;
2116 }
2117 if (cqr->status == DASD_CQR_FAILED)
2118 dasd_log_sense(cqr, &cqr->irb);
2119 if (cqr->refers) {
2120 __dasd_process_erp(device, cqr);
2121 return 1;
2122 }
2123 }
2124 return 0;
2125}
2126
2127static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2128{
2129 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2130 if (cqr->refers) /* erp is not done yet */
2131 return 1;
2132 return ((cqr->status != DASD_CQR_DONE) &&
2133 (cqr->status != DASD_CQR_FAILED));
2134 } else
2135 return (cqr->status == DASD_CQR_FILLED);
2136}
2137
2138static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2139{
2140 struct dasd_device *device;
2141 int rc;
2142 struct list_head ccw_queue;
2143 struct dasd_ccw_req *cqr;
2144
2145 INIT_LIST_HEAD(&ccw_queue);
2146 maincqr->status = DASD_CQR_FILLED;
2147 device = maincqr->startdev;
2148 list_add(&maincqr->blocklist, &ccw_queue);
2149 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2150 cqr = list_first_entry(&ccw_queue,
2151 struct dasd_ccw_req, blocklist)) {
2152
2153 if (__dasd_sleep_on_erp(cqr))
2154 continue;
2155 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2156 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002157 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2158 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2159 cqr->status = DASD_CQR_FAILED;
2160 cqr->intrc = -EPERM;
2161 continue;
2162 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002163 /* Non-temporary stop condition will trigger fail fast */
2164 if (device->stopped & ~DASD_STOPPED_PENDING &&
2165 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2166 (!dasd_eer_enabled(device))) {
2167 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002168 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002169 continue;
2170 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002171 /*
2172 * Don't try to start requests if device is stopped
2173 * except path verification requests
2174 */
2175 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2176 if (interruptible) {
2177 rc = wait_event_interruptible(
2178 generic_waitq, !(device->stopped));
2179 if (rc == -ERESTARTSYS) {
2180 cqr->status = DASD_CQR_FAILED;
2181 maincqr->intrc = rc;
2182 continue;
2183 }
2184 } else
2185 wait_event(generic_waitq, !(device->stopped));
2186 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002187 if (!cqr->callback)
2188 cqr->callback = dasd_wakeup_cb;
2189
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002190 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002191 dasd_add_request_tail(cqr);
2192 if (interruptible) {
2193 rc = wait_event_interruptible(
2194 generic_waitq, _wait_for_wakeup(cqr));
2195 if (rc == -ERESTARTSYS) {
2196 dasd_cancel_req(cqr);
2197 /* wait (non-interruptible) for final status */
2198 wait_event(generic_waitq,
2199 _wait_for_wakeup(cqr));
2200 cqr->status = DASD_CQR_FAILED;
2201 maincqr->intrc = rc;
2202 continue;
2203 }
2204 } else
2205 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2206 }
2207
Heiko Carstens1aae0562013-01-30 09:49:40 +01002208 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002209 if ((maincqr->status != DASD_CQR_DONE) &&
2210 (maincqr->intrc != -ERESTARTSYS))
2211 dasd_log_sense(maincqr, &maincqr->irb);
2212 if (maincqr->status == DASD_CQR_DONE)
2213 rc = 0;
2214 else if (maincqr->intrc)
2215 rc = maincqr->intrc;
2216 else
2217 rc = -EIO;
2218 return rc;
2219}
2220
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002221static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2222{
2223 struct dasd_ccw_req *cqr;
2224
2225 list_for_each_entry(cqr, ccw_queue, blocklist) {
2226 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2227 return 0;
2228 }
2229
2230 return 1;
2231}
2232
2233static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2234{
2235 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002236 struct dasd_ccw_req *cqr, *n;
Stefan Haberland362ce842014-10-01 13:04:54 +02002237 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002238
2239retry:
2240 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2241 device = cqr->startdev;
2242 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2243 continue;
2244
2245 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2246 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2247 cqr->status = DASD_CQR_FAILED;
2248 cqr->intrc = -EPERM;
2249 continue;
2250 }
2251 /*Non-temporary stop condition will trigger fail fast*/
2252 if (device->stopped & ~DASD_STOPPED_PENDING &&
2253 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2254 !dasd_eer_enabled(device)) {
2255 cqr->status = DASD_CQR_FAILED;
2256 cqr->intrc = -EAGAIN;
2257 continue;
2258 }
2259
2260 /*Don't try to start requests if device is stopped*/
2261 if (interruptible) {
2262 rc = wait_event_interruptible(
2263 generic_waitq, !device->stopped);
2264 if (rc == -ERESTARTSYS) {
2265 cqr->status = DASD_CQR_FAILED;
2266 cqr->intrc = rc;
2267 continue;
2268 }
2269 } else
2270 wait_event(generic_waitq, !(device->stopped));
2271
2272 if (!cqr->callback)
2273 cqr->callback = dasd_wakeup_cb;
2274 cqr->callback_data = DASD_SLEEPON_START_TAG;
2275 dasd_add_request_tail(cqr);
2276 }
2277
2278 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2279
2280 rc = 0;
2281 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002282 /*
2283 * for alias devices simplify error recovery and
2284 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002285 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002286 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002287 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002288 (cqr->status == DASD_CQR_TERMINATED ||
2289 cqr->status == DASD_CQR_NEED_ERP))
2290 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002291
2292 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002293 if (__dasd_sleep_on_erp(cqr))
2294 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002295 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002296 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002297
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002298 return 0;
2299}
2300
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002301/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002302 * Queue a request to the tail of the device ccw_queue and wait for
2303 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002305int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002307 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002309EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002312 * Start requests from a ccw_queue and wait for their completion.
2313 */
2314int dasd_sleep_on_queue(struct list_head *ccw_queue)
2315{
2316 return _dasd_sleep_on_queue(ccw_queue, 0);
2317}
2318EXPORT_SYMBOL(dasd_sleep_on_queue);
2319
2320/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002321 * Queue a request to the tail of the device ccw_queue and wait
2322 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002324int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002326 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002328EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330/*
2331 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2332 * for eckd devices) the currently running request has to be terminated
2333 * and be put back to status queued, before the special request is added
2334 * to the head of the queue. Then the special request is waited on normally.
2335 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002336static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002339 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 if (list_empty(&device->ccw_queue))
2342 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002343 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002344 rc = device->discipline->term_IO(cqr);
2345 if (!rc)
2346 /*
2347 * CQR terminated because a more important request is pending.
2348 * Undo decreasing of retry counter because this is
2349 * not an error case.
2350 */
2351 cqr->retries++;
2352 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002355int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct dasd_device *device;
2358 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002359
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002360 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002361 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2362 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2363 cqr->status = DASD_CQR_FAILED;
2364 cqr->intrc = -EPERM;
2365 return -EIO;
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 spin_lock_irq(get_ccwdev_lock(device->cdev));
2368 rc = _dasd_term_running_cqr(device);
2369 if (rc) {
2370 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2371 return rc;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002374 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002376 /*
2377 * add new request as second
2378 * first the terminated cqr needs to be finished
2379 */
2380 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002383 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2386
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002387 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002388
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002389 if (cqr->status == DASD_CQR_DONE)
2390 rc = 0;
2391 else if (cqr->intrc)
2392 rc = cqr->intrc;
2393 else
2394 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002395
2396 /* kick tasklets */
2397 dasd_schedule_device_bh(device);
2398 if (device->block)
2399 dasd_schedule_block_bh(device->block);
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return rc;
2402}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002403EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405/*
2406 * Cancels a request that was started with dasd_sleep_on_req.
2407 * This is useful to timeout requests. The request will be
2408 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002409 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002410 * negative error code if termination failed
2411 * Cancellation of a request is an asynchronous operation! The calling
2412 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002414int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002416 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 unsigned long flags;
2418 int rc;
2419
2420 rc = 0;
2421 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2422 switch (cqr->status) {
2423 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002424 /* request was not started - just set to cleared */
2425 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002426 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2427 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
2429 case DASD_CQR_IN_IO:
2430 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002431 rc = device->discipline->term_IO(cqr);
2432 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002433 dev_err(&device->cdev->dev,
2434 "Cancelling request %p failed with rc=%d\n",
2435 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002436 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002437 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002440 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002444 dasd_schedule_device_bh(device);
2445 return rc;
2446}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002447EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002448
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002449/*
2450 * SECTION: Operations of the dasd_block layer.
2451 */
2452
2453/*
2454 * Timeout function for dasd_block. This is used when the block layer
2455 * is waiting for something that may not come reliably, (e.g. a state
2456 * change interrupt)
2457 */
2458static void dasd_block_timeout(unsigned long ptr)
2459{
2460 unsigned long flags;
2461 struct dasd_block *block;
2462
2463 block = (struct dasd_block *) ptr;
2464 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2465 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002466 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002467 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2468 dasd_schedule_block_bh(block);
2469}
2470
2471/*
2472 * Setup timeout for a dasd_block in jiffies.
2473 */
2474void dasd_block_set_timer(struct dasd_block *block, int expires)
2475{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002476 if (expires == 0)
2477 del_timer(&block->timer);
2478 else
2479 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002480}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002481EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482
2483/*
2484 * Clear timeout for a dasd_block.
2485 */
2486void dasd_block_clear_timer(struct dasd_block *block)
2487{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002488 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002489}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002490EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002491
2492/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002493 * Process finished error recovery ccw.
2494 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002495static void __dasd_process_erp(struct dasd_device *device,
2496 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002497{
2498 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002499
2500 if (cqr->status == DASD_CQR_DONE)
2501 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2502 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002503 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002504 erp_fn = device->discipline->erp_postaction(cqr);
2505 erp_fn(cqr);
2506}
2507
2508/*
2509 * Fetch requests from the block device queue.
2510 */
2511static void __dasd_process_request_queue(struct dasd_block *block)
2512{
2513 struct request_queue *queue;
2514 struct request *req;
2515 struct dasd_ccw_req *cqr;
2516 struct dasd_device *basedev;
2517 unsigned long flags;
2518 queue = block->request_queue;
2519 basedev = block->base;
2520 /* No queue ? Then there is nothing to do. */
2521 if (queue == NULL)
2522 return;
2523
2524 /*
2525 * We requeue request from the block device queue to the ccw
2526 * queue only in two states. In state DASD_STATE_READY the
2527 * partition detection is done and we need to requeue requests
2528 * for that. State DASD_STATE_ONLINE is normal block device
2529 * operation.
2530 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002531 if (basedev->state < DASD_STATE_READY) {
2532 while ((req = blk_fetch_request(block->request_queue)))
2533 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002534 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002535 }
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002536
2537 /* if device ist stopped do not fetch new requests */
2538 if (basedev->stopped)
2539 return;
2540
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002541 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002542 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002543 if (basedev->features & DASD_FEATURE_READONLY &&
2544 rq_data_dir(req) == WRITE) {
2545 DBF_DEV_EVENT(DBF_ERR, basedev,
2546 "Rejecting write request %p",
2547 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002548 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002549 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002550 continue;
2551 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002552 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2553 (basedev->features & DASD_FEATURE_FAILFAST ||
2554 blk_noretry_request(req))) {
2555 DBF_DEV_EVENT(DBF_ERR, basedev,
2556 "Rejecting failfast request %p",
2557 req);
2558 blk_start_request(req);
2559 __blk_end_request_all(req, -ETIMEDOUT);
2560 continue;
2561 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002562 cqr = basedev->discipline->build_cp(basedev, block, req);
2563 if (IS_ERR(cqr)) {
2564 if (PTR_ERR(cqr) == -EBUSY)
2565 break; /* normal end condition */
2566 if (PTR_ERR(cqr) == -ENOMEM)
2567 break; /* terminate request queue loop */
2568 if (PTR_ERR(cqr) == -EAGAIN) {
2569 /*
2570 * The current request cannot be build right
2571 * now, we have to try later. If this request
2572 * is the head-of-queue we stop the device
2573 * for 1/2 second.
2574 */
2575 if (!list_empty(&block->ccw_queue))
2576 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002577 spin_lock_irqsave(
2578 get_ccwdev_lock(basedev->cdev), flags);
2579 dasd_device_set_stop_bits(basedev,
2580 DASD_STOPPED_PENDING);
2581 spin_unlock_irqrestore(
2582 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002583 dasd_block_set_timer(block, HZ/2);
2584 break;
2585 }
2586 DBF_DEV_EVENT(DBF_ERR, basedev,
2587 "CCW creation failed (rc=%ld) "
2588 "on request %p",
2589 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002590 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002591 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002592 continue;
2593 }
2594 /*
2595 * Note: callback is set to dasd_return_cqr_cb in
2596 * __dasd_block_start_head to cover erp requests as well
2597 */
2598 cqr->callback_data = (void *) req;
2599 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002600 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002601 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002602 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002603 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002604 dasd_profile_start(block, cqr, req);
2605 }
2606}
2607
2608static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2609{
2610 struct request *req;
2611 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002612 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002613
2614 req = (struct request *) cqr->callback_data;
2615 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002616 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002617 if (status < 0)
2618 error = status;
2619 else if (status == 0) {
2620 if (cqr->intrc == -EPERM)
2621 error = -EBADE;
2622 else if (cqr->intrc == -ENOLINK ||
2623 cqr->intrc == -ETIMEDOUT)
2624 error = cqr->intrc;
2625 else
2626 error = -EIO;
2627 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002628 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002629}
2630
2631/*
2632 * Process ccw request queue.
2633 */
2634static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2635 struct list_head *final_queue)
2636{
2637 struct list_head *l, *n;
2638 struct dasd_ccw_req *cqr;
2639 dasd_erp_fn_t erp_fn;
2640 unsigned long flags;
2641 struct dasd_device *base = block->base;
2642
2643restart:
2644 /* Process request with final status. */
2645 list_for_each_safe(l, n, &block->ccw_queue) {
2646 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2647 if (cqr->status != DASD_CQR_DONE &&
2648 cqr->status != DASD_CQR_FAILED &&
2649 cqr->status != DASD_CQR_NEED_ERP &&
2650 cqr->status != DASD_CQR_TERMINATED)
2651 continue;
2652
2653 if (cqr->status == DASD_CQR_TERMINATED) {
2654 base->discipline->handle_terminated_request(cqr);
2655 goto restart;
2656 }
2657
2658 /* Process requests that may be recovered */
2659 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002660 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002661 if (IS_ERR(erp_fn(cqr)))
2662 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002663 goto restart;
2664 }
2665
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002666 /* log sense for fatal error */
2667 if (cqr->status == DASD_CQR_FAILED) {
2668 dasd_log_sense(cqr, &cqr->irb);
2669 }
2670
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002671 /* First of all call extended error reporting. */
2672 if (dasd_eer_enabled(base) &&
2673 cqr->status == DASD_CQR_FAILED) {
2674 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2675
2676 /* restart request */
2677 cqr->status = DASD_CQR_FILLED;
2678 cqr->retries = 255;
2679 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002680 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002681 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2682 flags);
2683 goto restart;
2684 }
2685
2686 /* Process finished ERP request. */
2687 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002688 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002689 goto restart;
2690 }
2691
2692 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002693 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002694 list_move_tail(&cqr->blocklist, final_queue);
2695 }
2696}
2697
2698static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2699{
2700 dasd_schedule_block_bh(cqr->block);
2701}
2702
2703static void __dasd_block_start_head(struct dasd_block *block)
2704{
2705 struct dasd_ccw_req *cqr;
2706
2707 if (list_empty(&block->ccw_queue))
2708 return;
2709 /* We allways begin with the first requests on the queue, as some
2710 * of previously started requests have to be enqueued on a
2711 * dasd_device again for error recovery.
2712 */
2713 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2714 if (cqr->status != DASD_CQR_FILLED)
2715 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002716 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2717 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2718 cqr->status = DASD_CQR_FAILED;
2719 cqr->intrc = -EPERM;
2720 dasd_schedule_block_bh(block);
2721 continue;
2722 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002723 /* Non-temporary stop condition will trigger fail fast */
2724 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2725 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2726 (!dasd_eer_enabled(block->base))) {
2727 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002728 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002729 dasd_schedule_block_bh(block);
2730 continue;
2731 }
2732 /* Don't try to start requests if device is stopped */
2733 if (block->base->stopped)
2734 return;
2735
2736 /* just a fail safe check, should not happen */
2737 if (!cqr->startdev)
2738 cqr->startdev = block->base;
2739
2740 /* make sure that the requests we submit find their way back */
2741 cqr->callback = dasd_return_cqr_cb;
2742
2743 dasd_add_request_tail(cqr);
2744 }
2745}
2746
2747/*
2748 * Central dasd_block layer routine. Takes requests from the generic
2749 * block layer request queue, creates ccw requests, enqueues them on
2750 * a dasd_device and processes ccw requests that have been returned.
2751 */
2752static void dasd_block_tasklet(struct dasd_block *block)
2753{
2754 struct list_head final_queue;
2755 struct list_head *l, *n;
2756 struct dasd_ccw_req *cqr;
2757
2758 atomic_set(&block->tasklet_scheduled, 0);
2759 INIT_LIST_HEAD(&final_queue);
2760 spin_lock(&block->queue_lock);
2761 /* Finish off requests on ccw queue */
2762 __dasd_process_block_ccw_queue(block, &final_queue);
2763 spin_unlock(&block->queue_lock);
2764 /* Now call the callback function of requests with final status */
2765 spin_lock_irq(&block->request_queue_lock);
2766 list_for_each_safe(l, n, &final_queue) {
2767 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2768 list_del_init(&cqr->blocklist);
2769 __dasd_cleanup_cqr(cqr);
2770 }
2771 spin_lock(&block->queue_lock);
2772 /* Get new request from the block device request queue */
2773 __dasd_process_request_queue(block);
2774 /* Now check if the head of the ccw queue needs to be started. */
2775 __dasd_block_start_head(block);
2776 spin_unlock(&block->queue_lock);
2777 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002778 if (waitqueue_active(&shutdown_waitq))
2779 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002780 dasd_put_device(block->base);
2781}
2782
2783static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2784{
2785 wake_up(&dasd_flush_wq);
2786}
2787
2788/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002789 * Requeue a request back to the block request queue
2790 * only works for block requests
2791 */
2792static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2793{
2794 struct dasd_block *block = cqr->block;
2795 struct request *req;
2796 unsigned long flags;
2797
2798 if (!block)
2799 return -EINVAL;
2800 spin_lock_irqsave(&block->queue_lock, flags);
2801 req = (struct request *) cqr->callback_data;
2802 blk_requeue_request(block->request_queue, req);
2803 spin_unlock_irqrestore(&block->queue_lock, flags);
2804
2805 return 0;
2806}
2807
2808/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002809 * Go through all request on the dasd_block request queue, cancel them
2810 * on the respective dasd_device, and return them to the generic
2811 * block layer.
2812 */
2813static int dasd_flush_block_queue(struct dasd_block *block)
2814{
2815 struct dasd_ccw_req *cqr, *n;
2816 int rc, i;
2817 struct list_head flush_queue;
2818
2819 INIT_LIST_HEAD(&flush_queue);
2820 spin_lock_bh(&block->queue_lock);
2821 rc = 0;
2822restart:
2823 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2824 /* if this request currently owned by a dasd_device cancel it */
2825 if (cqr->status >= DASD_CQR_QUEUED)
2826 rc = dasd_cancel_req(cqr);
2827 if (rc < 0)
2828 break;
2829 /* Rechain request (including erp chain) so it won't be
2830 * touched by the dasd_block_tasklet anymore.
2831 * Replace the callback so we notice when the request
2832 * is returned from the dasd_device layer.
2833 */
2834 cqr->callback = _dasd_wake_block_flush_cb;
2835 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2836 list_move_tail(&cqr->blocklist, &flush_queue);
2837 if (i > 1)
2838 /* moved more than one request - need to restart */
2839 goto restart;
2840 }
2841 spin_unlock_bh(&block->queue_lock);
2842 /* Now call the callback function of flushed requests */
2843restart_cb:
2844 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2845 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2846 /* Process finished ERP request. */
2847 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002848 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002849 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002850 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002851 /* restart list_for_xx loop since dasd_process_erp
2852 * might remove multiple elements */
2853 goto restart_cb;
2854 }
2855 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002856 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002857 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002858 list_del_init(&cqr->blocklist);
2859 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002860 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return rc;
2863}
2864
2865/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002866 * Schedules a call to dasd_tasklet over the device tasklet.
2867 */
2868void dasd_schedule_block_bh(struct dasd_block *block)
2869{
2870 /* Protect against rescheduling. */
2871 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2872 return;
2873 /* life cycle of block is bound to it's base device */
2874 dasd_get_device(block->base);
2875 tasklet_hi_schedule(&block->tasklet);
2876}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002877EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002878
2879
2880/*
2881 * SECTION: external block device operations
2882 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 */
2884
2885/*
2886 * Dasd request queue function. Called from ll_rw_blk.c
2887 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002888static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002890 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002892 block = queue->queuedata;
2893 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002895 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002897 __dasd_block_start_head(block);
2898 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
2901/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00002902 * Block timeout callback, called from the block layer
2903 *
2904 * request_queue lock is held on entry.
2905 *
2906 * Return values:
2907 * BLK_EH_RESET_TIMER if the request should be left running
2908 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2909 * by the driver.
2910 */
2911enum blk_eh_timer_return dasd_times_out(struct request *req)
2912{
2913 struct dasd_ccw_req *cqr = req->completion_data;
2914 struct dasd_block *block = req->q->queuedata;
2915 struct dasd_device *device;
2916 int rc = 0;
2917
2918 if (!cqr)
2919 return BLK_EH_NOT_HANDLED;
2920
2921 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00002922 if (!device->blk_timeout)
2923 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002924 DBF_DEV_EVENT(DBF_WARNING, device,
2925 " dasd_times_out cqr %p status %x",
2926 cqr, cqr->status);
2927
2928 spin_lock(&block->queue_lock);
2929 spin_lock(get_ccwdev_lock(device->cdev));
2930 cqr->retries = -1;
2931 cqr->intrc = -ETIMEDOUT;
2932 if (cqr->status >= DASD_CQR_QUEUED) {
2933 spin_unlock(get_ccwdev_lock(device->cdev));
2934 rc = dasd_cancel_req(cqr);
2935 } else if (cqr->status == DASD_CQR_FILLED ||
2936 cqr->status == DASD_CQR_NEED_ERP) {
2937 cqr->status = DASD_CQR_TERMINATED;
2938 spin_unlock(get_ccwdev_lock(device->cdev));
2939 } else if (cqr->status == DASD_CQR_IN_ERP) {
2940 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2941
2942 list_for_each_entry_safe(searchcqr, nextcqr,
2943 &block->ccw_queue, blocklist) {
2944 tmpcqr = searchcqr;
2945 while (tmpcqr->refers)
2946 tmpcqr = tmpcqr->refers;
2947 if (tmpcqr != cqr)
2948 continue;
2949 /* searchcqr is an ERP request for cqr */
2950 searchcqr->retries = -1;
2951 searchcqr->intrc = -ETIMEDOUT;
2952 if (searchcqr->status >= DASD_CQR_QUEUED) {
2953 spin_unlock(get_ccwdev_lock(device->cdev));
2954 rc = dasd_cancel_req(searchcqr);
2955 spin_lock(get_ccwdev_lock(device->cdev));
2956 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2957 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2958 searchcqr->status = DASD_CQR_TERMINATED;
2959 rc = 0;
2960 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2961 /*
2962 * Shouldn't happen; most recent ERP
2963 * request is at the front of queue
2964 */
2965 continue;
2966 }
2967 break;
2968 }
2969 spin_unlock(get_ccwdev_lock(device->cdev));
2970 }
2971 dasd_schedule_block_bh(block);
2972 spin_unlock(&block->queue_lock);
2973
2974 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2975}
2976
2977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 * Allocate and initialize request queue and default I/O scheduler.
2979 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002980static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002982 block->request_queue = blk_init_queue(do_dasd_request,
2983 &block->request_queue_lock);
2984 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return -ENOMEM;
2986
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002987 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01002989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992/*
2993 * Allocate and initialize request queue.
2994 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002995static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 int max;
2998
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01002999 if (block->base->features & DASD_FEATURE_USERAW) {
3000 /*
3001 * the max_blocks value for raw_track access is 256
3002 * it is higher than the native ECKD value because we
3003 * only need one ccw per track
3004 * so the max_hw_sectors are
3005 * 2048 x 512B = 1024kB = 16 tracks
3006 */
3007 max = 2048;
3008 } else {
3009 max = block->base->discipline->max_blocks << block->s2b_shift;
3010 }
3011 blk_queue_logical_block_size(block->request_queue,
3012 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003013 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003014 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003015 /* with page sized segments we can translate each segement into
3016 * one idaw/tidaw
3017 */
3018 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3019 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
3022/*
3023 * Deactivate and free request queue.
3024 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003025static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003027 if (block->request_queue) {
3028 blk_cleanup_queue(block->request_queue);
3029 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
3031}
3032
3033/*
3034 * Flush request on the request queue.
3035 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003036static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 struct request *req;
3039
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003040 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003042
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003043 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003044 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003045 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003046 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Al Viro57a7c0b2008-03-02 10:36:08 -05003049static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003051 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 int rc;
3053
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003054 base = dasd_device_from_gendisk(bdev->bd_disk);
3055 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003056 return -ENODEV;
3057
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003058 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003059 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 rc = -ENODEV;
3061 goto unlock;
3062 }
3063
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003064 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 rc = -EINVAL;
3066 goto unlock;
3067 }
3068
3069 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003070 dev_info(&base->cdev->dev,
3071 "Accessing the DASD failed because it is in "
3072 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 rc = -EPERM;
3074 goto out;
3075 }
3076
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003077 if (base->state <= DASD_STATE_BASIC) {
3078 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 " Cannot open unrecognized device");
3080 rc = -ENODEV;
3081 goto out;
3082 }
3083
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003084 if ((mode & FMODE_WRITE) &&
3085 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3086 (base->features & DASD_FEATURE_READONLY))) {
3087 rc = -EROFS;
3088 goto out;
3089 }
3090
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003091 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return 0;
3093
3094out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003095 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003097 atomic_dec(&base->block->open_count);
3098 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return rc;
3100}
3101
Al Virodb2a1442013-05-05 21:52:57 -04003102static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Al Virodb2a1442013-05-05 21:52:57 -04003104 struct dasd_device *base = dasd_device_from_gendisk(disk);
3105 if (base) {
3106 atomic_dec(&base->block->open_count);
3107 module_put(base->discipline->owner);
3108 dasd_put_device(base);
3109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003112/*
3113 * Return disk geometry.
3114 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003115static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003116{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003117 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003118
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003119 base = dasd_device_from_gendisk(bdev->bd_disk);
3120 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003121 return -ENODEV;
3122
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003123 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003124 !base->discipline->fill_geometry) {
3125 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003126 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003127 }
3128 base->discipline->fill_geometry(base->block, geo);
3129 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3130 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003131 return 0;
3132}
3133
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003134const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135dasd_device_operations = {
3136 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003137 .open = dasd_open,
3138 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003139 .ioctl = dasd_ioctl,
3140 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003141 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142};
3143
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003144/*******************************************************************************
3145 * end of block device operations
3146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148static void
3149dasd_exit(void)
3150{
3151#ifdef CONFIG_PROC_FS
3152 dasd_proc_exit();
3153#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003154 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003155 if (dasd_page_cache != NULL) {
3156 kmem_cache_destroy(dasd_page_cache);
3157 dasd_page_cache = NULL;
3158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 dasd_gendisk_exit();
3160 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (dasd_debug_area != NULL) {
3162 debug_unregister(dasd_debug_area);
3163 dasd_debug_area = NULL;
3164 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003165 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
3168/*
3169 * SECTION: common functions for ccw_driver use
3170 */
3171
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003172/*
3173 * Is the device read-only?
3174 * Note that this function does not report the setting of the
3175 * readonly device attribute, but how it is configured in z/VM.
3176 */
3177int dasd_device_is_ro(struct dasd_device *device)
3178{
3179 struct ccw_dev_id dev_id;
3180 struct diag210 diag_data;
3181 int rc;
3182
3183 if (!MACHINE_IS_VM)
3184 return 0;
3185 ccw_device_get_id(device->cdev, &dev_id);
3186 memset(&diag_data, 0, sizeof(diag_data));
3187 diag_data.vrdcdvno = dev_id.devno;
3188 diag_data.vrdclen = sizeof(diag_data);
3189 rc = diag210(&diag_data);
3190 if (rc == 0 || rc == 2) {
3191 return diag_data.vrdcvfla & 0x80;
3192 } else {
3193 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3194 dev_id.devno, rc);
3195 return 0;
3196 }
3197}
3198EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3199
Cornelia Huckf3445a12009-04-14 15:36:23 +02003200static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3201{
3202 struct ccw_device *cdev = data;
3203 int ret;
3204
3205 ret = ccw_device_set_online(cdev);
3206 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003207 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3208 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003209}
3210
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003211/*
3212 * Initial attempt at a probe function. this can be simplified once
3213 * the other detection code is gone.
3214 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003215int dasd_generic_probe(struct ccw_device *cdev,
3216 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 int ret;
3219
3220 ret = dasd_add_sysfs_files(cdev);
3221 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003222 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3223 "dasd_generic_probe: could not add "
3224 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
Horst Hummel40545572006-06-29 15:08:18 +02003227 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Horst Hummel40545572006-06-29 15:08:18 +02003229 /*
3230 * Automatically online either all dasd devices (dasd_autodetect)
3231 * or all devices specified with dasd= parameters during
3232 * initial probe.
3233 */
3234 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003235 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003236 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003239EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003241/*
3242 * This will one day be called from a global not_oper handler.
3243 * It is also used by driver_unregister during module unload.
3244 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003245void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
3247 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003248 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Horst Hummel59afda72005-05-16 21:53:39 -07003250 cdev->handler = NULL;
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003253 if (IS_ERR(device)) {
3254 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003256 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003257 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3258 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 /* Already doing offline processing */
3260 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003261 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return;
3263 }
3264 /*
3265 * This device is removed unconditionally. Set offline
3266 * flag to prevent dasd_open from opening it while it is
3267 * no quite down yet.
3268 */
3269 dasd_set_target_state(device, DASD_STATE_NEW);
3270 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003271 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003273 /*
3274 * life cycle of block is bound to device, so delete it after
3275 * device was safely removed
3276 */
3277 if (block)
3278 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003279
3280 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003282EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003284/*
3285 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003287 * or the user has started activation through sysfs.
3288 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003289int dasd_generic_set_online(struct ccw_device *cdev,
3290 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003292 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003294 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003295
Horst Hummel40545572006-06-29 15:08:18 +02003296 /* first online clears initial online feature flag */
3297 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 device = dasd_create_device(cdev);
3299 if (IS_ERR(device))
3300 return PTR_ERR(device);
3301
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003302 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003303 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003305 /* Try to load the required module. */
3306 rc = request_module(DASD_DIAG_MOD);
3307 if (rc) {
3308 pr_warn("%s Setting the DASD online failed "
3309 "because the required module %s "
3310 "could not be loaded (rc=%d)\n",
3311 dev_name(&cdev->dev), DASD_DIAG_MOD,
3312 rc);
3313 dasd_delete_device(device);
3314 return -ENODEV;
3315 }
3316 }
3317 /* Module init could have failed, so check again here after
3318 * request_module(). */
3319 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003320 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3321 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 dasd_delete_device(device);
3323 return -ENODEV;
3324 }
3325 discipline = dasd_diag_discipline_pointer;
3326 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003327 if (!try_module_get(base_discipline->owner)) {
3328 dasd_delete_device(device);
3329 return -EINVAL;
3330 }
3331 if (!try_module_get(discipline->owner)) {
3332 module_put(base_discipline->owner);
3333 dasd_delete_device(device);
3334 return -EINVAL;
3335 }
3336 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 device->discipline = discipline;
3338
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003339 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 rc = discipline->check_device(device);
3341 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003342 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3343 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003344 module_put(discipline->owner);
3345 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 dasd_delete_device(device);
3347 return rc;
3348 }
3349
3350 dasd_set_target_state(device, DASD_STATE_ONLINE);
3351 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003352 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3353 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 rc = -ENODEV;
3355 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003356 if (device->block)
3357 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 dasd_delete_device(device);
3359 } else
3360 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003361 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003362
3363 wait_event(dasd_init_waitq, _wait_for_device(device));
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return rc;
3367}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003368EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003370int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003373 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003374 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003376 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 device = dasd_device_from_cdev(cdev);
3378 if (IS_ERR(device))
3379 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /*
3382 * We must make sure that this device is currently not in use.
3383 * The open_count is increased for every opener, that includes
3384 * the blkdev_get in dasd_scan_partitions. We are only interested
3385 * in the other openers.
3386 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003387 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003388 max_count = device->block->bdev ? 0 : -1;
3389 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003390 if (open_count > max_count) {
3391 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003392 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3393 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003394 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003395 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3396 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003397 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3398 dasd_put_device(device);
3399 return -EBUSY;
3400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003402
3403 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3404 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003405 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003406 * could only be called by normal offline so safe_offline flag
3407 * needs to be removed to run normal offline and kill all I/O
3408 */
3409 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3410 /* Already doing normal offline processing */
3411 dasd_put_device(device);
3412 return -EBUSY;
3413 } else
3414 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3415
3416 } else
3417 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3418 /* Already doing offline processing */
3419 dasd_put_device(device);
3420 return -EBUSY;
3421 }
3422
3423 /*
3424 * if safe_offline called set safe_offline_running flag and
3425 * clear safe_offline so that a call to normal offline
3426 * can overrun safe_offline processing
3427 */
3428 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3429 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3430 /*
3431 * If we want to set the device safe offline all IO operations
3432 * should be finished before continuing the offline process
3433 * so sync bdev first and then wait for our queues to become
3434 * empty
3435 */
3436 /* sync blockdev and partitions */
3437 rc = fsync_bdev(device->block->bdev);
3438 if (rc != 0)
3439 goto interrupted;
3440
3441 /* schedule device tasklet and wait for completion */
3442 dasd_schedule_device_bh(device);
3443 rc = wait_event_interruptible(shutdown_waitq,
3444 _wait_for_empty_queues(device));
3445 if (rc != 0)
3446 goto interrupted;
3447 }
3448
3449 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 dasd_set_target_state(device, DASD_STATE_NEW);
3451 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003452 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003454 /*
3455 * life cycle of block is bound to device, so delete it after
3456 * device was safely removed
3457 */
3458 if (block)
3459 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003461
3462interrupted:
3463 /* interrupted by signal */
3464 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3465 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3466 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3467 dasd_put_device(device);
3468 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003470EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003472int dasd_generic_last_path_gone(struct dasd_device *device)
3473{
3474 struct dasd_ccw_req *cqr;
3475
3476 dev_warn(&device->cdev->dev, "No operational channel path is left "
3477 "for the device\n");
3478 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3479 /* First of all call extended error reporting. */
3480 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3481
3482 if (device->state < DASD_STATE_BASIC)
3483 return 0;
3484 /* Device is active. We want to keep it. */
3485 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3486 if ((cqr->status == DASD_CQR_IN_IO) ||
3487 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3488 cqr->status = DASD_CQR_QUEUED;
3489 cqr->retries++;
3490 }
3491 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3492 dasd_device_clear_timer(device);
3493 dasd_schedule_device_bh(device);
3494 return 1;
3495}
3496EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3497
3498int dasd_generic_path_operational(struct dasd_device *device)
3499{
3500 dev_info(&device->cdev->dev, "A channel path to the device has become "
3501 "operational\n");
3502 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3503 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3504 if (device->stopped & DASD_UNRESUMED_PM) {
3505 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3506 dasd_restore_device(device);
3507 return 1;
3508 }
3509 dasd_schedule_device_bh(device);
3510 if (device->block)
3511 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003512
3513 if (!device->stopped)
3514 wake_up(&generic_waitq);
3515
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003516 return 1;
3517}
3518EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3519
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003520int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 int ret;
3524
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003525 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 if (IS_ERR(device))
3527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 ret = 0;
3529 switch (event) {
3530 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003531 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003533 device->path_data.opm = 0;
3534 device->path_data.ppm = 0;
3535 device->path_data.npm = 0;
3536 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 break;
3538 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003540 if (device->path_data.opm)
3541 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 break;
3543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 dasd_put_device(device);
3545 return ret;
3546}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003547EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003549void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3550{
3551 int chp;
3552 __u8 oldopm, eventlpm;
3553 struct dasd_device *device;
3554
3555 device = dasd_device_from_cdev_locked(cdev);
3556 if (IS_ERR(device))
3557 return;
3558 for (chp = 0; chp < 8; chp++) {
3559 eventlpm = 0x80 >> chp;
3560 if (path_event[chp] & PE_PATH_GONE) {
3561 oldopm = device->path_data.opm;
3562 device->path_data.opm &= ~eventlpm;
3563 device->path_data.ppm &= ~eventlpm;
3564 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003565 if (oldopm && !device->path_data.opm) {
3566 dev_warn(&device->cdev->dev,
3567 "No verified channel paths remain "
3568 "for the device\n");
3569 DBF_DEV_EVENT(DBF_WARNING, device,
3570 "%s", "last verified path gone");
3571 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3572 dasd_device_set_stop_bits(device,
3573 DASD_STOPPED_DC_WAIT);
3574 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003575 }
3576 if (path_event[chp] & PE_PATH_AVAILABLE) {
3577 device->path_data.opm &= ~eventlpm;
3578 device->path_data.ppm &= ~eventlpm;
3579 device->path_data.npm &= ~eventlpm;
3580 device->path_data.tbvpm |= eventlpm;
3581 dasd_schedule_device_bh(device);
3582 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003583 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003584 if (!(device->path_data.opm & eventlpm) &&
3585 !(device->path_data.tbvpm & eventlpm)) {
3586 /*
3587 * we can not establish a pathgroup on an
3588 * unavailable path, so trigger a path
3589 * verification first
3590 */
3591 device->path_data.tbvpm |= eventlpm;
3592 dasd_schedule_device_bh(device);
3593 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003594 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3595 "Pathgroup re-established\n");
3596 if (device->discipline->kick_validate)
3597 device->discipline->kick_validate(device);
3598 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003599 }
3600 dasd_put_device(device);
3601}
3602EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3603
3604int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3605{
3606 if (!device->path_data.opm && lpm) {
3607 device->path_data.opm = lpm;
3608 dasd_generic_path_operational(device);
3609 } else
3610 device->path_data.opm |= lpm;
3611 return 0;
3612}
3613EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3614
3615
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003616int dasd_generic_pm_freeze(struct ccw_device *cdev)
3617{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003618 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003619 struct list_head freeze_queue;
3620 struct dasd_ccw_req *cqr, *n;
3621 struct dasd_ccw_req *refers;
3622 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003623
3624 if (IS_ERR(device))
3625 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003626
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003627 /* mark device as suspended */
3628 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3629
Stefan Haberland6f272b92011-01-05 12:48:05 +01003630 if (device->discipline->freeze)
3631 rc = device->discipline->freeze(device);
3632
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003633 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003634 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003635
3636 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003637 INIT_LIST_HEAD(&freeze_queue);
3638 spin_lock_irq(get_ccwdev_lock(cdev));
3639 rc = 0;
3640 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3641 /* Check status and move request to flush_queue */
3642 if (cqr->status == DASD_CQR_IN_IO) {
3643 rc = device->discipline->term_IO(cqr);
3644 if (rc) {
3645 /* unable to terminate requeust */
3646 dev_err(&device->cdev->dev,
3647 "Unable to terminate request %p "
3648 "on suspend\n", cqr);
3649 spin_unlock_irq(get_ccwdev_lock(cdev));
3650 dasd_put_device(device);
3651 return rc;
3652 }
3653 }
3654 list_move_tail(&cqr->devlist, &freeze_queue);
3655 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003656 spin_unlock_irq(get_ccwdev_lock(cdev));
3657
3658 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3659 wait_event(dasd_flush_wq,
3660 (cqr->status != DASD_CQR_CLEAR_PENDING));
3661 if (cqr->status == DASD_CQR_CLEARED)
3662 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003663
Stefan Haberlandc5576872013-04-15 16:41:31 +02003664 /* requeue requests to blocklayer will only work for
3665 block device requests */
3666 if (_dasd_requeue_request(cqr))
3667 continue;
3668
3669 /* remove requests from device and block queue */
3670 list_del_init(&cqr->devlist);
3671 while (cqr->refers != NULL) {
3672 refers = cqr->refers;
3673 /* remove the request from the block queue */
3674 list_del(&cqr->blocklist);
3675 /* free the finished erp request */
3676 dasd_free_erp_request(cqr, cqr->memdev);
3677 cqr = refers;
3678 }
3679 if (cqr->block)
3680 list_del_init(&cqr->blocklist);
3681 cqr->block->base->discipline->free_cp(
3682 cqr, (struct request *) cqr->callback_data);
3683 }
3684
3685 /*
3686 * if requests remain then they are internal request
3687 * and go back to the device queue
3688 */
3689 if (!list_empty(&freeze_queue)) {
3690 /* move freeze_queue to start of the ccw_queue */
3691 spin_lock_irq(get_ccwdev_lock(cdev));
3692 list_splice_tail(&freeze_queue, &device->ccw_queue);
3693 spin_unlock_irq(get_ccwdev_lock(cdev));
3694 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003695 dasd_put_device(device);
3696 return rc;
3697}
3698EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3699
3700int dasd_generic_restore_device(struct ccw_device *cdev)
3701{
3702 struct dasd_device *device = dasd_device_from_cdev(cdev);
3703 int rc = 0;
3704
3705 if (IS_ERR(device))
3706 return PTR_ERR(device);
3707
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003708 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003709 dasd_device_remove_stop_bits(device,
3710 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003711
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003712 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003713
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003714 /*
3715 * call discipline restore function
3716 * if device is stopped do nothing e.g. for disconnected devices
3717 */
3718 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003719 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003720 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003721 /*
3722 * if the resume failed for the DASD we put it in
3723 * an UNRESUMED stop state
3724 */
3725 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003726
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003727 if (device->block)
3728 dasd_schedule_block_bh(device->block);
3729
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003730 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003731 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003732 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003733}
3734EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3735
Heiko Carstens763968e2007-05-10 15:45:46 +02003736static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3737 void *rdc_buffer,
3738 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003739 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003740{
3741 struct dasd_ccw_req *cqr;
3742 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003743 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003744
3745 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3746
3747 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003748 /* internal error 13 - Allocating the RDC request failed*/
3749 dev_err(&device->cdev->dev,
3750 "An error occurred in the DASD device driver, "
3751 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003752 return cqr;
3753 }
3754
3755 ccw = cqr->cpaddr;
3756 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003757 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3758 idaw = (unsigned long *) (cqr->data);
3759 ccw->cda = (__u32)(addr_t) idaw;
3760 ccw->flags = CCW_FLAG_IDA;
3761 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3762 } else {
3763 ccw->cda = (__u32)(addr_t) rdc_buffer;
3764 ccw->flags = 0;
3765 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003766
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003767 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003768 cqr->startdev = device;
3769 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003770 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003771 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003772 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003773 cqr->status = DASD_CQR_FILLED;
3774 return cqr;
3775}
3776
3777
Stefan Haberland68b781f2009-09-11 10:28:29 +02003778int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003779 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003780{
3781 int ret;
3782 struct dasd_ccw_req *cqr;
3783
Sebastian Ott92636b12009-06-12 10:26:37 +02003784 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003785 magic);
3786 if (IS_ERR(cqr))
3787 return PTR_ERR(cqr);
3788
3789 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003790 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003791 return ret;
3792}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003793EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003794
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003795/*
3796 * In command mode and transport mode we need to look for sense
3797 * data in different places. The sense data itself is allways
3798 * an array of 32 bytes, so we can unify the sense data access
3799 * for both modes.
3800 */
3801char *dasd_get_sense(struct irb *irb)
3802{
3803 struct tsb *tsb = NULL;
3804 char *sense = NULL;
3805
3806 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3807 if (irb->scsw.tm.tcw)
3808 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3809 irb->scsw.tm.tcw);
3810 if (tsb && tsb->length == 64 && tsb->flags)
3811 switch (tsb->flags & 0x07) {
3812 case 1: /* tsa_iostat */
3813 sense = tsb->tsa.iostat.sense;
3814 break;
3815 case 2: /* tsa_ddpc */
3816 sense = tsb->tsa.ddpc.sense;
3817 break;
3818 default:
3819 /* currently we don't use interrogate data */
3820 break;
3821 }
3822 } else if (irb->esw.esw0.erw.cons) {
3823 sense = irb->ecw;
3824 }
3825 return sense;
3826}
3827EXPORT_SYMBOL_GPL(dasd_get_sense);
3828
Stefan Haberland4679e892012-06-19 17:30:12 +02003829void dasd_generic_shutdown(struct ccw_device *cdev)
3830{
3831 struct dasd_device *device;
3832
3833 device = dasd_device_from_cdev(cdev);
3834 if (IS_ERR(device))
3835 return;
3836
3837 if (device->block)
3838 dasd_schedule_block_bh(device->block);
3839
3840 dasd_schedule_device_bh(device);
3841
3842 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3843}
3844EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3845
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003846static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 int rc;
3849
3850 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003851 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02003852 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02003853 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01003856 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 if (dasd_debug_area == NULL) {
3858 rc = -ENOMEM;
3859 goto failed;
3860 }
3861 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02003862 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3865
3866 dasd_diag_discipline_pointer = NULL;
3867
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003868 dasd_statistics_createroot();
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 rc = dasd_devmap_init();
3871 if (rc)
3872 goto failed;
3873 rc = dasd_gendisk_init();
3874 if (rc)
3875 goto failed;
3876 rc = dasd_parse();
3877 if (rc)
3878 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003879 rc = dasd_eer_init();
3880 if (rc)
3881 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882#ifdef CONFIG_PROC_FS
3883 rc = dasd_proc_init();
3884 if (rc)
3885 goto failed;
3886#endif
3887
3888 return 0;
3889failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003890 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 dasd_exit();
3892 return rc;
3893}
3894
3895module_init(dasd_init);
3896module_exit(dasd_exit);