blob: b0c65dcb6865777dfcbae07c76e5be8ee8c14e98 [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 Haberlanda521b042016-08-08 15:56:54 +020072static void do_requeue_requests(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010074static void dasd_device_timeout(unsigned long);
75static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010076static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020077static void dasd_profile_init(struct dasd_profile *, struct dentry *);
78static void dasd_profile_exit(struct dasd_profile *);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +010079static void dasd_hosts_init(struct dentry *, struct dasd_device *);
80static void dasd_hosts_exit(struct dasd_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * SECTION: Operations on the device structure.
84 */
85static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020086static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020087static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020088static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Allocate memory for a new device structure.
92 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010093struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct dasd_device *device;
96
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010097 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
98 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* Get two pages for normal block device operations. */
102 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100103 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kfree(device);
105 return ERR_PTR(-ENOMEM);
106 }
107 /* Get one page for error recovery. */
108 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100109 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 free_pages((unsigned long) device->ccw_mem, 1);
111 kfree(device);
112 return ERR_PTR(-ENOMEM);
113 }
114
115 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
116 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
117 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100118 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200119 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100120 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 (unsigned long) device);
122 INIT_LIST_HEAD(&device->ccw_queue);
123 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100124 device->timer.function = dasd_device_timeout;
125 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000126 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200127 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200128 INIT_WORK(&device->reload_device, do_reload_device);
Stefan Haberlanda521b042016-08-08 15:56:54 +0200129 INIT_WORK(&device->requeue_requests, do_requeue_requests);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 device->state = DASD_STATE_NEW;
131 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100132 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200133 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return device;
135}
136
137/*
138 * Free memory of a device structure.
139 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100140void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800142 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 free_page((unsigned long) device->erp_mem);
144 free_pages((unsigned long) device->ccw_mem, 1);
145 kfree(device);
146}
147
148/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100149 * Allocate memory for a new device structure.
150 */
151struct dasd_block *dasd_alloc_block(void)
152{
153 struct dasd_block *block;
154
155 block = kzalloc(sizeof(*block), GFP_ATOMIC);
156 if (!block)
157 return ERR_PTR(-ENOMEM);
158 /* open_count = 0 means device online but not in use */
159 atomic_set(&block->open_count, -1);
160
161 spin_lock_init(&block->request_queue_lock);
162 atomic_set(&block->tasklet_scheduled, 0);
163 tasklet_init(&block->tasklet,
164 (void (*)(unsigned long)) dasd_block_tasklet,
165 (unsigned long) block);
166 INIT_LIST_HEAD(&block->ccw_queue);
167 spin_lock_init(&block->queue_lock);
168 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100169 block->timer.function = dasd_block_timeout;
170 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200171 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100172
173 return block;
174}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200175EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176
177/*
178 * Free memory of a device structure.
179 */
180void dasd_free_block(struct dasd_block *block)
181{
182 kfree(block);
183}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200184EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100185
186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Make a new device known to the system.
188 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100189static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int rc;
192
193 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200194 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * keep the reference count > 0.
196 */
197 dasd_get_device(device);
198
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100199 if (device->block) {
200 rc = dasd_alloc_queue(device->block);
201 if (rc) {
202 dasd_put_device(device);
203 return rc;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 device->state = DASD_STATE_KNOWN;
207 return 0;
208}
209
210/*
211 * Let the system forget about a device.
212 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100213static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800215 /* Disable extended error reporting for this device. */
216 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 device->state = DASD_STATE_NEW;
218
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 if (device->block)
220 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Give up reference we took in dasd_state_new_to_known. */
223 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200227static struct dentry *dasd_debugfs_setup(const char *name,
228 struct dentry *base_dentry)
229{
230 struct dentry *pde;
231
232 if (!base_dentry)
233 return NULL;
234 pde = debugfs_create_dir(name, base_dentry);
235 if (!pde || IS_ERR(pde))
236 return NULL;
237 return pde;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Request the irq line for the device.
242 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100243static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200245 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200246 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200249 if (block) {
250 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100251 if (rc)
252 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200253 block->debugfs_dentry =
254 dasd_debugfs_setup(block->gdp->disk_name,
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&block->profile, block->debugfs_dentry);
257 if (dasd_global_profile_level == DASD_PROFILE_ON)
258 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100259 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200260 device->debugfs_dentry =
261 dasd_debugfs_setup(dev_name(&device->cdev->dev),
262 dasd_debugfs_root_entry);
263 dasd_profile_init(&device->profile, device->debugfs_dentry);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100264 dasd_hosts_init(device->debugfs_dentry, device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100267 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100268 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200270 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
272
273 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200274
275 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * Release the irq line for the device. Terminate any running i/o.
280 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100281static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Horst Hummel8f617012006-08-30 14:33:33 +0200283 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200284
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200285 if (device->discipline->basic_to_known) {
286 rc = device->discipline->basic_to_known(device);
287 if (rc)
288 return rc;
289 }
290
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100291 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200292 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200293 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294 dasd_gendisk_free(device->block);
295 dasd_block_clear_timer(device->block);
296 }
297 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200298 if (rc)
299 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100300 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200301 dasd_profile_exit(&device->profile);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100302 dasd_hosts_exit(device);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200303 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
305 if (device->debug_area != NULL) {
306 debug_unregister(device->debug_area);
307 device->debug_area = NULL;
308 }
309 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
314 * Do the initial analysis. The do_analysis function may return
315 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
316 * until the discipline decides to continue the startup sequence
317 * by calling the function dasd_change_state. The eckd disciplines
318 * uses this to start a ccw that detects the format. The completion
319 * interrupt for this detection ccw uses the kernel event daemon to
320 * trigger the call to dasd_change_state. All this is done in the
321 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800322 * After the analysis ccw is done (do_analysis returned 0) the block
323 * device is setup.
324 * In case the analysis returns an error, the device setup is stopped
325 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100330 struct dasd_block *block;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200331 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800335 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336 if (block) {
337 if (block->base->discipline->do_analysis != NULL)
338 rc = block->base->discipline->do_analysis(block);
339 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200340 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100341 device->state = DASD_STATE_UNFMT;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200342 disk = device->block->gdp;
343 kobject_uevent(&disk_to_dev(disk)->kobj,
344 KOBJ_CHANGE);
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
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001153static int dasd_hosts_show(struct seq_file *m, void *v)
1154{
1155 struct dasd_device *device;
1156 int rc = -EOPNOTSUPP;
1157
1158 device = m->private;
1159 dasd_get_device(device);
1160
1161 if (device->discipline->hosts_print)
1162 rc = device->discipline->hosts_print(device, m);
1163
1164 dasd_put_device(device);
1165 return rc;
1166}
1167
1168static int dasd_hosts_open(struct inode *inode, struct file *file)
1169{
1170 struct dasd_device *device = inode->i_private;
1171
1172 return single_open(file, dasd_hosts_show, device);
1173}
1174
1175static const struct file_operations dasd_hosts_fops = {
1176 .owner = THIS_MODULE,
1177 .open = dasd_hosts_open,
1178 .read = seq_read,
1179 .llseek = seq_lseek,
1180 .release = single_release,
1181};
1182
1183static void dasd_hosts_exit(struct dasd_device *device)
1184{
1185 debugfs_remove(device->hosts_dentry);
1186 device->hosts_dentry = NULL;
1187}
1188
1189static void dasd_hosts_init(struct dentry *base_dentry,
1190 struct dasd_device *device)
1191{
1192 struct dentry *pde;
1193 umode_t mode;
1194
1195 if (!base_dentry)
1196 return;
1197
1198 mode = S_IRUSR | S_IFREG;
1199 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1200 device, &dasd_hosts_fops);
1201 if (pde && !IS_ERR(pde))
1202 device->hosts_dentry = pde;
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*
1206 * Allocate memory for a channel program with 'cplength' channel
1207 * command words and 'datasize' additional space. There are two
1208 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1209 * memory and 2) dasd_smalloc_request uses the static ccw memory
1210 * that gets allocated for each device.
1211 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001212struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001213 int datasize,
1214 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct dasd_ccw_req *cqr;
1217
1218 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001219 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001220 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001222 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (cqr == NULL)
1224 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 cqr->cpaddr = NULL;
1226 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001227 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 GFP_ATOMIC | GFP_DMA);
1229 if (cqr->cpaddr == NULL) {
1230 kfree(cqr);
1231 return ERR_PTR(-ENOMEM);
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 cqr->data = NULL;
1235 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001236 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001238 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 kfree(cqr);
1240 return ERR_PTR(-ENOMEM);
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001243 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1245 dasd_get_device(device);
1246 return cqr;
1247}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001248EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Stefan Haberland68b781f2009-09-11 10:28:29 +02001250struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001251 int datasize,
1252 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 unsigned long flags;
1255 struct dasd_ccw_req *cqr;
1256 char *data;
1257 int size;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1260 if (cplength > 0)
1261 size += cplength * sizeof(struct ccw1);
1262 if (datasize > 0)
1263 size += datasize;
1264 spin_lock_irqsave(&device->mem_lock, flags);
1265 cqr = (struct dasd_ccw_req *)
1266 dasd_alloc_chunk(&device->ccw_chunks, size);
1267 spin_unlock_irqrestore(&device->mem_lock, flags);
1268 if (cqr == NULL)
1269 return ERR_PTR(-ENOMEM);
1270 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1271 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1272 cqr->cpaddr = NULL;
1273 if (cplength > 0) {
1274 cqr->cpaddr = (struct ccw1 *) data;
1275 data += cplength*sizeof(struct ccw1);
1276 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1277 }
1278 cqr->data = NULL;
1279 if (datasize > 0) {
1280 cqr->data = data;
1281 memset(cqr->data, 0, datasize);
1282 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001283 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1285 dasd_get_device(device);
1286 return cqr;
1287}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001288EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290/*
1291 * Free memory of a channel program. This function needs to free all the
1292 * idal lists that might have been created by dasd_set_cda and the
1293 * struct dasd_ccw_req itself.
1294 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001295void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct ccw1 *ccw;
1298
1299 /* Clear any idals used for the request. */
1300 ccw = cqr->cpaddr;
1301 do {
1302 clear_normalized_cda(ccw);
1303 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
Jesper Juhl17fd6822005-11-07 01:01:30 -08001304 kfree(cqr->cpaddr);
1305 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(cqr);
1307 dasd_put_device(device);
1308}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001309EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001311void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 unsigned long flags;
1314
1315 spin_lock_irqsave(&device->mem_lock, flags);
1316 dasd_free_chunk(&device->ccw_chunks, cqr);
1317 spin_unlock_irqrestore(&device->mem_lock, flags);
1318 dasd_put_device(device);
1319}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001320EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322/*
1323 * Check discipline magic in cqr.
1324 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001325static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct dasd_device *device;
1328
1329 if (cqr == NULL)
1330 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001331 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001333 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 " dasd_ccw_req 0x%08x magic doesn't match"
1335 " discipline 0x%08x",
1336 cqr->magic,
1337 *(unsigned int *) device->discipline->name);
1338 return -EINVAL;
1339 }
1340 return 0;
1341}
1342
1343/*
1344 * Terminate the current i/o and set the request to clear_pending.
1345 * Timer keeps device runnig.
1346 * ccw_device_clear can fail if the i/o subsystem
1347 * is in a bad mood.
1348 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001349int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct dasd_device *device;
1352 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001353 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /* Check the cqr */
1356 rc = dasd_check_cqr(cqr);
1357 if (rc)
1358 return rc;
1359 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001360 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1362 rc = ccw_device_clear(device->cdev, (long) cqr);
1363 switch (rc) {
1364 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001365 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001366 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001367 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 DBF_DEV_EVENT(DBF_DEBUG, device,
1369 "terminate cqr %p successful",
1370 cqr);
1371 break;
1372 case -ENODEV:
1373 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1374 "device gone, retry");
1375 break;
1376 case -EIO:
1377 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1378 "I/O error, retry");
1379 break;
1380 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001381 /*
1382 * device not valid so no I/O could be running
1383 * handle CQR as termination successful
1384 */
1385 cqr->status = DASD_CQR_CLEARED;
1386 cqr->stopclk = get_tod_clock();
1387 cqr->starttime = 0;
1388 /* no retries for invalid devices */
1389 cqr->retries = -1;
1390 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1391 "EINVAL, handle as terminated");
1392 /* fake rc to success */
1393 rc = 0;
1394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 case -EBUSY:
1396 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1397 "device busy, retry later");
1398 break;
1399 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001400 /* internal error 10 - unknown rc*/
1401 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1402 dev_err(&device->cdev->dev, "An error occurred in the "
1403 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 BUG();
1405 break;
1406 }
1407 retries++;
1408 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001409 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return rc;
1411}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001412EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414/*
1415 * Start the i/o. This start_IO can fail if the channel is really busy.
1416 * In that case set up a timer to start the request later.
1417 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001418int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 struct dasd_device *device;
1421 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001422 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /* Check the cqr */
1425 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001426 if (rc) {
1427 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001429 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001430 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001431 if (((cqr->block &&
1432 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1433 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1434 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1435 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1436 "because of stolen lock", cqr);
1437 cqr->status = DASD_CQR_ERROR;
1438 cqr->intrc = -EPERM;
1439 return -EPERM;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001442 /* internal error 14 - start_IO run out of retries */
1443 sprintf(errorstring, "14 %p", cqr);
1444 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1445 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001446 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EIO;
1448 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001449 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 cqr->starttime = jiffies;
1451 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001452 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001453 cqr->lpm &= dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001454 if (!cqr->lpm)
Stefan Haberlandc9346152016-08-08 15:53:54 +02001455 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001456 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001457 if (cqr->cpmode == 1) {
1458 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1459 (long) cqr, cqr->lpm);
1460 } else {
1461 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1462 (long) cqr, cqr->lpm, 0);
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 switch (rc) {
1465 case 0:
1466 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 break;
1468 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001469 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 "start_IO: device busy, retry later");
1471 break;
1472 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001473 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 "start_IO: request timeout, retry later");
1475 break;
1476 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001477 /* -EACCES indicates that the request used only a subset of the
1478 * available paths and all these paths are gone. If the lpm of
1479 * this request was only a subset of the opm (e.g. the ppm) then
1480 * we just do a retry with all available paths.
1481 * If we already use the full opm, something is amiss, and we
1482 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001484 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1485 DBF_DEV_EVENT(DBF_WARNING, device,
1486 "start_IO: selected paths gone (%x)",
1487 cqr->lpm);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001488 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1489 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001490 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1491 "start_IO: selected paths gone,"
1492 " retry on all paths");
1493 } else {
1494 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1495 "start_IO: all paths in opm gone,"
1496 " do path verification");
1497 dasd_generic_last_path_gone(device);
Stefan Haberlandc9346152016-08-08 15:53:54 +02001498 dasd_path_no_path(device);
1499 dasd_path_set_tbvpm(device,
1500 ccw_device_get_path_mask(
1501 device->cdev));
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 break;
1504 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001505 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001506 "start_IO: -ENODEV device gone, retry");
1507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001509 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001510 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001512 case -EINVAL:
1513 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001514 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001515 "start_IO: -EINVAL device currently "
1516 "not accessible");
1517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001519 /* internal error 11 - unknown rc */
1520 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1521 dev_err(&device->cdev->dev,
1522 "An error occurred in the DASD device driver, "
1523 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 BUG();
1525 break;
1526 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001527 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return rc;
1529}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001530EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532/*
1533 * Timeout function for dasd devices. This is used for different purposes
1534 * 1) missing interrupt handler for normal operation
1535 * 2) delayed start of request where start_IO failed with -EBUSY
1536 * 3) timeout for missing state change interrupts
1537 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1538 * DASD_CQR_QUEUED for 2) and 3).
1539 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001540static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 unsigned long flags;
1543 struct dasd_device *device;
1544
1545 device = (struct dasd_device *) ptr;
1546 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1547 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001548 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001550 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553/*
1554 * Setup timeout for a device in jiffies.
1555 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001556void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001558 if (expires == 0)
1559 del_timer(&device->timer);
1560 else
1561 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001563EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565/*
1566 * Clear timeout for a device.
1567 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001568void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001570 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001572EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001574static void dasd_handle_killed_request(struct ccw_device *cdev,
1575 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct dasd_ccw_req *cqr;
1578 struct dasd_device *device;
1579
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001580 if (!intparm)
1581 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 cqr = (struct dasd_ccw_req *) intparm;
1583 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001584 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1585 "invalid status in handle_killed_request: "
1586 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return;
1588 }
1589
Stefan Haberland589c74d2010-02-26 22:37:47 +01001590 device = dasd_device_from_cdev_locked(cdev);
1591 if (IS_ERR(device)) {
1592 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1593 "unable to get device from cdev");
1594 return;
1595 }
1596
1597 if (!cqr->startdev ||
1598 device != cqr->startdev ||
1599 strncmp(cqr->startdev->discipline->ebcname,
1600 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001601 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1602 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001603 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return;
1605 }
1606
1607 /* Schedule request to be retried. */
1608 cqr->status = DASD_CQR_QUEUED;
1609
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001610 dasd_device_clear_timer(device);
1611 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 dasd_put_device(device);
1613}
1614
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001615void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001617 /* First of all start sense subsystem status request. */
1618 dasd_eer_snss(device);
1619
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001620 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001621 dasd_schedule_device_bh(device);
1622 if (device->block)
1623 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001625EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Stefan Haberlanda521b042016-08-08 15:56:54 +02001627static int dasd_check_hpf_error(struct irb *irb)
1628{
1629 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1630 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1631 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/*
1635 * Interrupt handler for "normal" ssch-io based dasd devices.
1636 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001637void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1638 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct dasd_ccw_req *cqr, *next;
1641 struct dasd_device *device;
1642 unsigned long long now;
Jan Höppner8fd57522015-08-19 13:41:20 +02001643 int nrf_suppressed = 0;
1644 int fp_suppressed = 0;
1645 u8 *sense = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001648 cqr = (struct dasd_ccw_req *) intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (IS_ERR(irb)) {
1650 switch (PTR_ERR(irb)) {
1651 case -EIO:
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001652 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001653 device = cqr->startdev;
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001654 cqr->status = DASD_CQR_CLEARED;
1655 dasd_device_clear_timer(device);
1656 wake_up(&dasd_flush_wq);
1657 dasd_schedule_device_bh(device);
1658 return;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001662 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1663 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001666 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1667 "unknown error %ld\n", __func__,
1668 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001670 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return;
1672 }
1673
Heiko Carstens1aae0562013-01-30 09:49:40 +01001674 now = get_tod_clock();
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001675 /* check for conditions that should be handled immediately */
1676 if (!cqr ||
1677 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1678 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001679 if (cqr)
1680 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001681 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001682 if (IS_ERR(device))
1683 return;
1684 /* ignore unsolicited interrupts for DIAG discipline */
1685 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001689
1690 /*
1691 * In some cases 'File Protected' or 'No Record Found' errors
1692 * might be expected and debug log messages for the
1693 * corresponding interrupts shouldn't be written then.
1694 * Check if either of the according suppress bits is set.
1695 */
1696 sense = dasd_get_sense(irb);
1697 if (sense) {
1698 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1699 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1700 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1701 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1702 }
1703 if (!(fp_suppressed || nrf_suppressed))
1704 device->discipline->dump_sense_dbf(device, irb, "int");
1705
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001706 if (device->features & DASD_FEATURE_ERPLOG)
1707 device->discipline->dump_sense(device, cqr, irb);
1708 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001709 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001711
1712 /* check for for attention message */
1713 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1714 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland22021342017-01-25 14:47:32 +01001715 if (!IS_ERR(device)) {
1716 device->discipline->check_attention(device,
1717 irb->esw.esw1.lpum);
1718 dasd_put_device(device);
1719 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001720 }
1721
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001722 if (!cqr)
1723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001725 device = (struct dasd_device *) cqr->startdev;
1726 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001728 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1729 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return;
1731 }
1732
1733 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001734 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001735 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001736 cqr->status = DASD_CQR_CLEARED;
1737 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001738 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001739 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return;
1741 }
1742
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001743 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001745 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1746 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return;
1748 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001749
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001750 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001752 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1753 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001754 /* request was completed successfully */
1755 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 cqr->stopclk = now;
1757 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001758 if (cqr->devlist.next != &device->ccw_queue) {
1759 next = list_entry(cqr->devlist.next,
1760 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 } else { /* error */
Stefan Haberlanda521b042016-08-08 15:56:54 +02001763 /* check for HPF error
1764 * call discipline function to requeue all requests
1765 * and disable HPF accordingly
1766 */
1767 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1768 device->discipline->handle_hpf_error)
1769 device->discipline->handle_hpf_error(device, irb);
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001770 /*
1771 * If we don't want complex ERP for this request, then just
1772 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001773 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001774 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001775 cqr->retries > 0) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02001776 if (cqr->lpm == dasd_path_get_opm(device))
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001777 DBF_DEV_EVENT(DBF_DEBUG, device,
1778 "default ERP in fastpath "
1779 "(%i retries left)",
1780 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001781 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
Stefan Haberlandc9346152016-08-08 15:53:54 +02001782 cqr->lpm = dasd_path_get_opm(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001783 cqr->status = DASD_CQR_QUEUED;
1784 next = cqr;
1785 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001787 }
1788 if (next && (next->status == DASD_CQR_QUEUED) &&
1789 (!device->stopped)) {
1790 if (device->discipline->start_IO(next) == 0)
1791 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001794 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001796 dasd_device_clear_timer(device);
1797 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001799EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001801enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1802{
1803 struct dasd_device *device;
1804
1805 device = dasd_device_from_cdev_locked(cdev);
1806
1807 if (IS_ERR(device))
1808 goto out;
1809 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1810 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001811 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001812 dasd_put_device(device);
1813 goto out;
1814 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001815 if (device->discipline->dump_sense_dbf)
1816 device->discipline->dump_sense_dbf(device, irb, "uc");
1817 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001818 dasd_put_device(device);
1819out:
1820 return UC_TODO_RETRY;
1821}
1822EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001825 * If we have an error on a dasd_block layer request then we cancel
1826 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001828static void __dasd_device_recovery(struct dasd_device *device,
1829 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct list_head *l, *n;
1832 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001835 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001837 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001839
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001840 list_for_each_safe(l, n, &device->ccw_queue) {
1841 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1842 if (cqr->status == DASD_CQR_QUEUED &&
1843 ref_cqr->block == cqr->block) {
1844 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001846 }
1847};
1848
1849/*
1850 * Remove those ccw requests from the queue that need to be returned
1851 * to the upper layer.
1852 */
1853static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1854 struct list_head *final_queue)
1855{
1856 struct list_head *l, *n;
1857 struct dasd_ccw_req *cqr;
1858
1859 /* Process request with final status. */
1860 list_for_each_safe(l, n, &device->ccw_queue) {
1861 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1862
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001863 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001864 if (cqr->status == DASD_CQR_QUEUED ||
1865 cqr->status == DASD_CQR_IN_IO ||
1866 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001867 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001868 if (cqr->status == DASD_CQR_ERROR) {
1869 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001871 /* Rechain finished requests to final queue */
1872 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874}
1875
1876/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001877 * the cqrs from the final queue are returned to the upper layer
1878 * by setting a dasd_block state and calling the callback function
1879 */
1880static void __dasd_device_process_final_queue(struct dasd_device *device,
1881 struct list_head *final_queue)
1882{
1883 struct list_head *l, *n;
1884 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001885 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001886 void (*callback)(struct dasd_ccw_req *, void *data);
1887 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001888 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001889
1890 list_for_each_safe(l, n, final_queue) {
1891 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1892 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001893 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001894 callback = cqr->callback;
1895 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001896 if (block)
1897 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001898 switch (cqr->status) {
1899 case DASD_CQR_SUCCESS:
1900 cqr->status = DASD_CQR_DONE;
1901 break;
1902 case DASD_CQR_ERROR:
1903 cqr->status = DASD_CQR_NEED_ERP;
1904 break;
1905 case DASD_CQR_CLEARED:
1906 cqr->status = DASD_CQR_TERMINATED;
1907 break;
1908 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001909 /* internal error 12 - wrong cqr status*/
1910 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1911 dev_err(&device->cdev->dev,
1912 "An error occurred in the DASD device driver, "
1913 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001914 BUG();
1915 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001916 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001917 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001918 if (block)
1919 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001920 }
1921}
1922
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001923/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * Take a look at the first request on the ccw queue and check
1925 * if it reached its expire time. If so, terminate the IO.
1926 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001927static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct dasd_ccw_req *cqr;
1930
1931 if (list_empty(&device->ccw_queue))
1932 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001933 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001934 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1935 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001936 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1937 /*
1938 * IO in safe offline processing should not
1939 * run out of retries
1940 */
1941 cqr->retries++;
1942 }
Horst Hummel29145a62006-12-04 15:40:15 +01001943 if (device->discipline->term_IO(cqr) != 0) {
1944 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001945 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001946 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001947 "ended, retrying in 5 s\n",
1948 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001949 cqr->expires += 5*HZ;
1950 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001951 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001952 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001953 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001954 "remaining\n", cqr, (cqr->expires/HZ),
1955 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 }
1958}
1959
1960/*
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001961 * return 1 when device is not eligible for IO
1962 */
1963static int __dasd_device_is_unusable(struct dasd_device *device,
1964 struct dasd_ccw_req *cqr)
1965{
1966 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1967
1968 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1969 /* dasd is being set offline. */
1970 return 1;
1971 }
1972 if (device->stopped) {
1973 if (device->stopped & mask) {
1974 /* stopped and CQR will not change that. */
1975 return 1;
1976 }
1977 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1978 /* CQR is not able to change device to
1979 * operational. */
1980 return 1;
1981 }
1982 /* CQR required to get device operational. */
1983 }
1984 return 0;
1985}
1986
1987/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * Take a look at the first request on the ccw queue and check
1989 * if it needs to be started.
1990 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001991static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct dasd_ccw_req *cqr;
1994 int rc;
1995
1996 if (list_empty(&device->ccw_queue))
1997 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001998 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001999 if (cqr->status != DASD_CQR_QUEUED)
2000 return;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02002001 /* if device is not usable return request to upper layer */
2002 if (__dasd_device_is_unusable(device, cqr)) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002003 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002004 cqr->status = DASD_CQR_CLEARED;
2005 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002006 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002007 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002008
2009 rc = device->discipline->start_IO(cqr);
2010 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002011 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002012 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002013 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07002014 } else
2015 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002016 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02002017}
2018
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002019static void __dasd_device_check_path_events(struct dasd_device *device)
2020{
2021 int rc;
2022
Stefan Haberlandc9346152016-08-08 15:53:54 +02002023 if (!dasd_path_get_tbvpm(device))
2024 return;
2025
2026 if (device->stopped &
2027 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2028 return;
2029 rc = device->discipline->verify_path(device,
2030 dasd_path_get_tbvpm(device));
2031 if (rc)
2032 dasd_device_set_timer(device, 50);
2033 else
2034 dasd_path_clear_all_verify(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002035};
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002038 * Go through all request on the dasd_device request queue,
2039 * terminate them on the cdev if necessary, and return them to the
2040 * submitting layer via callback.
2041 * Note:
2042 * Make sure that all 'submitting layers' still exist when
2043 * this function is called!. In other words, when 'device' is a base
2044 * device then all block layer requests must have been removed before
2045 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002047int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002049 struct dasd_ccw_req *cqr, *n;
2050 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 INIT_LIST_HEAD(&flush_queue);
2054 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02002055 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002056 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02002057 /* Check status and move request to flush_queue */
2058 switch (cqr->status) {
2059 case DASD_CQR_IN_IO:
2060 rc = device->discipline->term_IO(cqr);
2061 if (rc) {
2062 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002063 dev_err(&device->cdev->dev,
2064 "Flushing the DASD request queue "
2065 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002066 /* stop flush processing */
2067 goto finished;
2068 }
2069 break;
2070 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002071 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002073 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002074 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002075 break;
2076 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002077 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002078 }
Horst Hummel8f617012006-08-30 14:33:33 +02002079finished:
2080 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002081 /*
2082 * After this point all requests must be in state CLEAR_PENDING,
2083 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2084 * one of the others.
2085 */
2086 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2087 wait_event(dasd_flush_wq,
2088 (cqr->status != DASD_CQR_CLEAR_PENDING));
2089 /*
2090 * Now set each request back to TERMINATED, DONE or NEED_ERP
2091 * and call the callback function of flushed requests
2092 */
2093 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002094 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002096EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098/*
2099 * Acquire the device lock and process queues for the device.
2100 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002101static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 atomic_set (&device->tasklet_scheduled, 0);
2106 INIT_LIST_HEAD(&final_queue);
2107 spin_lock_irq(get_ccwdev_lock(device->cdev));
2108 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002109 __dasd_device_check_expire(device);
2110 /* find final requests on ccw queue */
2111 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002112 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2114 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002115 __dasd_device_process_final_queue(device, &final_queue);
2116 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002118 __dasd_device_start_head(device);
2119 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002120 if (waitqueue_active(&shutdown_waitq))
2121 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 dasd_put_device(device);
2123}
2124
2125/*
2126 * Schedules a call to dasd_tasklet over the device tasklet.
2127 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002128void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002131 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return;
2133 dasd_get_device(device);
2134 tasklet_hi_schedule(&device->tasklet);
2135}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002136EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002138void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2139{
2140 device->stopped |= bits;
2141}
2142EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2143
2144void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2145{
2146 device->stopped &= ~bits;
2147 if (!device->stopped)
2148 wake_up(&generic_waitq);
2149}
2150EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002153 * Queue a request to the head of the device ccw_queue.
2154 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002156void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 struct dasd_device *device;
2159 unsigned long flags;
2160
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002161 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002163 cqr->status = DASD_CQR_QUEUED;
2164 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002166 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2168}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002169EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002172 * Queue a request to the tail of the device ccw_queue.
2173 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002175void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 struct dasd_device *device;
2178 unsigned long flags;
2179
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002180 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002182 cqr->status = DASD_CQR_QUEUED;
2183 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002185 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2187}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002188EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002191 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002193void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002195 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2196 cqr->callback_data = DASD_SLEEPON_END_TAG;
2197 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2198 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
Stefan Haberland5915a872011-10-30 15:16:57 +01002200EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002202static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 struct dasd_device *device;
2205 int rc;
2206
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002207 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002209 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2211 return rc;
2212}
2213
2214/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002215 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2216 */
2217static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2218{
2219 struct dasd_device *device;
2220 dasd_erp_fn_t erp_fn;
2221
2222 if (cqr->status == DASD_CQR_FILLED)
2223 return 0;
2224 device = cqr->startdev;
2225 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2226 if (cqr->status == DASD_CQR_TERMINATED) {
2227 device->discipline->handle_terminated_request(cqr);
2228 return 1;
2229 }
2230 if (cqr->status == DASD_CQR_NEED_ERP) {
2231 erp_fn = device->discipline->erp_action(cqr);
2232 erp_fn(cqr);
2233 return 1;
2234 }
2235 if (cqr->status == DASD_CQR_FAILED)
2236 dasd_log_sense(cqr, &cqr->irb);
2237 if (cqr->refers) {
2238 __dasd_process_erp(device, cqr);
2239 return 1;
2240 }
2241 }
2242 return 0;
2243}
2244
2245static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2246{
2247 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2248 if (cqr->refers) /* erp is not done yet */
2249 return 1;
2250 return ((cqr->status != DASD_CQR_DONE) &&
2251 (cqr->status != DASD_CQR_FAILED));
2252 } else
2253 return (cqr->status == DASD_CQR_FILLED);
2254}
2255
2256static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2257{
2258 struct dasd_device *device;
2259 int rc;
2260 struct list_head ccw_queue;
2261 struct dasd_ccw_req *cqr;
2262
2263 INIT_LIST_HEAD(&ccw_queue);
2264 maincqr->status = DASD_CQR_FILLED;
2265 device = maincqr->startdev;
2266 list_add(&maincqr->blocklist, &ccw_queue);
2267 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2268 cqr = list_first_entry(&ccw_queue,
2269 struct dasd_ccw_req, blocklist)) {
2270
2271 if (__dasd_sleep_on_erp(cqr))
2272 continue;
2273 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2274 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002275 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2276 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2277 cqr->status = DASD_CQR_FAILED;
2278 cqr->intrc = -EPERM;
2279 continue;
2280 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002281 /* Non-temporary stop condition will trigger fail fast */
2282 if (device->stopped & ~DASD_STOPPED_PENDING &&
2283 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2284 (!dasd_eer_enabled(device))) {
2285 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002286 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002287 continue;
2288 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002289 /*
Stefan Haberlanda9f62732016-09-20 10:29:22 +02002290 * Don't try to start requests if device is in
2291 * offline processing, it might wait forever
2292 */
2293 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2294 cqr->status = DASD_CQR_FAILED;
2295 cqr->intrc = -ENODEV;
2296 continue;
2297 }
2298 /*
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002299 * Don't try to start requests if device is stopped
2300 * except path verification requests
2301 */
2302 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2303 if (interruptible) {
2304 rc = wait_event_interruptible(
2305 generic_waitq, !(device->stopped));
2306 if (rc == -ERESTARTSYS) {
2307 cqr->status = DASD_CQR_FAILED;
2308 maincqr->intrc = rc;
2309 continue;
2310 }
2311 } else
2312 wait_event(generic_waitq, !(device->stopped));
2313 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002314 if (!cqr->callback)
2315 cqr->callback = dasd_wakeup_cb;
2316
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002317 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002318 dasd_add_request_tail(cqr);
2319 if (interruptible) {
2320 rc = wait_event_interruptible(
2321 generic_waitq, _wait_for_wakeup(cqr));
2322 if (rc == -ERESTARTSYS) {
2323 dasd_cancel_req(cqr);
2324 /* wait (non-interruptible) for final status */
2325 wait_event(generic_waitq,
2326 _wait_for_wakeup(cqr));
2327 cqr->status = DASD_CQR_FAILED;
2328 maincqr->intrc = rc;
2329 continue;
2330 }
2331 } else
2332 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2333 }
2334
Heiko Carstens1aae0562013-01-30 09:49:40 +01002335 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002336 if ((maincqr->status != DASD_CQR_DONE) &&
2337 (maincqr->intrc != -ERESTARTSYS))
2338 dasd_log_sense(maincqr, &maincqr->irb);
2339 if (maincqr->status == DASD_CQR_DONE)
2340 rc = 0;
2341 else if (maincqr->intrc)
2342 rc = maincqr->intrc;
2343 else
2344 rc = -EIO;
2345 return rc;
2346}
2347
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002348static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2349{
2350 struct dasd_ccw_req *cqr;
2351
2352 list_for_each_entry(cqr, ccw_queue, blocklist) {
2353 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2354 return 0;
2355 }
2356
2357 return 1;
2358}
2359
2360static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2361{
2362 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002363 struct dasd_ccw_req *cqr, *n;
Jan Höppner8fd57522015-08-19 13:41:20 +02002364 u8 *sense = NULL;
Stefan Haberland362ce842014-10-01 13:04:54 +02002365 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002366
2367retry:
2368 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2369 device = cqr->startdev;
2370 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2371 continue;
2372
2373 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2374 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2375 cqr->status = DASD_CQR_FAILED;
2376 cqr->intrc = -EPERM;
2377 continue;
2378 }
2379 /*Non-temporary stop condition will trigger fail fast*/
2380 if (device->stopped & ~DASD_STOPPED_PENDING &&
2381 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2382 !dasd_eer_enabled(device)) {
2383 cqr->status = DASD_CQR_FAILED;
2384 cqr->intrc = -EAGAIN;
2385 continue;
2386 }
2387
2388 /*Don't try to start requests if device is stopped*/
2389 if (interruptible) {
2390 rc = wait_event_interruptible(
2391 generic_waitq, !device->stopped);
2392 if (rc == -ERESTARTSYS) {
2393 cqr->status = DASD_CQR_FAILED;
2394 cqr->intrc = rc;
2395 continue;
2396 }
2397 } else
2398 wait_event(generic_waitq, !(device->stopped));
2399
2400 if (!cqr->callback)
2401 cqr->callback = dasd_wakeup_cb;
2402 cqr->callback_data = DASD_SLEEPON_START_TAG;
2403 dasd_add_request_tail(cqr);
2404 }
2405
2406 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2407
2408 rc = 0;
2409 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002410 /*
Jan Höppner8fd57522015-08-19 13:41:20 +02002411 * In some cases the 'File Protected' or 'Incorrect Length'
2412 * error might be expected and error recovery would be
2413 * unnecessary in these cases. Check if the according suppress
2414 * bit is set.
2415 */
2416 sense = dasd_get_sense(&cqr->irb);
2417 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2418 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2419 continue;
2420 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2421 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2422 continue;
2423
2424 /*
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002425 * for alias devices simplify error recovery and
2426 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002427 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002428 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002429 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002430 (cqr->status == DASD_CQR_TERMINATED ||
2431 cqr->status == DASD_CQR_NEED_ERP))
2432 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002433
2434 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002435 if (__dasd_sleep_on_erp(cqr))
2436 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002437 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002438 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002439
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002440 return 0;
2441}
2442
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002443/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002444 * Queue a request to the tail of the device ccw_queue and wait for
2445 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002447int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002449 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002451EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002454 * Start requests from a ccw_queue and wait for their completion.
2455 */
2456int dasd_sleep_on_queue(struct list_head *ccw_queue)
2457{
2458 return _dasd_sleep_on_queue(ccw_queue, 0);
2459}
2460EXPORT_SYMBOL(dasd_sleep_on_queue);
2461
2462/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002463 * Queue a request to the tail of the device ccw_queue and wait
2464 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002466int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002468 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002470EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472/*
2473 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2474 * for eckd devices) the currently running request has to be terminated
2475 * and be put back to status queued, before the special request is added
2476 * to the head of the queue. Then the special request is waited on normally.
2477 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002478static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002481 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 if (list_empty(&device->ccw_queue))
2484 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002485 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002486 rc = device->discipline->term_IO(cqr);
2487 if (!rc)
2488 /*
2489 * CQR terminated because a more important request is pending.
2490 * Undo decreasing of retry counter because this is
2491 * not an error case.
2492 */
2493 cqr->retries++;
2494 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002497int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct dasd_device *device;
2500 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002501
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002502 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002503 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2504 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2505 cqr->status = DASD_CQR_FAILED;
2506 cqr->intrc = -EPERM;
2507 return -EIO;
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 spin_lock_irq(get_ccwdev_lock(device->cdev));
2510 rc = _dasd_term_running_cqr(device);
2511 if (rc) {
2512 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2513 return rc;
2514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002516 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002518 /*
2519 * add new request as second
2520 * first the terminated cqr needs to be finished
2521 */
2522 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002525 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2528
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002529 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002530
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002531 if (cqr->status == DASD_CQR_DONE)
2532 rc = 0;
2533 else if (cqr->intrc)
2534 rc = cqr->intrc;
2535 else
2536 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002537
2538 /* kick tasklets */
2539 dasd_schedule_device_bh(device);
2540 if (device->block)
2541 dasd_schedule_block_bh(device->block);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return rc;
2544}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002545EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547/*
2548 * Cancels a request that was started with dasd_sleep_on_req.
2549 * This is useful to timeout requests. The request will be
2550 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002551 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002552 * negative error code if termination failed
2553 * Cancellation of a request is an asynchronous operation! The calling
2554 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002556int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002558 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 unsigned long flags;
2560 int rc;
2561
2562 rc = 0;
2563 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2564 switch (cqr->status) {
2565 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002566 /* request was not started - just set to cleared */
2567 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002568 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2569 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 break;
2571 case DASD_CQR_IN_IO:
2572 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002573 rc = device->discipline->term_IO(cqr);
2574 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002575 dev_err(&device->cdev->dev,
2576 "Cancelling request %p failed with rc=%d\n",
2577 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002578 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002579 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002582 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002586 dasd_schedule_device_bh(device);
2587 return rc;
2588}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002589EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002590
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002591/*
2592 * SECTION: Operations of the dasd_block layer.
2593 */
2594
2595/*
2596 * Timeout function for dasd_block. This is used when the block layer
2597 * is waiting for something that may not come reliably, (e.g. a state
2598 * change interrupt)
2599 */
2600static void dasd_block_timeout(unsigned long ptr)
2601{
2602 unsigned long flags;
2603 struct dasd_block *block;
2604
2605 block = (struct dasd_block *) ptr;
2606 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2607 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002608 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002609 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2610 dasd_schedule_block_bh(block);
2611}
2612
2613/*
2614 * Setup timeout for a dasd_block in jiffies.
2615 */
2616void dasd_block_set_timer(struct dasd_block *block, int expires)
2617{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002618 if (expires == 0)
2619 del_timer(&block->timer);
2620 else
2621 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002622}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002623EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002624
2625/*
2626 * Clear timeout for a dasd_block.
2627 */
2628void dasd_block_clear_timer(struct dasd_block *block)
2629{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002630 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002631}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002632EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002633
2634/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002635 * Process finished error recovery ccw.
2636 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002637static void __dasd_process_erp(struct dasd_device *device,
2638 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002639{
2640 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002641
2642 if (cqr->status == DASD_CQR_DONE)
2643 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2644 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002645 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002646 erp_fn = device->discipline->erp_postaction(cqr);
2647 erp_fn(cqr);
2648}
2649
2650/*
2651 * Fetch requests from the block device queue.
2652 */
2653static void __dasd_process_request_queue(struct dasd_block *block)
2654{
2655 struct request_queue *queue;
2656 struct request *req;
2657 struct dasd_ccw_req *cqr;
2658 struct dasd_device *basedev;
2659 unsigned long flags;
2660 queue = block->request_queue;
2661 basedev = block->base;
2662 /* No queue ? Then there is nothing to do. */
2663 if (queue == NULL)
2664 return;
2665
2666 /*
2667 * We requeue request from the block device queue to the ccw
2668 * queue only in two states. In state DASD_STATE_READY the
2669 * partition detection is done and we need to requeue requests
2670 * for that. State DASD_STATE_ONLINE is normal block device
2671 * operation.
2672 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002673 if (basedev->state < DASD_STATE_READY) {
2674 while ((req = blk_fetch_request(block->request_queue)))
2675 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002676 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002677 }
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002678
Stefan Haberlandc6fc7b62015-12-22 13:34:38 +01002679 /*
2680 * if device is stopped do not fetch new requests
2681 * except failfast is active which will let requests fail
2682 * immediately in __dasd_block_start_head()
2683 */
2684 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST))
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002685 return;
2686
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002687 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002688 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002689 if (basedev->features & DASD_FEATURE_READONLY &&
2690 rq_data_dir(req) == WRITE) {
2691 DBF_DEV_EVENT(DBF_ERR, basedev,
2692 "Rejecting write request %p",
2693 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002694 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002695 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002696 continue;
2697 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002698 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2699 (basedev->features & DASD_FEATURE_FAILFAST ||
2700 blk_noretry_request(req))) {
2701 DBF_DEV_EVENT(DBF_ERR, basedev,
2702 "Rejecting failfast request %p",
2703 req);
2704 blk_start_request(req);
2705 __blk_end_request_all(req, -ETIMEDOUT);
2706 continue;
2707 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002708 cqr = basedev->discipline->build_cp(basedev, block, req);
2709 if (IS_ERR(cqr)) {
2710 if (PTR_ERR(cqr) == -EBUSY)
2711 break; /* normal end condition */
2712 if (PTR_ERR(cqr) == -ENOMEM)
2713 break; /* terminate request queue loop */
2714 if (PTR_ERR(cqr) == -EAGAIN) {
2715 /*
2716 * The current request cannot be build right
2717 * now, we have to try later. If this request
2718 * is the head-of-queue we stop the device
2719 * for 1/2 second.
2720 */
2721 if (!list_empty(&block->ccw_queue))
2722 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002723 spin_lock_irqsave(
2724 get_ccwdev_lock(basedev->cdev), flags);
2725 dasd_device_set_stop_bits(basedev,
2726 DASD_STOPPED_PENDING);
2727 spin_unlock_irqrestore(
2728 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002729 dasd_block_set_timer(block, HZ/2);
2730 break;
2731 }
2732 DBF_DEV_EVENT(DBF_ERR, basedev,
2733 "CCW creation failed (rc=%ld) "
2734 "on request %p",
2735 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002736 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002737 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002738 continue;
2739 }
2740 /*
2741 * Note: callback is set to dasd_return_cqr_cb in
2742 * __dasd_block_start_head to cover erp requests as well
2743 */
2744 cqr->callback_data = (void *) req;
2745 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002746 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002747 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002748 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002749 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002750 dasd_profile_start(block, cqr, req);
2751 }
2752}
2753
2754static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2755{
2756 struct request *req;
2757 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002758 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002759
2760 req = (struct request *) cqr->callback_data;
2761 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002762 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002763 if (status < 0)
2764 error = status;
2765 else if (status == 0) {
2766 if (cqr->intrc == -EPERM)
2767 error = -EBADE;
2768 else if (cqr->intrc == -ENOLINK ||
2769 cqr->intrc == -ETIMEDOUT)
2770 error = cqr->intrc;
2771 else
2772 error = -EIO;
2773 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002774 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002775}
2776
2777/*
2778 * Process ccw request queue.
2779 */
2780static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2781 struct list_head *final_queue)
2782{
2783 struct list_head *l, *n;
2784 struct dasd_ccw_req *cqr;
2785 dasd_erp_fn_t erp_fn;
2786 unsigned long flags;
2787 struct dasd_device *base = block->base;
2788
2789restart:
2790 /* Process request with final status. */
2791 list_for_each_safe(l, n, &block->ccw_queue) {
2792 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2793 if (cqr->status != DASD_CQR_DONE &&
2794 cqr->status != DASD_CQR_FAILED &&
2795 cqr->status != DASD_CQR_NEED_ERP &&
2796 cqr->status != DASD_CQR_TERMINATED)
2797 continue;
2798
2799 if (cqr->status == DASD_CQR_TERMINATED) {
2800 base->discipline->handle_terminated_request(cqr);
2801 goto restart;
2802 }
2803
2804 /* Process requests that may be recovered */
2805 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002806 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002807 if (IS_ERR(erp_fn(cqr)))
2808 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002809 goto restart;
2810 }
2811
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002812 /* log sense for fatal error */
2813 if (cqr->status == DASD_CQR_FAILED) {
2814 dasd_log_sense(cqr, &cqr->irb);
2815 }
2816
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002817 /* First of all call extended error reporting. */
2818 if (dasd_eer_enabled(base) &&
2819 cqr->status == DASD_CQR_FAILED) {
2820 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2821
2822 /* restart request */
2823 cqr->status = DASD_CQR_FILLED;
2824 cqr->retries = 255;
2825 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002826 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002827 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2828 flags);
2829 goto restart;
2830 }
2831
2832 /* Process finished ERP request. */
2833 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002834 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002835 goto restart;
2836 }
2837
2838 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002839 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002840 list_move_tail(&cqr->blocklist, final_queue);
2841 }
2842}
2843
2844static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2845{
2846 dasd_schedule_block_bh(cqr->block);
2847}
2848
2849static void __dasd_block_start_head(struct dasd_block *block)
2850{
2851 struct dasd_ccw_req *cqr;
2852
2853 if (list_empty(&block->ccw_queue))
2854 return;
2855 /* We allways begin with the first requests on the queue, as some
2856 * of previously started requests have to be enqueued on a
2857 * dasd_device again for error recovery.
2858 */
2859 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2860 if (cqr->status != DASD_CQR_FILLED)
2861 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002862 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2863 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2864 cqr->status = DASD_CQR_FAILED;
2865 cqr->intrc = -EPERM;
2866 dasd_schedule_block_bh(block);
2867 continue;
2868 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002869 /* Non-temporary stop condition will trigger fail fast */
2870 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2871 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2872 (!dasd_eer_enabled(block->base))) {
2873 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002874 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002875 dasd_schedule_block_bh(block);
2876 continue;
2877 }
2878 /* Don't try to start requests if device is stopped */
2879 if (block->base->stopped)
2880 return;
2881
2882 /* just a fail safe check, should not happen */
2883 if (!cqr->startdev)
2884 cqr->startdev = block->base;
2885
2886 /* make sure that the requests we submit find their way back */
2887 cqr->callback = dasd_return_cqr_cb;
2888
2889 dasd_add_request_tail(cqr);
2890 }
2891}
2892
2893/*
2894 * Central dasd_block layer routine. Takes requests from the generic
2895 * block layer request queue, creates ccw requests, enqueues them on
2896 * a dasd_device and processes ccw requests that have been returned.
2897 */
2898static void dasd_block_tasklet(struct dasd_block *block)
2899{
2900 struct list_head final_queue;
2901 struct list_head *l, *n;
2902 struct dasd_ccw_req *cqr;
2903
2904 atomic_set(&block->tasklet_scheduled, 0);
2905 INIT_LIST_HEAD(&final_queue);
2906 spin_lock(&block->queue_lock);
2907 /* Finish off requests on ccw queue */
2908 __dasd_process_block_ccw_queue(block, &final_queue);
2909 spin_unlock(&block->queue_lock);
2910 /* Now call the callback function of requests with final status */
2911 spin_lock_irq(&block->request_queue_lock);
2912 list_for_each_safe(l, n, &final_queue) {
2913 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2914 list_del_init(&cqr->blocklist);
2915 __dasd_cleanup_cqr(cqr);
2916 }
2917 spin_lock(&block->queue_lock);
2918 /* Get new request from the block device request queue */
2919 __dasd_process_request_queue(block);
2920 /* Now check if the head of the ccw queue needs to be started. */
2921 __dasd_block_start_head(block);
2922 spin_unlock(&block->queue_lock);
2923 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002924 if (waitqueue_active(&shutdown_waitq))
2925 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002926 dasd_put_device(block->base);
2927}
2928
2929static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2930{
2931 wake_up(&dasd_flush_wq);
2932}
2933
2934/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002935 * Requeue a request back to the block request queue
2936 * only works for block requests
2937 */
2938static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2939{
2940 struct dasd_block *block = cqr->block;
2941 struct request *req;
2942 unsigned long flags;
2943
2944 if (!block)
2945 return -EINVAL;
Stefan Haberlanda521b042016-08-08 15:56:54 +02002946 spin_lock_irqsave(&block->request_queue_lock, flags);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002947 req = (struct request *) cqr->callback_data;
2948 blk_requeue_request(block->request_queue, req);
Stefan Haberlanda521b042016-08-08 15:56:54 +02002949 spin_unlock_irqrestore(&block->request_queue_lock, flags);
Stefan Haberlandc5576872013-04-15 16:41:31 +02002950
2951 return 0;
2952}
2953
2954/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002955 * Go through all request on the dasd_block request queue, cancel them
2956 * on the respective dasd_device, and return them to the generic
2957 * block layer.
2958 */
2959static int dasd_flush_block_queue(struct dasd_block *block)
2960{
2961 struct dasd_ccw_req *cqr, *n;
2962 int rc, i;
2963 struct list_head flush_queue;
2964
2965 INIT_LIST_HEAD(&flush_queue);
2966 spin_lock_bh(&block->queue_lock);
2967 rc = 0;
2968restart:
2969 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2970 /* if this request currently owned by a dasd_device cancel it */
2971 if (cqr->status >= DASD_CQR_QUEUED)
2972 rc = dasd_cancel_req(cqr);
2973 if (rc < 0)
2974 break;
2975 /* Rechain request (including erp chain) so it won't be
2976 * touched by the dasd_block_tasklet anymore.
2977 * Replace the callback so we notice when the request
2978 * is returned from the dasd_device layer.
2979 */
2980 cqr->callback = _dasd_wake_block_flush_cb;
2981 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2982 list_move_tail(&cqr->blocklist, &flush_queue);
2983 if (i > 1)
2984 /* moved more than one request - need to restart */
2985 goto restart;
2986 }
2987 spin_unlock_bh(&block->queue_lock);
2988 /* Now call the callback function of flushed requests */
2989restart_cb:
2990 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2991 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2992 /* Process finished ERP request. */
2993 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002994 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002995 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002996 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002997 /* restart list_for_xx loop since dasd_process_erp
2998 * might remove multiple elements */
2999 goto restart_cb;
3000 }
3001 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003002 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01003003 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003004 list_del_init(&cqr->blocklist);
3005 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01003006 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return rc;
3009}
3010
3011/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003012 * Schedules a call to dasd_tasklet over the device tasklet.
3013 */
3014void dasd_schedule_block_bh(struct dasd_block *block)
3015{
3016 /* Protect against rescheduling. */
3017 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3018 return;
3019 /* life cycle of block is bound to it's base device */
3020 dasd_get_device(block->base);
3021 tasklet_hi_schedule(&block->tasklet);
3022}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003023EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003024
3025
3026/*
3027 * SECTION: external block device operations
3028 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
3030
3031/*
3032 * Dasd request queue function. Called from ll_rw_blk.c
3033 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003034static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003036 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003038 block = queue->queuedata;
3039 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003041 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003043 __dasd_block_start_head(block);
3044 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
3047/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00003048 * Block timeout callback, called from the block layer
3049 *
3050 * request_queue lock is held on entry.
3051 *
3052 * Return values:
3053 * BLK_EH_RESET_TIMER if the request should be left running
3054 * BLK_EH_NOT_HANDLED if the request is handled or terminated
3055 * by the driver.
3056 */
3057enum blk_eh_timer_return dasd_times_out(struct request *req)
3058{
3059 struct dasd_ccw_req *cqr = req->completion_data;
3060 struct dasd_block *block = req->q->queuedata;
3061 struct dasd_device *device;
3062 int rc = 0;
3063
3064 if (!cqr)
3065 return BLK_EH_NOT_HANDLED;
3066
3067 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00003068 if (!device->blk_timeout)
3069 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003070 DBF_DEV_EVENT(DBF_WARNING, device,
3071 " dasd_times_out cqr %p status %x",
3072 cqr, cqr->status);
3073
3074 spin_lock(&block->queue_lock);
3075 spin_lock(get_ccwdev_lock(device->cdev));
3076 cqr->retries = -1;
3077 cqr->intrc = -ETIMEDOUT;
3078 if (cqr->status >= DASD_CQR_QUEUED) {
3079 spin_unlock(get_ccwdev_lock(device->cdev));
3080 rc = dasd_cancel_req(cqr);
3081 } else if (cqr->status == DASD_CQR_FILLED ||
3082 cqr->status == DASD_CQR_NEED_ERP) {
3083 cqr->status = DASD_CQR_TERMINATED;
3084 spin_unlock(get_ccwdev_lock(device->cdev));
3085 } else if (cqr->status == DASD_CQR_IN_ERP) {
3086 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3087
3088 list_for_each_entry_safe(searchcqr, nextcqr,
3089 &block->ccw_queue, blocklist) {
3090 tmpcqr = searchcqr;
3091 while (tmpcqr->refers)
3092 tmpcqr = tmpcqr->refers;
3093 if (tmpcqr != cqr)
3094 continue;
3095 /* searchcqr is an ERP request for cqr */
3096 searchcqr->retries = -1;
3097 searchcqr->intrc = -ETIMEDOUT;
3098 if (searchcqr->status >= DASD_CQR_QUEUED) {
3099 spin_unlock(get_ccwdev_lock(device->cdev));
3100 rc = dasd_cancel_req(searchcqr);
3101 spin_lock(get_ccwdev_lock(device->cdev));
3102 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3103 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3104 searchcqr->status = DASD_CQR_TERMINATED;
3105 rc = 0;
3106 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3107 /*
3108 * Shouldn't happen; most recent ERP
3109 * request is at the front of queue
3110 */
3111 continue;
3112 }
3113 break;
3114 }
3115 spin_unlock(get_ccwdev_lock(device->cdev));
3116 }
3117 dasd_schedule_block_bh(block);
3118 spin_unlock(&block->queue_lock);
3119
3120 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3121}
3122
3123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 * Allocate and initialize request queue and default I/O scheduler.
3125 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003126static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003128 block->request_queue = blk_init_queue(do_dasd_request,
3129 &block->request_queue_lock);
3130 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return -ENOMEM;
3132
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003133 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01003135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
3138/*
3139 * Allocate and initialize request queue.
3140 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003141static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Jan Höppner62ba6f82016-06-30 13:18:16 +02003143 struct request_queue *q = block->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 int max;
3145
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003146 if (block->base->features & DASD_FEATURE_USERAW) {
3147 /*
3148 * the max_blocks value for raw_track access is 256
3149 * it is higher than the native ECKD value because we
3150 * only need one ccw per track
3151 * so the max_hw_sectors are
3152 * 2048 x 512B = 1024kB = 16 tracks
3153 */
3154 max = 2048;
3155 } else {
3156 max = block->base->discipline->max_blocks << block->s2b_shift;
3157 }
Jan Höppner62ba6f82016-06-30 13:18:16 +02003158 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
3159 q->limits.max_dev_sectors = max;
3160 blk_queue_logical_block_size(q, block->bp_block);
3161 blk_queue_max_hw_sectors(q, max);
3162 blk_queue_max_segments(q, USHRT_MAX);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003163 /* with page sized segments we can translate each segement into
3164 * one idaw/tidaw
3165 */
Jan Höppner62ba6f82016-06-30 13:18:16 +02003166 blk_queue_max_segment_size(q, PAGE_SIZE);
3167 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
3170/*
3171 * Deactivate and free request queue.
3172 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003173static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003175 if (block->request_queue) {
3176 blk_cleanup_queue(block->request_queue);
3177 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
3179}
3180
3181/*
3182 * Flush request on the request queue.
3183 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003184static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186 struct request *req;
3187
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003188 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003190
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003191 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003192 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003193 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003194 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Al Viro57a7c0b2008-03-02 10:36:08 -05003197static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003199 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 int rc;
3201
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003202 base = dasd_device_from_gendisk(bdev->bd_disk);
3203 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003204 return -ENODEV;
3205
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003206 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003207 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 rc = -ENODEV;
3209 goto unlock;
3210 }
3211
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003212 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 rc = -EINVAL;
3214 goto unlock;
3215 }
3216
3217 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003218 dev_info(&base->cdev->dev,
3219 "Accessing the DASD failed because it is in "
3220 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 rc = -EPERM;
3222 goto out;
3223 }
3224
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003225 if (base->state <= DASD_STATE_BASIC) {
3226 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 " Cannot open unrecognized device");
3228 rc = -ENODEV;
3229 goto out;
3230 }
3231
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003232 if ((mode & FMODE_WRITE) &&
3233 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3234 (base->features & DASD_FEATURE_READONLY))) {
3235 rc = -EROFS;
3236 goto out;
3237 }
3238
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003239 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241
3242out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003243 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003245 atomic_dec(&base->block->open_count);
3246 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return rc;
3248}
3249
Al Virodb2a1442013-05-05 21:52:57 -04003250static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Al Virodb2a1442013-05-05 21:52:57 -04003252 struct dasd_device *base = dasd_device_from_gendisk(disk);
3253 if (base) {
3254 atomic_dec(&base->block->open_count);
3255 module_put(base->discipline->owner);
3256 dasd_put_device(base);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003260/*
3261 * Return disk geometry.
3262 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003263static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003264{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003265 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003266
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003267 base = dasd_device_from_gendisk(bdev->bd_disk);
3268 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003269 return -ENODEV;
3270
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003271 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003272 !base->discipline->fill_geometry) {
3273 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003274 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003275 }
3276 base->discipline->fill_geometry(base->block, geo);
3277 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3278 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003279 return 0;
3280}
3281
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003282const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283dasd_device_operations = {
3284 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003285 .open = dasd_open,
3286 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003287 .ioctl = dasd_ioctl,
3288 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003289 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290};
3291
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003292/*******************************************************************************
3293 * end of block device operations
3294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296static void
3297dasd_exit(void)
3298{
3299#ifdef CONFIG_PROC_FS
3300 dasd_proc_exit();
3301#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003302 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003303 if (dasd_page_cache != NULL) {
3304 kmem_cache_destroy(dasd_page_cache);
3305 dasd_page_cache = NULL;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 dasd_gendisk_exit();
3308 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (dasd_debug_area != NULL) {
3310 debug_unregister(dasd_debug_area);
3311 dasd_debug_area = NULL;
3312 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003313 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
3316/*
3317 * SECTION: common functions for ccw_driver use
3318 */
3319
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003320/*
3321 * Is the device read-only?
3322 * Note that this function does not report the setting of the
3323 * readonly device attribute, but how it is configured in z/VM.
3324 */
3325int dasd_device_is_ro(struct dasd_device *device)
3326{
3327 struct ccw_dev_id dev_id;
3328 struct diag210 diag_data;
3329 int rc;
3330
3331 if (!MACHINE_IS_VM)
3332 return 0;
3333 ccw_device_get_id(device->cdev, &dev_id);
3334 memset(&diag_data, 0, sizeof(diag_data));
3335 diag_data.vrdcdvno = dev_id.devno;
3336 diag_data.vrdclen = sizeof(diag_data);
3337 rc = diag210(&diag_data);
3338 if (rc == 0 || rc == 2) {
3339 return diag_data.vrdcvfla & 0x80;
3340 } else {
3341 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3342 dev_id.devno, rc);
3343 return 0;
3344 }
3345}
3346EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3347
Cornelia Huckf3445a12009-04-14 15:36:23 +02003348static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3349{
3350 struct ccw_device *cdev = data;
3351 int ret;
3352
3353 ret = ccw_device_set_online(cdev);
3354 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003355 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3356 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003357}
3358
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003359/*
3360 * Initial attempt at a probe function. this can be simplified once
3361 * the other detection code is gone.
3362 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003363int dasd_generic_probe(struct ccw_device *cdev,
3364 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 int ret;
3367
3368 ret = dasd_add_sysfs_files(cdev);
3369 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003370 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3371 "dasd_generic_probe: could not add "
3372 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Horst Hummel40545572006-06-29 15:08:18 +02003375 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Horst Hummel40545572006-06-29 15:08:18 +02003377 /*
3378 * Automatically online either all dasd devices (dasd_autodetect)
3379 * or all devices specified with dasd= parameters during
3380 * initial probe.
3381 */
3382 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003383 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003384 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003387EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Stefan Haberlandc020d722016-09-20 10:42:38 +02003389void dasd_generic_free_discipline(struct dasd_device *device)
3390{
3391 /* Forget the discipline information. */
3392 if (device->discipline) {
3393 if (device->discipline->uncheck_device)
3394 device->discipline->uncheck_device(device);
3395 module_put(device->discipline->owner);
3396 device->discipline = NULL;
3397 }
3398 if (device->base_discipline) {
3399 module_put(device->base_discipline->owner);
3400 device->base_discipline = NULL;
3401 }
3402}
3403EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3404
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003405/*
3406 * This will one day be called from a global not_oper handler.
3407 * It is also used by driver_unregister during module unload.
3408 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003409void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003412 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Horst Hummel59afda72005-05-16 21:53:39 -07003414 cdev->handler = NULL;
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003417 if (IS_ERR(device)) {
3418 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003420 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003421 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3422 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* Already doing offline processing */
3424 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003425 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return;
3427 }
3428 /*
3429 * This device is removed unconditionally. Set offline
3430 * flag to prevent dasd_open from opening it while it is
3431 * no quite down yet.
3432 */
3433 dasd_set_target_state(device, DASD_STATE_NEW);
3434 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003435 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003437 /*
3438 * life cycle of block is bound to device, so delete it after
3439 * device was safely removed
3440 */
3441 if (block)
3442 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003443
3444 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003446EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003448/*
3449 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003451 * or the user has started activation through sysfs.
3452 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003453int dasd_generic_set_online(struct ccw_device *cdev,
3454 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003456 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003458 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003459
Horst Hummel40545572006-06-29 15:08:18 +02003460 /* first online clears initial online feature flag */
3461 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 device = dasd_create_device(cdev);
3463 if (IS_ERR(device))
3464 return PTR_ERR(device);
3465
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003466 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003467 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003469 /* Try to load the required module. */
3470 rc = request_module(DASD_DIAG_MOD);
3471 if (rc) {
3472 pr_warn("%s Setting the DASD online failed "
3473 "because the required module %s "
3474 "could not be loaded (rc=%d)\n",
3475 dev_name(&cdev->dev), DASD_DIAG_MOD,
3476 rc);
3477 dasd_delete_device(device);
3478 return -ENODEV;
3479 }
3480 }
3481 /* Module init could have failed, so check again here after
3482 * request_module(). */
3483 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003484 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3485 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 dasd_delete_device(device);
3487 return -ENODEV;
3488 }
3489 discipline = dasd_diag_discipline_pointer;
3490 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003491 if (!try_module_get(base_discipline->owner)) {
3492 dasd_delete_device(device);
3493 return -EINVAL;
3494 }
3495 if (!try_module_get(discipline->owner)) {
3496 module_put(base_discipline->owner);
3497 dasd_delete_device(device);
3498 return -EINVAL;
3499 }
3500 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 device->discipline = discipline;
3502
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003503 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 rc = discipline->check_device(device);
3505 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003506 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3507 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003508 module_put(discipline->owner);
3509 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 dasd_delete_device(device);
3511 return rc;
3512 }
3513
3514 dasd_set_target_state(device, DASD_STATE_ONLINE);
3515 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003516 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3517 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 rc = -ENODEV;
3519 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003520 if (device->block)
3521 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 dasd_delete_device(device);
3523 } else
3524 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003525 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003526
3527 wait_event(dasd_init_waitq, _wait_for_device(device));
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return rc;
3531}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003532EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003534int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003537 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003538 int max_count, open_count, rc;
Jan Höppner0f57c972016-10-18 17:54:49 +02003539 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003541 rc = 0;
Jan Höppner0f57c972016-10-18 17:54:49 +02003542 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3543 device = dasd_device_from_cdev_locked(cdev);
3544 if (IS_ERR(device)) {
3545 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return PTR_ERR(device);
Jan Höppner0f57c972016-10-18 17:54:49 +02003547 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 /*
3550 * We must make sure that this device is currently not in use.
3551 * The open_count is increased for every opener, that includes
3552 * the blkdev_get in dasd_scan_partitions. We are only interested
3553 * in the other openers.
3554 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003555 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003556 max_count = device->block->bdev ? 0 : -1;
3557 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003558 if (open_count > max_count) {
3559 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003560 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3561 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003562 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003563 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3564 dev_name(&cdev->dev));
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003565 rc = -EBUSY;
3566 goto out_err;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003569
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003570 /*
3571 * Test if the offline processing is already running and exit if so.
3572 * If a safe offline is being processed this could only be a normal
3573 * offline that should be able to overtake the safe offline and
3574 * cancel any I/O we do not want to wait for any longer
3575 */
3576 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3577 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3578 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
3579 &device->flags);
3580 } else {
3581 rc = -EBUSY;
3582 goto out_err;
3583 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003584 }
Jan Höppner0f57c972016-10-18 17:54:49 +02003585 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003586
3587 /*
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003588 * if safe_offline is called set safe_offline_running flag and
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003589 * clear safe_offline so that a call to normal offline
3590 * can overrun safe_offline processing
3591 */
3592 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3593 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003594 /* need to unlock here to wait for outstanding I/O */
3595 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003596 /*
3597 * If we want to set the device safe offline all IO operations
3598 * should be finished before continuing the offline process
3599 * so sync bdev first and then wait for our queues to become
3600 * empty
3601 */
Stefan Haberlandca732e12017-01-25 16:56:41 +01003602 if (device->block) {
3603 rc = fsync_bdev(device->block->bdev);
3604 if (rc != 0)
3605 goto interrupted;
3606 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003607 dasd_schedule_device_bh(device);
3608 rc = wait_event_interruptible(shutdown_waitq,
3609 _wait_for_empty_queues(device));
3610 if (rc != 0)
3611 goto interrupted;
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003612
3613 /*
3614 * check if a normal offline process overtook the offline
3615 * processing in this case simply do nothing beside returning
3616 * that we got interrupted
3617 * otherwise mark safe offline as not running any longer and
3618 * continue with normal offline
3619 */
3620 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3621 if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3622 rc = -ERESTARTSYS;
3623 goto out_err;
3624 }
3625 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003626 }
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003627 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 dasd_set_target_state(device, DASD_STATE_NEW);
3630 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003631 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003633 /*
3634 * life cycle of block is bound to device, so delete it after
3635 * device was safely removed
3636 */
3637 if (block)
3638 dasd_free_block(block);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003641
3642interrupted:
3643 /* interrupted by signal */
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003644 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003645 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3646 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003647out_err:
Jan Höppner0f57c972016-10-18 17:54:49 +02003648 dasd_put_device(device);
3649 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Stefan Haberland2757fe1d2017-05-16 10:30:13 +02003650 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003652EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003654int dasd_generic_last_path_gone(struct dasd_device *device)
3655{
3656 struct dasd_ccw_req *cqr;
3657
3658 dev_warn(&device->cdev->dev, "No operational channel path is left "
3659 "for the device\n");
3660 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3661 /* First of all call extended error reporting. */
3662 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3663
3664 if (device->state < DASD_STATE_BASIC)
3665 return 0;
3666 /* Device is active. We want to keep it. */
3667 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3668 if ((cqr->status == DASD_CQR_IN_IO) ||
3669 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3670 cqr->status = DASD_CQR_QUEUED;
3671 cqr->retries++;
3672 }
3673 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3674 dasd_device_clear_timer(device);
3675 dasd_schedule_device_bh(device);
3676 return 1;
3677}
3678EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3679
3680int dasd_generic_path_operational(struct dasd_device *device)
3681{
3682 dev_info(&device->cdev->dev, "A channel path to the device has become "
3683 "operational\n");
3684 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3685 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3686 if (device->stopped & DASD_UNRESUMED_PM) {
3687 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3688 dasd_restore_device(device);
3689 return 1;
3690 }
3691 dasd_schedule_device_bh(device);
3692 if (device->block)
3693 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003694
3695 if (!device->stopped)
3696 wake_up(&generic_waitq);
3697
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003698 return 1;
3699}
3700EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3701
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003702int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
3704 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 int ret;
3706
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003707 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 if (IS_ERR(device))
3709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 ret = 0;
3711 switch (event) {
3712 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003713 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 case CIO_NO_PATH:
Stefan Haberlandc9346152016-08-08 15:53:54 +02003715 dasd_path_no_path(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003716 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 break;
3718 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 ret = 1;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003720 if (dasd_path_get_opm(device))
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003721 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 break;
3723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 dasd_put_device(device);
3725 return ret;
3726}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003727EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003729void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3730{
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003731 struct dasd_device *device;
Stefan Haberlanda521b042016-08-08 15:56:54 +02003732 int chp, oldopm, hpfpm, ifccpm;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003733
3734 device = dasd_device_from_cdev_locked(cdev);
3735 if (IS_ERR(device))
3736 return;
Stefan Haberlandc9346152016-08-08 15:53:54 +02003737
3738 oldopm = dasd_path_get_opm(device);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003739 for (chp = 0; chp < 8; chp++) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003740 if (path_event[chp] & PE_PATH_GONE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003741 dasd_path_notoper(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003742 }
3743 if (path_event[chp] & PE_PATH_AVAILABLE) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003744 dasd_path_available(device, chp);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003745 dasd_schedule_device_bh(device);
3746 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003747 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003748 if (!dasd_path_is_operational(device, chp) &&
3749 !dasd_path_need_verify(device, chp)) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003750 /*
3751 * we can not establish a pathgroup on an
3752 * unavailable path, so trigger a path
3753 * verification first
3754 */
Stefan Haberlandc9346152016-08-08 15:53:54 +02003755 dasd_path_available(device, chp);
3756 dasd_schedule_device_bh(device);
Stefan Haberland12d7b102012-09-11 15:10:58 +02003757 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003758 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3759 "Pathgroup re-established\n");
3760 if (device->discipline->kick_validate)
3761 device->discipline->kick_validate(device);
3762 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003763 }
Stefan Haberlanda521b042016-08-08 15:56:54 +02003764 hpfpm = dasd_path_get_hpfpm(device);
3765 ifccpm = dasd_path_get_ifccpm(device);
3766 if (!dasd_path_get_opm(device) && hpfpm) {
3767 /*
3768 * device has no operational paths but at least one path is
3769 * disabled due to HPF errors
3770 * disable HPF at all and use the path(s) again
3771 */
3772 if (device->discipline->disable_hpf)
3773 device->discipline->disable_hpf(device);
3774 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3775 dasd_path_set_tbvpm(device, hpfpm);
3776 dasd_schedule_device_bh(device);
3777 dasd_schedule_requeue(device);
3778 } else if (!dasd_path_get_opm(device) && ifccpm) {
3779 /*
3780 * device has no operational paths but at least one path is
3781 * disabled due to IFCC errors
3782 * trigger path verification on paths with IFCC errors
3783 */
3784 dasd_path_set_tbvpm(device, ifccpm);
3785 dasd_schedule_device_bh(device);
3786 }
3787 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
Stefan Haberlandc9346152016-08-08 15:53:54 +02003788 dev_warn(&device->cdev->dev,
3789 "No verified channel paths remain for the device\n");
3790 DBF_DEV_EVENT(DBF_WARNING, device,
3791 "%s", "last verified path gone");
3792 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3793 dasd_device_set_stop_bits(device,
3794 DASD_STOPPED_DC_WAIT);
3795 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003796 dasd_put_device(device);
3797}
3798EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3799
3800int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3801{
Stefan Haberlandc9346152016-08-08 15:53:54 +02003802 if (!dasd_path_get_opm(device) && lpm) {
3803 dasd_path_set_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003804 dasd_generic_path_operational(device);
3805 } else
Stefan Haberlandc9346152016-08-08 15:53:54 +02003806 dasd_path_add_opm(device, lpm);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003807 return 0;
3808}
3809EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3810
Stefan Haberlanda521b042016-08-08 15:56:54 +02003811/*
3812 * clear active requests and requeue them to block layer if possible
3813 */
3814static int dasd_generic_requeue_all_requests(struct dasd_device *device)
3815{
3816 struct list_head requeue_queue;
3817 struct dasd_ccw_req *cqr, *n;
3818 struct dasd_ccw_req *refers;
3819 int rc;
3820
3821 INIT_LIST_HEAD(&requeue_queue);
3822 spin_lock_irq(get_ccwdev_lock(device->cdev));
3823 rc = 0;
3824 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3825 /* Check status and move request to flush_queue */
3826 if (cqr->status == DASD_CQR_IN_IO) {
3827 rc = device->discipline->term_IO(cqr);
3828 if (rc) {
3829 /* unable to terminate requeust */
3830 dev_err(&device->cdev->dev,
3831 "Unable to terminate request %p "
3832 "on suspend\n", cqr);
3833 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3834 dasd_put_device(device);
3835 return rc;
3836 }
3837 }
3838 list_move_tail(&cqr->devlist, &requeue_queue);
3839 }
3840 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3841
3842 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
3843 wait_event(dasd_flush_wq,
3844 (cqr->status != DASD_CQR_CLEAR_PENDING));
3845
3846 /* mark sleepon requests as ended */
3847 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
3848 cqr->callback_data = DASD_SLEEPON_END_TAG;
3849
3850 /* remove requests from device and block queue */
3851 list_del_init(&cqr->devlist);
3852 while (cqr->refers != NULL) {
3853 refers = cqr->refers;
3854 /* remove the request from the block queue */
3855 list_del(&cqr->blocklist);
3856 /* free the finished erp request */
3857 dasd_free_erp_request(cqr, cqr->memdev);
3858 cqr = refers;
3859 }
3860
3861 /*
3862 * requeue requests to blocklayer will only work
3863 * for block device requests
3864 */
3865 if (_dasd_requeue_request(cqr))
3866 continue;
3867
3868 if (cqr->block)
3869 list_del_init(&cqr->blocklist);
3870 cqr->block->base->discipline->free_cp(
3871 cqr, (struct request *) cqr->callback_data);
3872 }
3873
3874 /*
3875 * if requests remain then they are internal request
3876 * and go back to the device queue
3877 */
3878 if (!list_empty(&requeue_queue)) {
3879 /* move freeze_queue to start of the ccw_queue */
3880 spin_lock_irq(get_ccwdev_lock(device->cdev));
3881 list_splice_tail(&requeue_queue, &device->ccw_queue);
3882 spin_unlock_irq(get_ccwdev_lock(device->cdev));
3883 }
3884 /* wake up generic waitqueue for eventually ended sleepon requests */
3885 wake_up(&generic_waitq);
3886 return rc;
3887}
3888
3889static void do_requeue_requests(struct work_struct *work)
3890{
3891 struct dasd_device *device = container_of(work, struct dasd_device,
3892 requeue_requests);
3893 dasd_generic_requeue_all_requests(device);
3894 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
3895 if (device->block)
3896 dasd_schedule_block_bh(device->block);
3897 dasd_put_device(device);
3898}
3899
3900void dasd_schedule_requeue(struct dasd_device *device)
3901{
3902 dasd_get_device(device);
3903 /* queue call to dasd_reload_device to the kernel event daemon. */
3904 if (!schedule_work(&device->requeue_requests))
3905 dasd_put_device(device);
3906}
3907EXPORT_SYMBOL(dasd_schedule_requeue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003908
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003909int dasd_generic_pm_freeze(struct ccw_device *cdev)
3910{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003911 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003912 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003913
3914 if (IS_ERR(device))
3915 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003916
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003917 /* mark device as suspended */
3918 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3919
Stefan Haberland6f272b92011-01-05 12:48:05 +01003920 if (device->discipline->freeze)
3921 rc = device->discipline->freeze(device);
3922
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003923 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003924 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003925
Stefan Haberlanda521b042016-08-08 15:56:54 +02003926 return dasd_generic_requeue_all_requests(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003927}
3928EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3929
3930int dasd_generic_restore_device(struct ccw_device *cdev)
3931{
3932 struct dasd_device *device = dasd_device_from_cdev(cdev);
3933 int rc = 0;
3934
3935 if (IS_ERR(device))
3936 return PTR_ERR(device);
3937
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003938 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003939 dasd_device_remove_stop_bits(device,
3940 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003941
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003942 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003943
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003944 /*
3945 * call discipline restore function
3946 * if device is stopped do nothing e.g. for disconnected devices
3947 */
3948 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003949 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003950 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003951 /*
3952 * if the resume failed for the DASD we put it in
3953 * an UNRESUMED stop state
3954 */
3955 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003956
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003957 if (device->block)
3958 dasd_schedule_block_bh(device->block);
3959
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003960 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003961 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003962 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003963}
3964EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3965
Heiko Carstens763968e2007-05-10 15:45:46 +02003966static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3967 void *rdc_buffer,
3968 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003969 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003970{
3971 struct dasd_ccw_req *cqr;
3972 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003973 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003974
3975 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3976
3977 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003978 /* internal error 13 - Allocating the RDC request failed*/
3979 dev_err(&device->cdev->dev,
3980 "An error occurred in the DASD device driver, "
3981 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003982 return cqr;
3983 }
3984
3985 ccw = cqr->cpaddr;
3986 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003987 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3988 idaw = (unsigned long *) (cqr->data);
3989 ccw->cda = (__u32)(addr_t) idaw;
3990 ccw->flags = CCW_FLAG_IDA;
3991 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3992 } else {
3993 ccw->cda = (__u32)(addr_t) rdc_buffer;
3994 ccw->flags = 0;
3995 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003996
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003997 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003998 cqr->startdev = device;
3999 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02004000 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01004001 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01004002 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02004003 cqr->status = DASD_CQR_FILLED;
4004 return cqr;
4005}
4006
4007
Stefan Haberland68b781f2009-09-11 10:28:29 +02004008int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02004009 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02004010{
4011 int ret;
4012 struct dasd_ccw_req *cqr;
4013
Sebastian Ott92636b12009-06-12 10:26:37 +02004014 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02004015 magic);
4016 if (IS_ERR(cqr))
4017 return PTR_ERR(cqr);
4018
4019 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004020 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02004021 return ret;
4022}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02004023EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004024
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01004025/*
4026 * In command mode and transport mode we need to look for sense
4027 * data in different places. The sense data itself is allways
4028 * an array of 32 bytes, so we can unify the sense data access
4029 * for both modes.
4030 */
4031char *dasd_get_sense(struct irb *irb)
4032{
4033 struct tsb *tsb = NULL;
4034 char *sense = NULL;
4035
4036 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4037 if (irb->scsw.tm.tcw)
4038 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4039 irb->scsw.tm.tcw);
4040 if (tsb && tsb->length == 64 && tsb->flags)
4041 switch (tsb->flags & 0x07) {
4042 case 1: /* tsa_iostat */
4043 sense = tsb->tsa.iostat.sense;
4044 break;
4045 case 2: /* tsa_ddpc */
4046 sense = tsb->tsa.ddpc.sense;
4047 break;
4048 default:
4049 /* currently we don't use interrogate data */
4050 break;
4051 }
4052 } else if (irb->esw.esw0.erw.cons) {
4053 sense = irb->ecw;
4054 }
4055 return sense;
4056}
4057EXPORT_SYMBOL_GPL(dasd_get_sense);
4058
Stefan Haberland4679e892012-06-19 17:30:12 +02004059void dasd_generic_shutdown(struct ccw_device *cdev)
4060{
4061 struct dasd_device *device;
4062
4063 device = dasd_device_from_cdev(cdev);
4064 if (IS_ERR(device))
4065 return;
4066
4067 if (device->block)
4068 dasd_schedule_block_bh(device->block);
4069
4070 dasd_schedule_device_bh(device);
4071
4072 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4073}
4074EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4075
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01004076static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 int rc;
4079
4080 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02004081 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02004082 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02004083 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01004086 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (dasd_debug_area == NULL) {
4088 rc = -ENOMEM;
4089 goto failed;
4090 }
4091 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02004092 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4095
4096 dasd_diag_discipline_pointer = NULL;
4097
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02004098 dasd_statistics_createroot();
4099
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 rc = dasd_devmap_init();
4101 if (rc)
4102 goto failed;
4103 rc = dasd_gendisk_init();
4104 if (rc)
4105 goto failed;
4106 rc = dasd_parse();
4107 if (rc)
4108 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004109 rc = dasd_eer_init();
4110 if (rc)
4111 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112#ifdef CONFIG_PROC_FS
4113 rc = dasd_proc_init();
4114 if (rc)
4115 goto failed;
4116#endif
4117
4118 return 0;
4119failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004120 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 dasd_exit();
4122 return rc;
4123}
4124
4125module_init(dasd_init);
4126module_exit(dasd_exit);