blob: 5ecd40884f015dc61fd591728fcacdfb78449581 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02007 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Stefan Haberlandfc19f382009-03-26 15:23:49 +010010#define KMSG_COMPONENT "dasd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kmod.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/ctype.h>
17#include <linux/major.h>
18#include <linux/slab.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020020#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010021#include <linux/mutex.h>
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020022#include <linux/debugfs.h>
23#include <linux/seq_file.h>
Stefan Weinhubere4258d52011-08-03 16:44:20 +020024#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010029#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010030#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +020041#define DASD_DIAG_MOD "dasd_diag_mod"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * SECTION: exported variables of dasd.c
45 */
46debug_info_t *dasd_debug_area;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020047EXPORT_SYMBOL(dasd_debug_area);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020048static struct dentry *dasd_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct dasd_discipline *dasd_diag_discipline_pointer;
Fabian Frederick7048a2b2014-06-26 19:41:47 +020050EXPORT_SYMBOL(dasd_diag_discipline_pointer);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010051void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
54MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020055 " Copyright IBM Corp. 2000");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_LICENSE("GPL");
58
59/*
60 * SECTION: prototypes for static functions of dasd.c
61 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010062static int dasd_alloc_queue(struct dasd_block *);
63static void dasd_setup_queue(struct dasd_block *);
64static void dasd_free_queue(struct dasd_block *);
65static void dasd_flush_request_queue(struct dasd_block *);
66static int dasd_flush_block_queue(struct dasd_block *);
67static void dasd_device_tasklet(struct dasd_device *);
68static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000069static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020070static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020071static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010072static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010073static void dasd_device_timeout(unsigned long);
74static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010075static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +020076static void dasd_profile_init(struct dasd_profile *, struct dentry *);
77static void dasd_profile_exit(struct dasd_profile *);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +010078static void dasd_hosts_init(struct dentry *, struct dasd_device *);
79static void dasd_hosts_exit(struct dasd_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * SECTION: Operations on the device structure.
83 */
84static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020085static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020086static wait_queue_head_t generic_waitq;
Stefan Haberland4679e892012-06-19 17:30:12 +020087static wait_queue_head_t shutdown_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/*
90 * Allocate memory for a new device structure.
91 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010092struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct dasd_device *device;
95
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010096 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
97 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* Get two pages for normal block device operations. */
101 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100102 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 kfree(device);
104 return ERR_PTR(-ENOMEM);
105 }
106 /* Get one page for error recovery. */
107 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100108 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 free_pages((unsigned long) device->ccw_mem, 1);
110 kfree(device);
111 return ERR_PTR(-ENOMEM);
112 }
113
114 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
115 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
116 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100117 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200118 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100119 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 (unsigned long) device);
121 INIT_LIST_HEAD(&device->ccw_queue);
122 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100123 device->timer.function = dasd_device_timeout;
124 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000125 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200126 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200127 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 device->state = DASD_STATE_NEW;
129 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100130 mutex_init(&device->state_mutex);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200131 spin_lock_init(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return device;
133}
134
135/*
136 * Free memory of a device structure.
137 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100138void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800140 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 free_page((unsigned long) device->erp_mem);
142 free_pages((unsigned long) device->ccw_mem, 1);
143 kfree(device);
144}
145
146/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100147 * Allocate memory for a new device structure.
148 */
149struct dasd_block *dasd_alloc_block(void)
150{
151 struct dasd_block *block;
152
153 block = kzalloc(sizeof(*block), GFP_ATOMIC);
154 if (!block)
155 return ERR_PTR(-ENOMEM);
156 /* open_count = 0 means device online but not in use */
157 atomic_set(&block->open_count, -1);
158
159 spin_lock_init(&block->request_queue_lock);
160 atomic_set(&block->tasklet_scheduled, 0);
161 tasklet_init(&block->tasklet,
162 (void (*)(unsigned long)) dasd_block_tasklet,
163 (unsigned long) block);
164 INIT_LIST_HEAD(&block->ccw_queue);
165 spin_lock_init(&block->queue_lock);
166 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100167 block->timer.function = dasd_block_timeout;
168 block->timer.data = (unsigned long) block;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200169 spin_lock_init(&block->profile.lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100170
171 return block;
172}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200173EXPORT_SYMBOL_GPL(dasd_alloc_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100174
175/*
176 * Free memory of a device structure.
177 */
178void dasd_free_block(struct dasd_block *block)
179{
180 kfree(block);
181}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200182EXPORT_SYMBOL_GPL(dasd_free_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100183
184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Make a new device known to the system.
186 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100187static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int rc;
190
191 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200192 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * keep the reference count > 0.
194 */
195 dasd_get_device(device);
196
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100197 if (device->block) {
198 rc = dasd_alloc_queue(device->block);
199 if (rc) {
200 dasd_put_device(device);
201 return rc;
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 device->state = DASD_STATE_KNOWN;
205 return 0;
206}
207
208/*
209 * Let the system forget about a device.
210 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100211static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800213 /* Disable extended error reporting for this device. */
214 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 device->state = DASD_STATE_NEW;
216
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217 if (device->block)
218 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Give up reference we took in dasd_state_new_to_known. */
221 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200225static struct dentry *dasd_debugfs_setup(const char *name,
226 struct dentry *base_dentry)
227{
228 struct dentry *pde;
229
230 if (!base_dentry)
231 return NULL;
232 pde = debugfs_create_dir(name, base_dentry);
233 if (!pde || IS_ERR(pde))
234 return NULL;
235 return pde;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Request the irq line for the device.
240 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100241static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200243 struct dasd_block *block = device->block;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200244 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Allocate and register gendisk structure. */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200247 if (block) {
248 rc = dasd_gendisk_alloc(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100249 if (rc)
250 return rc;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200251 block->debugfs_dentry =
252 dasd_debugfs_setup(block->gdp->disk_name,
253 dasd_debugfs_root_entry);
254 dasd_profile_init(&block->profile, block->debugfs_dentry);
255 if (dasd_global_profile_level == DASD_PROFILE_ON)
256 dasd_profile_on(&device->block->profile);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100257 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200258 device->debugfs_dentry =
259 dasd_debugfs_setup(dev_name(&device->cdev->dev),
260 dasd_debugfs_root_entry);
261 dasd_profile_init(&device->profile, device->debugfs_dentry);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100262 dasd_hosts_init(device->debugfs_dentry, device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100265 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100266 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200268 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
270
271 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200272
273 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * Release the irq line for the device. Terminate any running i/o.
278 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Horst Hummel8f617012006-08-30 14:33:33 +0200281 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200282
Stefan Haberlanddaa991b2014-07-18 14:19:25 +0200283 if (device->discipline->basic_to_known) {
284 rc = device->discipline->basic_to_known(device);
285 if (rc)
286 return rc;
287 }
288
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289 if (device->block) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200290 dasd_profile_exit(&device->block->profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200291 debugfs_remove(device->block->debugfs_dentry);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100292 dasd_gendisk_free(device->block);
293 dasd_block_clear_timer(device->block);
294 }
295 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200296 if (rc)
297 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100298 dasd_device_clear_timer(device);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200299 dasd_profile_exit(&device->profile);
Stefan Haberland5a3b7b12016-03-18 09:42:13 +0100300 dasd_hosts_exit(device);
Fabian Frederickd7309aa2014-06-26 19:41:46 +0200301 debugfs_remove(device->debugfs_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
303 if (device->debug_area != NULL) {
304 debug_unregister(device->debug_area);
305 device->debug_area = NULL;
306 }
307 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
312 * Do the initial analysis. The do_analysis function may return
313 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
314 * until the discipline decides to continue the startup sequence
315 * by calling the function dasd_change_state. The eckd disciplines
316 * uses this to start a ccw that detects the format. The completion
317 * interrupt for this detection ccw uses the kernel event daemon to
318 * trigger the call to dasd_change_state. All this is done in the
319 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800320 * After the analysis ccw is done (do_analysis returned 0) the block
321 * device is setup.
322 * In case the analysis returns an error, the device setup is stopped
323 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100328 struct dasd_block *block;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200329 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800333 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 if (block) {
335 if (block->base->discipline->do_analysis != NULL)
336 rc = block->base->discipline->do_analysis(block);
337 if (rc) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200338 if (rc != -EAGAIN) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100339 device->state = DASD_STATE_UNFMT;
Stefan Haberlandeed5c4b2016-08-31 13:31:10 +0200340 disk = device->block->gdp;
341 kobject_uevent(&disk_to_dev(disk)->kobj,
342 KOBJ_CHANGE);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200343 goto out;
344 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 return rc;
346 }
347 dasd_setup_queue(block);
348 set_capacity(block->gdp,
349 block->blocks << block->s2b_shift);
350 device->state = DASD_STATE_READY;
351 rc = dasd_scan_partitions(block);
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200352 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353 device->state = DASD_STATE_BASIC;
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200354 return rc;
355 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100356 } else {
357 device->state = DASD_STATE_READY;
358 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200359out:
360 if (device->discipline->basic_to_ready)
361 rc = device->discipline->basic_to_ready(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +0100365static inline
366int _wait_for_empty_queues(struct dasd_device *device)
367{
368 if (device->block)
369 return list_empty(&device->ccw_queue) &&
370 list_empty(&device->block->ccw_queue);
371 else
372 return list_empty(&device->ccw_queue);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Remove device from block device layer. Destroy dirty buffers.
377 * Forget format information. Check if the target level is basic
378 * and if it is create fake disk for formatting.
379 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Horst Hummel8f617012006-08-30 14:33:33 +0200382 int rc;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100385 if (device->block) {
386 struct dasd_block *block = device->block;
387 rc = dasd_flush_block_queue(block);
388 if (rc) {
389 device->state = DASD_STATE_READY;
390 return rc;
391 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100392 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100393 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100394 block->blocks = 0;
395 block->bp_block = 0;
396 block->s2b_shift = 0;
397 }
Horst Hummel8f617012006-08-30 14:33:33 +0200398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
Horst Hummel90f00942006-03-07 21:55:39 -0800402 * Back to basic.
403 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100404static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800405{
406 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200407 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800408}
409
410/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * Make the device online and schedule the bottom half to start
412 * the requeueing of requests from the linux request queue to the
413 * ccw queue.
414 */
Horst Hummel8f617012006-08-30 14:33:33 +0200415static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416dasd_state_ready_to_online(struct dasd_device * device)
417{
Stefan Weinhuber13018092009-01-09 12:14:50 +0100418 struct gendisk *disk;
419 struct disk_part_iter piter;
420 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100423 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100424 dasd_schedule_block_bh(device->block);
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100425 if ((device->features & DASD_FEATURE_USERAW)) {
426 disk = device->block->gdp;
427 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
428 return 0;
429 }
Stefan Weinhuber13018092009-01-09 12:14:50 +0100430 disk = device->block->bdev->bd_disk;
431 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
432 while ((part = disk_part_iter_next(&piter)))
433 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
434 disk_part_iter_exit(&piter);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
439/*
440 * Stop the requeueing of requests again.
441 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100442static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100445 struct gendisk *disk;
446 struct disk_part_iter piter;
447 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100448
449 if (device->discipline->online_to_ready) {
450 rc = device->discipline->online_to_ready(device);
451 if (rc)
452 return rc;
453 }
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 device->state = DASD_STATE_READY;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +0100456 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
Stefan Weinhuber13018092009-01-09 12:14:50 +0100457 disk = device->block->bdev->bd_disk;
458 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
459 while ((part = disk_part_iter_next(&piter)))
460 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
461 disk_part_iter_exit(&piter);
462 }
Horst Hummel8f617012006-08-30 14:33:33 +0200463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * Device startup state changes.
468 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100469static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int rc;
472
473 rc = 0;
474 if (device->state == DASD_STATE_NEW &&
475 device->target >= DASD_STATE_KNOWN)
476 rc = dasd_state_new_to_known(device);
477
478 if (!rc &&
479 device->state == DASD_STATE_KNOWN &&
480 device->target >= DASD_STATE_BASIC)
481 rc = dasd_state_known_to_basic(device);
482
483 if (!rc &&
484 device->state == DASD_STATE_BASIC &&
485 device->target >= DASD_STATE_READY)
486 rc = dasd_state_basic_to_ready(device);
487
488 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700489 device->state == DASD_STATE_UNFMT &&
490 device->target > DASD_STATE_UNFMT)
491 rc = -EPERM;
492
493 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 device->state == DASD_STATE_READY &&
495 device->target >= DASD_STATE_ONLINE)
496 rc = dasd_state_ready_to_online(device);
497
498 return rc;
499}
500
501/*
502 * Device shutdown state changes.
503 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100504static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Horst Hummel8f617012006-08-30 14:33:33 +0200506 int rc;
507
508 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (device->state == DASD_STATE_ONLINE &&
510 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200511 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200512
Horst Hummel8f617012006-08-30 14:33:33 +0200513 if (!rc &&
514 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200516 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800517
Horst Hummel8f617012006-08-30 14:33:33 +0200518 if (!rc &&
519 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800520 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200521 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800522
Horst Hummel8f617012006-08-30 14:33:33 +0200523 if (!rc &&
524 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200526 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200527
Horst Hummel8f617012006-08-30 14:33:33 +0200528 if (!rc &&
529 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200531 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Horst Hummel8f617012006-08-30 14:33:33 +0200533 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * This is the main startup/shutdown routine.
538 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100539static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Sebastian Ott181d9522009-06-22 12:08:21 +0200541 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (device->state == device->target)
544 /* Already where we want to go today... */
545 return;
546 if (device->state < device->target)
547 rc = dasd_increase_state(device);
548 else
549 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200550 if (rc == -EAGAIN)
551 return;
552 if (rc)
553 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200555 /* let user-space know that the device status changed */
556 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Sebastian Ott1f08be82012-09-05 14:18:22 +0200557
558 if (device->state == device->target)
559 wake_up(&dasd_init_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Kick starter for devices that did not complete the startup/shutdown
564 * procedure or were sleeping because of a pending state.
565 * dasd_kick_device will schedule a call do do_kick_device to the kernel
566 * event daemon.
567 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Al Viro4927b3f2006-12-06 19:18:20 +0000570 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100571 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100573 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100574 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dasd_put_device(device);
576}
577
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100578void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 dasd_get_device(device);
581 /* queue call to dasd_kick_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200582 if (!schedule_work(&device->kick_work))
583 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200585EXPORT_SYMBOL(dasd_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200588 * dasd_reload_device will schedule a call do do_reload_device to the kernel
589 * event daemon.
590 */
591static void do_reload_device(struct work_struct *work)
592{
593 struct dasd_device *device = container_of(work, struct dasd_device,
594 reload_device);
595 device->discipline->reload(device);
596 dasd_put_device(device);
597}
598
599void dasd_reload_device(struct dasd_device *device)
600{
601 dasd_get_device(device);
602 /* queue call to dasd_reload_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200603 if (!schedule_work(&device->reload_device))
604 dasd_put_device(device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200605}
606EXPORT_SYMBOL(dasd_reload_device);
607
608/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200609 * dasd_restore_device will schedule a call do do_restore_device to the kernel
610 * event daemon.
611 */
612static void do_restore_device(struct work_struct *work)
613{
614 struct dasd_device *device = container_of(work, struct dasd_device,
615 restore_device);
616 device->cdev->drv->restore(device->cdev);
617 dasd_put_device(device);
618}
619
620void dasd_restore_device(struct dasd_device *device)
621{
622 dasd_get_device(device);
623 /* queue call to dasd_restore_device to the kernel event daemon. */
Stefan Haberlandf2608cd2015-04-02 12:27:25 +0200624 if (!schedule_work(&device->restore_device))
625 dasd_put_device(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200626}
627
628/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * Set the target state for a device and starts the state change.
630 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100631void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200633 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100634 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* If we are in probeonly mode stop at DASD_STATE_READY. */
636 if (dasd_probeonly && target > DASD_STATE_READY)
637 target = DASD_STATE_READY;
638 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100639 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 wake_up(&dasd_init_waitq);
641 device->target = target;
642 }
643 if (device->state != device->target)
644 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100645 mutex_unlock(&device->state_mutex);
646 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200648EXPORT_SYMBOL(dasd_set_target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/*
651 * Enable devices with device numbers in [from..to].
652 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100653static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 return (device->state == device->target);
656}
657
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100658void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 dasd_set_target_state(device, DASD_STATE_ONLINE);
661 if (device->state <= DASD_STATE_KNOWN)
662 /* No discipline for device found. */
663 dasd_set_target_state(device, DASD_STATE_NEW);
664 /* Now wait for the devices to come up. */
665 wait_event(dasd_init_waitq, _wait_for_device(device));
Stefan Haberland25e2cf12012-03-11 11:59:37 -0400666
667 dasd_reload_device(device);
668 if (device->discipline->kick_validate)
669 device->discipline->kick_validate(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
Fabian Frederick7048a2b2014-06-26 19:41:47 +0200671EXPORT_SYMBOL(dasd_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/*
674 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
675 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200676
677unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679#ifdef CONFIG_DASD_PROFILE
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100680struct dasd_profile dasd_global_profile = {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100681 .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
682};
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200683static struct dentry *dasd_debugfs_global_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/*
686 * Add profiling information for cqr before execution.
687 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100688static void dasd_profile_start(struct dasd_block *block,
689 struct dasd_ccw_req *cqr,
690 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct list_head *l;
693 unsigned int counter;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200694 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* count the length of the chanq for statistics */
697 counter = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200698 if (dasd_global_profile_level || block->profile.data)
699 list_for_each(l, &block->ccw_queue)
700 if (++counter >= 31)
701 break;
702
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100703 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100704 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100705 dasd_global_profile.data->dasd_io_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200706 if (rq_data_dir(req) == READ)
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100707 dasd_global_profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200708 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100709 spin_unlock(&dasd_global_profile.lock);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200710
711 spin_lock(&block->profile.lock);
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200712 if (block->profile.data) {
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200713 block->profile.data->dasd_io_nr_req[counter]++;
714 if (rq_data_dir(req) == READ)
715 block->profile.data->dasd_read_nr_req[counter]++;
Stefan Weinhuberc81a90c2013-10-25 11:08:06 +0200716 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200717 spin_unlock(&block->profile.lock);
718
719 /*
720 * We count the request for the start device, even though it may run on
721 * some other device due to error recovery. This way we make sure that
722 * we count each request only once.
723 */
724 device = cqr->startdev;
725 if (device->profile.data) {
726 counter = 1; /* request is not yet queued on the start device */
727 list_for_each(l, &device->ccw_queue)
728 if (++counter >= 31)
729 break;
730 }
731 spin_lock(&device->profile.lock);
732 if (device->profile.data) {
733 device->profile.data->dasd_io_nr_req[counter]++;
734 if (rq_data_dir(req) == READ)
735 device->profile.data->dasd_read_nr_req[counter]++;
736 }
737 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740/*
741 * Add profiling information for cqr after execution.
742 */
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200743
744#define dasd_profile_counter(value, index) \
745{ \
746 for (index = 0; index < 31 && value >> (2+index); index++) \
747 ; \
748}
749
750static void dasd_profile_end_add_data(struct dasd_profile_info *data,
751 int is_alias,
752 int is_tpm,
753 int is_read,
754 long sectors,
755 int sectors_ind,
756 int tottime_ind,
757 int tottimeps_ind,
758 int strtime_ind,
759 int irqtime_ind,
760 int irqtimeps_ind,
761 int endtime_ind)
762{
763 /* in case of an overflow, reset the whole profile */
764 if (data->dasd_io_reqs == UINT_MAX) {
765 memset(data, 0, sizeof(*data));
766 getnstimeofday(&data->starttod);
767 }
768 data->dasd_io_reqs++;
769 data->dasd_io_sects += sectors;
770 if (is_alias)
771 data->dasd_io_alias++;
772 if (is_tpm)
773 data->dasd_io_tpm++;
774
775 data->dasd_io_secs[sectors_ind]++;
776 data->dasd_io_times[tottime_ind]++;
777 data->dasd_io_timps[tottimeps_ind]++;
778 data->dasd_io_time1[strtime_ind]++;
779 data->dasd_io_time2[irqtime_ind]++;
780 data->dasd_io_time2ps[irqtimeps_ind]++;
781 data->dasd_io_time3[endtime_ind]++;
782
783 if (is_read) {
784 data->dasd_read_reqs++;
785 data->dasd_read_sects += sectors;
786 if (is_alias)
787 data->dasd_read_alias++;
788 if (is_tpm)
789 data->dasd_read_tpm++;
790 data->dasd_read_secs[sectors_ind]++;
791 data->dasd_read_times[tottime_ind]++;
792 data->dasd_read_time1[strtime_ind]++;
793 data->dasd_read_time2[irqtime_ind]++;
794 data->dasd_read_time3[endtime_ind]++;
795 }
796}
797
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100798static void dasd_profile_end(struct dasd_block *block,
799 struct dasd_ccw_req *cqr,
800 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 long strtime, irqtime, endtime, tottime; /* in microseconds */
803 long tottimeps, sectors;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200804 struct dasd_device *device;
805 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
806 int irqtime_ind, irqtimeps_ind, endtime_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200808 device = cqr->startdev;
809 if (!(dasd_global_profile_level ||
810 block->profile.data ||
811 device->profile.data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return;
813
Tejun Heo83096eb2009-05-07 22:24:39 +0900814 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!cqr->buildclk || !cqr->startclk ||
816 !cqr->stopclk || !cqr->endclk ||
817 !sectors)
818 return;
819
820 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
821 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
822 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
823 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
824 tottimeps = tottime / sectors;
825
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200826 dasd_profile_counter(sectors, sectors_ind);
827 dasd_profile_counter(tottime, tottime_ind);
828 dasd_profile_counter(tottimeps, tottimeps_ind);
829 dasd_profile_counter(strtime, strtime_ind);
830 dasd_profile_counter(irqtime, irqtime_ind);
831 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
832 dasd_profile_counter(endtime, endtime_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100834 spin_lock(&dasd_global_profile.lock);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100835 if (dasd_global_profile.data) {
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100836 dasd_profile_end_add_data(dasd_global_profile.data,
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200837 cqr->startdev != block->base,
838 cqr->cpmode == 1,
839 rq_data_dir(req) == READ,
840 sectors, sectors_ind, tottime_ind,
841 tottimeps_ind, strtime_ind,
842 irqtime_ind, irqtimeps_ind,
843 endtime_ind);
844 }
Sebastian Ott8ea55c92015-01-28 18:44:17 +0100845 spin_unlock(&dasd_global_profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200847 spin_lock(&block->profile.lock);
848 if (block->profile.data)
849 dasd_profile_end_add_data(block->profile.data,
850 cqr->startdev != block->base,
851 cqr->cpmode == 1,
852 rq_data_dir(req) == READ,
853 sectors, sectors_ind, tottime_ind,
854 tottimeps_ind, strtime_ind,
855 irqtime_ind, irqtimeps_ind,
856 endtime_ind);
857 spin_unlock(&block->profile.lock);
858
859 spin_lock(&device->profile.lock);
860 if (device->profile.data)
861 dasd_profile_end_add_data(device->profile.data,
862 cqr->startdev != block->base,
863 cqr->cpmode == 1,
864 rq_data_dir(req) == READ,
865 sectors, sectors_ind, tottime_ind,
866 tottimeps_ind, strtime_ind,
867 irqtime_ind, irqtimeps_ind,
868 endtime_ind);
869 spin_unlock(&device->profile.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200871
872void dasd_profile_reset(struct dasd_profile *profile)
873{
874 struct dasd_profile_info *data;
875
876 spin_lock_bh(&profile->lock);
877 data = profile->data;
878 if (!data) {
879 spin_unlock_bh(&profile->lock);
880 return;
881 }
882 memset(data, 0, sizeof(*data));
883 getnstimeofday(&data->starttod);
884 spin_unlock_bh(&profile->lock);
885}
886
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200887int dasd_profile_on(struct dasd_profile *profile)
888{
889 struct dasd_profile_info *data;
890
891 data = kzalloc(sizeof(*data), GFP_KERNEL);
892 if (!data)
893 return -ENOMEM;
894 spin_lock_bh(&profile->lock);
895 if (profile->data) {
896 spin_unlock_bh(&profile->lock);
897 kfree(data);
898 return 0;
899 }
900 getnstimeofday(&data->starttod);
901 profile->data = data;
902 spin_unlock_bh(&profile->lock);
903 return 0;
904}
905
906void dasd_profile_off(struct dasd_profile *profile)
907{
908 spin_lock_bh(&profile->lock);
909 kfree(profile->data);
910 profile->data = NULL;
911 spin_unlock_bh(&profile->lock);
912}
913
914char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
915{
916 char *buffer;
917
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200918 buffer = vmalloc(user_len + 1);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200919 if (buffer == NULL)
920 return ERR_PTR(-ENOMEM);
921 if (copy_from_user(buffer, user_buf, user_len) != 0) {
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200922 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200923 return ERR_PTR(-EFAULT);
924 }
925 /* got the string, now strip linefeed. */
926 if (buffer[user_len - 1] == '\n')
927 buffer[user_len - 1] = 0;
928 else
929 buffer[user_len] = 0;
930 return buffer;
931}
932
933static ssize_t dasd_stats_write(struct file *file,
934 const char __user *user_buf,
935 size_t user_len, loff_t *pos)
936{
937 char *buffer, *str;
938 int rc;
939 struct seq_file *m = (struct seq_file *)file->private_data;
940 struct dasd_profile *prof = m->private;
941
942 if (user_len > 65536)
943 user_len = 65536;
944 buffer = dasd_get_user_string(user_buf, user_len);
945 if (IS_ERR(buffer))
946 return PTR_ERR(buffer);
947
948 str = skip_spaces(buffer);
949 rc = user_len;
950 if (strncmp(str, "reset", 5) == 0) {
951 dasd_profile_reset(prof);
952 } else if (strncmp(str, "on", 2) == 0) {
953 rc = dasd_profile_on(prof);
Sebastian Ott6765cc22015-01-28 19:06:29 +0100954 if (rc)
955 goto out;
956 rc = user_len;
957 if (prof == &dasd_global_profile) {
958 dasd_profile_reset(prof);
959 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
960 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200961 } else if (strncmp(str, "off", 3) == 0) {
Sebastian Ott6765cc22015-01-28 19:06:29 +0100962 if (prof == &dasd_global_profile)
963 dasd_global_profile_level = DASD_PROFILE_OFF;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200964 dasd_profile_off(prof);
965 } else
966 rc = -EINVAL;
Sebastian Ott6765cc22015-01-28 19:06:29 +0100967out:
Stefan Weinhubere4258d52011-08-03 16:44:20 +0200968 vfree(buffer);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200969 return rc;
970}
971
972static void dasd_stats_array(struct seq_file *m, unsigned int *array)
973{
974 int i;
975
976 for (i = 0; i < 32; i++)
977 seq_printf(m, "%u ", array[i]);
978 seq_putc(m, '\n');
979}
980
981static void dasd_stats_seq_print(struct seq_file *m,
982 struct dasd_profile_info *data)
983{
984 seq_printf(m, "start_time %ld.%09ld\n",
985 data->starttod.tv_sec, data->starttod.tv_nsec);
986 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
987 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
988 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
989 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200990 seq_puts(m, "histogram_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200991 dasd_stats_array(m, data->dasd_io_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200992 seq_puts(m, "histogram_io_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200993 dasd_stats_array(m, data->dasd_io_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200994 seq_puts(m, "histogram_io_times_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200995 dasd_stats_array(m, data->dasd_io_timps);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200996 seq_puts(m, "histogram_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200997 dasd_stats_array(m, data->dasd_io_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +0200998 seq_puts(m, "histogram_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200999 dasd_stats_array(m, data->dasd_io_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001000 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001001 dasd_stats_array(m, data->dasd_io_time2ps);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001002 seq_puts(m, "histogram_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001003 dasd_stats_array(m, data->dasd_io_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001004 seq_puts(m, "histogram_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001005 dasd_stats_array(m, data->dasd_io_nr_req);
1006 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
1007 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
1008 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
1009 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001010 seq_puts(m, "histogram_read_sectors ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001011 dasd_stats_array(m, data->dasd_read_secs);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001012 seq_puts(m, "histogram_read_times ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001013 dasd_stats_array(m, data->dasd_read_times);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001014 seq_puts(m, "histogram_read_time_build_to_ssch ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001015 dasd_stats_array(m, data->dasd_read_time1);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001016 seq_puts(m, "histogram_read_time_ssch_to_irq ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001017 dasd_stats_array(m, data->dasd_read_time2);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001018 seq_puts(m, "histogram_read_time_irq_to_end ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001019 dasd_stats_array(m, data->dasd_read_time3);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001020 seq_puts(m, "histogram_read_ccw_queue_length ");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001021 dasd_stats_array(m, data->dasd_read_nr_req);
1022}
1023
1024static int dasd_stats_show(struct seq_file *m, void *v)
1025{
1026 struct dasd_profile *profile;
1027 struct dasd_profile_info *data;
1028
1029 profile = m->private;
1030 spin_lock_bh(&profile->lock);
1031 data = profile->data;
1032 if (!data) {
1033 spin_unlock_bh(&profile->lock);
Fabian Frederickc794caf2014-06-26 19:41:49 +02001034 seq_puts(m, "disabled\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001035 return 0;
1036 }
1037 dasd_stats_seq_print(m, data);
1038 spin_unlock_bh(&profile->lock);
1039 return 0;
1040}
1041
1042static int dasd_stats_open(struct inode *inode, struct file *file)
1043{
1044 struct dasd_profile *profile = inode->i_private;
1045 return single_open(file, dasd_stats_show, profile);
1046}
1047
1048static const struct file_operations dasd_stats_raw_fops = {
1049 .owner = THIS_MODULE,
1050 .open = dasd_stats_open,
1051 .read = seq_read,
1052 .llseek = seq_lseek,
1053 .release = single_release,
1054 .write = dasd_stats_write,
1055};
1056
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001057static void dasd_profile_init(struct dasd_profile *profile,
1058 struct dentry *base_dentry)
1059{
Al Virof4ae40a2011-07-24 04:33:43 -04001060 umode_t mode;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001061 struct dentry *pde;
1062
1063 if (!base_dentry)
1064 return;
1065 profile->dentry = NULL;
1066 profile->data = NULL;
1067 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1068 pde = debugfs_create_file("statistics", mode, base_dentry,
1069 profile, &dasd_stats_raw_fops);
1070 if (pde && !IS_ERR(pde))
1071 profile->dentry = pde;
1072 return;
1073}
1074
1075static void dasd_profile_exit(struct dasd_profile *profile)
1076{
1077 dasd_profile_off(profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001078 debugfs_remove(profile->dentry);
1079 profile->dentry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001080}
1081
1082static void dasd_statistics_removeroot(void)
1083{
1084 dasd_global_profile_level = DASD_PROFILE_OFF;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001085 dasd_profile_exit(&dasd_global_profile);
Fabian Frederickd7309aa2014-06-26 19:41:46 +02001086 debugfs_remove(dasd_debugfs_global_entry);
1087 debugfs_remove(dasd_debugfs_root_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001088}
1089
1090static void dasd_statistics_createroot(void)
1091{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001092 struct dentry *pde;
1093
1094 dasd_debugfs_root_entry = NULL;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001095 pde = debugfs_create_dir("dasd", NULL);
1096 if (!pde || IS_ERR(pde))
1097 goto error;
1098 dasd_debugfs_root_entry = pde;
1099 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1100 if (!pde || IS_ERR(pde))
1101 goto error;
1102 dasd_debugfs_global_entry = pde;
Sebastian Ott6765cc22015-01-28 19:06:29 +01001103 dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001104 return;
1105
1106error:
1107 DBF_EVENT(DBF_ERR, "%s",
1108 "Creation of the dasd debugfs interface failed");
1109 dasd_statistics_removeroot();
1110 return;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001114#define dasd_profile_start(block, cqr, req) do {} while (0)
1115#define dasd_profile_end(block, cqr, req) do {} while (0)
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001116
1117static void dasd_statistics_createroot(void)
1118{
1119 return;
1120}
1121
1122static void dasd_statistics_removeroot(void)
1123{
1124 return;
1125}
1126
1127int dasd_stats_generic_show(struct seq_file *m, void *v)
1128{
Fabian Frederickc794caf2014-06-26 19:41:49 +02001129 seq_puts(m, "Statistics are not activated in this kernel\n");
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02001130 return 0;
1131}
1132
1133static void dasd_profile_init(struct dasd_profile *profile,
1134 struct dentry *base_dentry)
1135{
1136 return;
1137}
1138
1139static void dasd_profile_exit(struct dasd_profile *profile)
1140{
1141 return;
1142}
1143
1144int dasd_profile_on(struct dasd_profile *profile)
1145{
1146 return 0;
1147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149#endif /* CONFIG_DASD_PROFILE */
1150
Stefan Haberland5a3b7b12016-03-18 09:42:13 +01001151static int dasd_hosts_show(struct seq_file *m, void *v)
1152{
1153 struct dasd_device *device;
1154 int rc = -EOPNOTSUPP;
1155
1156 device = m->private;
1157 dasd_get_device(device);
1158
1159 if (device->discipline->hosts_print)
1160 rc = device->discipline->hosts_print(device, m);
1161
1162 dasd_put_device(device);
1163 return rc;
1164}
1165
1166static int dasd_hosts_open(struct inode *inode, struct file *file)
1167{
1168 struct dasd_device *device = inode->i_private;
1169
1170 return single_open(file, dasd_hosts_show, device);
1171}
1172
1173static const struct file_operations dasd_hosts_fops = {
1174 .owner = THIS_MODULE,
1175 .open = dasd_hosts_open,
1176 .read = seq_read,
1177 .llseek = seq_lseek,
1178 .release = single_release,
1179};
1180
1181static void dasd_hosts_exit(struct dasd_device *device)
1182{
1183 debugfs_remove(device->hosts_dentry);
1184 device->hosts_dentry = NULL;
1185}
1186
1187static void dasd_hosts_init(struct dentry *base_dentry,
1188 struct dasd_device *device)
1189{
1190 struct dentry *pde;
1191 umode_t mode;
1192
1193 if (!base_dentry)
1194 return;
1195
1196 mode = S_IRUSR | S_IFREG;
1197 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1198 device, &dasd_hosts_fops);
1199 if (pde && !IS_ERR(pde))
1200 device->hosts_dentry = pde;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/*
1204 * Allocate memory for a channel program with 'cplength' channel
1205 * command words and 'datasize' additional space. There are two
1206 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1207 * memory and 2) dasd_smalloc_request uses the static ccw memory
1208 * that gets allocated for each device.
1209 */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001210struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001211 int datasize,
1212 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct dasd_ccw_req *cqr;
1215
1216 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +02001217 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +01001218 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001220 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (cqr == NULL)
1222 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 cqr->cpaddr = NULL;
1224 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001225 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 GFP_ATOMIC | GFP_DMA);
1227 if (cqr->cpaddr == NULL) {
1228 kfree(cqr);
1229 return ERR_PTR(-ENOMEM);
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 cqr->data = NULL;
1233 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001234 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -08001236 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 kfree(cqr);
1238 return ERR_PTR(-ENOMEM);
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001241 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1243 dasd_get_device(device);
1244 return cqr;
1245}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001246EXPORT_SYMBOL(dasd_kmalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Stefan Haberland68b781f2009-09-11 10:28:29 +02001248struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001249 int datasize,
1250 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 unsigned long flags;
1253 struct dasd_ccw_req *cqr;
1254 char *data;
1255 int size;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1258 if (cplength > 0)
1259 size += cplength * sizeof(struct ccw1);
1260 if (datasize > 0)
1261 size += datasize;
1262 spin_lock_irqsave(&device->mem_lock, flags);
1263 cqr = (struct dasd_ccw_req *)
1264 dasd_alloc_chunk(&device->ccw_chunks, size);
1265 spin_unlock_irqrestore(&device->mem_lock, flags);
1266 if (cqr == NULL)
1267 return ERR_PTR(-ENOMEM);
1268 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1269 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1270 cqr->cpaddr = NULL;
1271 if (cplength > 0) {
1272 cqr->cpaddr = (struct ccw1 *) data;
1273 data += cplength*sizeof(struct ccw1);
1274 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1275 }
1276 cqr->data = NULL;
1277 if (datasize > 0) {
1278 cqr->data = data;
1279 memset(cqr->data, 0, datasize);
1280 }
Stefan Haberland68b781f2009-09-11 10:28:29 +02001281 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1283 dasd_get_device(device);
1284 return cqr;
1285}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001286EXPORT_SYMBOL(dasd_smalloc_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/*
1289 * Free memory of a channel program. This function needs to free all the
1290 * idal lists that might have been created by dasd_set_cda and the
1291 * struct dasd_ccw_req itself.
1292 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001293void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct ccw1 *ccw;
1296
1297 /* Clear any idals used for the request. */
1298 ccw = cqr->cpaddr;
1299 do {
1300 clear_normalized_cda(ccw);
1301 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
Jesper Juhl17fd6822005-11-07 01:01:30 -08001302 kfree(cqr->cpaddr);
1303 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 kfree(cqr);
1305 dasd_put_device(device);
1306}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001307EXPORT_SYMBOL(dasd_kfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001309void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 unsigned long flags;
1312
1313 spin_lock_irqsave(&device->mem_lock, flags);
1314 dasd_free_chunk(&device->ccw_chunks, cqr);
1315 spin_unlock_irqrestore(&device->mem_lock, flags);
1316 dasd_put_device(device);
1317}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001318EXPORT_SYMBOL(dasd_sfree_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320/*
1321 * Check discipline magic in cqr.
1322 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001323static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct dasd_device *device;
1326
1327 if (cqr == NULL)
1328 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001329 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001331 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 " dasd_ccw_req 0x%08x magic doesn't match"
1333 " discipline 0x%08x",
1334 cqr->magic,
1335 *(unsigned int *) device->discipline->name);
1336 return -EINVAL;
1337 }
1338 return 0;
1339}
1340
1341/*
1342 * Terminate the current i/o and set the request to clear_pending.
1343 * Timer keeps device runnig.
1344 * ccw_device_clear can fail if the i/o subsystem
1345 * is in a bad mood.
1346 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001347int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct dasd_device *device;
1350 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001351 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* Check the cqr */
1354 rc = dasd_check_cqr(cqr);
1355 if (rc)
1356 return rc;
1357 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001358 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1360 rc = ccw_device_clear(device->cdev, (long) cqr);
1361 switch (rc) {
1362 case 0: /* termination successful */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001363 cqr->status = DASD_CQR_CLEAR_PENDING;
Heiko Carstens1aae0562013-01-30 09:49:40 +01001364 cqr->stopclk = get_tod_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001365 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 DBF_DEV_EVENT(DBF_DEBUG, device,
1367 "terminate cqr %p successful",
1368 cqr);
1369 break;
1370 case -ENODEV:
1371 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1372 "device gone, retry");
1373 break;
1374 case -EIO:
1375 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1376 "I/O error, retry");
1377 break;
1378 case -EINVAL:
Stefan Haberland2c171242014-11-24 10:53:19 +01001379 /*
1380 * device not valid so no I/O could be running
1381 * handle CQR as termination successful
1382 */
1383 cqr->status = DASD_CQR_CLEARED;
1384 cqr->stopclk = get_tod_clock();
1385 cqr->starttime = 0;
1386 /* no retries for invalid devices */
1387 cqr->retries = -1;
1388 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1389 "EINVAL, handle as terminated");
1390 /* fake rc to success */
1391 rc = 0;
1392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case -EBUSY:
1394 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1395 "device busy, retry later");
1396 break;
1397 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001398 /* internal error 10 - unknown rc*/
1399 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1400 dev_err(&device->cdev->dev, "An error occurred in the "
1401 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 BUG();
1403 break;
1404 }
1405 retries++;
1406 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001407 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return rc;
1409}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001410EXPORT_SYMBOL(dasd_term_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412/*
1413 * Start the i/o. This start_IO can fail if the channel is really busy.
1414 * In that case set up a timer to start the request later.
1415 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001416int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct dasd_device *device;
1419 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001420 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* Check the cqr */
1423 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001424 if (rc) {
1425 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001427 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001428 device = (struct dasd_device *) cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001429 if (((cqr->block &&
1430 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1431 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1432 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1433 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1434 "because of stolen lock", cqr);
1435 cqr->status = DASD_CQR_ERROR;
1436 cqr->intrc = -EPERM;
1437 return -EPERM;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001440 /* internal error 14 - start_IO run out of retries */
1441 sprintf(errorstring, "14 %p", cqr);
1442 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1443 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001444 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EIO;
1446 }
Heiko Carstens1aae0562013-01-30 09:49:40 +01001447 cqr->startclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 cqr->starttime = jiffies;
1449 cqr->retries--;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001450 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1451 cqr->lpm &= device->path_data.opm;
1452 if (!cqr->lpm)
1453 cqr->lpm = device->path_data.opm;
1454 }
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001455 if (cqr->cpmode == 1) {
1456 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1457 (long) cqr, cqr->lpm);
1458 } else {
1459 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1460 (long) cqr, cqr->lpm, 0);
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 switch (rc) {
1463 case 0:
1464 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 break;
1466 case -EBUSY:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001467 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 "start_IO: device busy, retry later");
1469 break;
1470 case -ETIMEDOUT:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001471 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 "start_IO: request timeout, retry later");
1473 break;
1474 case -EACCES:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001475 /* -EACCES indicates that the request used only a subset of the
1476 * available paths and all these paths are gone. If the lpm of
1477 * this request was only a subset of the opm (e.g. the ppm) then
1478 * we just do a retry with all available paths.
1479 * If we already use the full opm, something is amiss, and we
1480 * need a full path verification.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001482 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1483 DBF_DEV_EVENT(DBF_WARNING, device,
1484 "start_IO: selected paths gone (%x)",
1485 cqr->lpm);
1486 } else if (cqr->lpm != device->path_data.opm) {
1487 cqr->lpm = device->path_data.opm;
1488 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1489 "start_IO: selected paths gone,"
1490 " retry on all paths");
1491 } else {
1492 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1493 "start_IO: all paths in opm gone,"
1494 " do path verification");
1495 dasd_generic_last_path_gone(device);
1496 device->path_data.opm = 0;
1497 device->path_data.ppm = 0;
1498 device->path_data.npm = 0;
1499 device->path_data.tbvpm =
1500 ccw_device_get_path_mask(device->cdev);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 break;
1503 case -ENODEV:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001504 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001505 "start_IO: -ENODEV device gone, retry");
1506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 case -EIO:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001508 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001509 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001511 case -EINVAL:
1512 /* most likely caused in power management context */
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001513 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Haberlandd41dd122009-06-16 10:30:25 +02001514 "start_IO: -EINVAL device currently "
1515 "not accessible");
1516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001518 /* internal error 11 - unknown rc */
1519 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1520 dev_err(&device->cdev->dev,
1521 "An error occurred in the DASD device driver, "
1522 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 BUG();
1524 break;
1525 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001526 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return rc;
1528}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001529EXPORT_SYMBOL(dasd_start_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/*
1532 * Timeout function for dasd devices. This is used for different purposes
1533 * 1) missing interrupt handler for normal operation
1534 * 2) delayed start of request where start_IO failed with -EBUSY
1535 * 3) timeout for missing state change interrupts
1536 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1537 * DASD_CQR_QUEUED for 2) and 3).
1538 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001539static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 unsigned long flags;
1542 struct dasd_device *device;
1543
1544 device = (struct dasd_device *) ptr;
1545 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1546 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001547 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001549 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/*
1553 * Setup timeout for a device in jiffies.
1554 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001555void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001557 if (expires == 0)
1558 del_timer(&device->timer);
1559 else
1560 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001562EXPORT_SYMBOL(dasd_device_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564/*
1565 * Clear timeout for a device.
1566 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001567void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001569 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001571EXPORT_SYMBOL(dasd_device_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001573static void dasd_handle_killed_request(struct ccw_device *cdev,
1574 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct dasd_ccw_req *cqr;
1577 struct dasd_device *device;
1578
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001579 if (!intparm)
1580 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 cqr = (struct dasd_ccw_req *) intparm;
1582 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001583 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1584 "invalid status in handle_killed_request: "
1585 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return;
1587 }
1588
Stefan Haberland589c74d2010-02-26 22:37:47 +01001589 device = dasd_device_from_cdev_locked(cdev);
1590 if (IS_ERR(device)) {
1591 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1592 "unable to get device from cdev");
1593 return;
1594 }
1595
1596 if (!cqr->startdev ||
1597 device != cqr->startdev ||
1598 strncmp(cqr->startdev->discipline->ebcname,
1599 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001600 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1601 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001602 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return;
1604 }
1605
1606 /* Schedule request to be retried. */
1607 cqr->status = DASD_CQR_QUEUED;
1608
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001609 dasd_device_clear_timer(device);
1610 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 dasd_put_device(device);
1612}
1613
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001614void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001616 /* First of all start sense subsystem status request. */
1617 dasd_eer_snss(device);
1618
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001619 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001620 dasd_schedule_device_bh(device);
1621 if (device->block)
1622 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001624EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626/*
1627 * Interrupt handler for "normal" ssch-io based dasd devices.
1628 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001629void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1630 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 struct dasd_ccw_req *cqr, *next;
1633 struct dasd_device *device;
1634 unsigned long long now;
Jan Höppner8fd57522015-08-19 13:41:20 +02001635 int nrf_suppressed = 0;
1636 int fp_suppressed = 0;
1637 u8 *sense = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001640 cqr = (struct dasd_ccw_req *) intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (IS_ERR(irb)) {
1642 switch (PTR_ERR(irb)) {
1643 case -EIO:
Stefan Haberland9ba333d2016-08-08 14:08:17 +02001644 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
1645 device = (struct dasd_device *) cqr->startdev;
1646 cqr->status = DASD_CQR_CLEARED;
1647 dasd_device_clear_timer(device);
1648 wake_up(&dasd_flush_wq);
1649 dasd_schedule_device_bh(device);
1650 return;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
1653 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001654 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1655 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 break;
1657 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001658 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1659 "unknown error %ld\n", __func__,
1660 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001662 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
1664 }
1665
Heiko Carstens1aae0562013-01-30 09:49:40 +01001666 now = get_tod_clock();
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001667 /* check for conditions that should be handled immediately */
1668 if (!cqr ||
1669 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1670 scsw_cstat(&irb->scsw) == 0)) {
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001671 if (cqr)
1672 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001673 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001674 if (IS_ERR(device))
1675 return;
1676 /* ignore unsolicited interrupts for DIAG discipline */
1677 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Jan Höppner8fd57522015-08-19 13:41:20 +02001681
1682 /*
1683 * In some cases 'File Protected' or 'No Record Found' errors
1684 * might be expected and debug log messages for the
1685 * corresponding interrupts shouldn't be written then.
1686 * Check if either of the according suppress bits is set.
1687 */
1688 sense = dasd_get_sense(irb);
1689 if (sense) {
1690 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1691 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1692 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1693 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1694 }
1695 if (!(fp_suppressed || nrf_suppressed))
1696 device->discipline->dump_sense_dbf(device, irb, "int");
1697
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001698 if (device->features & DASD_FEATURE_ERPLOG)
1699 device->discipline->dump_sense(device, cqr, irb);
1700 device->discipline->check_for_device_change(device, cqr, irb);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001701 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001703
1704 /* check for for attention message */
1705 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1706 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberlandba56e6c2017-10-07 22:36:56 +00001707 if (!IS_ERR(device)) {
1708 device->discipline->check_attention(device,
1709 irb->esw.esw1.lpum);
1710 dasd_put_device(device);
1711 }
Stefan Haberland5db84402014-10-01 14:39:47 +02001712 }
1713
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001714 if (!cqr)
1715 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001717 device = (struct dasd_device *) cqr->startdev;
1718 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001720 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1721 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return;
1723 }
1724
1725 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001726 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001727 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001728 cqr->status = DASD_CQR_CLEARED;
1729 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001730 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001731 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return;
1733 }
1734
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001735 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001737 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1738 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return;
1740 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001741
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001744 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1745 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001746 /* request was completed successfully */
1747 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 cqr->stopclk = now;
1749 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001750 if (cqr->devlist.next != &device->ccw_queue) {
1751 next = list_entry(cqr->devlist.next,
1752 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001754 } else { /* error */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001755 /*
1756 * If we don't want complex ERP for this request, then just
1757 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001758 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001759 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 cqr->retries > 0) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001761 if (cqr->lpm == device->path_data.opm)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001762 DBF_DEV_EVENT(DBF_DEBUG, device,
1763 "default ERP in fastpath "
1764 "(%i retries left)",
1765 cqr->retries);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001766 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1767 cqr->lpm = device->path_data.opm;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001768 cqr->status = DASD_CQR_QUEUED;
1769 next = cqr;
1770 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001772 }
1773 if (next && (next->status == DASD_CQR_QUEUED) &&
1774 (!device->stopped)) {
1775 if (device->discipline->start_IO(next) == 0)
1776 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001779 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001781 dasd_device_clear_timer(device);
1782 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02001784EXPORT_SYMBOL(dasd_int_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001786enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1787{
1788 struct dasd_device *device;
1789
1790 device = dasd_device_from_cdev_locked(cdev);
1791
1792 if (IS_ERR(device))
1793 goto out;
1794 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1795 device->state != device->target ||
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001796 !device->discipline->check_for_device_change){
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001797 dasd_put_device(device);
1798 goto out;
1799 }
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01001800 if (device->discipline->dump_sense_dbf)
1801 device->discipline->dump_sense_dbf(device, irb, "uc");
1802 device->discipline->check_for_device_change(device, NULL, irb);
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001803 dasd_put_device(device);
1804out:
1805 return UC_TODO_RETRY;
1806}
1807EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001810 * If we have an error on a dasd_block layer request then we cancel
1811 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001813static void __dasd_device_recovery(struct dasd_device *device,
1814 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 struct list_head *l, *n;
1817 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001820 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001822 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001824
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001825 list_for_each_safe(l, n, &device->ccw_queue) {
1826 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1827 if (cqr->status == DASD_CQR_QUEUED &&
1828 ref_cqr->block == cqr->block) {
1829 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001831 }
1832};
1833
1834/*
1835 * Remove those ccw requests from the queue that need to be returned
1836 * to the upper layer.
1837 */
1838static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1839 struct list_head *final_queue)
1840{
1841 struct list_head *l, *n;
1842 struct dasd_ccw_req *cqr;
1843
1844 /* Process request with final status. */
1845 list_for_each_safe(l, n, &device->ccw_queue) {
1846 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1847
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001848 /* Skip any non-final request. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001849 if (cqr->status == DASD_CQR_QUEUED ||
1850 cqr->status == DASD_CQR_IN_IO ||
1851 cqr->status == DASD_CQR_CLEAR_PENDING)
Hannes Reinecke1fbdb8b2013-01-30 09:26:13 +00001852 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001853 if (cqr->status == DASD_CQR_ERROR) {
1854 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001856 /* Rechain finished requests to final queue */
1857 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859}
1860
1861/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001862 * the cqrs from the final queue are returned to the upper layer
1863 * by setting a dasd_block state and calling the callback function
1864 */
1865static void __dasd_device_process_final_queue(struct dasd_device *device,
1866 struct list_head *final_queue)
1867{
1868 struct list_head *l, *n;
1869 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001870 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001871 void (*callback)(struct dasd_ccw_req *, void *data);
1872 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001873 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001874
1875 list_for_each_safe(l, n, final_queue) {
1876 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1877 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001878 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001879 callback = cqr->callback;
1880 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001881 if (block)
1882 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001883 switch (cqr->status) {
1884 case DASD_CQR_SUCCESS:
1885 cqr->status = DASD_CQR_DONE;
1886 break;
1887 case DASD_CQR_ERROR:
1888 cqr->status = DASD_CQR_NEED_ERP;
1889 break;
1890 case DASD_CQR_CLEARED:
1891 cqr->status = DASD_CQR_TERMINATED;
1892 break;
1893 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001894 /* internal error 12 - wrong cqr status*/
1895 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1896 dev_err(&device->cdev->dev,
1897 "An error occurred in the DASD device driver, "
1898 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001899 BUG();
1900 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001901 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001902 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001903 if (block)
1904 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001905 }
1906}
1907
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001908/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 * Take a look at the first request on the ccw queue and check
1910 * if it reached its expire time. If so, terminate the IO.
1911 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001912static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct dasd_ccw_req *cqr;
1915
1916 if (list_empty(&device->ccw_queue))
1917 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001918 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001919 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1920 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01001921 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1922 /*
1923 * IO in safe offline processing should not
1924 * run out of retries
1925 */
1926 cqr->retries++;
1927 }
Horst Hummel29145a62006-12-04 15:40:15 +01001928 if (device->discipline->term_IO(cqr) != 0) {
1929 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001930 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001931 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001932 "ended, retrying in 5 s\n",
1933 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001934 cqr->expires += 5*HZ;
1935 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001936 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001937 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001938 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001939 "remaining\n", cqr, (cqr->expires/HZ),
1940 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 }
1943}
1944
1945/*
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001946 * return 1 when device is not eligible for IO
1947 */
1948static int __dasd_device_is_unusable(struct dasd_device *device,
1949 struct dasd_ccw_req *cqr)
1950{
1951 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1952
1953 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1954 /* dasd is being set offline. */
1955 return 1;
1956 }
1957 if (device->stopped) {
1958 if (device->stopped & mask) {
1959 /* stopped and CQR will not change that. */
1960 return 1;
1961 }
1962 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1963 /* CQR is not able to change device to
1964 * operational. */
1965 return 1;
1966 }
1967 /* CQR required to get device operational. */
1968 }
1969 return 0;
1970}
1971
1972/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * Take a look at the first request on the ccw queue and check
1974 * if it needs to be started.
1975 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001976static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 struct dasd_ccw_req *cqr;
1979 int rc;
1980
1981 if (list_empty(&device->ccw_queue))
1982 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001983 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001984 if (cqr->status != DASD_CQR_QUEUED)
1985 return;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +02001986 /* if device is not usable return request to upper layer */
1987 if (__dasd_device_is_unusable(device, cqr)) {
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01001988 cqr->intrc = -EAGAIN;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001989 cqr->status = DASD_CQR_CLEARED;
1990 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001991 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001992 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001993
1994 rc = device->discipline->start_IO(cqr);
1995 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001996 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001997 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001998 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001999 } else
2000 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002001 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02002002}
2003
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002004static void __dasd_device_check_path_events(struct dasd_device *device)
2005{
2006 int rc;
2007
2008 if (device->path_data.tbvpm) {
2009 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
2010 DASD_UNRESUMED_PM))
2011 return;
2012 rc = device->discipline->verify_path(
2013 device, device->path_data.tbvpm);
2014 if (rc)
2015 dasd_device_set_timer(device, 50);
2016 else
2017 device->path_data.tbvpm = 0;
2018 }
2019};
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002022 * Go through all request on the dasd_device request queue,
2023 * terminate them on the cdev if necessary, and return them to the
2024 * submitting layer via callback.
2025 * Note:
2026 * Make sure that all 'submitting layers' still exist when
2027 * this function is called!. In other words, when 'device' is a base
2028 * device then all block layer requests must have been removed before
2029 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002031int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002033 struct dasd_ccw_req *cqr, *n;
2034 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 INIT_LIST_HEAD(&flush_queue);
2038 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02002039 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002040 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02002041 /* Check status and move request to flush_queue */
2042 switch (cqr->status) {
2043 case DASD_CQR_IN_IO:
2044 rc = device->discipline->term_IO(cqr);
2045 if (rc) {
2046 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002047 dev_err(&device->cdev->dev,
2048 "Flushing the DASD request queue "
2049 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02002050 /* stop flush processing */
2051 goto finished;
2052 }
2053 break;
2054 case DASD_CQR_QUEUED:
Heiko Carstens1aae0562013-01-30 09:49:40 +01002055 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002056 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02002057 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002058 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02002059 break;
2060 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002061 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002062 }
Horst Hummel8f617012006-08-30 14:33:33 +02002063finished:
2064 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002065 /*
2066 * After this point all requests must be in state CLEAR_PENDING,
2067 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2068 * one of the others.
2069 */
2070 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2071 wait_event(dasd_flush_wq,
2072 (cqr->status != DASD_CQR_CLEAR_PENDING));
2073 /*
2074 * Now set each request back to TERMINATED, DONE or NEED_ERP
2075 * and call the callback function of flushed requests
2076 */
2077 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02002078 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002080EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082/*
2083 * Acquire the device lock and process queues for the device.
2084 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002085static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 atomic_set (&device->tasklet_scheduled, 0);
2090 INIT_LIST_HEAD(&final_queue);
2091 spin_lock_irq(get_ccwdev_lock(device->cdev));
2092 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002093 __dasd_device_check_expire(device);
2094 /* find final requests on ccw queue */
2095 __dasd_device_process_ccw_queue(device, &final_queue);
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01002096 __dasd_device_check_path_events(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2098 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002099 __dasd_device_process_final_queue(device, &final_queue);
2100 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 __dasd_device_start_head(device);
2103 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Haberland4679e892012-06-19 17:30:12 +02002104 if (waitqueue_active(&shutdown_waitq))
2105 wake_up(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 dasd_put_device(device);
2107}
2108
2109/*
2110 * Schedules a call to dasd_tasklet over the device tasklet.
2111 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002112void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08002115 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return;
2117 dasd_get_device(device);
2118 tasklet_hi_schedule(&device->tasklet);
2119}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002120EXPORT_SYMBOL(dasd_schedule_device_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002122void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2123{
2124 device->stopped |= bits;
2125}
2126EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2127
2128void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2129{
2130 device->stopped &= ~bits;
2131 if (!device->stopped)
2132 wake_up(&generic_waitq);
2133}
2134EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002137 * Queue a request to the head of the device ccw_queue.
2138 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002140void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct dasd_device *device;
2143 unsigned long flags;
2144
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002145 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002147 cqr->status = DASD_CQR_QUEUED;
2148 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002150 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2152}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002153EXPORT_SYMBOL(dasd_add_request_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002156 * Queue a request to the tail of the device ccw_queue.
2157 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002159void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct dasd_device *device;
2162 unsigned long flags;
2163
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002164 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002166 cqr->status = DASD_CQR_QUEUED;
2167 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002169 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2171}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002172EXPORT_SYMBOL(dasd_add_request_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002175 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Stefan Haberland5915a872011-10-30 15:16:57 +01002177void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002179 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2180 cqr->callback_data = DASD_SLEEPON_END_TAG;
2181 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2182 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
Stefan Haberland5915a872011-10-30 15:16:57 +01002184EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002186static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct dasd_device *device;
2189 int rc;
2190
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002191 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002193 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2195 return rc;
2196}
2197
2198/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002199 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2200 */
2201static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2202{
2203 struct dasd_device *device;
2204 dasd_erp_fn_t erp_fn;
2205
2206 if (cqr->status == DASD_CQR_FILLED)
2207 return 0;
2208 device = cqr->startdev;
2209 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2210 if (cqr->status == DASD_CQR_TERMINATED) {
2211 device->discipline->handle_terminated_request(cqr);
2212 return 1;
2213 }
2214 if (cqr->status == DASD_CQR_NEED_ERP) {
2215 erp_fn = device->discipline->erp_action(cqr);
2216 erp_fn(cqr);
2217 return 1;
2218 }
2219 if (cqr->status == DASD_CQR_FAILED)
2220 dasd_log_sense(cqr, &cqr->irb);
2221 if (cqr->refers) {
2222 __dasd_process_erp(device, cqr);
2223 return 1;
2224 }
2225 }
2226 return 0;
2227}
2228
2229static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2230{
2231 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2232 if (cqr->refers) /* erp is not done yet */
2233 return 1;
2234 return ((cqr->status != DASD_CQR_DONE) &&
2235 (cqr->status != DASD_CQR_FAILED));
2236 } else
2237 return (cqr->status == DASD_CQR_FILLED);
2238}
2239
2240static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2241{
2242 struct dasd_device *device;
2243 int rc;
2244 struct list_head ccw_queue;
2245 struct dasd_ccw_req *cqr;
2246
2247 INIT_LIST_HEAD(&ccw_queue);
2248 maincqr->status = DASD_CQR_FILLED;
2249 device = maincqr->startdev;
2250 list_add(&maincqr->blocklist, &ccw_queue);
2251 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2252 cqr = list_first_entry(&ccw_queue,
2253 struct dasd_ccw_req, blocklist)) {
2254
2255 if (__dasd_sleep_on_erp(cqr))
2256 continue;
2257 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2258 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002259 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2260 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2261 cqr->status = DASD_CQR_FAILED;
2262 cqr->intrc = -EPERM;
2263 continue;
2264 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002265 /* Non-temporary stop condition will trigger fail fast */
2266 if (device->stopped & ~DASD_STOPPED_PENDING &&
2267 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2268 (!dasd_eer_enabled(device))) {
2269 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002270 cqr->intrc = -ENOLINK;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002271 continue;
2272 }
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002273 /*
Stefan Haberlanda9f62732016-09-20 10:29:22 +02002274 * Don't try to start requests if device is in
2275 * offline processing, it might wait forever
2276 */
2277 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2278 cqr->status = DASD_CQR_FAILED;
2279 cqr->intrc = -ENODEV;
2280 continue;
2281 }
2282 /*
Stefan Haberlanddf3044f2015-04-02 13:18:39 +02002283 * Don't try to start requests if device is stopped
2284 * except path verification requests
2285 */
2286 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2287 if (interruptible) {
2288 rc = wait_event_interruptible(
2289 generic_waitq, !(device->stopped));
2290 if (rc == -ERESTARTSYS) {
2291 cqr->status = DASD_CQR_FAILED;
2292 maincqr->intrc = rc;
2293 continue;
2294 }
2295 } else
2296 wait_event(generic_waitq, !(device->stopped));
2297 }
Stefan Haberland5915a872011-10-30 15:16:57 +01002298 if (!cqr->callback)
2299 cqr->callback = dasd_wakeup_cb;
2300
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002301 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002302 dasd_add_request_tail(cqr);
2303 if (interruptible) {
2304 rc = wait_event_interruptible(
2305 generic_waitq, _wait_for_wakeup(cqr));
2306 if (rc == -ERESTARTSYS) {
2307 dasd_cancel_req(cqr);
2308 /* wait (non-interruptible) for final status */
2309 wait_event(generic_waitq,
2310 _wait_for_wakeup(cqr));
2311 cqr->status = DASD_CQR_FAILED;
2312 maincqr->intrc = rc;
2313 continue;
2314 }
2315 } else
2316 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2317 }
2318
Heiko Carstens1aae0562013-01-30 09:49:40 +01002319 maincqr->endclk = get_tod_clock();
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002320 if ((maincqr->status != DASD_CQR_DONE) &&
2321 (maincqr->intrc != -ERESTARTSYS))
2322 dasd_log_sense(maincqr, &maincqr->irb);
2323 if (maincqr->status == DASD_CQR_DONE)
2324 rc = 0;
2325 else if (maincqr->intrc)
2326 rc = maincqr->intrc;
2327 else
2328 rc = -EIO;
2329 return rc;
2330}
2331
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002332static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2333{
2334 struct dasd_ccw_req *cqr;
2335
2336 list_for_each_entry(cqr, ccw_queue, blocklist) {
2337 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2338 return 0;
2339 }
2340
2341 return 1;
2342}
2343
2344static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2345{
2346 struct dasd_device *device;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002347 struct dasd_ccw_req *cqr, *n;
Jan Höppner8fd57522015-08-19 13:41:20 +02002348 u8 *sense = NULL;
Stefan Haberland362ce842014-10-01 13:04:54 +02002349 int rc;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002350
2351retry:
2352 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2353 device = cqr->startdev;
2354 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2355 continue;
2356
2357 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2358 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2359 cqr->status = DASD_CQR_FAILED;
2360 cqr->intrc = -EPERM;
2361 continue;
2362 }
2363 /*Non-temporary stop condition will trigger fail fast*/
2364 if (device->stopped & ~DASD_STOPPED_PENDING &&
2365 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2366 !dasd_eer_enabled(device)) {
2367 cqr->status = DASD_CQR_FAILED;
2368 cqr->intrc = -EAGAIN;
2369 continue;
2370 }
2371
2372 /*Don't try to start requests if device is stopped*/
2373 if (interruptible) {
2374 rc = wait_event_interruptible(
2375 generic_waitq, !device->stopped);
2376 if (rc == -ERESTARTSYS) {
2377 cqr->status = DASD_CQR_FAILED;
2378 cqr->intrc = rc;
2379 continue;
2380 }
2381 } else
2382 wait_event(generic_waitq, !(device->stopped));
2383
2384 if (!cqr->callback)
2385 cqr->callback = dasd_wakeup_cb;
2386 cqr->callback_data = DASD_SLEEPON_START_TAG;
2387 dasd_add_request_tail(cqr);
2388 }
2389
2390 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2391
2392 rc = 0;
2393 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002394 /*
Jan Höppner8fd57522015-08-19 13:41:20 +02002395 * In some cases the 'File Protected' or 'Incorrect Length'
2396 * error might be expected and error recovery would be
2397 * unnecessary in these cases. Check if the according suppress
2398 * bit is set.
2399 */
2400 sense = dasd_get_sense(&cqr->irb);
2401 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2402 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2403 continue;
2404 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2405 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2406 continue;
2407
2408 /*
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002409 * for alias devices simplify error recovery and
2410 * return to upper layer
Stefan Haberland362ce842014-10-01 13:04:54 +02002411 * do not skip ERP requests
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002412 */
Stefan Haberland362ce842014-10-01 13:04:54 +02002413 if (cqr->startdev != cqr->basedev && !cqr->refers &&
Stefan Haberland29b8dd92014-07-18 14:26:01 +02002414 (cqr->status == DASD_CQR_TERMINATED ||
2415 cqr->status == DASD_CQR_NEED_ERP))
2416 return -EAGAIN;
Stefan Haberland362ce842014-10-01 13:04:54 +02002417
2418 /* normal recovery for basedev IO */
Stefan Haberland590aeed2014-11-24 10:45:47 +01002419 if (__dasd_sleep_on_erp(cqr))
2420 /* handle erp first */
Stefan Haberland362ce842014-10-01 13:04:54 +02002421 goto retry;
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002422 }
Stefan Haberland362ce842014-10-01 13:04:54 +02002423
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002424 return 0;
2425}
2426
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002427/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002428 * Queue a request to the tail of the device ccw_queue and wait for
2429 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002431int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002433 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002435EXPORT_SYMBOL(dasd_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437/*
Stefan Haberlandd42e1712013-04-15 16:22:23 +02002438 * Start requests from a ccw_queue and wait for their completion.
2439 */
2440int dasd_sleep_on_queue(struct list_head *ccw_queue)
2441{
2442 return _dasd_sleep_on_queue(ccw_queue, 0);
2443}
2444EXPORT_SYMBOL(dasd_sleep_on_queue);
2445
2446/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002447 * Queue a request to the tail of the device ccw_queue and wait
2448 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002450int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002452 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002454EXPORT_SYMBOL(dasd_sleep_on_interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456/*
2457 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2458 * for eckd devices) the currently running request has to be terminated
2459 * and be put back to status queued, before the special request is added
2460 * to the head of the queue. Then the special request is waited on normally.
2461 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002462static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct dasd_ccw_req *cqr;
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002465 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 if (list_empty(&device->ccw_queue))
2468 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002469 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Stefan Haberlandaade6c02011-05-10 17:13:38 +02002470 rc = device->discipline->term_IO(cqr);
2471 if (!rc)
2472 /*
2473 * CQR terminated because a more important request is pending.
2474 * Undo decreasing of retry counter because this is
2475 * not an error case.
2476 */
2477 cqr->retries++;
2478 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002481int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 struct dasd_device *device;
2484 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02002485
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002486 device = cqr->startdev;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002487 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2488 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2489 cqr->status = DASD_CQR_FAILED;
2490 cqr->intrc = -EPERM;
2491 return -EIO;
2492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 spin_lock_irq(get_ccwdev_lock(device->cdev));
2494 rc = _dasd_term_running_cqr(device);
2495 if (rc) {
2496 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2497 return rc;
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02002500 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 cqr->status = DASD_CQR_QUEUED;
Stefan Haberland214b8ff2011-10-30 15:16:56 +01002502 /*
2503 * add new request as second
2504 * first the terminated cqr needs to be finished
2505 */
2506 list_add(&cqr->devlist, device->ccw_queue.next);
Horst Hummel138c0142006-06-29 14:58:12 +02002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002509 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2512
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002513 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02002514
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02002515 if (cqr->status == DASD_CQR_DONE)
2516 rc = 0;
2517 else if (cqr->intrc)
2518 rc = cqr->intrc;
2519 else
2520 rc = -EIO;
Stefan Haberland0e003b72013-07-30 10:49:43 +02002521
2522 /* kick tasklets */
2523 dasd_schedule_device_bh(device);
2524 if (device->block)
2525 dasd_schedule_block_bh(device->block);
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return rc;
2528}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002529EXPORT_SYMBOL(dasd_sleep_on_immediatly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531/*
2532 * Cancels a request that was started with dasd_sleep_on_req.
2533 * This is useful to timeout requests. The request will be
2534 * terminated if it is currently in i/o.
Hannes Reineckeb99a9462013-01-30 09:26:11 +00002535 * Returns 0 if request termination was successful
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002536 * negative error code if termination failed
2537 * Cancellation of a request is an asynchronous operation! The calling
2538 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002540int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002542 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 unsigned long flags;
2544 int rc;
2545
2546 rc = 0;
2547 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2548 switch (cqr->status) {
2549 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002550 /* request was not started - just set to cleared */
2551 cqr->status = DASD_CQR_CLEARED;
Stefan Haberland931a3dc2014-07-18 14:22:41 +02002552 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2553 cqr->callback_data = DASD_SLEEPON_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 break;
2555 case DASD_CQR_IN_IO:
2556 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002557 rc = device->discipline->term_IO(cqr);
2558 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002559 dev_err(&device->cdev->dev,
2560 "Cancelling request %p failed with rc=%d\n",
2561 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002562 } else {
Heiko Carstens1aae0562013-01-30 09:49:40 +01002563 cqr->stopclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002566 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002570 dasd_schedule_device_bh(device);
2571 return rc;
2572}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002573EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002574
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002575/*
2576 * SECTION: Operations of the dasd_block layer.
2577 */
2578
2579/*
2580 * Timeout function for dasd_block. This is used when the block layer
2581 * is waiting for something that may not come reliably, (e.g. a state
2582 * change interrupt)
2583 */
2584static void dasd_block_timeout(unsigned long ptr)
2585{
2586 unsigned long flags;
2587 struct dasd_block *block;
2588
2589 block = (struct dasd_block *) ptr;
2590 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2591 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002592 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002593 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2594 dasd_schedule_block_bh(block);
2595}
2596
2597/*
2598 * Setup timeout for a dasd_block in jiffies.
2599 */
2600void dasd_block_set_timer(struct dasd_block *block, int expires)
2601{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002602 if (expires == 0)
2603 del_timer(&block->timer);
2604 else
2605 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002606}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002607EXPORT_SYMBOL(dasd_block_set_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002608
2609/*
2610 * Clear timeout for a dasd_block.
2611 */
2612void dasd_block_clear_timer(struct dasd_block *block)
2613{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01002614 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002615}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02002616EXPORT_SYMBOL(dasd_block_clear_timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002617
2618/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002619 * Process finished error recovery ccw.
2620 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002621static void __dasd_process_erp(struct dasd_device *device,
2622 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002623{
2624 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002625
2626 if (cqr->status == DASD_CQR_DONE)
2627 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2628 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002629 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002630 erp_fn = device->discipline->erp_postaction(cqr);
2631 erp_fn(cqr);
2632}
2633
2634/*
2635 * Fetch requests from the block device queue.
2636 */
2637static void __dasd_process_request_queue(struct dasd_block *block)
2638{
2639 struct request_queue *queue;
2640 struct request *req;
2641 struct dasd_ccw_req *cqr;
2642 struct dasd_device *basedev;
2643 unsigned long flags;
2644 queue = block->request_queue;
2645 basedev = block->base;
2646 /* No queue ? Then there is nothing to do. */
2647 if (queue == NULL)
2648 return;
2649
2650 /*
2651 * We requeue request from the block device queue to the ccw
2652 * queue only in two states. In state DASD_STATE_READY the
2653 * partition detection is done and we need to requeue requests
2654 * for that. State DASD_STATE_ONLINE is normal block device
2655 * operation.
2656 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002657 if (basedev->state < DASD_STATE_READY) {
2658 while ((req = blk_fetch_request(block->request_queue)))
2659 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002660 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02002661 }
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002662
Stefan Haberlandc6fc7b62015-12-22 13:34:38 +01002663 /*
2664 * if device is stopped do not fetch new requests
2665 * except failfast is active which will let requests fail
2666 * immediately in __dasd_block_start_head()
2667 */
2668 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST))
Stefan Haberlanda3147a72015-04-02 12:52:41 +02002669 return;
2670
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002671 /* Now we try to fetch requests from the request queue */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002672 while ((req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002673 if (basedev->features & DASD_FEATURE_READONLY &&
2674 rq_data_dir(req) == WRITE) {
2675 DBF_DEV_EVENT(DBF_ERR, basedev,
2676 "Rejecting write request %p",
2677 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002678 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002679 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002680 continue;
2681 }
Hannes Reinecke5ea34a02013-01-30 09:26:19 +00002682 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2683 (basedev->features & DASD_FEATURE_FAILFAST ||
2684 blk_noretry_request(req))) {
2685 DBF_DEV_EVENT(DBF_ERR, basedev,
2686 "Rejecting failfast request %p",
2687 req);
2688 blk_start_request(req);
2689 __blk_end_request_all(req, -ETIMEDOUT);
2690 continue;
2691 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002692 cqr = basedev->discipline->build_cp(basedev, block, req);
2693 if (IS_ERR(cqr)) {
2694 if (PTR_ERR(cqr) == -EBUSY)
2695 break; /* normal end condition */
2696 if (PTR_ERR(cqr) == -ENOMEM)
2697 break; /* terminate request queue loop */
2698 if (PTR_ERR(cqr) == -EAGAIN) {
2699 /*
2700 * The current request cannot be build right
2701 * now, we have to try later. If this request
2702 * is the head-of-queue we stop the device
2703 * for 1/2 second.
2704 */
2705 if (!list_empty(&block->ccw_queue))
2706 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002707 spin_lock_irqsave(
2708 get_ccwdev_lock(basedev->cdev), flags);
2709 dasd_device_set_stop_bits(basedev,
2710 DASD_STOPPED_PENDING);
2711 spin_unlock_irqrestore(
2712 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002713 dasd_block_set_timer(block, HZ/2);
2714 break;
2715 }
2716 DBF_DEV_EVENT(DBF_ERR, basedev,
2717 "CCW creation failed (rc=%ld) "
2718 "on request %p",
2719 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002720 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002721 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002722 continue;
2723 }
2724 /*
2725 * Note: callback is set to dasd_return_cqr_cb in
2726 * __dasd_block_start_head to cover erp requests as well
2727 */
2728 cqr->callback_data = (void *) req;
2729 cqr->status = DASD_CQR_FILLED;
Hannes Reineckea2ace462013-01-30 09:26:14 +00002730 req->completion_data = cqr;
Tejun Heo9934c8c2009-05-08 11:54:16 +09002731 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002732 list_add_tail(&cqr->blocklist, &block->ccw_queue);
Hannes Reineckea2ace462013-01-30 09:26:14 +00002733 INIT_LIST_HEAD(&cqr->devlist);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002734 dasd_profile_start(block, cqr, req);
2735 }
2736}
2737
2738static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2739{
2740 struct request *req;
2741 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002742 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002743
2744 req = (struct request *) cqr->callback_data;
2745 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01002746 status = cqr->block->base->discipline->free_cp(cqr, req);
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002747 if (status < 0)
2748 error = status;
2749 else if (status == 0) {
2750 if (cqr->intrc == -EPERM)
2751 error = -EBADE;
2752 else if (cqr->intrc == -ENOLINK ||
2753 cqr->intrc == -ETIMEDOUT)
2754 error = cqr->intrc;
2755 else
2756 error = -EIO;
2757 }
Tejun Heo40cbbb72009-04-23 11:05:19 +09002758 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002759}
2760
2761/*
2762 * Process ccw request queue.
2763 */
2764static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2765 struct list_head *final_queue)
2766{
2767 struct list_head *l, *n;
2768 struct dasd_ccw_req *cqr;
2769 dasd_erp_fn_t erp_fn;
2770 unsigned long flags;
2771 struct dasd_device *base = block->base;
2772
2773restart:
2774 /* Process request with final status. */
2775 list_for_each_safe(l, n, &block->ccw_queue) {
2776 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2777 if (cqr->status != DASD_CQR_DONE &&
2778 cqr->status != DASD_CQR_FAILED &&
2779 cqr->status != DASD_CQR_NEED_ERP &&
2780 cqr->status != DASD_CQR_TERMINATED)
2781 continue;
2782
2783 if (cqr->status == DASD_CQR_TERMINATED) {
2784 base->discipline->handle_terminated_request(cqr);
2785 goto restart;
2786 }
2787
2788 /* Process requests that may be recovered */
2789 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01002790 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02002791 if (IS_ERR(erp_fn(cqr)))
2792 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002793 goto restart;
2794 }
2795
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01002796 /* log sense for fatal error */
2797 if (cqr->status == DASD_CQR_FAILED) {
2798 dasd_log_sense(cqr, &cqr->irb);
2799 }
2800
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002801 /* First of all call extended error reporting. */
2802 if (dasd_eer_enabled(base) &&
2803 cqr->status == DASD_CQR_FAILED) {
2804 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2805
2806 /* restart request */
2807 cqr->status = DASD_CQR_FILLED;
2808 cqr->retries = 255;
2809 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002810 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002811 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2812 flags);
2813 goto restart;
2814 }
2815
2816 /* Process finished ERP request. */
2817 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002818 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002819 goto restart;
2820 }
2821
2822 /* Rechain finished requests to final queue */
Heiko Carstens1aae0562013-01-30 09:49:40 +01002823 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002824 list_move_tail(&cqr->blocklist, final_queue);
2825 }
2826}
2827
2828static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2829{
2830 dasd_schedule_block_bh(cqr->block);
2831}
2832
2833static void __dasd_block_start_head(struct dasd_block *block)
2834{
2835 struct dasd_ccw_req *cqr;
2836
2837 if (list_empty(&block->ccw_queue))
2838 return;
2839 /* We allways begin with the first requests on the queue, as some
2840 * of previously started requests have to be enqueued on a
2841 * dasd_device again for error recovery.
2842 */
2843 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2844 if (cqr->status != DASD_CQR_FILLED)
2845 continue;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +01002846 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2847 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2848 cqr->status = DASD_CQR_FAILED;
2849 cqr->intrc = -EPERM;
2850 dasd_schedule_block_bh(block);
2851 continue;
2852 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002853 /* Non-temporary stop condition will trigger fail fast */
2854 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2855 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2856 (!dasd_eer_enabled(block->base))) {
2857 cqr->status = DASD_CQR_FAILED;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002858 cqr->intrc = -ENOLINK;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002859 dasd_schedule_block_bh(block);
2860 continue;
2861 }
2862 /* Don't try to start requests if device is stopped */
2863 if (block->base->stopped)
2864 return;
2865
2866 /* just a fail safe check, should not happen */
2867 if (!cqr->startdev)
2868 cqr->startdev = block->base;
2869
2870 /* make sure that the requests we submit find their way back */
2871 cqr->callback = dasd_return_cqr_cb;
2872
2873 dasd_add_request_tail(cqr);
2874 }
2875}
2876
2877/*
2878 * Central dasd_block layer routine. Takes requests from the generic
2879 * block layer request queue, creates ccw requests, enqueues them on
2880 * a dasd_device and processes ccw requests that have been returned.
2881 */
2882static void dasd_block_tasklet(struct dasd_block *block)
2883{
2884 struct list_head final_queue;
2885 struct list_head *l, *n;
2886 struct dasd_ccw_req *cqr;
2887
2888 atomic_set(&block->tasklet_scheduled, 0);
2889 INIT_LIST_HEAD(&final_queue);
2890 spin_lock(&block->queue_lock);
2891 /* Finish off requests on ccw queue */
2892 __dasd_process_block_ccw_queue(block, &final_queue);
2893 spin_unlock(&block->queue_lock);
2894 /* Now call the callback function of requests with final status */
2895 spin_lock_irq(&block->request_queue_lock);
2896 list_for_each_safe(l, n, &final_queue) {
2897 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2898 list_del_init(&cqr->blocklist);
2899 __dasd_cleanup_cqr(cqr);
2900 }
2901 spin_lock(&block->queue_lock);
2902 /* Get new request from the block device request queue */
2903 __dasd_process_request_queue(block);
2904 /* Now check if the head of the ccw queue needs to be started. */
2905 __dasd_block_start_head(block);
2906 spin_unlock(&block->queue_lock);
2907 spin_unlock_irq(&block->request_queue_lock);
Stefan Haberland4679e892012-06-19 17:30:12 +02002908 if (waitqueue_active(&shutdown_waitq))
2909 wake_up(&shutdown_waitq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002910 dasd_put_device(block->base);
2911}
2912
2913static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2914{
2915 wake_up(&dasd_flush_wq);
2916}
2917
2918/*
Stefan Haberlandc5576872013-04-15 16:41:31 +02002919 * Requeue a request back to the block request queue
2920 * only works for block requests
2921 */
2922static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2923{
2924 struct dasd_block *block = cqr->block;
2925 struct request *req;
2926 unsigned long flags;
2927
2928 if (!block)
2929 return -EINVAL;
2930 spin_lock_irqsave(&block->queue_lock, flags);
2931 req = (struct request *) cqr->callback_data;
2932 blk_requeue_request(block->request_queue, req);
2933 spin_unlock_irqrestore(&block->queue_lock, flags);
2934
2935 return 0;
2936}
2937
2938/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002939 * Go through all request on the dasd_block request queue, cancel them
2940 * on the respective dasd_device, and return them to the generic
2941 * block layer.
2942 */
2943static int dasd_flush_block_queue(struct dasd_block *block)
2944{
2945 struct dasd_ccw_req *cqr, *n;
2946 int rc, i;
2947 struct list_head flush_queue;
2948
2949 INIT_LIST_HEAD(&flush_queue);
2950 spin_lock_bh(&block->queue_lock);
2951 rc = 0;
2952restart:
2953 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2954 /* if this request currently owned by a dasd_device cancel it */
2955 if (cqr->status >= DASD_CQR_QUEUED)
2956 rc = dasd_cancel_req(cqr);
2957 if (rc < 0)
2958 break;
2959 /* Rechain request (including erp chain) so it won't be
2960 * touched by the dasd_block_tasklet anymore.
2961 * Replace the callback so we notice when the request
2962 * is returned from the dasd_device layer.
2963 */
2964 cqr->callback = _dasd_wake_block_flush_cb;
2965 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2966 list_move_tail(&cqr->blocklist, &flush_queue);
2967 if (i > 1)
2968 /* moved more than one request - need to restart */
2969 goto restart;
2970 }
2971 spin_unlock_bh(&block->queue_lock);
2972 /* Now call the callback function of flushed requests */
2973restart_cb:
2974 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2975 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2976 /* Process finished ERP request. */
2977 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002978 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002979 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002980 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002981 /* restart list_for_xx loop since dasd_process_erp
2982 * might remove multiple elements */
2983 goto restart_cb;
2984 }
2985 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002986 spin_lock_irq(&block->request_queue_lock);
Heiko Carstens1aae0562013-01-30 09:49:40 +01002987 cqr->endclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002988 list_del_init(&cqr->blocklist);
2989 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002990 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return rc;
2993}
2994
2995/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002996 * Schedules a call to dasd_tasklet over the device tasklet.
2997 */
2998void dasd_schedule_block_bh(struct dasd_block *block)
2999{
3000 /* Protect against rescheduling. */
3001 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3002 return;
3003 /* life cycle of block is bound to it's base device */
3004 dasd_get_device(block->base);
3005 tasklet_hi_schedule(&block->tasklet);
3006}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003007EXPORT_SYMBOL(dasd_schedule_block_bh);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003008
3009
3010/*
3011 * SECTION: external block device operations
3012 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
3014
3015/*
3016 * Dasd request queue function. Called from ll_rw_blk.c
3017 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003018static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003020 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003022 block = queue->queuedata;
3023 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003025 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003027 __dasd_block_start_head(block);
3028 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
3031/*
Hannes Reineckea2ace462013-01-30 09:26:14 +00003032 * Block timeout callback, called from the block layer
3033 *
3034 * request_queue lock is held on entry.
3035 *
3036 * Return values:
3037 * BLK_EH_RESET_TIMER if the request should be left running
3038 * BLK_EH_NOT_HANDLED if the request is handled or terminated
3039 * by the driver.
3040 */
3041enum blk_eh_timer_return dasd_times_out(struct request *req)
3042{
3043 struct dasd_ccw_req *cqr = req->completion_data;
3044 struct dasd_block *block = req->q->queuedata;
3045 struct dasd_device *device;
3046 int rc = 0;
3047
3048 if (!cqr)
3049 return BLK_EH_NOT_HANDLED;
3050
3051 device = cqr->startdev ? cqr->startdev : block->base;
Hannes Reinecke3d71ad32013-01-30 09:26:18 +00003052 if (!device->blk_timeout)
3053 return BLK_EH_RESET_TIMER;
Hannes Reineckea2ace462013-01-30 09:26:14 +00003054 DBF_DEV_EVENT(DBF_WARNING, device,
3055 " dasd_times_out cqr %p status %x",
3056 cqr, cqr->status);
3057
3058 spin_lock(&block->queue_lock);
3059 spin_lock(get_ccwdev_lock(device->cdev));
3060 cqr->retries = -1;
3061 cqr->intrc = -ETIMEDOUT;
3062 if (cqr->status >= DASD_CQR_QUEUED) {
3063 spin_unlock(get_ccwdev_lock(device->cdev));
3064 rc = dasd_cancel_req(cqr);
3065 } else if (cqr->status == DASD_CQR_FILLED ||
3066 cqr->status == DASD_CQR_NEED_ERP) {
3067 cqr->status = DASD_CQR_TERMINATED;
3068 spin_unlock(get_ccwdev_lock(device->cdev));
3069 } else if (cqr->status == DASD_CQR_IN_ERP) {
3070 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3071
3072 list_for_each_entry_safe(searchcqr, nextcqr,
3073 &block->ccw_queue, blocklist) {
3074 tmpcqr = searchcqr;
3075 while (tmpcqr->refers)
3076 tmpcqr = tmpcqr->refers;
3077 if (tmpcqr != cqr)
3078 continue;
3079 /* searchcqr is an ERP request for cqr */
3080 searchcqr->retries = -1;
3081 searchcqr->intrc = -ETIMEDOUT;
3082 if (searchcqr->status >= DASD_CQR_QUEUED) {
3083 spin_unlock(get_ccwdev_lock(device->cdev));
3084 rc = dasd_cancel_req(searchcqr);
3085 spin_lock(get_ccwdev_lock(device->cdev));
3086 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3087 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3088 searchcqr->status = DASD_CQR_TERMINATED;
3089 rc = 0;
3090 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3091 /*
3092 * Shouldn't happen; most recent ERP
3093 * request is at the front of queue
3094 */
3095 continue;
3096 }
3097 break;
3098 }
3099 spin_unlock(get_ccwdev_lock(device->cdev));
3100 }
3101 dasd_schedule_block_bh(block);
3102 spin_unlock(&block->queue_lock);
3103
3104 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3105}
3106
3107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * Allocate and initialize request queue and default I/O scheduler.
3109 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003110static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003112 block->request_queue = blk_init_queue(do_dasd_request,
3113 &block->request_queue_lock);
3114 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return -ENOMEM;
3116
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003117 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Stefan Haberlanda5fd8dd2015-03-04 14:29:47 +01003119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
3122/*
3123 * Allocate and initialize request queue.
3124 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003125static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
3127 int max;
3128
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003129 if (block->base->features & DASD_FEATURE_USERAW) {
3130 /*
3131 * the max_blocks value for raw_track access is 256
3132 * it is higher than the native ECKD value because we
3133 * only need one ccw per track
3134 * so the max_hw_sectors are
3135 * 2048 x 512B = 1024kB = 16 tracks
3136 */
3137 max = 2048;
3138 } else {
3139 max = block->base->discipline->max_blocks << block->s2b_shift;
3140 }
Christian Borntraeger9a212682015-09-04 13:52:44 +02003141 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, block->request_queue);
Stefan Haberland12d319b2016-02-12 14:50:52 +01003142 block->request_queue->limits.max_dev_sectors = max;
Stefan Haberlande4dbb0f2011-01-05 12:48:06 +01003143 blk_queue_logical_block_size(block->request_queue,
3144 block->bp_block);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05003145 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003146 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003147 /* with page sized segments we can translate each segement into
3148 * one idaw/tidaw
3149 */
3150 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3151 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
3154/*
3155 * Deactivate and free request queue.
3156 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003157static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003159 if (block->request_queue) {
3160 blk_cleanup_queue(block->request_queue);
3161 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163}
3164
3165/*
3166 * Flush request on the request queue.
3167 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003168static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct request *req;
3171
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003172 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return;
Horst Hummel138c0142006-06-29 14:58:12 +02003174
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003175 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09003176 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09003177 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003178 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Al Viro57a7c0b2008-03-02 10:36:08 -05003181static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Stefan Haberland9eb25122010-02-26 22:37:46 +01003183 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 int rc;
3185
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003186 base = dasd_device_from_gendisk(bdev->bd_disk);
3187 if (!base)
Stefan Haberland9eb25122010-02-26 22:37:46 +01003188 return -ENODEV;
3189
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003190 atomic_inc(&base->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003191 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 rc = -ENODEV;
3193 goto unlock;
3194 }
3195
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003196 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 rc = -EINVAL;
3198 goto unlock;
3199 }
3200
3201 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003202 dev_info(&base->cdev->dev,
3203 "Accessing the DASD failed because it is in "
3204 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 rc = -EPERM;
3206 goto out;
3207 }
3208
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003209 if (base->state <= DASD_STATE_BASIC) {
3210 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 " Cannot open unrecognized device");
3212 rc = -ENODEV;
3213 goto out;
3214 }
3215
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003216 if ((mode & FMODE_WRITE) &&
3217 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3218 (base->features & DASD_FEATURE_READONLY))) {
3219 rc = -EROFS;
3220 goto out;
3221 }
3222
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003223 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return 0;
3225
3226out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003227 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228unlock:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003229 atomic_dec(&base->block->open_count);
3230 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return rc;
3232}
3233
Al Virodb2a1442013-05-05 21:52:57 -04003234static void dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Al Virodb2a1442013-05-05 21:52:57 -04003236 struct dasd_device *base = dasd_device_from_gendisk(disk);
3237 if (base) {
3238 atomic_dec(&base->block->open_count);
3239 module_put(base->discipline->owner);
3240 dasd_put_device(base);
3241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003244/*
3245 * Return disk geometry.
3246 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003247static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003248{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003249 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003250
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003251 base = dasd_device_from_gendisk(bdev->bd_disk);
3252 if (!base)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003253 return -ENODEV;
3254
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003255 if (!base->discipline ||
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003256 !base->discipline->fill_geometry) {
3257 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003258 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +02003259 }
3260 base->discipline->fill_geometry(base->block, geo);
3261 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3262 dasd_put_device(base);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003263 return 0;
3264}
3265
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003266const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267dasd_device_operations = {
3268 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05003269 .open = dasd_open,
3270 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01003271 .ioctl = dasd_ioctl,
3272 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003273 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274};
3275
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003276/*******************************************************************************
3277 * end of block device operations
3278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280static void
3281dasd_exit(void)
3282{
3283#ifdef CONFIG_PROC_FS
3284 dasd_proc_exit();
3285#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003286 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07003287 if (dasd_page_cache != NULL) {
3288 kmem_cache_destroy(dasd_page_cache);
3289 dasd_page_cache = NULL;
3290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 dasd_gendisk_exit();
3292 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (dasd_debug_area != NULL) {
3294 debug_unregister(dasd_debug_area);
3295 dasd_debug_area = NULL;
3296 }
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02003297 dasd_statistics_removeroot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300/*
3301 * SECTION: common functions for ccw_driver use
3302 */
3303
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01003304/*
3305 * Is the device read-only?
3306 * Note that this function does not report the setting of the
3307 * readonly device attribute, but how it is configured in z/VM.
3308 */
3309int dasd_device_is_ro(struct dasd_device *device)
3310{
3311 struct ccw_dev_id dev_id;
3312 struct diag210 diag_data;
3313 int rc;
3314
3315 if (!MACHINE_IS_VM)
3316 return 0;
3317 ccw_device_get_id(device->cdev, &dev_id);
3318 memset(&diag_data, 0, sizeof(diag_data));
3319 diag_data.vrdcdvno = dev_id.devno;
3320 diag_data.vrdclen = sizeof(diag_data);
3321 rc = diag210(&diag_data);
3322 if (rc == 0 || rc == 2) {
3323 return diag_data.vrdcvfla & 0x80;
3324 } else {
3325 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3326 dev_id.devno, rc);
3327 return 0;
3328 }
3329}
3330EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3331
Cornelia Huckf3445a12009-04-14 15:36:23 +02003332static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3333{
3334 struct ccw_device *cdev = data;
3335 int ret;
3336
3337 ret = ccw_device_set_online(cdev);
3338 if (ret)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003339 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3340 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02003341}
3342
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003343/*
3344 * Initial attempt at a probe function. this can be simplified once
3345 * the other detection code is gone.
3346 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003347int dasd_generic_probe(struct ccw_device *cdev,
3348 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 int ret;
3351
3352 ret = dasd_add_sysfs_files(cdev);
3353 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01003354 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3355 "dasd_generic_probe: could not add "
3356 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02003357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Horst Hummel40545572006-06-29 15:08:18 +02003359 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Horst Hummel40545572006-06-29 15:08:18 +02003361 /*
3362 * Automatically online either all dasd devices (dasd_autodetect)
3363 * or all devices specified with dasd= parameters during
3364 * initial probe.
3365 */
3366 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02003367 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02003368 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01003369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003371EXPORT_SYMBOL_GPL(dasd_generic_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Stefan Haberlandc020d722016-09-20 10:42:38 +02003373void dasd_generic_free_discipline(struct dasd_device *device)
3374{
3375 /* Forget the discipline information. */
3376 if (device->discipline) {
3377 if (device->discipline->uncheck_device)
3378 device->discipline->uncheck_device(device);
3379 module_put(device->discipline->owner);
3380 device->discipline = NULL;
3381 }
3382 if (device->base_discipline) {
3383 module_put(device->base_discipline->owner);
3384 device->base_discipline = NULL;
3385 }
3386}
3387EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3388
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003389/*
3390 * This will one day be called from a global not_oper handler.
3391 * It is also used by driver_unregister during module unload.
3392 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003393void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
3395 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003396 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Horst Hummel59afda72005-05-16 21:53:39 -07003398 cdev->handler = NULL;
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 device = dasd_device_from_cdev(cdev);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003401 if (IS_ERR(device)) {
3402 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return;
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003404 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003405 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3406 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /* Already doing offline processing */
3408 dasd_put_device(device);
Stefan Weinhuberbe4904e2013-02-04 13:10:35 +01003409 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return;
3411 }
3412 /*
3413 * This device is removed unconditionally. Set offline
3414 * flag to prevent dasd_open from opening it while it is
3415 * no quite down yet.
3416 */
3417 dasd_set_target_state(device, DASD_STATE_NEW);
3418 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003419 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003421 /*
3422 * life cycle of block is bound to device, so delete it after
3423 * device was safely removed
3424 */
3425 if (block)
3426 dasd_free_block(block);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003427
3428 dasd_remove_sysfs_files(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003430EXPORT_SYMBOL_GPL(dasd_generic_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003432/*
3433 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08003435 * or the user has started activation through sysfs.
3436 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003437int dasd_generic_set_online(struct ccw_device *cdev,
3438 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003440 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003442 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07003443
Horst Hummel40545572006-06-29 15:08:18 +02003444 /* first online clears initial online feature flag */
3445 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 device = dasd_create_device(cdev);
3447 if (IS_ERR(device))
3448 return PTR_ERR(device);
3449
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003450 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07003451 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (!dasd_diag_discipline_pointer) {
Peter Oberparleiter7c53fcb2015-05-11 13:08:05 +02003453 /* Try to load the required module. */
3454 rc = request_module(DASD_DIAG_MOD);
3455 if (rc) {
3456 pr_warn("%s Setting the DASD online failed "
3457 "because the required module %s "
3458 "could not be loaded (rc=%d)\n",
3459 dev_name(&cdev->dev), DASD_DIAG_MOD,
3460 rc);
3461 dasd_delete_device(device);
3462 return -ENODEV;
3463 }
3464 }
3465 /* Module init could have failed, so check again here after
3466 * request_module(). */
3467 if (!dasd_diag_discipline_pointer) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003468 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3469 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 dasd_delete_device(device);
3471 return -ENODEV;
3472 }
3473 discipline = dasd_diag_discipline_pointer;
3474 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003475 if (!try_module_get(base_discipline->owner)) {
3476 dasd_delete_device(device);
3477 return -EINVAL;
3478 }
3479 if (!try_module_get(discipline->owner)) {
3480 module_put(base_discipline->owner);
3481 dasd_delete_device(device);
3482 return -EINVAL;
3483 }
3484 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 device->discipline = discipline;
3486
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003487 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 rc = discipline->check_device(device);
3489 if (rc) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003490 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3491 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08003492 module_put(discipline->owner);
3493 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 dasd_delete_device(device);
3495 return rc;
3496 }
3497
3498 dasd_set_target_state(device, DASD_STATE_ONLINE);
3499 if (device->state <= DASD_STATE_KNOWN) {
Fabian Frederick4ce25962014-06-26 19:41:48 +02003500 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3501 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 rc = -ENODEV;
3503 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003504 if (device->block)
3505 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 dasd_delete_device(device);
3507 } else
3508 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02003509 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01003510
3511 wait_event(dasd_init_waitq, _wait_for_device(device));
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 return rc;
3515}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003516EXPORT_SYMBOL_GPL(dasd_generic_set_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003518int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003521 struct dasd_block *block;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003522 int max_count, open_count, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003524 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 device = dasd_device_from_cdev(cdev);
3526 if (IS_ERR(device))
3527 return PTR_ERR(device);
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /*
3530 * We must make sure that this device is currently not in use.
3531 * The open_count is increased for every opener, that includes
3532 * the blkdev_get in dasd_scan_partitions. We are only interested
3533 * in the other openers.
3534 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003535 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02003536 max_count = device->block->bdev ? 0 : -1;
3537 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003538 if (open_count > max_count) {
3539 if (open_count > 0)
Fabian Frederick4ce25962014-06-26 19:41:48 +02003540 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3541 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003542 else
Fabian Frederick4ce25962014-06-26 19:41:48 +02003543 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3544 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003545 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3546 dasd_put_device(device);
3547 return -EBUSY;
3548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003550
3551 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3552 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +01003553 * safe offline already running
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003554 * could only be called by normal offline so safe_offline flag
3555 * needs to be removed to run normal offline and kill all I/O
3556 */
3557 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3558 /* Already doing normal offline processing */
3559 dasd_put_device(device);
3560 return -EBUSY;
3561 } else
3562 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3563
3564 } else
3565 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3566 /* Already doing offline processing */
3567 dasd_put_device(device);
3568 return -EBUSY;
3569 }
3570
3571 /*
3572 * if safe_offline called set safe_offline_running flag and
3573 * clear safe_offline so that a call to normal offline
3574 * can overrun safe_offline processing
3575 */
3576 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3577 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3578 /*
3579 * If we want to set the device safe offline all IO operations
3580 * should be finished before continuing the offline process
3581 * so sync bdev first and then wait for our queues to become
3582 * empty
3583 */
3584 /* sync blockdev and partitions */
3585 rc = fsync_bdev(device->block->bdev);
3586 if (rc != 0)
3587 goto interrupted;
3588
3589 /* schedule device tasklet and wait for completion */
3590 dasd_schedule_device_bh(device);
3591 rc = wait_event_interruptible(shutdown_waitq,
3592 _wait_for_empty_queues(device));
3593 if (rc != 0)
3594 goto interrupted;
3595 }
3596
3597 set_bit(DASD_FLAG_OFFLINE, &device->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 dasd_set_target_state(device, DASD_STATE_NEW);
3599 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003600 block = device->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003602 /*
3603 * life cycle of block is bound to device, so delete it after
3604 * device was safely removed
3605 */
3606 if (block)
3607 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 return 0;
Stefan Haberlandd07dc5d2012-11-28 13:43:38 +01003609
3610interrupted:
3611 /* interrupted by signal */
3612 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3613 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3614 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3615 dasd_put_device(device);
3616 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003618EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003620int dasd_generic_last_path_gone(struct dasd_device *device)
3621{
3622 struct dasd_ccw_req *cqr;
3623
3624 dev_warn(&device->cdev->dev, "No operational channel path is left "
3625 "for the device\n");
3626 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3627 /* First of all call extended error reporting. */
3628 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3629
3630 if (device->state < DASD_STATE_BASIC)
3631 return 0;
3632 /* Device is active. We want to keep it. */
3633 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3634 if ((cqr->status == DASD_CQR_IN_IO) ||
3635 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3636 cqr->status = DASD_CQR_QUEUED;
3637 cqr->retries++;
3638 }
3639 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3640 dasd_device_clear_timer(device);
3641 dasd_schedule_device_bh(device);
3642 return 1;
3643}
3644EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3645
3646int dasd_generic_path_operational(struct dasd_device *device)
3647{
3648 dev_info(&device->cdev->dev, "A channel path to the device has become "
3649 "operational\n");
3650 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3651 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3652 if (device->stopped & DASD_UNRESUMED_PM) {
3653 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3654 dasd_restore_device(device);
3655 return 1;
3656 }
3657 dasd_schedule_device_bh(device);
3658 if (device->block)
3659 dasd_schedule_block_bh(device->block);
Stefan Haberland931a3dc2014-07-18 14:22:41 +02003660
3661 if (!device->stopped)
3662 wake_up(&generic_waitq);
3663
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003664 return 1;
3665}
3666EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3667
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003668int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 struct dasd_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 int ret;
3672
Peter Oberparleiter91c36912008-08-21 19:46:39 +02003673 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (IS_ERR(device))
3675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 ret = 0;
3677 switch (event) {
3678 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02003679 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 case CIO_NO_PATH:
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003681 device->path_data.opm = 0;
3682 device->path_data.ppm = 0;
3683 device->path_data.npm = 0;
3684 ret = dasd_generic_last_path_gone(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 break;
3686 case CIO_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 ret = 1;
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003688 if (device->path_data.opm)
3689 ret = dasd_generic_path_operational(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 break;
3691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 dasd_put_device(device);
3693 return ret;
3694}
Fabian Frederick7048a2b2014-06-26 19:41:47 +02003695EXPORT_SYMBOL_GPL(dasd_generic_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003697void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3698{
3699 int chp;
3700 __u8 oldopm, eventlpm;
3701 struct dasd_device *device;
3702
3703 device = dasd_device_from_cdev_locked(cdev);
3704 if (IS_ERR(device))
3705 return;
3706 for (chp = 0; chp < 8; chp++) {
3707 eventlpm = 0x80 >> chp;
3708 if (path_event[chp] & PE_PATH_GONE) {
3709 oldopm = device->path_data.opm;
3710 device->path_data.opm &= ~eventlpm;
3711 device->path_data.ppm &= ~eventlpm;
3712 device->path_data.npm &= ~eventlpm;
Stefan Weinhuber8b811ba2013-05-28 15:26:06 +02003713 if (oldopm && !device->path_data.opm) {
3714 dev_warn(&device->cdev->dev,
3715 "No verified channel paths remain "
3716 "for the device\n");
3717 DBF_DEV_EVENT(DBF_WARNING, device,
3718 "%s", "last verified path gone");
3719 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3720 dasd_device_set_stop_bits(device,
3721 DASD_STOPPED_DC_WAIT);
3722 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003723 }
3724 if (path_event[chp] & PE_PATH_AVAILABLE) {
3725 device->path_data.opm &= ~eventlpm;
3726 device->path_data.ppm &= ~eventlpm;
3727 device->path_data.npm &= ~eventlpm;
3728 device->path_data.tbvpm |= eventlpm;
3729 dasd_schedule_device_bh(device);
3730 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003731 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
Stefan Haberland12d7b102012-09-11 15:10:58 +02003732 if (!(device->path_data.opm & eventlpm) &&
3733 !(device->path_data.tbvpm & eventlpm)) {
3734 /*
3735 * we can not establish a pathgroup on an
3736 * unavailable path, so trigger a path
3737 * verification first
3738 */
3739 device->path_data.tbvpm |= eventlpm;
3740 dasd_schedule_device_bh(device);
3741 }
Stefan Haberlandf1633032012-01-18 18:03:41 +01003742 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3743 "Pathgroup re-established\n");
3744 if (device->discipline->kick_validate)
3745 device->discipline->kick_validate(device);
3746 }
Stefan Weinhubera4d26c62011-01-05 12:48:03 +01003747 }
3748 dasd_put_device(device);
3749}
3750EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3751
3752int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3753{
3754 if (!device->path_data.opm && lpm) {
3755 device->path_data.opm = lpm;
3756 dasd_generic_path_operational(device);
3757 } else
3758 device->path_data.opm |= lpm;
3759 return 0;
3760}
3761EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3762
3763
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003764int dasd_generic_pm_freeze(struct ccw_device *cdev)
3765{
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003766 struct dasd_device *device = dasd_device_from_cdev(cdev);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003767 struct list_head freeze_queue;
3768 struct dasd_ccw_req *cqr, *n;
3769 struct dasd_ccw_req *refers;
3770 int rc;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003771
3772 if (IS_ERR(device))
3773 return PTR_ERR(device);
Stefan Haberland6f272b92011-01-05 12:48:05 +01003774
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003775 /* mark device as suspended */
3776 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3777
Stefan Haberland6f272b92011-01-05 12:48:05 +01003778 if (device->discipline->freeze)
3779 rc = device->discipline->freeze(device);
3780
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003781 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003782 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandc5576872013-04-15 16:41:31 +02003783
3784 /* clear active requests and requeue them to block layer if possible */
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003785 INIT_LIST_HEAD(&freeze_queue);
3786 spin_lock_irq(get_ccwdev_lock(cdev));
3787 rc = 0;
3788 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3789 /* Check status and move request to flush_queue */
3790 if (cqr->status == DASD_CQR_IN_IO) {
3791 rc = device->discipline->term_IO(cqr);
3792 if (rc) {
3793 /* unable to terminate requeust */
3794 dev_err(&device->cdev->dev,
3795 "Unable to terminate request %p "
3796 "on suspend\n", cqr);
3797 spin_unlock_irq(get_ccwdev_lock(cdev));
3798 dasd_put_device(device);
3799 return rc;
3800 }
3801 }
3802 list_move_tail(&cqr->devlist, &freeze_queue);
3803 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003804 spin_unlock_irq(get_ccwdev_lock(cdev));
3805
3806 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3807 wait_event(dasd_flush_wq,
3808 (cqr->status != DASD_CQR_CLEAR_PENDING));
3809 if (cqr->status == DASD_CQR_CLEARED)
3810 cqr->status = DASD_CQR_QUEUED;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003811
Stefan Haberlandc5576872013-04-15 16:41:31 +02003812 /* requeue requests to blocklayer will only work for
3813 block device requests */
3814 if (_dasd_requeue_request(cqr))
3815 continue;
3816
3817 /* remove requests from device and block queue */
3818 list_del_init(&cqr->devlist);
3819 while (cqr->refers != NULL) {
3820 refers = cqr->refers;
3821 /* remove the request from the block queue */
3822 list_del(&cqr->blocklist);
3823 /* free the finished erp request */
3824 dasd_free_erp_request(cqr, cqr->memdev);
3825 cqr = refers;
3826 }
3827 if (cqr->block)
3828 list_del_init(&cqr->blocklist);
3829 cqr->block->base->discipline->free_cp(
3830 cqr, (struct request *) cqr->callback_data);
3831 }
3832
3833 /*
3834 * if requests remain then they are internal request
3835 * and go back to the device queue
3836 */
3837 if (!list_empty(&freeze_queue)) {
3838 /* move freeze_queue to start of the ccw_queue */
3839 spin_lock_irq(get_ccwdev_lock(cdev));
3840 list_splice_tail(&freeze_queue, &device->ccw_queue);
3841 spin_unlock_irq(get_ccwdev_lock(cdev));
3842 }
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003843 dasd_put_device(device);
3844 return rc;
3845}
3846EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3847
3848int dasd_generic_restore_device(struct ccw_device *cdev)
3849{
3850 struct dasd_device *device = dasd_device_from_cdev(cdev);
3851 int rc = 0;
3852
3853 if (IS_ERR(device))
3854 return PTR_ERR(device);
3855
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003856 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003857 dasd_device_remove_stop_bits(device,
3858 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003859
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003860 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003861
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003862 /*
3863 * call discipline restore function
3864 * if device is stopped do nothing e.g. for disconnected devices
3865 */
3866 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003867 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003868 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003869 /*
3870 * if the resume failed for the DASD we put it in
3871 * an UNRESUMED stop state
3872 */
3873 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003874
Stefan Haberland6fca97a2009-10-06 10:34:15 +02003875 if (device->block)
3876 dasd_schedule_block_bh(device->block);
3877
Stefan Haberlandc8d1c0f2011-10-30 15:17:09 +01003878 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003879 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02003880 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02003881}
3882EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3883
Heiko Carstens763968e2007-05-10 15:45:46 +02003884static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3885 void *rdc_buffer,
3886 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02003887 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02003888{
3889 struct dasd_ccw_req *cqr;
3890 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003891 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02003892
3893 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3894
3895 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01003896 /* internal error 13 - Allocating the RDC request failed*/
3897 dev_err(&device->cdev->dev,
3898 "An error occurred in the DASD device driver, "
3899 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02003900 return cqr;
3901 }
3902
3903 ccw = cqr->cpaddr;
3904 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003905 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3906 idaw = (unsigned long *) (cqr->data);
3907 ccw->cda = (__u32)(addr_t) idaw;
3908 ccw->flags = CCW_FLAG_IDA;
3909 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3910 } else {
3911 ccw->cda = (__u32)(addr_t) rdc_buffer;
3912 ccw->flags = 0;
3913 }
Cornelia Huck17283b52007-05-04 18:47:51 +02003914
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02003915 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003916 cqr->startdev = device;
3917 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02003918 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01003919 cqr->retries = 256;
Heiko Carstens1aae0562013-01-30 09:49:40 +01003920 cqr->buildclk = get_tod_clock();
Cornelia Huck17283b52007-05-04 18:47:51 +02003921 cqr->status = DASD_CQR_FILLED;
3922 return cqr;
3923}
3924
3925
Stefan Haberland68b781f2009-09-11 10:28:29 +02003926int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02003927 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02003928{
3929 int ret;
3930 struct dasd_ccw_req *cqr;
3931
Sebastian Ott92636b12009-06-12 10:26:37 +02003932 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02003933 magic);
3934 if (IS_ERR(cqr))
3935 return PTR_ERR(cqr);
3936
3937 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003938 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02003939 return ret;
3940}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02003941EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08003942
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01003943/*
3944 * In command mode and transport mode we need to look for sense
3945 * data in different places. The sense data itself is allways
3946 * an array of 32 bytes, so we can unify the sense data access
3947 * for both modes.
3948 */
3949char *dasd_get_sense(struct irb *irb)
3950{
3951 struct tsb *tsb = NULL;
3952 char *sense = NULL;
3953
3954 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3955 if (irb->scsw.tm.tcw)
3956 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3957 irb->scsw.tm.tcw);
3958 if (tsb && tsb->length == 64 && tsb->flags)
3959 switch (tsb->flags & 0x07) {
3960 case 1: /* tsa_iostat */
3961 sense = tsb->tsa.iostat.sense;
3962 break;
3963 case 2: /* tsa_ddpc */
3964 sense = tsb->tsa.ddpc.sense;
3965 break;
3966 default:
3967 /* currently we don't use interrogate data */
3968 break;
3969 }
3970 } else if (irb->esw.esw0.erw.cons) {
3971 sense = irb->ecw;
3972 }
3973 return sense;
3974}
3975EXPORT_SYMBOL_GPL(dasd_get_sense);
3976
Stefan Haberland4679e892012-06-19 17:30:12 +02003977void dasd_generic_shutdown(struct ccw_device *cdev)
3978{
3979 struct dasd_device *device;
3980
3981 device = dasd_device_from_cdev(cdev);
3982 if (IS_ERR(device))
3983 return;
3984
3985 if (device->block)
3986 dasd_schedule_block_bh(device->block);
3987
3988 dasd_schedule_device_bh(device);
3989
3990 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3991}
3992EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3993
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01003994static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
3996 int rc;
3997
3998 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02003999 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02004000 init_waitqueue_head(&generic_waitq);
Stefan Haberland4679e892012-06-19 17:30:12 +02004001 init_waitqueue_head(&shutdown_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
4003 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01004004 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 if (dasd_debug_area == NULL) {
4006 rc = -ENOMEM;
4007 goto failed;
4008 }
4009 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02004010 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
4012 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4013
4014 dasd_diag_discipline_pointer = NULL;
4015
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +02004016 dasd_statistics_createroot();
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 rc = dasd_devmap_init();
4019 if (rc)
4020 goto failed;
4021 rc = dasd_gendisk_init();
4022 if (rc)
4023 goto failed;
4024 rc = dasd_parse();
4025 if (rc)
4026 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08004027 rc = dasd_eer_init();
4028 if (rc)
4029 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030#ifdef CONFIG_PROC_FS
4031 rc = dasd_proc_init();
4032 if (rc)
4033 goto failed;
4034#endif
4035
4036 return 0;
4037failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01004038 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 dasd_exit();
4040 return rc;
4041}
4042
4043module_init(dasd_init);
4044module_exit(dasd_exit);